Support showing notifications as badges.

And remove unneeded app level settings.

Change-Id: I88f506b0704195181d1748a3135f8ee0f39b774f
Test: manual for now. will add tests when have real mocks for the pages
This commit is contained in:
Julia Reynolds
2016-12-16 15:51:59 -05:00
parent dea703544a
commit d6835a20fc
11 changed files with 351 additions and 766 deletions

View File

@@ -16,17 +16,27 @@
package com.android.settings.notification;
import static android.app.NotificationManager.IMPORTANCE_HIGH;
import static android.app.NotificationManager.IMPORTANCE_LOW;
import static android.app.NotificationManager.IMPORTANCE_MIN;
import static android.app.NotificationManager.IMPORTANCE_NONE;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.admin.DevicePolicyManager;
import android.content.Intent;
import android.content.pm.UserInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.UserHandle;
import android.provider.Settings;
import android.service.notification.NotificationListenerService.Ranking;
import android.support.v7.preference.Preference;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.internal.widget.LockPatternUtils;
import com.android.settings.AppHeader;
import com.android.settings.R;
import com.android.settings.RingtonePreference;
@@ -36,17 +46,26 @@ import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.RestrictedSwitchPreference;
import java.util.ArrayList;
public class ChannelNotificationSettings extends NotificationSettingsBase {
protected static final String KEY_BYPASS_DND = "bypass_dnd";
protected static final String KEY_VISIBILITY_OVERRIDE = "visibility_override";
protected static final String KEY_IMPORTANCE = "importance";
protected static final String KEY_LIGHTS = "lights";
protected static final String KEY_VIBRATE = "vibrate";
protected static final String KEY_RINGTONE = "ringtone";
protected static final String KEY_SHOW = "show";
protected static final String KEY_BADGE = "badge";
protected RestrictedSwitchPreference mLights;
protected RestrictedSwitchPreference mVibrate;
protected DefaultNotificationTonePreference mRingtone;
protected RestrictedDropDownPreference mImportance;
protected RestrictedSwitchPreference mPriority;
protected RestrictedDropDownPreference mVisibilityOverride;
private DashboardFeatureProvider mDashboardFeatureProvider;
private int mMaxImportance;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
@@ -71,34 +90,26 @@ public class ChannelNotificationSettings extends NotificationSettingsBase {
FeatureFactory.getFactory(activity).getDashboardFeatureProvider(activity);
addPreferencesFromResource(R.xml.channel_notification_settings);
mImportance = (ImportanceSeekBarPreference) findPreference(KEY_IMPORTANCE);
mBlock = (RestrictedSwitchPreference) getPreferenceScreen().findPreference(KEY_BLOCK);
mShow = (RestrictedSwitchPreference) getPreferenceScreen().findPreference(KEY_SHOW);
mBadge = (RestrictedSwitchPreference) getPreferenceScreen().findPreference(KEY_BADGE);
mImportance = (RestrictedDropDownPreference) findPreference(KEY_IMPORTANCE);
mPriority =
(RestrictedSwitchPreference) findPreference(KEY_BYPASS_DND);
mVisibilityOverride =
(RestrictedDropDownPreference) findPreference(KEY_VISIBILITY_OVERRIDE);
mBlock = (RestrictedSwitchPreference) findPreference(KEY_BLOCK);
mSilent = (RestrictedSwitchPreference) findPreference(KEY_SILENT);
mLights = (RestrictedSwitchPreference) findPreference(KEY_LIGHTS);
mVibrate = (RestrictedSwitchPreference) findPreference(KEY_VIBRATE);
mRingtone = (DefaultNotificationTonePreference) findPreference(KEY_RINGTONE);
if (mPkgInfo != null) {
setupPriorityPref(mChannel.canBypassDnd());
if (mAppRow.appBypassDnd) {
mPriority.setShouldDisableView(true);
}
setupVisOverridePref(mChannel.getLockscreenVisibility());
if (mAppRow.appVisOverride != Ranking.VISIBILITY_NO_OVERRIDE) {
mVisibilityOverride.setShouldDisableView(true);
}
setupLights();
setupVibrate();
setupRingtone();
mMaxImportance = mAppRow.appImportance == NotificationManager.IMPORTANCE_UNSPECIFIED
? NotificationManager.IMPORTANCE_HIGH : mAppRow.appImportance;
setupImportancePrefs(false, mChannel.getImportance(),
mChannel.getImportance() == NotificationManager.IMPORTANCE_NONE,
mMaxImportance);
setupBlockAndImportance();
updateDependents();
}
if (mDashboardFeatureProvider.isEnabled()) {
final Preference pref = FeatureFactory.getFactory(activity)
@@ -124,14 +135,14 @@ public class ChannelNotificationSettings extends NotificationSettingsBase {
finish();
return;
}
mSuspendedAppsAdmin = RestrictedLockUtils.checkIfApplicationIsSuspended(
mContext, mPkg, mUserId);
if (mLights != null) {
mLights.setDisabledByAdmin(mSuspendedAppsAdmin);
}
if (mVibrate != null) {
mVibrate.setDisabledByAdmin(mSuspendedAppsAdmin);
}
mLights.setDisabledByAdmin(mSuspendedAppsAdmin);
mVibrate.setDisabledByAdmin(mSuspendedAppsAdmin);
mImportance.setDisabledByAdmin(mSuspendedAppsAdmin);
mPriority.setDisabledByAdmin(mSuspendedAppsAdmin);
mVisibilityOverride.setDisabledByAdmin(mSuspendedAppsAdmin);
mBlock.setDisabledByAdmin(mSuspendedAppsAdmin);
mShow.setDisabledByAdmin(mSuspendedAppsAdmin);
mBadge.setDisabledByAdmin(mSuspendedAppsAdmin);
}
private void setupLights() {
@@ -179,6 +190,209 @@ public class ChannelNotificationSettings extends NotificationSettingsBase {
});
}
protected void setupBlockAndImportance() {
mBlock.setDisabledByAdmin(mSuspendedAppsAdmin);
mBlock.setChecked(!mChannel.isAllowed());
mBlock.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
final boolean value = (Boolean) newValue;
mChannel.setAllowed(!value);
mChannel.lockFields(NotificationChannel.USER_LOCKED_ALLOWED);
mBackend.updateChannel(mPkg, mUid, mChannel);
updateDependents();
return true;
}
});
mShow.setDisabledByAdmin(mSuspendedAppsAdmin);
mShow.setChecked(mChannel.getImportance() != NotificationManager.IMPORTANCE_NONE);
mShow.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
final boolean value = (Boolean) newValue;
int importance = value ? IMPORTANCE_LOW : IMPORTANCE_NONE;
mImportance.setValue(String.valueOf(importance));
mChannel.setImportance(importance);
mChannel.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE);
mBackend.updateChannel(mPkg, mUid, mChannel);
updateDependents();
return true;
}
});
mBadge.setDisabledByAdmin(mSuspendedAppsAdmin);
mBadge.setChecked(mChannel.canShowBadge());
mBadge.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
final boolean value = (Boolean) newValue;
mChannel.setShowBadge(value);
mChannel.lockFields(NotificationChannel.USER_LOCKED_SHOW_BADGE);
mBackend.updateChannel(mPkg, mUid, mChannel);
return true;
}
});
mImportance.setDisabledByAdmin(mSuspendedAppsAdmin);
final int numImportances = IMPORTANCE_HIGH - IMPORTANCE_MIN + 1;
String[] summaries = new String[numImportances];
String[] values = new String[numImportances];
for (int i = 0; i < numImportances; i++) {
int importance = i + 1;
summaries[i] = getSummary(importance);
values[i] = String.valueOf(importance);
}
mImportance.setEntryValues(values);
mImportance.setEntries(summaries);
mImportance.setValue(String.valueOf(mChannel.getImportance()));
mImportance.setSummary("%s");
mImportance.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
int importance = Integer.parseInt((String) newValue);
mChannel.setImportance(importance);
mChannel.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE);
mBackend.updateChannel(mPkg, mUid, mChannel);
updateDependents();
return true;
}
});
}
private String getSummary(int importance) {
switch (importance) {
case NotificationManager.IMPORTANCE_NONE:
return getContext().getString(R.string.notification_importance_blocked);
case NotificationManager.IMPORTANCE_MIN:
return getContext().getString(R.string.notification_importance_min);
case NotificationManager.IMPORTANCE_LOW:
return getContext().getString(R.string.notification_importance_low);
case NotificationManager.IMPORTANCE_DEFAULT:
return getContext().getString(R.string.notification_importance_default);
case NotificationManager.IMPORTANCE_HIGH:
case NotificationManager.IMPORTANCE_MAX:
default:
return getContext().getString(R.string.notification_importance_high);
}
}
protected void setupPriorityPref(boolean priority) {
mPriority.setDisabledByAdmin(mSuspendedAppsAdmin);
mPriority.setChecked(priority);
mPriority.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
final boolean bypassZenMode = (Boolean) newValue;
mChannel.setBypassDnd(bypassZenMode);
mChannel.lockFields(NotificationChannel.USER_LOCKED_PRIORITY);
mBackend.updateChannel(mPkg, mUid, mChannel);
return true;
}
});
}
protected void setupVisOverridePref(int sensitive) {
ArrayList<CharSequence> entries = new ArrayList<>();
ArrayList<CharSequence> values = new ArrayList<>();
mVisibilityOverride.clearRestrictedItems();
if (getLockscreenNotificationsEnabled() && getLockscreenAllowPrivateNotifications()) {
final String summaryShowEntry =
getString(R.string.lock_screen_notifications_summary_show);
final String summaryShowEntryValue =
Integer.toString(NotificationManager.VISIBILITY_NO_OVERRIDE);
entries.add(summaryShowEntry);
values.add(summaryShowEntryValue);
setRestrictedIfNotificationFeaturesDisabled(summaryShowEntry, summaryShowEntryValue,
DevicePolicyManager.KEYGUARD_DISABLE_SECURE_NOTIFICATIONS
| DevicePolicyManager.KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS);
}
final String summaryHideEntry = getString(R.string.lock_screen_notifications_summary_hide);
final String summaryHideEntryValue = Integer.toString(Notification.VISIBILITY_PRIVATE);
entries.add(summaryHideEntry);
values.add(summaryHideEntryValue);
setRestrictedIfNotificationFeaturesDisabled(summaryHideEntry, summaryHideEntryValue,
DevicePolicyManager.KEYGUARD_DISABLE_SECURE_NOTIFICATIONS);
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) {
int sensitive = Integer.parseInt((String) newValue);
if (sensitive == getGlobalVisibility()) {
sensitive = Ranking.VISIBILITY_NO_OVERRIDE;
}
mChannel.setLockscreenVisibility(sensitive);
mChannel.lockFields(NotificationChannel.USER_LOCKED_VISIBILITY);
mBackend.updateChannel(mPkg, mUid, mChannel);
return true;
}
});
}
private void setRestrictedIfNotificationFeaturesDisabled(CharSequence entry,
CharSequence entryValue, int keyguardNotificationFeatures) {
RestrictedLockUtils.EnforcedAdmin admin =
RestrictedLockUtils.checkIfKeyguardFeaturesDisabled(
mContext, keyguardNotificationFeatures, mUserId);
if (admin != null) {
RestrictedDropDownPreference.RestrictedItem item =
new RestrictedDropDownPreference.RestrictedItem(entry, entryValue, admin);
mVisibilityOverride.addRestrictedItem(item);
}
}
private int getGlobalVisibility() {
int globalVis = Ranking.VISIBILITY_NO_OVERRIDE;
if (!getLockscreenNotificationsEnabled()) {
globalVis = Notification.VISIBILITY_SECRET;
} else if (!getLockscreenAllowPrivateNotifications()) {
globalVis = Notification.VISIBILITY_PRIVATE;
}
return globalVis;
}
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 boolean isLockScreenSecure() {
LockPatternUtils utils = new LockPatternUtils(getActivity());
boolean lockscreenSecure = utils.isSecure(UserHandle.myUserId());
UserInfo parentUser = mUm.getProfileParent(UserHandle.myUserId());
if (parentUser != null){
lockscreenSecure |= utils.isSecure(parentUser.id);
}
return lockscreenSecure;
}
protected boolean checkCanBeVisible(int minImportanceVisible, int importance) {
if (importance == NotificationManager.IMPORTANCE_UNSPECIFIED) {
return true;
}
return importance >= minImportanceVisible;
}
@Override
public boolean onPreferenceTreeClick(Preference preference) {
if (preference instanceof RingtonePreference) {
@@ -205,22 +419,24 @@ public class ChannelNotificationSettings extends NotificationSettingsBase {
Settings.System.NOTIFICATION_LIGHT_PULSE, 0) == 1;
}
@Override
protected void updateDependents(int importance) {
if (importance == NotificationManager.IMPORTANCE_UNSPECIFIED) {
importance = mMaxImportance;
}
importance = Math.min(mMaxImportance, importance);
super.updateDependents(importance);
setVisible(mLights, checkCanBeVisible(
private void updateDependents() {
boolean allowed = mChannel.isAllowed();
int importance = mChannel.getImportance();
setVisible(mShow, allowed);
setVisible(mBadge, allowed);
setVisible(mImportance, allowed && importance != NotificationManager.IMPORTANCE_NONE);
setVisible(mLights, allowed && checkCanBeVisible(
NotificationManager.IMPORTANCE_LOW, importance) && canPulseLight());
setVisible(mVibrate, checkCanBeVisible(NotificationManager.IMPORTANCE_DEFAULT, importance));
setVisible(mRingtone, checkCanBeVisible(
setVisible(mVibrate, allowed
&& checkCanBeVisible(NotificationManager.IMPORTANCE_DEFAULT, importance));
setVisible(mRingtone, allowed && checkCanBeVisible(
NotificationManager.IMPORTANCE_DEFAULT, importance));
if (mMaxImportance == NotificationManager.IMPORTANCE_LOW
&& getPreferenceScreen().findPreference(mBlock.getKey()) != null) {
setVisible(mSilent, false);
}
setVisible(mPriority, allowed
&& (checkCanBeVisible(NotificationManager.IMPORTANCE_DEFAULT, importance)
|| (checkCanBeVisible(NotificationManager.IMPORTANCE_LOW, importance)
&& mDndVisualEffectsSuppressed)));
setVisible(mVisibilityOverride, allowed
&& checkCanBeVisible(NotificationManager.IMPORTANCE_MIN, importance)
&& isLockScreenSecure());
}
}