blob: 24da3ad46f2341cee04e9e4c15e118fc175e93c5 [file] [log] [blame]
Jim Miller57375342012-09-09 15:20:31 -07001/*
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 Miller5ecd8112013-01-09 18:50:26 -080017package com.android.keyguard;
Adam Powell3fbbbb62012-11-07 20:34:00 -080018
Siva Velusamy94a6d152015-05-05 15:07:00 -070019import android.annotation.NonNull;
Jim Miller57375342012-09-09 15:20:31 -070020import android.content.Context;
Jason Chang1e4a4bd2018-05-22 17:30:19 +080021import android.content.res.ColorStateList;
Gus Prevasab336792018-11-14 13:52:20 -050022import android.content.res.TypedArray;
Jim Miller57375342012-09-09 15:20:31 -070023import android.graphics.Rect;
24import android.util.AttributeSet;
Adam Powell97997142012-11-06 21:32:42 -080025import android.util.Log;
Jim Miller57375342012-09-09 15:20:31 -070026import android.view.MotionEvent;
27import android.view.View;
Adam Powell3fbbbb62012-11-07 20:34:00 -080028import android.view.ViewDebug;
Adam Powell97997142012-11-06 21:32:42 -080029import android.view.ViewGroup;
Siva Velusamy94a6d152015-05-05 15:07:00 -070030import android.view.ViewHierarchyEncoder;
Adam Powell97997142012-11-06 21:32:42 -080031import android.widget.FrameLayout;
Jim Miller57375342012-09-09 15:20:31 -070032import android.widget.ViewFlipper;
33
Jim Miller5ecd8112013-01-09 18:50:26 -080034import com.android.internal.widget.LockPatternUtils;
Sunny Goyal87fccf02019-08-13 17:39:10 -070035import com.android.systemui.R;
Jim Miller5ecd8112013-01-09 18:50:26 -080036
Jim Miller57375342012-09-09 15:20:31 -070037/**
38 * Subclass of the current view flipper that allows us to overload dispatchTouchEvent() so
Gus Prevasab336792018-11-14 13:52:20 -050039 * we can emulate {@link android.view.WindowManager.LayoutParams#FLAG_SLIPPERY} within a view
40 * hierarchy.
Jim Miller57375342012-09-09 15:20:31 -070041 */
Adam Cohen6fb841f2012-10-24 13:15:38 -070042public class KeyguardSecurityViewFlipper extends ViewFlipper implements KeyguardSecurityView {
Adam Powell97997142012-11-06 21:32:42 -080043 private static final String TAG = "KeyguardSecurityViewFlipper";
Jorim Jaggi5cf17872014-03-26 18:31:48 +010044 private static final boolean DEBUG = KeyguardConstants.DEBUG;
Adam Powell97997142012-11-06 21:32:42 -080045
Jim Miller57375342012-09-09 15:20:31 -070046 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 Millerd2b82f72012-09-18 20:52:55 -070057 public boolean onTouchEvent(MotionEvent ev) {
58 boolean result = super.onTouchEvent(ev);
Jim Miller57375342012-09-09 15:20:31 -070059 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 Cohen6fb841f2012-10-24 13:15:38 -070072 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 Millerbbba68a2012-10-24 22:21:40 -070082 KeyguardSecurityView ksv = getSecurityView();
83 if (ksv != null) {
84 ksv.setKeyguardCallback(callback);
85 }
Adam Cohen6fb841f2012-10-24 13:15:38 -070086 }
87
88 @Override
89 public void setLockPatternUtils(LockPatternUtils utils) {
Jim Millerbbba68a2012-10-24 22:21:40 -070090 KeyguardSecurityView ksv = getSecurityView();
91 if (ksv != null) {
92 ksv.setLockPatternUtils(utils);
93 }
Adam Cohen6fb841f2012-10-24 13:15:38 -070094 }
95
96 @Override
97 public void reset() {
Jim Millerbbba68a2012-10-24 22:21:40 -070098 KeyguardSecurityView ksv = getSecurityView();
99 if (ksv != null) {
100 ksv.reset();
101 }
Adam Cohen6fb841f2012-10-24 13:15:38 -0700102 }
103
104 @Override
105 public void onPause() {
Jim Millerbbba68a2012-10-24 22:21:40 -0700106 KeyguardSecurityView ksv = getSecurityView();
107 if (ksv != null) {
108 ksv.onPause();
109 }
Adam Cohen6fb841f2012-10-24 13:15:38 -0700110 }
111
112 @Override
Chris Wrena042ac92012-11-07 11:37:06 -0500113 public void onResume(int reason) {
Jim Millerbbba68a2012-10-24 22:21:40 -0700114 KeyguardSecurityView ksv = getSecurityView();
115 if (ksv != null) {
Chris Wrena042ac92012-11-07 11:37:06 -0500116 ksv.onResume(reason);
Jim Millerbbba68a2012-10-24 22:21:40 -0700117 }
Adam Cohen6fb841f2012-10-24 13:15:38 -0700118 }
119
120 @Override
121 public boolean needsInput() {
Jim Millerbbba68a2012-10-24 22:21:40 -0700122 KeyguardSecurityView ksv = getSecurityView();
123 return (ksv != null) ? ksv.needsInput() : false;
Adam Cohen6fb841f2012-10-24 13:15:38 -0700124 }
125
126 @Override
127 public KeyguardSecurityCallback getCallback() {
Jim Millerbbba68a2012-10-24 22:21:40 -0700128 KeyguardSecurityView ksv = getSecurityView();
129 return (ksv != null) ? ksv.getCallback() : null;
Adam Cohen6fb841f2012-10-24 13:15:38 -0700130 }
131
132 @Override
Selim Cinek3122fa82015-06-18 01:38:59 -0700133 public void showPromptReason(int reason) {
134 KeyguardSecurityView ksv = getSecurityView();
135 if (ksv != null) {
136 ksv.showPromptReason(reason);
137 }
138 }
139
140 @Override
Jason Chang1e4a4bd2018-05-22 17:30:19 +0800141 public void showMessage(CharSequence message, ColorStateList colorState) {
Selim Cinekcfafe4e2015-08-11 14:58:44 -0700142 KeyguardSecurityView ksv = getSecurityView();
143 if (ksv != null) {
Jason Chang1e4a4bd2018-05-22 17:30:19 +0800144 ksv.showMessage(message, colorState);
Selim Cinekcfafe4e2015-08-11 14:58:44 -0700145 }
146 }
147
148 @Override
Adam Cohen6fb841f2012-10-24 13:15:38 -0700149 public void showUsabilityHint() {
150 KeyguardSecurityView ksv = getSecurityView();
151 if (ksv != null) {
152 ksv.showUsabilityHint();
153 }
154 }
Chris Wrenc0ae9e62012-11-05 13:16:31 -0500155
156 @Override
Jorim Jaggic14f8292014-05-27 02:25:45 +0200157 public void startAppearAnimation() {
158 KeyguardSecurityView ksv = getSecurityView();
159 if (ksv != null) {
160 ksv.startAppearAnimation();
161 }
162 }
163
164 @Override
Jorim Jaggi76a16232014-08-08 17:00:47 +0200165 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 Weaver7d847b02018-02-13 16:02:35 -0800175 public CharSequence getTitle() {
176 KeyguardSecurityView ksv = getSecurityView();
177 if (ksv != null) {
178 return ksv.getTitle();
179 }
180 return "";
181 }
182
183 @Override
Adam Powell97997142012-11-06 21:32:42 -0800184 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 Xia79f38a22015-05-28 14:57:21 -0700230 maxWidth = Math.max(0, maxWidth - wPadding);
231 maxHeight = Math.max(0, maxHeight - hPadding);
Adam Powell97997142012-11-06 21:32:42 -0800232
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 Powelleeb62552012-11-07 14:07:23 -0800244 width = Math.max(width, Math.min(child.getMeasuredWidth(), widthSize - wPadding));
245 height = Math.max(height, Math.min(child.getMeasuredHeight(), heightSize - hPadding));
Adam Powell97997142012-11-06 21:32:42 -0800246 }
Adam Powelleeb62552012-11-07 14:07:23 -0800247 setMeasuredDimension(width + wPadding, height + hPadding);
Adam Powell97997142012-11-06 21:32:42 -0800248 }
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 Powell3fbbbb62012-11-07 20:34:00 -0800271 @ViewDebug.ExportedProperty(category = "layout")
Adam Powell97997142012-11-06 21:32:42 -0800272 public int maxWidth;
Adam Powell3fbbbb62012-11-07 20:34:00 -0800273
274 @ViewDebug.ExportedProperty(category = "layout")
Adam Powell97997142012-11-06 21:32:42 -0800275 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 Velusamy94a6d152015-05-05 15:07:00 -0700299
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 Powell97997142012-11-06 21:32:42 -0800308 }
Jim Miller57375342012-09-09 15:20:31 -0700309}