OSDN Git Service

Import translations. DO NOT MERGE am: 3b4a3e0a0f -s ours
[android-x86/packages-apps-Settings.git] / src / com / android / settings / bluetooth / BluetoothSwitchPreferenceController.java
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package com.android.settings.bluetooth;
17
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;
22
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;
34
35 /**
36  * PreferenceController to update of bluetooth {@link SwitchPreference}. It will
37  *
38  * 1. Invoke the user toggle
39  * 2. Listen to the update from {@link LocalBluetoothManager}
40  */
41 public class BluetoothSwitchPreferenceController extends TogglePreferenceController
42         implements LifecycleObserver, OnStart, OnStop {
43
44     public static final String KEY_TOGGLE_BLUETOOTH = "toggle_bluetooth_switch";
45
46     private LocalBluetoothManager mBluetoothManager;
47     private SwitchPreference mBtPreference;
48     private BluetoothEnabler mBluetoothEnabler;
49     private RestrictionUtils mRestrictionUtils;
50     @VisibleForTesting
51     LocalBluetoothAdapter mBluetoothAdapter;
52
53     public BluetoothSwitchPreferenceController(Context context) {
54         this(context, Utils.getLocalBtManager(context), new RestrictionUtils());
55     }
56
57     @VisibleForTesting
58     public BluetoothSwitchPreferenceController(Context context,
59             LocalBluetoothManager bluetoothManager, RestrictionUtils restrictionUtils) {
60         super(context, KEY_TOGGLE_BLUETOOTH);
61         mBluetoothManager = bluetoothManager;
62         mRestrictionUtils = restrictionUtils;
63
64         if (mBluetoothManager != null) {
65             mBluetoothAdapter = mBluetoothManager.getBluetoothAdapter();
66         }
67     }
68
69     @Override
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,
77                 mRestrictionUtils);
78     }
79
80     @Override
81     public int getAvailabilityStatus() {
82         return mBluetoothAdapter != null ? AVAILABLE : DISABLED_UNSUPPORTED;
83     }
84
85     @Override
86     public void onStart() {
87         mBluetoothEnabler.resume(mContext);
88     }
89
90     @Override
91     public void onStop() {
92         mBluetoothEnabler.pause();
93     }
94
95     @Override
96     public boolean isChecked() {
97         return mBluetoothAdapter != null ? mBluetoothAdapter.isEnabled() : false;
98     }
99
100     @Override
101     public boolean setChecked(boolean isChecked) {
102         if (mBluetoothAdapter != null) {
103             mBluetoothAdapter.setBluetoothEnabled(isChecked);
104         }
105         return true;
106     }
107
108     /**
109      * Control the switch inside {@link SwitchPreference}
110      */
111     @VisibleForTesting
112     class SwitchController extends SwitchWidgetController implements
113             Preference.OnPreferenceChangeListener {
114         private SwitchPreference mSwitchPreference;
115
116         public SwitchController(SwitchPreference switchPreference) {
117             mSwitchPreference = switchPreference;
118         }
119
120         @Override
121         public void updateTitle(boolean isChecked) {
122         }
123
124         @Override
125         public void startListening() {
126             mSwitchPreference.setOnPreferenceChangeListener(this);
127         }
128
129         @Override
130         public void stopListening() {
131             mSwitchPreference.setOnPreferenceChangeListener(null);
132         }
133
134         @Override
135         public void setChecked(boolean checked) {
136             mSwitchPreference.setChecked(checked);
137         }
138
139         @Override
140         public boolean isChecked() {
141             return mSwitchPreference.isChecked();
142         }
143
144         @Override
145         public void setEnabled(boolean enabled) {
146             mSwitchPreference.setEnabled(enabled);
147         }
148
149         @Override
150         public boolean onPreferenceChange(Preference preference, Object newValue) {
151             if (mListener != null) {
152                 return mListener.onSwitchToggled((Boolean) newValue);
153             }
154             return false;
155         }
156
157         @Override
158         public void setDisabledByAdmin(RestrictedLockUtils.EnforcedAdmin admin) {
159             mBtPreference.setEnabled(admin == null);
160         }
161     }
162 }