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