From 18f14f10cf055d14e910a6892a9dd7ce2c8a99ef Mon Sep 17 00:00:00 2001 From: Julia Reynolds Date: Mon, 5 Feb 2018 12:49:44 -0500 Subject: [PATCH] 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 --- res/values/strings.xml | 4 +- .../notification/AppNotificationSettings.java | 76 +++++- .../BlockPreferenceController.java | 4 + .../NotificationAppPreference.java | 131 ++++++++++ .../notification/NotificationBackend.java | 9 + .../NotificationSettingsBase.java | 25 +- ...centNotifyingAppsPreferenceController.java | 33 ++- .../BlockPreferenceControllerTest.java | 9 + .../NotificationAppPreferenceTest.java | 239 ++++++++++++++++++ ...NotifyingAppsPreferenceControllerTest.java | 9 +- 10 files changed, 489 insertions(+), 50 deletions(-) create mode 100644 src/com/android/settings/notification/NotificationAppPreference.java create mode 100644 tests/robotests/src/com/android/settings/notification/NotificationAppPreferenceTest.java diff --git a/res/values/strings.xml b/res/values/strings.xml index 430c0d4647e..681bc4eeabc 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -7071,10 +7071,10 @@ summary on the channel page--> - No sound or visual interruption + Show silently and minimize - No sound + Show silently Make sound diff --git a/src/com/android/settings/notification/AppNotificationSettings.java b/src/com/android/settings/notification/AppNotificationSettings.java index 14ccf2350c4..78139dc310e 100644 --- a/src/com/android/settings/notification/AppNotificationSettings.java +++ b/src/com/android/settings/notification/AppNotificationSettings.java @@ -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 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 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 mChannelGroupComparator = new Comparator() { @@ -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 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 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()); + } + } + } + } + } diff --git a/src/com/android/settings/notification/BlockPreferenceController.java b/src/com/android/settings/notification/BlockPreferenceController.java index 6b65b0fdff5..8b270989a8a 100644 --- a/src/com/android/settings/notification/BlockPreferenceController.java +++ b/src/com/android/settings/notification/BlockPreferenceController.java @@ -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()); diff --git a/src/com/android/settings/notification/NotificationAppPreference.java b/src/com/android/settings/notification/NotificationAppPreference.java new file mode 100644 index 00000000000..a6bcdd6fd9b --- /dev/null +++ b/src/com/android/settings/notification/NotificationAppPreference.java @@ -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; + } +} diff --git a/src/com/android/settings/notification/NotificationBackend.java b/src/com/android/settings/notification/NotificationBackend.java index e047efa154b..d205fb4ab8e 100644 --- a/src/com/android/settings/notification/NotificationBackend.java +++ b/src/com/android/settings/notification/NotificationBackend.java @@ -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) { diff --git a/src/com/android/settings/notification/NotificationSettingsBase.java b/src/com/android/settings/notification/NotificationSettingsBase.java index 18b77bc0883..09789f34477 100644 --- a/src/com/android/settings/notification/NotificationSettingsBase.java +++ b/src/com/android/settings/notification/NotificationSettingsBase.java @@ -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( diff --git a/src/com/android/settings/notification/RecentNotifyingAppsPreferenceController.java b/src/com/android/settings/notification/RecentNotifyingAppsPreferenceController.java index 3240ae030af..279dcd39144 100644 --- a/src/com/android/settings/notification/RecentNotifyingAppsPreferenceController.java +++ b/src/com/android/settings/notification/RecentNotifyingAppsPreferenceController.java @@ -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 appPreferences = new ArrayMap<>(); + final Map 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); } diff --git a/tests/robotests/src/com/android/settings/notification/BlockPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/BlockPreferenceControllerTest.java index d6df6123510..7327d01605f 100644 --- a/tests/robotests/src/com/android/settings/notification/BlockPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/notification/BlockPreferenceControllerTest.java @@ -29,7 +29,9 @@ import static junit.framework.Assert.assertTrue; import static org.junit.Assert.assertEquals; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; @@ -238,19 +240,26 @@ public class BlockPreferenceControllerTest { @Test public void testOnSwitchChanged_channel_default() throws Exception { NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); + appRow.pkg = "pkg"; NotificationChannel channel = new NotificationChannel(DEFAULT_CHANNEL_ID, "a", IMPORTANCE_UNSPECIFIED); + when(mBackend.onlyHasDefaultChannel(anyString(), anyInt())).thenReturn(true); mController.onResume(appRow, channel, null, null); mController.updateState(mPreference); mController.onSwitchChanged(null, false); assertEquals(IMPORTANCE_NONE, channel.getImportance()); + assertTrue(appRow.banned); mController.onSwitchChanged(null, true); assertEquals(IMPORTANCE_UNSPECIFIED, channel.getImportance()); + assertFalse(appRow.banned); verify(mBackend, times(2)).updateChannel(any(), anyInt(), any()); + // 2 calls for onSwitchChanged + once when calling updateState originally + verify(mBackend, times(3)).setNotificationsEnabledForPackage( + anyString(), anyInt(), anyBoolean()); } @Test diff --git a/tests/robotests/src/com/android/settings/notification/NotificationAppPreferenceTest.java b/tests/robotests/src/com/android/settings/notification/NotificationAppPreferenceTest.java new file mode 100644 index 00000000000..dc8ee1317f6 --- /dev/null +++ b/tests/robotests/src/com/android/settings/notification/NotificationAppPreferenceTest.java @@ -0,0 +1,239 @@ +/* + * 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 static com.google.common.truth.Truth.assertThat; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +import android.content.Context; +import android.support.v7.preference.Preference; +import android.support.v7.preference.PreferenceViewHolder; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.LinearLayout; +import android.widget.Switch; + +import com.android.settings.R; +import com.android.settings.TestConfig; +import com.android.settings.testutils.SettingsRobolectricTestRunner; +import com.android.settingslib.RestrictedLockUtils; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + + +@RunWith(SettingsRobolectricTestRunner.class) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +public class NotificationAppPreferenceTest { + + private Context mContext; + + @Before + public void setUp() { + mContext = RuntimeEnvironment.application; + } + + @Test + public void createNewPreference_shouldSetLayout() { + final NotificationAppPreference preference = new NotificationAppPreference(mContext); + assertThat(preference.getWidgetLayoutResource()).isEqualTo( + R.layout.preference_widget_master_switch); + } + + @Test + public void setChecked_shouldUpdateButtonCheckedState() { + final NotificationAppPreference preference = new NotificationAppPreference(mContext); + final LayoutInflater inflater = LayoutInflater.from(mContext); + final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests( + inflater.inflate(R.layout.preference_app, null)); + final LinearLayout widgetView = holder.itemView.findViewById(android.R.id.widget_frame); + inflater.inflate(R.layout.preference_widget_master_switch, widgetView, true); + final Switch toggle = (Switch) holder.findViewById(R.id.switchWidget); + preference.onBindViewHolder(holder); + + preference.setChecked(true); + assertThat(toggle.isChecked()).isTrue(); + + preference.setChecked(false); + assertThat(toggle.isChecked()).isFalse(); + } + + @Test + public void setSwitchEnabled_shouldUpdateButtonEnabledState() { + final NotificationAppPreference preference = new NotificationAppPreference(mContext); + final LayoutInflater inflater = LayoutInflater.from(mContext); + final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests( + inflater.inflate(R.layout.preference_app, null)); + final LinearLayout widgetView = holder.itemView.findViewById(android.R.id.widget_frame); + inflater.inflate(R.layout.preference_widget_master_switch, widgetView, true); + final Switch toggle = (Switch) holder.findViewById(R.id.switchWidget); + preference.onBindViewHolder(holder); + + preference.setSwitchEnabled(true); + assertThat(toggle.isEnabled()).isTrue(); + + preference.setSwitchEnabled(false); + assertThat(toggle.isEnabled()).isFalse(); + } + + @Test + public void setSwitchEnabled_shouldUpdateButtonEnabledState_beforeViewBound() { + final NotificationAppPreference preference = new NotificationAppPreference(mContext); + final LayoutInflater inflater = LayoutInflater.from(mContext); + final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests( + inflater.inflate(R.layout.preference_app, null)); + final LinearLayout widgetView = holder.itemView.findViewById(android.R.id.widget_frame); + inflater.inflate(R.layout.preference_widget_master_switch, widgetView, true); + final Switch toggle = (Switch) holder.findViewById(R.id.switchWidget); + + preference.setSwitchEnabled(false); + preference.onBindViewHolder(holder); + assertThat(toggle.isEnabled()).isFalse(); + } + + @Test + public void clickWidgetView_shouldToggleButton() { + final NotificationAppPreference preference = new NotificationAppPreference(mContext); + final LayoutInflater inflater = LayoutInflater.from(mContext); + final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests( + inflater.inflate(R.layout.preference_app, null)); + final LinearLayout widgetView = holder.itemView.findViewById(android.R.id.widget_frame); + inflater.inflate(R.layout.preference_widget_master_switch, widgetView, true); + final Switch toggle = (Switch) holder.findViewById(R.id.switchWidget); + preference.onBindViewHolder(holder); + + widgetView.performClick(); + assertThat(toggle.isChecked()).isTrue(); + + widgetView.performClick(); + assertThat(toggle.isChecked()).isFalse(); + } + + @Test + public void clickWidgetView_shouldNotToggleButtonIfDisabled() { + final NotificationAppPreference preference = new NotificationAppPreference(mContext); + final LayoutInflater inflater = LayoutInflater.from(mContext); + final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests( + inflater.inflate(R.layout.preference_app, null)); + final LinearLayout widgetView = holder.itemView.findViewById(android.R.id.widget_frame); + inflater.inflate(R.layout.preference_widget_master_switch, widgetView, true); + final Switch toggle = (Switch) holder.findViewById(R.id.switchWidget); + preference.onBindViewHolder(holder); + toggle.setEnabled(false); + + widgetView.performClick(); + assertThat(toggle.isChecked()).isFalse(); + } + + @Test + public void clickWidgetView_shouldNotifyPreferenceChanged() { + final NotificationAppPreference preference = new NotificationAppPreference(mContext); + final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests( + LayoutInflater.from(mContext).inflate(R.layout.preference_app, null)); + final View widgetView = holder.findViewById(android.R.id.widget_frame); + final Preference.OnPreferenceChangeListener + listener = mock(Preference.OnPreferenceChangeListener.class); + preference.setOnPreferenceChangeListener(listener); + preference.onBindViewHolder(holder); + + preference.setChecked(false); + widgetView.performClick(); + verify(listener).onPreferenceChange(preference, true); + + preference.setChecked(true); + widgetView.performClick(); + verify(listener).onPreferenceChange(preference, false); + } + + @Test + public void setDisabledByAdmin_hasEnforcedAdmin_shouldDisableButton() { + final NotificationAppPreference preference = new NotificationAppPreference(mContext); + final LayoutInflater inflater = LayoutInflater.from(mContext); + final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests( + inflater.inflate(R.layout.preference_app, null)); + final LinearLayout widgetView = holder.itemView.findViewById(android.R.id.widget_frame); + inflater.inflate(R.layout.preference_widget_master_switch, widgetView, true); + final Switch toggle = (Switch) holder.findViewById(R.id.switchWidget); + toggle.setEnabled(true); + preference.onBindViewHolder(holder); + + preference.setDisabledByAdmin(mock(RestrictedLockUtils.EnforcedAdmin.class)); + assertThat(toggle.isEnabled()).isFalse(); + } + + @Test + public void setDisabledByAdmin_noEnforcedAdmin_shouldEnableButton() { + final NotificationAppPreference preference = new NotificationAppPreference(mContext); + final LayoutInflater inflater = LayoutInflater.from(mContext); + final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests( + inflater.inflate(R.layout.preference_app, null)); + final LinearLayout widgetView = holder.itemView.findViewById(android.R.id.widget_frame); + inflater.inflate(R.layout.preference_widget_master_switch, widgetView, true); + final Switch toggle = (Switch) holder.findViewById(R.id.switchWidget); + toggle.setEnabled(false); + preference.onBindViewHolder(holder); + + preference.setDisabledByAdmin(null); + assertThat(toggle.isEnabled()).isTrue(); + } + + @Test + public void onBindViewHolder_toggleButtonShouldHaveContentDescription() { + final NotificationAppPreference preference = new NotificationAppPreference(mContext); + final LayoutInflater inflater = LayoutInflater.from(mContext); + final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests( + inflater.inflate(R.layout.preference_app, null)); + final LinearLayout widgetView = holder.itemView.findViewById(android.R.id.widget_frame); + inflater.inflate(R.layout.preference_widget_master_switch, widgetView, true); + final Switch toggle = (Switch) holder.findViewById(R.id.switchWidget); + final String label = "TestButton"; + preference.setTitle(label); + + preference.onBindViewHolder(holder); + + assertThat(toggle.getContentDescription()).isEqualTo(label); + } + + @Test + public void setSummary_showSummaryContainer() { + final NotificationAppPreference preference = new NotificationAppPreference(mContext); + View rootView = View.inflate(mContext, R.layout.preference_app, null /* parent */); + PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(rootView); + preference.setSummary("test"); + preference.onBindViewHolder(holder); + + assertThat(holder.findViewById(R.id.summary_container).getVisibility()) + .isEqualTo(View.VISIBLE); + } + + @Test + public void noSummary_hideSummaryContainer() { + final NotificationAppPreference preference = new NotificationAppPreference(mContext); + View rootView = View.inflate(mContext, R.layout.preference_app, null /* parent */); + PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(rootView); + preference.setSummary(null); + preference.onBindViewHolder(holder); + + assertThat(holder.findViewById(R.id.summary_container).getVisibility()) + .isEqualTo(View.GONE); + } +} diff --git a/tests/robotests/src/com/android/settings/notification/RecentNotifyingAppsPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/RecentNotifyingAppsPreferenceControllerTest.java index a25bb002502..1aa963db74c 100644 --- a/tests/robotests/src/com/android/settings/notification/RecentNotifyingAppsPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/notification/RecentNotifyingAppsPreferenceControllerTest.java @@ -31,7 +31,9 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import android.app.Activity; import android.app.Application; +import android.app.Fragment; import android.content.Context; import android.content.Intent; import android.content.pm.ApplicationInfo; @@ -90,6 +92,10 @@ public class RecentNotifyingAppsPreferenceControllerTest { private ApplicationInfo mApplicationInfo; @Mock private NotificationBackend mBackend; + @Mock + private Fragment mHost; + @Mock + private Activity mActivity; private Context mContext; private RecentNotifyingAppsPreferenceController mController; @@ -102,7 +108,7 @@ public class RecentNotifyingAppsPreferenceControllerTest { doReturn(mPackageManager).when(mContext).getPackageManager(); mController = new RecentNotifyingAppsPreferenceController( - mContext, mBackend, mAppState, null); + mContext, mBackend, mAppState, mHost); when(mScreen.findPreference(anyString())).thenReturn(mCategory); when(mScreen.findPreference(RecentNotifyingAppsPreferenceController.KEY_SEE_ALL)) @@ -110,6 +116,7 @@ public class RecentNotifyingAppsPreferenceControllerTest { when(mScreen.findPreference(RecentNotifyingAppsPreferenceController.KEY_DIVIDER)) .thenReturn(mDivider); when(mCategory.getContext()).thenReturn(mContext); + when(mHost.getActivity()).thenReturn(mActivity); } @Test