1 /* 2 * Copyright (C) 2015 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 package com.android.packageinstaller.permission.ui; 17 18 import android.content.Context; 19 import android.util.AttributeSet; 20 import android.view.View; 21 import android.view.ViewGroup; 22 23 public class ManualLayoutFrame extends ViewGroup { 24 private int mContentBottom; 25 private int mWidth; 26 27 public ManualLayoutFrame(Context context, AttributeSet attrs) { 28 super(context, attrs); 29 } 30 31 public void onConfigurationChanged() { 32 mContentBottom = 0; 33 mWidth = 0; 34 } 35 36 @Override 37 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 38 if (mWidth != 0) { 39 int newWidth = mWidth; 40 final int widthMode = MeasureSpec.getMode(widthMeasureSpec); 41 switch (widthMode) { 42 case MeasureSpec.AT_MOST: { 43 newWidth = Math.min(mWidth, MeasureSpec.getSize(widthMeasureSpec)); 44 } break; 45 case MeasureSpec.EXACTLY: { 46 newWidth = MeasureSpec.getSize(widthMeasureSpec); 47 } break; 48 } 49 if (newWidth != mWidth) { 50 mWidth = newWidth; 51 } 52 widthMeasureSpec = MeasureSpec.makeMeasureSpec(mWidth, MeasureSpec.EXACTLY); 53 } 54 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 55 if (mWidth == 0) { 56 mWidth = getMeasuredWidth(); 57 } 58 59 measureChild(getChildAt(0), widthMeasureSpec, heightMeasureSpec); 60 } 61 62 @Override 63 protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 64 View content = getChildAt(0); 65 if (mContentBottom == 0) { 66 mContentBottom = (getMeasuredHeight() + content.getMeasuredHeight()) / 2; 67 } 68 final int contentLeft = (getMeasuredWidth() - content.getMeasuredWidth()) / 2; 69 final int contentTop = mContentBottom - content.getMeasuredHeight(); 70 final int contentRight = contentLeft + content.getMeasuredWidth(); 71 content.layout(contentLeft, contentTop, contentRight, mContentBottom); 72 } 73 } 74