aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxianyuanjia <56282287+xianyuanjia@users.noreply.github.com>2021-10-06 09:00:01 -0400
committerGitHub <noreply@github.com>2021-10-06 06:00:01 -0700
commitb6e05f13903157185bf7c15bcd8b9e809530ceec (patch)
tree7bb0eeeed2fcb5dddaf32efc06b7502988f6efd3
parent43942f884c5e7cccafc7156ef408f3e081718f69 (diff)
downloadmobly-snippet-lib-b6e05f13903157185bf7c15bcd8b9e809530ceec.tar.gz
Added Notification for O (#111)
-rw-r--r--third_party/sl4a/src/main/java/com/google/android/mobly/snippet/SnippetRunner.java39
-rw-r--r--third_party/sl4a/src/main/java/com/google/android/mobly/snippet/manager/SnippetManager.java11
2 files changed, 39 insertions, 11 deletions
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 ed83469..36d1348 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
@@ -17,8 +17,10 @@ package com.google.android.mobly.snippet;
import android.app.Instrumentation;
import android.app.Notification;
+import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
+import android.os.Build;
import android.os.Bundle;
import android.os.Process;
import androidx.test.runner.AndroidJUnitRunner;
@@ -77,6 +79,13 @@ public class SnippetRunner extends AndroidJUnitRunner {
private static final String ARG_ACTION = "action";
private static final String ARG_PORT = "port";
+ /**
+ * Values needed to create a notification channel. This applies to versions > O (26).
+ */
+ private static final String NOTIFICATION_CHANNEL_ID = "msl_channel";
+ private static final String NOTIFICATION_CHANNEL_DESC = "Channel reserved for mobly-snippet-lib.";
+ private static final CharSequence NOTIFICATION_CHANNEL_NAME = "msl";
+
private enum Action {
START,
STOP
@@ -152,13 +161,31 @@ public class SnippetRunner extends AndroidJUnitRunner {
Log.i("Snippet server started for process " + Process.myPid() + " on port " + actualPort);
}
+ @SuppressWarnings("deprecation") // Depreciated calls needed for versions < O (26)
private void createNotification() {
- Notification.Builder builder = new Notification.Builder(getTargetContext());
- builder.setSmallIcon(android.R.drawable.btn_star)
- .setTicker(null)
- .setWhen(System.currentTimeMillis())
- .setContentTitle("Snippet Service");
- mNotification = builder.getNotification();
+ Notification.Builder builder;
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
+ builder = new Notification.Builder(getTargetContext());
+ builder.setSmallIcon(android.R.drawable.btn_star)
+ .setTicker(null)
+ .setWhen(System.currentTimeMillis())
+ .setContentTitle("Snippet Service");
+ mNotification = builder.getNotification();
+ } else {
+ // Create a new channel for notifications. Needed for versions >= O
+ NotificationChannel channel = new NotificationChannel(
+ NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
+ channel.setDescription(NOTIFICATION_CHANNEL_DESC);
+ mNotificationManager.createNotificationChannel(channel);
+
+ // Build notification
+ builder = new Notification.Builder(getTargetContext(), NOTIFICATION_CHANNEL_ID);
+ builder.setSmallIcon(android.R.drawable.btn_star)
+ .setTicker(null)
+ .setWhen(System.currentTimeMillis())
+ .setContentTitle("Snippet Service");
+ mNotification = builder.build();
+ }
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/manager/SnippetManager.java b/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/manager/SnippetManager.java
index 7772d4d..7707de2 100644
--- a/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/manager/SnippetManager.java
+++ b/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/manager/SnippetManager.java
@@ -198,8 +198,8 @@ public class SnippetManager {
return null;
}
try {
- return (Class<? extends SnippetObjectConverter>) Class.forName(className);
- } catch (ClassNotFoundException e) {
+ return Class.forName(className).asSubclass(SnippetObjectConverter.class);
+ } catch (ClassNotFoundException | ClassCastException e) {
Log.e("Failed to find class " + className);
throw new RuntimeException(e);
}
@@ -223,9 +223,10 @@ public class SnippetManager {
for (String snippetClassName : snippetClassNames) {
try {
Log.i("Trying to load Snippet class: " + snippetClassName);
- Class<?> snippetClass = Class.forName(snippetClassName);
- receiverSet.add((Class<? extends Snippet>) snippetClass);
- } catch (ClassNotFoundException e) {
+ Class<? extends Snippet> snippetClass =
+ Class.forName(snippetClassName).asSubclass(Snippet.class);
+ receiverSet.add(snippetClass);
+ } catch (ClassNotFoundException | ClassCastException e) {
Log.e("Failed to find class " + snippetClassName);
throw new RuntimeException(e);
}