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