import logging
import socket
import subprocess
import time
import json
import struct
import os
from adapter import Adapter

DROIDBOT_APP_REMOTE_ADDR = "tcp:7336"
DROIDBOT_APP_PACKAGE = "io.github.ylimit.droidbotapp"
DROIDBOT_APP_PACKET_HEAD_LEN = 6
ACCESSIBILITY_SERVICE = DROIDBOT_APP_PACKAGE + "/io.github.privacystreams.accessibility.PSAccessibilityService"

MAX_NUM_GET_VIEWS = 5
GET_VIEW_WAIT_TIME = 1
MAX_RESTARTED_TIMES = 100

class DroidBotAppConnException(Exception):
    """
    Exception in telnet connection
    """
    pass

class EOF(Exception):
    """
    Exception in telnet connection
    """
    pass

class DroidBotAppConn(Adapter):
    """
    a connection with droidbot app.
    """
    def __init__(self, device=None):
        """
        initiate a droidbot app connection
        :param device: instance of Device
        :return:
        """
        self.logger = logging.getLogger(self.__class__.__name__)
        self.host = "localhost"
        if device is None:
            from droidbot.device import Device
            device = Device()