Add more test notifications to KitchenSink.
Test: manual
Change-Id: Id5a2a9ed7ba36c10277b7da9ff290ca4dab91418
(cherry picked from commit 53c6229f9e223bf9b0e04ea14cf38a31b1c1430a)
diff --git a/tests/EmbeddedKitchenSinkApp/src/com/google/android/car/kitchensink/notification/NotificationFragment.java b/tests/EmbeddedKitchenSinkApp/src/com/google/android/car/kitchensink/notification/NotificationFragment.java
index d7230fc..fb30802 100644
--- a/tests/EmbeddedKitchenSinkApp/src/com/google/android/car/kitchensink/notification/NotificationFragment.java
+++ b/tests/EmbeddedKitchenSinkApp/src/com/google/android/car/kitchensink/notification/NotificationFragment.java
@@ -1,7 +1,5 @@
package com.google.android.car.kitchensink.notification;
-import static android.security.KeyStore.getApplicationContext;
-
import android.annotation.Nullable;
import android.app.Notification;
import android.app.NotificationChannel;
@@ -12,146 +10,142 @@
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
+import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
-import android.widget.Button;
import androidx.fragment.app.Fragment;
import com.google.android.car.kitchensink.KitchenSinkActivity;
import com.google.android.car.kitchensink.R;
+import java.util.HashMap;
+
/**
* Test fragment that can send all sorts of notifications.
*/
public class NotificationFragment extends Fragment {
- private static final String CHANNEL_ID_1 = "kitchensink.channel1";
- private static final String CHANNEL_ID_2 = "kitchensink.channel2";
- private static final String CHANNEL_ID_3 = "kitchensink.channel3";
- private static final String CHANNEL_ID_4 = "kitchensink.channel4";
- private static final String CHANNEL_ID_5 = "kitchensink.channel5";
- private static final String CHANNEL_ID_6 = "kitchensink.channel6";
+ private static final String IMPORTANCE_HIGH_ID = "importance_high";
+ private static final String IMPORTANCE_DEFAULT_ID = "importance_default";
+ private static final String IMPORTANCE_LOW_ID = "importance_low";
+ private static final String IMPORTANCE_MIN_ID = "importance_min";
+ private static final String IMPORTANCE_NONE_ID = "importance_none";
+ private int mCurrentNotificationId = 0;
+ private NotificationManager mManager;
+ private Context mContext;
+ private Handler mHandler = new Handler();
+ private HashMap<Integer, Runnable> mUpdateRunnables = new HashMap<>();
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ mManager =
+ (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
+ mContext = getActivity();
+
+ mManager.createNotificationChannel(new NotificationChannel(
+ IMPORTANCE_HIGH_ID, "Importance High", NotificationManager.IMPORTANCE_HIGH));
+
+ mManager.createNotificationChannel(new NotificationChannel(
+ IMPORTANCE_DEFAULT_ID,
+ "Importance Default",
+ NotificationManager.IMPORTANCE_DEFAULT));
+
+ mManager.createNotificationChannel(new NotificationChannel(
+ IMPORTANCE_LOW_ID, "Importance Low", NotificationManager.IMPORTANCE_LOW));
+
+ mManager.createNotificationChannel(new NotificationChannel(
+ IMPORTANCE_MIN_ID, "Importance Min", NotificationManager.IMPORTANCE_MIN));
+
+ mManager.createNotificationChannel(new NotificationChannel(
+ IMPORTANCE_NONE_ID, "Importance None", NotificationManager.IMPORTANCE_NONE));
+ }
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.notification_fragment, container, false);
- Button cancelAllButton = view.findViewById(R.id.cancel_all_button);
- Button importanceHighButton = view.findViewById(R.id.importance_high_button);
- Button importanceHighButton2 = view.findViewById(R.id.importance_high_button_2);
- Button importanceLowButton = view.findViewById(R.id.importance_low_button);
- Button importanceMinButton = view.findViewById(R.id.importance_min_button);
- Button importanceDefaultButton = view.findViewById(R.id.importance_default_button);
- Button ongoingButton = view.findViewById(R.id.ongoing_button);
- Button messageButton = view.findViewById(R.id.category_message_button);
- Button emerg = view.findViewById(R.id.category_car_emerg_button);
- Button warn = view.findViewById(R.id.category_car_warning_button);
- NotificationManager manager =
- (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
+ initCancelAllButton(view);
+ initHeadsupAndUpdatesBotton(view);
+ initImportanceDefaultButton(view);
+ initImportanceLowButton(view);
+ initImportanceMinButton(view);
+ initOngoingButton(view);
+ initMessagingStyleButton(view);
+ initProgressButton(view);
+ initCarCategoriesButton(view);
- // cancel all button
- cancelAllButton.setOnClickListener(v -> manager.cancelAll());
+ return view;
+ }
- // importance high notifications
- NotificationChannel highImportanceChannel =
- new NotificationChannel(
- CHANNEL_ID_1, "Importance High", NotificationManager.IMPORTANCE_HIGH);
- manager.createNotificationChannel(highImportanceChannel);
-
- importanceHighButton.setOnClickListener(v -> {
-
- Notification notification = new Notification.Builder(getActivity(), CHANNEL_ID_1)
- .setContentTitle("Importance High")
- .setContentText("blah")
- .setSmallIcon(R.drawable.car_ic_mode)
- .build();
- manager.notify(1, notification);
+ private void initCancelAllButton(View view) {
+ view.findViewById(R.id.cancel_all_button).setOnClickListener(v -> {
+ for (Runnable runnable : mUpdateRunnables.values()) {
+ mHandler.removeCallbacks(runnable);
+ }
+ mUpdateRunnables.clear();
+ mManager.cancelAll();
});
+ }
- importanceHighButton2.setOnClickListener(v -> {
- Notification notification = new Notification.Builder(getActivity(), CHANNEL_ID_1)
- .setContentTitle("Importance High 2")
- .setContentText("blah blah blah")
- .setSmallIcon(R.drawable.car_ic_mode)
- .build();
- manager.notify(2, notification);
- });
+ private void initHeadsupAndUpdatesBotton(View view) {
+ int id = mCurrentNotificationId++;
+ Intent mIntent = new Intent(getActivity(), KitchenSinkActivity.class);
+ PendingIntent mPendingIntent = PendingIntent.getActivity(getActivity(), 0, mIntent, 0);
- // importance default
- importanceDefaultButton.setOnClickListener(v -> {
- NotificationChannel channel =
- new NotificationChannel(
- CHANNEL_ID_3,
- "Importance Default",
- NotificationManager.IMPORTANCE_DEFAULT);
- manager.createNotificationChannel(channel);
+ Notification notification1 = new Notification
+ .Builder(getActivity(), IMPORTANCE_HIGH_ID)
+ .setContentTitle("Importance High")
+ .setContentText("blah")
+ .setSmallIcon(R.drawable.car_ic_mode)
+ .addAction(
+ new Notification.Action.Builder(null, "Action1", mPendingIntent).build())
+ .addAction(
+ new Notification.Action.Builder(null, "Action2", mPendingIntent).build())
+ .addAction(
+ new Notification.Action.Builder(null, "Action3", mPendingIntent).build())
+ .setColor(mContext.getColor(android.R.color.holo_red_light))
+ .build();
- Notification notification = new Notification.Builder(getActivity(), CHANNEL_ID_3)
- .setContentTitle("Importance Default")
- .setSmallIcon(R.drawable.car_ic_mode)
- .build();
- manager.notify(4, notification);
- });
+ view.findViewById(R.id.importance_high_button).setOnClickListener(
+ v -> mManager.notify(id, notification1)
+ );
- // importance low
- importanceLowButton.setOnClickListener(v -> {
- NotificationChannel channel =
- new NotificationChannel(
- CHANNEL_ID_4, "Importance Low", NotificationManager.IMPORTANCE_LOW);
- manager.createNotificationChannel(channel);
+ Notification notification2 = new Notification
+ .Builder(getActivity(), IMPORTANCE_HIGH_ID)
+ .setContentTitle("This is an instant update")
+ .setContentText("of the previous one with IMPORTANCE_HIGH")
+ .setSmallIcon(R.drawable.car_ic_mode)
+ .setColor(mContext.getColor(android.R.color.holo_red_light))
+ .addAction(
+ new Notification.Action.Builder(null, "Action?", mPendingIntent).build())
+ .build();
+ view.findViewById(R.id.importance_high_button_2).setOnClickListener(
+ v -> mManager.notify(id, notification2));
- Notification notification = new Notification.Builder(getActivity(), CHANNEL_ID_4)
- .setContentTitle("Importance Low")
- .setContentText("low low low")
- .setSmallIcon(R.drawable.car_ic_mode)
- .build();
- manager.notify(5, notification);
- });
+ Notification notification3 = new Notification
+ .Builder(getActivity(), IMPORTANCE_DEFAULT_ID)
+ .setContentTitle("This is an update")
+ .setContentText("of the previous one with IMPORTANCE_DEFAULT")
+ .setSmallIcon(R.drawable.car_ic_mode)
+ .setColor(mContext.getColor(android.R.color.holo_red_light))
+ .build();
+ view.findViewById(R.id.importance_high_button_3).setOnClickListener(
+ v -> mManager.notify(id, notification3));
+ }
- // importance min
- importanceMinButton.setOnClickListener(v -> {
- NotificationChannel channel =
- new NotificationChannel(
- CHANNEL_ID_5, "Importance Min", NotificationManager.IMPORTANCE_MIN);
- manager.createNotificationChannel(channel);
+ private void initMessagingStyleButton(View view) {
- Notification notification = new Notification.Builder(getActivity(), CHANNEL_ID_5)
- .setContentTitle("Importance Min")
- .setContentText("min min min")
- .setSmallIcon(R.drawable.car_ic_mode)
- .build();
- manager.notify(6, notification);
- });
+ Intent intent = new Intent(getActivity(), KitchenSinkActivity.class);
+ PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), 0, intent, 0);
- // ongoing
- ongoingButton.setOnClickListener(v -> {
- NotificationChannel channel =
- new NotificationChannel(
- CHANNEL_ID_6, "Ongoing", NotificationManager.IMPORTANCE_DEFAULT);
- manager.createNotificationChannel(channel);
-
- Notification notification = new Notification.Builder(getActivity(), CHANNEL_ID_6)
- .setContentTitle("Playing music or something")
- .setSmallIcon(R.drawable.car_ic_mode)
- .setOngoing(true)
- .build();
- manager.notify(7, notification);
- });
-
- // category message
- messageButton.setOnClickListener(v -> {
- NotificationChannel channel =
- new NotificationChannel(
- CHANNEL_ID_2, "Message", NotificationManager.IMPORTANCE_HIGH);
- manager.createNotificationChannel(channel);
-
- Intent intent = new Intent(getActivity(), KitchenSinkActivity.class);
- PendingIntent readIntent = PendingIntent.getActivity(getActivity(), 0, intent, 0);
+ view.findViewById(R.id.category_message_button).setOnClickListener(v -> {
RemoteInput remoteInput = new RemoteInput.Builder("voice reply").build();
- PendingIntent replyIntent = PendingIntent.getBroadcast(getApplicationContext(),
+ PendingIntent replyIntent = PendingIntent.getBroadcast(
+ view.getContext().getApplicationContext(),
12345,
intent,
PendingIntent.FLAG_UPDATE_CURRENT);
@@ -169,45 +163,146 @@
.addMessage(new Notification.MessagingStyle.Message(
"message", System.currentTimeMillis(), personJane));
- Notification notification = new Notification.Builder(getActivity(), CHANNEL_ID_2)
+ Notification notification = new Notification
+ .Builder(getActivity(), IMPORTANCE_HIGH_ID)
.setContentTitle("Message from someone")
.setContentText("hi")
.setCategory(Notification.CATEGORY_MESSAGE)
.setSmallIcon(R.drawable.car_ic_mode)
.setStyle(messagingStyle)
.setAutoCancel(true)
+ .setColor(mContext.getColor(android.R.color.holo_green_light))
.addAction(
- new Notification.Action.Builder(null, "read", readIntent).build())
+ new Notification.Action.Builder(null, "read", pendingIntent).build())
.addAction(
new Notification.Action.Builder(null, "reply", replyIntent)
.addRemoteInput(remoteInput).build())
- .extend(new Notification.CarExtender().setColor(R.color.car_red_500))
.build();
- manager.notify(3, notification);
+ mManager.notify(mCurrentNotificationId++, notification);
});
- emerg.setOnClickListener(v -> {
+ }
- Notification notification = new Notification.Builder(getActivity(), CHANNEL_ID_1)
+ private void initCarCategoriesButton(View view) {
+ view.findViewById(R.id.category_car_emergency_button).setOnClickListener(v -> {
+ Notification notification = new Notification
+ .Builder(getActivity(), IMPORTANCE_DEFAULT_ID)
.setContentTitle("OMG")
.setContentText("This is of top importance")
.setCategory(Notification.CATEGORY_CAR_EMERGENCY)
.setSmallIcon(R.drawable.car_ic_mode)
.build();
- manager.notify(10, notification);
+ mManager.notify(mCurrentNotificationId++, notification);
});
- warn.setOnClickListener(v -> {
+ view.findViewById(R.id.category_car_warning_button).setOnClickListener(v -> {
- Notification notification = new Notification.Builder(getActivity(), CHANNEL_ID_1)
+ Notification notification = new Notification
+ .Builder(getActivity(), IMPORTANCE_MIN_ID)
.setContentTitle("OMG -ish ")
.setContentText("This is of less importance but still")
.setCategory(Notification.CATEGORY_CAR_WARNING)
.setSmallIcon(R.drawable.car_ic_mode)
.build();
- manager.notify(11, notification);
+ mManager.notify(mCurrentNotificationId++, notification);
});
- return view;
+ view.findViewById(R.id.category_car_info_button).setOnClickListener(v -> {
+ Notification notification = new Notification
+ .Builder(getActivity(), IMPORTANCE_DEFAULT_ID)
+ .setContentTitle("Car information")
+ .setContentText("Oil change due")
+ .setCategory(Notification.CATEGORY_CAR_INFORMATION)
+ .setColor(mContext.getColor(android.R.color.holo_purple))
+ .setColorized(true)
+ .setSmallIcon(R.drawable.car_ic_mode)
+ .build();
+ mManager.notify(mCurrentNotificationId++, notification);
+ });
+
+ }
+
+ private void initProgressButton(View view) {
+ view.findViewById(R.id.progress_button).setOnClickListener(v -> {
+ int id = mCurrentNotificationId++;
+
+ Notification notification = new Notification
+ .Builder(getActivity(), IMPORTANCE_DEFAULT_ID)
+ .setContentTitle("Progress")
+ .setProgress(100, 0, false)
+ .setContentInfo("0%")
+ .setSmallIcon(R.drawable.car_ic_mode)
+ .build();
+ mManager.notify(id, notification);
+
+ Runnable runnable = new Runnable() {
+ int mProgress = 3;
+
+ @Override
+ public void run() {
+ Notification updateNotification = new Notification
+ .Builder(getActivity(), IMPORTANCE_DEFAULT_ID)
+ .setContentTitle("Progress")
+ .setProgress(100, mProgress, false)
+ .setContentInfo(mProgress + "%")
+ .setSmallIcon(R.drawable.car_ic_mode)
+ .build();
+ mManager.notify(id, updateNotification);
+ mProgress += 3;
+ mProgress %= 100;
+ mHandler.postDelayed(this, 1000);
+ }
+ };
+ mUpdateRunnables.put(id, runnable);
+ mHandler.post(runnable);
+ });
+ }
+
+ private void initImportanceDefaultButton(View view) {
+ view.findViewById(R.id.importance_default_button).setOnClickListener(v -> {
+ Notification notification = new Notification
+ .Builder(getActivity(), IMPORTANCE_DEFAULT_ID)
+ .setContentTitle("Importance Default")
+ .setSmallIcon(R.drawable.car_ic_mode)
+ .build();
+ mManager.notify(mCurrentNotificationId++, notification);
+ });
+ }
+
+ private void initImportanceLowButton(View view) {
+ view.findViewById(R.id.importance_low_button).setOnClickListener(v -> {
+
+ Notification notification = new Notification.Builder(getActivity(), IMPORTANCE_LOW_ID)
+ .setContentTitle("Importance Low")
+ .setContentText("low low low")
+ .setSmallIcon(R.drawable.car_ic_mode)
+ .build();
+ mManager.notify(mCurrentNotificationId++, notification);
+ });
+ }
+
+ private void initImportanceMinButton(View view) {
+ view.findViewById(R.id.importance_min_button).setOnClickListener(v -> {
+
+ Notification notification = new Notification.Builder(getActivity(), IMPORTANCE_MIN_ID)
+ .setContentTitle("Importance Min")
+ .setContentText("min min min")
+ .setSmallIcon(R.drawable.car_ic_mode)
+ .build();
+ mManager.notify(mCurrentNotificationId++, notification);
+ });
+ }
+
+ private void initOngoingButton(View view) {
+ view.findViewById(R.id.ongoing_button).setOnClickListener(v -> {
+
+ Notification notification = new Notification
+ .Builder(getActivity(), IMPORTANCE_DEFAULT_ID)
+ .setContentTitle("Playing music or something")
+ .setSmallIcon(R.drawable.car_ic_mode)
+ .setOngoing(true)
+ .build();
+ mManager.notify(mCurrentNotificationId++, notification);
+ });
}
}