FlashlightTile.java revision 1aec93f1819369bda9e6cb0bd282419808c304c3
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.content.Intent; 21import android.graphics.drawable.Drawable; 22import android.provider.MediaStore; 23import android.text.SpannableStringBuilder; 24import android.text.style.ForegroundColorSpan; 25import com.android.internal.logging.MetricsLogger; 26import com.android.internal.logging.MetricsProto.MetricsEvent; 27import com.android.systemui.R; 28import com.android.systemui.qs.QSTile; 29import com.android.systemui.statusbar.policy.FlashlightController; 30 31/** Quick settings tile: Control flashlight **/ 32public class FlashlightTile extends QSTile<QSTile.BooleanState> implements 33 FlashlightController.FlashlightListener { 34 35 private final AnimationIcon mEnable 36 = new AnimationIcon(R.drawable.ic_signal_flashlight_enable_animation, 37 R.drawable.ic_signal_flashlight_disable); 38 private final AnimationIcon mDisable 39 = new AnimationIcon(R.drawable.ic_signal_flashlight_disable_animation, 40 R.drawable.ic_signal_flashlight_enable); 41 private final FlashlightController mFlashlightController; 42 43 public FlashlightTile(Host host) { 44 super(host); 45 mFlashlightController = host.getFlashlightController(); 46 mFlashlightController.addListener(this); 47 } 48 49 @Override 50 protected void handleDestroy() { 51 super.handleDestroy(); 52 mFlashlightController.removeListener(this); 53 } 54 55 @Override 56 public BooleanState newTileState() { 57 return new BooleanState(); 58 } 59 60 @Override 61 public void setListening(boolean listening) { 62 } 63 64 @Override 65 protected void handleUserSwitch(int newUserId) { 66 } 67 68 @Override 69 public Intent getLongClickIntent() { 70 return new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA); 71 } 72 73 @Override 74 public boolean isAvailable() { 75 return mFlashlightController.hasFlashlight(); 76 } 77 78 @Override 79 protected void handleClick() { 80 if (ActivityManager.isUserAMonkey()) { 81 return; 82 } 83 MetricsLogger.action(mContext, getMetricsCategory(), !mState.value); 84 boolean newState = !mState.value; 85 refreshState(newState); 86 mFlashlightController.setFlashlight(newState); 87 } 88 89 @Override 90 protected void handleUpdateState(BooleanState state, Object arg) { 91 state.label = mHost.getContext().getString(R.string.quick_settings_flashlight_label); 92 if (!mFlashlightController.isAvailable()) { 93 Drawable icon = mHost.getContext().getDrawable(R.drawable.ic_signal_flashlight_disable); 94 final int disabledColor = mHost.getContext().getColor(R.color.qs_tile_tint_unavailable); 95 icon.setTint(disabledColor); 96 state.icon = new DrawableIcon(icon); 97 state.label = new SpannableStringBuilder().append(state.label, 98 new ForegroundColorSpan(disabledColor), 99 SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE); 100 state.contentDescription = mContext.getString( 101 R.string.accessibility_quick_settings_flashlight_unavailable); 102 return; 103 } 104 if (arg instanceof Boolean) { 105 boolean value = (Boolean) arg; 106 if (value == state.value) { 107 return; 108 } 109 state.value = value; 110 } else { 111 state.value = mFlashlightController.isEnabled(); 112 } 113 final AnimationIcon icon = state.value ? mEnable : mDisable; 114 state.icon = icon; 115 int onOrOffId = state.value 116 ? R.string.accessibility_quick_settings_flashlight_on 117 : R.string.accessibility_quick_settings_flashlight_off; 118 state.contentDescription = mContext.getString(onOrOffId); 119 } 120 121 @Override 122 public int getMetricsCategory() { 123 return MetricsEvent.QS_FLASHLIGHT; 124 } 125 126 @Override 127 protected String composeChangeAnnouncement() { 128 if (mState.value) { 129 return mContext.getString(R.string.accessibility_quick_settings_flashlight_changed_on); 130 } else { 131 return mContext.getString(R.string.accessibility_quick_settings_flashlight_changed_off); 132 } 133 } 134 135 @Override 136 public void onFlashlightChanged(boolean enabled) { 137 refreshState(enabled); 138 } 139 140 @Override 141 public void onFlashlightError() { 142 refreshState(false); 143 } 144 145 @Override 146 public void onFlashlightAvailabilityChanged(boolean available) { 147 refreshState(); 148 } 149} 150