Doris Liu | 048a61c | 2013-04-02 17:48:56 -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.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 | implements RotatableLayout.RotationListener { |
| 34 | |
Doris Liu | 1d8ea53 | 2013-04-24 18:09:04 -0700 | [diff] [blame] | 35 | private int mTopMargin = 0; |
| 36 | private int mBottomMargin = 0; |
| 37 | private int mLeftMargin = 0; |
| 38 | private int mRightMargin = 0; |
Doris Liu | 048a61c | 2013-04-02 17:48:56 -0700 | [diff] [blame] | 39 | private int mOffset = 0; |
| 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 | // insets include status bar, navigation bar, etc |
| 51 | // In this case, we are only concerned with the size of nav bar |
Doris Liu | 1d8ea53 | 2013-04-24 18:09:04 -0700 | [diff] [blame] | 52 | if (mOffset > 0) { |
| 53 | // Add margin if necessary to the view to ensure nothing is covered |
| 54 | // by navigation bar |
| 55 | FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams(); |
| 56 | int right, bottom; |
| 57 | if (insets.right > 0) { |
| 58 | // navigation bar on the right |
| 59 | right = mRightMargin > 0 ? 0 : insets.right; |
| 60 | bottom = 0; |
| 61 | } else { |
| 62 | // navigation bar on the bottom |
| 63 | bottom = mBottomMargin > 0 ? 0 : insets.bottom; |
| 64 | right = 0; |
| 65 | } |
| 66 | lp.setMargins(mLeftMargin, mTopMargin, mRightMargin + right, mBottomMargin + bottom); |
| 67 | return true; |
| 68 | } |
Doris Liu | 048a61c | 2013-04-02 17:48:56 -0700 | [diff] [blame] | 69 | FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams(); |
| 70 | if (insets.bottom > 0) { |
| 71 | mOffset = insets.bottom; |
| 72 | } else if (insets.right > 0) { |
| 73 | mOffset = insets.right; |
| 74 | } |
| 75 | Configuration config = getResources().getConfiguration(); |
| 76 | if (config.orientation == Configuration.ORIENTATION_PORTRAIT) { |
Doris Liu | 1d8ea53 | 2013-04-24 18:09:04 -0700 | [diff] [blame] | 77 | mBottomMargin = mOffset; |
Doris Liu | 048a61c | 2013-04-02 17:48:56 -0700 | [diff] [blame] | 78 | } else if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) { |
Doris Liu | 1d8ea53 | 2013-04-24 18:09:04 -0700 | [diff] [blame] | 79 | mRightMargin = mOffset; |
Doris Liu | 048a61c | 2013-04-02 17:48:56 -0700 | [diff] [blame] | 80 | } |
Doris Liu | 1d8ea53 | 2013-04-24 18:09:04 -0700 | [diff] [blame] | 81 | lp.setMargins( mLeftMargin, mTopMargin, mRightMargin, mBottomMargin); |
Doris Liu | 048a61c | 2013-04-02 17:48:56 -0700 | [diff] [blame] | 82 | CameraControls controls = (CameraControls) findViewById(R.id.camera_controls); |
| 83 | if (controls != null) { |
| 84 | controls.setRotationListener(this); |
| 85 | controls.adjustControlsToRightPosition(); |
| 86 | } |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public void onRotation(int rotation) { |
| 92 | FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams(); |
Doris Liu | 1d8ea53 | 2013-04-24 18:09:04 -0700 | [diff] [blame] | 93 | int b = mBottomMargin; |
| 94 | int t = mTopMargin; |
| 95 | int l = mLeftMargin; |
| 96 | int r = mRightMargin; |
Doris Liu | 048a61c | 2013-04-02 17:48:56 -0700 | [diff] [blame] | 97 | rotation = (rotation + 360) % 360; |
| 98 | if (rotation == 90) { |
| 99 | lp.setMargins(b, l, t, r); |
| 100 | } else if (rotation == 270) { |
| 101 | lp.setMargins(t, r, b, l); |
| 102 | } else if (rotation == 180) { |
| 103 | lp.setMargins(r, b, l, t); |
| 104 | } |
Doris Liu | 1d8ea53 | 2013-04-24 18:09:04 -0700 | [diff] [blame] | 105 | mLeftMargin = lp.leftMargin; |
| 106 | mTopMargin = lp.topMargin; |
| 107 | mRightMargin = lp.rightMargin; |
| 108 | mBottomMargin = lp.bottomMargin; |
Doris Liu | 048a61c | 2013-04-02 17:48:56 -0700 | [diff] [blame] | 109 | } |
| 110 | } |