Add shell commands to post Toast and Notification.
Bug: 225877702
Test: adb shell dumpsys activity com.google.android.car.kitchensink/.KitchenSinkActivity cmd
Test: cmd post-notification notification_message
Test: cmd post-toast toast_message
Change-Id: Id423d7105f4bd3d61bd4d328da33e3a3b71df559
diff --git a/tests/EmbeddedKitchenSinkApp/src/com/google/android/car/kitchensink/KitchenSinkActivity.java b/tests/EmbeddedKitchenSinkApp/src/com/google/android/car/kitchensink/KitchenSinkActivity.java
index 11b75b6..45716e8 100644
--- a/tests/EmbeddedKitchenSinkApp/src/com/google/android/car/kitchensink/KitchenSinkActivity.java
+++ b/tests/EmbeddedKitchenSinkApp/src/com/google/android/car/kitchensink/KitchenSinkActivity.java
@@ -112,6 +112,7 @@
private String mLastFragmentTag = DEFAULT_FRAGMENT_TAG;
@Nullable
private Fragment mLastFragment;
+ private int mNotificationId = 1000;
public static final String DUMP_ARG_CMD = "cmd";
public static final String DUMP_ARG_FRAGMENT = "fragment";
@@ -480,7 +481,7 @@
case DUMP_ARG_CMD:
String[] cmdArgs = new String[args.length - 1];
System.arraycopy(args, 1, cmdArgs, 0, args.length - 1);
- new KitchenSinkShellCommand(this, writer, cmdArgs).run();
+ new KitchenSinkShellCommand(this, writer, cmdArgs, mNotificationId++).run();
return;
case DUMP_ARG_FRAGMENT:
if (args.length < 2) {
@@ -513,6 +514,7 @@
writer.printf("%smLastFragmentTag: %s\n", innerPrefix, mLastFragmentTag);
writer.printf("%smLastFragment: %s\n", innerPrefix, mLastFragment);
writer.printf("%sHeader views: %d\n", innerPrefix, mHeader.getChildCount());
+ writer.printf("%sNext Notification Id: %d\n", innerPrefix, mNotificationId);
if (skipParentState) {
Log.v(TAG, "dump(): skipping parent state");
diff --git a/tests/EmbeddedKitchenSinkApp/src/com/google/android/car/kitchensink/KitchenSinkShellCommand.java b/tests/EmbeddedKitchenSinkApp/src/com/google/android/car/kitchensink/KitchenSinkShellCommand.java
index 0668670..d2073ea 100644
--- a/tests/EmbeddedKitchenSinkApp/src/com/google/android/car/kitchensink/KitchenSinkShellCommand.java
+++ b/tests/EmbeddedKitchenSinkApp/src/com/google/android/car/kitchensink/KitchenSinkShellCommand.java
@@ -16,6 +16,9 @@
package com.google.android.car.kitchensink;
import android.annotation.Nullable;
+import android.app.Notification;
+import android.app.NotificationChannel;
+import android.app.NotificationManager;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
import android.os.Handler;
@@ -25,6 +28,7 @@
import android.security.keystore.KeyProperties;
import android.util.IndentingPrintWriter;
import android.util.Log;
+import android.widget.Toast;
import java.io.PrintWriter;
import java.util.Arrays;
@@ -50,22 +54,26 @@
private static final String CMD_SET_UNINSTALL_BLOCKED = "set-uninstall-blocked";
private static final String CMD_GENERATE_DEVICE_ATTESTATION_KEY_PAIR =
"generate-device-attestation-key-pair";
+ private static final String CMD_POST_NOTIFICATION = "post-notification";
+ private static final String CMD_POST_TOAST = "post-toast";
private final Context mContext;
private final @Nullable DevicePolicyManager mDpm;
private final IndentingPrintWriter mWriter;
private final String[] mArgs;
+ private final int mNotificationId;
@Nullable // dynamically created on post() method
private Handler mHandler;
private int mNextArgIndex;
- KitchenSinkShellCommand(Context context, PrintWriter writer, String[] args) {
+ KitchenSinkShellCommand(Context context, PrintWriter writer, String[] args, int id) {
mContext = context;
mDpm = context.getSystemService(DevicePolicyManager.class);
mWriter = new IndentingPrintWriter(writer);
mArgs = args;
+ mNotificationId = id;
}
void run() {
@@ -90,6 +98,12 @@
case CMD_GENERATE_DEVICE_ATTESTATION_KEY_PAIR:
generateDeviceAttestationKeyPair();
break;
+ case CMD_POST_NOTIFICATION:
+ postNotification();
+ break;
+ case CMD_POST_TOAST:
+ postToast();
+ break;
default:
showHelp("Invalid command: %s", cmd);
}
@@ -113,6 +127,10 @@
CMD_SET_UNINSTALL_BLOCKED, "<PKG>", "<true|false>");
showCommandHelp("Generates a device attestation key.",
CMD_GENERATE_DEVICE_ATTESTATION_KEY_PAIR, "<ALIAS>", "[FLAGS]");
+ showCommandHelp("Post Notification.",
+ CMD_POST_NOTIFICATION, "<MESSAGE>");
+ showCommandHelp("Post Toast.",
+ CMD_POST_TOAST, "<MESSAGE>");
mWriter.decreaseIndent();
}
@@ -173,6 +191,33 @@
Log.i(TAG, "key: " + kp);
}
+ private void postNotification() {
+ String message = getNextArg();
+ String channelId = "importance_high";
+
+ NotificationManager notificationMgr = mContext.getSystemService(NotificationManager.class);
+ notificationMgr.createNotificationChannel(
+ new NotificationChannel(channelId, "Importance High",
+ NotificationManager.IMPORTANCE_HIGH));
+ Notification notification = new Notification
+ .Builder(mContext, channelId)
+ .setContentTitle("Car Emergency")
+ .setContentText(message)
+ .setCategory(Notification.CATEGORY_CAR_EMERGENCY)
+ .setColor(mContext.getColor(android.R.color.holo_red_light))
+ .setColorized(true)
+ .setSmallIcon(R.drawable.car_ic_mode)
+ .build();
+ notificationMgr.notify(mNotificationId, notification);
+ Log.i(TAG, "Post Notification: id=" + mNotificationId + ", message=" + message);
+ }
+
+ private void postToast() {
+ String message = getNextArg();
+ Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
+ Log.i(TAG, "Post Toast: " + message);
+ }
+
private void warnAboutAsyncCall() {
mWriter.printf("Command will be executed asynchronally; use `adb logcat %s *:s` for result"
+ "\n", TAG);