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