aboutsummaryrefslogtreecommitdiff
path: root/third_party/sl4a/src
diff options
context:
space:
mode:
authoradorokhine <adorokhine@google.com>2016-12-12 16:42:58 -0800
committerGitHub <noreply@github.com>2016-12-12 16:42:58 -0800
commitebf3df57a7e684363c2fa3e5b90b93e610f1f489 (patch)
treee13ed2e6087bb10fbe6af4b2a61c27859e2fa211 /third_party/sl4a/src
parent5243ca1569c875c7b89f47425520ffea21d86856 (diff)
downloadmobly-snippet-lib-ebf3df57a7e684363c2fa3e5b90b93e610f1f489.tar.gz
Start the snippet server from the instrumentation process, not a separate service. (#5)
When run as an androidTest instead of a build flavour, the process started as a service doesn't see the instrumented app's classpath.
Diffstat (limited to 'third_party/sl4a/src')
-rw-r--r--third_party/sl4a/src/main/AndroidManifest.xml9
-rw-r--r--third_party/sl4a/src/main/java/com/google/android/mobly/snippet/Constants.java22
-rw-r--r--third_party/sl4a/src/main/java/com/google/android/mobly/snippet/SnippetRunner.java54
-rw-r--r--third_party/sl4a/src/main/java/com/google/android/mobly/snippet/service/ForegroundService.java41
-rw-r--r--third_party/sl4a/src/main/java/com/google/android/mobly/snippet/service/SnippetService.java114
5 files changed, 47 insertions, 193 deletions
diff --git a/third_party/sl4a/src/main/AndroidManifest.xml b/third_party/sl4a/src/main/AndroidManifest.xml
index bb254d4..8e1a579 100644
--- a/third_party/sl4a/src/main/AndroidManifest.xml
+++ b/third_party/sl4a/src/main/AndroidManifest.xml
@@ -1,16 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.android.mobly.snippet">
<uses-permission android:name="android.permission.INTERNET" />
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
-
<application>
- <service
- android:name=".service.SnippetService"
- android:exported="true">
- <intent-filter>
- <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/Constants.java b/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/Constants.java
deleted file mode 100644
index 0a2ae48..0000000
--- a/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/Constants.java
+++ /dev/null
@@ -1,22 +0,0 @@
-/*
- * Copyright (C) 2016 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
-
-package com.google.android.mobly.snippet;
-
-public interface Constants {
- String ACTION_LAUNCH_SERVER = "com.google.android.mobly.snippet.action.LAUNCH_SERVER";
- String EXTRA_SERVICE_PORT = "com.google.android.mobly.snippet.extra.SERVICE_PORT";
-}
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
index 56f2df5..b3f5e8a 100644
--- 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
@@ -1,12 +1,30 @@
+/*
+ * Copyright (C) 2016 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
package com.google.android.mobly.snippet;
+import android.app.Notification;
+import android.app.NotificationManager;
import android.content.Context;
-import android.content.Intent;
import android.os.Bundle;
+import android.os.Process;
import android.support.test.runner.AndroidJUnitRunner;
-import com.google.android.mobly.snippet.service.SnippetService;
+import com.google.android.mobly.snippet.rpc.AndroidProxy;
import com.google.android.mobly.snippet.util.Log;
+import com.google.android.mobly.snippet.util.NotificationIdFactory;
/**
* A launcher that starts the snippet server as an instrumentation so that it has access to the
@@ -17,12 +35,18 @@ import com.google.android.mobly.snippet.util.Log;
public class SnippetRunner extends AndroidJUnitRunner {
private static final String ARG_PORT = "port";
+ private static final int NOTIFICATION_ID = NotificationIdFactory.create();
+
private Context mContext;
+ private NotificationManager mNotificationManager;
private int mServicePort;
+ private Notification mNotification;
@Override
public void onCreate(Bundle arguments) {
mContext = getContext();
+ mNotificationManager = (NotificationManager)
+ mContext.getSystemService(Context.NOTIFICATION_SERVICE);
String servicePort = arguments.getString(ARG_PORT);
if (servicePort == null) {
throw new IllegalArgumentException("\"--e port <port>\" was not specified");
@@ -33,11 +57,7 @@ public class SnippetRunner extends AndroidJUnitRunner {
@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);
+ startServer();
// Wait forever. Once our instrumentation returns, everything related to the app is killed.
while (true) {
@@ -48,4 +68,24 @@ public class SnippetRunner extends AndroidJUnitRunner {
}
}
}
+
+ private void startServer() {
+ AndroidProxy androidProxy = new AndroidProxy(mContext);
+ if (androidProxy.startLocal(mServicePort) == null) {
+ throw new RuntimeException("Failed to start server on port " + mServicePort);
+ }
+ createNotification();
+ Log.i("Snippet server started for process " + Process.myPid() + " on port " + mServicePort);
+ }
+
+ private void createNotification() {
+ Notification.Builder builder = new Notification.Builder(mContext);
+ builder.setSmallIcon(android.R.drawable.btn_star)
+ .setTicker(null)
+ .setWhen(System.currentTimeMillis())
+ .setContentTitle("Snippet Service");
+ mNotification = builder.getNotification();
+ mNotification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
+ mNotificationManager.notify(NOTIFICATION_ID, mNotification);
+ }
}
diff --git a/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/service/ForegroundService.java b/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/service/ForegroundService.java
deleted file mode 100644
index 4681fb3..0000000
--- a/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/service/ForegroundService.java
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * Copyright (C) 2016 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
-
-package com.google.android.mobly.snippet.service;
-
-import android.app.Notification;
-import android.app.Service;
-
-public abstract class ForegroundService extends Service {
- private final int mNotificationId;
-
- public ForegroundService(int id) {
- mNotificationId = id;
- }
-
- protected abstract Notification createNotification();
-
- @Override
- public void onCreate() {
- startForeground(mNotificationId, createNotification());
- }
-
- @Override
- public void onDestroy() {
- // Make sure our notification is gone.
- stopForeground(true /* removeNotification */);
- }
-} \ No newline at end of file
diff --git a/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/service/SnippetService.java b/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/service/SnippetService.java
deleted file mode 100644
index dab0914..0000000
--- a/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/service/SnippetService.java
+++ /dev/null
@@ -1,114 +0,0 @@
-/*
- * Copyright (C) 2016 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
-
-package com.google.android.mobly.snippet.service;
-
-import android.app.Notification;
-import android.app.NotificationManager;
-import android.content.Context;
-import android.content.Intent;
-import android.os.Binder;
-import android.os.IBinder;
-
-import com.google.android.mobly.snippet.Constants;
-import com.google.android.mobly.snippet.rpc.AndroidProxy;
-import com.google.android.mobly.snippet.util.NotificationIdFactory;
-
-/**
- * A service that allows scripts and the RPC server to run in the background.
- *
- */
-public class SnippetService extends ForegroundService {
- private static final int NOTIFICATION_ID = NotificationIdFactory.create();
-
- private final IBinder mBinder;
- private NotificationManager mNotificationManager;
- private Notification mNotification;
-
- public class LocalBinder extends Binder {
- public SnippetService getService() {
- return SnippetService.this;
- }
- }
-
- @Override
- public IBinder onBind(Intent intent) {
- return mBinder;
- }
-
- public SnippetService() {
- super(NOTIFICATION_ID);
- mBinder = new LocalBinder();
- }
-
- @Override
- public void onCreate() {
- super.onCreate();
- mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- }
-
- @Override
- protected Notification createNotification() {
- Notification.Builder builder = new Notification.Builder(this);
- builder.setSmallIcon(android.R.drawable.btn_star)
- .setTicker(null)
- .setWhen(System.currentTimeMillis())
- .setContentTitle("Snippet Service");
- mNotification = builder.getNotification();
- mNotification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
- return mNotification;
- }
-
- private void updateNotification(String tickerText) {
- if (tickerText.equals(mNotification.tickerText)) {
- // Consequent notifications with the same ticker-text are displayed without any ticker-text.
- // This is a way around. Alternatively, we can display process name and port.
- tickerText = tickerText + " ";
- }
- Notification.Builder builder = new Notification.Builder(this);
- builder.setContentTitle("Snippet Service")
- .setWhen(mNotification.when)
- .setTicker(tickerText);
-
- mNotification = builder.getNotification();
- mNotificationManager.notify(NOTIFICATION_ID, mNotification);
- }
-
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- super.onStartCommand(intent, flags, startId);
- if (intent == null) {
- return START_REDELIVER_INTENT;
- } else if (intent.getAction().equals(Constants.ACTION_LAUNCH_SERVER)) {
- launchServer(intent);
- } else {
- updateNotification("Action not implemented: " + intent.getAction());
- }
- return START_REDELIVER_INTENT;
- }
-
- private void launchServer(Intent intent) {
- AndroidProxy androidProxy = new AndroidProxy(this);
- int servicePort = intent.getIntExtra(Constants.EXTRA_SERVICE_PORT, 0);
- if (servicePort == 0) {
- throw new IllegalArgumentException(
- "Intent missing required extra: " + Constants.EXTRA_SERVICE_PORT);
- }
- if (androidProxy.startLocal(servicePort) == null) {
- throw new RuntimeException("Failed to start server on port " + servicePort);
- }
- }
-}