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 Context.RECEIVER_EXPORTED_UNAUDITED); 62 } 63 64 @Override onStop()65 public void onStop() { 66 mContext.unregisterReceiver(mReceiver); 67 } 68 69 @Override displayPreference(PreferenceScreen screen)70 public void displayPreference(PreferenceScreen screen) { 71 super.displayPreference(screen); 72 if (isAvailable()) { 73 mPreference = screen.findPreference(getPreferenceKey()); 74 updateState(mPreference); 75 } 76 } 77 78 @Override getAvailabilityStatus()79 public int getAvailabilityStatus() { 80 return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH) 81 ? AVAILABLE 82 : UNSUPPORTED_ON_DEVICE; 83 } 84 85 @Override getSummary()86 public CharSequence getSummary() { 87 return isBluetoothEnabled() 88 ? "" 89 : mContext.getString(R.string.connected_device_add_device_summary); 90 } 91 isBluetoothEnabled()92 protected boolean isBluetoothEnabled() { 93 return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled(); 94 } 95 } 96