Adrian Roos | b83777b | 2014-06-30 15:11:53 +0200 | [diff] [blame] | 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 | |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 19 | import android.app.ActivityManager; |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 20 | import android.content.Intent; |
Jason Monk | 611994e | 2016-02-25 16:46:55 -0500 | [diff] [blame] | 21 | import android.graphics.drawable.Drawable; |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 22 | import android.provider.MediaStore; |
Jason Monk | 611994e | 2016-02-25 16:46:55 -0500 | [diff] [blame] | 23 | import android.text.SpannableStringBuilder; |
| 24 | import android.text.style.ForegroundColorSpan; |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 25 | import com.android.internal.logging.MetricsLogger; |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 26 | import com.android.internal.logging.MetricsProto.MetricsEvent; |
Adrian Roos | b83777b | 2014-06-30 15:11:53 +0200 | [diff] [blame] | 27 | import com.android.systemui.R; |
| 28 | import com.android.systemui.qs.QSTile; |
Adrian Roos | b83777b | 2014-06-30 15:11:53 +0200 | [diff] [blame] | 29 | import com.android.systemui.statusbar.policy.FlashlightController; |
| 30 | |
Adrian Roos | b83777b | 2014-06-30 15:11:53 +0200 | [diff] [blame] | 31 | /** Quick settings tile: Control flashlight **/ |
| 32 | public class FlashlightTile extends QSTile<QSTile.BooleanState> implements |
| 33 | FlashlightController.FlashlightListener { |
| 34 | |
John Spurlock | c6df3cf | 2014-11-04 19:36:44 -0500 | [diff] [blame] | 35 | private final AnimationIcon mEnable |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 36 | = new AnimationIcon(R.drawable.ic_signal_flashlight_enable_animation, |
| 37 | R.drawable.ic_signal_flashlight_disable); |
John Spurlock | c6df3cf | 2014-11-04 19:36:44 -0500 | [diff] [blame] | 38 | private final AnimationIcon mDisable |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 39 | = new AnimationIcon(R.drawable.ic_signal_flashlight_disable_animation, |
| 40 | R.drawable.ic_signal_flashlight_enable); |
Adrian Roos | b83777b | 2014-06-30 15:11:53 +0200 | [diff] [blame] | 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 |
John Spurlock | bceed06 | 2014-08-10 18:04:16 -0400 | [diff] [blame] | 50 | protected void handleDestroy() { |
| 51 | super.handleDestroy(); |
| 52 | mFlashlightController.removeListener(this); |
| 53 | } |
| 54 | |
| 55 | @Override |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 56 | public BooleanState newTileState() { |
Adrian Roos | b83777b | 2014-06-30 15:11:53 +0200 | [diff] [blame] | 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 |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 69 | public Intent getLongClickIntent() { |
| 70 | return new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA); |
| 71 | } |
| 72 | |
| 73 | @Override |
Jason Monk | 72d7bc4 | 2016-02-29 10:03:13 -0500 | [diff] [blame] | 74 | public boolean isAvailable() { |
| 75 | return mFlashlightController.hasFlashlight(); |
| 76 | } |
| 77 | |
| 78 | @Override |
Adrian Roos | b83777b | 2014-06-30 15:11:53 +0200 | [diff] [blame] | 79 | protected void handleClick() { |
Guang Zhu | 2649f6b | 2014-07-27 14:47:43 -0700 | [diff] [blame] | 80 | if (ActivityManager.isUserAMonkey()) { |
| 81 | return; |
| 82 | } |
Chris Wren | 9e7283f | 2015-05-08 17:23:47 -0400 | [diff] [blame] | 83 | MetricsLogger.action(mContext, getMetricsCategory(), !mState.value); |
Adrian Roos | b83777b | 2014-06-30 15:11:53 +0200 | [diff] [blame] | 84 | boolean newState = !mState.value; |
Jason Monk | 66239fb | 2015-12-21 14:27:00 -0500 | [diff] [blame] | 85 | refreshState(newState); |
Adrian Roos | fab0d81 | 2015-03-10 14:43:47 +0100 | [diff] [blame] | 86 | mFlashlightController.setFlashlight(newState); |
Adrian Roos | b83777b | 2014-06-30 15:11:53 +0200 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Override |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 90 | public CharSequence getTileLabel() { |
| 91 | return mContext.getString(R.string.quick_settings_flashlight_label); |
| 92 | } |
| 93 | |
| 94 | @Override |
Jason Monk | 8d34469 | 2016-03-28 10:19:49 -0400 | [diff] [blame] | 95 | protected void handleLongClick() { |
| 96 | handleClick(); |
| 97 | } |
| 98 | |
| 99 | @Override |
Adrian Roos | b83777b | 2014-06-30 15:11:53 +0200 | [diff] [blame] | 100 | protected void handleUpdateState(BooleanState state, Object arg) { |
Adrian Roos | fab0d81 | 2015-03-10 14:43:47 +0100 | [diff] [blame] | 101 | state.label = mHost.getContext().getString(R.string.quick_settings_flashlight_label); |
Jason Monk | 611994e | 2016-02-25 16:46:55 -0500 | [diff] [blame] | 102 | if (!mFlashlightController.isAvailable()) { |
| 103 | Drawable icon = mHost.getContext().getDrawable(R.drawable.ic_signal_flashlight_disable); |
| 104 | final int disabledColor = mHost.getContext().getColor(R.color.qs_tile_tint_unavailable); |
| 105 | icon.setTint(disabledColor); |
| 106 | state.icon = new DrawableIcon(icon); |
| 107 | state.label = new SpannableStringBuilder().append(state.label, |
| 108 | new ForegroundColorSpan(disabledColor), |
| 109 | SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE); |
| 110 | state.contentDescription = mContext.getString( |
| 111 | R.string.accessibility_quick_settings_flashlight_unavailable); |
| 112 | return; |
| 113 | } |
Jason Monk | 66239fb | 2015-12-21 14:27:00 -0500 | [diff] [blame] | 114 | if (arg instanceof Boolean) { |
| 115 | boolean value = (Boolean) arg; |
Jason Monk | 9128b1e | 2015-06-04 13:36:14 -0400 | [diff] [blame] | 116 | if (value == state.value) { |
| 117 | return; |
| 118 | } |
| 119 | state.value = value; |
Jason Monk | 3266f61 | 2015-07-31 11:37:11 -0400 | [diff] [blame] | 120 | } else { |
| 121 | state.value = mFlashlightController.isEnabled(); |
Adrian Roos | b83777b | 2014-06-30 15:11:53 +0200 | [diff] [blame] | 122 | } |
John Spurlock | c6df3cf | 2014-11-04 19:36:44 -0500 | [diff] [blame] | 123 | final AnimationIcon icon = state.value ? mEnable : mDisable; |
John Spurlock | c6df3cf | 2014-11-04 19:36:44 -0500 | [diff] [blame] | 124 | state.icon = icon; |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 125 | int onOrOffId = state.value |
| 126 | ? R.string.accessibility_quick_settings_flashlight_on |
| 127 | : R.string.accessibility_quick_settings_flashlight_off; |
| 128 | state.contentDescription = mContext.getString(onOrOffId); |
| 129 | } |
| 130 | |
| 131 | @Override |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 132 | public int getMetricsCategory() { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 133 | return MetricsEvent.QS_FLASHLIGHT; |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | @Override |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 137 | protected String composeChangeAnnouncement() { |
| 138 | if (mState.value) { |
| 139 | return mContext.getString(R.string.accessibility_quick_settings_flashlight_changed_on); |
| 140 | } else { |
| 141 | return mContext.getString(R.string.accessibility_quick_settings_flashlight_changed_off); |
| 142 | } |
Adrian Roos | b83777b | 2014-06-30 15:11:53 +0200 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @Override |
Adrian Roos | fab0d81 | 2015-03-10 14:43:47 +0100 | [diff] [blame] | 146 | public void onFlashlightChanged(boolean enabled) { |
Jason Monk | 66239fb | 2015-12-21 14:27:00 -0500 | [diff] [blame] | 147 | refreshState(enabled); |
Adrian Roos | b83777b | 2014-06-30 15:11:53 +0200 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public void onFlashlightError() { |
Jason Monk | 66239fb | 2015-12-21 14:27:00 -0500 | [diff] [blame] | 152 | refreshState(false); |
Adrian Roos | b83777b | 2014-06-30 15:11:53 +0200 | [diff] [blame] | 153 | } |
Adrian Roos | 930aaa7 | 2014-07-22 15:40:59 +0200 | [diff] [blame] | 154 | |
| 155 | @Override |
| 156 | public void onFlashlightAvailabilityChanged(boolean available) { |
| 157 | refreshState(); |
| 158 | } |
Adrian Roos | b83777b | 2014-06-30 15:11:53 +0200 | [diff] [blame] | 159 | } |