support: Add SystemSettingsMasterSwitchPreference
- Import MasterSwitchPreference from Settings(Lib) - Add System setting variant + fix initial values - Use icon_frame as ic in layout instead of icon_container to fix hiding icon - Allow launching fragments with the android:fragment extra same as currently possible for PreferenceScreens - Add possibility to reload value when it might have changed (best to call onResume when the value can also be changed from the sub preference screen) @ZeNiXxX: Adapt to AndroidX Original commit history for TwoTargetPreference from frameworks/base/packages/SettingsLib: commit 8a01abf592762c75bb92a9e1787227c44f1f4a8e Author: Fan Zhang <zhfan@google.com> Adjust icon width to match the width with framework commit 035ff93430b71ef856f299c9f676f8d340403e16 Author: Fan Zhang <zhfan@google.com> Move two target preference layout from settings to lib Original commit history for MasterSwitchPreference.java: commit e2ad415fdba79b27dfd2c98a2b01d56db9257c71 Author: Doris Ling <dling@google.com> Add content description to the master switch toggle button. commit c68ae0b9fdd5f5a149364d0af7e8a9497a91adfd Author: Julia Reynolds <juliacr@google.com> Channel settings updates commit aa805e70ed0c7f4eaa66b6e6a0b79bf1b3b9d16d Author: Doris Ling <dling@google.com> Allow settings preference title to span over multiple lines. commit fe292515d88e803057a5b6077f67ce284b595d7a Author: Fan Zhang <zhfan@google.com> Refactor the layout for MasterSwitch to a 2 target pref commit 6467d2027e8fefd646af03c08d51d91386843463 Author: Doris Ling <dling@google.com> Extends the touch area for the switch in Master Switch preference. commit 762a733afa26b3d49edfae81536c9fcbe9384483 Author: Julia Reynolds <juliacr@google.com> Allow switch preferences to span lines. commit 56199e94876d4bbd89fb0272156702a321dd681e Author: Fan Zhang <zhfan@google.com> Misc fix on network & internet screen for developer preview commit c4c9f4d50eec659eb50f8ed671c27d8c6ef0c924 Author: Doris Ling <dling@google.com> Update Network & internet->Wi-Fi to use MasterSwitchPreference. commit 1432cb852972daedd6c2fde78abb769b722fc47d Author: Doris Ling <dling@google.com> Add new preference class MasterSwitchPreference. Original commit history for preference_widget_master_switch.xml: commit fe292515d88e803057a5b6077f67ce284b595d7a Author: Fan Zhang <zhfan@google.com> Refactor the layout for MasterSwitch to a 2 target pref commit 6467d2027e8fefd646af03c08d51d91386843463 Author: Doris Ling <dling@google.com> Extends the touch area for the switch in Master Switch preference. commit 14c2050330b36a00b708049a691d4e1c39700586 Merge: 249077a0cd 56199e9487 Author: TreeHugger Robot <treehugger-gerrit@google.com> Merge "Misc fix on network & internet screen for developer preview" commit 56199e94876d4bbd89fb0272156702a321dd681e Author: Fan Zhang <zhfan@google.com> Misc fix on network & internet screen for developer preview commit 4f1eca05be15b9a30757434ce6c11abe5e6db381 Author: Doris Ling <dling@google.com> Update the color for the master switch divider. commit 1432cb852972daedd6c2fde78abb769b722fc47d Author: Doris Ling <dling@google.com> Add new preference class MasterSwitchPreference. Change-Id: I7ee76f4626223ec3c66ae7d10ebb87c88c5a8175 Signed-off-by: spezi77 <spezi7713@gmx.net>
This commit is contained in:
128
src/com/cherish/settings/preferences/MasterSwitchPreference.java
Normal file
128
src/com/cherish/settings/preferences/MasterSwitchPreference.java
Normal file
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
* Copyright (C) 2017 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.cherish.settings.preferences;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.TypedArray;
|
||||
import androidx.preference.PreferenceViewHolder;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
import android.widget.Switch;
|
||||
|
||||
import com.android.settings.R;
|
||||
|
||||
/**
|
||||
* A custom preference that provides inline switch toggle. It has a mandatory field for title, and
|
||||
* optional fields for icon and sub-text.
|
||||
*/
|
||||
public class MasterSwitchPreference extends TwoTargetPreference {
|
||||
|
||||
private Switch mSwitch;
|
||||
private boolean mChecked;
|
||||
private boolean mEnableSwitch = true;
|
||||
|
||||
public MasterSwitchPreference(Context context, AttributeSet attrs,
|
||||
int defStyleAttr, int defStyleRes) {
|
||||
super(context, attrs, defStyleAttr, defStyleRes);
|
||||
}
|
||||
|
||||
public MasterSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
}
|
||||
|
||||
public MasterSwitchPreference(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
public MasterSwitchPreference(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getSecondTargetResId() {
|
||||
return R.layout.preference_widget_master_switch;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(PreferenceViewHolder holder) {
|
||||
super.onBindViewHolder(holder);
|
||||
final View widgetView = holder.findViewById(android.R.id.widget_frame);
|
||||
if (widgetView != null) {
|
||||
widgetView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (mSwitch != null && !mSwitch.isEnabled()) {
|
||||
return;
|
||||
}
|
||||
setChecked(!mChecked);
|
||||
if (!callChangeListener(mChecked)) {
|
||||
setChecked(!mChecked);
|
||||
} else {
|
||||
persistBoolean(mChecked);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
mSwitch = (Switch) holder.findViewById(R.id.switchWidget);
|
||||
if (mSwitch != null) {
|
||||
mSwitch.setContentDescription(getTitle());
|
||||
mSwitch.setChecked(mChecked);
|
||||
mSwitch.setEnabled(mEnableSwitch);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isChecked() {
|
||||
return mSwitch != null && mSwitch.isEnabled() && mChecked;
|
||||
}
|
||||
|
||||
public void setChecked(boolean checked) {
|
||||
mChecked = checked;
|
||||
if (mSwitch != null) {
|
||||
mSwitch.setChecked(checked);
|
||||
}
|
||||
}
|
||||
|
||||
public void setSwitchEnabled(boolean enabled) {
|
||||
mEnableSwitch = enabled;
|
||||
if (mSwitch != null) {
|
||||
mSwitch.setEnabled(enabled);
|
||||
}
|
||||
}
|
||||
|
||||
public Switch getSwitch() {
|
||||
return mSwitch;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object onGetDefaultValue(TypedArray a, int index) {
|
||||
return a.getBoolean(index, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
|
||||
setChecked(restoreValue ? getPersistedBoolean((Boolean) defaultValue)
|
||||
: (Boolean) defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Call from outside when value might have changed.
|
||||
*/
|
||||
public void reloadValue() {
|
||||
setChecked(getPersistedBoolean(mChecked));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (C) 2017 AICP
|
||||
*
|
||||
* 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.cherish.settings.preferences;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.AttributeSet;
|
||||
|
||||
import com.android.settings.R;
|
||||
|
||||
import com.cherish.settings.preferences.SystemSettingsStore;
|
||||
|
||||
public class SystemSettingMasterSwitchPreference extends MasterSwitchPreference {
|
||||
|
||||
public SystemSettingMasterSwitchPreference(Context context, AttributeSet attrs, int defStyle) {
|
||||
super(context, attrs, defStyle);
|
||||
setPreferenceDataStore(new SystemSettingsStore(context.getContentResolver()));
|
||||
}
|
||||
|
||||
public SystemSettingMasterSwitchPreference(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
setPreferenceDataStore(new SystemSettingsStore(context.getContentResolver()));
|
||||
}
|
||||
|
||||
public SystemSettingMasterSwitchPreference(Context context) {
|
||||
super(context);
|
||||
setPreferenceDataStore(new SystemSettingsStore(context.getContentResolver()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright (C) 2017 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.cherish.settings.preferences;
|
||||
|
||||
import android.content.Context;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceViewHolder;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
|
||||
import com.android.settings.R;
|
||||
|
||||
public class TwoTargetPreference extends Preference {
|
||||
|
||||
public TwoTargetPreference(Context context, AttributeSet attrs,
|
||||
int defStyleAttr, int defStyleRes) {
|
||||
super(context, attrs, defStyleAttr, defStyleRes);
|
||||
init();
|
||||
}
|
||||
|
||||
public TwoTargetPreference(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
init();
|
||||
}
|
||||
|
||||
public TwoTargetPreference(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
init();
|
||||
}
|
||||
|
||||
public TwoTargetPreference(Context context) {
|
||||
super(context);
|
||||
init();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
setLayoutResource(R.layout.preference_two_target);
|
||||
final int secondTargetResId = getSecondTargetResId();
|
||||
if (secondTargetResId != 0) {
|
||||
setWidgetLayoutResource(secondTargetResId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(PreferenceViewHolder holder) {
|
||||
super.onBindViewHolder(holder);
|
||||
final View divider = holder.findViewById(R.id.two_target_divider);
|
||||
final View widgetFrame = holder.findViewById(android.R.id.widget_frame);
|
||||
final boolean shouldHideSecondTarget = shouldHideSecondTarget();
|
||||
if (divider != null) {
|
||||
divider.setVisibility(shouldHideSecondTarget ? View.GONE : View.VISIBLE);
|
||||
}
|
||||
if (widgetFrame != null) {
|
||||
widgetFrame.setVisibility(shouldHideSecondTarget ? View.GONE : View.VISIBLE);
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean shouldHideSecondTarget() {
|
||||
return getSecondTargetResId() == 0;
|
||||
}
|
||||
|
||||
protected int getSecondTargetResId() {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user