apkcrawler: Store screenshots.

Each time we explore an activity, store a screenshot with a unique
filename.

I also commented out the adb installation process to simplify
running the application and allow the inspection of any UI.

Change-Id: I1bb13c57ca0041ab167e790d0b3ba4199fa7183f
diff --git a/.gitignore b/.gitignore
index c6f377d..99c0d01 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
 *.pyc
 *.py~
 *.apk
+data/
diff --git a/crawlui.py b/crawlui.py
index 83d8e52..8807574 100644
--- a/crawlui.py
+++ b/crawlui.py
@@ -4,26 +4,61 @@
 import sys
 import os
 
+# Linux ADB path
 _ADB_PATH = os.path.expanduser('~') + "/Android/Sdk/platform-tools/adb"
+# OS X ADB path
+#_ADB_PATH = "/usr/local/bin/adb"
 
-from com.dtmilano.android.viewclient import ViewClient, View
+from com.dtmilano.android.viewclient import ViewClient, ViewClient
+
+
+def get_activity_name(package_name, vc):
+  """Gets the current running activity of the package."""
+  # TODO(afergan): If there are multiple windows of the application open, make
+  # sure we are getting the top window.
+  windows = vc.list()
+  for wId in windows.keys():
+    if package_name in windows[wId]:
+      return windows[wId].split(".")[-1]
+
+def crawl_activity(package_name, vc, device):
+
+  screenshot_num = 0
+  directory = (os.path.dirname(os.path.abspath(__file__)) + "/data/"
+               + package_name)
+  if not os.path.exists(directory):
+    os.makedirs(directory)
+  view = vc.dump(window='-1')
+  activity = get_activity_name(package_name, vc)
+  filename = directory + "/" + activity + str(screenshot_num) + ".png"
+  # If the screenshot already exists, increment the filename.
+  while os.path.exists(filename):
+    screenshot_num += 1
+    filename = directory + "/" + activity + str(screenshot_num) + ".png"
+
+  device.takeSnapshot().save(filename, 'PNG')
+  screenshot_num += 1
+
+  clickable_components = []
+
+  # Print the details of every component in the view.
+  for component in view:
+    # print ">>Component: ", component
+    if (component.isClickable()):
+      clickable_components.append(component)
+
+  # Print only the names of clickable components.
+  for c in clickable_components:
+    print "Clickable: " + c['uniqueId'] + " " + c['class']
 
 def crawl_package(apk_dir, package_name, vc, device):
 
   # Install the app.
-  subprocess.call([_ADB_PATH, 'install', '-r', apk_dir + package_name + ".apk"])
+  # subprocess.call([_ADB_PATH, 'install', '-r', apk_dir + package_name + ".apk"])
 
   # Launch the app.
-  subprocess.call([_ADB_PATH, 'shell', 'monkey', '-p', package_name, '-c',
-                  'android.intent.category.LAUNCHER', '1'])
+  # subprocess.call([_ADB_PATH, 'shell', 'monkey', '-p', package_name, '-c',
+                #    'android.intent.category.LAUNCHER', '1'])
 
-  view = vc.dump()
+  crawl_activity(package_name, vc, device)
 
-  # Print the details of every component in the view.
-  for component in view:
-      print ">>Component:", component
-
-  # Print only the names of clickable components.
-  for component in view:
-    if (component.isClickable()):
-      print ">>Clickable component:", component['uniqueId']
diff --git a/main.py b/main.py
index 9fbad7b..a3ef693 100644
--- a/main.py
+++ b/main.py
@@ -5,9 +5,10 @@
 import os
 import crawlui
 
-_ADB_PATH = os.path.expanduser('~') + "/Android/Sdk/platform-tools/adb"
+# os.environ["ANDROID_ADB_SERVER_PORT"] = "5554"
 _APK_DIR = os.path.dirname(os.path.abspath(__file__)) + "/apks/"
 
+
 # PyDev sets PYTHONPATH, use it
 try:
   for p in os.environ['PYTHONPATH'].split(':'):
@@ -33,11 +34,16 @@
   vc = ViewClient(device, serialno, **kwargs2)
 
   # Simple setup
-  #device, serialno = ViewClient.connectToDeviceOrExit()
-  #vc = ViewClient(device, serialno)
+  # device, serialno = ViewClient.connectToDeviceOrExit()
+  # vc = ViewClient(device, serialno)
 
-  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)
+  # TODO (afergan): In the future we will go through all apps, but for
+  # development, we can just specify 1 app.
+  # package_list = os.listdir(_APK_DIR)
+  # for package in package_list:
+
+  # 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)