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 to show summary or not. 37 */ 38 public class AddDevicePreferenceController extends BasePreferenceController 39 implements LifecycleObserver, OnStart, OnStop { 40 41 private Preference mPreference; 42 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 43 @Override 44 public void onReceive(Context context, Intent intent) { 45 updateState(); 46 } 47 }; 48 private IntentFilter mIntentFilter; 49 private BluetoothAdapter mBluetoothAdapter; 50 AddDevicePreferenceController(Context context, String key)51 public AddDevicePreferenceController(Context context, String key) { 52 super(context, key); 53 mIntentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); 54 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 55 } 56 57 @Override onStart()58 public void onStart() { 59 mContext.registerReceiver(mReceiver, mIntentFilter); 60 } 61 62 @Override onStop()63 public void onStop() { 64 mContext.unregisterReceiver(mReceiver); 65 } 66 67 @Override displayPreference(PreferenceScreen screen)68 public void displayPreference(PreferenceScreen screen) { 69 super.displayPreference(screen); 70 if (isAvailable()) { 71 mPreference = screen.findPreference(getPreferenceKey()); 72 } 73 } 74 75 @Override getAvailabilityStatus()76 public int getAvailabilityStatus() { 77 return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH) 78 ? AVAILABLE 79 : UNSUPPORTED_ON_DEVICE; 80 } 81 82 @Override getSummary()83 public CharSequence getSummary() { 84 return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled() 85 ? "" 86 : mContext.getString(R.string.connected_device_add_device_summary); 87 } 88 updateState()89 void updateState() { 90 updateState(mPreference); 91 } 92 } 93