import android.net.Uri;
import android.os.Handler;
import android.provider.Settings;
+import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.support.v7.preference.TwoStatePreference;
import com.android.settings.core.PreferenceControllerMixin;
+import com.android.settings.core.TogglePreferenceController;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settings.R;
import com.android.settings.search.DatabaseIndexingUtils;
import static android.provider.Settings.Secure.NOTIFICATION_BADGING;
-public class BadgingNotificationPreferenceController extends AbstractPreferenceController
+public class BadgingNotificationPreferenceController extends TogglePreferenceController
implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener,
LifecycleObserver, OnResume, OnPause {
private static final String TAG = "BadgeNotifPrefContr";
- private static final String KEY_NOTIFICATION_BADGING = "notification_badging";
- private static final int ON = 1;
- private static final int OFF = 0;
+ @VisibleForTesting
+ static final int ON = 1;
+ @VisibleForTesting
+ static final int OFF = 0;
private SettingObserver mSettingObserver;
- public BadgingNotificationPreferenceController(Context context) {
- super(context);
+ public BadgingNotificationPreferenceController(Context context, String preferenceKey) {
+ super(context, preferenceKey);
}
@Override
}
@Override
- public String getPreferenceKey() {
- return KEY_NOTIFICATION_BADGING;
- }
-
- @Override
- public boolean isAvailable() {
+ public int getAvailabilityStatus() {
return mContext.getResources()
- .getBoolean(com.android.internal.R.bool.config_notificationBadging);
+ .getBoolean(com.android.internal.R.bool.config_notificationBadging)
+ ? AVAILABLE : DISABLED_UNSUPPORTED;
}
@Override
- public void updateState(Preference preference) {
- final boolean checked = Settings.Secure.getInt(mContext.getContentResolver(),
+ public boolean isChecked() {
+ return Settings.Secure.getInt(mContext.getContentResolver(),
NOTIFICATION_BADGING, ON) == ON;
- ((TwoStatePreference) preference).setChecked(checked);
}
@Override
- public boolean onPreferenceChange(Preference preference, Object newValue) {
- final boolean val = (Boolean) newValue;
+ public boolean setChecked(boolean isChecked) {
return Settings.Secure.putInt(mContext.getContentResolver(),
- NOTIFICATION_BADGING, val ? ON : OFF);
+ NOTIFICATION_BADGING, isChecked ? ON : OFF);
}
class SettingObserver extends ContentObserver {
@Override
public ResultPayload getResultPayload() {
final Intent intent = DatabaseIndexingUtils.buildSearchResultPageIntent(mContext,
- ConfigureNotificationSettings.class.getName(), KEY_NOTIFICATION_BADGING,
+ ConfigureNotificationSettings.class.getName(), getPreferenceKey(),
mContext.getString(R.string.configure_notification_settings));
return new InlineSwitchPayload(Settings.Secure.NOTIFICATION_BADGING,
private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
Lifecycle lifecycle, Application app, Fragment host) {
final List<AbstractPreferenceController> controllers = new ArrayList<>();
- final BadgingNotificationPreferenceController badgeController =
- new BadgingNotificationPreferenceController(context);
final PulseNotificationPreferenceController pulseController =
new PulseNotificationPreferenceController(context);
final LockScreenNotificationPreferenceController lockScreenNotificationController =
}
controllers.add(new RecentNotifyingAppsPreferenceController(
context, new NotificationBackend(), app, host));
- controllers.add(badgeController);
controllers.add(pulseController);
controllers.add(lockScreenNotificationController);
controllers.add(new NotificationRingtonePreferenceController(context) {
package com.android.settings.notification;
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 org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
private BadgingNotificationPreferenceController mController;
private Preference mPreference;
+ private static final String KEY_NOTIFICATION_BADGING = "notification_badging";
+
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
- mController = new BadgingNotificationPreferenceController(mContext);
+ mController = new BadgingNotificationPreferenceController(mContext,
+ KEY_NOTIFICATION_BADGING);
mPreference = new Preference(RuntimeEnvironment.application);
mPreference.setKey(mController.getPreferenceKey());
when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference);
public void updateState_preferenceSetCheckedWhenSettingIsOn() {
final TwoStatePreference preference = mock(TwoStatePreference.class);
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);
verify(preference).setChecked(true);
public void updateState_preferenceSetUncheckedWhenSettingIsOff() {
final TwoStatePreference preference = mock(TwoStatePreference.class);
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);
verify(preference).setChecked(false);
public void testSetValue_updatesCorrectly() {
final int newValue = 0;
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);
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(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);
+ }
}