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 361 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
404 URI uri = new URI(parts[1]); | 405 URI uri = new URI(parts[1]); |
405 if (uri.getHost() != null) return parts[1]; | 406 if (uri.getHost() != null) return parts[1]; |
406 } catch (URISyntaxException e) { | 407 } catch (URISyntaxException e) { |
407 Log.e(TAG, "Expected to find a valid url in the notification tag ext ra.", e); | 408 Log.e(TAG, "Expected to find a valid url in the notification tag ext ra.", e); |
408 return null; | 409 return null; |
409 } | 410 } |
410 return null; | 411 return null; |
411 } | 412 } |
412 | 413 |
413 /** | 414 /** |
414 * Generates the notfiication defaults from vibrationPattern's size and sile nt. | 415 * Generates the notification defaults from vibrationPattern's size and sile nt. |
415 * | 416 * |
416 * Use the system's default ringtone, vibration and indicator lights unless the notification | 417 * Use the system's default ringtone, vibration and indicator lights unless the notification |
417 * has been marked as being silent. | 418 * has been marked as being silent. |
418 * If a vibration pattern is set, the notification should use the provided p attern | 419 * If a vibration pattern is set, the notification should use the provided p attern |
419 * rather than the defaulting to system settings. | 420 * rather than defaulting to the system settings. |
420 * | 421 * |
421 * @param vibrationPatternLength Vibration pattern's size for the Notificati on. | 422 * @param vibrationPatternLength Vibration pattern's size for the Notificati on. |
422 * @param silent Whether the default sound, vibration and lights should be s uppressed. | 423 * @param silent Whether the default sound, vibration and lights should be s uppressed. |
424 * @param vibrateEnabled Whether vibration is enabled in preferences. | |
423 * @return The generated notification's default value. | 425 * @return The generated notification's default value. |
424 */ | 426 */ |
425 @VisibleForTesting | 427 @VisibleForTesting |
426 static int makeDefaults(int vibrationPatternLength, boolean silent) { | 428 static int makeDefaults(int vibrationPatternLength, boolean silent, boolean vibrateEnabled) { |
427 assert !silent || vibrationPatternLength == 0; | 429 assert !silent || vibrationPatternLength == 0; |
428 | 430 |
429 if (silent) return 0; | 431 if (silent) return 0; |
430 | 432 |
431 int defaults = Notification.DEFAULT_ALL; | 433 int defaults = Notification.DEFAULT_ALL; |
432 if (vibrationPatternLength > 0) { | 434 if (vibrationPatternLength > 0 || !vibrateEnabled) { |
433 defaults &= ~Notification.DEFAULT_VIBRATE; | 435 defaults &= ~Notification.DEFAULT_VIBRATE; |
434 } | 436 } |
435 return defaults; | 437 return defaults; |
436 } | 438 } |
437 | 439 |
438 /** | 440 /** |
439 * Generates the vibration pattern used in Android notification. | 441 * Generates the vibration pattern used in Android notification. |
440 * | 442 * |
441 * Android takes a long array where the first entry indicates the number of milliseconds to wait | 443 * 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. | 444 * 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. | 547 // the label and icon provided here only affect Android Wear, so don't a bbreviate them. |
546 boolean abbreviateSiteSettings = actionTitles.length > 0 && !useCustomLa youts(); | 548 boolean abbreviateSiteSettings = actionTitles.length > 0 && !useCustomLa youts(); |
547 int settingsIconId = abbreviateSiteSettings ? 0 : R.drawable.settings_co g; | 549 int settingsIconId = abbreviateSiteSettings ? 0 : R.drawable.settings_co g; |
548 CharSequence settingsTitle = abbreviateSiteSettings | 550 CharSequence settingsTitle = abbreviateSiteSettings |
549 ? res.getString(R.string.notification_site_ settings_button) | 551 ? res.getString(R.string.notification_site_ settings_button) |
550 : res.getString(R.string.page_info_site_set tings_button); | 552 : 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 | 553 // 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. | 554 // one, so add it after the other actions. |
553 notificationBuilder.addSettingsAction(settingsIconId, settingsTitle, pen dingSettingsIntent); | 555 notificationBuilder.addSettingsAction(settingsIconId, settingsTitle, pen dingSettingsIntent); |
554 | 556 |
555 notificationBuilder.setDefaults(makeDefaults(vibrationPattern.length, si lent)); | 557 // The NotificationManagerService in the core Android framework has a fa llback vibration |
556 if (vibrationPattern.length > 0) { | 558 // pattern. It is used when the device is in vibrate mode, there is no c ustom pattern, and |
557 notificationBuilder.setVibrate(makeVibrationPattern(vibrationPattern )); | 559 // the vibration default has been disabled, but there is a sound to play . Effectively, it |
560 // plays the fallback pattern as fallback for the sound, even though the default vibration | |
561 // has been turned off. So in order to truly prevent vibration, specify a custom empty | |
562 // pattern. | |
Peter Beverloo
2016/06/07 12:46:01
Can we make this significantly more concise? What
Michael van Ouwerkerk
2016/06/07 16:34:21
Sure, I did something shorter like that.
| |
563 boolean vibrateEnabled = PrefServiceBridge.getInstance().isNotifications VibrateEnabled(); | |
564 if (!vibrateEnabled) { | |
565 vibrationPattern = new int[0]; | |
558 } | 566 } |
567 notificationBuilder.setDefaults( | |
568 makeDefaults(vibrationPattern.length, silent, vibrateEnabled)); | |
569 notificationBuilder.setVibrate(makeVibrationPattern(vibrationPattern)); | |
559 | 570 |
560 String platformTag = makePlatformTag(persistentNotificationId, origin, t ag); | 571 String platformTag = makePlatformTag(persistentNotificationId, origin, t ag); |
561 if (webApkPackage.isEmpty()) { | 572 if (webApkPackage.isEmpty()) { |
562 mNotificationManager.notify(platformTag, PLATFORM_ID, notificationBu ilder.build()); | 573 mNotificationManager.notify(platformTag, PLATFORM_ID, notificationBu ilder.build()); |
563 } else { | 574 } else { |
564 WebApkNotificationClient.notifyNotification( | 575 WebApkNotificationClient.notifyNotification( |
565 webApkPackage, notificationBuilder, platformTag, PLATFORM_ID ); | 576 webApkPackage, notificationBuilder, platformTag, PLATFORM_ID ); |
566 } | 577 } |
567 } | 578 } |
568 | 579 |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
725 | 736 |
726 private static native void nativeInitializeNotificationPlatformBridge(); | 737 private static native void nativeInitializeNotificationPlatformBridge(); |
727 | 738 |
728 private native void nativeOnNotificationClicked(long nativeNotificationPlatf ormBridgeAndroid, | 739 private native void nativeOnNotificationClicked(long nativeNotificationPlatf ormBridgeAndroid, |
729 long persistentNotificationId, String origin, String profileId, bool ean incognito, | 740 long persistentNotificationId, String origin, String profileId, bool ean incognito, |
730 String tag, String webApkPackage, int actionIndex); | 741 String tag, String webApkPackage, int actionIndex); |
731 private native void nativeOnNotificationClosed(long nativeNotificationPlatfo rmBridgeAndroid, | 742 private native void nativeOnNotificationClosed(long nativeNotificationPlatfo rmBridgeAndroid, |
732 long persistentNotificationId, String origin, String profileId, bool ean incognito, | 743 long persistentNotificationId, String origin, String profileId, bool ean incognito, |
733 String tag, boolean byUser); | 744 String tag, boolean byUser); |
734 } | 745 } |
OLD | NEW |