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.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.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 BluetoothSwitchPreferenceController(Context context, SwitchWidgetController switchController, FooterPreference footerPreference)49 public BluetoothSwitchPreferenceController(Context context, 50 SwitchWidgetController switchController, 51 FooterPreference footerPreference) { 52 this(context, new RestrictionUtils(), switchController, footerPreference); 53 } 54 55 @VisibleForTesting BluetoothSwitchPreferenceController(Context context, RestrictionUtils restrictionUtils, SwitchWidgetController switchController, FooterPreference footerPreference)56 public BluetoothSwitchPreferenceController(Context context, RestrictionUtils restrictionUtils, 57 SwitchWidgetController switchController, FooterPreference footerPreference) { 58 mRestrictionUtils = restrictionUtils; 59 mSwitch = switchController; 60 mContext = context; 61 mFooterPreference = footerPreference; 62 63 mSwitch.setupView(); 64 updateText(mSwitch.isChecked()); 65 66 mBluetoothEnabler = new BluetoothEnabler(context, 67 switchController, 68 FeatureFactory.getFactory(context).getMetricsFeatureProvider(), 69 SettingsEnums.ACTION_SETTINGS_MASTER_SWITCH_BLUETOOTH_TOGGLE, 70 mRestrictionUtils); 71 mBluetoothEnabler.setToggleCallback(this); 72 } 73 74 @Override onStart()75 public void onStart() { 76 mBluetoothEnabler.resume(mContext); 77 if (mSwitch != null) { 78 updateText(mSwitch.isChecked()); 79 } 80 } 81 82 @Override onStop()83 public void onStop() { 84 mBluetoothEnabler.pause(); 85 } 86 87 @Override onSwitchToggled(boolean isChecked)88 public boolean onSwitchToggled(boolean isChecked) { 89 updateText(isChecked); 90 return true; 91 } 92 93 @Override onClick(View v)94 public void onClick(View v) { 95 // send users to scanning settings if they click on the link in the summary text 96 new SubSettingLauncher(mContext) 97 .setDestination(ScanningSettings.class.getName()) 98 .setSourceMetricsCategory(SettingsEnums.BLUETOOTH_FRAGMENT) 99 .launch(); 100 } 101 updateText(boolean isChecked)102 @VisibleForTesting void updateText(boolean isChecked) { 103 if (!isChecked 104 && Utils.isBluetoothScanningEnabled(mContext)) { 105 AnnotationSpan.LinkInfo info = new AnnotationSpan.LinkInfo( 106 AnnotationSpan.LinkInfo.DEFAULT_ANNOTATION, this); 107 CharSequence text = AnnotationSpan.linkify( 108 mContext.getText(R.string.bluetooth_scanning_on_info_message), info); 109 mFooterPreference.setTitle(text); 110 } else { 111 mFooterPreference.setTitle(R.string.bluetooth_empty_list_bluetooth_off); 112 } 113 } 114 }