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.content.Intent;
     21 import android.graphics.drawable.Drawable;
     22 import android.provider.MediaStore;
     23 import android.service.quicksettings.Tile;
     24 import android.widget.Switch;
     25 
     26 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
     27 import com.android.systemui.Dependency;
     28 import com.android.systemui.R;
     29 import com.android.systemui.plugins.qs.QSTile.BooleanState;
     30 import com.android.systemui.qs.QSHost;
     31 import com.android.systemui.qs.tileimpl.QSTileImpl;
     32 import com.android.systemui.statusbar.policy.FlashlightController;
     33 
     34 /** Quick settings tile: Control flashlight **/
     35 public class FlashlightTile extends QSTileImpl<BooleanState> implements
     36         FlashlightController.FlashlightListener {
     37 
     38     private final Icon mIcon = ResourceIcon.get(R.drawable.ic_signal_flashlight);
     39     private final FlashlightController mFlashlightController;
     40 
     41     public FlashlightTile(QSHost host) {
     42         super(host);
     43         mFlashlightController = Dependency.get(FlashlightController.class);
     44     }
     45 
     46     @Override
     47     protected void handleDestroy() {
     48         super.handleDestroy();
     49     }
     50 
     51     @Override
     52     public BooleanState newTileState() {
     53         return new BooleanState();
     54     }
     55 
     56     @Override
     57     public void handleSetListening(boolean listening) {
     58         if (listening) {
     59             mFlashlightController.addCallback(this);
     60         } else {
     61             mFlashlightController.removeCallback(this);
     62         }
     63     }
     64 
     65     @Override
     66     protected void handleUserSwitch(int newUserId) {
     67     }
     68 
     69     @Override
     70     public Intent getLongClickIntent() {
     71         return new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
     72     }
     73 
     74     @Override
     75     public boolean isAvailable() {
     76         return mFlashlightController.hasFlashlight();
     77     }
     78 
     79     @Override
     80     protected void handleClick() {
     81         if (ActivityManager.isUserAMonkey()) {
     82             return;
     83         }
     84         boolean newState = !mState.value;
     85         refreshState(newState);
     86         mFlashlightController.setFlashlight(newState);
     87     }
     88 
     89     @Override
     90     public CharSequence getTileLabel() {
     91         return mContext.getString(R.string.quick_settings_flashlight_label);
     92     }
     93 
     94     @Override
     95     protected void handleLongClick() {
     96         handleClick();
     97     }
     98 
     99     @Override
    100     protected void handleUpdateState(BooleanState state, Object arg) {
    101         if (state.slash == null) {
    102             state.slash = new SlashState();
    103         }
    104         state.label = mHost.getContext().getString(R.string.quick_settings_flashlight_label);
    105         if (!mFlashlightController.isAvailable()) {
    106             state.icon = mIcon;
    107             state.slash.isSlashed = true;
    108             state.contentDescription = mContext.getString(
    109                     R.string.accessibility_quick_settings_flashlight_unavailable);
    110             state.state = Tile.STATE_UNAVAILABLE;
    111             return;
    112         }
    113         if (arg instanceof Boolean) {
    114             boolean value = (Boolean) arg;
    115             if (value == state.value) {
    116                 return;
    117             }
    118             state.value = value;
    119         } else {
    120             state.value = mFlashlightController.isEnabled();
    121         }
    122         state.icon = mIcon;
    123         state.slash.isSlashed = !state.value;
    124         state.contentDescription = mContext.getString(R.string.quick_settings_flashlight_label);
    125         state.expandedAccessibilityClassName = Switch.class.getName();
    126         state.state = state.value ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE;
    127     }
    128 
    129     @Override
    130     public int getMetricsCategory() {
    131         return MetricsEvent.QS_FLASHLIGHT;
    132     }
    133 
    134     @Override
    135     protected String composeChangeAnnouncement() {
    136         if (mState.value) {
    137             return mContext.getString(R.string.accessibility_quick_settings_flashlight_changed_on);
    138         } else {
    139             return mContext.getString(R.string.accessibility_quick_settings_flashlight_changed_off);
    140         }
    141     }
    142 
    143     @Override
    144     public void onFlashlightChanged(boolean enabled) {
    145         refreshState(enabled);
    146     }
    147 
    148     @Override
    149     public void onFlashlightError() {
    150         refreshState(false);
    151     }
    152 
    153     @Override
    154     public void onFlashlightAvailabilityChanged(boolean available) {
    155         refreshState();
    156     }
    157 }
    158