Refactor badging notification preference controller.
- Convert inheritance from AbstractPreferenceController to TogglePreferenceController. - Register BadgingNotificationPreferenceController in XML. - Add RoboTests test cases for BadgingNotificationPreferenceController. Fixes: 67997784 Test: RunSettingsRoboTests Change-Id: If10744c067f065e7c2465ca1fff66895d7dc1c56
This commit is contained in:
@@ -28,7 +28,8 @@
|
|||||||
<!-- Notification badging -->
|
<!-- Notification badging -->
|
||||||
<SwitchPreference
|
<SwitchPreference
|
||||||
android:key="notification_badging"
|
android:key="notification_badging"
|
||||||
android:title="@string/notification_badging_title"/>
|
android:title="@string/notification_badging_title"
|
||||||
|
settings:controller="com.android.settings.notification.BadgingNotificationPreferenceController"/>
|
||||||
|
|
||||||
<!-- Pulse notification light -->
|
<!-- Pulse notification light -->
|
||||||
<SwitchPreference
|
<SwitchPreference
|
||||||
|
@@ -23,11 +23,14 @@ import android.database.ContentObserver;
|
|||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
|
|
||||||
|
import androidx.annotation.VisibleForTesting;
|
||||||
import androidx.preference.Preference;
|
import androidx.preference.Preference;
|
||||||
import androidx.preference.PreferenceScreen;
|
import androidx.preference.PreferenceScreen;
|
||||||
import androidx.preference.TwoStatePreference;
|
import androidx.preference.TwoStatePreference;
|
||||||
|
|
||||||
import com.android.settings.core.PreferenceControllerMixin;
|
import com.android.settings.core.PreferenceControllerMixin;
|
||||||
|
import com.android.settings.core.TogglePreferenceController;
|
||||||
import com.android.settingslib.core.AbstractPreferenceController;
|
import com.android.settingslib.core.AbstractPreferenceController;
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.search.DatabaseIndexingUtils;
|
import com.android.settings.search.DatabaseIndexingUtils;
|
||||||
@@ -39,19 +42,20 @@ import com.android.settingslib.core.lifecycle.events.OnResume;
|
|||||||
|
|
||||||
import static android.provider.Settings.Secure.NOTIFICATION_BADGING;
|
import static android.provider.Settings.Secure.NOTIFICATION_BADGING;
|
||||||
|
|
||||||
public class BadgingNotificationPreferenceController extends AbstractPreferenceController
|
public class BadgingNotificationPreferenceController extends TogglePreferenceController
|
||||||
implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener,
|
implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener,
|
||||||
LifecycleObserver, OnResume, OnPause {
|
LifecycleObserver, OnResume, OnPause {
|
||||||
|
|
||||||
private static final String TAG = "BadgeNotifPrefContr";
|
private static final String TAG = "BadgeNotifPrefContr";
|
||||||
private static final String KEY_NOTIFICATION_BADGING = "notification_badging";
|
@VisibleForTesting
|
||||||
private static final int ON = 1;
|
static final int ON = 1;
|
||||||
private static final int OFF = 0;
|
@VisibleForTesting
|
||||||
|
static final int OFF = 0;
|
||||||
|
|
||||||
private SettingObserver mSettingObserver;
|
private SettingObserver mSettingObserver;
|
||||||
|
|
||||||
public BadgingNotificationPreferenceController(Context context) {
|
public BadgingNotificationPreferenceController(Context context, String preferenceKey) {
|
||||||
super(context);
|
super(context, preferenceKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -78,28 +82,22 @@ public class BadgingNotificationPreferenceController extends AbstractPreferenceC
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getPreferenceKey() {
|
public int getAvailabilityStatus() {
|
||||||
return KEY_NOTIFICATION_BADGING;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isAvailable() {
|
|
||||||
return mContext.getResources()
|
return mContext.getResources()
|
||||||
.getBoolean(com.android.internal.R.bool.config_notificationBadging);
|
.getBoolean(com.android.internal.R.bool.config_notificationBadging)
|
||||||
|
? AVAILABLE : DISABLED_UNSUPPORTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateState(Preference preference) {
|
public boolean isChecked() {
|
||||||
final boolean checked = Settings.Secure.getInt(mContext.getContentResolver(),
|
return Settings.Secure.getInt(mContext.getContentResolver(),
|
||||||
NOTIFICATION_BADGING, ON) == ON;
|
NOTIFICATION_BADGING, ON) == ON;
|
||||||
((TwoStatePreference) preference).setChecked(checked);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
public boolean setChecked(boolean isChecked) {
|
||||||
final boolean val = (Boolean) newValue;
|
|
||||||
return Settings.Secure.putInt(mContext.getContentResolver(),
|
return Settings.Secure.putInt(mContext.getContentResolver(),
|
||||||
NOTIFICATION_BADGING, val ? ON : OFF);
|
NOTIFICATION_BADGING, isChecked ? ON : OFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
class SettingObserver extends ContentObserver {
|
class SettingObserver extends ContentObserver {
|
||||||
@@ -134,7 +132,7 @@ public class BadgingNotificationPreferenceController extends AbstractPreferenceC
|
|||||||
@Override
|
@Override
|
||||||
public ResultPayload getResultPayload() {
|
public ResultPayload getResultPayload() {
|
||||||
final Intent intent = DatabaseIndexingUtils.buildSearchResultPageIntent(mContext,
|
final Intent intent = DatabaseIndexingUtils.buildSearchResultPageIntent(mContext,
|
||||||
ConfigureNotificationSettings.class.getName(), KEY_NOTIFICATION_BADGING,
|
ConfigureNotificationSettings.class.getName(), getPreferenceKey(),
|
||||||
mContext.getString(R.string.configure_notification_settings));
|
mContext.getString(R.string.configure_notification_settings));
|
||||||
|
|
||||||
return new InlineSwitchPayload(Settings.Secure.NOTIFICATION_BADGING,
|
return new InlineSwitchPayload(Settings.Secure.NOTIFICATION_BADGING,
|
||||||
|
@@ -93,8 +93,6 @@ public class ConfigureNotificationSettings extends DashboardFragment {
|
|||||||
private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
|
private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
|
||||||
Lifecycle lifecycle, Application app, Fragment host) {
|
Lifecycle lifecycle, Application app, Fragment host) {
|
||||||
final List<AbstractPreferenceController> controllers = new ArrayList<>();
|
final List<AbstractPreferenceController> controllers = new ArrayList<>();
|
||||||
final BadgingNotificationPreferenceController badgeController =
|
|
||||||
new BadgingNotificationPreferenceController(context);
|
|
||||||
final LockScreenNotificationPreferenceController lockScreenNotificationController =
|
final LockScreenNotificationPreferenceController lockScreenNotificationController =
|
||||||
new LockScreenNotificationPreferenceController(context,
|
new LockScreenNotificationPreferenceController(context,
|
||||||
KEY_LOCKSCREEN,
|
KEY_LOCKSCREEN,
|
||||||
@@ -105,7 +103,6 @@ public class ConfigureNotificationSettings extends DashboardFragment {
|
|||||||
}
|
}
|
||||||
controllers.add(new RecentNotifyingAppsPreferenceController(
|
controllers.add(new RecentNotifyingAppsPreferenceController(
|
||||||
context, new NotificationBackend(), app, host));
|
context, new NotificationBackend(), app, host));
|
||||||
controllers.add(badgeController);
|
|
||||||
controllers.add(lockScreenNotificationController);
|
controllers.add(lockScreenNotificationController);
|
||||||
controllers.add(new NotificationRingtonePreferenceController(context) {
|
controllers.add(new NotificationRingtonePreferenceController(context) {
|
||||||
@Override
|
@Override
|
||||||
|
@@ -17,6 +17,12 @@
|
|||||||
package com.android.settings.notification;
|
package com.android.settings.notification;
|
||||||
|
|
||||||
import static android.provider.Settings.Secure.NOTIFICATION_BADGING;
|
import static android.provider.Settings.Secure.NOTIFICATION_BADGING;
|
||||||
|
|
||||||
|
import static com.android.settings.core.BasePreferenceController.AVAILABLE;
|
||||||
|
import static com.android.settings.core.BasePreferenceController.DISABLED_UNSUPPORTED;
|
||||||
|
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 com.google.common.truth.Truth.assertThat;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
@@ -54,10 +60,13 @@ public class BadgingNotificationPreferenceControllerTest {
|
|||||||
private BadgingNotificationPreferenceController mController;
|
private BadgingNotificationPreferenceController mController;
|
||||||
private Preference mPreference;
|
private Preference mPreference;
|
||||||
|
|
||||||
|
private static final String KEY_NOTIFICATION_BADGING = "notification_badging";
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
mController = new BadgingNotificationPreferenceController(mContext);
|
mController = new BadgingNotificationPreferenceController(mContext,
|
||||||
|
KEY_NOTIFICATION_BADGING);
|
||||||
mPreference = new Preference(RuntimeEnvironment.application);
|
mPreference = new Preference(RuntimeEnvironment.application);
|
||||||
mPreference.setKey(mController.getPreferenceKey());
|
mPreference.setKey(mController.getPreferenceKey());
|
||||||
when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference);
|
when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference);
|
||||||
@@ -88,9 +97,10 @@ public class BadgingNotificationPreferenceControllerTest {
|
|||||||
public void updateState_preferenceSetCheckedWhenSettingIsOn() {
|
public void updateState_preferenceSetCheckedWhenSettingIsOn() {
|
||||||
final TwoStatePreference preference = mock(TwoStatePreference.class);
|
final TwoStatePreference preference = mock(TwoStatePreference.class);
|
||||||
final Context context = RuntimeEnvironment.application;
|
final Context context = RuntimeEnvironment.application;
|
||||||
Settings.Secure.putInt(context.getContentResolver(), NOTIFICATION_BADGING, 1);
|
Settings.Secure.putInt(context.getContentResolver(), NOTIFICATION_BADGING, ON);
|
||||||
|
|
||||||
mController = new BadgingNotificationPreferenceController(context);
|
mController = new BadgingNotificationPreferenceController(context,
|
||||||
|
KEY_NOTIFICATION_BADGING);
|
||||||
mController.updateState(preference);
|
mController.updateState(preference);
|
||||||
|
|
||||||
verify(preference).setChecked(true);
|
verify(preference).setChecked(true);
|
||||||
@@ -100,9 +110,10 @@ public class BadgingNotificationPreferenceControllerTest {
|
|||||||
public void updateState_preferenceSetUncheckedWhenSettingIsOff() {
|
public void updateState_preferenceSetUncheckedWhenSettingIsOff() {
|
||||||
final TwoStatePreference preference = mock(TwoStatePreference.class);
|
final TwoStatePreference preference = mock(TwoStatePreference.class);
|
||||||
final Context context = RuntimeEnvironment.application;
|
final Context context = RuntimeEnvironment.application;
|
||||||
Settings.Secure.putInt(context.getContentResolver(), NOTIFICATION_BADGING, 0);
|
Settings.Secure.putInt(context.getContentResolver(), NOTIFICATION_BADGING, OFF);
|
||||||
|
|
||||||
mController = new BadgingNotificationPreferenceController(context);
|
mController = new BadgingNotificationPreferenceController(context,
|
||||||
|
KEY_NOTIFICATION_BADGING);
|
||||||
mController.updateState(preference);
|
mController.updateState(preference);
|
||||||
|
|
||||||
verify(preference).setChecked(false);
|
verify(preference).setChecked(false);
|
||||||
@@ -118,11 +129,11 @@ public class BadgingNotificationPreferenceControllerTest {
|
|||||||
public void testSetValue_updatesCorrectly() {
|
public void testSetValue_updatesCorrectly() {
|
||||||
final int newValue = 0;
|
final int newValue = 0;
|
||||||
ContentResolver resolver = mContext.getContentResolver();
|
ContentResolver resolver = mContext.getContentResolver();
|
||||||
Settings.Secure.putInt(resolver, Settings.Secure.NOTIFICATION_BADGING, 1);
|
Settings.Secure.putInt(resolver, Settings.Secure.NOTIFICATION_BADGING, ON);
|
||||||
|
|
||||||
((InlinePayload) mController.getResultPayload()).setValue(mContext, newValue);
|
((InlinePayload) mController.getResultPayload()).setValue(mContext, newValue);
|
||||||
final int updatedValue =
|
final int updatedValue =
|
||||||
Settings.Secure.getInt(resolver, Settings.Secure.NOTIFICATION_BADGING, 1);
|
Settings.Secure.getInt(resolver, Settings.Secure.NOTIFICATION_BADGING, ON);
|
||||||
|
|
||||||
assertThat(updatedValue).isEqualTo(newValue);
|
assertThat(updatedValue).isEqualTo(newValue);
|
||||||
}
|
}
|
||||||
@@ -138,4 +149,40 @@ public class BadgingNotificationPreferenceControllerTest {
|
|||||||
|
|
||||||
assertThat(newValue).isEqualTo(currentValue);
|
assertThat(newValue).isEqualTo(currentValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isChecked_settingIsOff_shouldReturnFalse() {
|
||||||
|
Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BADGING, OFF);
|
||||||
|
|
||||||
|
assertThat(mController.isChecked()).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isChecked_settingIsOn_shouldReturnTrue() {
|
||||||
|
Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BADGING, ON);
|
||||||
|
|
||||||
|
assertThat(mController.isChecked()).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void setChecked_setFalse_disablesSetting() {
|
||||||
|
Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BADGING, ON);
|
||||||
|
|
||||||
|
mController.setChecked(false);
|
||||||
|
int updatedValue = Settings.Secure.getInt(mContext.getContentResolver(),
|
||||||
|
NOTIFICATION_BADGING, -1);
|
||||||
|
|
||||||
|
assertThat(updatedValue).isEqualTo(OFF);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void setChecked_setTrue_enablesSetting() {
|
||||||
|
Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BADGING, OFF);
|
||||||
|
|
||||||
|
mController.setChecked(true);
|
||||||
|
int updatedValue = Settings.Secure.getInt(mContext.getContentResolver(),
|
||||||
|
NOTIFICATION_BADGING, -1);
|
||||||
|
|
||||||
|
assertThat(updatedValue).isEqualTo(ON);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user