BluetoothTile.java revision ccb6b9a90f228cc4e31a9442ed28756ff474c080
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    }
37
38    @Override
39    public boolean supportsDualTargets() {
40        return true;
41    }
42
43    @Override
44    protected BooleanState newTileState() {
45        return new BooleanState();
46    }
47
48    @Override
49    public void setListening(boolean listening) {
50        if (listening) {
51            mController.addStateChangedCallback(mCallback);
52        } else {
53            mController.removeStateChangedCallback(mCallback);
54        }
55    }
56
57    @Override
58    protected void handleClick() {
59        final boolean isEnabled = (Boolean)mState.value;
60        mController.setBluetoothEnabled(!isEnabled);
61    }
62
63    @Override
64    protected void handleSecondaryClick() {
65        mHost.startSettingsActivity(BLUETOOTH_SETTINGS);
66    }
67
68    @Override
69    protected void handleUpdateState(BooleanState state, Object arg) {
70        final boolean supported = mController.isBluetoothSupported();
71        final boolean enabled = mController.isBluetoothEnabled();
72        final boolean connected = mController.isBluetoothConnected();
73        state.visible = supported;
74        state.value = enabled;
75        state.icon = mHost.getVectorDrawable(R.drawable.ic_qs_bluetooth);
76        final String stateContentDescription;
77        if (enabled) {
78            if (connected) {
79                state.iconId = R.drawable.ic_qs_bluetooth_on;
80                stateContentDescription = mContext.getString(R.string.accessibility_desc_connected);
81            } else {
82                state.iconId = R.drawable.ic_qs_bluetooth_not_connected;
83                stateContentDescription = mContext.getString(R.string.accessibility_desc_on);
84            }
85            state.label = mContext.getString(R.string.quick_settings_bluetooth_label);
86        } else {
87            state.iconId = R.drawable.ic_qs_bluetooth_off;
88            state.label = mContext.getString(R.string.quick_settings_bluetooth_off_label);
89            stateContentDescription = mContext.getString(R.string.accessibility_desc_off);
90        }
91        state.contentDescription = mContext.getString(
92                R.string.accessibility_quick_settings_bluetooth, stateContentDescription);
93    }
94
95    private final BluetoothStateChangeCallback mCallback = new BluetoothStateChangeCallback() {
96        @Override
97        public void onBluetoothStateChange(boolean on) {
98            refreshState();
99        }
100    };
101}
102