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.view.View;
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;
38 * PreferenceController to update of bluetooth state. All behavior except managing the footer text
39 * is delegated to the SwitchWidgetController it uses.
41 public class BluetoothSwitchPreferenceController
42 implements LifecycleObserver, OnStart, OnStop,
43 SwitchWidgetController.OnSwitchChangeListener, View.OnClickListener {
46 LocalBluetoothAdapter mBluetoothAdapter;
48 private LocalBluetoothManager mBluetoothManager;
49 private BluetoothEnabler mBluetoothEnabler;
50 private RestrictionUtils mRestrictionUtils;
51 private SwitchWidgetController mSwitch;
52 private Context mContext;
53 private FooterPreference mFooterPreference;
55 public BluetoothSwitchPreferenceController(Context context,
56 SwitchWidgetController switchController,
57 FooterPreference footerPreference) {
58 this(context, Utils.getLocalBtManager(context), new RestrictionUtils(), switchController,
63 public BluetoothSwitchPreferenceController(Context context,
64 LocalBluetoothManager bluetoothManager, RestrictionUtils restrictionUtils,
65 SwitchWidgetController switchController, FooterPreference footerPreference) {
66 mBluetoothManager = bluetoothManager;
67 mRestrictionUtils = restrictionUtils;
68 mSwitch = switchController;
70 mFooterPreference = footerPreference;
73 updateText(mSwitch.isChecked());
75 if (mBluetoothManager != null) {
76 mBluetoothAdapter = mBluetoothManager.getBluetoothAdapter();
78 mBluetoothEnabler = new BluetoothEnabler(context,
80 FeatureFactory.getFactory(context).getMetricsFeatureProvider(), mBluetoothManager,
81 MetricsEvent.ACTION_SETTINGS_MASTER_SWITCH_BLUETOOTH_TOGGLE,
83 mBluetoothEnabler.setToggleCallback(this);
87 public void onStart() {
88 mBluetoothEnabler.resume(mContext);
89 if (mSwitch != null) {
90 updateText(mSwitch.isChecked());
95 public void onStop() {
96 mBluetoothEnabler.pause();
100 public boolean onSwitchToggled(boolean isChecked) {
101 updateText(isChecked);
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)
114 @VisibleForTesting void updateText(boolean 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);
123 mFooterPreference.setTitle(R.string.bluetooth_empty_list_bluetooth_off);