Esempio n. 1
0
    def startActivity(self, activity):
        """Starts the specified activity on the device"""
        if self.state != AndroidDevice.STATE_STARTED:
            raise Exception("Cannot start an activity since the device is not started.")

        if activity is None or len(activity)==0:
            raise Exception("Cannot start an activity that has no name.")

        if not self.__checkADBRecognizeDevice():
            # self.__restartADBServer() # We cannot do that if we have multiple emulators...
            raise Exception("ADB didn't find {0}".format(self.name))

        self._logger.info("Starting activity {0} on device {1}".format(activity, self.name))

        activityPackage = '.'.join(activity.split('.')[:-1])
        activityName = ''.join(activity.split('.')[-1:])
                
        # $ adb shell am start -n activityPackage/activity
        cmd = [
            self.mainConfiguration.adbPath,
            "-s",
            self.serialNumber,
            "shell",
            "am",
            "start",
            "-n",
            "{0}/.{1}".format(activityPackage, activityName)
        ]
        OSCommand.executeAsyncCommand(cmd)
Esempio n. 2
0
    def startActivityFromPackage(self, packageName, activityName):
        """
        Starts the specified activity from the specified package name on the device.
        This method has to be called when package name is different from main activity.
        """
        if self.state != AndroidDevice.STATE_STARTED:
            raise Exception("Cannot start an activity since the device is not started.")

        if activityName is None or len(activityName)==0:
            raise Exception("Activity name is null.")

        if packageName is None or len(packageName)==0:
            raise Exception("Package name is null.")

        if not self.__checkADBRecognizeDevice():
            # self.__restartADBServer() # We cannot do that if we have multiple emulators...
            raise Exception("ADB didn't find {0}".format(self.name))
            
        self._logger.info("Starting activity {0}/{1} on device {2}".format(packageName, activityName, self.name))

        # $ adb shell am start -n activityPackage/activity
        cmd = [
            self.mainConfiguration.adbPath,
            "-s",
            self.serialNumber,
            "shell",
            "am",
            "start",
            "-n",
            "{0}/{1}".format(packageName, activityName)
        ]
        OSCommand.executeAsyncCommand(cmd)
Esempio n. 3
0
    def startActivityFromPackage(self, packageName, activityName):
        """
        Starts the specified activity from the specified package name on the emulator.
        This method has to be called when package name is different from main activity.
        """

        if self.state != AVDEmulator.STATE_STARTED:
            raise Exception("Cannot start an activity since the emulator is not started.")

        if activityName is None or len(activityName)==0:
            raise Exception("Activity name is null.")

        if packageName is None or len(packageName)==0:
            raise Exception("Package name is null.")

        if not self.__checkADBRecognizeEmu():
            # self.__restartADBServer() # We cannot do that if we have multiple emulators...
            raise Exception("ADB didn't find {0}".format(self.name))
            
        self._logger.info("Starting activity {0}/{1} on emulator {2}".format(packageName, activityName, self.name))

        # $ adb shell am start -n activityPackage/activity
        cmd = [
            self.mainConfiguration.adbPath,
            "-s",
            self.emulatorSerialNumber,
            "shell",
            "am",
            "start",
            "-n",
            "{0}/{1}".format(packageName, activityName)
        ]
        
        OSCommand.executeAsyncCommand(cmd)
Esempio n. 4
0
    def startActivity(self, activity):
        """Starts the specified activity on the emulator"""

        if self.state != AVDEmulator.STATE_STARTED:
            raise Exception("Cannot start an activity since the emulator is not started.")

        if activity is None or len(activity)==0:
            raise Exception("Cannot start an activity that has no name.")

        if not self.__checkADBRecognizeEmu():
            # self.__restartADBServer() # We cannot do that if we have multiple emulators...
            raise Exception("ADB didn't find {0}".format(self.name))

        self._logger.info("Starting activity {0} on emulator {1}".format(activity, self.name))

        activityPackage = '.'.join(activity.split('.')[:-1])
        activityName = ''.join(activity.split('.')[-1:])
                
        # $ adb shell am start -n activityPackage/activity
        cmd = [
            self.mainConfiguration.adbPath,
            "-s",
            self.emulatorSerialNumber,
            "shell",
            "am",
            "start",
            "-n",
            "{0}/.{1}".format(activityPackage, activityName)
        ]
        
        OSCommand.executeAsyncCommand(cmd)
Esempio n. 5
0
    def stimulateWithMonkey(self, packageName):
        """Stimulates application with monkey"""

        if self.state != AVDEmulator.STATE_STARTED:
            raise Exception("Emulator is not started.")

        if packageName is None or len(packageName)==0:
            raise Exception("Cannot stimulate package that has no name.")

        self._logger.info("Stimulating package {0} with monkey.".format(packageName))
        cmd = [
            self.mainConfiguration.adbPath,
            "-s",
            self.emulatorSerialNumber,
            "shell",
            "monkey",
            "-p",
            packageName,
            "-v",
            "500",
            "--throttle",
            "6000",
            "--ignore-timeouts"
        ]
        OSCommand.executeAsyncCommand(cmd) 
Esempio n. 6
0
 def __pullResults(self):
     """Pull results of analysis"""
     self._logger.info("Pulling results of analysis")
     cmd = [
         self.mainConfiguration.adbPath, "-s", self.serialNumber, "pull",
         "/sdcard/hooker/events.logs", "{0}{1}-events.logs".format(
             self.mainConfiguration.androidTemporaryPath,
             datetime.datetime.now().strftime("%Y-%m-%d-%H:%M"))
     ]
     OSCommand.executeAsyncCommand(cmd)
     self._logger.info("Event logs has been pulled in {0}".format(
         self.mainConfiguration.androidTemporaryPath))
Esempio n. 7
0
    def rebootAVD(self):
        """Reboot the emulator"""
        self._logger.info("Rebooting AVD listening on port {0}".format(
            self.emulatorSerialNumber))
        cmd = [
            self.mainConfiguration.adbPath, "-s", self.emulatorSerialNumber,
            "shell", "setprop", "ctl.restart", "zygote"
        ]
        OSCommand.executeAsyncCommand(cmd)

        self.state = AVDEmulator.STATE_STARTING
        # waits for device to be ready
        self.__waitForDeviceToBeReady()
Esempio n. 8
0
 def reboot(self):
     """Reboot the device"""
     self._logger.info("Rebooting device listening on port {0}".format(
         self.serialNumber))
     cmd = [
         self.mainConfiguration.adbPath, "-s", self.serialNumber, "reboot"
     ]
     OSCommand.executeAsyncCommand(cmd)
     # Real device can take time to reboot
     time.sleep(15)
     # waits for device to be ready
     self._waitForDeviceToBeReady()
     self.state = AndroidDevice.STATE_STARTING
Esempio n. 9
0
    def stimulateWithMonkey(self, packageName):
        """Stimulates application with monkey"""

        if packageName is None or len(packageName) == 0:
            raise Exception("Cannot stimulate package that has no name.")

        self._logger.info(
            "Stimulating package {0} with monkey.".format(packageName))
        cmd = [
            self.mainConfiguration.adbPath, "-s", self.serialNumber, "shell",
            "monkey", "-p", packageName, "-v", "500", "--throttle", "6000",
            "--ignore-timeouts"
        ]
        OSCommand.executeAsyncCommand(cmd)
Esempio n. 10
0
 def __pullResults(self):
     """Pull results of analysis"""
     self._logger.info("Pulling results of analysis")
     cmd = [
         self.mainConfiguration.adbPath,
         "-s",
         self.serialNumber,
         "pull",
         "/sdcard/hooker/events.logs",
         "{0}{1}-events.logs".format(self.mainConfiguration.androidTemporaryPath,
                         datetime.datetime.now().strftime("%Y-%m-%d-%H:%M"))
     ]
     OSCommand.executeAsyncCommand(cmd)
     self._logger.info("Event logs has been pulled in {0}".format(self.mainConfiguration.androidTemporaryPath))
Esempio n. 11
0
 def reboot(self):
     """Reboot the device"""
     self._logger.info("Rebooting device listening on port {0}".format(self.serialNumber))
     cmd = [
         self.mainConfiguration.adbPath,
         "-s",
         self.serialNumber,
         "reboot"
     ]
     OSCommand.executeAsyncCommand(cmd)
     # Real device can take time to reboot
     time.sleep(15)
     # waits for device to be ready
     self._waitForDeviceToBeReady()
     self.state = AndroidDevice.STATE_STARTING            
Esempio n. 12
0
    def start(self):
        """Starts the emulator"""
        if self.state != AndroidDevice.STATE_PREPARED:
            raise Exception(
                "Cannot start the emulator. (expected state was {0}, current state is {1})"
                .format(AndroidDevice.STATE_PREPARED, self.state))

        # clean the temporary directory
        self.__cleanTemporaryDirectory()

        cmd = [
            self.mainConfiguration.emulatorPath, "@{0}".format(self.name),
            "-no-snapshot-save", "-netspeed", "full", "-netdelay", "none",
            "-port",
            str(self.adbPort)
        ]

        self.__emulatorProcess = OSCommand.executeAsyncCommand(cmd)

        self.state = AndroidDevice.STATE_STARTING

        # Waits for device to be ready
        self._waitForDeviceToBeReady()

        # Checks that APKInstrumenter is install
        self.checkAPKInstrumenter()
Esempio n. 13
0
    def stimulateWithMonkey(self, packageName):
        """Stimulates application with monkey"""
        if self.state != AndroidDevice.STATE_STARTED:
            raise Exception("Device is not started.")

        if packageName is None or len(packageName)==0:
            raise Exception("Cannot stimulate package that has no name.")

        self._logger.info("Stimulating package {0} with monkey.".format(packageName))
        cmd = [
            self.mainConfiguration.adbPath,
            "-s",
            self.serialNumber,
            "shell",
            "monkey",
            "-p",
            packageName,
            "-v",
            "500",
            "--throttle",
            "6000",
            "--ignore-timeouts"
        ]
        p = OSCommand.executeAsyncCommand(cmd)
        stdout, stderr = p.communicate()
        self._logger.debug("{0}".format(stdout))
Esempio n. 14
0
    def startActivityFromPackage(self, packageName, activityName):
        """
        Starts the specified activity from the specified package name on the device.
        This method has to be called when package name is different from main activity.
        """
        if self.state != AndroidDevice.STATE_STARTED:
            raise Exception("Cannot start an activity since the device is not started.")

        if activityName is None or len(activityName)==0:
            raise Exception("Activity name is null.")

        if packageName is None or len(packageName)==0:
            raise Exception("Package name is null.")

        self._logger.info("Starting activity {0}/{1} on device {2}".format(packageName, activityName, self.name))

        # $ adb shell am start -n activityPackage/activity
        cmd = [
            self.mainConfiguration.adbPath,
            "-s",
            self.serialNumber,
            "shell",
            "am",
            "start",
            "-n",
            "{0}/{1}".format(packageName, activityName)
        ]
        p = OSCommand.executeAsyncCommand(cmd)
        stdout,stderr = p.communicate()
        self._logger.debug("{0}".format(stdout))
Esempio n. 15
0
 def __startAvd(self):
     """ Starts AVD """
     cmd = [
         self.__emulatorPath, "-avd", self.__avdName, "-partition-size",
         self.__partitionSize
     ]
     return OSCommand.executeAsyncCommand(cmd)
Esempio n. 16
0
    def startActivityFromPackage(self, packageName, activityName):
        """
        Starts the specified activity from the specified package name on the device.
        This method has to be called when package name is different from main activity.
        """
        if self.state != AndroidDevice.STATE_STARTED:
            raise Exception(
                "Cannot start an activity since the device is not started.")

        if activityName is None or len(activityName) == 0:
            raise Exception("Activity name is null.")

        if packageName is None or len(packageName) == 0:
            raise Exception("Package name is null.")

        self._logger.info("Starting activity {0}/{1} on device {2}".format(
            packageName, activityName, self.name))

        # $ adb shell am start -n activityPackage/activity
        cmd = [
            self.mainConfiguration.adbPath, "-s", self.serialNumber, "shell",
            "am", "start", "-n", "{0}/{1}".format(packageName, activityName)
        ]
        p = OSCommand.executeAsyncCommand(cmd)
        stdout, stderr = p.communicate()
        self._logger.debug("{0}".format(stdout))
Esempio n. 17
0
    def start(self):
        """Starts the emulator"""
        if self.state != AndroidDevice.STATE_PREPARED:
            raise Exception("Cannot start the emulator. (expected state was {0}, current state is {1})".format(AndroidDevice.STATE_PREPARED, self.state))

        # clean the temporary directory
        self.__cleanTemporaryDirectory()
            
        cmd = [
            self.mainConfiguration.emulatorPath,
            "@{0}".format(self.name),
            "-no-snapshot-save",
            "-netspeed",
            "full",
            "-netdelay",
            "none",
            "-port",
            str(self.adbPort)
        ]
        
        self.__emulatorProcess = OSCommand.executeAsyncCommand(cmd)
        
        self.state = AndroidDevice.STATE_STARTING
        
        # Waits for device to be ready
        self._waitForDeviceToBeReady()

        # Checks that APKInstrumenter is install
        self.checkAPKInstrumenter()
Esempio n. 18
0
    def rebootAVD(self):
        """Reboot the emulator"""
        self._logger.info("Rebooting AVD listening on port {0}".format(self.emulatorSerialNumber))
        cmd = [
            self.mainConfiguration.adbPath,
            "-s",
            self.emulatorSerialNumber,
            "shell",
            "setprop",
            "ctl.restart",
            "zygote"
        ]
        OSCommand.executeAsyncCommand(cmd)

        self.state = AVDEmulator.STATE_STARTING
        # waits for device to be ready
        self.__waitForDeviceToBeReady()
Esempio n. 19
0
    def stop(self, askUser=False):
        """ Stop the device"""
        self._logger.info("Stopping device listening on port {0}".format(
            self.serialNumber))
        clean = True

        # Pull our analysis events
        self.__pullResults()

        # Ask user if they want to clean the device
        if askUser:
            answer = raw_input(
                "Do you want to clean your device? [Yes or No] ").lower()
            while answer != 'yes' and answer != 'no':
                answer = raw_input(
                    "Do you want to clean your device? [Yes or No] ").lower()
            if answer == 'no':
                clean = False

        if clean:
            # If we have a real device we have to push backup to sdcard and push TWRP script to /cache/recovery/
            # at each experiment
            self.__pushBackup()
            self.__pushRecoveryScript()

            # reboot into recovery
            cmd = [
                self.mainConfiguration.adbPath, "-s", self.serialNumber,
                "reboot", "recovery"
            ]
            OSCommand.executeAsyncCommand(cmd)
            time.sleep(30)
            self._waitForDeviceToBeReady()

            time.sleep(5)  # Wait 5 seconds to be sure SDcard will be mounted

            # When device is ready, don't forget to restore sdcard
            self.__restoreSDCard()
Esempio n. 20
0
    def stop(self, askUser=False):
        """ Stop the device"""
        self._logger.info("Stopping device listening on port {0}".format(self.serialNumber))
        clean = True
        
        # Pull our analysis events
        self.__pullResults()

        # Ask user if they want to clean the device
        if askUser:
            answer = raw_input("Do you want to clean your device? [Yes or No] ").lower()
            while answer!='yes' and answer!='no':
                answer = raw_input("Do you want to clean your device? [Yes or No] ").lower()
            if answer=='no':
                clean=False
        
        if clean:
            # If we have a real device we have to push backup to sdcard and push TWRP script to /cache/recovery/
            # at each experiment
            self.__pushBackup()
            self.__pushRecoveryScript()
            
            # reboot into recovery
            cmd = [
                self.mainConfiguration.adbPath,
                "-s",
                self.serialNumber,
                "reboot",
                "recovery"
            ]
            OSCommand.executeAsyncCommand(cmd)
            time.sleep(30)
            self._waitForDeviceToBeReady()
            
            time.sleep(5) # Wait 5 seconds to be sure SDcard will be mounted
            
            # When device is ready, don't forget to restore sdcard
            self.__restoreSDCard()
Esempio n. 21
0
    def stimulateWithMonkey(self, packageName):
        """Stimulates application with monkey"""
        if self.state != AndroidDevice.STATE_STARTED:
            raise Exception("Device is not started.")

        if packageName is None or len(packageName) == 0:
            raise Exception("Cannot stimulate package that has no name.")

        self._logger.info(
            "Stimulating package {0} with monkey.".format(packageName))
        cmd = [
            self.mainConfiguration.adbPath, "-s", self.serialNumber, "shell",
            "monkey", "-p", packageName, "-v", "500", "--throttle", "6000",
            "--ignore-timeouts"
        ]
        p = OSCommand.executeAsyncCommand(cmd)
        stdout, stderr = p.communicate()
        self._logger.debug("{0}".format(stdout))
Esempio n. 22
0
    def start(self):
        """Starts the emulator"""
        if self.state != AndroidDevice.STATE_PREPARED:
            raise Exception("Cannot start the emulator. (expected state was {0}, current state is {1})".format(AndroidDevice.STATE_PREPARED, self.state))

        # clean the temporary directory
        self.__cleanTemporaryDirectory()
        if self.__partitionSize is None:
            raise Exception("Partition size cannot be None")
        
        cmd = [
            self.mainConfiguration.emulatorPath,
            "@{0}".format(self.name),
            "-partition-size",
            str(self.__partitionSize),
            "-no-snapshot-save",
            "-netspeed",
            "full",
            "-netdelay",
            "none",
            "-port",
            str(self.adbPort)
        ]
        
        self.__emulatorProcess = OSCommand.executeAsyncCommand(cmd)
        time.sleep(2)
        if self.__emulatorProcess.poll() is not None:
            raise Exception(self.__emulatorProcess.communicate())

        self.state = AndroidDevice.STATE_STARTING
        
        # Waits for device to be ready
        self._waitForDeviceToBeReady()
        
        # Set the same time as host!
        self._logger.info("Setting emulator at the same time as host")
        localTime = datetime.datetime.now().strftime("%Y%m%d.%H%M%S")
        cmd = [
            self.mainConfiguration.adbPath, "-s", self.serialNumber, "shell", "date", "-s", localTime
        ]
        self._logger.debug(OSCommand.executeCommand(cmd))
        
        # Checks that APKInstrumenter is install
        self.checkAPKInstrumenter()
Esempio n. 23
0
    def start(self):
        """Starts the emulator"""
        if self.state != AndroidDevice.STATE_PREPARED:
            raise Exception(
                "Cannot start the emulator. (expected state was {0}, current state is {1})"
                .format(AndroidDevice.STATE_PREPARED, self.state))

        # clean the temporary directory
        self.__cleanTemporaryDirectory()
        if self.__partitionSize is None:
            raise Exception("Partition size cannot be None")

        cmd = [
            self.mainConfiguration.emulatorPath, "@{0}".format(self.name),
            "-partition-size",
            str(self.__partitionSize), "-no-snapshot-save", "-netspeed",
            "full", "-netdelay", "none", "-port",
            str(self.adbPort)
        ]

        self.__emulatorProcess = OSCommand.executeAsyncCommand(cmd)
        time.sleep(2)
        if self.__emulatorProcess.poll() is not None:
            raise Exception(self.__emulatorProcess.communicate())

        self.state = AndroidDevice.STATE_STARTING

        # Waits for device to be ready
        self._waitForDeviceToBeReady()

        # Set the same time as host!
        self._logger.info("Setting emulator at the same time as host")
        localTime = datetime.datetime.now().strftime("%Y%m%d.%H%M%S")
        cmd = [
            self.mainConfiguration.adbPath, "-s", self.serialNumber, "shell",
            "date", "-s", localTime
        ]
        self._logger.debug(OSCommand.executeCommand(cmd))

        # Checks that APKInstrumenter is install
        self.checkAPKInstrumenter()
Esempio n. 24
0
 def __startAvd(self):
     """ Starts AVD """
     cmd = [
         self.__emulatorPath, "-avd", self.__avdName, "-partition-size", self.__partitionSize
     ]
     return OSCommand.executeAsyncCommand(cmd)