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);
if (!isInEditMode()) {
mOrientationHelper = new OrientationHelper(context) {
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);
}
};
mOrientationHelper = new OrientationHelper(context, mCameraCallbacks);
}
}
@ -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);
// Orientation TODO: move this logic into OrientationHelper
private Integer mDisplayOffset;
private Integer mDeviceOrientation;
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() {
@Override
public void run() {

@ -2,14 +2,13 @@ package com.otaliastudios.cameraview;
import android.content.Context;
import android.hardware.SensorManager;
import android.support.annotation.NonNull;
import android.util.SparseIntArray;
import android.view.Display;
import android.view.OrientationEventListener;
import android.view.Surface;
abstract class OrientationHelper {
private final OrientationEventListener mOrientationEventListener;
class OrientationHelper {
private static final SparseIntArray DISPLAY_ORIENTATIONS = new SparseIntArray();
static {
@ -19,13 +18,20 @@ abstract class OrientationHelper {
DISPLAY_ORIENTATIONS.put(Surface.ROTATION_270, 270);
}
private final OrientationEventListener mListener;
private final Callbacks mCallbacks;
private Display mDisplay;
private int mLastKnownDisplayOffset = -1;
private int mLastOrientation = -1;
OrientationHelper(Context context) {
mOrientationEventListener = new OrientationEventListener(context, SensorManager.SENSOR_DELAY_NORMAL) {
interface Callbacks {
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
public void onOrientationChanged(int orientation) {
@ -44,7 +50,7 @@ abstract class OrientationHelper {
if (or != mLastOrientation) {
mLastOrientation = or;
onDeviceOrientationChanged(mLastOrientation);
mCallbacks.onDeviceOrientationChanged(mLastOrientation);
}
// 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();
if (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) {
mDisplay = display;
mOrientationEventListener.enable();
mListener.enable();
mLastKnownDisplayOffset = DISPLAY_ORIENTATIONS.get(display.getRotation());
onDisplayOffsetChanged(mLastKnownDisplayOffset);
mCallbacks.onDisplayOffsetChanged(mLastKnownDisplayOffset);
}
void disable() {
mOrientationEventListener.disable();
mListener.disable();
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 Class getOutputClass();
abstract boolean isReady();
protected void onDisplayOffset(int displayOrientation) {}
protected void onDeviceOrientation(int deviceOrientation) {}
SurfaceHolder getSurfaceHolder() {
return null;
}

Loading…
Cancel
Save