Rearrange orientation callbacks

pull/13/head
Mattia Iavarone 8 years ago
parent b5cf4d01ca
commit 4a4b5c8dcf
  1. 59
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java
  2. 33
      cameraview/src/main/utils/com/otaliastudios/cameraview/OrientationHelper.java
  3. 2
      cameraview/src/main/views/com/otaliastudios/cameraview/Preview.java

@ -147,34 +147,7 @@ public class CameraView extends FrameLayout {
mapGesture(Gesture.SCROLL_VERTICAL, scrollVerticalGesture); mapGesture(Gesture.SCROLL_VERTICAL, scrollVerticalGesture);
if (!isInEditMode()) { if (!isInEditMode()) {
mOrientationHelper = new OrientationHelper(context) { mOrientationHelper = new OrientationHelper(context, mCameraCallbacks);
private Integer mDisplayOffset;
private Integer mDeviceOrientation;
@Override
public void onDisplayOffsetChanged(int displayOffset) {
mCameraController.onDisplayOffset(displayOffset);
mPreviewImpl.onDisplayOffset(displayOffset);
mDisplayOffset = displayOffset;
send();
}
@Override
protected void onDeviceOrientationChanged(int deviceOrientation) {
mCameraController.onDeviceOrientation(deviceOrientation);
mPreviewImpl.onDeviceOrientation(deviceOrientation);
mDeviceOrientation = deviceOrientation;
send();
}
private void send() {
if (mDeviceOrientation == null) return;
if (mDisplayOffset == null) return;
int value = (mDeviceOrientation + mDisplayOffset) % 360;
mCameraCallbacks.dispatchOnOrientationChanged(value);
}
};
} }
} }
@ -1193,10 +1166,16 @@ public class CameraView extends FrameLayout {
} }
} }
class CameraCallbacks {
class CameraCallbacks implements OrientationHelper.Callbacks {
// Outer listeners
private ArrayList<CameraListener> mListeners = new ArrayList<>(2); private ArrayList<CameraListener> mListeners = new ArrayList<>(2);
// Orientation TODO: move this logic into OrientationHelper
private Integer mDisplayOffset;
private Integer mDeviceOrientation;
CameraCallbacks() {} CameraCallbacks() {}
@ -1359,8 +1338,28 @@ public class CameraView extends FrameLayout {
}); });
} }
@Override
public void onDisplayOffsetChanged(int displayOffset) {
mCameraController.onDisplayOffset(displayOffset);
mDisplayOffset = displayOffset;
if (mDeviceOrientation != null) {
int value = (mDeviceOrientation + mDisplayOffset) % 360;
dispatchOnOrientationChanged(value);
}
}
@Override
public void onDeviceOrientationChanged(int deviceOrientation) {
mCameraController.onDeviceOrientation(deviceOrientation);
mDeviceOrientation = deviceOrientation;
if (mDisplayOffset != null) {
int value = (mDeviceOrientation + mDisplayOffset) % 360;
dispatchOnOrientationChanged(value);
}
}
public void dispatchOnOrientationChanged(final int value) { private void dispatchOnOrientationChanged(final int value) {
mUiHandler.post(new Runnable() { mUiHandler.post(new Runnable() {
@Override @Override
public void run() { public void run() {

@ -2,14 +2,13 @@ package com.otaliastudios.cameraview;
import android.content.Context; import android.content.Context;
import android.hardware.SensorManager; import android.hardware.SensorManager;
import android.support.annotation.NonNull;
import android.util.SparseIntArray; import android.util.SparseIntArray;
import android.view.Display; import android.view.Display;
import android.view.OrientationEventListener; import android.view.OrientationEventListener;
import android.view.Surface; import android.view.Surface;
abstract class OrientationHelper { class OrientationHelper {
private final OrientationEventListener mOrientationEventListener;
private static final SparseIntArray DISPLAY_ORIENTATIONS = new SparseIntArray(); private static final SparseIntArray DISPLAY_ORIENTATIONS = new SparseIntArray();
static { static {
@ -19,13 +18,20 @@ abstract class OrientationHelper {
DISPLAY_ORIENTATIONS.put(Surface.ROTATION_270, 270); DISPLAY_ORIENTATIONS.put(Surface.ROTATION_270, 270);
} }
private final OrientationEventListener mListener;
private final Callbacks mCallbacks;
private Display mDisplay; private Display mDisplay;
private int mLastKnownDisplayOffset = -1; private int mLastKnownDisplayOffset = -1;
private int mLastOrientation = -1; private int mLastOrientation = -1;
OrientationHelper(Context context) { interface Callbacks {
mOrientationEventListener = new OrientationEventListener(context, SensorManager.SENSOR_DELAY_NORMAL) { void onDisplayOffsetChanged(int displayOffset);
void onDeviceOrientationChanged(int deviceOrientation);
}
OrientationHelper(Context context, @NonNull Callbacks callbacks) {
mCallbacks = callbacks;
mListener = new OrientationEventListener(context, SensorManager.SENSOR_DELAY_NORMAL) {
@Override @Override
public void onOrientationChanged(int orientation) { public void onOrientationChanged(int orientation) {
@ -44,7 +50,7 @@ abstract class OrientationHelper {
if (or != mLastOrientation) { if (or != mLastOrientation) {
mLastOrientation = or; mLastOrientation = or;
onDeviceOrientationChanged(mLastOrientation); mCallbacks.onDeviceOrientationChanged(mLastOrientation);
} }
// Let's see if display rotation has changed.. but how could it ever change...?? // Let's see if display rotation has changed.. but how could it ever change...??
@ -53,7 +59,7 @@ abstract class OrientationHelper {
final int offset = mDisplay.getRotation(); final int offset = mDisplay.getRotation();
if (mLastKnownDisplayOffset != offset) { if (mLastKnownDisplayOffset != offset) {
mLastKnownDisplayOffset = offset; mLastKnownDisplayOffset = offset;
onDisplayOffsetChanged(DISPLAY_ORIENTATIONS.get(offset)); mCallbacks.onDisplayOffsetChanged(DISPLAY_ORIENTATIONS.get(offset));
} }
} }
} }
@ -63,18 +69,13 @@ abstract class OrientationHelper {
void enable(Display display) { void enable(Display display) {
mDisplay = display; mDisplay = display;
mOrientationEventListener.enable(); mListener.enable();
mLastKnownDisplayOffset = DISPLAY_ORIENTATIONS.get(display.getRotation()); mLastKnownDisplayOffset = DISPLAY_ORIENTATIONS.get(display.getRotation());
onDisplayOffsetChanged(mLastKnownDisplayOffset); mCallbacks.onDisplayOffsetChanged(mLastKnownDisplayOffset);
} }
void disable() { void disable() {
mOrientationEventListener.disable(); mListener.disable();
mDisplay = null; mDisplay = null;
} }
protected abstract void onDisplayOffsetChanged(int displayOffset);
protected abstract void onDeviceOrientationChanged(int deviceOrientation);
} }

@ -33,8 +33,6 @@ abstract class Preview {
abstract View getView(); abstract View getView();
abstract Class getOutputClass(); abstract Class getOutputClass();
abstract boolean isReady(); abstract boolean isReady();
protected void onDisplayOffset(int displayOrientation) {}
protected void onDeviceOrientation(int deviceOrientation) {}
SurfaceHolder getSurfaceHolder() { SurfaceHolder getSurfaceHolder() {
return null; return null;
} }

Loading…
Cancel
Save