Merge "Add character limit for app_link_open_always_summary string"
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 1f9c650..4f0ba58 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -8,6 +8,7 @@
<uses-permission android:name="android.permission.REQUEST_NETWORK_SCORES" />
<uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE" />
+ <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 71f2482..e5e55c6 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -8080,6 +8080,9 @@
<!-- Gentle notifications settings: display on all surfaces [CHAR LIMIT=100] -->
<string name="gentle_notifications_display_summary_shade_status_lock">Display in pull-down shade, status bar & on lock screen</string>
+ <!-- Notification settings: suppress status bar/ambient icons for silent notifications -->
+ <string name="silent_notifications_status_bar">Hide silent notifications in status bar</string>
+
<!-- Configure Notifications: Title for the pulse notification light option. [CHAR LIMIT=30] -->
<string name="notification_pulse_title">Blink light</string>
diff --git a/res/xml/configure_notification_settings.xml b/res/xml/configure_notification_settings.xml
index 7dea4e6..6e8f701 100644
--- a/res/xml/configure_notification_settings.xml
+++ b/res/xml/configure_notification_settings.xml
@@ -88,6 +88,11 @@
settings:initialExpandedChildrenCount="0">
<SwitchPreference
+ android:key="silent_icons"
+ android:title="@string/silent_notifications_status_bar"
+ settings:controller="com.android.settings.notification.SilentStatusBarPreferenceController"/>
+
+ <SwitchPreference
android:key="show_snooze_options"
android:title="@string/snooze_options_title"
settings:controller="com.android.settings.notification.SnoozeNotificationPreferenceController" />
diff --git a/src/com/android/settings/notification/GentleNotificationsPreferenceController.java b/src/com/android/settings/notification/GentleNotificationsPreferenceController.java
deleted file mode 100644
index 67a4707..0000000
--- a/src/com/android/settings/notification/GentleNotificationsPreferenceController.java
+++ /dev/null
@@ -1,76 +0,0 @@
-/*
- * Copyright (C) 2019 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.provider.Settings;
-
-import com.android.settings.R;
-import com.android.settings.core.BasePreferenceController;
-
-import androidx.annotation.VisibleForTesting;
-
-public class GentleNotificationsPreferenceController extends BasePreferenceController {
-
- @VisibleForTesting
- static final int ON = 1;
-
- private NotificationBackend mBackend;
-
- public GentleNotificationsPreferenceController(Context context, String preferenceKey) {
- super(context, preferenceKey);
- mBackend = new NotificationBackend();
- }
-
- @VisibleForTesting
- void setBackend(NotificationBackend backend) {
- mBackend = backend;
- }
-
- @Override
- public CharSequence getSummary() {
- boolean showOnLockscreen = showOnLockscreen();
- boolean showOnStatusBar = showOnStatusBar();
-
- if (showOnLockscreen) {
- if (showOnStatusBar) {
- return mContext.getString(
- R.string.gentle_notifications_display_summary_shade_status_lock);
- } else {
- return mContext.getString(R.string.gentle_notifications_display_summary_shade_lock);
- }
- } else if (showOnStatusBar) {
- return mContext.getString(R.string.gentle_notifications_display_summary_shade_status);
- } else {
- return mContext.getString(R.string.gentle_notifications_display_summary_shade);
- }
- }
-
- @Override
- public int getAvailabilityStatus() {
- return AVAILABLE;
- }
-
- private boolean showOnLockscreen() {
- return Settings.Secure.getInt(mContext.getContentResolver(),
- Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, ON) == ON;
- }
-
- private boolean showOnStatusBar() {
- return !mBackend.shouldHideSilentStatusBarIcons(mContext);
- }
-}
diff --git a/src/com/android/settings/notification/RecentNotifyingAppsPreferenceController.java b/src/com/android/settings/notification/RecentNotifyingAppsPreferenceController.java
index 8998d65..020cf7a 100644
--- a/src/com/android/settings/notification/RecentNotifyingAppsPreferenceController.java
+++ b/src/com/android/settings/notification/RecentNotifyingAppsPreferenceController.java
@@ -38,7 +38,7 @@
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.core.SubSettingLauncher;
import com.android.settings.notification.app.AppNotificationSettings;
-import com.android.settings.notification.app.NotificationAppPreference;
+import com.android.settings.widget.MasterSwitchPreference;
import com.android.settingslib.TwoTargetPreference;
import com.android.settingslib.applications.ApplicationsState;
import com.android.settingslib.core.AbstractPreferenceController;
@@ -228,13 +228,13 @@
// Rebind prefs/avoid adding new prefs if possible. Adding/removing prefs causes jank.
// Build a cached preference pool
- final Map<String, NotificationAppPreference> appPreferences = new ArrayMap<>();
+ final Map<String, MasterSwitchPreference> 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, (NotificationAppPreference) pref);
+ appPreferences.put(key, (MasterSwitchPreference) pref);
}
}
final int recentAppsCount = recentApps.size();
@@ -249,10 +249,10 @@
}
boolean rebindPref = true;
- NotificationAppPreference pref = appPreferences.remove(getKey(app.getUserId(),
+ MasterSwitchPreference pref = appPreferences.remove(getKey(app.getUserId(),
pkgName));
if (pref == null) {
- pref = new NotificationAppPreference(prefContext);
+ pref = new MasterSwitchPreference(prefContext);
rebindPref = false;
}
pref.setKey(getKey(app.getUserId(), pkgName));
@@ -278,9 +278,8 @@
});
pref.setSwitchEnabled(mNotificationBackend.isBlockable(mContext, appEntry.info));
pref.setOnPreferenceChangeListener((preference, newValue) -> {
- boolean blocked = !(Boolean) newValue;
mNotificationBackend.setNotificationsEnabledForPackage(
- pkgName, appEntry.info.uid, !blocked);
+ pkgName, appEntry.info.uid, (Boolean) newValue);
return true;
});
pref.setChecked(
diff --git a/src/com/android/settings/notification/SilentStatusBarPreferenceController.java b/src/com/android/settings/notification/SilentStatusBarPreferenceController.java
new file mode 100644
index 0000000..0585382
--- /dev/null
+++ b/src/com/android/settings/notification/SilentStatusBarPreferenceController.java
@@ -0,0 +1,55 @@
+/*
+ * 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;
+
+import android.content.Context;
+
+import com.android.settings.core.TogglePreferenceController;
+
+import com.google.common.annotations.VisibleForTesting;
+
+public class SilentStatusBarPreferenceController extends TogglePreferenceController {
+
+ private static final String KEY = "silent_icons";
+ private NotificationBackend mBackend;
+
+ public SilentStatusBarPreferenceController(Context context) {
+ super(context, KEY);
+ mBackend = new NotificationBackend();
+ }
+
+ @VisibleForTesting
+ void setBackend(NotificationBackend backend) {
+ mBackend = backend;
+ }
+
+ @Override
+ public boolean isChecked() {
+ return mBackend.shouldHideSilentStatusBarIcons(mContext);
+ }
+
+ @Override
+ public boolean setChecked(boolean isChecked) {
+ mBackend.setHideSilentStatusIcons(isChecked);
+ return true;
+ }
+
+ @Override
+ public int getAvailabilityStatus() {
+ return AVAILABLE;
+ }
+}
diff --git a/src/com/android/settings/notification/app/NotificationAppPreference.java b/src/com/android/settings/notification/app/NotificationAppPreference.java
deleted file mode 100644
index 4180a80..0000000
--- a/src/com/android/settings/notification/app/NotificationAppPreference.java
+++ /dev/null
@@ -1,119 +0,0 @@
-/*
- * 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.app;
-
-import android.content.Context;
-import android.util.AttributeSet;
-import android.view.View;
-import android.widget.Switch;
-
-import androidx.preference.PreferenceViewHolder;
-
-import com.android.settings.R;
-import com.android.settings.widget.MasterSwitchPreference;
-import com.android.settingslib.RestrictedLockUtils;
-
-/**
- * Shows an app icon, title and summary. Has a second switch touch target.
- */
-public class NotificationAppPreference extends MasterSwitchPreference {
-
- private Switch mSwitch;
- private boolean mChecked;
- private boolean mEnableSwitch = true;
-
- public NotificationAppPreference(Context context) {
- super(context);
- }
-
- public NotificationAppPreference(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
-
- public NotificationAppPreference(Context context, AttributeSet attrs,
- int defStyleAttr, int defStyleRes) {
- super(context, attrs, defStyleAttr, defStyleRes);
- }
-
- public NotificationAppPreference(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- }
-
- @Override
- protected int getSecondTargetResId() {
- return R.layout.preference_widget_master_switch;
- }
-
- @Override
- public void onBindViewHolder(PreferenceViewHolder view) {
- super.onBindViewHolder(view);
-
- 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/password/ConfirmLockPassword.java b/src/com/android/settings/password/ConfirmLockPassword.java
index a62f77f..a6a1b37 100644
--- a/src/com/android/settings/password/ConfirmLockPassword.java
+++ b/src/com/android/settings/password/ConfirmLockPassword.java
@@ -56,12 +56,16 @@
public class ConfirmLockPassword extends ConfirmDeviceCredentialBaseActivity {
- // The index of the array is isStrongAuth << 1 + isAlpha.
+ // The index of the array is isStrongAuth << 2 + isManagedProfile << 1 + isAlpha.
private static final int[] DETAIL_TEXTS = new int[] {
R.string.lockpassword_confirm_your_pin_generic,
R.string.lockpassword_confirm_your_password_generic,
+ R.string.lockpassword_confirm_your_pin_generic_profile,
+ R.string.lockpassword_confirm_your_password_generic_profile,
R.string.lockpassword_strong_auth_required_device_pin,
- R.string.lockpassword_strong_auth_required_device_password
+ R.string.lockpassword_strong_auth_required_device_password,
+ R.string.lockpassword_strong_auth_required_work_pin,
+ R.string.lockpassword_strong_auth_required_work_password
};
public static class InternalActivity extends ConfirmLockPassword {
@@ -218,8 +222,9 @@
: R.string.lockpassword_confirm_your_pin_details_frp;
}
boolean isStrongAuthRequired = isStrongAuthRequired();
- // Map boolean flags to an index by isStrongAuth << 1 + isAlpha.
- int index = ((isStrongAuthRequired ? 1 : 0) << 1) + (mIsAlpha ? 1 : 0);
+ // Map boolean flags to an index by isStrongAuth << 2 + isManagedProfile << 1 + isAlpha.
+ int index = ((isStrongAuthRequired ? 1 : 0) << 2) + ((mIsManagedProfile ? 1 : 0) << 1)
+ + (mIsAlpha ? 1 : 0);
return DETAIL_TEXTS[index];
}
diff --git a/src/com/android/settings/password/ConfirmLockPattern.java b/src/com/android/settings/password/ConfirmLockPattern.java
index 557eae4..d52992f 100644
--- a/src/com/android/settings/password/ConfirmLockPattern.java
+++ b/src/com/android/settings/password/ConfirmLockPattern.java
@@ -239,7 +239,7 @@
return R.string.lockpassword_confirm_your_pattern_details_frp;
}
final boolean isStrongAuthRequired = isStrongAuthRequired();
- if (UserManager.get(getActivity()).isManagedProfile(mEffectiveUserId)) {
+ if (mIsManagedProfile) {
return isStrongAuthRequired
? R.string.lockpassword_strong_auth_required_work_pattern
: R.string.lockpassword_confirm_your_pattern_generic_profile;
diff --git a/tests/robotests/src/com/android/settings/notification/GentleNotificationsPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/GentleNotificationsPreferenceControllerTest.java
deleted file mode 100644
index bd01379..0000000
--- a/tests/robotests/src/com/android/settings/notification/GentleNotificationsPreferenceControllerTest.java
+++ /dev/null
@@ -1,107 +0,0 @@
-/*
- * Copyright (C) 2019 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.android.settings.notification.BadgingNotificationPreferenceController.OFF;
-import static com.android.settings.notification.BadgingNotificationPreferenceController.ON;
-
-import static com.google.common.truth.Truth.assertThat;
-
-import static org.mockito.Mockito.when;
-
-import android.content.Context;
-import android.provider.Settings;
-
-import com.android.settings.R;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-import org.robolectric.RobolectricTestRunner;
-import org.robolectric.RuntimeEnvironment;
-
-import androidx.preference.Preference;
-
-@RunWith(RobolectricTestRunner.class)
-public class GentleNotificationsPreferenceControllerTest {
-
- private Context mContext;
-
- @Mock
- NotificationBackend mBackend;
-
- private GentleNotificationsPreferenceController mController;
- private Preference mPreference;
-
- private static final String KEY = "gentle_notifications";
-
- @Before
- public void setUp() {
- MockitoAnnotations.initMocks(this);
- mContext = RuntimeEnvironment.application;
- mController = new GentleNotificationsPreferenceController(mContext, KEY);
- mController.setBackend(mBackend);
- mPreference = new Preference(mContext);
- }
-
- @Test
- public void display_shouldDisplay() {
- assertThat(mPreference.isVisible()).isTrue();
- }
-
- @Test
- public void getSummary_lock() {
- Settings.Secure.putInt(mContext.getContentResolver(),
- Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 1);
- when(mBackend.shouldHideSilentStatusBarIcons(mContext)).thenReturn(true);
-
- assertThat(mController.getSummary()).isEqualTo(
- mContext.getString(R.string.gentle_notifications_display_summary_shade_lock));
- }
-
- @Test
- public void getSummary_status() {
- Settings.Secure.putInt(mContext.getContentResolver(),
- Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 0);
- when(mBackend.shouldHideSilentStatusBarIcons(mContext)).thenReturn(false);
-
- assertThat(mController.getSummary()).isEqualTo(
- mContext.getString(R.string.gentle_notifications_display_summary_shade_status));
- }
-
- @Test
- public void getSummary_both() {
- Settings.Secure.putInt(mContext.getContentResolver(),
- Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 1);
- when(mBackend.shouldHideSilentStatusBarIcons(mContext)).thenReturn(false);
-
- assertThat(mController.getSummary()).isEqualTo(mContext.getString(
- R.string.gentle_notifications_display_summary_shade_status_lock));
- }
-
- @Test
- public void getSummary_neither() {
- Settings.Secure.putInt(mContext.getContentResolver(),
- Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 0);
- when(mBackend.shouldHideSilentStatusBarIcons(mContext)).thenReturn(true);
-
- assertThat(mController.getSummary()).isEqualTo(
- mContext.getString(R.string.gentle_notifications_display_summary_shade));
- }
-}
diff --git a/tests/robotests/src/com/android/settings/notification/SilentStatusBarPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/SilentStatusBarPreferenceControllerTest.java
new file mode 100644
index 0000000..a913b9e
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/notification/SilentStatusBarPreferenceControllerTest.java
@@ -0,0 +1,85 @@
+/*
+ * 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;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+
+import androidx.preference.Preference;
+import androidx.preference.PreferenceScreen;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.RuntimeEnvironment;
+
+@RunWith(RobolectricTestRunner.class)
+public class SilentStatusBarPreferenceControllerTest {
+
+ @Mock
+ private NotificationBackend mBackend;
+ @Mock
+ private PreferenceScreen mScreen;
+
+ private Context mContext;
+ private SilentStatusBarPreferenceController mController;
+ private Preference mPreference;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ mContext = RuntimeEnvironment.application;
+ mController = new SilentStatusBarPreferenceController(mContext);
+ mController.setBackend(mBackend);
+ mPreference = new Preference(mContext);
+ mPreference.setKey(mController.getPreferenceKey());
+ when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
+ }
+
+ @Test
+ public void isChecked_settingIsOff() {
+ when(mBackend.shouldHideSilentStatusBarIcons(any())).thenReturn(false);
+ assertThat(mController.isChecked()).isFalse();
+ }
+
+ @Test
+ public void isChecked_settingIsOn() {
+ when(mBackend.shouldHideSilentStatusBarIcons(any())).thenReturn(true);
+ assertThat(mController.isChecked()).isTrue();
+ }
+
+ @Test
+ public void setChecked_Off() {
+ mController.setChecked(false);
+ verify(mBackend, times(1)).setHideSilentStatusIcons(false);
+ }
+
+ @Test
+ public void setChecked_On() {
+ mController.setChecked(true);
+ verify(mBackend, times(1)).setHideSilentStatusIcons(true);
+ }
+}
\ No newline at end of file
diff --git a/tests/robotests/src/com/android/settings/notification/app/NotificationAppPreferenceTest.java b/tests/robotests/src/com/android/settings/notification/app/NotificationAppPreferenceTest.java
deleted file mode 100644
index c8c7396..0000000
--- a/tests/robotests/src/com/android/settings/notification/app/NotificationAppPreferenceTest.java
+++ /dev/null
@@ -1,217 +0,0 @@
-/*
- * 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.app;
-
-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.view.LayoutInflater;
-import android.view.View;
-import android.widget.LinearLayout;
-import android.widget.Switch;
-
-import androidx.preference.Preference;
-import androidx.preference.PreferenceViewHolder;
-
-import com.android.settings.R;
-import com.android.settings.notification.app.NotificationAppPreference;
-import com.android.settingslib.RestrictedLockUtils;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.robolectric.RobolectricTestRunner;
-import org.robolectric.RuntimeEnvironment;
-
-@RunWith(RobolectricTestRunner.class)
-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);
- assertThat(widgetView).isNotNull();
-
- 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);
- assertThat(widgetView).isNotNull();
-
- 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);
- }
-}