blob: 3239dd5b32f30b753074f3d9b1b4e30eb5174ec8 [file] [log] [blame]
Jyun LuoLai41e02a92018-03-27 11:05:56 +08001/*
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 */
16package com.android.settings.connecteddevice;
17
18import android.bluetooth.BluetoothAdapter;
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
23import android.content.pm.PackageManager;
Fan Zhangde117042018-09-04 13:52:15 -070024
Aurimas Liutikas03fcde32018-04-17 11:22:43 -070025import androidx.preference.Preference;
26import androidx.preference.PreferenceScreen;
Jyun LuoLai41e02a92018-03-27 11:05:56 +080027
Jyun LuoLai41e02a92018-03-27 11:05:56 +080028import com.android.settings.R;
Fan Zhangde117042018-09-04 13:52:15 -070029import com.android.settings.core.BasePreferenceController;
Jyun LuoLai41e02a92018-03-27 11:05:56 +080030import com.android.settingslib.core.lifecycle.LifecycleObserver;
31import com.android.settingslib.core.lifecycle.events.OnStart;
32import com.android.settingslib.core.lifecycle.events.OnStop;
33
34/**
Aurimas Liutikas03fcde32018-04-17 11:22:43 -070035 * Controller to maintain the {@link androidx.preference.Preference} for add
Jyun LuoLai41e02a92018-03-27 11:05:56 +080036 * device. It monitor Bluetooth's status(on/off) and decide if need to show summary or not.
37 */
38public class AddDevicePreferenceController extends BasePreferenceController
39 implements LifecycleObserver, OnStart, OnStop {
40
Jyun LuoLai41e02a92018-03-27 11:05:56 +080041 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 LuoLaie44b94c2018-03-30 10:01:39 +080051 public AddDevicePreferenceController(Context context, String key) {
52 super(context, key);
Jyun LuoLai41e02a92018-03-27 11:05:56 +080053 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 Fritzebd376292018-05-03 16:46:51 -070079 : UNSUPPORTED_ON_DEVICE;
Jyun LuoLai41e02a92018-03-27 11:05:56 +080080 }
81
82 @Override
Jyun LuoLai41e02a92018-03-27 11:05:56 +080083 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}