From 33e9656a532baf1633a4236b83ccc5603300b3a9 Mon Sep 17 00:00:00 2001 From: Andrei Stingaceanu Date: Wed, 27 Jan 2016 15:29:03 +0000 Subject: [PATCH] Settings - disable notification settings for suspended apps When we suspend an app we disable notifications for the app (via notification manager as in Settings). While an app is suspended do not allow the user to reach the NotificationSettings screen. Bug: 22776761 Change-Id: Ia0dbad6adce64b7d9238e1215a37c73e192dbc45 --- .../notification/AppNotificationSettings.java | 18 +++++++--- .../NotificationSettingsBase.java | 35 ++++++++++++++++--- .../TopicNotificationSettings.java | 5 +-- 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/src/com/android/settings/notification/AppNotificationSettings.java b/src/com/android/settings/notification/AppNotificationSettings.java index f3e95abb5d9..4fcec269020 100644 --- a/src/com/android/settings/notification/AppNotificationSettings.java +++ b/src/com/android/settings/notification/AppNotificationSettings.java @@ -38,6 +38,10 @@ import com.android.settings.R; import com.android.settings.Utils; import com.android.settings.applications.AppInfoBase; import com.android.settings.notification.NotificationBackend.AppRow; +import com.android.settingslib.RestrictedLockUtils; +import com.android.settingslib.RestrictedSwitchPreference; +import com.android.settingslib.RestrictedPreference; + import java.util.List; @@ -54,7 +58,7 @@ public class AppNotificationSettings extends NotificationSettingsBase { = new Intent(Intent.ACTION_MAIN) .addCategory(Notification.INTENT_CATEGORY_NOTIFICATION_PREFERENCES); - private SwitchPreference mBlock; + private RestrictedSwitchPreference mBlock; private PreferenceCategory mCategories; private AppRow mAppRow; @@ -97,7 +101,9 @@ public class AppNotificationSettings extends NotificationSettingsBase { mCategories.setOrderingAsAdded(true); getPreferenceScreen().addPreference(mCategories); for (Notification.Topic topic : topics) { - Preference topicPreference = new Preference(getPrefContext()); + RestrictedPreference topicPreference = new RestrictedPreference(getPrefContext()); + topicPreference.setDisabledByAdmin( + RestrictedLockUtils.checkIfApplicationIsSuspended(mContext, mPkg, mUserId)); topicPreference.setKey(topic.getId()); topicPreference.setTitle(topic.getLabel()); // Create intent for this preference. @@ -138,6 +144,8 @@ public class AppNotificationSettings extends NotificationSettingsBase { private void updateDependents(boolean banned) { if (mBlock != null) { mBlock.setEnabled(!mAppRow.systemApp); + mBlock.setDisabledByAdmin( + RestrictedLockUtils.checkIfApplicationIsSuspended(mContext, mPkg, mUserId)); } if (mCategories != null) { setVisible(mCategories, !banned); @@ -145,7 +153,9 @@ public class AppNotificationSettings extends NotificationSettingsBase { } private void setupBlockSwitch() { - mBlock = new SwitchPreference(getPrefContext()); + mBlock = new RestrictedSwitchPreference(getPrefContext()); + mBlock.setDisabledByAdmin( + RestrictedLockUtils.checkIfApplicationIsSuspended(mContext, mPkg, mUserId)); mBlock.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { @@ -153,7 +163,7 @@ public class AppNotificationSettings extends NotificationSettingsBase { if (banned) { MetricsLogger.action(getActivity(), MetricsEvent.ACTION_BAN_APP_NOTES, mPkg); } - final boolean success = mBackend.setNotificationsBanned(mPkg, mUid, banned); + final boolean success = mBackend.setNotificationsBanned(mPkg, mUid, banned); if (success) { updateDependents(banned); } diff --git a/src/com/android/settings/notification/NotificationSettingsBase.java b/src/com/android/settings/notification/NotificationSettingsBase.java index a669d9548c3..92d8d2f5208 100644 --- a/src/com/android/settings/notification/NotificationSettingsBase.java +++ b/src/com/android/settings/notification/NotificationSettingsBase.java @@ -19,6 +19,8 @@ package com.android.settings.notification; import com.android.settings.R; import com.android.settings.SettingsPreferenceFragment; import com.android.settings.applications.AppInfoBase; +import com.android.settingslib.RestrictedLockUtils; +import com.android.settingslib.RestrictedSwitchPreference; import android.app.Notification; import android.content.Context; @@ -27,6 +29,7 @@ import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.os.Bundle; +import android.os.UserHandle; import android.provider.Settings; import android.service.notification.NotificationListenerService.Ranking; import android.support.v14.preference.SwitchPreference; @@ -35,6 +38,8 @@ import android.text.TextUtils; import android.util.Log; import android.widget.Toast; +import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; + abstract public class NotificationSettingsBase extends SettingsPreferenceFragment { private static final String TAG = "NotifiSettingsBase"; private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); @@ -50,11 +55,12 @@ abstract public class NotificationSettingsBase extends SettingsPreferenceFragmen protected Context mContext; protected boolean mCreated; protected int mUid; + protected int mUserId; protected String mPkg; protected PackageInfo mPkgInfo; protected ImportanceSeekBarPreference mImportance; - protected SwitchPreference mPriority; - protected SwitchPreference mSensitive; + protected RestrictedSwitchPreference mPriority; + protected RestrictedSwitchPreference mSensitive; @Override public void onActivityCreated(Bundle savedInstanceState) { @@ -94,6 +100,7 @@ abstract public class NotificationSettingsBase extends SettingsPreferenceFragmen toastAndFinish(); return; } + mUserId = UserHandle.getUserId(mUid); if (DEBUG) Log.d(TAG, "Load details for pkg=" + mPkg + " uid=" + mUid); mPkgInfo = args != null && args.containsKey(ARG_PACKAGE_INFO) @@ -112,9 +119,21 @@ abstract public class NotificationSettingsBase extends SettingsPreferenceFragmen @Override public void onResume() { super.onResume(); - if (mUid != -1 && getPackageManager().getPackagesForUid(mUid) == null) { + if ((mUid != -1 && getPackageManager().getPackagesForUid(mUid) == null)) { // App isn't around anymore, must have been removed. finish(); + return; + } + EnforcedAdmin admin = RestrictedLockUtils.checkIfApplicationIsSuspended( + mContext, mPkg, mUserId); + if (mImportance != null) { + mImportance.setDisabledByAdmin(admin); + } + if (mPriority != null) { + mPriority.setDisabledByAdmin(admin); + } + if (mSensitive != null) { + mSensitive.setDisabledByAdmin(admin); } } @@ -122,6 +141,8 @@ abstract public class NotificationSettingsBase extends SettingsPreferenceFragmen final Notification.Topic topic, int importance) { if (mImportance == null) { mImportance = new ImportanceSeekBarPreference(getPrefContext()); + mImportance.setDisabledByAdmin( + RestrictedLockUtils.checkIfApplicationIsSuspended(mContext, mPkg, mUserId)); mImportance.setTitle(R.string.notification_importance_title); mImportance.setKey(KEY_IMPORTANCE); getPreferenceScreen().addPreference(mImportance); @@ -146,7 +167,9 @@ abstract public class NotificationSettingsBase extends SettingsPreferenceFragmen protected void setupPriorityPref(final Notification.Topic topic, boolean priority) { if (mPriority == null) { - mPriority = new SwitchPreference(getPrefContext()); + mPriority = new RestrictedSwitchPreference(getPrefContext()); + mPriority.setDisabledByAdmin( + RestrictedLockUtils.checkIfApplicationIsSuspended(mContext, mPkg, mUserId)); mPriority.setTitle(R.string.app_notification_override_dnd_title); mPriority.setSummary(R.string.app_notification_override_dnd_summary); mPriority.setKey(KEY_BYPASS_DND); @@ -164,7 +187,9 @@ abstract public class NotificationSettingsBase extends SettingsPreferenceFragmen protected void setupSensitivePref(final Notification.Topic topic, boolean sensitive) { if (mSensitive == null) { - mSensitive = new SwitchPreference(getPrefContext()); + mSensitive = new RestrictedSwitchPreference(getPrefContext()); + mSensitive.setDisabledByAdmin( + RestrictedLockUtils.checkIfApplicationIsSuspended(mContext, mPkg, mUserId)); mSensitive.setTitle(R.string.app_notification_sensitive_title); mSensitive.setSummary(R.string.app_notification_sensitive_summary); mSensitive.setKey(KEY_SENSITIVE); diff --git a/src/com/android/settings/notification/TopicNotificationSettings.java b/src/com/android/settings/notification/TopicNotificationSettings.java index 96104e440e2..ce4c4778df1 100644 --- a/src/com/android/settings/notification/TopicNotificationSettings.java +++ b/src/com/android/settings/notification/TopicNotificationSettings.java @@ -23,6 +23,7 @@ import com.android.settings.R; import com.android.settings.SettingsPreferenceFragment; import com.android.settings.applications.AppInfoBase; import com.android.settings.notification.NotificationBackend.TopicRow; +import com.android.settingslib.RestrictedSwitchPreference; import android.app.Notification; import android.app.NotificationManager; @@ -85,10 +86,10 @@ public class TopicNotificationSettings extends NotificationSettingsBase { mImportance = (ImportanceSeekBarPreference) findPreference(KEY_IMPORTANCE); setupImportancePref(mTopicRow, mTopicRow.topic, mTopicRow.importance); - mPriority = (SwitchPreference) findPreference(KEY_BYPASS_DND); + mPriority = (RestrictedSwitchPreference) findPreference(KEY_BYPASS_DND); setupPriorityPref(mTopicRow.topic, mTopicRow.priority); - mSensitive = (SwitchPreference) findPreference(KEY_SENSITIVE); + mSensitive = (RestrictedSwitchPreference) findPreference(KEY_SENSITIVE); setupSensitivePref(mTopicRow.topic, mTopicRow.sensitive); updateDependents(mTopicRow.importance);