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