Merge "Use MainSwitchPreference on Bubbles, Screen Saver and One-Handed mode pages." into sc-dev
This commit is contained in:
@@ -129,9 +129,8 @@ public class DreamSettings extends DashboardFragment {
|
||||
}
|
||||
|
||||
private static List<AbstractPreferenceController> buildPreferenceControllers(Context context) {
|
||||
List<AbstractPreferenceController> controllers = new ArrayList<>();
|
||||
final List<AbstractPreferenceController> controllers = new ArrayList<>();
|
||||
controllers.add(new WhenToDreamPreferenceController(context));
|
||||
controllers.add(new StartNowPreferenceController(context));
|
||||
return controllers;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,63 +17,52 @@
|
||||
package com.android.settings.dream;
|
||||
|
||||
import android.content.Context;
|
||||
import android.widget.Button;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.core.PreferenceControllerMixin;
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
import com.android.settings.widget.SettingsMainSwitchPreferenceController;
|
||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||
import com.android.settingslib.dream.DreamBackend;
|
||||
import com.android.settingslib.widget.LayoutPreference;
|
||||
|
||||
public class StartNowPreferenceController extends AbstractPreferenceController implements
|
||||
PreferenceControllerMixin {
|
||||
/**
|
||||
* Controller that used to enable screen saver
|
||||
*/
|
||||
public class StartNowPreferenceController extends SettingsMainSwitchPreferenceController {
|
||||
|
||||
private static final String PREF_KEY = "dream_start_now_button_container";
|
||||
private final DreamBackend mBackend;
|
||||
private final MetricsFeatureProvider mMetricsFeatureProvider;
|
||||
|
||||
public StartNowPreferenceController(Context context) {
|
||||
super(context);
|
||||
|
||||
public StartNowPreferenceController(Context context, String preferenceKey) {
|
||||
super(context, preferenceKey);
|
||||
mBackend = DreamBackend.getInstance(context);
|
||||
mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPreferenceKey() {
|
||||
return PREF_KEY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
|
||||
LayoutPreference pref = screen.findPreference(getPreferenceKey());
|
||||
Button startButton = pref.findViewById(R.id.dream_start_now_button);
|
||||
startButton.setOnClickListener(v -> {
|
||||
mMetricsFeatureProvider.logClickedPreference(pref,
|
||||
pref.getExtras().getInt(DashboardFragment.CATEGORY));
|
||||
mBackend.startDreaming();
|
||||
});
|
||||
public int getAvailabilityStatus() {
|
||||
return AVAILABLE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
super.updateState(preference);
|
||||
mSwitchPreference.setChecked(false);
|
||||
mSwitchPreference.setEnabled(mBackend.getWhenToDreamSetting() != DreamBackend.NEVER);
|
||||
}
|
||||
|
||||
Button startButton = ((LayoutPreference) preference)
|
||||
.findViewById(R.id.dream_start_now_button);
|
||||
startButton.setEnabled(mBackend.getWhenToDreamSetting() != DreamBackend.NEVER);
|
||||
@Override
|
||||
public boolean isChecked() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setChecked(boolean isChecked) {
|
||||
if (isChecked) {
|
||||
mMetricsFeatureProvider.logClickedPreference(mSwitchPreference,
|
||||
mSwitchPreference.getExtras().getInt(DashboardFragment.CATEGORY));
|
||||
mBackend.startDreaming();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,12 +20,12 @@ import android.content.Context;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
import com.android.settings.core.TogglePreferenceController;
|
||||
import com.android.settings.widget.SettingsMainSwitchPreferenceController;
|
||||
|
||||
/**
|
||||
* The controller to handle one-handed mode enable or disable state.
|
||||
**/
|
||||
public class OneHandedEnablePreferenceController extends TogglePreferenceController {
|
||||
public class OneHandedEnablePreferenceController extends SettingsMainSwitchPreferenceController {
|
||||
|
||||
public OneHandedEnablePreferenceController(Context context, String key) {
|
||||
super(context, key);
|
||||
|
||||
@@ -30,8 +30,7 @@ import androidx.annotation.VisibleForTesting;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.core.PreferenceControllerMixin;
|
||||
import com.android.settings.core.TogglePreferenceController;
|
||||
import com.android.settings.widget.SettingsMainSwitchPreferenceController;
|
||||
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settingslib.core.lifecycle.events.OnPause;
|
||||
import com.android.settingslib.core.lifecycle.events.OnResume;
|
||||
@@ -40,9 +39,8 @@ import com.android.settingslib.core.lifecycle.events.OnResume;
|
||||
* Feature level screen for bubbles, available through notification menu.
|
||||
* Allows user to turn bubbles on or off for the device.
|
||||
*/
|
||||
public class BubbleNotificationPreferenceController extends TogglePreferenceController
|
||||
implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener,
|
||||
LifecycleObserver, OnResume, OnPause {
|
||||
public class BubbleNotificationPreferenceController extends
|
||||
SettingsMainSwitchPreferenceController implements LifecycleObserver, OnResume, OnPause {
|
||||
|
||||
private static final String TAG = "BubbleNotifPrefContr";
|
||||
|
||||
@@ -60,9 +58,8 @@ public class BubbleNotificationPreferenceController extends TogglePreferenceCont
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
Preference preference = screen.findPreference(getPreferenceKey());
|
||||
if (preference != null) {
|
||||
mSettingObserver = new SettingObserver(preference);
|
||||
if (mSwitchPreference != null) {
|
||||
mSettingObserver = new SettingObserver(mSwitchPreference);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,21 +83,22 @@ public class BubbleNotificationPreferenceController extends TogglePreferenceCont
|
||||
return am.isLowRamDevice() ? UNSUPPORTED_ON_DEVICE : AVAILABLE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSliceable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isChecked() {
|
||||
return Settings.Secure.getInt(mContext.getContentResolver(),
|
||||
return Settings.Global.getInt(mContext.getContentResolver(),
|
||||
NOTIFICATION_BUBBLES, ON) == ON;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setChecked(boolean isChecked) {
|
||||
return Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BUBBLES,
|
||||
isChecked ? ON : OFF);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSliceable() {
|
||||
return false;
|
||||
Settings.Global.putInt(mContext.getContentResolver(),
|
||||
NOTIFICATION_BUBBLES, isChecked ? ON : OFF);
|
||||
return true;
|
||||
}
|
||||
|
||||
class SettingObserver extends ContentObserver {
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright (C) 2021 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.widget;
|
||||
|
||||
import android.content.Context;
|
||||
import android.widget.Switch;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.core.TogglePreferenceController;
|
||||
import com.android.settingslib.widget.MainSwitchPreference;
|
||||
import com.android.settingslib.widget.OnMainSwitchChangeListener;
|
||||
|
||||
/**
|
||||
* Preference controller for MainSwitchPreference.
|
||||
*/
|
||||
public abstract class SettingsMainSwitchPreferenceController extends
|
||||
TogglePreferenceController implements OnMainSwitchChangeListener {
|
||||
|
||||
protected MainSwitchPreference mSwitchPreference;
|
||||
|
||||
public SettingsMainSwitchPreferenceController(Context context, String preferenceKey) {
|
||||
super(context, preferenceKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
final Preference pref = screen.findPreference(getPreferenceKey());
|
||||
if (pref != null && pref instanceof MainSwitchPreference) {
|
||||
mSwitchPreference = (MainSwitchPreference) pref;
|
||||
mSwitchPreference.addOnSwitchChangeListener(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSwitchChanged(Switch switchView, boolean isChecked) {
|
||||
mSwitchPreference.setChecked(isChecked);
|
||||
setChecked(isChecked);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user