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.v7.preference.PreferenceScreen; 20 21 import com.android.internal.annotations.VisibleForTesting; 22 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 23 import com.android.settings.core.PreferenceController; 24 import com.android.settings.core.lifecycle.LifecycleObserver; 25 import com.android.settings.core.lifecycle.events.OnPause; 26 import com.android.settings.core.lifecycle.events.OnResume; 27 import com.android.settings.core.lifecycle.events.OnStart; 28 import com.android.settings.core.lifecycle.events.OnStop; 29 import com.android.settings.overlay.FeatureFactory; 30 import com.android.settings.widget.MasterSwitchController; 31 import com.android.settings.widget.MasterSwitchPreference; 32 import com.android.settings.widget.SummaryUpdater.OnSummaryChangeListener; 33 import com.android.settingslib.bluetooth.LocalBluetoothManager; 34 35 public class BluetoothMasterSwitchPreferenceController extends PreferenceController 36 implements OnSummaryChangeListener, 37 LifecycleObserver, OnResume, OnPause, OnStart, OnStop { 38 39 public static final String KEY_TOGGLE_BLUETOOTH = "toggle_bluetooth"; 40 41 private LocalBluetoothManager mBluetoothManager; 42 private MasterSwitchPreference mBtPreference; 43 private BluetoothEnabler mBluetoothEnabler; 44 private BluetoothSummaryUpdater mSummaryUpdater; 45 private RestrictionUtils mRestrictionUtils; 46 BluetoothMasterSwitchPreferenceController(Context context, LocalBluetoothManager bluetoothManager)47 public BluetoothMasterSwitchPreferenceController(Context context, 48 LocalBluetoothManager bluetoothManager) { 49 this(context, bluetoothManager, new RestrictionUtils()); 50 } 51 52 @VisibleForTesting BluetoothMasterSwitchPreferenceController(Context context, LocalBluetoothManager bluetoothManager, RestrictionUtils restrictionUtils)53 public BluetoothMasterSwitchPreferenceController(Context context, 54 LocalBluetoothManager bluetoothManager, RestrictionUtils restrictionUtils) { 55 super(context); 56 mBluetoothManager = bluetoothManager; 57 mSummaryUpdater = new BluetoothSummaryUpdater(mContext, this, mBluetoothManager); 58 mRestrictionUtils = restrictionUtils; 59 } 60 61 @Override displayPreference(PreferenceScreen screen)62 public void displayPreference(PreferenceScreen screen) { 63 super.displayPreference(screen); 64 mBtPreference = (MasterSwitchPreference) screen.findPreference(KEY_TOGGLE_BLUETOOTH); 65 mBluetoothEnabler = new BluetoothEnabler(mContext, 66 new MasterSwitchController(mBtPreference), 67 FeatureFactory.getFactory(mContext).getMetricsFeatureProvider(), mBluetoothManager, 68 MetricsEvent.ACTION_SETTINGS_MASTER_SWITCH_BLUETOOTH_TOGGLE, 69 mRestrictionUtils); 70 } 71 72 @Override isAvailable()73 public boolean isAvailable() { 74 return true; 75 } 76 77 @Override getPreferenceKey()78 public String getPreferenceKey() { 79 return KEY_TOGGLE_BLUETOOTH; 80 } 81 onResume()82 public void onResume() { 83 mSummaryUpdater.register(true); 84 } 85 86 @Override onPause()87 public void onPause() { 88 mSummaryUpdater.register(false); 89 } 90 91 @Override onStart()92 public void onStart() { 93 if (mBluetoothEnabler != null) { 94 mBluetoothEnabler.resume(mContext); 95 } 96 } 97 98 @Override onStop()99 public void onStop() { 100 if (mBluetoothEnabler != null) { 101 mBluetoothEnabler.pause(); 102 } 103 } 104 105 @Override onSummaryChanged(String summary)106 public void onSummaryChanged(String summary) { 107 if (mBtPreference != null) { 108 mBtPreference.setSummary(summary); 109 } 110 } 111 112 } 113