HotspotTile.java revision 63224c39543688fd7eea62fe0238d1b32daf5d30
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 AnimationIcon mEnable =
31            new AnimationIcon(R.drawable.ic_hotspot_enable_animation);
32    private final AnimationIcon mDisable =
33            new AnimationIcon(R.drawable.ic_hotspot_disable_animation);
34    private final HotspotController mController;
35    private final Callback mCallback = new Callback();
36    private final UsageTracker mUsageTracker;
37
38    public HotspotTile(Host host) {
39        super(host);
40        mController = host.getHotspotController();
41        mUsageTracker = newUsageTracker(host.getContext());
42        mUsageTracker.setListening(true);
43    }
44
45    @Override
46    protected void handleDestroy() {
47        super.handleDestroy();
48        mUsageTracker.setListening(false);
49    }
50
51    @Override
52    protected BooleanState newTileState() {
53        return new BooleanState();
54    }
55
56    @Override
57    public void setListening(boolean listening) {
58        if (listening) {
59            mController.addCallback(mCallback);
60        } else {
61            mController.removeCallback(mCallback);
62        }
63    }
64
65    @Override
66    protected void handleClick() {
67        final boolean isEnabled = (Boolean) mState.value;
68        mController.setHotspotEnabled(!isEnabled);
69        mEnable.setAllowAnimation(true);
70        mDisable.setAllowAnimation(true);
71    }
72
73    @Override
74    protected void handleLongClick() {
75        if (mState.value) return;  // don't allow usage reset if hotspot is active
76        final String title = mContext.getString(R.string.quick_settings_reset_confirmation_title,
77                mState.label);
78        mUsageTracker.showResetConfirmation(title, new Runnable() {
79            @Override
80            public void run() {
81                refreshState();
82            }
83        });
84    }
85
86    @Override
87    protected void handleUpdateState(BooleanState state, Object arg) {
88        state.visible = mController.isHotspotSupported() && mUsageTracker.isRecentlyUsed();
89        state.label = mContext.getString(R.string.quick_settings_hotspot_label);
90
91        state.value = mController.isHotspotEnabled();
92        state.icon = state.visible && state.value ? mEnable : mDisable;
93    }
94
95    @Override
96    protected String composeChangeAnnouncement() {
97        if (mState.value) {
98            return mContext.getString(R.string.accessibility_quick_settings_hotspot_changed_on);
99        } else {
100            return mContext.getString(R.string.accessibility_quick_settings_hotspot_changed_off);
101        }
102    }
103
104    private static UsageTracker newUsageTracker(Context context) {
105        return new UsageTracker(context, HotspotTile.class, R.integer.days_to_show_hotspot_tile);
106    }
107
108    private final class Callback implements HotspotController.Callback {
109        @Override
110        public void onHotspotChanged(boolean enabled) {
111            refreshState();
112        }
113    };
114
115    /**
116     * This will catch broadcasts for changes in hotspot state so we can show
117     * the hotspot tile for a number of days after use.
118     */
119    public static class APChangedReceiver extends BroadcastReceiver {
120        private UsageTracker mUsageTracker;
121
122        @Override
123        public void onReceive(Context context, Intent intent) {
124            if (mUsageTracker == null) {
125                mUsageTracker = newUsageTracker(context);
126            }
127            mUsageTracker.trackUsage();
128        }
129    }
130}
131