• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 
17 package com.android.settings.bluetooth;
18 
19 import android.bluetooth.BluetoothAdapter;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.text.BidiFormatter;
25 import android.text.TextUtils;
26 import android.util.Log;
27 
28 import androidx.annotation.VisibleForTesting;
29 import androidx.preference.Preference;
30 import androidx.preference.PreferenceScreen;
31 
32 import com.android.settings.R;
33 import com.android.settings.core.BasePreferenceController;
34 import com.android.settingslib.core.lifecycle.LifecycleObserver;
35 import com.android.settingslib.core.lifecycle.events.OnStart;
36 import com.android.settingslib.core.lifecycle.events.OnStop;
37 
38 /**
39  * Controller that shows and updates the bluetooth device name
40  */
41 public class BluetoothDeviceNamePreferenceController extends BasePreferenceController implements
42         LifecycleObserver, OnStart, OnStop {
43     private static final String TAG = "BluetoothNamePrefCtrl";
44 
45     @VisibleForTesting
46     Preference mPreference;
47     protected BluetoothAdapter mBluetoothAdapter;
48 
49     /**
50      * Constructor exclusively used for Slice.
51      */
BluetoothDeviceNamePreferenceController(Context context, String preferenceKey)52     public BluetoothDeviceNamePreferenceController(Context context, String preferenceKey) {
53         super(context, preferenceKey);
54 
55         mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
56         if (mBluetoothAdapter == null) {
57             Log.e(TAG, "Bluetooth is not supported on this device");
58             return;
59         }
60     }
61 
62     @Override
displayPreference(PreferenceScreen screen)63     public void displayPreference(PreferenceScreen screen) {
64         mPreference = screen.findPreference(getPreferenceKey());
65         super.displayPreference(screen);
66     }
67 
68     @Override
onStart()69     public void onStart() {
70         final IntentFilter intentFilter = new IntentFilter();
71         intentFilter.addAction(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED);
72         intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
73         mContext.registerReceiver(mReceiver, intentFilter);
74     }
75 
76     @Override
onStop()77     public void onStop() {
78         mContext.unregisterReceiver(mReceiver);
79     }
80 
81     @Override
getAvailabilityStatus()82     public int getAvailabilityStatus() {
83         return mBluetoothAdapter != null ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
84     }
85 
86     @Override
updateState(Preference preference)87     public void updateState(Preference preference) {
88         updatePreferenceState(preference);
89     }
90 
91     @Override
getSummary()92     public CharSequence getSummary() {
93         String deviceName = getDeviceName();
94         if (TextUtils.isEmpty(deviceName)) {
95             return super.getSummary();
96         }
97 
98         return TextUtils.expandTemplate(
99                 mContext.getText(R.string.bluetooth_device_name_summary),
100                 BidiFormatter.getInstance().unicodeWrap(deviceName)).toString();
101     }
102 
103     /**
104      * Create preference to show bluetooth device name
105      *
106      * @param screen to add the preference in
107      * @param order  to decide position of the preference
108      * @return bluetooth preference that created in this method
109      */
createBluetoothDeviceNamePreference(PreferenceScreen screen, int order)110     public Preference createBluetoothDeviceNamePreference(PreferenceScreen screen, int order) {
111         mPreference = new Preference(screen.getContext());
112         mPreference.setOrder(order);
113         mPreference.setKey(getPreferenceKey());
114         screen.addPreference(mPreference);
115 
116         return mPreference;
117     }
118 
119     /**
120      * Update device summary with {@code deviceName}, where {@code deviceName} has accent color
121      *
122      * @param preference to set the summary for
123      */
updatePreferenceState(final Preference preference)124     protected void updatePreferenceState(final Preference preference) {
125         preference.setSelectable(false);
126         preference.setSummary(getSummary());
127     }
128 
getDeviceName()129     protected String getDeviceName() {
130         return mBluetoothAdapter.getName();
131     }
132 
133     /**
134      * Receiver that listens to {@link BluetoothAdapter#ACTION_LOCAL_NAME_CHANGED} and updates the
135      * device name if possible
136      */
137     @VisibleForTesting
138     final BroadcastReceiver mReceiver = new BroadcastReceiver() {
139         @Override
140         public void onReceive(Context context, Intent intent) {
141             final String action = intent.getAction();
142 
143             if (TextUtils.equals(action, BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED)) {
144                 if (mPreference != null && mBluetoothAdapter != null
145                         && mBluetoothAdapter.isEnabled()) {
146                     updatePreferenceState(mPreference);
147                 }
148             } else if (TextUtils.equals(action, BluetoothAdapter.ACTION_STATE_CHANGED)) {
149                 updatePreferenceState(mPreference);
150             }
151         }
152     };
153 }
154