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; |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 8 | import android.content.Context; |
| 9 | import android.content.Intent; |
| 10 | import android.os.Bundle; |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame] | 11 | import android.os.Handler; |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 12 | import android.view.LayoutInflater; |
| 13 | import android.view.View; |
| 14 | import android.view.ViewGroup; |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 15 | |
Adora Zhang | 236b90e | 2019-01-24 20:40:20 -0800 | [diff] [blame] | 16 | import androidx.core.app.NotificationCompat; |
| 17 | import androidx.core.app.NotificationCompat.Action; |
| 18 | import androidx.core.app.NotificationCompat.MessagingStyle; |
| 19 | import androidx.core.app.Person; |
| 20 | import androidx.core.app.RemoteInput; |
Anthony Chen | d12cd77 | 2018-04-16 16:20:44 -0700 | [diff] [blame] | 21 | import androidx.fragment.app.Fragment; |
| 22 | |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 23 | import com.google.android.car.kitchensink.KitchenSinkActivity; |
| 24 | import com.google.android.car.kitchensink.R; |
| 25 | |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame] | 26 | import java.util.HashMap; |
| 27 | |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 28 | /** |
| 29 | * Test fragment that can send all sorts of notifications. |
| 30 | */ |
| 31 | public class NotificationFragment extends Fragment { |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame] | 32 | private static final String IMPORTANCE_HIGH_ID = "importance_high"; |
Adora Zhang | 236b90e | 2019-01-24 20:40:20 -0800 | [diff] [blame] | 33 | private static final String IMPORTANCE_HIGH_NO_SOUND_ID = "importance_high_no_sound"; |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame] | 34 | private static final String IMPORTANCE_DEFAULT_ID = "importance_default"; |
| 35 | private static final String IMPORTANCE_LOW_ID = "importance_low"; |
| 36 | private static final String IMPORTANCE_MIN_ID = "importance_min"; |
| 37 | private static final String IMPORTANCE_NONE_ID = "importance_none"; |
| 38 | private int mCurrentNotificationId = 0; |
| 39 | private NotificationManager mManager; |
| 40 | private Context mContext; |
| 41 | private Handler mHandler = new Handler(); |
| 42 | private HashMap<Integer, Runnable> mUpdateRunnables = new HashMap<>(); |
| 43 | |
| 44 | @Override |
| 45 | public void onCreate(Bundle savedInstanceState) { |
| 46 | super.onCreate(savedInstanceState); |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame] | 47 | mContext = getActivity(); |
Adora Zhang | 9ecb46e | 2019-02-11 19:46:02 -0800 | [diff] [blame^] | 48 | mManager = |
| 49 | (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame] | 50 | |
| 51 | mManager.createNotificationChannel(new NotificationChannel( |
| 52 | IMPORTANCE_HIGH_ID, "Importance High", NotificationManager.IMPORTANCE_HIGH)); |
| 53 | |
Adora Zhang | 236b90e | 2019-01-24 20:40:20 -0800 | [diff] [blame] | 54 | NotificationChannel noSoundChannel = new NotificationChannel( |
| 55 | IMPORTANCE_HIGH_NO_SOUND_ID, "No sound", NotificationManager.IMPORTANCE_HIGH); |
| 56 | noSoundChannel.setSound(null, null); |
| 57 | mManager.createNotificationChannel(noSoundChannel); |
| 58 | |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame] | 59 | mManager.createNotificationChannel(new NotificationChannel( |
| 60 | IMPORTANCE_DEFAULT_ID, |
| 61 | "Importance Default", |
| 62 | NotificationManager.IMPORTANCE_DEFAULT)); |
| 63 | |
| 64 | mManager.createNotificationChannel(new NotificationChannel( |
| 65 | IMPORTANCE_LOW_ID, "Importance Low", NotificationManager.IMPORTANCE_LOW)); |
| 66 | |
| 67 | mManager.createNotificationChannel(new NotificationChannel( |
| 68 | IMPORTANCE_MIN_ID, "Importance Min", NotificationManager.IMPORTANCE_MIN)); |
| 69 | |
| 70 | mManager.createNotificationChannel(new NotificationChannel( |
| 71 | IMPORTANCE_NONE_ID, "Importance None", NotificationManager.IMPORTANCE_NONE)); |
| 72 | } |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 73 | |
| 74 | @Override |
| 75 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, |
| 76 | @Nullable Bundle savedInstanceState) { |
| 77 | View view = inflater.inflate(R.layout.notification_fragment, container, false); |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 78 | |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame] | 79 | initCancelAllButton(view); |
Adora Zhang | 236b90e | 2019-01-24 20:40:20 -0800 | [diff] [blame] | 80 | |
| 81 | initCarCategoriesButton(view); |
| 82 | |
| 83 | initImportanceHighBotton(view); |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame] | 84 | initImportanceDefaultButton(view); |
| 85 | initImportanceLowButton(view); |
| 86 | initImportanceMinButton(view); |
Adora Zhang | 236b90e | 2019-01-24 20:40:20 -0800 | [diff] [blame] | 87 | |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame] | 88 | initOngoingButton(view); |
| 89 | initMessagingStyleButton(view); |
| 90 | initProgressButton(view); |
Adora Zhang | 236b90e | 2019-01-24 20:40:20 -0800 | [diff] [blame] | 91 | initNavigationButton(view); |
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 | return view; |
| 94 | } |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 95 | |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame] | 96 | private void initCancelAllButton(View view) { |
| 97 | view.findViewById(R.id.cancel_all_button).setOnClickListener(v -> { |
| 98 | for (Runnable runnable : mUpdateRunnables.values()) { |
| 99 | mHandler.removeCallbacks(runnable); |
| 100 | } |
| 101 | mUpdateRunnables.clear(); |
| 102 | mManager.cancelAll(); |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 103 | }); |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame] | 104 | } |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 105 | |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame] | 106 | private void initCarCategoriesButton(View view) { |
| 107 | view.findViewById(R.id.category_car_emergency_button).setOnClickListener(v -> { |
| 108 | Notification notification = new Notification |
Adora Zhang | 9ecb46e | 2019-02-11 19:46:02 -0800 | [diff] [blame^] | 109 | .Builder(mContext, IMPORTANCE_DEFAULT_ID) |
Adora Zhang | 236b90e | 2019-01-24 20:40:20 -0800 | [diff] [blame] | 110 | .setContentTitle("Car Emergency") |
| 111 | .setContentText("Shows heads-up; Shows on top of the list; Does not group") |
Brad Stenning | 2056efe | 2018-08-14 11:20:13 -0700 | [diff] [blame] | 112 | .setCategory(Notification.CATEGORY_CAR_EMERGENCY) |
| 113 | .setSmallIcon(R.drawable.car_ic_mode) |
| 114 | .build(); |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame] | 115 | mManager.notify(mCurrentNotificationId++, notification); |
Brad Stenning | 2056efe | 2018-08-14 11:20:13 -0700 | [diff] [blame] | 116 | }); |
| 117 | |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame] | 118 | view.findViewById(R.id.category_car_warning_button).setOnClickListener(v -> { |
Brad Stenning | 2056efe | 2018-08-14 11:20:13 -0700 | [diff] [blame] | 119 | |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame] | 120 | Notification notification = new Notification |
Adora Zhang | 9ecb46e | 2019-02-11 19:46:02 -0800 | [diff] [blame^] | 121 | .Builder(mContext, IMPORTANCE_MIN_ID) |
Adora Zhang | 236b90e | 2019-01-24 20:40:20 -0800 | [diff] [blame] | 122 | .setContentTitle("Car Warning") |
| 123 | .setContentText( |
| 124 | "Shows heads-up; Shows on top of the list but below Car Emergency; " |
| 125 | + "Does not group") |
Brad Stenning | 2056efe | 2018-08-14 11:20:13 -0700 | [diff] [blame] | 126 | .setCategory(Notification.CATEGORY_CAR_WARNING) |
Adora Zhang | 236b90e | 2019-01-24 20:40:20 -0800 | [diff] [blame] | 127 | .setColor(mContext.getColor(android.R.color.holo_orange_dark)) |
| 128 | .setColorized(true) |
Brad Stenning | 2056efe | 2018-08-14 11:20:13 -0700 | [diff] [blame] | 129 | .setSmallIcon(R.drawable.car_ic_mode) |
| 130 | .build(); |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame] | 131 | mManager.notify(mCurrentNotificationId++, notification); |
Brad Stenning | 2056efe | 2018-08-14 11:20:13 -0700 | [diff] [blame] | 132 | }); |
| 133 | |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame] | 134 | view.findViewById(R.id.category_car_info_button).setOnClickListener(v -> { |
| 135 | Notification notification = new Notification |
Adora Zhang | 9ecb46e | 2019-02-11 19:46:02 -0800 | [diff] [blame^] | 136 | .Builder(mContext, IMPORTANCE_DEFAULT_ID) |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame] | 137 | .setContentTitle("Car information") |
Adora Zhang | 236b90e | 2019-01-24 20:40:20 -0800 | [diff] [blame] | 138 | .setContentText("Doesn't show heads-up; Importance Default; Groups") |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame] | 139 | .setCategory(Notification.CATEGORY_CAR_INFORMATION) |
Adora Zhang | 236b90e | 2019-01-24 20:40:20 -0800 | [diff] [blame] | 140 | .setColor(mContext.getColor(android.R.color.holo_orange_light)) |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame] | 141 | .setColorized(true) |
| 142 | .setSmallIcon(R.drawable.car_ic_mode) |
| 143 | .build(); |
| 144 | mManager.notify(mCurrentNotificationId++, notification); |
| 145 | }); |
| 146 | |
| 147 | } |
| 148 | |
Adora Zhang | 236b90e | 2019-01-24 20:40:20 -0800 | [diff] [blame] | 149 | private void initImportanceHighBotton(View view) { |
Adora Zhang | 9ecb46e | 2019-02-11 19:46:02 -0800 | [diff] [blame^] | 150 | Intent mIntent = new Intent(mContext, KitchenSinkActivity.class); |
| 151 | PendingIntent mPendingIntent = PendingIntent.getActivity(mContext, 0, mIntent, 0); |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame] | 152 | |
Adora Zhang | 236b90e | 2019-01-24 20:40:20 -0800 | [diff] [blame] | 153 | Notification notification1 = new Notification |
Adora Zhang | 9ecb46e | 2019-02-11 19:46:02 -0800 | [diff] [blame^] | 154 | .Builder(mContext, IMPORTANCE_HIGH_ID) |
Adora Zhang | 236b90e | 2019-01-24 20:40:20 -0800 | [diff] [blame] | 155 | .setContentTitle("Importance High: Shows as a heads-up") |
| 156 | .setContentText( |
| 157 | "Each click generates a new notification. And some " |
| 158 | + "looooooong text. " |
| 159 | + "Loooooooooooooooooooooong. " |
| 160 | + "Loooooooooooooooooooooooooooooooooooooooooooooooooong.") |
| 161 | .setSmallIcon(R.drawable.car_ic_mode) |
| 162 | .addAction( |
| 163 | new Notification.Action.Builder( |
| 164 | null, "Long Action (no-op)", mPendingIntent).build()) |
| 165 | .addAction( |
| 166 | new Notification.Action.Builder( |
| 167 | null, "Action (no-op)", mPendingIntent).build()) |
| 168 | .addAction( |
| 169 | new Notification.Action.Builder( |
| 170 | null, "Long Action (no-op)", mPendingIntent).build()) |
| 171 | .setColor(mContext.getColor(android.R.color.holo_red_light)) |
| 172 | .build(); |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame] | 173 | |
Adora Zhang | 236b90e | 2019-01-24 20:40:20 -0800 | [diff] [blame] | 174 | view.findViewById(R.id.importance_high_button).setOnClickListener( |
| 175 | v -> mManager.notify(mCurrentNotificationId++, notification1) |
| 176 | ); |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | private void initImportanceDefaultButton(View view) { |
| 180 | view.findViewById(R.id.importance_default_button).setOnClickListener(v -> { |
| 181 | Notification notification = new Notification |
Adora Zhang | 9ecb46e | 2019-02-11 19:46:02 -0800 | [diff] [blame^] | 182 | .Builder(mContext, IMPORTANCE_DEFAULT_ID) |
Adora Zhang | 236b90e | 2019-01-24 20:40:20 -0800 | [diff] [blame] | 183 | .setContentTitle("No heads-up; Importance Default; Groups") |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame] | 184 | .setSmallIcon(R.drawable.car_ic_mode) |
| 185 | .build(); |
| 186 | mManager.notify(mCurrentNotificationId++, notification); |
| 187 | }); |
| 188 | } |
| 189 | |
| 190 | private void initImportanceLowButton(View view) { |
| 191 | view.findViewById(R.id.importance_low_button).setOnClickListener(v -> { |
| 192 | |
Adora Zhang | 9ecb46e | 2019-02-11 19:46:02 -0800 | [diff] [blame^] | 193 | Notification notification = new Notification.Builder(mContext, IMPORTANCE_LOW_ID) |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame] | 194 | .setContentTitle("Importance Low") |
Adora Zhang | 236b90e | 2019-01-24 20:40:20 -0800 | [diff] [blame] | 195 | .setContentText("No heads-up; Below Importance Default; Groups") |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame] | 196 | .setSmallIcon(R.drawable.car_ic_mode) |
| 197 | .build(); |
| 198 | mManager.notify(mCurrentNotificationId++, notification); |
| 199 | }); |
| 200 | } |
| 201 | |
| 202 | private void initImportanceMinButton(View view) { |
| 203 | view.findViewById(R.id.importance_min_button).setOnClickListener(v -> { |
| 204 | |
Adora Zhang | 9ecb46e | 2019-02-11 19:46:02 -0800 | [diff] [blame^] | 205 | Notification notification = new Notification.Builder(mContext, IMPORTANCE_MIN_ID) |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame] | 206 | .setContentTitle("Importance Min") |
Adora Zhang | 236b90e | 2019-01-24 20:40:20 -0800 | [diff] [blame] | 207 | .setContentText("No heads-up; Below Importance Low; Groups") |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame] | 208 | .setSmallIcon(R.drawable.car_ic_mode) |
| 209 | .build(); |
| 210 | mManager.notify(mCurrentNotificationId++, notification); |
| 211 | }); |
| 212 | } |
| 213 | |
| 214 | private void initOngoingButton(View view) { |
| 215 | view.findViewById(R.id.ongoing_button).setOnClickListener(v -> { |
| 216 | |
| 217 | Notification notification = new Notification |
Adora Zhang | 9ecb46e | 2019-02-11 19:46:02 -0800 | [diff] [blame^] | 218 | .Builder(mContext, IMPORTANCE_DEFAULT_ID) |
Adora Zhang | 236b90e | 2019-01-24 20:40:20 -0800 | [diff] [blame] | 219 | .setContentTitle("Persistent/Ongoing Notification") |
| 220 | .setContentText("Cannot be dismissed; No heads-up; Importance default; Groups") |
Adora Zhang | f27f1c9 | 2019-01-10 11:25:59 -0800 | [diff] [blame] | 221 | .setSmallIcon(R.drawable.car_ic_mode) |
| 222 | .setOngoing(true) |
| 223 | .build(); |
| 224 | mManager.notify(mCurrentNotificationId++, notification); |
| 225 | }); |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 226 | } |
Adora Zhang | 236b90e | 2019-01-24 20:40:20 -0800 | [diff] [blame] | 227 | |
| 228 | private void initMessagingStyleButton(View view) { |
Adora Zhang | 236b90e | 2019-01-24 20:40:20 -0800 | [diff] [blame] | 229 | view.findViewById(R.id.category_message_button).setOnClickListener(v -> { |
Adora Zhang | 9ecb46e | 2019-02-11 19:46:02 -0800 | [diff] [blame^] | 230 | int id = mCurrentNotificationId++; |
Adora Zhang | 236b90e | 2019-01-24 20:40:20 -0800 | [diff] [blame] | 231 | |
| 232 | PendingIntent replyIntent = createServiceIntent(id, "reply"); |
| 233 | PendingIntent markAsReadIntent = createServiceIntent(id, "read"); |
| 234 | |
Adora Zhang | 9ecb46e | 2019-02-11 19:46:02 -0800 | [diff] [blame^] | 235 | Person personJohn = new Person.Builder().setName("John Doe").build(); |
| 236 | Person personJane = new Person.Builder().setName("Jane Roe").build(); |
Adora Zhang | 236b90e | 2019-01-24 20:40:20 -0800 | [diff] [blame] | 237 | MessagingStyle messagingStyle = |
| 238 | new MessagingStyle(personJohn) |
| 239 | .setConversationTitle("Heads-up: New Message") |
| 240 | .addMessage( |
| 241 | new MessagingStyle.Message( |
| 242 | "The meaning of life, or the answer to the question" |
| 243 | + "What is the meaning of life?, pertains to " |
| 244 | + "the significance of living or existence in" |
| 245 | + " general. Many other related questions " |
| 246 | + "include: Why are we here?, What is " |
| 247 | + "life all about?, or What is the " |
| 248 | + "purpose of existence?", |
| 249 | System.currentTimeMillis() - 3600, |
| 250 | personJohn)) |
| 251 | .addMessage( |
| 252 | new MessagingStyle.Message( |
Adora Zhang | 9ecb46e | 2019-02-11 19:46:02 -0800 | [diff] [blame^] | 253 | "Importance High; Groups; Each click generates a new" |
| 254 | + "notification. And some looooooong text. " |
| 255 | + "Loooooooooooooooooooooong. " |
| 256 | + "Loooooooooooooooooooooooooong." |
| 257 | + "Long long long long text.", |
| 258 | System.currentTimeMillis(), |
Adora Zhang | 236b90e | 2019-01-24 20:40:20 -0800 | [diff] [blame] | 259 | personJane)); |
| 260 | |
| 261 | NotificationCompat.Builder notification = new NotificationCompat |
Adora Zhang | 9ecb46e | 2019-02-11 19:46:02 -0800 | [diff] [blame^] | 262 | .Builder(mContext, IMPORTANCE_HIGH_ID) |
Adora Zhang | 236b90e | 2019-01-24 20:40:20 -0800 | [diff] [blame] | 263 | .setContentTitle("Message from someone") |
| 264 | .setContentText("hi") |
| 265 | .setShowWhen(true) |
| 266 | .setCategory(Notification.CATEGORY_MESSAGE) |
| 267 | .setSmallIcon(R.drawable.car_ic_mode) |
| 268 | .setStyle(messagingStyle) |
| 269 | .setAutoCancel(true) |
| 270 | .setColor(mContext.getColor(android.R.color.holo_green_light)) |
| 271 | .addAction( |
| 272 | new Action.Builder(R.drawable.ic_check_box, "read", markAsReadIntent) |
| 273 | .setSemanticAction(Action.SEMANTIC_ACTION_MARK_AS_READ) |
| 274 | .setShowsUserInterface(false) |
| 275 | .build()) |
| 276 | .addAction( |
| 277 | new Action.Builder(R.drawable.ic_check_box, "reply", replyIntent) |
| 278 | .setSemanticAction(Action.SEMANTIC_ACTION_REPLY) |
| 279 | .setShowsUserInterface(false) |
| 280 | .addRemoteInput(new RemoteInput.Builder("input").build()) |
| 281 | .build()); |
| 282 | |
| 283 | mManager.notify(id, notification.build()); |
| 284 | }); |
| 285 | } |
| 286 | |
| 287 | private PendingIntent createServiceIntent(int notificationId, String action) { |
| 288 | Intent intent = new Intent(mContext, KitchenSinkActivity.class).setAction(action); |
| 289 | |
| 290 | return PendingIntent.getForegroundService(mContext, notificationId, intent, |
| 291 | PendingIntent.FLAG_UPDATE_CURRENT); |
| 292 | } |
| 293 | |
| 294 | private void initProgressButton(View view) { |
| 295 | view.findViewById(R.id.progress_button).setOnClickListener(v -> { |
| 296 | int id = mCurrentNotificationId++; |
| 297 | |
| 298 | Notification notification = new Notification |
Adora Zhang | 9ecb46e | 2019-02-11 19:46:02 -0800 | [diff] [blame^] | 299 | .Builder(mContext, IMPORTANCE_DEFAULT_ID) |
Adora Zhang | 236b90e | 2019-01-24 20:40:20 -0800 | [diff] [blame] | 300 | .setContentTitle("Progress") |
Adora Zhang | 9ecb46e | 2019-02-11 19:46:02 -0800 | [diff] [blame^] | 301 | .setOngoing(true) |
| 302 | .setContentText( |
| 303 | "Doesn't show heads-up; Importance Default; Groups; Ongoing (cannot " |
| 304 | + "be dismissed)") |
Adora Zhang | 236b90e | 2019-01-24 20:40:20 -0800 | [diff] [blame] | 305 | .setProgress(100, 0, false) |
| 306 | .setColor(mContext.getColor(android.R.color.holo_purple)) |
| 307 | .setContentInfo("0%") |
| 308 | .setSmallIcon(R.drawable.car_ic_mode) |
| 309 | .build(); |
| 310 | mManager.notify(id, notification); |
| 311 | |
| 312 | Runnable runnable = new Runnable() { |
| 313 | int mProgress = 0; |
| 314 | |
| 315 | @Override |
| 316 | public void run() { |
| 317 | Notification updateNotification = new Notification |
Adora Zhang | 9ecb46e | 2019-02-11 19:46:02 -0800 | [diff] [blame^] | 318 | .Builder(mContext, IMPORTANCE_DEFAULT_ID) |
Adora Zhang | 236b90e | 2019-01-24 20:40:20 -0800 | [diff] [blame] | 319 | .setContentTitle("Progress") |
| 320 | .setContentText("Doesn't show heads-up; Importance Default; Groups") |
| 321 | .setProgress(100, mProgress, false) |
Adora Zhang | 9ecb46e | 2019-02-11 19:46:02 -0800 | [diff] [blame^] | 322 | .setOngoing(true) |
Adora Zhang | 236b90e | 2019-01-24 20:40:20 -0800 | [diff] [blame] | 323 | .setColor(mContext.getColor(android.R.color.holo_purple)) |
| 324 | .setContentInfo(mProgress + "%") |
| 325 | .setSmallIcon(R.drawable.car_ic_mode) |
| 326 | .build(); |
| 327 | mManager.notify(id, updateNotification); |
| 328 | mProgress += 5; |
| 329 | if (mProgress <= 100) { |
| 330 | mHandler.postDelayed(this, 1000); |
| 331 | } |
| 332 | } |
| 333 | }; |
| 334 | mUpdateRunnables.put(id, runnable); |
| 335 | mHandler.post(runnable); |
| 336 | }); |
| 337 | } |
| 338 | |
| 339 | private void initNavigationButton(View view) { |
| 340 | view.findViewById(R.id.navigation_button).setOnClickListener(v -> { |
| 341 | int id = mCurrentNotificationId++; |
| 342 | |
| 343 | Notification notification = new Notification |
Adora Zhang | 9ecb46e | 2019-02-11 19:46:02 -0800 | [diff] [blame^] | 344 | .Builder(mContext, IMPORTANCE_HIGH_ID) |
| 345 | .setCategory(Notification.CATEGORY_NAVIGATION) |
Adora Zhang | 236b90e | 2019-01-24 20:40:20 -0800 | [diff] [blame] | 346 | .setContentTitle("Navigation") |
| 347 | .setContentText("Turn right in 900 ft") |
| 348 | .setColor(mContext.getColor(android.R.color.holo_green_dark)) |
| 349 | .setColorized(true) |
| 350 | .setSubText("900 ft") |
| 351 | .setSmallIcon(R.drawable.car_ic_mode) |
| 352 | .build(); |
| 353 | mManager.notify(id, notification); |
| 354 | |
| 355 | Runnable rightTurnRunnable = new Runnable() { |
| 356 | int mDistance = 800; |
| 357 | |
| 358 | @Override |
| 359 | public void run() { |
| 360 | Notification updateNotification = new Notification |
Adora Zhang | 9ecb46e | 2019-02-11 19:46:02 -0800 | [diff] [blame^] | 361 | .Builder(mContext, IMPORTANCE_HIGH_NO_SOUND_ID) |
| 362 | .setCategory(Notification.CATEGORY_NAVIGATION) |
Adora Zhang | 236b90e | 2019-01-24 20:40:20 -0800 | [diff] [blame] | 363 | .setContentTitle("Navigation") |
| 364 | .setContentText("Turn right in " + mDistance + " ft") |
| 365 | .setColor(mContext.getColor(android.R.color.holo_green_dark)) |
| 366 | .setColorized(true) |
| 367 | .setSubText(mDistance + " ft") |
| 368 | .setSmallIcon(R.drawable.car_ic_mode) |
| 369 | .build(); |
| 370 | mManager.notify(id, updateNotification); |
| 371 | mDistance -= 100; |
| 372 | if (mDistance >= 0) { |
| 373 | mHandler.postDelayed(this, 1000); |
| 374 | } |
| 375 | } |
| 376 | }; |
| 377 | |
| 378 | Runnable exitRunnable = new Runnable() { |
| 379 | int mDistance = 9; |
| 380 | |
| 381 | @Override |
| 382 | public void run() { |
| 383 | Notification updateNotification = new Notification |
Adora Zhang | 9ecb46e | 2019-02-11 19:46:02 -0800 | [diff] [blame^] | 384 | .Builder(mContext, IMPORTANCE_HIGH_NO_SOUND_ID) |
| 385 | .setCategory(Notification.CATEGORY_NAVIGATION) |
Adora Zhang | 236b90e | 2019-01-24 20:40:20 -0800 | [diff] [blame] | 386 | .setContentTitle("Navigation") |
| 387 | .setContentText("Exit in " + mDistance + " miles") |
| 388 | .setColor(mContext.getColor(android.R.color.holo_green_dark)) |
| 389 | .setColorized(true) |
| 390 | .setSubText(mDistance + " miles") |
| 391 | .setSmallIcon(R.drawable.car_ic_mode) |
| 392 | .build(); |
| 393 | mManager.notify(id, updateNotification); |
| 394 | mDistance -= 1; |
| 395 | if (mDistance >= 0) { |
| 396 | mHandler.postDelayed(this, 1000); |
| 397 | } |
| 398 | } |
| 399 | }; |
| 400 | |
| 401 | mUpdateRunnables.put(id, rightTurnRunnable); |
| 402 | mUpdateRunnables.put(id, exitRunnable); |
| 403 | mHandler.postDelayed(rightTurnRunnable, 1000); |
| 404 | mHandler.postDelayed(exitRunnable, 10000); |
| 405 | }); |
| 406 | } |
Adora Zhang | 6ce0589 | 2018-03-02 18:29:41 -0800 | [diff] [blame] | 407 | } |