1 /* 2 * Copyright (C) 2013 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.camera.ui; 18 19 import android.app.Activity; 20 import android.content.Context; 21 import android.content.res.Configuration; 22 import android.graphics.Rect; 23 import android.util.AttributeSet; 24 import android.view.Gravity; 25 import android.view.View; 26 import android.widget.FrameLayout; 27 import android.widget.RelativeLayout; 28 29 import com.android.camera.Util; 30 import com.android.gallery3d.R; 31 32 public class CameraRootView extends RelativeLayout { 33 34 private int mTopMargin = 0; 35 private int mBottomMargin = 0; 36 private int mLeftMargin = 0; 37 private int mRightMargin = 0; 38 private int mOffset = 0; 39 private Rect mCurrentInsets; 40 public CameraRootView(Context context, AttributeSet attrs) { 41 super(context, attrs); 42 // Layout the window as if we did not need navigation bar 43 setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 44 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION); 45 } 46 47 @Override 48 protected boolean fitSystemWindows(Rect insets) { 49 super.fitSystemWindows(insets); 50 mCurrentInsets = insets; 51 // insets include status bar, navigation bar, etc 52 // In this case, we are only concerned with the size of nav bar 53 if (mOffset > 0) return true; 54 55 if (insets.bottom > 0) { 56 mOffset = insets.bottom; 57 } else if (insets.right > 0) { 58 mOffset = insets.right; 59 } 60 return true; 61 } 62 63 @Override 64 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 65 int rotation = Util.getDisplayRotation((Activity) getContext()); 66 // all the layout code assumes camera device orientation to be portrait 67 // adjust rotation for landscape 68 int orientation = getResources().getConfiguration().orientation; 69 int camOrientation = (rotation % 180 == 0) ? Configuration.ORIENTATION_PORTRAIT 70 : Configuration.ORIENTATION_LANDSCAPE; 71 if (camOrientation != orientation) { 72 rotation = (rotation + 90) % 360; 73 } 74 // calculate margins 75 mLeftMargin = 0; 76 mRightMargin = 0; 77 mBottomMargin = 0; 78 mTopMargin = 0; 79 switch (rotation) { 80 case 0: 81 mBottomMargin += mOffset; 82 break; 83 case 90: 84 mRightMargin += mOffset; 85 break; 86 case 180: 87 mTopMargin += mOffset; 88 break; 89 case 270: 90 mLeftMargin += mOffset; 91 break; 92 } 93 if (mCurrentInsets != null) { 94 if (mCurrentInsets.right > 0) { 95 // navigation bar on the right 96 mRightMargin = mRightMargin > 0 ? mRightMargin : mCurrentInsets.right; 97 } else { 98 // navigation bar on the bottom 99 mBottomMargin = mBottomMargin > 0 ? mBottomMargin : mCurrentInsets.bottom; 100 } 101 } 102 // make sure all the children are resized 103 super.onMeasure(widthMeasureSpec - mLeftMargin - mRightMargin, 104 heightMeasureSpec - mTopMargin - mBottomMargin); 105 106 setMeasuredDimension(widthMeasureSpec, heightMeasureSpec); 107 } 108 109 @Override 110 public void onLayout(boolean changed, int l, int t, int r, int b) { 111 int orientation = getResources().getConfiguration().orientation; 112 // Lay out children 113 for (int i = 0; i < getChildCount(); i++) { 114 View v = getChildAt(i); 115 if (v instanceof CameraControls) { 116 // Lay out camera controls to center on the short side of the screen 117 // so that they stay in place during rotation 118 int width = v.getMeasuredWidth(); 119 int height = v.getMeasuredHeight(); 120 if (orientation == Configuration.ORIENTATION_PORTRAIT) { 121 int left = (l + r - width) / 2; 122 v.layout(left, t + mTopMargin, left + width, b - mBottomMargin); 123 } else { 124 int top = (t + b - height) / 2; 125 v.layout(l + mLeftMargin, top, r - mRightMargin, top + height); 126 } 127 } else { 128 v.layout(l + mLeftMargin, t + mTopMargin, r - mRightMargin, b - mBottomMargin); 129 } 130 } 131 } 132 } 133