FlashlightTile.java revision f6e9228b8a97603d3ceb8f0d61e8d87cf19bd21f
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.app.ActivityManager;
20
21import com.android.internal.logging.MetricsLogger;
22import com.android.internal.logging.MetricsProto.MetricsEvent;
23import com.android.systemui.R;
24import com.android.systemui.qs.QSTile;
25import com.android.systemui.statusbar.policy.FlashlightController;
26
27/** Quick settings tile: Control flashlight **/
28public class FlashlightTile extends QSTile<QSTile.BooleanState> implements
29        FlashlightController.FlashlightListener {
30
31    private final AnimationIcon mEnable
32            = new AnimationIcon(R.drawable.ic_signal_flashlight_enable_animation);
33    private final AnimationIcon mDisable
34            = new AnimationIcon(R.drawable.ic_signal_flashlight_disable_animation);
35    private final FlashlightController mFlashlightController;
36
37    public FlashlightTile(Host host) {
38        super(host);
39        mFlashlightController = host.getFlashlightController();
40        mFlashlightController.addListener(this);
41    }
42
43    @Override
44    protected void handleDestroy() {
45        super.handleDestroy();
46        mFlashlightController.removeListener(this);
47    }
48
49    @Override
50    protected BooleanState newTileState() {
51        return new BooleanState();
52    }
53
54    @Override
55    public void setListening(boolean listening) {
56    }
57
58    @Override
59    protected void handleUserSwitch(int newUserId) {
60    }
61
62    @Override
63    protected void handleClick() {
64        if (ActivityManager.isUserAMonkey()) {
65            return;
66        }
67        MetricsLogger.action(mContext, getMetricsCategory(), !mState.value);
68        boolean newState = !mState.value;
69        refreshState(newState);
70        mFlashlightController.setFlashlight(newState);
71    }
72
73    @Override
74    protected void handleUpdateState(BooleanState state, Object arg) {
75        // TODO: Flashlight available handling...
76//        state.visible = mFlashlightController.isAvailable();
77        state.label = mHost.getContext().getString(R.string.quick_settings_flashlight_label);
78        if (arg instanceof Boolean) {
79            boolean value = (Boolean) arg;
80            if (value == state.value) {
81                return;
82            }
83            state.value = value;
84        } else {
85            state.value = mFlashlightController.isEnabled();
86        }
87        final AnimationIcon icon = state.value ? mEnable : mDisable;
88        state.icon = icon;
89        int onOrOffId = state.value
90                ? R.string.accessibility_quick_settings_flashlight_on
91                : R.string.accessibility_quick_settings_flashlight_off;
92        state.contentDescription = mContext.getString(onOrOffId);
93    }
94
95    @Override
96    public int getMetricsCategory() {
97        return MetricsEvent.QS_FLASHLIGHT;
98    }
99
100    @Override
101    protected String composeChangeAnnouncement() {
102        if (mState.value) {
103            return mContext.getString(R.string.accessibility_quick_settings_flashlight_changed_on);
104        } else {
105            return mContext.getString(R.string.accessibility_quick_settings_flashlight_changed_off);
106        }
107    }
108
109    @Override
110    public void onFlashlightChanged(boolean enabled) {
111        refreshState(enabled);
112    }
113
114    @Override
115    public void onFlashlightError() {
116        refreshState(false);
117    }
118
119    @Override
120    public void onFlashlightAvailabilityChanged(boolean available) {
121        refreshState();
122    }
123}
124