DND Bypassing Apps redesign
- Add link in DND Conversations Page to the overall conversations list Settings page - Add custom_rule xml pages for custom schedule rule settings for messages and calls (so the UI is the same as before the message/calls redesign) - Change app exceptions to display apps with subtext indicating which notitfication channels are allowed to bypass dnd (previously, would display each channel individually) - Add individual AppBypassDnd channel pages where users can decide which channels will bypass DND for an app on a single page (AppChannelsBypassingDndSettings) - Only remove dnd bypassing apps preferences from the preference list if the list changed, else just update the preference itself to avoid the list from flashing Test: make RunSettingsRoboTests7 Bug: 151845457 Change-Id: If12d8921e1405aefb1066acc2ef5c55d216fe47a
This commit is contained in:
@@ -0,0 +1,227 @@
|
||||
/*
|
||||
* Copyright (C) 2020 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.app;
|
||||
|
||||
import static android.app.NotificationManager.IMPORTANCE_NONE;
|
||||
|
||||
import android.app.NotificationChannel;
|
||||
import android.app.NotificationChannelGroup;
|
||||
import android.app.settings.SettingsEnums;
|
||||
import android.content.Context;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.provider.Settings;
|
||||
|
||||
import androidx.core.text.BidiFormatter;
|
||||
import androidx.lifecycle.LifecycleObserver;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceCategory;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
import androidx.preference.SwitchPreference;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.applications.AppInfoBase;
|
||||
import com.android.settings.core.PreferenceControllerMixin;
|
||||
import com.android.settings.core.SubSettingLauncher;
|
||||
import com.android.settings.notification.NotificationBackend;
|
||||
import com.android.settings.widget.MasterSwitchPreference;
|
||||
import com.android.settingslib.RestrictedSwitchPreference;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Populates the PreferenceCategory with notification channels associated with the given app.
|
||||
* Users can allow/disallow notification channels from bypassing DND on a single settings
|
||||
* page.
|
||||
*/
|
||||
public class AppChannelsBypassingDndPreferenceController extends NotificationPreferenceController
|
||||
implements PreferenceControllerMixin, LifecycleObserver {
|
||||
|
||||
private static final String KEY = "zen_mode_bypassing_app_channels_list";
|
||||
private static final String ARG_FROM_SETTINGS = "fromSettings";
|
||||
|
||||
private RestrictedSwitchPreference mAllNotificationsToggle;
|
||||
private PreferenceCategory mPreferenceCategory;
|
||||
private final List<NotificationChannel> mChannels = new ArrayList<>();
|
||||
|
||||
public AppChannelsBypassingDndPreferenceController(
|
||||
Context context,
|
||||
NotificationBackend backend) {
|
||||
super(context, backend);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
mPreferenceCategory = screen.findPreference(KEY);
|
||||
|
||||
mAllNotificationsToggle = new RestrictedSwitchPreference(mPreferenceCategory.getContext());
|
||||
mAllNotificationsToggle.setTitle(R.string.zen_mode_bypassing_app_channels_toggle_all);
|
||||
mAllNotificationsToggle.setDisabledByAdmin(mAdmin);
|
||||
mAllNotificationsToggle.setEnabled(
|
||||
(mAdmin == null || !mAllNotificationsToggle.isDisabledByAdmin()));
|
||||
mAllNotificationsToggle.setOnPreferenceClickListener(
|
||||
new Preference.OnPreferenceClickListener() {
|
||||
@Override
|
||||
public boolean onPreferenceClick(Preference pref) {
|
||||
SwitchPreference preference = (SwitchPreference) pref;
|
||||
final boolean bypassDnd = preference.isChecked();
|
||||
for (NotificationChannel channel : mChannels) {
|
||||
if (showNotification(channel) && isChannelConfigurable(channel)) {
|
||||
channel.setBypassDnd(bypassDnd);
|
||||
channel.lockFields(NotificationChannel.USER_LOCKED_PRIORITY);
|
||||
mBackend.updateChannel(mAppRow.pkg, mAppRow.uid, channel);
|
||||
}
|
||||
}
|
||||
// the 0th index is the mAllNotificationsToggle which allows users to
|
||||
// toggle all notifications from this app to bypass DND
|
||||
for (int i = 1; i < mPreferenceCategory.getPreferenceCount(); i++) {
|
||||
MasterSwitchPreference childPreference =
|
||||
(MasterSwitchPreference) mPreferenceCategory.getPreference(i);
|
||||
childPreference.setChecked(showNotificationInDnd(mChannels.get(i - 1)));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
loadAppChannels();
|
||||
super.displayPreference(screen);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPreferenceKey() {
|
||||
return KEY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable() {
|
||||
return mAppRow != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
if (mAppRow != null) {
|
||||
loadAppChannels();
|
||||
}
|
||||
}
|
||||
|
||||
private void loadAppChannels() {
|
||||
// Load channel settings
|
||||
new AsyncTask<Void, Void, Void>() {
|
||||
@Override
|
||||
protected Void doInBackground(Void... unused) {
|
||||
List<NotificationChannelGroup> mChannelGroupList = mBackend.getGroups(mAppRow.pkg,
|
||||
mAppRow.uid).getList();
|
||||
mChannels.clear();
|
||||
for (NotificationChannelGroup channelGroup : mChannelGroupList) {
|
||||
for (NotificationChannel channel : channelGroup.getChannels()) {
|
||||
if (!isConversation(channel)) {
|
||||
mChannels.add(channel);
|
||||
}
|
||||
}
|
||||
}
|
||||
Collections.sort(mChannels, CHANNEL_COMPARATOR);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Void unused) {
|
||||
if (mContext == null) {
|
||||
return;
|
||||
}
|
||||
populateList();
|
||||
}
|
||||
}.execute();
|
||||
}
|
||||
|
||||
private void populateList() {
|
||||
if (mPreferenceCategory == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
mPreferenceCategory.removeAll();
|
||||
mPreferenceCategory.addPreference(mAllNotificationsToggle);
|
||||
for (NotificationChannel channel : mChannels) {
|
||||
MasterSwitchPreference channelPreference = new MasterSwitchPreference(mContext);
|
||||
channelPreference.setDisabledByAdmin(mAdmin);
|
||||
channelPreference.setSwitchEnabled(
|
||||
(mAdmin == null || !channelPreference.isDisabledByAdmin())
|
||||
&& isChannelConfigurable(channel)
|
||||
&& showNotification(channel));
|
||||
channelPreference.setTitle(BidiFormatter.getInstance().unicodeWrap(channel.getName()));
|
||||
channelPreference.setChecked(showNotificationInDnd(channel));
|
||||
channelPreference.setOnPreferenceChangeListener(
|
||||
new Preference.OnPreferenceChangeListener() {
|
||||
@Override
|
||||
public boolean onPreferenceChange(Preference pref, Object val) {
|
||||
boolean switchOn = (Boolean) val;
|
||||
channel.setBypassDnd(switchOn);
|
||||
channel.lockFields(NotificationChannel.USER_LOCKED_PRIORITY);
|
||||
mBackend.updateChannel(mAppRow.pkg, mAppRow.uid, channel);
|
||||
mAllNotificationsToggle.setChecked(areAllChannelsBypassing());
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
Bundle channelArgs = new Bundle();
|
||||
channelArgs.putInt(AppInfoBase.ARG_PACKAGE_UID, mAppRow.uid);
|
||||
channelArgs.putString(AppInfoBase.ARG_PACKAGE_NAME, mAppRow.pkg);
|
||||
channelArgs.putString(Settings.EXTRA_CHANNEL_ID, channel.getId());
|
||||
channelArgs.putBoolean(ARG_FROM_SETTINGS, true);
|
||||
channelPreference.setIntent(new SubSettingLauncher(mContext)
|
||||
.setDestination(ChannelNotificationSettings.class.getName())
|
||||
.setArguments(channelArgs)
|
||||
.setTitleRes(com.android.settings.R.string.notification_channel_title)
|
||||
.setSourceMetricsCategory(SettingsEnums.DND_APPS_BYPASSING)
|
||||
.toIntent());
|
||||
mPreferenceCategory.addPreference(channelPreference);
|
||||
}
|
||||
mAllNotificationsToggle.setChecked(areAllChannelsBypassing());
|
||||
}
|
||||
|
||||
private boolean areAllChannelsBypassing() {
|
||||
boolean allChannelsBypassing = true;
|
||||
for (NotificationChannel channel : mChannels) {
|
||||
if (showNotification(channel)) {
|
||||
allChannelsBypassing &= showNotificationInDnd(channel);
|
||||
}
|
||||
}
|
||||
return allChannelsBypassing;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether notifications from this channel would show if DND were on.
|
||||
*/
|
||||
private boolean showNotificationInDnd(NotificationChannel channel) {
|
||||
return channel.canBypassDnd() && showNotification(channel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether notifications from this channel would show if DND weren't on.
|
||||
*/
|
||||
private boolean showNotification(NotificationChannel channel) {
|
||||
return channel.getImportance() != IMPORTANCE_NONE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this notification channel is representing a conversation.
|
||||
*/
|
||||
private boolean isConversation(NotificationChannel channel) {
|
||||
return channel.getConversationId() != null && !channel.isDemoted();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright (C) 2020 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.app;
|
||||
|
||||
import android.app.settings.SettingsEnums;
|
||||
import android.content.Context;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.notification.NotificationBackend;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Per-app Settings page that shows a list of notification channels that a user can toggle for
|
||||
* the channel to bypass DND.
|
||||
*
|
||||
* This can be found at:
|
||||
* Settings > Sound > Do Not Disturb > Apps > (Choose app)
|
||||
*/
|
||||
public class AppChannelsBypassingDndSettings extends NotificationSettings {
|
||||
private static final String TAG = "AppChannelsBypassingDndSettings";
|
||||
|
||||
@Override
|
||||
public int getMetricsCategory() {
|
||||
return SettingsEnums.DND_APPS_BYPASSING;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
if (mUid < 0 || TextUtils.isEmpty(mPkg) || mPkgInfo == null) {
|
||||
Log.w(TAG, "Missing package or uid or packageinfo");
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
|
||||
for (NotificationPreferenceController controller : mControllers) {
|
||||
controller.onResume(mAppRow, null, null, null, null, mSuspendedAppsAdmin);
|
||||
controller.displayPreference(getPreferenceScreen());
|
||||
}
|
||||
updatePreferenceStates();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getLogTag() {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getPreferenceScreenResId() {
|
||||
return R.xml.app_channels_bypassing_dnd_settings;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
|
||||
mControllers = new ArrayList<>();
|
||||
mControllers.add(new HeaderPreferenceController(context, this));
|
||||
mControllers.add(new AppChannelsBypassingDndPreferenceController(context,
|
||||
new NotificationBackend()));
|
||||
return new ArrayList<>(mControllers);
|
||||
}
|
||||
}
|
||||
@@ -33,6 +33,11 @@ import android.os.Bundle;
|
||||
import android.provider.Settings;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceCategory;
|
||||
import androidx.preference.PreferenceGroup;
|
||||
import androidx.preference.SwitchPreference;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.Utils;
|
||||
import com.android.settings.applications.AppInfoBase;
|
||||
@@ -43,14 +48,8 @@ import com.android.settingslib.RestrictedSwitchPreference;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceCategory;
|
||||
import androidx.preference.PreferenceGroup;
|
||||
import androidx.preference.SwitchPreference;
|
||||
|
||||
public class ChannelListPreferenceController extends NotificationPreferenceController {
|
||||
|
||||
private static final String KEY = "channels";
|
||||
@@ -94,7 +93,7 @@ public class ChannelListPreferenceController extends NotificationPreferenceContr
|
||||
@Override
|
||||
protected Void doInBackground(Void... unused) {
|
||||
mChannelGroupList = mBackend.getGroups(mAppRow.pkg, mAppRow.uid).getList();
|
||||
Collections.sort(mChannelGroupList, mChannelGroupComparator);
|
||||
Collections.sort(mChannelGroupList, CHANNEL_GROUP_COMPARATOR);
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -142,7 +141,7 @@ public class ChannelListPreferenceController extends NotificationPreferenceContr
|
||||
}
|
||||
if (!group.isBlocked()) {
|
||||
final List<NotificationChannel> channels = group.getChannels();
|
||||
Collections.sort(channels, mChannelComparator);
|
||||
Collections.sort(channels, CHANNEL_COMPARATOR);
|
||||
int N = channels.size();
|
||||
for (int i = 0; i < N; i++) {
|
||||
final NotificationChannel channel = channels.get(i);
|
||||
@@ -274,7 +273,7 @@ public class ChannelListPreferenceController extends NotificationPreferenceContr
|
||||
}
|
||||
} else {
|
||||
final List<NotificationChannel> channels = group.getChannels();
|
||||
Collections.sort(channels, mChannelComparator);
|
||||
Collections.sort(channels, CHANNEL_COMPARATOR);
|
||||
int N = channels.size();
|
||||
for (int i = 0; i < N; i++) {
|
||||
final NotificationChannel channel = channels.get(i);
|
||||
@@ -283,33 +282,4 @@ public class ChannelListPreferenceController extends NotificationPreferenceContr
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Comparator<NotificationChannelGroup> mChannelGroupComparator =
|
||||
new Comparator<NotificationChannelGroup>() {
|
||||
|
||||
@Override
|
||||
public int compare(NotificationChannelGroup left, NotificationChannelGroup right) {
|
||||
// Non-grouped channels (in placeholder group with a null id) come last
|
||||
if (left.getId() == null && right.getId() != null) {
|
||||
return 1;
|
||||
} else if (right.getId() == null && left.getId() != null) {
|
||||
return -1;
|
||||
}
|
||||
return left.getId().compareTo(right.getId());
|
||||
}
|
||||
};
|
||||
|
||||
protected Comparator<NotificationChannel> mChannelComparator =
|
||||
(left, right) -> {
|
||||
if (left.isDeleted() != right.isDeleted()) {
|
||||
return Boolean.compare(left.isDeleted(), right.isDeleted());
|
||||
} else if (left.getId().equals(NotificationChannel.DEFAULT_CHANNEL_ID)) {
|
||||
// Uncategorized/miscellaneous legacy channel goes last
|
||||
return 1;
|
||||
} else if (right.getId().equals(NotificationChannel.DEFAULT_CHANNEL_ID)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return left.getId().compareTo(right.getId());
|
||||
};
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@ import com.android.settings.notification.NotificationBackend;
|
||||
import com.android.settingslib.RestrictedLockUtils;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
@@ -172,4 +173,31 @@ public abstract class NotificationPreferenceController extends AbstractPreferenc
|
||||
}
|
||||
return Objects.equals(NotificationChannel.DEFAULT_CHANNEL_ID, mChannel.getId());
|
||||
}
|
||||
|
||||
public static final Comparator<NotificationChannelGroup> CHANNEL_GROUP_COMPARATOR =
|
||||
new Comparator<NotificationChannelGroup>() {
|
||||
@Override
|
||||
public int compare(NotificationChannelGroup left, NotificationChannelGroup right) {
|
||||
// Non-grouped channels (in placeholder group with a null id) come last
|
||||
if (left.getId() == null && right.getId() != null) {
|
||||
return 1;
|
||||
} else if (right.getId() == null && left.getId() != null) {
|
||||
return -1;
|
||||
}
|
||||
return left.getId().compareTo(right.getId());
|
||||
}
|
||||
};
|
||||
|
||||
public static final Comparator<NotificationChannel> CHANNEL_COMPARATOR = (left, right) -> {
|
||||
if (left.isDeleted() != right.isDeleted()) {
|
||||
return Boolean.compare(left.isDeleted(), right.isDeleted());
|
||||
} else if (left.getId().equals(NotificationChannel.DEFAULT_CHANNEL_ID)) {
|
||||
// Uncategorized/miscellaneous legacy channel goes last
|
||||
return 1;
|
||||
} else if (right.getId().equals(NotificationChannel.DEFAULT_CHANNEL_ID)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return left.getId().compareTo(right.getId());
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user