BluetoothTile.java revision 83e2848c562bf951fd49d85da338eb6dc459cff2
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        final String stateContentDescription;
76        if (enabled) {
77            if (connected) {
78                state.iconId = R.drawable.ic_qs_bluetooth_connected;
79                stateContentDescription = mContext.getString(R.string.accessibility_desc_connected);
80            } else {
81                state.iconId = R.drawable.ic_qs_bluetooth_on;
82                stateContentDescription = mContext.getString(R.string.accessibility_desc_on);
83            }
84            state.label = mContext.getString(R.string.quick_settings_bluetooth_label);
85        } else {
86            state.iconId = R.drawable.ic_qs_bluetooth_off;
87            state.label = mContext.getString(R.string.quick_settings_bluetooth_off_label);
88            stateContentDescription = mContext.getString(R.string.accessibility_desc_off);
89        }
90        state.contentDescription = mContext.getString(
91                R.string.accessibility_quick_settings_bluetooth, stateContentDescription);
92    }
93
94    private final BluetoothStateChangeCallback mCallback = new BluetoothStateChangeCallback() {
95        @Override
96        public void onBluetoothStateChange(boolean on) {
97            refreshState();
98        }
99    };
100}
101