BluetoothTile.java revision af8d6c44f06d2f8baac2c5774a9efdae3fc36797
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.BluetoothAdapter.BluetoothStateChangeCallback;
20import android.content.Intent;
21import android.provider.Settings;
22
23import com.android.systemui.R;
24import com.android.systemui.qs.QSTile;
25import com.android.systemui.statusbar.policy.BluetoothController;
26
27/** Quick settings tile: Bluetooth **/
28public class BluetoothTile extends QSTile<QSTile.BooleanState>  {
29    private static final Intent BLUETOOTH_SETTINGS = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS);
30
31    private final BluetoothController mController;
32
33    public BluetoothTile(Host host) {
34        super(host);
35        mController = host.getBluetoothController();
36        mController.addStateChangedCallback(mCallback);
37    }
38
39    @Override
40    protected BooleanState newTileState() {
41        return new BooleanState();
42    }
43
44    @Override
45    public void dispose() {
46        mController.removeStateChangedCallback(mCallback);
47    }
48
49    @Override
50    protected void handleClick() {
51        final boolean isEnabled = (Boolean)mState.value;
52        mController.setBluetoothEnabled(!isEnabled);
53    }
54
55    @Override
56    protected void handleSecondaryClick() {
57        mHost.startSettingsActivity(BLUETOOTH_SETTINGS);
58    }
59
60    @Override
61    protected void handleUpdateState(BooleanState state, Object arg) {
62        final boolean supported = mController.isBluetoothSupported();
63        final boolean enabled = mController.isBluetoothEnabled();
64        final boolean connected = mController.isBluetoothConnected();
65        state.visible = supported;
66        state.value = enabled;
67        state.icon = mHost.getVectorDrawable(R.drawable.ic_qs_bluetooth);
68        final String stateContentDescription;
69        if (enabled) {
70            if (connected) {
71                state.iconId = R.drawable.ic_qs_bluetooth_on;
72                stateContentDescription = mContext.getString(R.string.accessibility_desc_connected);
73            } else {
74                state.iconId = R.drawable.ic_qs_bluetooth_not_connected;
75                stateContentDescription = mContext.getString(R.string.accessibility_desc_on);
76            }
77            state.label = mContext.getString(R.string.quick_settings_bluetooth_label);
78        } else {
79            state.iconId = R.drawable.ic_qs_bluetooth_off;
80            state.label = mContext.getString(R.string.quick_settings_bluetooth_off_label);
81            stateContentDescription = mContext.getString(R.string.accessibility_desc_off);
82        }
83        state.contentDescription = mContext.getString(
84                R.string.accessibility_quick_settings_bluetooth, stateContentDescription);
85    }
86
87    private final BluetoothStateChangeCallback mCallback = new BluetoothStateChangeCallback() {
88        @Override
89        public void onBluetoothStateChange(boolean on) {
90            refreshState();
91        }
92    };
93}
94