OSDN Git Service

Prevent overlay drawing on top of Bluetooth discovery request dialog
[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.view.View;
20
21 import com.android.internal.annotations.VisibleForTesting;
22 import com.android.internal.logging.nano.MetricsProto;
23 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
24 import com.android.settings.R;
25 import com.android.settings.core.SubSettingLauncher;
26 import com.android.settings.location.ScanningSettings;
27 import com.android.settings.overlay.FeatureFactory;
28 import com.android.settings.utils.AnnotationSpan;
29 import com.android.settings.widget.SwitchWidgetController;
30 import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
31 import com.android.settingslib.bluetooth.LocalBluetoothManager;
32 import com.android.settingslib.core.lifecycle.LifecycleObserver;
33 import com.android.settingslib.core.lifecycle.events.OnStart;
34 import com.android.settingslib.core.lifecycle.events.OnStop;
35 import com.android.settingslib.widget.FooterPreference;
36
37 /**
38  * PreferenceController to update of bluetooth state. All behavior except managing the footer text
39  * is delegated to the SwitchWidgetController it uses.
40  */
41 public class BluetoothSwitchPreferenceController
42         implements LifecycleObserver, OnStart, OnStop,
43         SwitchWidgetController.OnSwitchChangeListener, View.OnClickListener {
44
45     @VisibleForTesting
46     LocalBluetoothAdapter mBluetoothAdapter;
47
48     private LocalBluetoothManager mBluetoothManager;
49     private BluetoothEnabler mBluetoothEnabler;
50     private RestrictionUtils mRestrictionUtils;
51     private SwitchWidgetController mSwitch;
52     private Context mContext;
53     private FooterPreference mFooterPreference;
54
55     public BluetoothSwitchPreferenceController(Context context,
56             SwitchWidgetController switchController,
57             FooterPreference footerPreference) {
58         this(context, Utils.getLocalBtManager(context), new RestrictionUtils(), switchController,
59                 footerPreference);
60     }
61
62     @VisibleForTesting
63     public BluetoothSwitchPreferenceController(Context context,
64             LocalBluetoothManager bluetoothManager, RestrictionUtils restrictionUtils,
65             SwitchWidgetController switchController, FooterPreference footerPreference) {
66         mBluetoothManager = bluetoothManager;
67         mRestrictionUtils = restrictionUtils;
68         mSwitch = switchController;
69         mContext = context;
70         mFooterPreference = footerPreference;
71
72         mSwitch.setupView();
73         updateText(mSwitch.isChecked());
74
75         if (mBluetoothManager != null) {
76             mBluetoothAdapter = mBluetoothManager.getBluetoothAdapter();
77         }
78         mBluetoothEnabler = new BluetoothEnabler(context,
79                 switchController,
80                 FeatureFactory.getFactory(context).getMetricsFeatureProvider(), mBluetoothManager,
81                 MetricsEvent.ACTION_SETTINGS_MASTER_SWITCH_BLUETOOTH_TOGGLE,
82                 mRestrictionUtils);
83         mBluetoothEnabler.setToggleCallback(this);
84     }
85
86     @Override
87     public void onStart() {
88         mBluetoothEnabler.resume(mContext);
89         if (mSwitch != null) {
90             updateText(mSwitch.isChecked());
91         }
92     }
93
94     @Override
95     public void onStop() {
96         mBluetoothEnabler.pause();
97     }
98
99     @Override
100     public boolean onSwitchToggled(boolean isChecked) {
101         updateText(isChecked);
102         return true;
103     }
104
105     @Override
106     public void onClick(View v) {
107         // send users to scanning settings if they click on the link in the summary text
108         new SubSettingLauncher(mContext)
109                 .setDestination(ScanningSettings.class.getName())
110                 .setSourceMetricsCategory(MetricsProto.MetricsEvent.BLUETOOTH_FRAGMENT)
111                 .launch();
112     }
113
114     @VisibleForTesting void updateText(boolean isChecked) {
115         if (!isChecked
116                 && Utils.isBluetoothScanningEnabled(mContext)) {
117             AnnotationSpan.LinkInfo info = new AnnotationSpan.LinkInfo(
118                     AnnotationSpan.LinkInfo.DEFAULT_ANNOTATION, this);
119             CharSequence text = AnnotationSpan.linkify(
120                     mContext.getText(R.string.bluetooth_scanning_on_info_message), info);
121             mFooterPreference.setTitle(text);
122         } else {
123             mFooterPreference.setTitle(R.string.bluetooth_empty_list_bluetooth_off);
124         }
125     }
126 }