2 * Copyright (C) 2017 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.android.settings.bluetooth;
18 import android.content.Context;
19 import android.support.v14.preference.SwitchPreference;
20 import android.support.v7.preference.Preference;
21 import android.support.v7.preference.PreferenceScreen;
23 import com.android.internal.annotations.VisibleForTesting;
24 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
25 import com.android.settings.core.TogglePreferenceController;
26 import com.android.settings.overlay.FeatureFactory;
27 import com.android.settings.widget.SwitchWidgetController;
28 import com.android.settingslib.RestrictedLockUtils;
29 import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
30 import com.android.settingslib.bluetooth.LocalBluetoothManager;
31 import com.android.settingslib.core.lifecycle.LifecycleObserver;
32 import com.android.settingslib.core.lifecycle.events.OnStart;
33 import com.android.settingslib.core.lifecycle.events.OnStop;
36 * PreferenceController to update of bluetooth {@link SwitchPreference}. It will
38 * 1. Invoke the user toggle
39 * 2. Listen to the update from {@link LocalBluetoothManager}
41 public class BluetoothSwitchPreferenceController extends TogglePreferenceController
42 implements LifecycleObserver, OnStart, OnStop {
44 public static final String KEY_TOGGLE_BLUETOOTH = "toggle_bluetooth_switch";
46 private LocalBluetoothManager mBluetoothManager;
47 private SwitchPreference mBtPreference;
48 private BluetoothEnabler mBluetoothEnabler;
49 private RestrictionUtils mRestrictionUtils;
51 LocalBluetoothAdapter mBluetoothAdapter;
53 public BluetoothSwitchPreferenceController(Context context) {
54 this(context, Utils.getLocalBtManager(context), new RestrictionUtils());
58 public BluetoothSwitchPreferenceController(Context context,
59 LocalBluetoothManager bluetoothManager, RestrictionUtils restrictionUtils) {
60 super(context, KEY_TOGGLE_BLUETOOTH);
61 mBluetoothManager = bluetoothManager;
62 mRestrictionUtils = restrictionUtils;
64 if (mBluetoothManager != null) {
65 mBluetoothAdapter = mBluetoothManager.getBluetoothAdapter();
70 public void displayPreference(PreferenceScreen screen) {
71 super.displayPreference(screen);
72 mBtPreference = (SwitchPreference) screen.findPreference(KEY_TOGGLE_BLUETOOTH);
73 mBluetoothEnabler = new BluetoothEnabler(mContext,
74 new SwitchController(mBtPreference),
75 FeatureFactory.getFactory(mContext).getMetricsFeatureProvider(), mBluetoothManager,
76 MetricsEvent.ACTION_SETTINGS_MASTER_SWITCH_BLUETOOTH_TOGGLE,
81 public int getAvailabilityStatus() {
82 return mBluetoothAdapter != null ? AVAILABLE : DISABLED_UNSUPPORTED;
86 public void onStart() {
87 mBluetoothEnabler.resume(mContext);
91 public void onStop() {
92 mBluetoothEnabler.pause();
96 public boolean isChecked() {
97 return mBluetoothAdapter != null ? mBluetoothAdapter.isEnabled() : false;
101 public boolean setChecked(boolean isChecked) {
102 if (mBluetoothAdapter != null) {
103 mBluetoothAdapter.setBluetoothEnabled(isChecked);
109 * Control the switch inside {@link SwitchPreference}
112 class SwitchController extends SwitchWidgetController implements
113 Preference.OnPreferenceChangeListener {
114 private SwitchPreference mSwitchPreference;
116 public SwitchController(SwitchPreference switchPreference) {
117 mSwitchPreference = switchPreference;
121 public void updateTitle(boolean isChecked) {
125 public void startListening() {
126 mSwitchPreference.setOnPreferenceChangeListener(this);
130 public void stopListening() {
131 mSwitchPreference.setOnPreferenceChangeListener(null);
135 public void setChecked(boolean checked) {
136 mSwitchPreference.setChecked(checked);
140 public boolean isChecked() {
141 return mSwitchPreference.isChecked();
145 public void setEnabled(boolean enabled) {
146 mSwitchPreference.setEnabled(enabled);
150 public boolean onPreferenceChange(Preference preference, Object newValue) {
151 if (mListener != null) {
152 return mListener.onSwitchToggled((Boolean) newValue);
158 public void setDisabledByAdmin(RestrictedLockUtils.EnforcedAdmin admin) {
159 mBtPreference.setEnabled(admin == null);