BluetoothTile.java revision c17d3298255357ce6e49d889cb4548e33004344c
1/*
2 * Copyright (C) 2014 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
17package com.android.systemui.qs.tiles;
18
19import android.bluetooth.BluetoothDevice;
20import android.bluetooth.BluetoothProfile;
21import android.content.Context;
22import android.content.Intent;
23import android.provider.Settings;
24import android.text.TextUtils;
25import android.view.View;
26import android.view.ViewGroup;
27
28import com.android.internal.logging.MetricsLogger;
29import com.android.internal.logging.MetricsProto.MetricsEvent;
30import com.android.settingslib.bluetooth.CachedBluetoothDevice;
31import com.android.systemui.R;
32import com.android.systemui.qs.QSDetailItems;
33import com.android.systemui.qs.QSDetailItems.Item;
34import com.android.systemui.qs.QSTile;
35import com.android.systemui.statusbar.policy.BluetoothController;
36
37import java.util.ArrayList;
38import java.util.Collection;
39
40/** Quick settings tile: Bluetooth **/
41public class BluetoothTile extends QSTile<QSTile.BooleanState>  {
42    private static final Intent BLUETOOTH_SETTINGS = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS);
43
44    private final BluetoothController mController;
45    private final BluetoothDetailAdapter mDetailAdapter;
46
47    public BluetoothTile(Host host) {
48        super(host);
49        mController = host.getBluetoothController();
50        mDetailAdapter = new BluetoothDetailAdapter();
51    }
52
53    @Override
54    public DetailAdapter getDetailAdapter() {
55        return mDetailAdapter;
56    }
57
58    @Override
59    protected BooleanState newTileState() {
60        return new BooleanState();
61    }
62
63    @Override
64    public void setListening(boolean listening) {
65        if (listening) {
66            mController.addStateChangedCallback(mCallback);
67        } else {
68            mController.removeStateChangedCallback(mCallback);
69        }
70    }
71
72    @Override
73    protected void handleSecondaryClick() {
74        // Secondary clicks are header clicks, just toggle.
75        final boolean isEnabled = (Boolean)mState.value;
76        MetricsLogger.action(mContext, getMetricsCategory(), !isEnabled);
77        mController.setBluetoothEnabled(!isEnabled);
78    }
79
80    @Override
81    protected void handleClick() {
82        if (!mController.canConfigBluetooth()) {
83            mHost.startActivityDismissingKeyguard(new Intent(Settings.ACTION_BLUETOOTH_SETTINGS));
84            return;
85        }
86        if (!mState.value) {
87            mState.value = true;
88            mController.setBluetoothEnabled(true);
89        }
90        showDetail(true);
91    }
92
93    @Override
94    protected void handleUpdateState(BooleanState state, Object arg) {
95        final boolean enabled = mController.isBluetoothEnabled();
96        final boolean connected = mController.isBluetoothConnected();
97        final boolean connecting = mController.isBluetoothConnecting();
98        state.value = enabled;
99        state.autoMirrorDrawable = false;
100        if (enabled) {
101            state.label = null;
102            if (connected) {
103                state.icon = ResourceIcon.get(R.drawable.ic_qs_bluetooth_connected);
104                state.contentDescription = mContext.getString(
105                        R.string.accessibility_quick_settings_bluetooth_connected);
106                state.label = mController.getLastDeviceName();
107            } else if (connecting) {
108                state.icon = ResourceIcon.get(R.drawable.ic_qs_bluetooth_connecting);
109                state.contentDescription = mContext.getString(
110                        R.string.accessibility_quick_settings_bluetooth_connecting);
111                state.label = mContext.getString(R.string.quick_settings_bluetooth_label);
112            } else {
113                state.icon = ResourceIcon.get(R.drawable.ic_qs_bluetooth_on);
114                state.contentDescription = mContext.getString(
115                        R.string.accessibility_quick_settings_bluetooth_on);
116            }
117            if (TextUtils.isEmpty(state.label)) {
118                state.label = mContext.getString(R.string.quick_settings_bluetooth_label);
119            }
120        } else {
121            state.icon = ResourceIcon.get(R.drawable.ic_qs_bluetooth_off);
122            state.label = mContext.getString(R.string.quick_settings_bluetooth_label);
123            state.contentDescription = mContext.getString(
124                    R.string.accessibility_quick_settings_bluetooth_off);
125        }
126
127        CharSequence bluetoothName = state.label;
128        if (connected) {
129            bluetoothName = state.dualLabelContentDescription = mContext.getString(
130                    R.string.accessibility_bluetooth_name, state.label);
131        }
132        state.dualLabelContentDescription = bluetoothName;
133    }
134
135    @Override
136    public int getMetricsCategory() {
137        return MetricsEvent.QS_BLUETOOTH;
138    }
139
140    @Override
141    protected String composeChangeAnnouncement() {
142        if (mState.value) {
143            return mContext.getString(R.string.accessibility_quick_settings_bluetooth_changed_on);
144        } else {
145            return mContext.getString(R.string.accessibility_quick_settings_bluetooth_changed_off);
146        }
147    }
148
149    public static boolean isSupported(Host host) {
150        return host.getBluetoothController().isBluetoothSupported();
151    }
152
153    private final BluetoothController.Callback mCallback = new BluetoothController.Callback() {
154        @Override
155        public void onBluetoothStateChange(boolean enabled) {
156            refreshState();
157        }
158
159        @Override
160        public void onBluetoothDevicesChanged() {
161            mUiHandler.post(new Runnable() {
162                @Override
163                public void run() {
164                    mDetailAdapter.updateItems();
165                }
166            });
167            refreshState();
168        }
169    };
170
171    private final class BluetoothDetailAdapter implements DetailAdapter, QSDetailItems.Callback {
172        private QSDetailItems mItems;
173
174        @Override
175        public CharSequence getTitle() {
176            return mContext.getString(R.string.quick_settings_bluetooth_label);
177        }
178
179        @Override
180        public Boolean getToggleState() {
181            return mState.value;
182        }
183
184        @Override
185        public Intent getSettingsIntent() {
186            return BLUETOOTH_SETTINGS;
187        }
188
189        @Override
190        public void setToggleState(boolean state) {
191            MetricsLogger.action(mContext, MetricsEvent.QS_BLUETOOTH_TOGGLE, state);
192            mController.setBluetoothEnabled(state);
193            showDetail(false);
194        }
195
196        @Override
197        public int getMetricsCategory() {
198            return MetricsEvent.QS_BLUETOOTH_DETAILS;
199        }
200
201        @Override
202        public View createDetailView(Context context, View convertView, ViewGroup parent) {
203            mItems = QSDetailItems.convertOrInflate(context, convertView, parent);
204            mItems.setTagSuffix("Bluetooth");
205            mItems.setEmptyState(R.drawable.ic_qs_bluetooth_detail_empty,
206                    R.string.quick_settings_bluetooth_detail_empty_text);
207            mItems.setCallback(this);
208            mItems.setMinHeightInItems(0);
209            updateItems();
210            setItemsVisible(mState.value);
211            return mItems;
212        }
213
214        public void setItemsVisible(boolean visible) {
215            if (mItems == null) return;
216            mItems.setItemsVisible(visible);
217        }
218
219        private void updateItems() {
220            if (mItems == null) return;
221            ArrayList<Item> items = new ArrayList<Item>();
222            final Collection<CachedBluetoothDevice> devices = mController.getDevices();
223            if (devices != null) {
224                for (CachedBluetoothDevice device : devices) {
225                    if (device.getBondState() == BluetoothDevice.BOND_NONE) continue;
226                    final Item item = new Item();
227                    item.icon = R.drawable.ic_qs_bluetooth_on;
228                    item.line1 = device.getName();
229                    int state = device.getMaxConnectionState();
230                    if (state == BluetoothProfile.STATE_CONNECTED) {
231                        item.icon = R.drawable.ic_qs_bluetooth_connected;
232                        item.line2 = mContext.getString(R.string.quick_settings_connected);
233                        item.canDisconnect = true;
234                    } else if (state == BluetoothProfile.STATE_CONNECTING) {
235                        item.icon = R.drawable.ic_qs_bluetooth_connecting;
236                        item.line2 = mContext.getString(R.string.quick_settings_connecting);
237                    }
238                    item.tag = device;
239                    items.add(item);
240                }
241            }
242            mItems.setItems(items.toArray(new Item[items.size()]));
243        }
244
245        @Override
246        public void onDetailItemClick(Item item) {
247            if (item == null || item.tag == null) return;
248            final CachedBluetoothDevice device = (CachedBluetoothDevice) item.tag;
249            if (device != null && device.getMaxConnectionState()
250                    == BluetoothProfile.STATE_DISCONNECTED) {
251                mController.connect(device);
252            }
253        }
254
255        @Override
256        public void onDetailItemDisconnect(Item item) {
257            if (item == null || item.tag == null) return;
258            final CachedBluetoothDevice device = (CachedBluetoothDevice) item.tag;
259            if (device != null) {
260                mController.disconnect(device);
261            }
262        }
263    }
264}
265