OSDN Git Service

Merge "Merge sub pages for lock/unlock into 1 xml."
[android-x86/packages-apps-Settings.git] / src / com / android / settings / bluetooth / BluetoothDeviceNamePreferenceController.java
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.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;
39
40 /**
41  * Controller that shows and updates the bluetooth device name
42  */
43 public class BluetoothDeviceNamePreferenceController extends BasePreferenceController implements
44         LifecycleObserver, OnStart, OnStop {
45     private static final String TAG = "BluetoothNamePrefCtrl";
46
47     public static final String KEY_DEVICE_NAME = "device_name";
48
49
50     @VisibleForTesting
51     Preference mPreference;
52     private LocalBluetoothManager mLocalManager;
53     private LocalBluetoothAdapter mLocalAdapter;
54
55     public BluetoothDeviceNamePreferenceController(Context context, Lifecycle lifecycle) {
56         this(context, (LocalBluetoothAdapter) null);
57
58         mLocalManager = Utils.getLocalBtManager(context);
59         if (mLocalManager == null) {
60             Log.e(TAG, "Bluetooth is not supported on this device");
61             return;
62         }
63         mLocalAdapter = mLocalManager.getBluetoothAdapter();
64
65         if (lifecycle != null) {
66             lifecycle.addObserver(this);
67         }
68     }
69
70     /**
71      * Constructor exclusively used for Slice.
72      */
73     public BluetoothDeviceNamePreferenceController(Context context) {
74         this(context, (Lifecycle) null);
75     }
76
77     @VisibleForTesting
78     BluetoothDeviceNamePreferenceController(Context context, LocalBluetoothAdapter localAdapter) {
79         super(context, KEY_DEVICE_NAME);
80         mLocalAdapter = localAdapter;
81     }
82
83     @Override
84     public void displayPreference(PreferenceScreen screen) {
85         mPreference = screen.findPreference(getPreferenceKey());
86         super.displayPreference(screen);
87     }
88
89     @Override
90     public void onStart() {
91         mContext.registerReceiver(mReceiver,
92                 new IntentFilter(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED));
93     }
94
95     @Override
96     public void onStop() {
97         mContext.unregisterReceiver(mReceiver);
98     }
99
100     @Override
101     public int getAvailabilityStatus() {
102         return mLocalAdapter != null ? AVAILABLE : DISABLED_UNSUPPORTED;
103     }
104
105     @Override
106     public String getPreferenceKey() {
107         return KEY_DEVICE_NAME;
108     }
109
110     @Override
111     public void updateState(Preference preference) {
112         updateDeviceName(preference);
113     }
114
115     @Override
116     public String getSummary() {
117         String deviceName = getDeviceName();
118         if (TextUtils.isEmpty(deviceName)) {
119             return super.getSummary();
120         }
121
122         return TextUtils.expandTemplate(
123                 mContext.getText(R.string.bluetooth_device_name_summary),
124                 BidiFormatter.getInstance().unicodeWrap(deviceName)).toString();
125     }
126
127     /**
128      * Create preference to show bluetooth device name
129      *
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
133      */
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);
139
140         return mPreference;
141     }
142
143     /**
144      * Update device summary with {@code deviceName}, where {@code deviceName} has accent color
145      *
146      * @param preference to set the summary for
147      */
148     protected void updateDeviceName(final Preference preference) {
149         preference.setSelectable(false);
150         preference.setSummary(getSummary());
151     }
152
153     protected String getDeviceName() {
154         return mLocalAdapter.getName();
155     }
156
157     /**
158      * Receiver that listens to {@link BluetoothAdapter#ACTION_LOCAL_NAME_CHANGED} and updates the
159      * device name if possible
160      */
161     @VisibleForTesting
162     final BroadcastReceiver mReceiver = new BroadcastReceiver() {
163         @Override
164         public void onReceive(Context context, Intent intent) {
165             final String action = intent.getAction();
166
167             if (TextUtils.equals(action, BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED)) {
168                 if (mPreference != null && mLocalAdapter != null && mLocalAdapter.isEnabled()) {
169                     updateDeviceName(mPreference);
170                 }
171             }
172         }
173     };
174 }