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 AnimationIcon mEnable
     35             = new AnimationIcon(R.drawable.ic_signal_flashlight_enable_animation);
     36     private final AnimationIcon mDisable
     37             = new AnimationIcon(R.drawable.ic_signal_flashlight_disable_animation);
     38     private final FlashlightController mFlashlightController;
     39     private long mWasLastOn;
     40 
     41     public FlashlightTile(Host host) {
     42         super(host);
     43         mFlashlightController = host.getFlashlightController();
     44         mFlashlightController.addListener(this);
     45     }
     46 
     47     @Override
     48     protected void handleDestroy() {
     49         super.handleDestroy();
     50         mFlashlightController.removeListener(this);
     51     }
     52 
     53     @Override
     54     protected BooleanState newTileState() {
     55         return new BooleanState();
     56     }
     57 
     58     @Override
     59     public void setListening(boolean listening) {
     60     }
     61 
     62     @Override
     63     protected void handleUserSwitch(int newUserId) {
     64     }
     65 
     66     @Override
     67     protected void handleClick() {
     68         if (ActivityManager.isUserAMonkey()) {
     69             return;
     70         }
     71         boolean newState = !mState.value;
     72         mFlashlightController.setFlashlight(newState);
     73         refreshState(newState ? UserBoolean.USER_TRUE : UserBoolean.USER_FALSE);
     74     }
     75 
     76     @Override
     77     protected void handleUpdateState(BooleanState state, Object arg) {
     78         if (state.value) {
     79             mWasLastOn = SystemClock.uptimeMillis();
     80         }
     81 
     82         if (arg instanceof UserBoolean) {
     83             state.value = ((UserBoolean) arg).value;
     84         }
     85 
     86         if (!state.value && mWasLastOn != 0) {
     87             if (SystemClock.uptimeMillis() > mWasLastOn + RECENTLY_ON_DURATION_MILLIS) {
     88                 mWasLastOn = 0;
     89             } else {
     90                 mHandler.removeCallbacks(mRecentlyOnTimeout);
     91                 mHandler.postAtTime(mRecentlyOnTimeout, mWasLastOn + RECENTLY_ON_DURATION_MILLIS);
     92             }
     93         }
     94 
     95         // Always show the tile when the flashlight is or was recently on. This is needed because
     96         // the camera is not available while it is being used for the flashlight.
     97         state.visible = mWasLastOn != 0 || mFlashlightController.isAvailable();
     98         state.label = mHost.getContext().getString(R.string.quick_settings_flashlight_label);
     99         final AnimationIcon icon = state.value ? mEnable : mDisable;
    100         icon.setAllowAnimation(arg instanceof UserBoolean && ((UserBoolean) arg).userInitiated);
    101         state.icon = icon;
    102         int onOrOffId = state.value
    103                 ? R.string.accessibility_quick_settings_flashlight_on
    104                 : R.string.accessibility_quick_settings_flashlight_off;
    105         state.contentDescription = mContext.getString(onOrOffId);
    106     }
    107 
    108     @Override
    109     protected String composeChangeAnnouncement() {
    110         if (mState.value) {
    111             return mContext.getString(R.string.accessibility_quick_settings_flashlight_changed_on);
    112         } else {
    113             return mContext.getString(R.string.accessibility_quick_settings_flashlight_changed_off);
    114         }
    115     }
    116 
    117     @Override
    118     public void onFlashlightOff() {
    119         refreshState(UserBoolean.BACKGROUND_FALSE);
    120     }
    121 
    122     @Override
    123     public void onFlashlightError() {
    124         refreshState(UserBoolean.BACKGROUND_FALSE);
    125     }
    126 
    127     @Override
    128     public void onFlashlightAvailabilityChanged(boolean available) {
    129         refreshState();
    130     }
    131 
    132     private Runnable mRecentlyOnTimeout = new Runnable() {
    133         @Override
    134         public void run() {
    135             refreshState();
    136         }
    137     };
    138 }
    139