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