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 12 matching lines...) Expand all Loading... |
23 import org.chromium.base.ContextUtils; | 23 import org.chromium.base.ContextUtils; |
24 import org.chromium.base.Log; | 24 import org.chromium.base.Log; |
25 import org.chromium.base.VisibleForTesting; | 25 import org.chromium.base.VisibleForTesting; |
26 import org.chromium.base.annotations.CalledByNative; | 26 import org.chromium.base.annotations.CalledByNative; |
27 import org.chromium.base.library_loader.ProcessInitException; | 27 import org.chromium.base.library_loader.ProcessInitException; |
28 import org.chromium.base.metrics.RecordHistogram; | 28 import org.chromium.base.metrics.RecordHistogram; |
29 import org.chromium.base.metrics.RecordUserAction; | 29 import org.chromium.base.metrics.RecordUserAction; |
30 import org.chromium.chrome.R; | 30 import org.chromium.chrome.R; |
31 import org.chromium.chrome.browser.ChromeSwitches; | 31 import org.chromium.chrome.browser.ChromeSwitches; |
32 import org.chromium.chrome.browser.init.ChromeBrowserInitializer; | 32 import org.chromium.chrome.browser.init.ChromeBrowserInitializer; |
| 33 import org.chromium.chrome.browser.preferences.PrefServiceBridge; |
33 import org.chromium.chrome.browser.preferences.Preferences; | 34 import org.chromium.chrome.browser.preferences.Preferences; |
34 import org.chromium.chrome.browser.preferences.PreferencesLauncher; | 35 import org.chromium.chrome.browser.preferences.PreferencesLauncher; |
35 import org.chromium.chrome.browser.preferences.website.SingleCategoryPreferences
; | 36 import org.chromium.chrome.browser.preferences.website.SingleCategoryPreferences
; |
36 import org.chromium.chrome.browser.preferences.website.SingleWebsitePreferences; | 37 import org.chromium.chrome.browser.preferences.website.SingleWebsitePreferences; |
37 import org.chromium.chrome.browser.preferences.website.SiteSettingsCategory; | 38 import org.chromium.chrome.browser.preferences.website.SiteSettingsCategory; |
38 import org.chromium.chrome.browser.util.UrlUtilities; | 39 import org.chromium.chrome.browser.util.UrlUtilities; |
39 import org.chromium.chrome.browser.widget.RoundedIconGenerator; | 40 import org.chromium.chrome.browser.widget.RoundedIconGenerator; |
40 import org.chromium.webapk.lib.client.WebApkValidator; | 41 import org.chromium.webapk.lib.client.WebApkValidator; |
41 | 42 |
42 import java.net.URI; | 43 import java.net.URI; |
(...skipping 20 matching lines...) Expand all Loading... |
63 private static final String PLATFORM_TAG_PREFIX = | 64 private static final String PLATFORM_TAG_PREFIX = |
64 NotificationPlatformBridge.class.getSimpleName(); | 65 NotificationPlatformBridge.class.getSimpleName(); |
65 | 66 |
66 private static final int NOTIFICATION_ICON_BG_COLOR = 0xFF969696; | 67 private static final int NOTIFICATION_ICON_BG_COLOR = 0xFF969696; |
67 private static final int NOTIFICATION_TEXT_SIZE_DP = 28; | 68 private static final int NOTIFICATION_TEXT_SIZE_DP = 28; |
68 | 69 |
69 // We always use the same request code for pending intents. We use other way
s to force | 70 // We always use the same request code for pending intents. We use other way
s to force |
70 // uniqueness of pending intents when necessary. | 71 // uniqueness of pending intents when necessary. |
71 private static final int PENDING_INTENT_REQUEST_CODE = 0; | 72 private static final int PENDING_INTENT_REQUEST_CODE = 0; |
72 | 73 |
| 74 private static final int[] EMPTY_VIBRATION_PATTERN = new int[0]; |
| 75 |
73 private static NotificationPlatformBridge sInstance; | 76 private static NotificationPlatformBridge sInstance; |
74 private static NotificationManagerProxy sNotificationManagerOverride; | 77 private static NotificationManagerProxy sNotificationManagerOverride; |
75 | 78 |
76 private final long mNativeNotificationPlatformBridge; | 79 private final long mNativeNotificationPlatformBridge; |
77 | 80 |
78 private final Context mAppContext; | 81 private final Context mAppContext; |
79 private final NotificationManagerProxy mNotificationManager; | 82 private final NotificationManagerProxy mNotificationManager; |
80 | 83 |
81 @VisibleForTesting public RoundedIconGenerator mIconGenerator; | 84 @VisibleForTesting public RoundedIconGenerator mIconGenerator; |
82 private final int mLargeIconWidthPx; | 85 private final int mLargeIconWidthPx; |
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 URI uri = new URI(parts[1]); | 407 URI uri = new URI(parts[1]); |
405 if (uri.getHost() != null) return parts[1]; | 408 if (uri.getHost() != null) return parts[1]; |
406 } catch (URISyntaxException e) { | 409 } catch (URISyntaxException e) { |
407 Log.e(TAG, "Expected to find a valid url in the notification tag ext
ra.", e); | 410 Log.e(TAG, "Expected to find a valid url in the notification tag ext
ra.", e); |
408 return null; | 411 return null; |
409 } | 412 } |
410 return null; | 413 return null; |
411 } | 414 } |
412 | 415 |
413 /** | 416 /** |
414 * Generates the notfiication defaults from vibrationPattern's size and sile
nt. | 417 * Generates the notification defaults from vibrationPattern's size and sile
nt. |
415 * | 418 * |
416 * Use the system's default ringtone, vibration and indicator lights unless
the notification | 419 * Use the system's default ringtone, vibration and indicator lights unless
the notification |
417 * has been marked as being silent. | 420 * has been marked as being silent. |
418 * If a vibration pattern is set, the notification should use the provided p
attern | 421 * If a vibration pattern is set, the notification should use the provided p
attern |
419 * rather than the defaulting to system settings. | 422 * rather than defaulting to the system settings. |
420 * | 423 * |
421 * @param vibrationPatternLength Vibration pattern's size for the Notificati
on. | 424 * @param vibrationPatternLength Vibration pattern's size for the Notificati
on. |
422 * @param silent Whether the default sound, vibration and lights should be s
uppressed. | 425 * @param silent Whether the default sound, vibration and lights should be s
uppressed. |
| 426 * @param vibrateEnabled Whether vibration is enabled in preferences. |
423 * @return The generated notification's default value. | 427 * @return The generated notification's default value. |
424 */ | 428 */ |
425 @VisibleForTesting | 429 @VisibleForTesting |
426 static int makeDefaults(int vibrationPatternLength, boolean silent) { | 430 static int makeDefaults(int vibrationPatternLength, boolean silent, boolean
vibrateEnabled) { |
427 assert !silent || vibrationPatternLength == 0; | 431 assert !silent || vibrationPatternLength == 0; |
428 | 432 |
429 if (silent) return 0; | 433 if (silent) return 0; |
430 | 434 |
431 int defaults = Notification.DEFAULT_ALL; | 435 int defaults = Notification.DEFAULT_ALL; |
432 if (vibrationPatternLength > 0) { | 436 if (vibrationPatternLength > 0 || !vibrateEnabled) { |
433 defaults &= ~Notification.DEFAULT_VIBRATE; | 437 defaults &= ~Notification.DEFAULT_VIBRATE; |
434 } | 438 } |
435 return defaults; | 439 return defaults; |
436 } | 440 } |
437 | 441 |
438 /** | 442 /** |
439 * Generates the vibration pattern used in Android notification. | 443 * Generates the vibration pattern used in Android notification. |
440 * | 444 * |
441 * Android takes a long array where the first entry indicates the number of
milliseconds to wait | 445 * Android takes a long array where the first entry indicates the number of
milliseconds to wait |
442 * prior to starting the vibration, whereas Chrome follows the syntax of the
Web Vibration API. | 446 * prior to starting the vibration, whereas Chrome follows the syntax of the
Web Vibration API. |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
545 // the label and icon provided here only affect Android Wear, so don't a
bbreviate them. | 549 // the label and icon provided here only affect Android Wear, so don't a
bbreviate them. |
546 boolean abbreviateSiteSettings = actionTitles.length > 0 && !useCustomLa
youts(); | 550 boolean abbreviateSiteSettings = actionTitles.length > 0 && !useCustomLa
youts(); |
547 int settingsIconId = abbreviateSiteSettings ? 0 : R.drawable.settings_co
g; | 551 int settingsIconId = abbreviateSiteSettings ? 0 : R.drawable.settings_co
g; |
548 CharSequence settingsTitle = abbreviateSiteSettings | 552 CharSequence settingsTitle = abbreviateSiteSettings |
549 ? res.getString(R.string.notification_site_
settings_button) | 553 ? res.getString(R.string.notification_site_
settings_button) |
550 : res.getString(R.string.page_info_site_set
tings_button); | 554 : res.getString(R.string.page_info_site_set
tings_button); |
551 // If the settings button is displayed together with the other buttons i
t has to be the last | 555 // If the settings button is displayed together with the other buttons i
t has to be the last |
552 // one, so add it after the other actions. | 556 // one, so add it after the other actions. |
553 notificationBuilder.addSettingsAction(settingsIconId, settingsTitle, pen
dingSettingsIntent); | 557 notificationBuilder.addSettingsAction(settingsIconId, settingsTitle, pen
dingSettingsIntent); |
554 | 558 |
555 notificationBuilder.setDefaults(makeDefaults(vibrationPattern.length, si
lent)); | 559 // The Android framework applies a fallback vibration pattern for the so
und when the device |
556 if (vibrationPattern.length > 0) { | 560 // is in vibrate mode, there is no custom pattern, and the vibration def
ault has been |
557 notificationBuilder.setVibrate(makeVibrationPattern(vibrationPattern
)); | 561 // disabled. To truly prevent vibration, provide a custom empty pattern. |
| 562 boolean vibrateEnabled = PrefServiceBridge.getInstance().isNotifications
VibrateEnabled(); |
| 563 if (!vibrateEnabled) { |
| 564 vibrationPattern = EMPTY_VIBRATION_PATTERN; |
558 } | 565 } |
| 566 notificationBuilder.setDefaults( |
| 567 makeDefaults(vibrationPattern.length, silent, vibrateEnabled)); |
| 568 notificationBuilder.setVibrate(makeVibrationPattern(vibrationPattern)); |
559 | 569 |
560 String platformTag = makePlatformTag(persistentNotificationId, origin, t
ag); | 570 String platformTag = makePlatformTag(persistentNotificationId, origin, t
ag); |
561 if (webApkPackage.isEmpty()) { | 571 if (webApkPackage.isEmpty()) { |
562 mNotificationManager.notify(platformTag, PLATFORM_ID, notificationBu
ilder.build()); | 572 mNotificationManager.notify(platformTag, PLATFORM_ID, notificationBu
ilder.build()); |
563 } else { | 573 } else { |
564 WebApkNotificationClient.notifyNotification( | 574 WebApkNotificationClient.notifyNotification( |
565 webApkPackage, notificationBuilder, platformTag, PLATFORM_ID
); | 575 webApkPackage, notificationBuilder, platformTag, PLATFORM_ID
); |
566 } | 576 } |
567 } | 577 } |
568 | 578 |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
725 | 735 |
726 private static native void nativeInitializeNotificationPlatformBridge(); | 736 private static native void nativeInitializeNotificationPlatformBridge(); |
727 | 737 |
728 private native void nativeOnNotificationClicked(long nativeNotificationPlatf
ormBridgeAndroid, | 738 private native void nativeOnNotificationClicked(long nativeNotificationPlatf
ormBridgeAndroid, |
729 long persistentNotificationId, String origin, String profileId, bool
ean incognito, | 739 long persistentNotificationId, String origin, String profileId, bool
ean incognito, |
730 String tag, String webApkPackage, int actionIndex); | 740 String tag, String webApkPackage, int actionIndex); |
731 private native void nativeOnNotificationClosed(long nativeNotificationPlatfo
rmBridgeAndroid, | 741 private native void nativeOnNotificationClosed(long nativeNotificationPlatfo
rmBridgeAndroid, |
732 long persistentNotificationId, String origin, String profileId, bool
ean incognito, | 742 long persistentNotificationId, String origin, String profileId, bool
ean incognito, |
733 String tag, boolean byUser); | 743 String tag, boolean byUser); |
734 } | 744 } |
OLD | NEW |