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