Home | History | Annotate | Download | only in tiles
      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 
     17 package com.android.systemui.qs.tiles;
     18 
     19 import android.app.ActivityManager;
     20 import android.os.SystemClock;
     21 
     22 import com.android.systemui.R;
     23 import com.android.systemui.qs.QSTile;
     24 import com.android.systemui.statusbar.policy.FlashlightController;
     25 
     26 /** Quick settings tile: Control flashlight **/
     27 public 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         int onOrOffId = state.value
     98                 ? R.string.accessibility_quick_settings_flashlight_on
     99                 : R.string.accessibility_quick_settings_flashlight_off;
    100         state.contentDescription = mContext.getString(onOrOffId);
    101     }
    102 
    103     @Override
    104     protected String composeChangeAnnouncement() {
    105         if (mState.value) {
    106             return mContext.getString(R.string.accessibility_quick_settings_flashlight_changed_on);
    107         } else {
    108             return mContext.getString(R.string.accessibility_quick_settings_flashlight_changed_off);
    109         }
    110     }
    111 
    112     @Override
    113     public void onFlashlightOff() {
    114         refreshState(false);
    115     }
    116 
    117     @Override
    118     public void onFlashlightError() {
    119         refreshState(false);
    120     }
    121 
    122     @Override
    123     public void onFlashlightAvailabilityChanged(boolean available) {
    124         refreshState();
    125     }
    126 
    127     private Runnable mRecentlyOnTimeout = new Runnable() {
    128         @Override
    129         public void run() {
    130             refreshState();
    131         }
    132     };
    133 }
    134