Convert Blink light to TogglePreferenceController

Convert Blink light (notification) controller:
PulseNotificationPreferenceController
to TogglePreferenceController for slices

Change-Id: I4c49d2d52a5909b45f1a74518aa925abb14e1336
Fixes: 74923755
Test: make RunSettingsRoboTests
This commit is contained in:
HJ ChangLiao
2018-04-17 17:52:55 +08:00
parent e2d07ea458
commit c65b1b3cca
4 changed files with 90 additions and 45 deletions

View File

@@ -24,33 +24,28 @@ import android.os.Handler;
import android.provider.Settings;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.support.v7.preference.TwoStatePreference;
import android.util.Log;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settings.core.TogglePreferenceController;
import com.android.settingslib.core.lifecycle.events.OnPause;
import com.android.settingslib.core.lifecycle.events.OnResume;
import static android.provider.Settings.System.NOTIFICATION_LIGHT_PULSE;
public class PulseNotificationPreferenceController extends AbstractPreferenceController
implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener,
LifecycleObserver, OnResume, OnPause {
public class PulseNotificationPreferenceController extends TogglePreferenceController
implements OnResume, OnPause {
private static final String TAG = "PulseNotifPrefContr";
private static final String KEY_NOTIFICATION_PULSE = "notification_pulse";
private static final int ON = 1;
private static final int OFF = 0;
private SettingObserver mSettingObserver;
public PulseNotificationPreferenceController(Context context) {
super(context);
public PulseNotificationPreferenceController(Context context, String key) {
super(context, key);
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
Preference preference = screen.findPreference(KEY_NOTIFICATION_PULSE);
Preference preference = screen.findPreference(getPreferenceKey());
if (preference != null) {
mSettingObserver = new SettingObserver(preference);
}
@@ -71,32 +66,22 @@ public class PulseNotificationPreferenceController extends AbstractPreferenceCon
}
@Override
public String getPreferenceKey() {
return KEY_NOTIFICATION_PULSE;
public int getAvailabilityStatus() {
return mContext.getResources().getBoolean(
com.android.internal.R.bool.config_intrusiveNotificationLed) ? AVAILABLE
: DISABLED_UNSUPPORTED;
}
@Override
public boolean isAvailable() {
return mContext.getResources()
.getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed);
public boolean isChecked() {
return Settings.System.getInt(mContext.getContentResolver(), NOTIFICATION_LIGHT_PULSE, OFF)
== ON;
}
@Override
public void updateState(Preference preference) {
try {
final boolean checked = Settings.System.getInt(mContext.getContentResolver(),
NOTIFICATION_LIGHT_PULSE) == 1;
((TwoStatePreference) preference).setChecked(checked);
} catch (Settings.SettingNotFoundException snfe) {
Log.e(TAG, NOTIFICATION_LIGHT_PULSE + " not found");
}
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
final boolean val = (Boolean) newValue;
return Settings.System.putInt(mContext.getContentResolver(),
NOTIFICATION_LIGHT_PULSE, val ? 1 : 0);
public boolean setChecked(boolean isChecked) {
return Settings.System.putInt(mContext.getContentResolver(), NOTIFICATION_LIGHT_PULSE,
isChecked ? ON : OFF);
}
class SettingObserver extends ContentObserver {