FlashlightTile.java revision bceed060f0090a4f86418c4515128d5ec8ebdd4a
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 android.os.SystemClock;
21
22import com.android.systemui.R;
23import com.android.systemui.qs.QSTile;
24import com.android.systemui.statusbar.policy.FlashlightController;
25
26/** Quick settings tile: Control flashlight **/
27public class FlashlightTile extends QSTile<QSTile.BooleanState> implements
28        FlashlightController.FlashlightListener {
29
30    /** Grace period for which we consider the flashlight
31     * still available because it was recently on. */
32    private static final long RECENTLY_ON_DURATION_MILLIS = 500;
33
34    private final FlashlightController mFlashlightController;
35    private long mWasLastOn;
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        boolean newState = !mState.value;
68        mFlashlightController.setFlashlight(newState);
69        refreshState(newState);
70    }
71
72    @Override
73    protected void handleUpdateState(BooleanState state, Object arg) {
74        if (state.value) {
75            mWasLastOn = SystemClock.uptimeMillis();
76        }
77
78        if (arg instanceof Boolean) {
79            state.value = (Boolean) arg;
80        }
81
82        if (!state.value && mWasLastOn != 0) {
83            if (SystemClock.uptimeMillis() > mWasLastOn + RECENTLY_ON_DURATION_MILLIS) {
84                mWasLastOn = 0;
85            } else {
86                mHandler.removeCallbacks(mRecentlyOnTimeout);
87                mHandler.postAtTime(mRecentlyOnTimeout, mWasLastOn + RECENTLY_ON_DURATION_MILLIS);
88            }
89        }
90
91        // Always show the tile when the flashlight is or was recently on. This is needed because
92        // the camera is not available while it is being used for the flashlight.
93        state.visible = mWasLastOn != 0 || mFlashlightController.isAvailable();
94        state.label = mHost.getContext().getString(R.string.quick_settings_flashlight_label);
95        state.iconId = state.value
96                ? R.drawable.ic_qs_flashlight_on : R.drawable.ic_qs_flashlight_off;
97    }
98
99    @Override
100    public void onFlashlightOff() {
101        refreshState(false);
102    }
103
104    @Override
105    public void onFlashlightError() {
106        refreshState(false);
107    }
108
109    @Override
110    public void onFlashlightAvailabilityChanged(boolean available) {
111        refreshState();
112    }
113
114    private Runnable mRecentlyOnTimeout = new Runnable() {
115        @Override
116        public void run() {
117            refreshState();
118        }
119    };
120}
121