Example #1
0
 def __init__(self, device_id):
     self.device_id = device_id
     self.abi = self.get_abi()
     if self.is_mnt_existed():
         logger.info("minitouch already existed in {}".format(device_id))
     else:
         self.download_target_mnt()
Example #2
0
 def publish(self, connection):
     """ apply current commands (_content), to your device """
     self.commit()
     final_content = self._content
     logger.info("send operation: {}".format(
         final_content.replace("\n", "\\n")))
     connection.send(final_content)
     time.sleep(self._delay / 1000 + config.DEFAULT_DELAY)
     self.reset()
Example #3
0
def is_android_device_connected_by_adb(device_id):
    """ return True if device connected, else return False """
    try:
        device_name = subprocess.check_output(
            ['adb', "-s", device_id, "shell", "getprop", "ro.product.model"])
        device_name = (device_name.decode('utf-8').replace("\n", "").replace(
            "\r", ""))
        logger.info("device {} online".format(device_name))
    except subprocess.CalledProcessError:
        return False
    return True
Example #4
0
def is_device_connected(device_id):
    """ return True if device connected, else return False """
    _ADB = config.ADB_EXECUTOR
    try:
        device_name = subprocess.check_output(
            [_ADB, "-s", device_id, "shell", "getprop", "ro.product.model"])
        device_name = (device_name.decode(config.DEFAULT_CHARSET).replace(
            "\n", "").replace("\r", ""))
        logger.info("device {} online".format(device_name))
    except subprocess.CalledProcessError:
        return False
    return True
Example #5
0
 def _start_mnt(self):
     """ fork a process to start minitouch on android """
     command_list = [
         _ADB,
         "-s",
         self.device_id,
         "shell",
         "/data/local/tmp/minitouch",
     ]
     logger.info("start minitouch: {}".format(" ".join(command_list)))
     self.mnt_process = subprocess.Popen(command_list, stdout=subprocess.DEVNULL)
     logger.info("start minitouch: {}".format(self.mnt_process))
Example #6
0
def adb_connect_android_device(device_id):
    """ return True if device connected, else return False """
    try:
        device_name = subprocess.check_output(['adb', "connect", device_id])
        device_name = (device_name.decode('utf-8').replace("\n", "").replace(
            "\r", ""))
        logger.info("device {} online".format(device_name))
        if 'cannot' in device_name:
            logger.info(device_name)
            return False
        return True
    except subprocess.CalledProcessError:
        return False
Example #7
0
 def adb_push(self, device_id, remote_path):
     try:
         dst = "/data/local/tmp/tmp-{}.apk".format(int(time.time() * 1000))
         device_name = subprocess.check_output([
             self._ADB, "-s", device_id, "push", "{}".format(remote_path),
             "{}".format(dst)
         ])
         device_name = (device_name.decode('utf-8').replace("\n",
                                                            "").replace(
                                                                "\r", ""))
         logger.info(device_name)
         return dst
     except subprocess.CalledProcessError:
         return False
Example #8
0
 def adb_send_keys(self, device_id, content):
     """ 暂时不支持中文,支持中文需要ADB keyboard https://github.com/senzhk/ADBKeyBoard """
     try:
         device_name = subprocess.check_output([
             self._ADB, "-s", device_id, "shell", "input", "text",
             "{}".format(content)
         ])
         logger.info(device_name)
         device_name = (device_name.decode('utf-8').replace("\n",
                                                            "").replace(
                                                                "\r", ""))
         logger.info(device_name)
     except subprocess.CalledProcessError:
         return False
     return True
Example #9
0
    def download_target_mnt(self):
        abi = self.get_abi()
        target_url = "{}/{}/bin/minitouch".format(config.MNT_PREBUILT_URL, abi)
        logger.info("target minitouch url: " + target_url)
        mnt_path = download_file(target_url)

        # push and grant
        subprocess.check_call(
            [_ADB, "-s", self.device_id, "push", mnt_path, config.MNT_HOME]
        )
        subprocess.check_call(
            [_ADB, "-s", self.device_id, "shell", "chmod", "777", config.MNT_HOME]
        )
        logger.info("minitouch already installed in {}".format(config.MNT_HOME))

        # remove temp
        os.remove(mnt_path)
Example #10
0
 def install_apk(self, device_id, apk_path):
     # -g :为应用程序授予所有运行时的权限  -t :允许测试包 -r:覆盖安装
     try:
         device_name = subprocess.check_output([
             self._ADB, "-s", device_id, "install", "-r", "-t", "-g",
             "{}".format(apk_path)
         ])
         device_name = (device_name.decode('utf-8').replace("\n",
                                                            "").replace(
                                                                "\r", ""))
         logger.info(device_name)
         if 'Success' in device_name:
             return True
         else:
             return False
     except subprocess.CalledProcessError:
         return False
Example #11
0
    def __init__(self, device_id):
        assert is_device_connected(device_id)

        self.device_id = device_id
        logger.info("searching a usable port ...")
        self.port = self._get_port()
        logger.info("device {} bind to port {}".format(device_id, self.port))

        # check minitouch
        self.installer = MNTInstaller(device_id)

        # keep minitouch alive
        self._forward_port()
        self.mnt_process = None
        self._start_mnt()

        # make sure it's up
        time.sleep(1)
Example #12
0
    def adb_install_local(self, device_id, remote_path):
        local_path = self.adb_push(device_id, remote_path)
        try:
            device_name = subprocess.check_output([
                self._ADB, "-s", device_id, "shell", "pm", "install", "-r",
                "-t", "-g", "{}".format(local_path)
            ])
            device_name = (device_name.decode('utf-8').replace("\n",
                                                               "").replace(
                                                                   "\r", ""))
            logger.info(device_name)

        except subprocess.CalledProcessError:
            return False
        finally:
            subprocess.check_output([
                self._ADB, "-s", device_id, "shell", "rm",
                "{}".format(local_path)
            ])
Example #13
0
    def __init__(self, port):
        self.port = port

        # build connection
        client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        client.connect((self._DEFAULT_HOST, self.port))
        self.client = client

        # get minitouch server info
        socket_out = client.makefile()

        # v <version>
        # protocol version, usually it is 1. needn't use this
        socket_out.readline()

        # ^ <max-contacts> <max-x> <max-y> <max-pressure>
        _, max_contacts, max_x, max_y, max_pressure, *_ = (
            socket_out.readline().replace("\n", "").replace("\r", "").split(" ")
        )
        self.max_contacts = max_contacts
        self.max_x = max_x
        self.max_y = max_y
        self.max_pressure = max_pressure

        # $ <pid>
        _, pid = socket_out.readline().replace("\n", "").replace("\r", "").split(" ")
        self.pid = pid

        logger.info(
            "minitouch running on port: {}, pid: {}".format(self.port, self.pid)
        )
        logger.info(
            "max_contact: {}; max_x: {}; max_y: {}; max_pressure: {}".format(
                max_contacts, max_x, max_y, max_pressure
            )
        )
Example #14
0
 def get_abi(self):
     abi = subprocess.getoutput(
         "{} -s {} shell getprop ro.product.cpu.abi".format(_ADB, self.device_id)
     )
     logger.info("device {} is {}".format(self.device_id, abi))
     return abi.strip()
Example #15
0
 def send(self, content):
     """ send message and get its response """
     byte_content = str2byte(content)
     self.client.sendall(byte_content)
     logger.info("touch: {}".format(byte_content))
     return self.client.recv(self._DEFAULT_BUFFER_SIZE)
Example #16
0
 def disconnect(self):
     self.client and self.client.close()
     self.client = None
     logger.info("minitouch disconnected")
Example #17
0
 def stop(self):
     self.mnt_process and self.mnt_process.kill()
     self._PORT_SET.add(self.port)
     logger.info("device {} unbind to {}".format(self.device_id, self.port))