OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.notifications; | 5 package org.chromium.chrome.browser.notifications; |
6 | 6 |
7 import android.app.Notification; | 7 import android.app.Notification; |
8 import android.app.NotificationManager; | 8 import android.app.NotificationManager; |
9 import android.app.PendingIntent; | 9 import android.app.PendingIntent; |
10 import android.content.Context; | 10 import android.content.Context; |
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 URI uri = new URI(parts[1]); | 286 URI uri = new URI(parts[1]); |
287 if (uri.getHost() != null) return parts[1]; | 287 if (uri.getHost() != null) return parts[1]; |
288 } catch (URISyntaxException e) { | 288 } catch (URISyntaxException e) { |
289 Log.e(TAG, "Expected to find a valid url in the notification tag ext
ra.", e); | 289 Log.e(TAG, "Expected to find a valid url in the notification tag ext
ra.", e); |
290 return null; | 290 return null; |
291 } | 291 } |
292 return null; | 292 return null; |
293 } | 293 } |
294 | 294 |
295 /** | 295 /** |
| 296 * Generates the notfiication defaults from vibrationPattern's size and sile
nt. |
| 297 * |
| 298 * Use the system's default ringtone, vibration and indicator lights unless
the notification |
| 299 * has been marked as being silent. |
| 300 * If a vibration pattern is set, the notification should use the provided p
attern |
| 301 * rather than the defaulting to system settings. |
| 302 * |
| 303 * @param vibrationPatternLength Vibration pattern's size for the Notificati
on. |
| 304 * @param silent Whether the default sound, vibration and lights should be s
uppressed. |
| 305 * @return The generated notification's default value. |
| 306 */ |
| 307 @VisibleForTesting |
| 308 static int makeDefaults(int vibrationPatternLength, boolean silent) { |
| 309 assert !silent || vibrationPatternLength == 0; |
| 310 |
| 311 if (silent) return 0; |
| 312 |
| 313 int defaults = Notification.DEFAULT_ALL; |
| 314 if (vibrationPatternLength > 0) { |
| 315 defaults &= ~Notification.DEFAULT_VIBRATE; |
| 316 } |
| 317 return defaults; |
| 318 } |
| 319 |
| 320 /** |
| 321 * Generates the vibration pattern used in Android notification. |
| 322 * |
| 323 * Android takes a long array where the first entry indicates the number of
milliseconds to wait |
| 324 * prior to starting the vibration, whereas Chrome follows the syntax of the
Web Vibration API. |
| 325 * |
| 326 * @param vibrationPattern Vibration pattern following the Web Vibration API
syntax. |
| 327 * @return Vibration pattern following the Android syntax. |
| 328 */ |
| 329 @VisibleForTesting |
| 330 static long[] makeVibrationPattern(int[] vibrationPattern) { |
| 331 long[] pattern = new long[vibrationPattern.length + 1]; |
| 332 for (int i = 0; i < vibrationPattern.length; ++i) { |
| 333 pattern[i + 1] = vibrationPattern[i]; |
| 334 } |
| 335 return pattern; |
| 336 } |
| 337 |
| 338 /** |
296 * Displays a notification with the given details. | 339 * Displays a notification with the given details. |
297 * | 340 * |
298 * @param persistentNotificationId The persistent id of the notification. | 341 * @param persistentNotificationId The persistent id of the notification. |
299 * @param origin Full text of the origin, including the protocol, owning thi
s notification. | 342 * @param origin Full text of the origin, including the protocol, owning thi
s notification. |
300 * @param tag A string identifier for this notification. If the tag is not e
mpty, the new | 343 * @param tag A string identifier for this notification. If the tag is not e
mpty, the new |
301 * notification will replace the previous notification with the s
ame tag and origin, | 344 * notification will replace the previous notification with the s
ame tag and origin, |
302 * if present. If no matching previous notification is present, t
he new one will just | 345 * if present. If no matching previous notification is present, t
he new one will just |
303 * be added. | 346 * be added. |
304 * @param title Title to be displayed in the notification. | 347 * @param title Title to be displayed in the notification. |
305 * @param body Message to be displayed in the notification. Will be trimmed
to one line of | 348 * @param body Message to be displayed in the notification. Will be trimmed
to one line of |
306 * text by the Android notification system. | 349 * text by the Android notification system. |
307 * @param icon Icon to be displayed in the notification. When this isn't a v
alid Bitmap, a | 350 * @param icon Icon to be displayed in the notification. When this isn't a v
alid Bitmap, a |
308 * default icon will be generated instead. | 351 * default icon will be generated instead. |
| 352 * @param vibrationPattern Vibration pattern following the Web Vibration syn
tax. |
309 * @param silent Whether the default sound, vibration and lights should be s
uppressed. | 353 * @param silent Whether the default sound, vibration and lights should be s
uppressed. |
| 354 * @see https://developer.android.com/reference/android/app/Notification.htm
l |
310 */ | 355 */ |
311 @CalledByNative | 356 @CalledByNative |
312 private void displayNotification(long persistentNotificationId, String origi
n, String tag, | 357 private void displayNotification(long persistentNotificationId, String origi
n, String tag, |
313 String title, String body, Bitmap icon, boolean silent) { | 358 String title, String body, Bitmap icon, int[] vibrationPattern, bool
ean silent) { |
314 if (icon == null || icon.getWidth() == 0) { | 359 if (icon == null || icon.getWidth() == 0) { |
315 icon = getIconGenerator().generateIconForUrl(origin, true); | 360 icon = getIconGenerator().generateIconForUrl(origin, true); |
316 } | 361 } |
317 | 362 |
318 Resources res = mAppContext.getResources(); | 363 Resources res = mAppContext.getResources(); |
319 | 364 |
320 // The data used to make each intent unique according to the rules of In
tent#filterEquals. | 365 // The data used to make each intent unique according to the rules of In
tent#filterEquals. |
321 // Without this, the pending intents derived from them may be reused, be
cause extras are | 366 // Without this, the pending intents derived from them may be reused, be
cause extras are |
322 // not taken into account for the filterEquals comparison. | 367 // not taken into account for the filterEquals comparison. |
323 Uri intentData = Uri.parse(origin).buildUpon().fragment( | 368 Uri intentData = Uri.parse(origin).buildUpon().fragment( |
(...skipping 20 matching lines...) Expand all Loading... |
344 persistentNotificationId, origin, tag, intentData)) | 389 persistentNotificationId, origin, tag, intentData)) |
345 .setDeleteIntent(getPendingIntent( | 390 .setDeleteIntent(getPendingIntent( |
346 NotificationConstants.ACTION_CLOSE_NOTIFICATION, | 391 NotificationConstants.ACTION_CLOSE_NOTIFICATION, |
347 persistentNotificationId, origin, tag, intentData)) | 392 persistentNotificationId, origin, tag, intentData)) |
348 .addAction(R.drawable.settings_cog, | 393 .addAction(R.drawable.settings_cog, |
349 res.getString(R.string.page_info_site_settings_button
), | 394 res.getString(R.string.page_info_site_settings_button
), |
350 pendingSettingsIntent) | 395 pendingSettingsIntent) |
351 .setTicker(createTickerText(title, body)) | 396 .setTicker(createTickerText(title, body)) |
352 .setSubText(origin); | 397 .setSubText(origin); |
353 | 398 |
354 // Use the system's default ringtone, vibration and indicator lights unl
ess the notification | 399 notificationBuilder.setDefaults(makeDefaults(vibrationPattern.length, si
lent)); |
355 // has been marked as being silent, for example because it's low priorit
y. | 400 if (vibrationPattern.length > 0) { |
356 if (!silent) notificationBuilder.setDefaults(Notification.DEFAULT_ALL); | 401 notificationBuilder.setVibrate(makeVibrationPattern(vibrationPattern
)); |
| 402 } |
357 | 403 |
358 String platformTag = makePlatformTag(persistentNotificationId, origin, t
ag); | 404 String platformTag = makePlatformTag(persistentNotificationId, origin, t
ag); |
359 mNotificationManager.notify(platformTag, PLATFORM_ID, notificationBuilde
r.build()); | 405 mNotificationManager.notify(platformTag, PLATFORM_ID, notificationBuilde
r.build()); |
360 } | 406 } |
361 | 407 |
362 /** | 408 /** |
363 * Creates the ticker text for a notification having |title| and |body|. The
notification's | 409 * Creates the ticker text for a notification having |title| and |body|. The
notification's |
364 * title will be printed in bold, followed by the text of the body. | 410 * title will be printed in bold, followed by the text of the body. |
365 * | 411 * |
366 * @param title Title of the notification. | 412 * @param title Title of the notification. |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 origin, tag); | 508 origin, tag); |
463 } | 509 } |
464 | 510 |
465 private static native void nativeInitializeNotificationUIManager(); | 511 private static native void nativeInitializeNotificationUIManager(); |
466 | 512 |
467 private native boolean nativeOnNotificationClicked(long nativeNotificationUI
ManagerAndroid, | 513 private native boolean nativeOnNotificationClicked(long nativeNotificationUI
ManagerAndroid, |
468 long persistentNotificationId, String origin, String tag); | 514 long persistentNotificationId, String origin, String tag); |
469 private native boolean nativeOnNotificationClosed(long nativeNotificationUIM
anagerAndroid, | 515 private native boolean nativeOnNotificationClosed(long nativeNotificationUIM
anagerAndroid, |
470 long persistentNotificationId, String origin, String tag); | 516 long persistentNotificationId, String origin, String tag); |
471 } | 517 } |
OLD | NEW |