From f417eaa29c72692ede7b5c211a0bf7fc90d47dd9 Mon Sep 17 00:00:00 2001 From: Alexander Dorokhine Date: Mon, 5 Dec 2016 17:05:29 -0800 Subject: Launch snippets as an instrumentation rather than a service. This allows us to create snippets that control other apps. --- third_party/sl4a/build.gradle | 4 ++ third_party/sl4a/src/main/AndroidManifest.xml | 1 + .../android/mobly/snippet/SnippetRunner.java | 51 ++++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 third_party/sl4a/src/main/java/com/google/android/mobly/snippet/SnippetRunner.java (limited to 'third_party/sl4a') 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 @@ + 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 \" 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); + } + } + } +} -- cgit v1.2.3