Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 1 | package com.google.android.car.kitchensink.notification; |
| 2 | |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 3 | import android.annotation.Nullable; |
| 4 | import android.app.Notification; |
| 5 | import android.app.NotificationChannel; |
| 6 | import android.app.NotificationManager; |
| 7 | import android.app.PendingIntent; |
Selim Cinek | 890675e | 2018-04-05 14:48:26 -0700 | [diff] [blame] | 8 | import android.app.Person; |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 9 | import android.app.RemoteInput; |
| 10 | import android.content.Context; |
| 11 | import android.content.Intent; |
| 12 | import android.os.Bundle; |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame^] | 13 | import android.os.Handler; |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 14 | import android.view.LayoutInflater; |
| 15 | import android.view.View; |
| 16 | import android.view.ViewGroup; |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 17 | |
Anthony Chen | d12cd77 | 2018-04-16 16:20:44 -0700 | [diff] [blame] | 18 | import androidx.fragment.app.Fragment; |
| 19 | |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 20 | import com.google.android.car.kitchensink.KitchenSinkActivity; |
| 21 | import com.google.android.car.kitchensink.R; |
| 22 | |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame^] | 23 | import java.util.HashMap; |
| 24 | |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 25 | /** |
| 26 | * Test fragment that can send all sorts of notifications. |
| 27 | */ |
| 28 | public class NotificationFragment extends Fragment { |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame^] | 29 | 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 Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 64 | |
| 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 Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 69 | |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame^] | 70 | 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 Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 79 | |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame^] | 80 | return view; |
| 81 | } |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 82 | |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame^] | 83 | 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 Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 90 | }); |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame^] | 91 | } |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 92 | |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame^] | 93 | 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 Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 97 | |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame^] | 98 | 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 Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 111 | |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame^] | 112 | view.findViewById(R.id.importance_high_button).setOnClickListener( |
| 113 | v -> mManager.notify(id, notification1) |
| 114 | ); |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 115 | |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame^] | 116 | 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 Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 127 | |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame^] | 128 | 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 Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 138 | |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame^] | 139 | private void initMessagingStyleButton(View view) { |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 140 | |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame^] | 141 | Intent intent = new Intent(getActivity(), KitchenSinkActivity.class); |
| 142 | PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), 0, intent, 0); |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 143 | |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame^] | 144 | view.findViewById(R.id.category_message_button).setOnClickListener(v -> { |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 145 | |
| 146 | RemoteInput remoteInput = new RemoteInput.Builder("voice reply").build(); |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame^] | 147 | PendingIntent replyIntent = PendingIntent.getBroadcast( |
| 148 | view.getContext().getApplicationContext(), |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 149 | 12345, |
| 150 | intent, |
| 151 | PendingIntent.FLAG_UPDATE_CURRENT); |
| 152 | |
Selim Cinek | 890675e | 2018-04-05 14:48:26 -0700 | [diff] [blame] | 153 | Person personJohn = new Person.Builder().setName("John Doe").build(); |
| 154 | Person personJane = new Person.Builder().setName("Jane Roe").build(); |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 155 | 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 Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame^] | 166 | Notification notification = new Notification |
| 167 | .Builder(getActivity(), IMPORTANCE_HIGH_ID) |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 168 | .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 Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame^] | 174 | .setColor(mContext.getColor(android.R.color.holo_green_light)) |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 175 | .addAction( |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame^] | 176 | new Notification.Action.Builder(null, "read", pendingIntent).build()) |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 177 | .addAction( |
| 178 | new Notification.Action.Builder(null, "reply", replyIntent) |
| 179 | .addRemoteInput(remoteInput).build()) |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 180 | .build(); |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame^] | 181 | mManager.notify(mCurrentNotificationId++, notification); |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 182 | }); |
| 183 | |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame^] | 184 | } |
Brad Stenning | 2056efe | 2018-08-14 11:20:13 -0700 | [diff] [blame] | 185 | |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame^] | 186 | 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 Stenning | 2056efe | 2018-08-14 11:20:13 -0700 | [diff] [blame] | 190 | .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 Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame^] | 195 | mManager.notify(mCurrentNotificationId++, notification); |
Brad Stenning | 2056efe | 2018-08-14 11:20:13 -0700 | [diff] [blame] | 196 | }); |
| 197 | |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame^] | 198 | view.findViewById(R.id.category_car_warning_button).setOnClickListener(v -> { |
Brad Stenning | 2056efe | 2018-08-14 11:20:13 -0700 | [diff] [blame] | 199 | |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame^] | 200 | Notification notification = new Notification |
| 201 | .Builder(getActivity(), IMPORTANCE_MIN_ID) |
Brad Stenning | 2056efe | 2018-08-14 11:20:13 -0700 | [diff] [blame] | 202 | .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 Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame^] | 207 | mManager.notify(mCurrentNotificationId++, notification); |
Brad Stenning | 2056efe | 2018-08-14 11:20:13 -0700 | [diff] [blame] | 208 | }); |
| 209 | |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame^] | 210 | 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 Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 307 | } |
| 308 | } |