| Jyun LuoLai | 41e02a9 | 2018-03-27 11:05:56 +0800 | [diff] [blame] | 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; |
| Fan Zhang | de11704 | 2018-09-04 13:52:15 -0700 | [diff] [blame^] | 24 | |
| Aurimas Liutikas | 03fcde3 | 2018-04-17 11:22:43 -0700 | [diff] [blame] | 25 | import androidx.preference.Preference; |
| 26 | import androidx.preference.PreferenceScreen; |
| Jyun LuoLai | 41e02a9 | 2018-03-27 11:05:56 +0800 | [diff] [blame] | 27 | |
| Jyun LuoLai | 41e02a9 | 2018-03-27 11:05:56 +0800 | [diff] [blame] | 28 | import com.android.settings.R; |
| Fan Zhang | de11704 | 2018-09-04 13:52:15 -0700 | [diff] [blame^] | 29 | import com.android.settings.core.BasePreferenceController; |
| Jyun LuoLai | 41e02a9 | 2018-03-27 11:05:56 +0800 | [diff] [blame] | 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 | /** |
| Aurimas Liutikas | 03fcde3 | 2018-04-17 11:22:43 -0700 | [diff] [blame] | 35 | * Controller to maintain the {@link androidx.preference.Preference} for add |
| Jyun LuoLai | 41e02a9 | 2018-03-27 11:05:56 +0800 | [diff] [blame] | 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 | |
| Jyun LuoLai | 41e02a9 | 2018-03-27 11:05:56 +0800 | [diff] [blame] | 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 | |
| Jyun LuoLai | e44b94c | 2018-03-30 10:01:39 +0800 | [diff] [blame] | 51 | public AddDevicePreferenceController(Context context, String key) { |
| 52 | super(context, key); |
| Jyun LuoLai | 41e02a9 | 2018-03-27 11:05:56 +0800 | [diff] [blame] | 53 | mIntentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); |
| 54 | mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public void onStart() { |
| 59 | mContext.registerReceiver(mReceiver, mIntentFilter); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public void onStop() { |
| 64 | mContext.unregisterReceiver(mReceiver); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void displayPreference(PreferenceScreen screen) { |
| 69 | super.displayPreference(screen); |
| 70 | if (isAvailable()) { |
| 71 | mPreference = (Preference) screen.findPreference(getPreferenceKey()); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public int getAvailabilityStatus() { |
| 77 | return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH) |
| 78 | ? AVAILABLE |
| Matthew Fritze | bd37629 | 2018-05-03 16:46:51 -0700 | [diff] [blame] | 79 | : UNSUPPORTED_ON_DEVICE; |
| Jyun LuoLai | 41e02a9 | 2018-03-27 11:05:56 +0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
| Jyun LuoLai | 41e02a9 | 2018-03-27 11:05:56 +0800 | [diff] [blame] | 83 | public CharSequence getSummary() { |
| 84 | return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled() |
| 85 | ? "" |
| 86 | : mContext.getString(R.string.connected_device_add_device_summary); |
| 87 | } |
| 88 | |
| 89 | void updateState() { |
| 90 | updateState(mPreference); |
| 91 | } |
| 92 | } |