Home | History | Annotate | Download | only in ui
      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.annotation.SuppressLint;
     20 import android.app.Activity;
     21 import android.content.Context;
     22 import android.content.res.Configuration;
     23 import android.graphics.Rect;
     24 import android.hardware.display.DisplayManager;
     25 import android.hardware.display.DisplayManager.DisplayListener;
     26 import android.util.AttributeSet;
     27 import android.view.View;
     28 import android.widget.FrameLayout;
     29 
     30 import com.android.camera.util.ApiHelper;
     31 import com.android.camera.util.CameraUtil;
     32 
     33 @SuppressLint("NewApi")
     34 public class CameraRootView extends FrameLayout {
     35 
     36     private int mTopMargin = 0;
     37     private int mBottomMargin = 0;
     38     private int mLeftMargin = 0;
     39     private int mRightMargin = 0;
     40     private final Rect mCurrentInsets = new Rect(0, 0, 0, 0);
     41     private int mOffset = 0;
     42     private Object mDisplayListener;
     43     private MyDisplayListener mListener;
     44 
     45     public interface MyDisplayListener {
     46         public void onDisplayChanged();
     47     }
     48 
     49     public CameraRootView(Context context, AttributeSet attrs) {
     50         super(context, attrs);
     51         initDisplayListener();
     52     }
     53 
     54     @Override
     55     protected boolean fitSystemWindows(Rect insets) {
     56         // insets include status bar, navigation bar, etc
     57         // In this case, we are only concerned with the size of nav bar
     58         if (mCurrentInsets.equals(insets)) {
     59             // Local copy of the insets is up to date. No need to do anything.
     60             return false;
     61         }
     62 
     63         if (mOffset == 0) {
     64             if (insets.bottom > 0) {
     65                 mOffset = insets.bottom;
     66             } else if (insets.right > 0) {
     67                 mOffset = insets.right;
     68             }
     69         }
     70         mCurrentInsets.set(insets);
     71         // Make sure onMeasure will be called to adapt to the new insets.
     72         requestLayout();
     73         return false;
     74     }
     75 
     76     public void initDisplayListener() {
     77         if (ApiHelper.HAS_DISPLAY_LISTENER) {
     78             mDisplayListener = new DisplayListener() {
     79 
     80                 @Override
     81                 public void onDisplayAdded(int arg0) {}
     82 
     83                 @Override
     84                 public void onDisplayChanged(int arg0) {
     85                     if (mListener != null) {
     86                         mListener.onDisplayChanged();
     87                     }
     88                 }
     89 
     90                 @Override
     91                 public void onDisplayRemoved(int arg0) {}
     92             };
     93         }
     94     }
     95 
     96     public void removeDisplayChangeListener() {
     97         mListener = null;
     98     }
     99 
    100     public void setDisplayChangeListener(MyDisplayListener listener) {
    101         mListener = listener;
    102     }
    103 
    104     @Override
    105     public void onAttachedToWindow() {
    106         super.onAttachedToWindow();
    107         if (ApiHelper.HAS_DISPLAY_LISTENER) {
    108             ((DisplayManager) getContext().getSystemService(Context.DISPLAY_SERVICE))
    109             .registerDisplayListener((DisplayListener) mDisplayListener, null);
    110         }
    111     }
    112 
    113     @Override
    114     public void onDetachedFromWindow () {
    115         super.onDetachedFromWindow();
    116         if (ApiHelper.HAS_DISPLAY_LISTENER) {
    117             ((DisplayManager) getContext().getSystemService(Context.DISPLAY_SERVICE))
    118             .unregisterDisplayListener((DisplayListener) mDisplayListener);
    119         }
    120     }
    121 
    122     @Override
    123     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    124         int rotation = CameraUtil.getDisplayRotation((Activity) getContext());
    125         // all the layout code assumes camera device orientation to be portrait
    126         // adjust rotation for landscape
    127         int orientation = getResources().getConfiguration().orientation;
    128         int camOrientation = (rotation % 180 == 0) ? Configuration.ORIENTATION_PORTRAIT
    129                 : Configuration.ORIENTATION_LANDSCAPE;
    130         if (camOrientation != orientation) {
    131             rotation = (rotation + 90) % 360;
    132         }
    133         // calculate margins
    134         mLeftMargin = 0;
    135         mRightMargin = 0;
    136         mBottomMargin = 0;
    137         mTopMargin = 0;
    138         switch (rotation) {
    139             case 0:
    140                 mBottomMargin += mOffset;
    141                 break;
    142             case 90:
    143                 mRightMargin += mOffset;
    144                 break;
    145             case 180:
    146                 mTopMargin += mOffset;
    147                 break;
    148             case 270:
    149                 mLeftMargin += mOffset;
    150                 break;
    151         }
    152         if (mCurrentInsets != null) {
    153             if (mCurrentInsets.right > 0) {
    154                 // navigation bar on the right
    155                 mRightMargin = mRightMargin > 0 ? mRightMargin : mCurrentInsets.right;
    156             } else {
    157                 // navigation bar on the bottom
    158                 mBottomMargin = mBottomMargin > 0 ? mBottomMargin : mCurrentInsets.bottom;
    159             }
    160         }
    161         // make sure all the children are resized
    162         super.onMeasure(widthMeasureSpec - mLeftMargin - mRightMargin,
    163                 heightMeasureSpec - mTopMargin - mBottomMargin);
    164         setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
    165     }
    166 
    167     @Override
    168     public void onLayout(boolean changed, int l, int t, int r, int b) {
    169         r -= l;
    170         b -= t;
    171         l = 0;
    172         t = 0;
    173         int orientation = getResources().getConfiguration().orientation;
    174         // Lay out children
    175         for (int i = 0; i < getChildCount(); i++) {
    176             View v = getChildAt(i);
    177             if (v instanceof CameraControls) {
    178                 // Lay out camera controls to center on the short side of the screen
    179                 // so that they stay in place during rotation
    180                 int width = v.getMeasuredWidth();
    181                 int height = v.getMeasuredHeight();
    182                 if (orientation == Configuration.ORIENTATION_PORTRAIT) {
    183                     int left = (l + r - width) / 2;
    184                     v.layout(left, t + mTopMargin, left + width, b - mBottomMargin);
    185                 } else {
    186                     int top = (t + b - height) / 2;
    187                     v.layout(l + mLeftMargin, top, r - mRightMargin, top + height);
    188                 }
    189             } else {
    190                 v.layout(l + mLeftMargin, t + mTopMargin, r - mRightMargin, b - mBottomMargin);
    191             }
    192         }
    193     }
    194 }
    195