2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.bluetooth;
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;
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.Lifecycle;
36 import com.android.settingslib.core.lifecycle.LifecycleObserver;
37 import com.android.settingslib.core.lifecycle.events.OnStart;
38 import com.android.settingslib.core.lifecycle.events.OnStop;
41 * Controller that shows and updates the bluetooth device name
43 public class BluetoothDeviceNamePreferenceController extends BasePreferenceController implements
44 LifecycleObserver, OnStart, OnStop {
45 private static final String TAG = "BluetoothNamePrefCtrl";
47 public static final String KEY_DEVICE_NAME = "device_name";
51 Preference mPreference;
52 private LocalBluetoothManager mLocalManager;
53 private LocalBluetoothAdapter mLocalAdapter;
55 public BluetoothDeviceNamePreferenceController(Context context, Lifecycle lifecycle) {
56 this(context, (LocalBluetoothAdapter) null);
58 mLocalManager = Utils.getLocalBtManager(context);
59 if (mLocalManager == null) {
60 Log.e(TAG, "Bluetooth is not supported on this device");
63 mLocalAdapter = mLocalManager.getBluetoothAdapter();
65 if (lifecycle != null) {
66 lifecycle.addObserver(this);
71 * Constructor exclusively used for Slice.
73 public BluetoothDeviceNamePreferenceController(Context context) {
74 this(context, (Lifecycle) null);
78 BluetoothDeviceNamePreferenceController(Context context, LocalBluetoothAdapter localAdapter) {
79 super(context, KEY_DEVICE_NAME);
80 mLocalAdapter = localAdapter;
84 public void displayPreference(PreferenceScreen screen) {
85 mPreference = screen.findPreference(getPreferenceKey());
86 super.displayPreference(screen);
90 public void onStart() {
91 mContext.registerReceiver(mReceiver,
92 new IntentFilter(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED));
96 public void onStop() {
97 mContext.unregisterReceiver(mReceiver);
101 public int getAvailabilityStatus() {
102 return mLocalAdapter != null ? AVAILABLE : DISABLED_UNSUPPORTED;
106 public String getPreferenceKey() {
107 return KEY_DEVICE_NAME;
111 public void updateState(Preference preference) {
112 updateDeviceName(preference);
116 public String getSummary() {
117 String deviceName = getDeviceName();
118 if (TextUtils.isEmpty(deviceName)) {
119 return super.getSummary();
122 return TextUtils.expandTemplate(
123 mContext.getText(R.string.bluetooth_device_name_summary),
124 BidiFormatter.getInstance().unicodeWrap(deviceName)).toString();
128 * Create preference to show bluetooth device name
130 * @param screen to add the preference in
131 * @param order to decide position of the preference
132 * @return bluetooth preference that created in this method
134 public Preference createBluetoothDeviceNamePreference(PreferenceScreen screen, int order) {
135 mPreference = new Preference(screen.getContext());
136 mPreference.setOrder(order);
137 mPreference.setKey(KEY_DEVICE_NAME);
138 screen.addPreference(mPreference);
144 * Update device summary with {@code deviceName}, where {@code deviceName} has accent color
146 * @param preference to set the summary for
148 protected void updateDeviceName(final Preference preference) {
149 preference.setSelectable(false);
150 preference.setSummary(getSummary());
153 protected String getDeviceName() {
154 return mLocalAdapter.getName();
158 * Receiver that listens to {@link BluetoothAdapter#ACTION_LOCAL_NAME_CHANGED} and updates the
159 * device name if possible
162 final BroadcastReceiver mReceiver = new BroadcastReceiver() {
164 public void onReceive(Context context, Intent intent) {
165 final String action = intent.getAction();
167 if (TextUtils.equals(action, BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED)) {
168 if (mPreference != null && mLocalAdapter != null && mLocalAdapter.isEnabled()) {
169 updateDeviceName(mPreference);