Merge "Add new preference class MasterSwitchPreference."

This commit is contained in:
TreeHugger Robot
2017-01-21 02:00:06 +00:00
committed by Android (Google) Code Review
16 changed files with 1107 additions and 233 deletions

View File

@@ -24,14 +24,13 @@ import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.widget.Switch;
import android.widget.Toast;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.R;
import com.android.settings.core.instrumentation.MetricsFeatureProvider;
import com.android.settings.search.Index;
import com.android.settings.widget.SwitchBar;
import com.android.settings.widget.SwitchWidgetController;
import com.android.settingslib.WirelessUtils;
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
@@ -41,9 +40,8 @@ import com.android.settingslib.bluetooth.LocalBluetoothManager;
* preference. It turns on/off Bluetooth and ensures the summary of the
* preference reflects the current state.
*/
public final class BluetoothEnabler implements SwitchBar.OnSwitchChangeListener {
private final Switch mSwitch;
private final SwitchBar mSwitchBar;
public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchChangeListener {
private final SwitchWidgetController mSwitchWidget;
private final MetricsFeatureProvider mMetricsFeatureProvider;
private Context mContext;
private boolean mValidListener;
@@ -76,19 +74,18 @@ public final class BluetoothEnabler implements SwitchBar.OnSwitchChangeListener
}
};
public BluetoothEnabler(Context context, SwitchBar switchBar,
MetricsFeatureProvider metricsFeatureProvider) {
public BluetoothEnabler(Context context, SwitchWidgetController switchWidget,
MetricsFeatureProvider metricsFeatureProvider, LocalBluetoothManager manager) {
mContext = context;
mMetricsFeatureProvider = metricsFeatureProvider;
mSwitchBar = switchBar;
mSwitch = switchBar.getSwitch();
mSwitchWidget = switchWidget;
mSwitchWidget.setListener(this);
mValidListener = false;
LocalBluetoothManager manager = Utils.getLocalBtManager(context);
if (manager == null) {
// Bluetooth is not supported
mLocalAdapter = null;
mSwitch.setEnabled(false);
mSwitchWidget.setEnabled(false);
} else {
mLocalAdapter = manager.getBluetoothAdapter();
}
@@ -96,16 +93,16 @@ public final class BluetoothEnabler implements SwitchBar.OnSwitchChangeListener
}
public void setupSwitchBar() {
mSwitchBar.show();
mSwitchWidget.setupView();
}
public void teardownSwitchBar() {
mSwitchBar.hide();
mSwitchWidget.teardownView();
}
public void resume(Context context) {
if (mLocalAdapter == null) {
mSwitch.setEnabled(false);
mSwitchWidget.setEnabled(false);
return;
}
@@ -116,7 +113,7 @@ public final class BluetoothEnabler implements SwitchBar.OnSwitchChangeListener
// Bluetooth state is not sticky, so set it manually
handleStateChanged(mLocalAdapter.getBluetoothState());
mSwitchBar.addOnSwitchChangeListener(this);
mSwitchWidget.startListening();
mContext.registerReceiver(mReceiver, mIntentFilter);
mValidListener = true;
}
@@ -125,47 +122,48 @@ public final class BluetoothEnabler implements SwitchBar.OnSwitchChangeListener
if (mLocalAdapter == null) {
return;
}
mSwitchBar.removeOnSwitchChangeListener(this);
mContext.unregisterReceiver(mReceiver);
mValidListener = false;
if (mValidListener) {
mSwitchWidget.stopListening();
mContext.unregisterReceiver(mReceiver);
mValidListener = false;
}
}
void handleStateChanged(int state) {
switch (state) {
case BluetoothAdapter.STATE_TURNING_ON:
mSwitch.setEnabled(false);
mSwitchWidget.setEnabled(false);
break;
case BluetoothAdapter.STATE_ON:
setChecked(true);
mSwitch.setEnabled(true);
mSwitchWidget.setEnabled(true);
updateSearchIndex(true);
break;
case BluetoothAdapter.STATE_TURNING_OFF:
mSwitch.setEnabled(false);
mSwitchWidget.setEnabled(false);
break;
case BluetoothAdapter.STATE_OFF:
setChecked(false);
mSwitch.setEnabled(true);
mSwitchWidget.setEnabled(true);
updateSearchIndex(false);
break;
default:
setChecked(false);
mSwitch.setEnabled(true);
mSwitchWidget.setEnabled(true);
updateSearchIndex(false);
}
}
private void setChecked(boolean isChecked) {
if (isChecked != mSwitch.isChecked()) {
if (isChecked != mSwitchWidget.isChecked()) {
// set listener to null, so onCheckedChanged won't be called
// if the checked status on Switch isn't changed by user click
if (mValidListener) {
mSwitchBar.removeOnSwitchChangeListener(this);
mSwitchWidget.stopListening();
}
mSwitch.setChecked(isChecked);
mSwitchWidget.setChecked(isChecked);
if (mValidListener) {
mSwitchBar.addOnSwitchChangeListener(this);
mSwitchWidget.startListening();
}
}
}
@@ -180,13 +178,14 @@ public final class BluetoothEnabler implements SwitchBar.OnSwitchChangeListener
}
@Override
public void onSwitchChanged(Switch switchView, boolean isChecked) {
public boolean onSwitchToggled(boolean isChecked) {
// Show toast message if Bluetooth is not allowed in airplane mode
if (isChecked &&
!WirelessUtils.isRadioAllowed(mContext, Settings.Global.RADIO_BLUETOOTH)) {
Toast.makeText(mContext, R.string.wifi_in_airplane_mode, Toast.LENGTH_SHORT).show();
// Reset switch to off
switchView.setChecked(false);
mSwitchWidget.setChecked(false);
return false;
}
mMetricsFeatureProvider.action(mContext, MetricsEvent.ACTION_BLUETOOTH_TOGGLE, isChecked);
@@ -197,12 +196,13 @@ public final class BluetoothEnabler implements SwitchBar.OnSwitchChangeListener
// a) The switch should be OFF but it should still be togglable (enabled = True)
// b) The switch bar should have OFF text.
if (isChecked && !status) {
switchView.setChecked(false);
mSwitch.setEnabled(true);
mSwitchBar.setTextViewLabel(false);
return;
mSwitchWidget.setChecked(false);
mSwitchWidget.setEnabled(true);
mSwitchWidget.updateTitle(false);
return false;
}
}
mSwitch.setEnabled(false);
mSwitchWidget.setEnabled(false);
return true;
}
}

View File

@@ -0,0 +1,100 @@
/*
* 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.android.settings.bluetooth;
import android.content.Context;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.core.PreferenceController;
import com.android.settings.core.lifecycle.LifecycleObserver;
import com.android.settings.core.lifecycle.events.OnPause;
import com.android.settings.core.lifecycle.events.OnResume;
import com.android.settings.core.lifecycle.events.OnStart;
import com.android.settings.core.lifecycle.events.OnStop;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.widget.MasterSwitchPreference;
import com.android.settings.widget.MasterSwitchController;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
public class BluetoothMasterSwitchPreferenceController extends PreferenceController
implements BluetoothSummaryHelper.OnSummaryChangeListener,
LifecycleObserver, OnResume, OnPause, OnStart, OnStop {
private static final String KEY_TOGGLE_BLUETOOTH = "toggle_bluetooth";
private LocalBluetoothManager mBluetoothManager;
private MasterSwitchPreference mBtPreference;
private BluetoothEnabler mBluetoothEnabler;
private BluetoothSummaryHelper mSummaryHelper;
public BluetoothMasterSwitchPreferenceController(Context context,
LocalBluetoothManager bluetoothManager) {
super(context);
mBluetoothManager = bluetoothManager;
mSummaryHelper = new BluetoothSummaryHelper(mContext, mBluetoothManager);
mSummaryHelper.setOnSummaryChangeListener(this);
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mBtPreference = (MasterSwitchPreference) screen.findPreference(KEY_TOGGLE_BLUETOOTH);
mBluetoothEnabler = new BluetoothEnabler(mContext,
new MasterSwitchController(mBtPreference),
FeatureFactory.getFactory(mContext).getMetricsFeatureProvider(), mBluetoothManager);
}
@Override
public boolean isAvailable() {
return true;
}
@Override
public String getPreferenceKey() {
return KEY_TOGGLE_BLUETOOTH;
}
public void onResume() {
mSummaryHelper.setListening(true);
}
@Override
public void onPause() {
mSummaryHelper.setListening(false);
}
@Override
public void onStart() {
if (mBluetoothEnabler != null) {
mBluetoothEnabler.resume(mContext);
}
}
@Override
public void onStop() {
if (mBluetoothEnabler != null) {
mBluetoothEnabler.pause();
}
}
@Override
public void onSummaryChanged(String summary) {
if (mBtPreference != null) {
mBtPreference.setSummary(summary);
}
}
}

View File

@@ -45,6 +45,7 @@ import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.LinkifyUtils;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.bluetooth.BluetoothSummaryHelper.OnSummaryChangeListener;
import com.android.settings.dashboard.SummaryLoader;
import com.android.settings.location.ScanningSettings;
import com.android.settings.search.BaseSearchIndexProvider;
@@ -52,13 +53,12 @@ import com.android.settings.search.Indexable;
import com.android.settings.search.SearchIndexableRaw;
import com.android.settings.widget.FooterPreference;
import com.android.settings.widget.SwitchBar;
import com.android.settingslib.bluetooth.BluetoothCallback;
import com.android.settings.widget.SwitchBarController;
import com.android.settingslib.bluetooth.BluetoothDeviceFilter;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Set;
@@ -148,7 +148,8 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment implem
final SettingsActivity activity = (SettingsActivity) getActivity();
mSwitchBar = activity.getSwitchBar();
mBluetoothEnabler = new BluetoothEnabler(activity, mSwitchBar, mMetricsFeatureProvider);
mBluetoothEnabler = new BluetoothEnabler(activity, new SwitchBarController(mSwitchBar),
mMetricsFeatureProvider, Utils.getLocalBtManager(activity));
mBluetoothEnabler.setupSwitchBar();
}
@@ -508,113 +509,35 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment implem
}
@VisibleForTesting
static class SummaryProvider
implements SummaryLoader.SummaryProvider, BluetoothCallback {
static class SummaryProvider implements SummaryLoader.SummaryProvider, OnSummaryChangeListener {
private final LocalBluetoothManager mBluetoothManager;
private final Context mContext;
private final SummaryLoader mSummaryLoader;
private boolean mEnabled;
private int mConnectionState;
@VisibleForTesting
BluetoothSummaryHelper mSummaryHelper;
public SummaryProvider(Context context, SummaryLoader summaryLoader,
LocalBluetoothManager bluetoothManager) {
mBluetoothManager = bluetoothManager;
mContext = context;
mSummaryLoader = summaryLoader;
mSummaryHelper = new BluetoothSummaryHelper(mContext, mBluetoothManager);
mSummaryHelper.setOnSummaryChangeListener(this);
}
@Override
public void setListening(boolean listening) {
BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
if (defaultAdapter == null) return;
if (listening) {
mEnabled = defaultAdapter.isEnabled();
mConnectionState = defaultAdapter.getConnectionState();
mSummaryLoader.setSummary(this, getSummary());
mBluetoothManager.getEventManager().registerCallback(this);
} else {
mBluetoothManager.getEventManager().unregisterCallback(this);
mSummaryHelper.setListening(listening);
}
@Override
public void onSummaryChanged(String summary) {
if (mSummaryLoader != null) {
mSummaryLoader.setSummary(this, summary);
}
}
private CharSequence getSummary() {
if (!mEnabled) {
return mContext.getString(R.string.bluetooth_disabled);
} else if (mConnectionState == BluetoothAdapter.STATE_CONNECTED) {
return mContext.getString(R.string.bluetooth_connected);
} else {
return mContext.getString(R.string.bluetooth_disconnected);
}
}
@Override
public void onBluetoothStateChanged(int bluetoothState) {
mEnabled = bluetoothState == BluetoothAdapter.STATE_ON
|| bluetoothState == BluetoothAdapter.STATE_TURNING_ON;
mSummaryLoader.setSummary(this, getSummary());
}
@Override
public void onConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state) {
mConnectionState = state;
updateConnected();
mSummaryLoader.setSummary(this, getSummary());
}
@Override
public void onScanningStateChanged(boolean started) {
}
@Override
public void onDeviceAdded(CachedBluetoothDevice cachedDevice) {
}
@Override
public void onDeviceDeleted(CachedBluetoothDevice cachedDevice) {
}
@Override
public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {
}
private void updateConnected() {
// Make sure our connection state is up to date.
int state = mBluetoothManager.getBluetoothAdapter().getConnectionState();
if (state != mConnectionState) {
mConnectionState = state;
return;
}
final Collection<CachedBluetoothDevice> devices = getDevices();
if (devices == null) {
mConnectionState = BluetoothAdapter.STATE_DISCONNECTED;
return;
}
if (mConnectionState == BluetoothAdapter.STATE_CONNECTED) {
CachedBluetoothDevice connectedDevice = null;
for (CachedBluetoothDevice device : devices) {
if (device.isConnected()) {
connectedDevice = device;
}
}
if (connectedDevice == null) {
// If somehow we think we are connected, but have no connected devices, we
// aren't connected.
mConnectionState = BluetoothAdapter.STATE_DISCONNECTED;
}
}
}
private Collection<CachedBluetoothDevice> getDevices() {
return mBluetoothManager != null
? mBluetoothManager.getCachedDeviceManager().getCachedDevicesCopy()
: null;
}
}
public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY

View File

@@ -0,0 +1,172 @@
/*
* 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.android.settings.bluetooth;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.text.TextUtils;
import com.android.settings.R;
import com.android.settingslib.bluetooth.BluetoothCallback;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import java.util.Collection;
/**
* Helper class that listeners to bluetooth callback and notify client when there is update in
* bluetooth summary info.
*/
public final class BluetoothSummaryHelper implements BluetoothCallback {
private OnSummaryChangeListener mListener;
private final LocalBluetoothManager mBluetoothManager;
private final LocalBluetoothAdapter mBluetoothAdapter;
private final Context mContext;
private boolean mEnabled;
private int mConnectionState;
private String mSummary;
public interface OnSummaryChangeListener {
/**
* Called when bluetooth summary has changed.
*
* @param summary The new bluetooth summary .
*/
void onSummaryChanged(String summary);
}
public BluetoothSummaryHelper(Context context, LocalBluetoothManager bluetoothManager) {
mContext = context;
mBluetoothManager = bluetoothManager;
mBluetoothAdapter = mBluetoothManager != null
? mBluetoothManager.getBluetoothAdapter() : null;
}
@Override
public void onBluetoothStateChanged(int bluetoothState) {
mEnabled = bluetoothState == BluetoothAdapter.STATE_ON
|| bluetoothState == BluetoothAdapter.STATE_TURNING_ON;
notifyChangeIfNeeded();
}
@Override
public void onConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state) {
mConnectionState = state;
updateConnected();
notifyChangeIfNeeded();
}
@Override
public void onScanningStateChanged(boolean started) {
}
@Override
public void onDeviceAdded(CachedBluetoothDevice cachedDevice) {
}
@Override
public void onDeviceDeleted(CachedBluetoothDevice cachedDevice) {
}
@Override
public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {
}
public void setOnSummaryChangeListener(OnSummaryChangeListener listener) {
mListener = listener;
}
public void setListening(boolean listening) {
if (mBluetoothAdapter == null) {
return;
}
if (listening) {
mEnabled = mBluetoothAdapter.isEnabled();
mConnectionState = mBluetoothAdapter.getConnectionState();
notifyChangeIfNeeded();
mBluetoothManager.getEventManager().registerCallback(this);
} else {
mBluetoothManager.getEventManager().unregisterCallback(this);
}
}
private void notifyChangeIfNeeded() {
String summary = getSummary();
if (!TextUtils.equals(mSummary, summary)) {
mSummary = summary;
if (mListener != null) {
mListener.onSummaryChanged(summary);
}
}
}
private String getSummary() {
if (!mEnabled) {
return mContext.getString(R.string.bluetooth_disabled);
}
switch (mConnectionState) {
case BluetoothAdapter.STATE_CONNECTED:
return mContext.getString(R.string.bluetooth_connected);
case BluetoothAdapter.STATE_CONNECTING:
return mContext.getString(R.string.bluetooth_connecting);
case BluetoothAdapter.STATE_DISCONNECTING:
return mContext.getString(R.string.bluetooth_disconnecting);
default:
return mContext.getString(R.string.bluetooth_disconnected);
}
}
private void updateConnected() {
if (mBluetoothAdapter == null) {
return;
}
// Make sure our connection state is up to date.
int state = mBluetoothAdapter.getConnectionState();
if (state != mConnectionState) {
mConnectionState = state;
return;
}
final Collection<CachedBluetoothDevice> devices = getDevices();
if (devices == null) {
mConnectionState = BluetoothAdapter.STATE_DISCONNECTED;
return;
}
if (mConnectionState == BluetoothAdapter.STATE_CONNECTED) {
CachedBluetoothDevice connectedDevice = null;
for (CachedBluetoothDevice device : devices) {
if (device.isConnected()) {
connectedDevice = device;
break;
}
}
if (connectedDevice == null) {
// If somehow we think we are connected, but have no connected devices, we
// aren't connected.
mConnectionState = BluetoothAdapter.STATE_DISCONNECTED;
}
}
}
private Collection<CachedBluetoothDevice> getDevices() {
return mBluetoothManager != null
? mBluetoothManager.getCachedDeviceManager().getCachedDevicesCopy()
: null;
}
}

View File

@@ -20,7 +20,10 @@ import android.provider.SearchIndexableResource;
import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.R;
import com.android.settings.bluetooth.BluetoothMasterSwitchPreferenceController;
import com.android.settings.bluetooth.Utils;
import com.android.settings.core.PreferenceController;
import com.android.settings.core.lifecycle.Lifecycle;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.deviceinfo.UsbBackend;
import com.android.settings.nfc.NfcPreferenceController;
@@ -37,6 +40,7 @@ public class ConnectedDeviceDashboardFragment extends DashboardFragment {
private static final String TAG = "ConnectedDeviceFrag";
private UsbModePreferenceController mUsbPrefController;
private BluetoothMasterSwitchPreferenceController mBluetoothPreferenceController;
@Override
public int getMetricsCategory() {
@@ -61,13 +65,19 @@ public class ConnectedDeviceDashboardFragment extends DashboardFragment {
@Override
protected List<PreferenceController> getPreferenceControllers(Context context) {
final List<PreferenceController> controllers = new ArrayList<>();
final Lifecycle lifecycle = getLifecycle();
final NfcPreferenceController nfcPreferenceController =
new NfcPreferenceController(context);
getLifecycle().addObserver(nfcPreferenceController);
lifecycle.addObserver(nfcPreferenceController);
controllers.add(nfcPreferenceController);
mUsbPrefController = new UsbModePreferenceController(context, new UsbBackend(context));
getLifecycle().addObserver(mUsbPrefController);
lifecycle.addObserver(mUsbPrefController);
controllers.add(mUsbPrefController);
mBluetoothPreferenceController =
new BluetoothMasterSwitchPreferenceController(
context, Utils.getLocalBtManager(context));
lifecycle.addObserver(mBluetoothPreferenceController);
controllers.add(mBluetoothPreferenceController);
return controllers;
}

View File

@@ -0,0 +1,70 @@
/*
* 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.android.settings.widget;
import android.support.v7.preference.Preference;
/*
* The switch controller that is used to update the switch widget in the MasterSwitchPreference
* layout.
*/
public class MasterSwitchController extends SwitchWidgetController implements
Preference.OnPreferenceChangeListener {
private final MasterSwitchPreference mPreference;
public MasterSwitchController(MasterSwitchPreference preference) {
mPreference = preference;
}
@Override
public void updateTitle(boolean isChecked) {
}
@Override
public void startListening() {
mPreference.setOnPreferenceChangeListener(this);
}
@Override
public void stopListening() {
mPreference.setOnPreferenceChangeListener(null);
}
@Override
public void setChecked(boolean checked) {
mPreference.setChecked(checked);
}
@Override
public boolean isChecked() {
return mPreference.isChecked();
}
@Override
public void setEnabled(boolean enabled) {
mPreference.setSwitchEnabled(enabled);
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (mListener != null) {
return mListener.onSwitchToggled((Boolean) newValue);
}
return false;
}
}

View File

@@ -0,0 +1,102 @@
/*
* 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.android.settings.widget;
import android.content.Context;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceViewHolder;
import android.util.AttributeSet;
import android.widget.CompoundButton;
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 Preference {
private Switch mSwitch;
private boolean mChecked;
public MasterSwitchPreference(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
public MasterSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public MasterSwitchPreference(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MasterSwitchPreference(Context context) {
super(context);
init();
}
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
mSwitch = (Switch) holder.itemView.findViewById(R.id.switchWidget);
if (mSwitch != null) {
mSwitch.setChecked(mChecked);
mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton button, boolean isChecked) {
if (!callChangeListener(isChecked)) {
button.setChecked(!isChecked);
} else {
persistBoolean(isChecked);
mChecked = isChecked;
}
}
});
}
}
public boolean isChecked() {
return isEnabled() && mChecked;
}
public void setChecked(boolean checked) {
mChecked = checked;
if (mSwitch != null) {
mSwitch.setChecked(checked);
}
}
public boolean isSwitchEnabled() {
return isEnabled() && mSwitch != null && mSwitch.isEnabled();
}
public void setSwitchEnabled(boolean enabled) {
if (mSwitch != null) {
mSwitch.setEnabled(enabled);
}
}
private void init() {
setWidgetLayoutResource(R.layout.preference_widget_master_switch);
}
}

View File

@@ -0,0 +1,81 @@
/*
* 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.android.settings.widget;
import android.widget.Switch;
/*
* The switch controller that is used to update the switch widget in the SwitchBar layout.
*/
public class SwitchBarController extends SwitchWidgetController implements
SwitchBar.OnSwitchChangeListener {
private final SwitchBar mSwitchBar;
private final Switch mSwitch;
public SwitchBarController(SwitchBar switchBar) {
mSwitchBar = switchBar;
mSwitch = switchBar.getSwitch();
}
@Override
public void setupView() {
mSwitchBar.show();
}
@Override
public void teardownView() {
mSwitchBar.hide();
}
@Override
public void updateTitle(boolean isChecked) {
mSwitchBar.setTextViewLabel(isChecked);
}
@Override
public void startListening() {
mSwitchBar.addOnSwitchChangeListener(this);
}
@Override
public void stopListening() {
mSwitchBar.removeOnSwitchChangeListener(this);
}
@Override
public void setChecked(boolean checked) {
mSwitch.setChecked(checked);
}
@Override
public boolean isChecked() {
return mSwitch.isChecked();
}
@Override
public void setEnabled(boolean enabled) {
mSwitch.setEnabled(enabled);
}
@Override
public void onSwitchChanged(Switch switchView, boolean isChecked) {
if (mListener != null) {
mListener.onSwitchToggled(isChecked);
}
}
}

View File

@@ -0,0 +1,58 @@
/*
* 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.android.settings.widget;
/*
* A controller class for general switch widget handling. We have different containers that provide
* different forms of switch layout. Provide a centralized control for updating the switch widget.
*/
public abstract class SwitchWidgetController {
protected OnSwitchChangeListener mListener;
public interface OnSwitchChangeListener {
/**
* Called when the checked state of the Switch has changed.
*
* @param isChecked The new checked state of switchView.
*/
boolean onSwitchToggled(boolean isChecked);
}
public void setupView() {
}
public void teardownView() {
}
public void setListener(OnSwitchChangeListener listener) {
mListener = listener;
}
public abstract void updateTitle(boolean isChecked);
public abstract void startListening();
public abstract void stopListening();
public abstract void setChecked(boolean checked);
public abstract boolean isChecked();
public abstract void setEnabled(boolean enabled);
}