Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 17 | package com.android.keyguard; |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 18 | |
Lucas Dupin | 2e838ac | 2019-04-17 16:50:58 -0700 | [diff] [blame] | 19 | import static com.android.systemui.util.InjectionInflationController.VIEW_CONTEXT; |
| 20 | |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Jason Chang | 1e4a4bd | 2018-05-22 17:30:19 +0800 | [diff] [blame] | 22 | import android.content.res.ColorStateList; |
Lucas Dupin | 2e838ac | 2019-04-17 16:50:58 -0700 | [diff] [blame] | 23 | import android.content.res.TypedArray; |
| 24 | import android.graphics.Color; |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 25 | import android.os.Handler; |
| 26 | import android.os.Looper; |
Alan Viverette | b428b0f | 2013-02-07 12:19:15 -0800 | [diff] [blame] | 27 | import android.os.SystemClock; |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 28 | import android.text.TextUtils; |
| 29 | import android.util.AttributeSet; |
Lucas Dupin | 3277492 | 2019-04-25 12:42:25 -0700 | [diff] [blame] | 30 | import android.util.TypedValue; |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 31 | import android.view.View; |
| 32 | import android.widget.TextView; |
| 33 | |
Sunny Goyal | 87fccf0 | 2019-08-13 17:39:10 -0700 | [diff] [blame] | 34 | import com.android.systemui.R; |
Lucas Dupin | 2e838ac | 2019-04-17 16:50:58 -0700 | [diff] [blame] | 35 | import com.android.systemui.statusbar.policy.ConfigurationController; |
| 36 | |
Alan Viverette | b428b0f | 2013-02-07 12:19:15 -0800 | [diff] [blame] | 37 | import java.lang.ref.WeakReference; |
| 38 | |
Lucas Dupin | 2e838ac | 2019-04-17 16:50:58 -0700 | [diff] [blame] | 39 | import javax.inject.Inject; |
| 40 | import javax.inject.Named; |
| 41 | |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 42 | /*** |
| 43 | * Manages a number of views inside of the given layout. See below for a list of widgets. |
| 44 | */ |
Lucas Dupin | 2e838ac | 2019-04-17 16:50:58 -0700 | [diff] [blame] | 45 | public class KeyguardMessageArea extends TextView implements SecurityMessageDisplay, |
| 46 | ConfigurationController.ConfigurationListener { |
Alan Viverette | b428b0f | 2013-02-07 12:19:15 -0800 | [diff] [blame] | 47 | /** Handler token posted with accessibility announcement runnables. */ |
| 48 | private static final Object ANNOUNCE_TOKEN = new Object(); |
| 49 | |
| 50 | /** |
| 51 | * Delay before speaking an accessibility announcement. Used to prevent |
| 52 | * lift-to-type from interrupting itself. |
| 53 | */ |
| 54 | private static final long ANNOUNCEMENT_DELAY = 250; |
Selim Cinek | cfafe4e | 2015-08-11 14:58:44 -0700 | [diff] [blame] | 55 | private static final int DEFAULT_COLOR = -1; |
Alan Viverette | b428b0f | 2013-02-07 12:19:15 -0800 | [diff] [blame] | 56 | |
Adrian Roos | 28828b5 | 2015-05-25 18:48:14 -0700 | [diff] [blame] | 57 | private final Handler mHandler; |
Lucas Dupin | 2e838ac | 2019-04-17 16:50:58 -0700 | [diff] [blame] | 58 | private final ConfigurationController mConfigurationController; |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 59 | |
Lucas Dupin | 2e838ac | 2019-04-17 16:50:58 -0700 | [diff] [blame] | 60 | private ColorStateList mDefaultColorState; |
Adrian Roos | 154b9ee | 2016-10-20 15:34:23 -0700 | [diff] [blame] | 61 | private CharSequence mMessage; |
Jason Chang | 1e4a4bd | 2018-05-22 17:30:19 +0800 | [diff] [blame] | 62 | private ColorStateList mNextMessageColorState = ColorStateList.valueOf(DEFAULT_COLOR); |
Lucas Dupin | 473f3a5 | 2019-04-30 15:33:28 -0700 | [diff] [blame] | 63 | private boolean mBouncerVisible; |
Jim Miller | 187ec58 | 2013-04-15 18:27:54 -0700 | [diff] [blame] | 64 | |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 65 | private KeyguardUpdateMonitorCallback mInfoCallback = new KeyguardUpdateMonitorCallback() { |
Jorim Jaggi | 0d210f6 | 2015-07-10 14:24:44 -0700 | [diff] [blame] | 66 | public void onFinishedGoingToSleep(int why) { |
Jim Miller | 20daffd | 2013-10-07 14:59:53 -0700 | [diff] [blame] | 67 | setSelected(false); |
Lucas Dupin | 473f3a5 | 2019-04-30 15:33:28 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Jorim Jaggi | 0d210f6 | 2015-07-10 14:24:44 -0700 | [diff] [blame] | 70 | public void onStartedWakingUp() { |
Jim Miller | 20daffd | 2013-10-07 14:59:53 -0700 | [diff] [blame] | 71 | setSelected(true); |
Lucas Dupin | 473f3a5 | 2019-04-30 15:33:28 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public void onKeyguardBouncerChanged(boolean bouncer) { |
| 76 | mBouncerVisible = bouncer; |
| 77 | update(); |
| 78 | } |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | public KeyguardMessageArea(Context context) { |
Lucas Dupin | 2e838ac | 2019-04-17 16:50:58 -0700 | [diff] [blame] | 82 | super(context, null); |
| 83 | throw new IllegalStateException("This constructor should never be invoked"); |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Lucas Dupin | 2e838ac | 2019-04-17 16:50:58 -0700 | [diff] [blame] | 86 | @Inject |
| 87 | public KeyguardMessageArea(@Named(VIEW_CONTEXT) Context context, AttributeSet attrs, |
| 88 | ConfigurationController configurationController) { |
| 89 | this(context, attrs, KeyguardUpdateMonitor.getInstance(context), configurationController); |
Adrian Roos | 154b9ee | 2016-10-20 15:34:23 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Lucas Dupin | 2e838ac | 2019-04-17 16:50:58 -0700 | [diff] [blame] | 92 | public KeyguardMessageArea(Context context, AttributeSet attrs, KeyguardUpdateMonitor monitor, |
| 93 | ConfigurationController configurationController) { |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 94 | super(context, attrs); |
Chris Craik | 362adb8 | 2013-11-05 17:41:21 -0800 | [diff] [blame] | 95 | setLayerType(LAYER_TYPE_HARDWARE, null); // work around nested unclipped SaveLayer bug |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 96 | |
Adrian Roos | 154b9ee | 2016-10-20 15:34:23 -0700 | [diff] [blame] | 97 | monitor.registerCallback(mInfoCallback); |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 98 | mHandler = new Handler(Looper.myLooper()); |
Lucas Dupin | 2e838ac | 2019-04-17 16:50:58 -0700 | [diff] [blame] | 99 | mConfigurationController = configurationController; |
Lucas Dupin | 2e838ac | 2019-04-17 16:50:58 -0700 | [diff] [blame] | 100 | onThemeChanged(); |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Jim Miller | 20daffd | 2013-10-07 14:59:53 -0700 | [diff] [blame] | 103 | @Override |
Lucas Dupin | 2e838ac | 2019-04-17 16:50:58 -0700 | [diff] [blame] | 104 | protected void onAttachedToWindow() { |
| 105 | super.onAttachedToWindow(); |
| 106 | mConfigurationController.addCallback(this); |
Lucas Dupin | 12dab90 | 2019-04-19 11:10:48 -0700 | [diff] [blame] | 107 | onThemeChanged(); |
Lucas Dupin | 2e838ac | 2019-04-17 16:50:58 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Override |
| 111 | protected void onDetachedFromWindow() { |
| 112 | super.onDetachedFromWindow(); |
| 113 | mConfigurationController.removeCallback(this); |
| 114 | } |
| 115 | |
| 116 | @Override |
Jason Chang | 1e4a4bd | 2018-05-22 17:30:19 +0800 | [diff] [blame] | 117 | public void setNextMessageColor(ColorStateList colorState) { |
| 118 | mNextMessageColorState = colorState; |
Selim Cinek | cfafe4e | 2015-08-11 14:58:44 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | @Override |
Lucas Dupin | 2e838ac | 2019-04-17 16:50:58 -0700 | [diff] [blame] | 122 | public void onThemeChanged() { |
| 123 | TypedArray array = mContext.obtainStyledAttributes(new int[] { |
| 124 | R.attr.wallpaperTextColor |
| 125 | }); |
| 126 | ColorStateList newTextColors = ColorStateList.valueOf(array.getColor(0, Color.RED)); |
| 127 | array.recycle(); |
Lucas Dupin | 2e838ac | 2019-04-17 16:50:58 -0700 | [diff] [blame] | 128 | mDefaultColorState = newTextColors; |
Lucas Dupin | 12dab90 | 2019-04-19 11:10:48 -0700 | [diff] [blame] | 129 | update(); |
Lucas Dupin | 2e838ac | 2019-04-17 16:50:58 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
Lucas Dupin | 3277492 | 2019-04-25 12:42:25 -0700 | [diff] [blame] | 133 | public void onDensityOrFontScaleChanged() { |
| 134 | TypedArray array = mContext.obtainStyledAttributes(R.style.Keyguard_TextView, new int[] { |
| 135 | android.R.attr.textSize |
| 136 | }); |
| 137 | setTextSize(TypedValue.COMPLEX_UNIT_PX, array.getDimensionPixelSize(0, 0)); |
| 138 | array.recycle(); |
| 139 | } |
| 140 | |
| 141 | @Override |
Adrian Roos | db327e9 | 2016-10-12 16:41:28 -0700 | [diff] [blame] | 142 | public void setMessage(CharSequence msg) { |
| 143 | if (!TextUtils.isEmpty(msg)) { |
Adrian Roos | 28828b5 | 2015-05-25 18:48:14 -0700 | [diff] [blame] | 144 | securityMessageChanged(msg); |
| 145 | } else { |
| 146 | clearMessage(); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | @Override |
Adrian Roos | db327e9 | 2016-10-12 16:41:28 -0700 | [diff] [blame] | 151 | public void setMessage(int resId) { |
| 152 | CharSequence message = null; |
| 153 | if (resId != 0) { |
| 154 | message = getContext().getResources().getText(resId); |
Adrian Roos | 28828b5 | 2015-05-25 18:48:14 -0700 | [diff] [blame] | 155 | } |
Adrian Roos | db327e9 | 2016-10-12 16:41:28 -0700 | [diff] [blame] | 156 | setMessage(message); |
Adrian Roos | 28828b5 | 2015-05-25 18:48:14 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | @Override |
Adrian Roos | db327e9 | 2016-10-12 16:41:28 -0700 | [diff] [blame] | 160 | public void formatMessage(int resId, Object... formatArgs) { |
| 161 | CharSequence message = null; |
| 162 | if (resId != 0) { |
| 163 | message = getContext().getString(resId, formatArgs); |
Adrian Roos | 28828b5 | 2015-05-25 18:48:14 -0700 | [diff] [blame] | 164 | } |
Adrian Roos | db327e9 | 2016-10-12 16:41:28 -0700 | [diff] [blame] | 165 | setMessage(message); |
Adrian Roos | 28828b5 | 2015-05-25 18:48:14 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Lucas Dupin | 2e838ac | 2019-04-17 16:50:58 -0700 | [diff] [blame] | 168 | public static KeyguardMessageArea findSecurityMessageDisplay(View v) { |
| 169 | KeyguardMessageArea messageArea = v.findViewById(R.id.keyguard_message_area); |
| 170 | if (messageArea == null) { |
| 171 | messageArea = v.getRootView().findViewById(R.id.keyguard_message_area); |
| 172 | } |
Adrian Roos | 28828b5 | 2015-05-25 18:48:14 -0700 | [diff] [blame] | 173 | if (messageArea == null) { |
| 174 | throw new RuntimeException("Can't find keyguard_message_area in " + v.getClass()); |
| 175 | } |
| 176 | return messageArea; |
| 177 | } |
| 178 | |
| 179 | @Override |
Jim Miller | 20daffd | 2013-10-07 14:59:53 -0700 | [diff] [blame] | 180 | protected void onFinishInflate() { |
Jorim Jaggi | 0d210f6 | 2015-07-10 14:24:44 -0700 | [diff] [blame] | 181 | boolean shouldMarquee = KeyguardUpdateMonitor.getInstance(mContext).isDeviceInteractive(); |
| 182 | setSelected(shouldMarquee); // This is required to ensure marquee works |
Jim Miller | 20daffd | 2013-10-07 14:59:53 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Adrian Roos | 28828b5 | 2015-05-25 18:48:14 -0700 | [diff] [blame] | 185 | private void securityMessageChanged(CharSequence message) { |
| 186 | mMessage = message; |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 187 | update(); |
Alan Viverette | b428b0f | 2013-02-07 12:19:15 -0800 | [diff] [blame] | 188 | mHandler.removeCallbacksAndMessages(ANNOUNCE_TOKEN); |
| 189 | mHandler.postAtTime(new AnnounceRunnable(this, getText()), ANNOUNCE_TOKEN, |
| 190 | (SystemClock.uptimeMillis() + ANNOUNCEMENT_DELAY)); |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Adrian Roos | 28828b5 | 2015-05-25 18:48:14 -0700 | [diff] [blame] | 193 | private void clearMessage() { |
Adrian Roos | 154b9ee | 2016-10-20 15:34:23 -0700 | [diff] [blame] | 194 | mMessage = null; |
| 195 | update(); |
Xiyuan Xia | 09eb033 | 2015-05-13 15:29:42 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Adrian Roos | 28828b5 | 2015-05-25 18:48:14 -0700 | [diff] [blame] | 198 | private void update() { |
| 199 | CharSequence status = mMessage; |
Lucas Dupin | 473f3a5 | 2019-04-30 15:33:28 -0700 | [diff] [blame] | 200 | setVisibility(TextUtils.isEmpty(status) || !mBouncerVisible ? INVISIBLE : VISIBLE); |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 201 | setText(status); |
Jason Chang | 1e4a4bd | 2018-05-22 17:30:19 +0800 | [diff] [blame] | 202 | ColorStateList colorState = mDefaultColorState; |
| 203 | if (mNextMessageColorState.getDefaultColor() != DEFAULT_COLOR) { |
| 204 | colorState = mNextMessageColorState; |
| 205 | mNextMessageColorState = ColorStateList.valueOf(DEFAULT_COLOR); |
Selim Cinek | cfafe4e | 2015-08-11 14:58:44 -0700 | [diff] [blame] | 206 | } |
Jason Chang | 1e4a4bd | 2018-05-22 17:30:19 +0800 | [diff] [blame] | 207 | setTextColor(colorState); |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 210 | |
Alan Viverette | b428b0f | 2013-02-07 12:19:15 -0800 | [diff] [blame] | 211 | /** |
| 212 | * Runnable used to delay accessibility announcements. |
| 213 | */ |
| 214 | private static class AnnounceRunnable implements Runnable { |
| 215 | private final WeakReference<View> mHost; |
| 216 | private final CharSequence mTextToAnnounce; |
| 217 | |
Adrian Roos | 28828b5 | 2015-05-25 18:48:14 -0700 | [diff] [blame] | 218 | AnnounceRunnable(View host, CharSequence textToAnnounce) { |
Alan Viverette | b428b0f | 2013-02-07 12:19:15 -0800 | [diff] [blame] | 219 | mHost = new WeakReference<View>(host); |
| 220 | mTextToAnnounce = textToAnnounce; |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public void run() { |
| 225 | final View host = mHost.get(); |
| 226 | if (host != null) { |
| 227 | host.announceForAccessibility(mTextToAnnounce); |
| 228 | } |
| 229 | } |
| 230 | } |
Jim Miller | 0b72824 | 2012-10-28 19:42:30 -0700 | [diff] [blame] | 231 | } |