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