Notification settings updates

- Hide channels when a group is blocked
- Update importance strings
- Allow app blocking from 'recently seen'

Test: RoboSettingsTests
Fixes: 72879584
Fixes: 72882969
Fixes: 72831483
Change-Id: I99e001d3eb9eef52251cd50da191d33923335fcf
This commit is contained in:
Julia Reynolds
2018-02-05 12:49:44 -05:00
parent 7262d37c53
commit 18f14f10cf
10 changed files with 489 additions and 50 deletions

View File

@@ -19,10 +19,8 @@ package com.android.settings.notification;
import android.app.NotificationChannel;
import android.app.NotificationChannelGroup;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v14.preference.SwitchPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceCategory;
import android.support.v7.preference.PreferenceGroup;
@@ -32,9 +30,8 @@ import android.util.Log;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.internal.widget.LockPatternUtils;
import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settings.applications.AppInfoBase;
import com.android.settings.widget.MasterSwitchPreference;
import com.android.settings.widget.MasterCheckBoxPreference;
import com.android.settingslib.RestrictedSwitchPreference;
import com.android.settingslib.core.AbstractPreferenceController;
import java.util.ArrayList;
@@ -179,17 +176,37 @@ public class AppNotificationSettings extends NotificationSettingsBase {
groupCategory.setKey(group.getId());
populateGroupToggle(groupCategory, group);
}
final List<NotificationChannel> channels = group.getChannels();
Collections.sort(channels, mChannelComparator);
int N = channels.size();
for (int i = 0; i < N; i++) {
final NotificationChannel channel = channels.get(i);
populateSingleChannelPrefs(groupCategory, channel, group.isBlocked());
if (!group.isBlocked()) {
final List<NotificationChannel> channels = group.getChannels();
Collections.sort(channels, mChannelComparator);
int N = channels.size();
for (int i = 0; i < N; i++) {
final NotificationChannel channel = channels.get(i);
populateSingleChannelPrefs(groupCategory, channel, group.isBlocked());
}
}
}
}
protected void populateGroupToggle(final PreferenceGroup parent,
NotificationChannelGroup group) {
RestrictedSwitchPreference preference = new RestrictedSwitchPreference(getPrefContext());
preference.setTitle(R.string.notification_switch_label);
preference.setEnabled(mSuspendedAppsAdmin == null
&& isChannelGroupBlockable(group));
preference.setChecked(!group.isBlocked());
preference.setOnPreferenceClickListener(preference1 -> {
final boolean allowGroup = ((SwitchPreference) preference1).isChecked();
group.setBlocked(!allowGroup);
mBackend.updateChannelGroup(mAppRow.pkg, mAppRow.uid, group);
onGroupBlockStateChanged(group);
return true;
});
parent.addPreference(preference);
}
private Comparator<NotificationChannelGroup> mChannelGroupComparator =
new Comparator<NotificationChannelGroup>() {
@@ -204,4 +221,37 @@ public class AppNotificationSettings extends NotificationSettingsBase {
return left.getId().compareTo(right.getId());
}
};
protected void onGroupBlockStateChanged(NotificationChannelGroup group) {
if (group == null) {
return;
}
PreferenceGroup groupGroup = (
PreferenceGroup) getPreferenceScreen().findPreference(group.getId());
if (groupGroup != null) {
if (group.isBlocked()) {
List<Preference> toRemove = new ArrayList<>();
int childCount = groupGroup.getPreferenceCount();
for (int i = 0; i < childCount; i++) {
Preference pref = groupGroup.getPreference(i);
if (pref instanceof MasterCheckBoxPreference) {
toRemove.add(pref);
}
}
for (Preference pref : toRemove) {
groupGroup.removePreference(pref);
}
} else {
final List<NotificationChannel> channels = group.getChannels();
Collections.sort(channels, mChannelComparator);
int N = channels.size();
for (int i = 0; i < N; i++) {
final NotificationChannel channel = channels.get(i);
populateSingleChannelPrefs(groupGroup, channel, group.isBlocked());
}
}
}
}
}

View File

@@ -103,6 +103,10 @@ public class BlockPreferenceController extends NotificationPreferenceController
mChannel.setImportance(importance);
saveChannel();
}
if (mBackend.onlyHasDefaultChannel(mAppRow.pkg, mAppRow.uid)) {
mAppRow.banned = blocked;
mBackend.setNotificationsEnabledForPackage(mAppRow.pkg, mAppRow.uid, !blocked);
}
} else if (mChannelGroup != null && mChannelGroup.getGroup() != null) {
mChannelGroup.setBlocked(blocked);
mBackend.updateChannelGroup(mAppRow.pkg, mAppRow.uid, mChannelGroup.getGroup());

View File

@@ -0,0 +1,131 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification;
import android.content.Context;
import android.support.v7.preference.PreferenceViewHolder;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Switch;
import com.android.settings.R;
import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.TwoTargetPreference;
/**
* Shows an app icon, title and summary. Has a second switch touch target.
*/
public class NotificationAppPreference extends TwoTargetPreference {
private int mProgress;
private boolean mProgressVisible;
private Switch mSwitch;
private boolean mChecked;
private boolean mEnableSwitch = true;
public NotificationAppPreference(Context context) {
super(context);
setLayoutResource(R.layout.preference_app);
}
public NotificationAppPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setLayoutResource(R.layout.preference_app);
}
@Override
protected int getSecondTargetResId() {
return R.layout.preference_widget_master_switch;
}
public void setProgress(int amount) {
mProgress = amount;
mProgressVisible = true;
notifyChanged();
}
@Override
public void onBindViewHolder(PreferenceViewHolder view) {
super.onBindViewHolder(view);
view.findViewById(R.id.summary_container)
.setVisibility(TextUtils.isEmpty(getSummary()) ? View.GONE : View.VISIBLE);
final ProgressBar progress = (ProgressBar) view.findViewById(android.R.id.progress);
if (mProgressVisible) {
progress.setProgress(mProgress);
progress.setVisibility(View.VISIBLE);
} else {
progress.setVisibility(View.GONE);
}
final View widgetView = view.findViewById(android.R.id.widget_frame);
if (widgetView != null) {
widgetView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mSwitch != null && !mSwitch.isEnabled()) {
return;
}
setChecked(!mChecked);
if (!callChangeListener(mChecked)) {
setChecked(!mChecked);
} else {
persistBoolean(mChecked);
}
}
});
}
mSwitch = (Switch) view.findViewById(R.id.switchWidget);
if (mSwitch != null) {
mSwitch.setContentDescription(getTitle());
mSwitch.setChecked(mChecked);
mSwitch.setEnabled(mEnableSwitch);
}
}
public boolean isChecked() {
return mSwitch != null && mChecked;
}
public void setChecked(boolean checked) {
mChecked = checked;
if (mSwitch != null) {
mSwitch.setChecked(checked);
}
}
public void setSwitchEnabled(boolean enabled) {
mEnableSwitch = enabled;
if (mSwitch != null) {
mSwitch.setEnabled(enabled);
}
}
/**
* If admin is not null, disables the switch.
* Otherwise, keep it enabled.
*/
public void setDisabledByAdmin(RestrictedLockUtils.EnforcedAdmin admin) {
setSwitchEnabled(admin == null);
}
public Switch getSwitch() {
return mSwitch;
}
}

View File

@@ -15,6 +15,9 @@
*/
package com.android.settings.notification;
import static android.app.NotificationManager.IMPORTANCE_NONE;
import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED;
import android.app.INotificationManager;
import android.app.NotificationChannel;
import android.app.NotificationChannelGroup;
@@ -101,6 +104,12 @@ public class NotificationBackend {
public boolean setNotificationsEnabledForPackage(String pkg, int uid, boolean enabled) {
try {
if (onlyHasDefaultChannel(pkg, uid)) {
NotificationChannel defaultChannel =
getChannel(pkg, uid, NotificationChannel.DEFAULT_CHANNEL_ID);
defaultChannel.setImportance(enabled ? IMPORTANCE_UNSPECIFIED : IMPORTANCE_NONE);
updateChannel(pkg, uid, defaultChannel);
}
sINM.setNotificationsEnabledForPackage(pkg, uid, enabled);
return true;
} catch (Exception e) {

View File

@@ -74,6 +74,7 @@ import android.widget.Toast;
import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
@@ -252,30 +253,6 @@ abstract public class NotificationSettingsBase extends DashboardFragment {
return null;
}
protected void populateGroupToggle(final PreferenceGroup parent,
NotificationChannelGroup group) {
RestrictedSwitchPreference preference = new RestrictedSwitchPreference(getPrefContext());
preference.setTitle(R.string.notification_switch_label);
preference.setEnabled(mSuspendedAppsAdmin == null
&& isChannelGroupBlockable(group));
preference.setChecked(!group.isBlocked());
preference.setOnPreferenceClickListener(preference1 -> {
final boolean allowGroup = ((SwitchPreference) preference1).isChecked();
group.setBlocked(!allowGroup);
mBackend.updateChannelGroup(mAppRow.pkg, mAppRow.uid, group);
for (int i = 0; i < parent.getPreferenceCount(); i++) {
Preference pref = parent.getPreference(i);
if (pref instanceof MasterSwitchPreference) {
((MasterSwitchPreference) pref).setSwitchEnabled(allowGroup);
}
}
return true;
});
parent.addPreference(preference);
}
protected Preference populateSingleChannelPrefs(PreferenceGroup parent,
final NotificationChannel channel, final boolean groupBlocked) {
MasterCheckBoxPreference channelPref = new MasterCheckBoxPreference(

View File

@@ -21,6 +21,7 @@ import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.UserHandle;
import android.service.notification.NotifyingApp;
import android.support.annotation.VisibleForTesting;
@@ -32,6 +33,7 @@ import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.IconDrawableFactory;
import android.util.Log;
import android.widget.Switch;
import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.R;
@@ -40,6 +42,7 @@ import com.android.settings.applications.AppInfoBase;
import com.android.settings.applications.InstalledAppCounter;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.widget.AppPreference;
import com.android.settings.widget.MasterSwitchPreference;
import com.android.settingslib.applications.AppUtils;
import com.android.settingslib.applications.ApplicationsState;
import com.android.settingslib.core.AbstractPreferenceController;
@@ -197,13 +200,13 @@ public class RecentNotifyingAppsPreferenceController extends AbstractPreferenceC
// Rebind prefs/avoid adding new prefs if possible. Adding/removing prefs causes jank.
// Build a cached preference pool
final Map<String, Preference> appPreferences = new ArrayMap<>();
final Map<String, NotificationAppPreference> appPreferences = new ArrayMap<>();
int prefCount = mCategory.getPreferenceCount();
for (int i = 0; i < prefCount; i++) {
final Preference pref = mCategory.getPreference(i);
final String key = pref.getKey();
if (!TextUtils.equals(key, KEY_SEE_ALL)) {
appPreferences.put(key, pref);
appPreferences.put(key, (NotificationAppPreference) pref);
}
}
final int recentAppsCount = recentApps.size();
@@ -218,9 +221,9 @@ public class RecentNotifyingAppsPreferenceController extends AbstractPreferenceC
}
boolean rebindPref = true;
Preference pref = appPreferences.remove(pkgName);
NotificationAppPreference pref = appPreferences.remove(pkgName);
if (pref == null) {
pref = new AppPreference(prefContext);
pref = new NotificationAppPreference(prefContext);
rebindPref = false;
}
pref.setKey(pkgName);
@@ -229,13 +232,23 @@ public class RecentNotifyingAppsPreferenceController extends AbstractPreferenceC
pref.setSummary(Utils.formatRelativeTime(mContext,
System.currentTimeMillis() - app.getLastNotified(), false));
pref.setOrder(i);
pref.setOnPreferenceClickListener(preference -> {
AppInfoBase.startAppInfoFragment(AppNotificationSettings.class,
R.string.notifications_title, pkgName, appEntry.info.uid, mHost,
1001 /*RequestCode */,
MetricsProto.MetricsEvent.MANAGE_APPLICATIONS_NOTIFICATIONS);
return true;
Bundle args = new Bundle();
args.putString(AppInfoBase.ARG_PACKAGE_NAME, pkgName);
args.putInt(AppInfoBase.ARG_PACKAGE_UID, appEntry.info.uid);
pref.setIntent(Utils.onBuildStartFragmentIntent(mHost.getActivity(),
AppNotificationSettings.class.getName(), args, null,
R.string.notifications_title, null, false,
MetricsProto.MetricsEvent.MANAGE_APPLICATIONS_NOTIFICATIONS));
pref.setOnPreferenceChangeListener((preference, newValue) -> {
boolean blocked = !(Boolean) newValue;
mNotificationBackend.setNotificationsEnabledForPackage(
pkgName, appEntry.info.uid, !blocked);
return true;
});
pref.setChecked(
!mNotificationBackend.getNotificationsBanned(pkgName, appEntry.info.uid));
if (!rebindPref) {
mCategory.addPreference(pref);
}