apkcrawler: Remove hardcoding.
When in debug mode, crawl whatever app we're in (instead of having to
specify it in main.py).
Determine the OS and set ADB_PATH accordingly.
Change-Id: I57034db13a877e2f986f59a860c03d26c79401bc
diff --git a/apkcrawler/crawlui.py b/apkcrawler/crawlui.py
index 4d94719..b5ff21c 100644
--- a/apkcrawler/crawlui.py
+++ b/apkcrawler/crawlui.py
@@ -8,10 +8,8 @@
from view import View
-# Linux ADB path
-ADB_PATH = os.path.expanduser('~') + '/Android/Sdk/platform-tools/adb'
-# OS X ADB path
-# ADB_PATH = '/usr/local/bin/adb'
+
+ADB_PATH = None
# Nexus 6 dimensions.
MAX_WIDTH = 1440
@@ -30,6 +28,18 @@
EXITED_APP = 'exited app'
+def set_adb_path():
+ """Define the ADB path based on operating system."""
+ try:
+ global ADB_PATH
+ # For machines with multiple installations of adb, use the last listed
+ # version of adb. If this doesn't work for your setup, modify to taste.
+ ADB_PATH = subprocess.check_output(['which -a adb'], shell=True).split(
+ '\n')[-2]
+ except subprocess.CalledProcessError:
+ print 'Could not find adb. Please check your PATH.'
+
+
def perform_press_back():
subprocess.call([ADB_PATH, 'shell', 'input', 'keyevent', '4'])
@@ -38,7 +48,7 @@
"""Gets the current running activity of the package."""
# TODO(afergan): See if we can consolidate this with get_fragment_list, but
# still make sure that the current app has focus.
-
+ # TODO(afergan): Check for Windows compatibility.
proc = subprocess.Popen([ADB_PATH, 'shell', 'dumpsys window windows '
'| grep -E \'mCurrentFocus\''],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
@@ -79,6 +89,22 @@
return frag_list
+def get_package_name():
+ """Get the package name of the current focused window."""
+ proc = subprocess.Popen([ADB_PATH, 'shell', 'dumpsys window windows '
+ '| grep -E \'mCurrentFocus\''],
+ stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+ activity_str, _ = proc.communicate()
+
+ # The current focus returns a string in the format
+ # mCurrentFocus=Window{35f66c3 u0 com.google.zagat/com.google.android.apps.
+ # zagat.activities.BrowseListsActivity}
+ # We want the text before the /
+ pkg_name = activity_str.split('/')[0].split(' ')[-1]
+ print 'Package name is ' + pkg_name
+ return pkg_name
+
+
def save_view_data(package_name, activity, frag_list, vc_dump):
"""Store the screenshot with a unique filename."""
directory = (os.path.dirname(os.path.abspath(__file__)) + '/data/'
@@ -153,12 +179,15 @@
return v
-def crawl_package(apk_dir, package_name, vc, device, debug):
+def crawl_package(apk_dir, vc, device, debug, package_name=None):
"""Main crawler loop. Evaluate views, store new views, and click on items."""
-
+ set_adb_path()
view_root = []
view_array = []
- if not debug:
+
+ if debug or not package_name: # These should be equal
+ package_name = get_package_name()
+ else:
# Install the app.
subprocess.call([ADB_PATH, 'install', '-r', apk_dir + package_name
+ '.apk'])
diff --git a/apkcrawler/main.py b/apkcrawler/main.py
index f5a976f..f01eeb2 100644
--- a/apkcrawler/main.py
+++ b/apkcrawler/main.py
@@ -6,6 +6,7 @@
from com.dtmilano.android.viewclient import ViewClient
import crawlui
+
# os.environ['ANDROID_ADB_SERVER_PORT'] = '5554'
APK_DIR = os.path.dirname(os.path.abspath(__file__)) + '/apks/'
# Whether we should skip the install & load process and just run the program
@@ -37,12 +38,9 @@
if not DEBUG:
package_list = os.listdir(APK_DIR)
for package in package_list:
- app_name = package.split('.apk')[0]
- print app_name
- crawlui.crawl_package(APK_DIR, app_name, vc, device, DEBUG)
+ if '.apk' in package:
+ package_name = os.path.splitext(package)[0]
+ print package_name
+ crawlui.crawl_package(APK_DIR, vc, device, DEBUG, package_name)
else:
- # For now, just use one application.
- package = 'com.google.zagat.apk'
- app_name = package.split('.apk')[0]
- print app_name
- crawlui.crawl_package(APK_DIR, app_name, vc, device, DEBUG)
+ crawlui.crawl_package(APK_DIR, vc, device, DEBUG)