blob: fb30802a403d391c42595b1f68e207cfb016aa33 [file] [log] [blame]
Adora Zhang6ce05892018-03-02 18:29:41 -08001package com.google.android.car.kitchensink.notification;
2
Adora Zhang6ce05892018-03-02 18:29:41 -08003import android.annotation.Nullable;
4import android.app.Notification;
5import android.app.NotificationChannel;
6import android.app.NotificationManager;
7import android.app.PendingIntent;
Selim Cinek890675e2018-04-05 14:48:26 -07008import android.app.Person;
Adora Zhang6ce05892018-03-02 18:29:41 -08009import android.app.RemoteInput;
10import android.content.Context;
11import android.content.Intent;
12import android.os.Bundle;
Adora Zhangf27f1c92019-01-10 11:25:59 -080013import android.os.Handler;
Adora Zhang6ce05892018-03-02 18:29:41 -080014import android.view.LayoutInflater;
15import android.view.View;
16import android.view.ViewGroup;
Adora Zhang6ce05892018-03-02 18:29:41 -080017
Anthony Chend12cd772018-04-16 16:20:44 -070018import androidx.fragment.app.Fragment;
19
Adora Zhang6ce05892018-03-02 18:29:41 -080020import com.google.android.car.kitchensink.KitchenSinkActivity;
21import com.google.android.car.kitchensink.R;
22
Adora Zhangf27f1c92019-01-10 11:25:59 -080023import java.util.HashMap;
24
Adora Zhang6ce05892018-03-02 18:29:41 -080025/**
26 * Test fragment that can send all sorts of notifications.
27 */
28public class NotificationFragment extends Fragment {
Adora Zhangf27f1c92019-01-10 11:25:59 -080029 private static final String IMPORTANCE_HIGH_ID = "importance_high";
30 private static final String IMPORTANCE_DEFAULT_ID = "importance_default";
31 private static final String IMPORTANCE_LOW_ID = "importance_low";
32 private static final String IMPORTANCE_MIN_ID = "importance_min";
33 private static final String IMPORTANCE_NONE_ID = "importance_none";
34 private int mCurrentNotificationId = 0;
35 private NotificationManager mManager;
36 private Context mContext;
37 private Handler mHandler = new Handler();
38 private HashMap<Integer, Runnable> mUpdateRunnables = new HashMap<>();
39
40 @Override
41 public void onCreate(Bundle savedInstanceState) {
42 super.onCreate(savedInstanceState);
43 mManager =
44 (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
45 mContext = getActivity();
46
47 mManager.createNotificationChannel(new NotificationChannel(
48 IMPORTANCE_HIGH_ID, "Importance High", NotificationManager.IMPORTANCE_HIGH));
49
50 mManager.createNotificationChannel(new NotificationChannel(
51 IMPORTANCE_DEFAULT_ID,
52 "Importance Default",
53 NotificationManager.IMPORTANCE_DEFAULT));
54
55 mManager.createNotificationChannel(new NotificationChannel(
56 IMPORTANCE_LOW_ID, "Importance Low", NotificationManager.IMPORTANCE_LOW));
57
58 mManager.createNotificationChannel(new NotificationChannel(
59 IMPORTANCE_MIN_ID, "Importance Min", NotificationManager.IMPORTANCE_MIN));
60
61 mManager.createNotificationChannel(new NotificationChannel(
62 IMPORTANCE_NONE_ID, "Importance None", NotificationManager.IMPORTANCE_NONE));
63 }
Adora Zhang6ce05892018-03-02 18:29:41 -080064
65 @Override
66 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
67 @Nullable Bundle savedInstanceState) {
68 View view = inflater.inflate(R.layout.notification_fragment, container, false);
Adora Zhang6ce05892018-03-02 18:29:41 -080069
Adora Zhangf27f1c92019-01-10 11:25:59 -080070 initCancelAllButton(view);
71 initHeadsupAndUpdatesBotton(view);
72 initImportanceDefaultButton(view);
73 initImportanceLowButton(view);
74 initImportanceMinButton(view);
75 initOngoingButton(view);
76 initMessagingStyleButton(view);
77 initProgressButton(view);
78 initCarCategoriesButton(view);
Adora Zhang6ce05892018-03-02 18:29:41 -080079
Adora Zhangf27f1c92019-01-10 11:25:59 -080080 return view;
81 }
Adora Zhang6ce05892018-03-02 18:29:41 -080082
Adora Zhangf27f1c92019-01-10 11:25:59 -080083 private void initCancelAllButton(View view) {
84 view.findViewById(R.id.cancel_all_button).setOnClickListener(v -> {
85 for (Runnable runnable : mUpdateRunnables.values()) {
86 mHandler.removeCallbacks(runnable);
87 }
88 mUpdateRunnables.clear();
89 mManager.cancelAll();
Adora Zhang6ce05892018-03-02 18:29:41 -080090 });
Adora Zhangf27f1c92019-01-10 11:25:59 -080091 }
Adora Zhang6ce05892018-03-02 18:29:41 -080092
Adora Zhangf27f1c92019-01-10 11:25:59 -080093 private void initHeadsupAndUpdatesBotton(View view) {
94 int id = mCurrentNotificationId++;
95 Intent mIntent = new Intent(getActivity(), KitchenSinkActivity.class);
96 PendingIntent mPendingIntent = PendingIntent.getActivity(getActivity(), 0, mIntent, 0);
Adora Zhang6ce05892018-03-02 18:29:41 -080097
Adora Zhangf27f1c92019-01-10 11:25:59 -080098 Notification notification1 = new Notification
99 .Builder(getActivity(), IMPORTANCE_HIGH_ID)
100 .setContentTitle("Importance High")
101 .setContentText("blah")
102 .setSmallIcon(R.drawable.car_ic_mode)
103 .addAction(
104 new Notification.Action.Builder(null, "Action1", mPendingIntent).build())
105 .addAction(
106 new Notification.Action.Builder(null, "Action2", mPendingIntent).build())
107 .addAction(
108 new Notification.Action.Builder(null, "Action3", mPendingIntent).build())
109 .setColor(mContext.getColor(android.R.color.holo_red_light))
110 .build();
Adora Zhang6ce05892018-03-02 18:29:41 -0800111
Adora Zhangf27f1c92019-01-10 11:25:59 -0800112 view.findViewById(R.id.importance_high_button).setOnClickListener(
113 v -> mManager.notify(id, notification1)
114 );
Adora Zhang6ce05892018-03-02 18:29:41 -0800115
Adora Zhangf27f1c92019-01-10 11:25:59 -0800116 Notification notification2 = new Notification
117 .Builder(getActivity(), IMPORTANCE_HIGH_ID)
118 .setContentTitle("This is an instant update")
119 .setContentText("of the previous one with IMPORTANCE_HIGH")
120 .setSmallIcon(R.drawable.car_ic_mode)
121 .setColor(mContext.getColor(android.R.color.holo_red_light))
122 .addAction(
123 new Notification.Action.Builder(null, "Action?", mPendingIntent).build())
124 .build();
125 view.findViewById(R.id.importance_high_button_2).setOnClickListener(
126 v -> mManager.notify(id, notification2));
Adora Zhang6ce05892018-03-02 18:29:41 -0800127
Adora Zhangf27f1c92019-01-10 11:25:59 -0800128 Notification notification3 = new Notification
129 .Builder(getActivity(), IMPORTANCE_DEFAULT_ID)
130 .setContentTitle("This is an update")
131 .setContentText("of the previous one with IMPORTANCE_DEFAULT")
132 .setSmallIcon(R.drawable.car_ic_mode)
133 .setColor(mContext.getColor(android.R.color.holo_red_light))
134 .build();
135 view.findViewById(R.id.importance_high_button_3).setOnClickListener(
136 v -> mManager.notify(id, notification3));
137 }
Adora Zhang6ce05892018-03-02 18:29:41 -0800138
Adora Zhangf27f1c92019-01-10 11:25:59 -0800139 private void initMessagingStyleButton(View view) {
Adora Zhang6ce05892018-03-02 18:29:41 -0800140
Adora Zhangf27f1c92019-01-10 11:25:59 -0800141 Intent intent = new Intent(getActivity(), KitchenSinkActivity.class);
142 PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), 0, intent, 0);
Adora Zhang6ce05892018-03-02 18:29:41 -0800143
Adora Zhangf27f1c92019-01-10 11:25:59 -0800144 view.findViewById(R.id.category_message_button).setOnClickListener(v -> {
Adora Zhang6ce05892018-03-02 18:29:41 -0800145
146 RemoteInput remoteInput = new RemoteInput.Builder("voice reply").build();
Adora Zhangf27f1c92019-01-10 11:25:59 -0800147 PendingIntent replyIntent = PendingIntent.getBroadcast(
148 view.getContext().getApplicationContext(),
Adora Zhang6ce05892018-03-02 18:29:41 -0800149 12345,
150 intent,
151 PendingIntent.FLAG_UPDATE_CURRENT);
152
Selim Cinek890675e2018-04-05 14:48:26 -0700153 Person personJohn = new Person.Builder().setName("John Doe").build();
154 Person personJane = new Person.Builder().setName("Jane Roe").build();
Adora Zhang6ce05892018-03-02 18:29:41 -0800155 Notification.MessagingStyle messagingStyle =
156 new Notification.MessagingStyle(personJohn)
157 .setConversationTitle("Whassup")
158 .addHistoricMessage(
159 new Notification.MessagingStyle.Message(
160 "historic message",
161 System.currentTimeMillis() - 3600,
162 personJohn))
163 .addMessage(new Notification.MessagingStyle.Message(
164 "message", System.currentTimeMillis(), personJane));
165
Adora Zhangf27f1c92019-01-10 11:25:59 -0800166 Notification notification = new Notification
167 .Builder(getActivity(), IMPORTANCE_HIGH_ID)
Adora Zhang6ce05892018-03-02 18:29:41 -0800168 .setContentTitle("Message from someone")
169 .setContentText("hi")
170 .setCategory(Notification.CATEGORY_MESSAGE)
171 .setSmallIcon(R.drawable.car_ic_mode)
172 .setStyle(messagingStyle)
173 .setAutoCancel(true)
Adora Zhangf27f1c92019-01-10 11:25:59 -0800174 .setColor(mContext.getColor(android.R.color.holo_green_light))
Adora Zhang6ce05892018-03-02 18:29:41 -0800175 .addAction(
Adora Zhangf27f1c92019-01-10 11:25:59 -0800176 new Notification.Action.Builder(null, "read", pendingIntent).build())
Adora Zhang6ce05892018-03-02 18:29:41 -0800177 .addAction(
178 new Notification.Action.Builder(null, "reply", replyIntent)
179 .addRemoteInput(remoteInput).build())
Adora Zhang6ce05892018-03-02 18:29:41 -0800180 .build();
Adora Zhangf27f1c92019-01-10 11:25:59 -0800181 mManager.notify(mCurrentNotificationId++, notification);
Adora Zhang6ce05892018-03-02 18:29:41 -0800182 });
183
Adora Zhangf27f1c92019-01-10 11:25:59 -0800184 }
Brad Stenning2056efe2018-08-14 11:20:13 -0700185
Adora Zhangf27f1c92019-01-10 11:25:59 -0800186 private void initCarCategoriesButton(View view) {
187 view.findViewById(R.id.category_car_emergency_button).setOnClickListener(v -> {
188 Notification notification = new Notification
189 .Builder(getActivity(), IMPORTANCE_DEFAULT_ID)
Brad Stenning2056efe2018-08-14 11:20:13 -0700190 .setContentTitle("OMG")
191 .setContentText("This is of top importance")
192 .setCategory(Notification.CATEGORY_CAR_EMERGENCY)
193 .setSmallIcon(R.drawable.car_ic_mode)
194 .build();
Adora Zhangf27f1c92019-01-10 11:25:59 -0800195 mManager.notify(mCurrentNotificationId++, notification);
Brad Stenning2056efe2018-08-14 11:20:13 -0700196 });
197
Adora Zhangf27f1c92019-01-10 11:25:59 -0800198 view.findViewById(R.id.category_car_warning_button).setOnClickListener(v -> {
Brad Stenning2056efe2018-08-14 11:20:13 -0700199
Adora Zhangf27f1c92019-01-10 11:25:59 -0800200 Notification notification = new Notification
201 .Builder(getActivity(), IMPORTANCE_MIN_ID)
Brad Stenning2056efe2018-08-14 11:20:13 -0700202 .setContentTitle("OMG -ish ")
203 .setContentText("This is of less importance but still")
204 .setCategory(Notification.CATEGORY_CAR_WARNING)
205 .setSmallIcon(R.drawable.car_ic_mode)
206 .build();
Adora Zhangf27f1c92019-01-10 11:25:59 -0800207 mManager.notify(mCurrentNotificationId++, notification);
Brad Stenning2056efe2018-08-14 11:20:13 -0700208 });
209
Adora Zhangf27f1c92019-01-10 11:25:59 -0800210 view.findViewById(R.id.category_car_info_button).setOnClickListener(v -> {
211 Notification notification = new Notification
212 .Builder(getActivity(), IMPORTANCE_DEFAULT_ID)
213 .setContentTitle("Car information")
214 .setContentText("Oil change due")
215 .setCategory(Notification.CATEGORY_CAR_INFORMATION)
216 .setColor(mContext.getColor(android.R.color.holo_purple))
217 .setColorized(true)
218 .setSmallIcon(R.drawable.car_ic_mode)
219 .build();
220 mManager.notify(mCurrentNotificationId++, notification);
221 });
222
223 }
224
225 private void initProgressButton(View view) {
226 view.findViewById(R.id.progress_button).setOnClickListener(v -> {
227 int id = mCurrentNotificationId++;
228
229 Notification notification = new Notification
230 .Builder(getActivity(), IMPORTANCE_DEFAULT_ID)
231 .setContentTitle("Progress")
232 .setProgress(100, 0, false)
233 .setContentInfo("0%")
234 .setSmallIcon(R.drawable.car_ic_mode)
235 .build();
236 mManager.notify(id, notification);
237
238 Runnable runnable = new Runnable() {
239 int mProgress = 3;
240
241 @Override
242 public void run() {
243 Notification updateNotification = new Notification
244 .Builder(getActivity(), IMPORTANCE_DEFAULT_ID)
245 .setContentTitle("Progress")
246 .setProgress(100, mProgress, false)
247 .setContentInfo(mProgress + "%")
248 .setSmallIcon(R.drawable.car_ic_mode)
249 .build();
250 mManager.notify(id, updateNotification);
251 mProgress += 3;
252 mProgress %= 100;
253 mHandler.postDelayed(this, 1000);
254 }
255 };
256 mUpdateRunnables.put(id, runnable);
257 mHandler.post(runnable);
258 });
259 }
260
261 private void initImportanceDefaultButton(View view) {
262 view.findViewById(R.id.importance_default_button).setOnClickListener(v -> {
263 Notification notification = new Notification
264 .Builder(getActivity(), IMPORTANCE_DEFAULT_ID)
265 .setContentTitle("Importance Default")
266 .setSmallIcon(R.drawable.car_ic_mode)
267 .build();
268 mManager.notify(mCurrentNotificationId++, notification);
269 });
270 }
271
272 private void initImportanceLowButton(View view) {
273 view.findViewById(R.id.importance_low_button).setOnClickListener(v -> {
274
275 Notification notification = new Notification.Builder(getActivity(), IMPORTANCE_LOW_ID)
276 .setContentTitle("Importance Low")
277 .setContentText("low low low")
278 .setSmallIcon(R.drawable.car_ic_mode)
279 .build();
280 mManager.notify(mCurrentNotificationId++, notification);
281 });
282 }
283
284 private void initImportanceMinButton(View view) {
285 view.findViewById(R.id.importance_min_button).setOnClickListener(v -> {
286
287 Notification notification = new Notification.Builder(getActivity(), IMPORTANCE_MIN_ID)
288 .setContentTitle("Importance Min")
289 .setContentText("min min min")
290 .setSmallIcon(R.drawable.car_ic_mode)
291 .build();
292 mManager.notify(mCurrentNotificationId++, notification);
293 });
294 }
295
296 private void initOngoingButton(View view) {
297 view.findViewById(R.id.ongoing_button).setOnClickListener(v -> {
298
299 Notification notification = new Notification
300 .Builder(getActivity(), IMPORTANCE_DEFAULT_ID)
301 .setContentTitle("Playing music or something")
302 .setSmallIcon(R.drawable.car_ic_mode)
303 .setOngoing(true)
304 .build();
305 mManager.notify(mCurrentNotificationId++, notification);
306 });
Adora Zhang6ce05892018-03-02 18:29:41 -0800307 }
308}