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 |
| 90 | protected void handleUpdateState(BooleanState state, Object arg) { |
Adrian Roos | fab0d81 | 2015-03-10 14:43:47 +0100 | [diff] [blame] | 91 | state.label = mHost.getContext().getString(R.string.quick_settings_flashlight_label); |
Jason Monk | 611994e | 2016-02-25 16:46:55 -0500 | [diff] [blame] | 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 | } |
Jason Monk | 66239fb | 2015-12-21 14:27:00 -0500 | [diff] [blame] | 104 | if (arg instanceof Boolean) { |
| 105 | boolean value = (Boolean) arg; |
Jason Monk | 9128b1e | 2015-06-04 13:36:14 -0400 | [diff] [blame] | 106 | if (value == state.value) { |
| 107 | return; |
| 108 | } |
| 109 | state.value = value; |
Jason Monk | 3266f61 | 2015-07-31 11:37:11 -0400 | [diff] [blame] | 110 | } else { |
| 111 | state.value = mFlashlightController.isEnabled(); |
Adrian Roos | b83777b | 2014-06-30 15:11:53 +0200 | [diff] [blame] | 112 | } |
John Spurlock | c6df3cf | 2014-11-04 19:36:44 -0500 | [diff] [blame] | 113 | final AnimationIcon icon = state.value ? mEnable : mDisable; |
John Spurlock | c6df3cf | 2014-11-04 19:36:44 -0500 | [diff] [blame] | 114 | state.icon = icon; |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 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 |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 122 | public int getMetricsCategory() { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 123 | return MetricsEvent.QS_FLASHLIGHT; |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Override |
Selim Cinek | 4fda7b2 | 2014-08-18 22:07:25 +0200 | [diff] [blame] | 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 | } |
Adrian Roos | b83777b | 2014-06-30 15:11:53 +0200 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Override |
Adrian Roos | fab0d81 | 2015-03-10 14:43:47 +0100 | [diff] [blame] | 136 | public void onFlashlightChanged(boolean enabled) { |
Jason Monk | 66239fb | 2015-12-21 14:27:00 -0500 | [diff] [blame] | 137 | refreshState(enabled); |
Adrian Roos | b83777b | 2014-06-30 15:11:53 +0200 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public void onFlashlightError() { |
Jason Monk | 66239fb | 2015-12-21 14:27:00 -0500 | [diff] [blame] | 142 | refreshState(false); |
Adrian Roos | b83777b | 2014-06-30 15:11:53 +0200 | [diff] [blame] | 143 | } |
Adrian Roos | 930aaa7 | 2014-07-22 15:40:59 +0200 | [diff] [blame] | 144 | |
| 145 | @Override |
| 146 | public void onFlashlightAvailabilityChanged(boolean available) { |
| 147 | refreshState(); |
| 148 | } |
Adrian Roos | b83777b | 2014-06-30 15:11:53 +0200 | [diff] [blame] | 149 | } |