forked from google/cameraview
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better handling for display orientation
Externalize all tasks related to display orientation into DisplayOrientationDetector. Change-Id: I5d7c52f4912e1e5065505d4c9e73cce915c4408d
- Loading branch information
Showing
4 changed files
with
132 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
library/src/main/java/com/google/android/cameraview/DisplayOrientationDetector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright (C) 2016 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.android.cameraview; | ||
|
||
import android.content.Context; | ||
import android.util.SparseIntArray; | ||
import android.view.Display; | ||
import android.view.OrientationEventListener; | ||
import android.view.Surface; | ||
|
||
|
||
/** | ||
* Monitors the value returned from {@link Display#getRotation()}. | ||
*/ | ||
abstract class DisplayOrientationDetector { | ||
|
||
private final OrientationEventListener mOrientationEventListener; | ||
|
||
/** Mapping from Surface.Rotation_n to degrees. */ | ||
private static final SparseIntArray DISPLAY_ORIENTATIONS = new SparseIntArray(); | ||
|
||
static { | ||
DISPLAY_ORIENTATIONS.put(Surface.ROTATION_0, 0); | ||
DISPLAY_ORIENTATIONS.put(Surface.ROTATION_90, 90); | ||
DISPLAY_ORIENTATIONS.put(Surface.ROTATION_180, 180); | ||
DISPLAY_ORIENTATIONS.put(Surface.ROTATION_270, 270); | ||
} | ||
|
||
private Display mDisplay; | ||
|
||
private int mLastKnownDisplayOrientation = 0; | ||
|
||
public DisplayOrientationDetector(Context context) { | ||
mOrientationEventListener = new OrientationEventListener(context) { | ||
|
||
/** This is either Surface.Rotation_0, _90, _180, _270, or -1 (invalid). */ | ||
private int mLastKnownRotation = -1; | ||
|
||
@Override | ||
public void onOrientationChanged(int orientation) { | ||
if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN || | ||
mDisplay == null) { | ||
return; | ||
} | ||
final int rotation = mDisplay.getRotation(); | ||
if (mLastKnownRotation != rotation) { | ||
mLastKnownRotation = rotation; | ||
dispatchOnDisplayOrientationChanged(DISPLAY_ORIENTATIONS.get(rotation)); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
public void enable(Display display) { | ||
mDisplay = display; | ||
mOrientationEventListener.enable(); | ||
// Immediately dispatch the first callback | ||
dispatchOnDisplayOrientationChanged(DISPLAY_ORIENTATIONS.get(display.getRotation())); | ||
} | ||
|
||
public void disable() { | ||
mOrientationEventListener.disable(); | ||
mDisplay = null; | ||
} | ||
|
||
public int getLastKnownDisplayOrientation() { | ||
return mLastKnownDisplayOrientation; | ||
} | ||
|
||
private void dispatchOnDisplayOrientationChanged(int displayOrientation) { | ||
mLastKnownDisplayOrientation = displayOrientation; | ||
onDisplayOrientationChanged(displayOrientation); | ||
} | ||
|
||
/** | ||
* Called when display orientation is changed. | ||
* | ||
* @param displayOrientation One of 0, 90, 180, and 270. | ||
*/ | ||
public abstract void onDisplayOrientationChanged(int displayOrientation); | ||
|
||
} |