From 44ab147ed213bf6a1aa6c62e08c20d7fca03503d Mon Sep 17 00:00:00 2001 From: Doris Liu Date: Fri, 7 Jun 2013 18:04:57 -0700 Subject: Update camera orientation when device is flipped Bug: 8963053 Change-Id: I909793a7da3903a080a19cd7e271b3720df4ec0a --- src/com/android/camera/NewPhotoModule.java | 7 ++ src/com/android/camera/NewPhotoUI.java | 14 +++- src/com/android/camera/NewVideoModule.java | 8 +++ src/com/android/camera/NewVideoUI.java | 13 +++- src/com/android/camera/PhotoController.java | 1 + src/com/android/camera/PhotoModule.java | 1 + src/com/android/camera/VideoController.java | 2 + src/com/android/camera/VideoModule.java | 3 + src/com/android/camera/ui/CameraControls.java | 85 ------------------------ src/com/android/camera/ui/NewCameraRootView.java | 50 ++++++++++++++ src/com/android/camera/ui/RotatableLayout.java | 52 +++++++++++++-- 11 files changed, 142 insertions(+), 94 deletions(-) (limited to 'src/com/android/camera') diff --git a/src/com/android/camera/NewPhotoModule.java b/src/com/android/camera/NewPhotoModule.java index e5d50f383..81e9f6d16 100644 --- a/src/com/android/camera/NewPhotoModule.java +++ b/src/com/android/camera/NewPhotoModule.java @@ -1388,6 +1388,13 @@ public class NewPhotoModule setDisplayOrientation(); } + @Override + public void updateCameraOrientation() { + if (mDisplayRotation != Util.getDisplayRotation(mActivity)) { + setDisplayOrientation(); + } + } + @Override public void onActivityResult( int requestCode, int resultCode, Intent data) { diff --git a/src/com/android/camera/NewPhotoUI.java b/src/com/android/camera/NewPhotoUI.java index cd93a7ea9..b9bc4d31a 100644 --- a/src/com/android/camera/NewPhotoUI.java +++ b/src/com/android/camera/NewPhotoUI.java @@ -43,12 +43,14 @@ import android.widget.Toast; import com.android.camera.CameraPreference.OnPreferenceChangedListener; import com.android.camera.FocusOverlayManager.FocusUI; import com.android.camera.ui.AbstractSettingPopup; +import com.android.camera.ui.CameraControls; import com.android.camera.ui.CameraSwitcher.CameraSwitchListener; import com.android.camera.ui.CountDownView; import com.android.camera.ui.CountDownView.OnCountDownFinishedListener; import com.android.camera.ui.CameraSwitcher; import com.android.camera.ui.FaceView; import com.android.camera.ui.FocusIndicator; +import com.android.camera.ui.NewCameraRootView; import com.android.camera.ui.PieRenderer; import com.android.camera.ui.PieRenderer.PieListener; import com.android.camera.ui.RenderOverlay; @@ -62,7 +64,7 @@ import java.util.List; public class NewPhotoUI implements PieListener, NewPreviewGestures.SingleTapListener, FocusUI, TextureView.SurfaceTextureListener, - LocationManager.Listener, + LocationManager.Listener, NewCameraRootView.MyDisplayListener, FaceDetectionListener { private static final String TAG = "CAM_UI"; @@ -88,7 +90,7 @@ public class NewPhotoUI implements PieListener, private View mBlocker; private NewPhotoMenu mMenu; private CameraSwitcher mSwitcher; - private View mCameraControls; + private CameraControls mCameraControls; // Small indicators which show the camera settings in the viewfinder. private OnScreenIndicators mOnScreenIndicators; @@ -180,7 +182,8 @@ public class NewPhotoUI implements PieListener, (SurfaceTextureSizeChangedListener) mFaceView); } } - mCameraControls = mRootView.findViewById(R.id.camera_controls); + mCameraControls = (CameraControls) mRootView.findViewById(R.id.camera_controls); + ((NewCameraRootView) mRootView).setDisplayChangeListener(this); } public void onScreenSizeChanged(int width, int height, int previewWidth, int previewHeight) { @@ -780,4 +783,9 @@ public class NewPhotoUI implements PieListener, mFaceView.setFaces(faces); } + public void onDisplayChanged() { + mCameraControls.checkLayoutFlip(); + mController.updateCameraOrientation(); + } + } diff --git a/src/com/android/camera/NewVideoModule.java b/src/com/android/camera/NewVideoModule.java index d613eb4e5..3649dcbd1 100644 --- a/src/com/android/camera/NewVideoModule.java +++ b/src/com/android/camera/NewVideoModule.java @@ -799,6 +799,14 @@ public class NewVideoModule implements NewCameraModule, } } + @Override + public void updateCameraOrientation() { + if (mMediaRecorderRecording) return; + if (mDisplayRotation != Util.getDisplayRotation(mActivity)) { + setDisplayOrientation(); + } + } + @Override public int onZoomChanged(int index) { // Not useful to change zoom value when the activity is paused. diff --git a/src/com/android/camera/NewVideoUI.java b/src/com/android/camera/NewVideoUI.java index 915269032..3abda228e 100644 --- a/src/com/android/camera/NewVideoUI.java +++ b/src/com/android/camera/NewVideoUI.java @@ -42,7 +42,9 @@ import android.widget.TextView; import com.android.camera.CameraPreference.OnPreferenceChangedListener; import com.android.camera.ui.AbstractSettingPopup; +import com.android.camera.ui.CameraControls; import com.android.camera.ui.CameraSwitcher; +import com.android.camera.ui.NewCameraRootView; import com.android.camera.ui.PieRenderer; import com.android.camera.ui.RenderOverlay; import com.android.camera.ui.RotateLayout; @@ -55,6 +57,7 @@ import java.util.List; public class NewVideoUI implements PieRenderer.PieListener, NewPreviewGestures.SingleTapListener, + NewCameraRootView.MyDisplayListener, SurfaceTextureListener, SurfaceHolder.Callback { private final static String TAG = "CAM_VideoUI"; private static final int UPDATE_TRANSFORM_MATRIX = 1; @@ -76,7 +79,7 @@ public class NewVideoUI implements PieRenderer.PieListener, private RenderOverlay mRenderOverlay; private PieRenderer mPieRenderer; private NewVideoMenu mVideoMenu; - private View mCameraControls; + private CameraControls mCameraControls; private AbstractSettingPopup mPopup; private ZoomRenderer mZoomRenderer; private NewPreviewGestures mGestures; @@ -139,6 +142,7 @@ public class NewVideoUI implements PieRenderer.PieListener, mTextureView = (TextureView) mRootView.findViewById(R.id.preview_content); mTextureView.setSurfaceTextureListener(this); mRootView.addOnLayoutChangeListener(mLayoutListener); + ((NewCameraRootView) mRootView).setDisplayChangeListener(this); mShutterButton = (ShutterButton) mRootView.findViewById(R.id.shutter_button); mSwitcher = (CameraSwitcher) mRootView.findViewById(R.id.camera_switcher); mSwitcher.setCurrentIndex(1); @@ -167,7 +171,7 @@ public class NewVideoUI implements PieRenderer.PieListener, } }); - mCameraControls = mActivity.findViewById(R.id.camera_controls); + mCameraControls = (CameraControls) mActivity.findViewById(R.id.camera_controls); mOnScreenIndicators = new OnScreenIndicators(mActivity, mActivity.findViewById(R.id.on_screen_indicators)); mOnScreenIndicators.resetToDefault(); @@ -613,6 +617,11 @@ public class NewVideoUI implements PieRenderer.PieListener, return mTextureView.getVisibility() == View.VISIBLE; } + public void onDisplayChanged() { + mCameraControls.checkLayoutFlip(); + mController.updateCameraOrientation(); + } + /** * Enable or disable the preview thumbnail for click events. */ diff --git a/src/com/android/camera/PhotoController.java b/src/com/android/camera/PhotoController.java index b76022e57..47c17218a 100644 --- a/src/com/android/camera/PhotoController.java +++ b/src/com/android/camera/PhotoController.java @@ -59,4 +59,5 @@ public interface PhotoController extends OnShutterButtonListener { public void onScreenSizeChanged(int width, int height, int previewWidth, int previewHeight); + public void updateCameraOrientation(); } diff --git a/src/com/android/camera/PhotoModule.java b/src/com/android/camera/PhotoModule.java index c3d152716..ecda6c6e3 100644 --- a/src/com/android/camera/PhotoModule.java +++ b/src/com/android/camera/PhotoModule.java @@ -2101,4 +2101,5 @@ public class PhotoModule mHeading += 360; } } + public void updateCameraOrientation() {} } diff --git a/src/com/android/camera/VideoController.java b/src/com/android/camera/VideoController.java index 474f521de..b53dec616 100644 --- a/src/com/android/camera/VideoController.java +++ b/src/com/android/camera/VideoController.java @@ -33,4 +33,6 @@ public interface VideoController extends OnShutterButtonListener { public void onSingleTapUp(View view, int x, int y); public void stopPreview(); + + public void updateCameraOrientation(); } diff --git a/src/com/android/camera/VideoModule.java b/src/com/android/camera/VideoModule.java index 2c654fc29..977986d1f 100644 --- a/src/com/android/camera/VideoModule.java +++ b/src/com/android/camera/VideoModule.java @@ -2316,4 +2316,7 @@ public class VideoModule implements CameraModule, public void onMediaSaveServiceConnected(MediaSaveService s) { // do nothing. } + + @Override + public void updateCameraOrientation() {} } diff --git a/src/com/android/camera/ui/CameraControls.java b/src/com/android/camera/ui/CameraControls.java index 7940ae0d9..7fa6890a7 100644 --- a/src/com/android/camera/ui/CameraControls.java +++ b/src/com/android/camera/ui/CameraControls.java @@ -20,8 +20,6 @@ import android.app.Activity; import android.content.Context; import android.content.res.Configuration; import android.graphics.Rect; -import android.hardware.display.DisplayManager; -import android.hardware.display.DisplayManager.DisplayListener; import android.util.AttributeSet; import android.view.View; import android.widget.FrameLayout; @@ -40,44 +38,13 @@ public class CameraControls extends RotatableLayout { private View mMenu; private View mIndicators; private View mPreview; - private Object mDisplayListener = null; - private int mLastRotation = 0; public CameraControls(Context context, AttributeSet attrs) { super(context, attrs); - initDisplayListener(); } public CameraControls(Context context) { super(context); - initDisplayListener(); - } - - public void initDisplayListener() { - if (ApiHelper.HAS_DISPLAY_LISTENER) { - mDisplayListener = new DisplayListener() { - - @Override - public void onDisplayAdded(int arg0) {} - - @Override - public void onDisplayChanged(int arg0) { - checkLayoutFlip(); - } - - @Override - public void onDisplayRemoved(int arg0) {} - }; - } - } - - private void checkLayoutFlip() { - int currentRotation = Util.getDisplayRotation((Activity) getContext()); - if ((currentRotation - mLastRotation + 360) % 360 == 180) { - mLastRotation = currentRotation; - flipChildren(); - getParent().requestLayout(); - } } @Override @@ -91,37 +58,8 @@ public class CameraControls extends RotatableLayout { mPreview = findViewById(R.id.preview_thumb); } - @Override - public void onAttachedToWindow() { - super.onAttachedToWindow(); - adjustControlsToRightPosition(); - mLastRotation = Util.getDisplayRotation((Activity) getContext()); - if (ApiHelper.HAS_DISPLAY_LISTENER) { - ((DisplayManager) getContext().getSystemService(Context.DISPLAY_SERVICE)) - .registerDisplayListener((DisplayListener) mDisplayListener, null); - } - } - - @Override - public void onWindowVisibilityChanged(int visibility) { - if (visibility == View.VISIBLE) { - // Make sure when coming back from onPause, the layout is rotated correctly - checkLayoutFlip(); - } - } - - @Override - public void onDetachedFromWindow () { - super.onDetachedFromWindow(); - if (ApiHelper.HAS_DISPLAY_LISTENER) { - ((DisplayManager) getContext().getSystemService(Context.DISPLAY_SERVICE)) - .unregisterDisplayListener((DisplayListener) mDisplayListener); - } - } - @Override public void onLayout(boolean changed, int l, int t, int r, int b) { - mLastRotation = Util.getDisplayRotation((Activity) getContext()); int orientation = getResources().getConfiguration().orientation; int size = getResources().getDimensionPixelSize(R.dimen.camera_controls_size); int rotation = getUnifiedRotation(); @@ -168,19 +106,6 @@ public class CameraControls extends RotatableLayout { } } - private int getUnifiedRotation() { - // all the layout code assumes camera device orientation to be portrait - // adjust rotation for landscape - int orientation = getResources().getConfiguration().orientation; - int rotation = Util.getDisplayRotation((Activity) getContext()); - int camOrientation = (rotation % 180 == 0) ? Configuration.ORIENTATION_PORTRAIT - : Configuration.ORIENTATION_LANDSCAPE; - if (camOrientation != orientation) { - return (rotation + 90) % 360; - } - return rotation; - } - private void center(View v, int l, int t, int r, int b, int orientation, int rotation, Rect result) { FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) v.getLayoutParams(); int tw = lp.leftMargin + v.getMeasuredWidth() + lp.rightMargin; @@ -313,16 +238,6 @@ public class CameraControls extends RotatableLayout { v.layout(r - v.getMeasuredWidth() - mr, t + mt, r - mr, t + mt + v.getMeasuredHeight()); } - // In reverse landscape and reverse portrait, camera controls will be laid out - // on the wrong side of the screen. We need to make adjustment to move the controls - // to the USB side - public void adjustControlsToRightPosition() { - int orientation = getUnifiedRotation(); - if (orientation >= 180) { - flipChildren(); - } - } - private void adjustBackground() { int rotation = getUnifiedRotation(); // remove current drawable and reset rotation diff --git a/src/com/android/camera/ui/NewCameraRootView.java b/src/com/android/camera/ui/NewCameraRootView.java index bf8192564..710d6d304 100644 --- a/src/com/android/camera/ui/NewCameraRootView.java +++ b/src/com/android/camera/ui/NewCameraRootView.java @@ -20,6 +20,8 @@ import android.app.Activity; import android.content.Context; import android.content.res.Configuration; import android.graphics.Rect; +import android.hardware.display.DisplayManager; +import android.hardware.display.DisplayManager.DisplayListener; import android.util.AttributeSet; import android.view.Gravity; import android.view.MotionEvent; @@ -28,6 +30,7 @@ import android.widget.FrameLayout; import com.android.camera.Util; import com.android.gallery3d.R; +import com.android.gallery3d.common.ApiHelper; public class NewCameraRootView extends FrameLayout { @@ -37,8 +40,15 @@ public class NewCameraRootView extends FrameLayout { private int mRightMargin = 0; private Rect mCurrentInsets; private int mOffset = 0; + private Object mDisplayListener; + private MyDisplayListener mListener; + public interface MyDisplayListener { + public void onDisplayChanged(); + } + public NewCameraRootView(Context context, AttributeSet attrs) { super(context, attrs); + initDisplayListener(); setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); } @@ -59,6 +69,46 @@ public class NewCameraRootView extends FrameLayout { return true; } + public void initDisplayListener() { + if (ApiHelper.HAS_DISPLAY_LISTENER) { + mDisplayListener = new DisplayListener() { + + @Override + public void onDisplayAdded(int arg0) {} + + @Override + public void onDisplayChanged(int arg0) { + mListener.onDisplayChanged(); + } + + @Override + public void onDisplayRemoved(int arg0) {} + }; + } + } + + public void setDisplayChangeListener(MyDisplayListener listener) { + mListener = listener; + } + + @Override + public void onAttachedToWindow() { + super.onAttachedToWindow(); + if (ApiHelper.HAS_DISPLAY_LISTENER) { + ((DisplayManager) getContext().getSystemService(Context.DISPLAY_SERVICE)) + .registerDisplayListener((DisplayListener) mDisplayListener, null); + } + } + + @Override + public void onDetachedFromWindow () { + super.onDetachedFromWindow(); + if (ApiHelper.HAS_DISPLAY_LISTENER) { + ((DisplayManager) getContext().getSystemService(Context.DISPLAY_SERVICE)) + .unregisterDisplayListener((DisplayListener) mDisplayListener); + } + } + @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int rotation = Util.getDisplayRotation((Activity) getContext()); diff --git a/src/com/android/camera/ui/RotatableLayout.java b/src/com/android/camera/ui/RotatableLayout.java index 8355c8826..965d62a90 100644 --- a/src/com/android/camera/ui/RotatableLayout.java +++ b/src/com/android/camera/ui/RotatableLayout.java @@ -65,9 +65,11 @@ public class RotatableLayout extends FrameLayout { mPrevRotation = Util.getDisplayRotation((Activity) getContext()); // check if there is any rotation before the view is attached to window int currentOrientation = getResources().getConfiguration().orientation; - if (mInitialOrientation == currentOrientation) { + int orientation = getUnifiedRotation(); + if (mInitialOrientation == currentOrientation && orientation < 180) { return; } + if (mInitialOrientation == Configuration.ORIENTATION_LANDSCAPE && currentOrientation == Configuration.ORIENTATION_PORTRAIT) { rotateLayout(true); @@ -75,19 +77,62 @@ public class RotatableLayout extends FrameLayout { && currentOrientation == Configuration.ORIENTATION_LANDSCAPE) { rotateLayout(false); } + // In reverse landscape and reverse portrait, camera controls will be laid out + // on the wrong side of the screen. We need to make adjustment to move the controls + // to the USB side + if (orientation >= 180) { + flipChildren(); + } + } + + protected int getUnifiedRotation() { + // all the layout code assumes camera device orientation to be portrait + // adjust rotation for landscape + int orientation = getResources().getConfiguration().orientation; + int rotation = Util.getDisplayRotation((Activity) getContext()); + int camOrientation = (rotation % 180 == 0) ? Configuration.ORIENTATION_PORTRAIT + : Configuration.ORIENTATION_LANDSCAPE; + if (camOrientation != orientation) { + return (rotation + 90) % 360; + } + return rotation; + } + + public void checkLayoutFlip() { + int currentRotation = Util.getDisplayRotation((Activity) getContext()); + if ((currentRotation - mPrevRotation + 360) % 360 == 180) { + mPrevRotation = currentRotation; + flipChildren(); + getParent().requestLayout(); + } + } + + @Override + public void onWindowVisibilityChanged(int visibility) { + if (visibility == View.VISIBLE) { + // Make sure when coming back from onPause, the layout is rotated correctly + checkLayoutFlip(); + } } @Override public void onConfigurationChanged(Configuration config) { super.onConfigurationChanged(config); int rotation = Util.getDisplayRotation((Activity) getContext()); - if ((rotation - mPrevRotation + 360) % 180 == 0) { + int diff = (rotation - mPrevRotation + 360) % 360; + if ( diff == 0) { + // No rotation + return; + } else if (diff == 180) { + // 180-degree rotation mPrevRotation = rotation; + flipChildren(); return; } + // 90 or 270-degree rotation boolean clockwise = isClockWiseRotation(mPrevRotation, rotation); - rotateLayout(clockwise); mPrevRotation = rotation; + rotateLayout(clockwise); } protected void rotateLayout(boolean clockwise) { @@ -113,7 +158,6 @@ public class RotatableLayout extends FrameLayout { } protected void flipChildren() { - mPrevRotation = Util.getDisplayRotation((Activity) getContext()); int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View child = getChildAt(i); -- cgit v1.2.3