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