Skip to content

Commit

Permalink
modify manual mode, add exception handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanchun-li committed Jan 4, 2016
1 parent bc9cc51 commit 98aee4a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 35 deletions.
41 changes: 24 additions & 17 deletions droidbot/app_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
from threading import Timer
from types import Intent

EVENT_POLICIES = [
"none",
"monkey",
"random",
"static",
"dynamic",
"file"
]
POLICY_NONE = "none"
POLICY_MONKEY = "monkey"
POLICY_RANDOM = "random"
POLICY_STATIC = "static"
POLICY_DYNAMIC = "dynamic"
POLICY_STATE_RECORDER = "state_recorder"
POLICY_MANUAL = "manual"
POLICY_FILE = "file"

POSSIBLE_KEYS = [
"BACK",
Expand Down Expand Up @@ -588,22 +588,22 @@ def __init__(self, device, app, event_policy, event_count, event_interval, event
self.event_count = 100

if not self.policy or self.policy is None:
self.policy = "monkey"
self.policy = POLICY_NONE

if not self.event_interval or self.event_interval is None:
self.event_interval = 2

if self.policy == "none":
self.event_factory = NoneEventFactory(device, app)
elif self.policy == "monkey":
if self.policy == POLICY_NONE:
self.event_factory = None
elif self.policy == POLICY_MONKEY:
self.event_factory = None
elif self.policy == "random":
elif self.policy == POLICY_RANDOM:
self.event_factory = RandomEventFactory(device, app)
elif self.policy == "static":
elif self.policy == POLICY_STATIC:
self.event_factory = StaticEventFactory(device, app)
elif self.policy == "dynamic":
elif self.policy == POLICY_DYNAMIC:
self.event_factory = DynamicEventFactory(device, app)
elif self.policy == "state_recorder":
elif self.policy == POLICY_STATE_RECORDER:
self.event_factory = StateRecorderFactory(device, app)
else:
self.event_factory = FileEventFactory(device, app, self.policy)
Expand Down Expand Up @@ -661,7 +661,7 @@ def start(self):
try:
if self.event_factory is not None:
self.event_factory.start(self)
else:
elif self.policy == POLICY_MONKEY:
throttle = self.event_interval * 1000
monkey_cmd = "adb shell monkey %s --throttle %d -v %d" % (
("" if self.app.get_package_name() is None else "-p " + (self.app.get_package_name())),
Expand All @@ -670,6 +670,13 @@ def start(self):
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while self.enabled:
time.sleep(1)
elif self.policy == POLICY_NONE:
self.device.start_app(self.app)
while True:
print raw_input("press ENTER to save current device state...")
state = self.device.get_current_state()
if state is not None:
state.save2dir()
except KeyboardInterrupt:
pass
out_file = open(os.path.join(self.device.output_dir, "droidbot_event.json"), "w")
Expand Down
28 changes: 16 additions & 12 deletions droidbot/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

__author__ = 'liyc'
import argparse
import logging
from argparse import RawTextHelpFormatter
from droidbot import DroidBot
import app_event


def parse_args():
"""
Expand All @@ -26,18 +27,21 @@ def parse_args():
type=int, help="duration of droidbot running (seconds)")
parser.add_argument("-env", action="store", dest="env_policy",
help="policy to set up environment. Supported policies:\n"
"none\tno environment will be set. App will run in default environment of device; \n"
"dummy\tadd some fake contacts, SMS log, call log; \n"
"static\tset environment based on static analysis result; \n"
"<file>\tget environment policy from a json file.\n")
"none\tno environment will be set. App will run in default environment of device; \n"
"dummy\tadd some fake contacts, SMS log, call log; \n"
"static\tset environment based on static analysis result; \n"
"<file>\tget environment policy from a json file.\n")
parser.add_argument("-event", action="store", dest="event_policy",
help="policy to generate events. Supported policies:\n"
"monkey\tuse \"adb shell monkey\" to send events; \n" \
"random\tpseudo-random events, similar with monkey; \n" \
"static\tsend events based on static analysis result; \n" \
"dynamic\tsend events based on dynamic app state,"
" this policy requires framework instrumented; \n" \
"<file>\tget event policy from a json file.\n")
help='policy to generate events. Supported policies:\n'
'%s\tno event will be sent, user should interact manually with device; \n'
'%s\tuse "adb shell monkey" to send events; \n'
'%s\tpseudo-random events, similar with monkey; \n'
'%s\tsend events based on static analysis result; \n'
'%s\tsend events based on dynamic app state,'
' this policy requires framework instrumented; \n'
'<%s>\tget event policy from a json file; \n' %
(app_event.POLICY_NONE, app_event.POLICY_MONKEY, app_event.POLICY_RANDOM,
app_event.POLICY_STATIC, app_event.POLICY_DYNAMIC, app_event.POLICY_FILE))
parser.add_argument("-o", action="store", dest="output_dir",
help="directory of output")
parser.add_argument("-droidbox", action="store_true", dest="with_droidbox",
Expand Down
21 changes: 15 additions & 6 deletions droidbot/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,13 @@ def start_app(self, app):
package_name = app
elif isinstance(app, App):
package_name = app.get_package_name()
if app.get_main_activity():
package_name += "/%s" % app.get_main_activity()
else:
self.logger.warning("unsupported param " + app + " with type: ", type(app))
return
self.get_adb().startActivity(uri=package_name)
intent = Intent(suffix=package_name)
self.send_intent(intent)

def get_service_names(self):
"""
Expand Down Expand Up @@ -490,11 +493,17 @@ def uninstall_app(self, app):
stdout=subprocess.PIPE, stderr=subprocess.PIPE)

def get_current_state(self):
current_views = self.get_view_client().dump()
foreground_activity = self.get_adb().getTopActivityName()
background_services = self.get_service_names()
snapshot = self.get_adb().takeSnapshot(reconnect=True)
return DeviceState(self, current_views, foreground_activity, background_services, snapshot)
self.logger.info("getting current device state...")
try:
current_views = self.get_view_client().dump()
foreground_activity = self.get_adb().getTopActivityName()
background_services = self.get_service_names()
snapshot = self.get_adb().takeSnapshot(reconnect=True)
self.logger.info("finish getting current device state...")
return DeviceState(self, current_views, foreground_activity, background_services, snapshot)
except Exception as e:
self.logger.warning(e)
return None


class DeviceState(object):
Expand Down

0 comments on commit 98aee4a

Please sign in to comment.