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