1 /* 2 * Copyright (C) 2018 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.connecteddevice; 17 18 import android.bluetooth.BluetoothAdapter; 19 import android.content.BroadcastReceiver; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.IntentFilter; 23 import android.content.pm.PackageManager; 24 25 import androidx.preference.Preference; 26 import androidx.preference.PreferenceScreen; 27 28 import com.android.settings.R; 29 import com.android.settings.core.BasePreferenceController; 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 34 /** 35 * Controller to maintain the {@link androidx.preference.Preference} for add 36 * device. It monitor Bluetooth's status(on/off) and decide if need 37 * to show summary or not. 38 */ 39 public class AddDevicePreferenceController extends BasePreferenceController 40 implements LifecycleObserver, OnStart, OnStop { 41 42 private Preference mPreference; 43 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 44 @Override 45 public void onReceive(Context context, Intent intent) { 46 updateState(mPreference); 47 } 48 }; 49 private IntentFilter mIntentFilter; 50 private BluetoothAdapter mBluetoothAdapter; 51 AddDevicePreferenceController(Context context, String key)52 public AddDevicePreferenceController(Context context, String key) { 53 super(context, key); 54 mIntentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); 55 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 56 } 57 58 @Override onStart()59 public void onStart() { 60 mContext.registerReceiver(mReceiver, mIntentFilter); 61 } 62 63 @Override onStop()64 public void onStop() { 65 mContext.unregisterReceiver(mReceiver); 66 } 67 68 @Override displayPreference(PreferenceScreen screen)69 public void displayPreference(PreferenceScreen screen) { 70 super.displayPreference(screen); 71 if (isAvailable()) { 72 mPreference = screen.findPreference(getPreferenceKey()); 73 updateState(mPreference); 74 } 75 } 76 77 @Override getAvailabilityStatus()78 public int getAvailabilityStatus() { 79 return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH) 80 ? AVAILABLE 81 : UNSUPPORTED_ON_DEVICE; 82 } 83 84 @Override getSummary()85 public CharSequence getSummary() { 86 return isBluetoothEnabled() 87 ? "" 88 : mContext.getString(R.string.connected_device_add_device_summary); 89 } 90 isBluetoothEnabled()91 protected boolean isBluetoothEnabled() { 92 return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled(); 93 } 94 } 95