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 21 import com.android.internal.logging.MetricsLogger; 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 private final AnimationIcon mEnable 31 = new AnimationIcon(R.drawable.ic_signal_flashlight_enable_animation); 32 private final AnimationIcon mDisable 33 = new AnimationIcon(R.drawable.ic_signal_flashlight_disable_animation); 34 private final FlashlightController mFlashlightController; 35 36 public FlashlightTile(Host host) { 37 super(host); 38 mFlashlightController = host.getFlashlightController(); 39 mFlashlightController.addListener(this); 40 } 41 42 @Override 43 protected void handleDestroy() { 44 super.handleDestroy(); 45 mFlashlightController.removeListener(this); 46 } 47 48 @Override 49 protected BooleanState newTileState() { 50 return new BooleanState(); 51 } 52 53 @Override 54 public void setListening(boolean listening) { 55 } 56 57 @Override 58 protected void handleUserSwitch(int newUserId) { 59 } 60 61 @Override 62 protected void handleClick() { 63 if (ActivityManager.isUserAMonkey()) { 64 return; 65 } 66 MetricsLogger.action(mContext, getMetricsCategory(), !mState.value); 67 boolean newState = !mState.value; 68 refreshState(newState ? UserBoolean.USER_TRUE : UserBoolean.USER_FALSE); 69 mFlashlightController.setFlashlight(newState); 70 } 71 72 @Override 73 protected void handleUpdateState(BooleanState state, Object arg) { 74 state.visible = mFlashlightController.isAvailable(); 75 state.label = mHost.getContext().getString(R.string.quick_settings_flashlight_label); 76 if (arg instanceof UserBoolean) { 77 boolean value = ((UserBoolean) arg).value; 78 if (value == state.value) { 79 return; 80 } 81 state.value = value; 82 } else { 83 state.value = mFlashlightController.isEnabled(); 84 } 85 final AnimationIcon icon = state.value ? mEnable : mDisable; 86 icon.setAllowAnimation(arg instanceof UserBoolean && ((UserBoolean) arg).userInitiated); 87 state.icon = icon; 88 int onOrOffId = state.value 89 ? R.string.accessibility_quick_settings_flashlight_on 90 : R.string.accessibility_quick_settings_flashlight_off; 91 state.contentDescription = mContext.getString(onOrOffId); 92 } 93 94 @Override 95 public int getMetricsCategory() { 96 return MetricsLogger.QS_FLASHLIGHT; 97 } 98 99 @Override 100 protected String composeChangeAnnouncement() { 101 if (mState.value) { 102 return mContext.getString(R.string.accessibility_quick_settings_flashlight_changed_on); 103 } else { 104 return mContext.getString(R.string.accessibility_quick_settings_flashlight_changed_off); 105 } 106 } 107 108 @Override 109 public void onFlashlightChanged(boolean enabled) { 110 refreshState(enabled ? UserBoolean.BACKGROUND_TRUE : UserBoolean.BACKGROUND_FALSE); 111 } 112 113 @Override 114 public void onFlashlightError() { 115 refreshState(UserBoolean.BACKGROUND_FALSE); 116 } 117 118 @Override 119 public void onFlashlightAvailabilityChanged(boolean available) { 120 refreshState(); 121 } 122 } 123