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.widget.FrameLayout; 31 import android.widget.ViewFlipper; 32 33 import com.android.systemui.R; 34 35 /** 36 * Subclass of the current view flipper that allows us to overload dispatchTouchEvent() so 37 * we can emulate {@link android.view.WindowManager.LayoutParams#FLAG_SLIPPERY} within a view 38 * hierarchy. 39 */ 40 public class KeyguardSecurityViewFlipper extends ViewFlipper { 41 private static final String TAG = "KeyguardSecurityViewFlipper"; 42 private static final boolean DEBUG = KeyguardConstants.DEBUG; 43 44 private Rect mTempRect = new Rect(); 45 KeyguardSecurityViewFlipper(Context context)46 public KeyguardSecurityViewFlipper(Context context) { 47 this(context, null); 48 } 49 KeyguardSecurityViewFlipper(Context context, AttributeSet attr)50 public KeyguardSecurityViewFlipper(Context context, AttributeSet attr) { 51 super(context, attr); 52 } 53 54 @Override onTouchEvent(MotionEvent ev)55 public boolean onTouchEvent(MotionEvent ev) { 56 boolean result = super.onTouchEvent(ev); 57 mTempRect.set(0, 0, 0, 0); 58 for (int i = 0; i < getChildCount(); i++) { 59 View child = getChildAt(i); 60 if (child.getVisibility() == View.VISIBLE) { 61 offsetRectIntoDescendantCoords(child, mTempRect); 62 ev.offsetLocation(mTempRect.left, mTempRect.top); 63 result = child.dispatchTouchEvent(ev) || result; 64 ev.offsetLocation(-mTempRect.left, -mTempRect.top); 65 } 66 } 67 return result; 68 } 69 getSecurityView()70 KeyguardInputView getSecurityView() { 71 View child = getChildAt(getDisplayedChild()); 72 if (child instanceof KeyguardInputView) { 73 return (KeyguardInputView) child; 74 } 75 return null; 76 } 77 getTitle()78 public CharSequence getTitle() { 79 KeyguardInputView ksv = getSecurityView(); 80 if (ksv != null) { 81 return ksv.getTitle(); 82 } 83 return ""; 84 } 85 86 @Override checkLayoutParams(ViewGroup.LayoutParams p)87 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { 88 return p instanceof LayoutParams; 89 } 90 91 @Override generateLayoutParams(ViewGroup.LayoutParams p)92 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { 93 return p instanceof LayoutParams ? new LayoutParams((LayoutParams) p) : new LayoutParams(p); 94 } 95 96 @Override generateLayoutParams(AttributeSet attrs)97 public LayoutParams generateLayoutParams(AttributeSet attrs) { 98 return new LayoutParams(getContext(), attrs); 99 } 100 101 @Override onMeasure(int widthSpec, int heightSpec)102 protected void onMeasure(int widthSpec, int heightSpec) { 103 final int widthMode = MeasureSpec.getMode(widthSpec); 104 final int heightMode = MeasureSpec.getMode(heightSpec); 105 if (DEBUG && widthMode != MeasureSpec.AT_MOST) { 106 Log.w(TAG, "onMeasure: widthSpec " + MeasureSpec.toString(widthSpec) + 107 " should be AT_MOST"); 108 } 109 if (DEBUG && heightMode != MeasureSpec.AT_MOST) { 110 Log.w(TAG, "onMeasure: heightSpec " + MeasureSpec.toString(heightSpec) + 111 " should be AT_MOST"); 112 } 113 114 final int widthSize = MeasureSpec.getSize(widthSpec); 115 final int heightSize = MeasureSpec.getSize(heightSpec); 116 int maxWidth = widthSize; 117 int maxHeight = heightSize; 118 final int count = getChildCount(); 119 for (int i = 0; i < count; i++) { 120 final View child = getChildAt(i); 121 if (child.getVisibility() != View.VISIBLE) continue; 122 123 final LayoutParams lp = (LayoutParams) child.getLayoutParams(); 124 125 if (lp.maxWidth > 0 && lp.maxWidth < maxWidth) { 126 maxWidth = lp.maxWidth; 127 } 128 if (lp.maxHeight > 0 && lp.maxHeight < maxHeight) { 129 maxHeight = lp.maxHeight; 130 } 131 } 132 133 final int wPadding = getPaddingLeft() + getPaddingRight(); 134 final int hPadding = getPaddingTop() + getPaddingBottom(); 135 maxWidth = Math.max(0, maxWidth - wPadding); 136 maxHeight = Math.max(0, maxHeight - hPadding); 137 138 int width = widthMode == MeasureSpec.EXACTLY ? widthSize : 0; 139 int height = heightMode == MeasureSpec.EXACTLY ? heightSize : 0; 140 for (int i = 0; i < count; i++) { 141 final View child = getChildAt(i); 142 final LayoutParams lp = (LayoutParams) child.getLayoutParams(); 143 144 final int childWidthSpec = makeChildMeasureSpec(maxWidth, lp.width); 145 final int childHeightSpec = makeChildMeasureSpec(maxHeight, lp.height); 146 147 child.measure(childWidthSpec, childHeightSpec); 148 149 width = Math.max(width, Math.min(child.getMeasuredWidth(), widthSize - wPadding)); 150 height = Math.max(height, Math.min(child.getMeasuredHeight(), heightSize - hPadding)); 151 } 152 setMeasuredDimension(width + wPadding, height + hPadding); 153 } 154 makeChildMeasureSpec(int maxSize, int childDimen)155 private int makeChildMeasureSpec(int maxSize, int childDimen) { 156 final int mode; 157 final int size; 158 switch (childDimen) { 159 case LayoutParams.WRAP_CONTENT: 160 mode = MeasureSpec.AT_MOST; 161 size = maxSize; 162 break; 163 case LayoutParams.MATCH_PARENT: 164 mode = MeasureSpec.EXACTLY; 165 size = maxSize; 166 break; 167 default: 168 mode = MeasureSpec.EXACTLY; 169 size = Math.min(maxSize, childDimen); 170 break; 171 } 172 return MeasureSpec.makeMeasureSpec(size, mode); 173 } 174 175 public static class LayoutParams extends FrameLayout.LayoutParams { 176 @ViewDebug.ExportedProperty(category = "layout") 177 public int maxWidth; 178 179 @ViewDebug.ExportedProperty(category = "layout") 180 public int maxHeight; 181 LayoutParams(ViewGroup.LayoutParams other)182 public LayoutParams(ViewGroup.LayoutParams other) { 183 super(other); 184 } 185 LayoutParams(LayoutParams other)186 public LayoutParams(LayoutParams other) { 187 super(other); 188 189 maxWidth = other.maxWidth; 190 maxHeight = other.maxHeight; 191 } 192 LayoutParams(Context c, AttributeSet attrs)193 public LayoutParams(Context c, AttributeSet attrs) { 194 super(c, attrs); 195 196 final TypedArray a = c.obtainStyledAttributes(attrs, 197 R.styleable.KeyguardSecurityViewFlipper_Layout, 0, 0); 198 maxWidth = a.getDimensionPixelSize( 199 R.styleable.KeyguardSecurityViewFlipper_Layout_layout_maxWidth, 0); 200 maxHeight = a.getDimensionPixelSize( 201 R.styleable.KeyguardSecurityViewFlipper_Layout_layout_maxHeight, 0); 202 a.recycle(); 203 } 204 205 /** @hide */ 206 @Override encodeProperties(@onNull ViewHierarchyEncoder encoder)207 protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) { 208 super.encodeProperties(encoder); 209 210 encoder.addProperty("layout:maxWidth", maxWidth); 211 encoder.addProperty("layout:maxHeight", maxHeight); 212 } 213 } 214 } 215