Jim Miller | 5737534 | 2012-09-09 15:20:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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; |
Adam Powell | 3fbbbb6 | 2012-11-07 20:34:00 -0800 | [diff] [blame] | 18 | |
Siva Velusamy | 94a6d15 | 2015-05-05 15:07:00 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Jim Miller | 5737534 | 2012-09-09 15:20:31 -0700 | [diff] [blame] | 20 | import android.content.Context; |
Jason Chang | 1e4a4bd | 2018-05-22 17:30:19 +0800 | [diff] [blame] | 21 | import android.content.res.ColorStateList; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 22 | import android.content.res.TypedArray; |
Jim Miller | 5737534 | 2012-09-09 15:20:31 -0700 | [diff] [blame] | 23 | import android.graphics.Rect; |
| 24 | import android.util.AttributeSet; |
Adam Powell | 9799714 | 2012-11-06 21:32:42 -0800 | [diff] [blame] | 25 | import android.util.Log; |
Jim Miller | 5737534 | 2012-09-09 15:20:31 -0700 | [diff] [blame] | 26 | import android.view.MotionEvent; |
| 27 | import android.view.View; |
Adam Powell | 3fbbbb6 | 2012-11-07 20:34:00 -0800 | [diff] [blame] | 28 | import android.view.ViewDebug; |
Adam Powell | 9799714 | 2012-11-06 21:32:42 -0800 | [diff] [blame] | 29 | import android.view.ViewGroup; |
Siva Velusamy | 94a6d15 | 2015-05-05 15:07:00 -0700 | [diff] [blame] | 30 | import android.view.ViewHierarchyEncoder; |
Adam Powell | 9799714 | 2012-11-06 21:32:42 -0800 | [diff] [blame] | 31 | import android.widget.FrameLayout; |
Jim Miller | 5737534 | 2012-09-09 15:20:31 -0700 | [diff] [blame] | 32 | import android.widget.ViewFlipper; |
| 33 | |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 34 | import com.android.internal.widget.LockPatternUtils; |
Sunny Goyal | 87fccf0 | 2019-08-13 17:39:10 -0700 | [diff] [blame] | 35 | import com.android.systemui.R; |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 36 | |
Jim Miller | 5737534 | 2012-09-09 15:20:31 -0700 | [diff] [blame] | 37 | /** |
| 38 | * Subclass of the current view flipper that allows us to overload dispatchTouchEvent() so |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 39 | * we can emulate {@link android.view.WindowManager.LayoutParams#FLAG_SLIPPERY} within a view |
| 40 | * hierarchy. |
Jim Miller | 5737534 | 2012-09-09 15:20:31 -0700 | [diff] [blame] | 41 | */ |
Adam Cohen | 6fb841f | 2012-10-24 13:15:38 -0700 | [diff] [blame] | 42 | public class KeyguardSecurityViewFlipper extends ViewFlipper implements KeyguardSecurityView { |
Adam Powell | 9799714 | 2012-11-06 21:32:42 -0800 | [diff] [blame] | 43 | private static final String TAG = "KeyguardSecurityViewFlipper"; |
Jorim Jaggi | 5cf1787 | 2014-03-26 18:31:48 +0100 | [diff] [blame] | 44 | private static final boolean DEBUG = KeyguardConstants.DEBUG; |
Adam Powell | 9799714 | 2012-11-06 21:32:42 -0800 | [diff] [blame] | 45 | |
Jim Miller | 5737534 | 2012-09-09 15:20:31 -0700 | [diff] [blame] | 46 | private Rect mTempRect = new Rect(); |
| 47 | |
| 48 | public KeyguardSecurityViewFlipper(Context context) { |
| 49 | this(context, null); |
| 50 | } |
| 51 | |
| 52 | public KeyguardSecurityViewFlipper(Context context, AttributeSet attr) { |
| 53 | super(context, attr); |
| 54 | } |
| 55 | |
| 56 | @Override |
Jim Miller | d2b82f7 | 2012-09-18 20:52:55 -0700 | [diff] [blame] | 57 | public boolean onTouchEvent(MotionEvent ev) { |
| 58 | boolean result = super.onTouchEvent(ev); |
Jim Miller | 5737534 | 2012-09-09 15:20:31 -0700 | [diff] [blame] | 59 | mTempRect.set(0, 0, 0, 0); |
| 60 | for (int i = 0; i < getChildCount(); i++) { |
| 61 | View child = getChildAt(i); |
| 62 | if (child.getVisibility() == View.VISIBLE) { |
| 63 | offsetRectIntoDescendantCoords(child, mTempRect); |
| 64 | ev.offsetLocation(mTempRect.left, mTempRect.top); |
| 65 | result = child.dispatchTouchEvent(ev) || result; |
| 66 | ev.offsetLocation(-mTempRect.left, -mTempRect.top); |
| 67 | } |
| 68 | } |
| 69 | return result; |
| 70 | } |
| 71 | |
Adam Cohen | 6fb841f | 2012-10-24 13:15:38 -0700 | [diff] [blame] | 72 | KeyguardSecurityView getSecurityView() { |
| 73 | View child = getChildAt(getDisplayedChild()); |
| 74 | if (child instanceof KeyguardSecurityView) { |
| 75 | return (KeyguardSecurityView) child; |
| 76 | } |
| 77 | return null; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public void setKeyguardCallback(KeyguardSecurityCallback callback) { |
Jim Miller | bbba68a | 2012-10-24 22:21:40 -0700 | [diff] [blame] | 82 | KeyguardSecurityView ksv = getSecurityView(); |
| 83 | if (ksv != null) { |
| 84 | ksv.setKeyguardCallback(callback); |
| 85 | } |
Adam Cohen | 6fb841f | 2012-10-24 13:15:38 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public void setLockPatternUtils(LockPatternUtils utils) { |
Jim Miller | bbba68a | 2012-10-24 22:21:40 -0700 | [diff] [blame] | 90 | KeyguardSecurityView ksv = getSecurityView(); |
| 91 | if (ksv != null) { |
| 92 | ksv.setLockPatternUtils(utils); |
| 93 | } |
Adam Cohen | 6fb841f | 2012-10-24 13:15:38 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void reset() { |
Jim Miller | bbba68a | 2012-10-24 22:21:40 -0700 | [diff] [blame] | 98 | KeyguardSecurityView ksv = getSecurityView(); |
| 99 | if (ksv != null) { |
| 100 | ksv.reset(); |
| 101 | } |
Adam Cohen | 6fb841f | 2012-10-24 13:15:38 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public void onPause() { |
Jim Miller | bbba68a | 2012-10-24 22:21:40 -0700 | [diff] [blame] | 106 | KeyguardSecurityView ksv = getSecurityView(); |
| 107 | if (ksv != null) { |
| 108 | ksv.onPause(); |
| 109 | } |
Adam Cohen | 6fb841f | 2012-10-24 13:15:38 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | @Override |
Chris Wren | a042ac9 | 2012-11-07 11:37:06 -0500 | [diff] [blame] | 113 | public void onResume(int reason) { |
Jim Miller | bbba68a | 2012-10-24 22:21:40 -0700 | [diff] [blame] | 114 | KeyguardSecurityView ksv = getSecurityView(); |
| 115 | if (ksv != null) { |
Chris Wren | a042ac9 | 2012-11-07 11:37:06 -0500 | [diff] [blame] | 116 | ksv.onResume(reason); |
Jim Miller | bbba68a | 2012-10-24 22:21:40 -0700 | [diff] [blame] | 117 | } |
Adam Cohen | 6fb841f | 2012-10-24 13:15:38 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public boolean needsInput() { |
Jim Miller | bbba68a | 2012-10-24 22:21:40 -0700 | [diff] [blame] | 122 | KeyguardSecurityView ksv = getSecurityView(); |
| 123 | return (ksv != null) ? ksv.needsInput() : false; |
Adam Cohen | 6fb841f | 2012-10-24 13:15:38 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public KeyguardSecurityCallback getCallback() { |
Jim Miller | bbba68a | 2012-10-24 22:21:40 -0700 | [diff] [blame] | 128 | KeyguardSecurityView ksv = getSecurityView(); |
| 129 | return (ksv != null) ? ksv.getCallback() : null; |
Adam Cohen | 6fb841f | 2012-10-24 13:15:38 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
Selim Cinek | 3122fa8 | 2015-06-18 01:38:59 -0700 | [diff] [blame] | 133 | public void showPromptReason(int reason) { |
| 134 | KeyguardSecurityView ksv = getSecurityView(); |
| 135 | if (ksv != null) { |
| 136 | ksv.showPromptReason(reason); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | @Override |
Jason Chang | 1e4a4bd | 2018-05-22 17:30:19 +0800 | [diff] [blame] | 141 | public void showMessage(CharSequence message, ColorStateList colorState) { |
Selim Cinek | cfafe4e | 2015-08-11 14:58:44 -0700 | [diff] [blame] | 142 | KeyguardSecurityView ksv = getSecurityView(); |
| 143 | if (ksv != null) { |
Jason Chang | 1e4a4bd | 2018-05-22 17:30:19 +0800 | [diff] [blame] | 144 | ksv.showMessage(message, colorState); |
Selim Cinek | cfafe4e | 2015-08-11 14:58:44 -0700 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| 148 | @Override |
Adam Cohen | 6fb841f | 2012-10-24 13:15:38 -0700 | [diff] [blame] | 149 | public void showUsabilityHint() { |
| 150 | KeyguardSecurityView ksv = getSecurityView(); |
| 151 | if (ksv != null) { |
| 152 | ksv.showUsabilityHint(); |
| 153 | } |
| 154 | } |
Chris Wren | c0ae9e6 | 2012-11-05 13:16:31 -0500 | [diff] [blame] | 155 | |
| 156 | @Override |
Jorim Jaggi | c14f829 | 2014-05-27 02:25:45 +0200 | [diff] [blame] | 157 | public void startAppearAnimation() { |
| 158 | KeyguardSecurityView ksv = getSecurityView(); |
| 159 | if (ksv != null) { |
| 160 | ksv.startAppearAnimation(); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | @Override |
Jorim Jaggi | 76a1623 | 2014-08-08 17:00:47 +0200 | [diff] [blame] | 165 | public boolean startDisappearAnimation(Runnable finishRunnable) { |
| 166 | KeyguardSecurityView ksv = getSecurityView(); |
| 167 | if (ksv != null) { |
| 168 | return ksv.startDisappearAnimation(finishRunnable); |
| 169 | } else { |
| 170 | return false; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | @Override |
Phil Weaver | 7d847b0 | 2018-02-13 16:02:35 -0800 | [diff] [blame] | 175 | public CharSequence getTitle() { |
| 176 | KeyguardSecurityView ksv = getSecurityView(); |
| 177 | if (ksv != null) { |
| 178 | return ksv.getTitle(); |
| 179 | } |
| 180 | return ""; |
| 181 | } |
| 182 | |
| 183 | @Override |
Adam Powell | 9799714 | 2012-11-06 21:32:42 -0800 | [diff] [blame] | 184 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 185 | return p instanceof LayoutParams; |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { |
| 190 | return p instanceof LayoutParams ? new LayoutParams((LayoutParams) p) : new LayoutParams(p); |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 195 | return new LayoutParams(getContext(), attrs); |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | protected void onMeasure(int widthSpec, int heightSpec) { |
| 200 | final int widthMode = MeasureSpec.getMode(widthSpec); |
| 201 | final int heightMode = MeasureSpec.getMode(heightSpec); |
| 202 | if (DEBUG && widthMode != MeasureSpec.AT_MOST) { |
| 203 | Log.w(TAG, "onMeasure: widthSpec " + MeasureSpec.toString(widthSpec) + |
| 204 | " should be AT_MOST"); |
| 205 | } |
| 206 | if (DEBUG && heightMode != MeasureSpec.AT_MOST) { |
| 207 | Log.w(TAG, "onMeasure: heightSpec " + MeasureSpec.toString(heightSpec) + |
| 208 | " should be AT_MOST"); |
| 209 | } |
| 210 | |
| 211 | final int widthSize = MeasureSpec.getSize(widthSpec); |
| 212 | final int heightSize = MeasureSpec.getSize(heightSpec); |
| 213 | int maxWidth = widthSize; |
| 214 | int maxHeight = heightSize; |
| 215 | final int count = getChildCount(); |
| 216 | for (int i = 0; i < count; i++) { |
| 217 | final View child = getChildAt(i); |
| 218 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 219 | |
| 220 | if (lp.maxWidth > 0 && lp.maxWidth < maxWidth) { |
| 221 | maxWidth = lp.maxWidth; |
| 222 | } |
| 223 | if (lp.maxHeight > 0 && lp.maxHeight < maxHeight) { |
| 224 | maxHeight = lp.maxHeight; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | final int wPadding = getPaddingLeft() + getPaddingRight(); |
| 229 | final int hPadding = getPaddingTop() + getPaddingBottom(); |
Xiyuan Xia | 79f38a2 | 2015-05-28 14:57:21 -0700 | [diff] [blame] | 230 | maxWidth = Math.max(0, maxWidth - wPadding); |
| 231 | maxHeight = Math.max(0, maxHeight - hPadding); |
Adam Powell | 9799714 | 2012-11-06 21:32:42 -0800 | [diff] [blame] | 232 | |
| 233 | int width = widthMode == MeasureSpec.EXACTLY ? widthSize : 0; |
| 234 | int height = heightMode == MeasureSpec.EXACTLY ? heightSize : 0; |
| 235 | for (int i = 0; i < count; i++) { |
| 236 | final View child = getChildAt(i); |
| 237 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 238 | |
| 239 | final int childWidthSpec = makeChildMeasureSpec(maxWidth, lp.width); |
| 240 | final int childHeightSpec = makeChildMeasureSpec(maxHeight, lp.height); |
| 241 | |
| 242 | child.measure(childWidthSpec, childHeightSpec); |
| 243 | |
Adam Powell | eeb6255 | 2012-11-07 14:07:23 -0800 | [diff] [blame] | 244 | width = Math.max(width, Math.min(child.getMeasuredWidth(), widthSize - wPadding)); |
| 245 | height = Math.max(height, Math.min(child.getMeasuredHeight(), heightSize - hPadding)); |
Adam Powell | 9799714 | 2012-11-06 21:32:42 -0800 | [diff] [blame] | 246 | } |
Adam Powell | eeb6255 | 2012-11-07 14:07:23 -0800 | [diff] [blame] | 247 | setMeasuredDimension(width + wPadding, height + hPadding); |
Adam Powell | 9799714 | 2012-11-06 21:32:42 -0800 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | private int makeChildMeasureSpec(int maxSize, int childDimen) { |
| 251 | final int mode; |
| 252 | final int size; |
| 253 | switch (childDimen) { |
| 254 | case LayoutParams.WRAP_CONTENT: |
| 255 | mode = MeasureSpec.AT_MOST; |
| 256 | size = maxSize; |
| 257 | break; |
| 258 | case LayoutParams.MATCH_PARENT: |
| 259 | mode = MeasureSpec.EXACTLY; |
| 260 | size = maxSize; |
| 261 | break; |
| 262 | default: |
| 263 | mode = MeasureSpec.EXACTLY; |
| 264 | size = Math.min(maxSize, childDimen); |
| 265 | break; |
| 266 | } |
| 267 | return MeasureSpec.makeMeasureSpec(size, mode); |
| 268 | } |
| 269 | |
| 270 | public static class LayoutParams extends FrameLayout.LayoutParams { |
Adam Powell | 3fbbbb6 | 2012-11-07 20:34:00 -0800 | [diff] [blame] | 271 | @ViewDebug.ExportedProperty(category = "layout") |
Adam Powell | 9799714 | 2012-11-06 21:32:42 -0800 | [diff] [blame] | 272 | public int maxWidth; |
Adam Powell | 3fbbbb6 | 2012-11-07 20:34:00 -0800 | [diff] [blame] | 273 | |
| 274 | @ViewDebug.ExportedProperty(category = "layout") |
Adam Powell | 9799714 | 2012-11-06 21:32:42 -0800 | [diff] [blame] | 275 | public int maxHeight; |
| 276 | |
| 277 | public LayoutParams(ViewGroup.LayoutParams other) { |
| 278 | super(other); |
| 279 | } |
| 280 | |
| 281 | public LayoutParams(LayoutParams other) { |
| 282 | super(other); |
| 283 | |
| 284 | maxWidth = other.maxWidth; |
| 285 | maxHeight = other.maxHeight; |
| 286 | } |
| 287 | |
| 288 | public LayoutParams(Context c, AttributeSet attrs) { |
| 289 | super(c, attrs); |
| 290 | |
| 291 | final TypedArray a = c.obtainStyledAttributes(attrs, |
| 292 | R.styleable.KeyguardSecurityViewFlipper_Layout, 0, 0); |
| 293 | maxWidth = a.getDimensionPixelSize( |
| 294 | R.styleable.KeyguardSecurityViewFlipper_Layout_layout_maxWidth, 0); |
| 295 | maxHeight = a.getDimensionPixelSize( |
| 296 | R.styleable.KeyguardSecurityViewFlipper_Layout_layout_maxHeight, 0); |
| 297 | a.recycle(); |
| 298 | } |
Siva Velusamy | 94a6d15 | 2015-05-05 15:07:00 -0700 | [diff] [blame] | 299 | |
| 300 | /** @hide */ |
| 301 | @Override |
| 302 | protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) { |
| 303 | super.encodeProperties(encoder); |
| 304 | |
| 305 | encoder.addProperty("layout:maxWidth", maxWidth); |
| 306 | encoder.addProperty("layout:maxHeight", maxHeight); |
| 307 | } |
Adam Powell | 9799714 | 2012-11-06 21:32:42 -0800 | [diff] [blame] | 308 | } |
Jim Miller | 5737534 | 2012-09-09 15:20:31 -0700 | [diff] [blame] | 309 | } |