Convert BatterySaverButton controller to Slice compatible.

- Update preference key to match the key defined in SettingsSlicesContract
- Model TwoStateButtonPreference similar to TwoStatePreference (add
  setChecked, isChecked method)
- Remove TwoStateButtonPreferenceController entirely because all methods
  are moved into Preference directly for better encapsulation.
- Make BatterySaverButtonPrefController direclty implement
  TogglePreferenceController. It was not possible before because the
  interface between TwoStateButtonPreferene is too different from
  TwoStatePreference.

Bug: 80106671
Test: robotests
Change-Id: Ib72807dcf1b36e959e08df8d80538c3f9f79b76d
This commit is contained in:
Fan Zhang
2018-05-22 14:12:32 -07:00
committed by Matthew Fritze
parent c4a3393b45
commit ddc6c646c0
7 changed files with 159 additions and 198 deletions

View File

@@ -18,22 +18,34 @@ package com.android.settings.widget;
import android.content.Context;
import android.content.res.TypedArray;
import androidx.core.content.res.TypedArrayUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import com.android.settings.R;
import com.android.settings.applications.LayoutPreference;
import androidx.annotation.VisibleForTesting;
import androidx.core.content.res.TypedArrayUtils;
/**
* Preference that presents a button with two states(On vs Off)
*/
public class TwoStateButtonPreference extends LayoutPreference {
public class TwoStateButtonPreference extends LayoutPreference implements
View.OnClickListener {
private boolean mIsChecked;
private final Button mButtonOn;
private final Button mButtonOff;
public TwoStateButtonPreference(Context context, AttributeSet attrs) {
super(context, attrs, TypedArrayUtils.getAttr(
context, R.attr.twoStateButtonPreferenceStyle, android.R.attr.preferenceStyle));
if (attrs != null) {
if (attrs == null) {
mButtonOn = null;
mButtonOff = null;
} else {
final TypedArray styledAttrs = context.obtainStyledAttributes(attrs,
R.styleable.TwoStateButtonPreference);
final int textOnId = styledAttrs.getResourceId(
@@ -44,19 +56,52 @@ public class TwoStateButtonPreference extends LayoutPreference {
R.string.summary_placeholder);
styledAttrs.recycle();
final Button buttonOn = getStateOnButton();
buttonOn.setText(textOnId);
final Button buttonOff = getStateOffButton();
buttonOff.setText(textOffId);
mButtonOn = findViewById(R.id.state_on_button);
mButtonOn.setText(textOnId);
mButtonOn.setOnClickListener(this);
mButtonOff = findViewById(R.id.state_off_button);
mButtonOff.setText(textOffId);
mButtonOff.setOnClickListener(this);
setChecked(isChecked());
}
}
public Button getStateOnButton() {
return findViewById(R.id.state_on_button);
@Override
public void onClick(View v) {
final boolean stateOn = v.getId() == R.id.state_on_button;
setChecked(stateOn);
callChangeListener(stateOn);
}
public void setChecked(boolean checked) {
// Update state
mIsChecked = checked;
// And update UI
if (checked) {
mButtonOn.setVisibility(View.GONE);
mButtonOff.setVisibility(View.VISIBLE);
} else {
mButtonOn.setVisibility(View.VISIBLE);
mButtonOff.setVisibility(View.GONE);
}
}
public boolean isChecked() {
return mIsChecked;
}
public void setButtonEnabled(boolean enabled) {
mButtonOn.setEnabled(enabled);
mButtonOff.setEnabled(enabled);
}
@VisibleForTesting
public Button getStateOnButton() {
return mButtonOn;
}
@VisibleForTesting
public Button getStateOffButton() {
return findViewById(R.id.state_off_button);
return mButtonOff;
}
}

View File

@@ -1,78 +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.widget;
import android.content.Context;
import androidx.preference.PreferenceScreen;
import android.view.View;
import android.widget.Button;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
/**
* Controller to update the button with two states(On vs Off).
*/
public abstract class TwoStateButtonPreferenceController extends BasePreferenceController
implements View.OnClickListener {
private Button mButtonOn;
private Button mButtonOff;
public TwoStateButtonPreferenceController(Context context, String key) {
super(context, key);
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
final TwoStateButtonPreference preference =
(TwoStateButtonPreference) screen.findPreference(getPreferenceKey());
mButtonOn = preference.getStateOnButton();
mButtonOn.setOnClickListener(this);
mButtonOff = preference.getStateOffButton();
mButtonOff.setOnClickListener(this);
}
protected void setButtonVisibility(boolean stateOn) {
if (stateOn) {
mButtonOff.setVisibility(View.GONE);
mButtonOn.setVisibility(View.VISIBLE);
} else {
mButtonOff.setVisibility(View.VISIBLE);
mButtonOn.setVisibility(View.GONE);
}
}
protected void setButtonEnabled(boolean enabled) {
mButtonOn.setEnabled(enabled);
mButtonOff.setEnabled(enabled);
}
@Override
public void onClick(View v) {
final boolean stateOn = v.getId() == R.id.state_on_button;
onButtonClicked(stateOn);
}
/**
* Callback when button is clicked
*
* @param stateOn {@code true} if stateOn button is clicked, otherwise it means stateOff
* button is clicked
*/
public abstract void onButtonClicked(boolean stateOn);
}