Doris Liu | 361bec2 | 2013-05-14 14:37:46 -0700 | [diff] [blame] | 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.os.Debug; |
| 24 | import android.util.AttributeSet; |
| 25 | import android.view.Gravity; |
Doris Liu | 0245bbb | 2013-05-14 14:37:46 -0700 | [diff] [blame^] | 26 | import android.view.MotionEvent; |
Doris Liu | 361bec2 | 2013-05-14 14:37:46 -0700 | [diff] [blame] | 27 | import android.view.View; |
| 28 | import android.widget.FrameLayout; |
| 29 | |
| 30 | import com.android.camera.Util; |
| 31 | import com.android.gallery3d.R; |
| 32 | |
| 33 | public class NewCameraRootView extends FrameLayout { |
| 34 | |
| 35 | private int mTopMargin = 0; |
| 36 | private int mBottomMargin = 0; |
| 37 | private int mLeftMargin = 0; |
| 38 | private int mRightMargin = 0; |
| 39 | private Rect mCurrentInsets; |
| 40 | private int mOffset = 0; |
| 41 | public NewCameraRootView(Context context, AttributeSet attrs) { |
| 42 | super(context, attrs); |
| 43 | setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
| 44 | | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); |
| 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 | setMeasuredDimension(widthMeasureSpec, heightMeasureSpec); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public void onLayout(boolean changed, int l, int t, int r, int b) { |
| 110 | r -= l; |
| 111 | b -= t; |
| 112 | l = 0; |
| 113 | t = 0; |
| 114 | int orientation = getResources().getConfiguration().orientation; |
| 115 | // Lay out children |
| 116 | for (int i = 0; i < getChildCount(); i++) { |
| 117 | View v = getChildAt(i); |
| 118 | if (v instanceof CameraControls) { |
| 119 | // Lay out camera controls to center on the short side of the screen |
| 120 | // so that they stay in place during rotation |
| 121 | int width = v.getMeasuredWidth(); |
| 122 | int height = v.getMeasuredHeight(); |
| 123 | if (orientation == Configuration.ORIENTATION_PORTRAIT) { |
| 124 | int left = (l + r - width) / 2; |
| 125 | v.layout(left, t + mTopMargin, left + width, b - mBottomMargin); |
| 126 | } else { |
| 127 | int top = (t + b - height) / 2; |
| 128 | v.layout(l + mLeftMargin, top, r - mRightMargin, top + height); |
| 129 | } |
| 130 | } else { |
| 131 | v.layout(l + mLeftMargin, t + mTopMargin, r - mRightMargin, b - mBottomMargin); |
| 132 | } |
| 133 | } |
| 134 | } |
Doris Liu | 0245bbb | 2013-05-14 14:37:46 -0700 | [diff] [blame^] | 135 | |
| 136 | @Override |
| 137 | public boolean dispatchTouchEvent(MotionEvent ev) { |
| 138 | //TODO: This scale check is temporary, should be removed once full screen notification |
| 139 | // is implemented |
| 140 | if (((View) getParent()).getScaleX() == 1.0f) { |
| 141 | return super.dispatchTouchEvent(ev); |
| 142 | } else { |
| 143 | return false; |
| 144 | } |
| 145 | } |
Doris Liu | 361bec2 | 2013-05-14 14:37:46 -0700 | [diff] [blame] | 146 | } |