aboutsummaryrefslogtreecommitdiff
path: root/third_party/sl4a
diff options
context:
space:
mode:
authorAlexander Dorokhine <adorokhine@google.com>2016-12-05 17:05:29 -0800
committerAlexander Dorokhine <adorokhine@google.com>2016-12-05 17:05:29 -0800
commitf417eaa29c72692ede7b5c211a0bf7fc90d47dd9 (patch)
tree703381bc657c72d1be5be271c3115140667b812e /third_party/sl4a
parent171092d2b296115510b5bc0b3cf224a4b05fc638 (diff)
downloadmobly-snippet-lib-f417eaa29c72692ede7b5c211a0bf7fc90d47dd9.tar.gz
Launch snippets as an instrumentation rather than a service.
This allows us to create snippets that control other apps.
Diffstat (limited to 'third_party/sl4a')
-rw-r--r--third_party/sl4a/build.gradle4
-rw-r--r--third_party/sl4a/src/main/AndroidManifest.xml1
-rw-r--r--third_party/sl4a/src/main/java/com/google/android/mobly/snippet/SnippetRunner.java51
3 files changed, 56 insertions, 0 deletions
diff --git a/third_party/sl4a/build.gradle b/third_party/sl4a/build.gradle
index 015499b..3f76d58 100644
--- a/third_party/sl4a/build.gradle
+++ b/third_party/sl4a/build.gradle
@@ -11,3 +11,7 @@ android {
versionName "0.0.1"
}
}
+
+dependencies {
+ compile 'com.android.support.test:runner:0.5'
+}
diff --git a/third_party/sl4a/src/main/AndroidManifest.xml b/third_party/sl4a/src/main/AndroidManifest.xml
index 6c42f77..bb254d4 100644
--- a/third_party/sl4a/src/main/AndroidManifest.xml
+++ b/third_party/sl4a/src/main/AndroidManifest.xml
@@ -11,5 +11,6 @@
<action android:name="com.google.android.mobly.snippet.action.LAUNCH_SERVER"/>
</intent-filter>
</service>
+ <uses-library android:name="android.test.runner"/>
</application>
</manifest>
diff --git a/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/SnippetRunner.java b/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/SnippetRunner.java
new file mode 100644
index 0000000..56f2df5
--- /dev/null
+++ b/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/SnippetRunner.java
@@ -0,0 +1,51 @@
+package com.google.android.mobly.snippet;
+
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+import android.support.test.runner.AndroidJUnitRunner;
+
+import com.google.android.mobly.snippet.service.SnippetService;
+import com.google.android.mobly.snippet.util.Log;
+
+/**
+ * A launcher that starts the snippet server as an instrumentation so that it has access to the
+ * target app's context.
+ *
+ * It is written this way to be compatible with 'am instrument'.
+ */
+public class SnippetRunner extends AndroidJUnitRunner {
+ private static final String ARG_PORT = "port";
+
+ private Context mContext;
+ private int mServicePort;
+
+ @Override
+ public void onCreate(Bundle arguments) {
+ mContext = getContext();
+ String servicePort = arguments.getString(ARG_PORT);
+ if (servicePort == null) {
+ throw new IllegalArgumentException("\"--e port <port>\" was not specified");
+ }
+ mServicePort = Integer.parseInt(servicePort);
+ super.onCreate(arguments);
+ }
+
+ @Override
+ public void onStart() {
+ Intent intent = new Intent(mContext, SnippetService.class);
+ intent.setAction(Constants.ACTION_LAUNCH_SERVER);
+ intent.putExtra(Constants.EXTRA_SERVICE_PORT, mServicePort);
+ mContext.startService(intent);
+ Log.e("Started up the snippet server on port " + mServicePort);
+
+ // Wait forever. Once our instrumentation returns, everything related to the app is killed.
+ while (true) {
+ try {
+ Thread.sleep(24L * 60 * 60 * 1000);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ }
+}