HotspotTile.java revision 4fda7b2d97c77c64bac0b44dc7a44597ad2c48b0
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.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22
23import com.android.systemui.R;
24import com.android.systemui.qs.UsageTracker;
25import com.android.systemui.qs.QSTile;
26import com.android.systemui.statusbar.policy.HotspotController;
27
28/** Quick settings tile: Hotspot **/
29public class HotspotTile extends QSTile<QSTile.BooleanState> {
30    private final HotspotController mController;
31    private final Callback mCallback = new Callback();
32    private final UsageTracker mUsageTracker;
33
34    public HotspotTile(Host host) {
35        super(host);
36        mController = host.getHotspotController();
37        mUsageTracker = new UsageTracker(host.getContext(), HotspotTile.class);
38        mUsageTracker.setListening(true);
39    }
40
41    @Override
42    protected void handleDestroy() {
43        super.handleDestroy();
44        mUsageTracker.setListening(false);
45    }
46
47    @Override
48    protected BooleanState newTileState() {
49        return new BooleanState();
50    }
51
52    @Override
53    public void setListening(boolean listening) {
54        if (listening) {
55            mController.addCallback(mCallback);
56        } else {
57            mController.removeCallback(mCallback);
58        }
59    }
60
61    @Override
62    protected void handleClick() {
63        final boolean isEnabled = (Boolean) mState.value;
64        mController.setHotspotEnabled(!isEnabled);
65    }
66
67    @Override
68    protected void handleUpdateState(BooleanState state, Object arg) {
69        state.visible = mController.isHotspotSupported() && mUsageTracker.isRecentlyUsed();
70        state.label = mContext.getString(R.string.quick_settings_hotspot_label);
71
72        state.value = mController.isHotspotEnabled();
73        state.iconId = state.visible && state.value ? R.drawable.ic_qs_hotspot_on
74                : R.drawable.ic_qs_hotspot_off;
75    }
76
77    @Override
78    protected String composeChangeAnnouncement() {
79        if (mState.value) {
80            return mContext.getString(R.string.accessibility_quick_settings_hotspot_changed_on);
81        } else {
82            return mContext.getString(R.string.accessibility_quick_settings_hotspot_changed_off);
83        }
84    }
85
86    private final class Callback implements HotspotController.Callback {
87        @Override
88        public void onHotspotChanged(boolean enabled) {
89            refreshState();
90        }
91    };
92
93    /**
94     * This will catch broadcasts for changes in hotspot state so we can show
95     * the hotspot tile for a number of days after use.
96     */
97    public static class APChangedReceiver extends BroadcastReceiver {
98        private UsageTracker mUsageTracker;
99
100        @Override
101        public void onReceive(Context context, Intent intent) {
102            if (mUsageTracker == null) {
103                mUsageTracker = new UsageTracker(context, HotspotTile.class);
104            }
105            mUsageTracker.trackUsage();
106        }
107    }
108}
109