From 60e90ac07cd165bcc004040356f29e0dc7d78973 Mon Sep 17 00:00:00 2001 From: Julia Reynolds Date: Wed, 2 Mar 2016 08:54:56 -0500 Subject: [PATCH] Allow notifications be to hidden from the lockscreen by app. Bug: 26642033 Change-Id: I932defebe00c566c0a930a0cabc118238c349272 --- res/values/strings.xml | 7 +- res/xml/app_notification_settings.xml | 9 ++- .../notification/AppNotificationSettings.java | 25 ++----- .../notification/NotificationBackend.java | 16 ++--- .../NotificationSettingsBase.java | 66 +++++++++++++++---- 5 files changed, 75 insertions(+), 48 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index e9008328b98..91099ac82c5 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -6037,11 +6037,8 @@ Let these notifications continue to interrupt when Do Not Disturb is set to Priority Only - - Hide sensitive content - - - When the device is locked, hide content in these notifications that might reveal private information + + On the lock screen Blocked diff --git a/res/xml/app_notification_settings.xml b/res/xml/app_notification_settings.xml index 37c4426f02e..8bd0c88aee3 100644 --- a/res/xml/app_notification_settings.xml +++ b/res/xml/app_notification_settings.xml @@ -45,11 +45,10 @@ android:layout="@layout/two_buttons_panel" android:order="6" /> - - + diff --git a/src/com/android/settings/notification/AppNotificationSettings.java b/src/com/android/settings/notification/AppNotificationSettings.java index c82ad198426..f3933001845 100644 --- a/src/com/android/settings/notification/AppNotificationSettings.java +++ b/src/com/android/settings/notification/AppNotificationSettings.java @@ -24,8 +24,8 @@ import android.content.pm.ApplicationInfo; import android.content.pm.ResolveInfo; import android.os.Bundle; import android.os.UserHandle; -import android.provider.Settings; import android.service.notification.NotificationListenerService.Ranking; +import android.support.v7.preference.DropDownPreference; import android.util.ArrayMap; import android.util.Log; @@ -76,8 +76,9 @@ public class AppNotificationSettings extends NotificationSettingsBase { mImportanceTitle = (RestrictedPreference) findPreference(KEY_IMPORTANCE_TITLE); mPriority = (RestrictedSwitchPreference) getPreferenceScreen().findPreference(KEY_BYPASS_DND); - mSensitive = - (RestrictedSwitchPreference) getPreferenceScreen().findPreference(KEY_SENSITIVE); + mVisibilityOverride = + (DropDownPreference) getPreferenceScreen().findPreference( + KEY_VISIBILITY_OVERRIDE); mBlock = (RestrictedSwitchPreference) getPreferenceScreen().findPreference(KEY_BLOCK); mSilent = (RestrictedSwitchPreference) getPreferenceScreen().findPreference(KEY_SILENT); @@ -94,7 +95,7 @@ public class AppNotificationSettings extends NotificationSettingsBase { setupImportancePrefs(mAppRow.systemApp, mAppRow.appImportance, mAppRow.banned); setupPriorityPref(mAppRow.appBypassDnd); - setupSensitivePref(mAppRow.appSensitive); + setupVisOverridePref(mAppRow.appVisOverride); updateDependents(mAppRow.appImportance); } @@ -102,8 +103,6 @@ public class AppNotificationSettings extends NotificationSettingsBase { protected void updateDependents(int importance) { final boolean lockscreenSecure = new LockPatternUtils(getActivity()).isSecure( UserHandle.myUserId()); - final boolean lockscreenNotificationsEnabled = getLockscreenNotificationsEnabled(); - final boolean allowPrivate = getLockscreenAllowPrivateNotifications(); if (getPreferenceScreen().findPreference(mBlock.getKey()) != null) { setVisible(mSilent, checkCanBeVisible(Ranking.IMPORTANCE_MIN, importance)); @@ -111,8 +110,8 @@ public class AppNotificationSettings extends NotificationSettingsBase { } setVisible(mPriority, checkCanBeVisible(Ranking.IMPORTANCE_DEFAULT, importance) && !mDndVisualEffectsSuppressed); - setVisible(mSensitive, checkCanBeVisible(Ranking.IMPORTANCE_MIN, importance) - && lockscreenSecure && lockscreenNotificationsEnabled && allowPrivate); + setVisible(mVisibilityOverride, + checkCanBeVisible(Ranking.IMPORTANCE_MIN, importance) && lockscreenSecure); } protected boolean checkCanBeVisible(int minImportanceVisible, int importance) { @@ -122,16 +121,6 @@ public class AppNotificationSettings extends NotificationSettingsBase { return importance >= minImportanceVisible; } - private boolean getLockscreenNotificationsEnabled() { - return Settings.Secure.getInt(getContentResolver(), - Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 0) != 0; - } - - private boolean getLockscreenAllowPrivateNotifications() { - return Settings.Secure.getInt(getContentResolver(), - Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 0) != 0; - } - private List queryNotificationConfigActivities() { if (DEBUG) Log.d(TAG, "APP_NOTIFICATION_PREFS_CATEGORY_INTENT is " + APP_NOTIFICATION_PREFS_CATEGORY_INTENT); diff --git a/src/com/android/settings/notification/NotificationBackend.java b/src/com/android/settings/notification/NotificationBackend.java index 772fa196844..c418769d04e 100644 --- a/src/com/android/settings/notification/NotificationBackend.java +++ b/src/com/android/settings/notification/NotificationBackend.java @@ -48,7 +48,7 @@ public class NotificationBackend { row.banned = getNotificationsBanned(row.pkg, row.uid); row.appImportance = getImportance(row.pkg, row.uid); row.appBypassDnd = getBypassZenMode(row.pkg, row.uid); - row.appSensitive = getSensitive(row.pkg, row.uid); + row.appVisOverride = getVisibilityOverride(row.pkg, row.uid); return row; } @@ -88,20 +88,18 @@ public class NotificationBackend { } } - public boolean getSensitive(String pkg, int uid) { + public int getVisibilityOverride(String pkg, int uid) { try { - return sINM.getVisibilityOverride(pkg, uid) == Notification.VISIBILITY_PRIVATE; + return sINM.getVisibilityOverride(pkg, uid); } catch (Exception e) { Log.w(TAG, "Error calling NoMan", e); - return false; + return NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE; } } - public boolean setSensitive(String pkg, int uid, boolean sensitive) { + public boolean setVisibilityOverride(String pkg, int uid, int override) { try { - sINM.setVisibilityOverride(pkg, uid, - sensitive ? Notification.VISIBILITY_PRIVATE - : NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE); + sINM.setVisibilityOverride(pkg, uid, override); return true; } catch (Exception e) { Log.w(TAG, "Error calling NoMan", e); @@ -143,6 +141,6 @@ public class NotificationBackend { public boolean systemApp; public int appImportance; public boolean appBypassDnd; - public boolean appSensitive; + public int appVisOverride; } } diff --git a/src/com/android/settings/notification/NotificationSettingsBase.java b/src/com/android/settings/notification/NotificationSettingsBase.java index ee0cac076bf..314c2f8d0c7 100644 --- a/src/com/android/settings/notification/NotificationSettingsBase.java +++ b/src/com/android/settings/notification/NotificationSettingsBase.java @@ -34,6 +34,7 @@ import android.os.Bundle; import android.os.UserHandle; import android.provider.Settings; import android.service.notification.NotificationListenerService.Ranking; +import android.support.v7.preference.DropDownPreference; import android.support.v7.preference.Preference; import android.text.TextUtils; import android.util.Log; @@ -41,6 +42,8 @@ import android.view.View; import android.widget.Button; import android.widget.Toast; +import java.util.ArrayList; + import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; abstract public class NotificationSettingsBase extends SettingsPreferenceFragment { @@ -49,7 +52,7 @@ abstract public class NotificationSettingsBase extends SettingsPreferenceFragmen private static final String TUNER_SETTING = "show_importance_slider"; protected static final String KEY_BYPASS_DND = "bypass_dnd"; - protected static final String KEY_SENSITIVE = "sensitive"; + protected static final String KEY_VISIBILITY_OVERRIDE = "visibility_override"; protected static final String KEY_IMPORTANCE = "importance"; protected static final String KEY_IMPORTANCE_TITLE = "importance_title"; protected static final String KEY_IMPORTANCE_RESET = "importance_reset_button"; @@ -68,7 +71,7 @@ abstract public class NotificationSettingsBase extends SettingsPreferenceFragmen protected RestrictedPreference mImportanceTitle; protected LayoutPreference mImportanceReset; protected RestrictedSwitchPreference mPriority; - protected RestrictedSwitchPreference mSensitive; + protected DropDownPreference mVisibilityOverride; protected RestrictedSwitchPreference mBlock; protected RestrictedSwitchPreference mSilent; protected EnforcedAdmin mSuspendedAppsAdmin; @@ -144,9 +147,6 @@ abstract public class NotificationSettingsBase extends SettingsPreferenceFragmen if (mPriority != null) { mPriority.setDisabledByAdmin(mSuspendedAppsAdmin); } - if (mSensitive != null) { - mSensitive.setDisabledByAdmin(mSuspendedAppsAdmin); - } if (mImportanceTitle != null) { mImportanceTitle.setDisabledByAdmin(mSuspendedAppsAdmin); } @@ -273,18 +273,62 @@ abstract public class NotificationSettingsBase extends SettingsPreferenceFragmen }); } - protected void setupSensitivePref(boolean sensitive) { - mSensitive.setDisabledByAdmin(mSuspendedAppsAdmin); - mSensitive.setChecked(sensitive); - mSensitive.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + protected void setupVisOverridePref(int sensitive) { + ArrayList entries = new ArrayList<>(); + ArrayList values = new ArrayList<>(); + + if (getLockscreenNotificationsEnabled() && getLockscreenAllowPrivateNotifications()) { + entries.add(getString(R.string.lock_screen_notifications_summary_show)); + values.add(Integer.toString(Ranking.VISIBILITY_NO_OVERRIDE)); + } + + entries.add(getString(R.string.lock_screen_notifications_summary_hide)); + values.add(Integer.toString(Notification.VISIBILITY_PRIVATE)); + entries.add(getString(R.string.lock_screen_notifications_summary_disable)); + values.add(Integer.toString(Notification.VISIBILITY_SECRET)); + mVisibilityOverride.setEntries(entries.toArray(new CharSequence[entries.size()])); + mVisibilityOverride.setEntryValues(values.toArray(new CharSequence[values.size()])); + + if (sensitive == Ranking.VISIBILITY_NO_OVERRIDE) { + mVisibilityOverride.setValue(Integer.toString(getGlobalVisibility())); + } else { + mVisibilityOverride.setValue(Integer.toString(sensitive)); + } + mVisibilityOverride.setSummary("%s"); + + mVisibilityOverride.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { - final boolean sensitive = (Boolean) newValue; - return mBackend.setSensitive(mPkgInfo.packageName, mUid, sensitive); + int sensitive = Integer.parseInt((String) newValue); + if (sensitive == getGlobalVisibility()) { + sensitive = Ranking.VISIBILITY_NO_OVERRIDE; + } + mBackend.setVisibilityOverride(mPkgInfo.packageName, mUid, sensitive); + return true; } }); } + private int getGlobalVisibility() { + int globalVis = Ranking.VISIBILITY_NO_OVERRIDE; + if (!getLockscreenNotificationsEnabled()) { + globalVis = Notification.VISIBILITY_SECRET; + } else if (!getLockscreenAllowPrivateNotifications()) { + globalVis = Notification.VISIBILITY_PRIVATE; + } + return globalVis; + } + + protected boolean getLockscreenNotificationsEnabled() { + return Settings.Secure.getInt(getContentResolver(), + Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 0) != 0; + } + + protected boolean getLockscreenAllowPrivateNotifications() { + return Settings.Secure.getInt(getContentResolver(), + Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 0) != 0; + } + abstract void updateDependents(int progress); protected void setVisible(Preference p, boolean visible) {