1414import android .view .Surface ;
1515import android .view .WindowManager ;
1616import androidx .annotation .NonNull ;
17- import androidx .annotation .Nullable ;
1817import androidx .annotation .VisibleForTesting ;
1918import io .flutter .embedding .engine .systemchannels .PlatformChannel ;
2019import io .flutter .embedding .engine .systemchannels .PlatformChannel .DeviceOrientation ;
@@ -85,120 +84,6 @@ public void stop() {
8584 broadcastReceiver = null ;
8685 }
8786
88- /**
89- * Returns the device's photo orientation in degrees based on the sensor orientation and the last
90- * known UI orientation.
91- *
92- * <p>Returns one of 0, 90, 180 or 270.
93- *
94- * @return The device's photo orientation in degrees.
95- */
96- public int getPhotoOrientation () {
97- return this .getPhotoOrientation (this .lastOrientation );
98- }
99-
100- /**
101- * Returns the device's photo orientation in degrees based on the sensor orientation and the
102- * supplied {@link PlatformChannel.DeviceOrientation} value.
103- *
104- * <p>Returns one of 0, 90, 180 or 270.
105- *
106- * @param orientation The {@link PlatformChannel.DeviceOrientation} value that is to be converted
107- * into degrees.
108- * @return The device's photo orientation in degrees.
109- */
110- public int getPhotoOrientation (@ Nullable PlatformChannel .DeviceOrientation orientation ) {
111- int angle = 0 ;
112- // Fallback to device orientation when the orientation value is null.
113- if (orientation == null ) {
114- orientation = getUIOrientation ();
115- }
116-
117- switch (orientation ) {
118- case PORTRAIT_UP :
119- angle = 90 ;
120- break ;
121- case PORTRAIT_DOWN :
122- angle = 270 ;
123- break ;
124- case LANDSCAPE_LEFT :
125- angle = isFrontFacing ? 180 : 0 ;
126- break ;
127- case LANDSCAPE_RIGHT :
128- angle = isFrontFacing ? 0 : 180 ;
129- break ;
130- }
131-
132- // Sensor orientation is 90 for most devices, or 270 for some devices (eg. Nexus 5X).
133- // This has to be taken into account so the JPEG is rotated properly.
134- // For devices with orientation of 90, this simply returns the mapping from ORIENTATIONS.
135- // For devices with orientation of 270, the JPEG is rotated 180 degrees instead.
136- return (angle + sensorOrientation + 270 ) % 360 ;
137- }
138-
139- /**
140- * Returns the device's video orientation in clockwise degrees based on the sensor orientation and
141- * the last known UI orientation.
142- *
143- * <p>Returns one of 0, 90, 180 or 270.
144- *
145- * @return The device's video orientation in clockwise degrees.
146- */
147- public int getVideoOrientation () {
148- return this .getVideoOrientation (this .lastOrientation );
149- }
150-
151- /**
152- * Returns the device's video orientation in clockwise degrees based on the sensor orientation and
153- * the supplied {@link PlatformChannel.DeviceOrientation} value.
154- *
155- * <p>Returns one of 0, 90, 180 or 270.
156- *
157- * <p>More details can be found in the official Android documentation:
158- * https://developer.android.com/reference/android/media/MediaRecorder#setOrientationHint(int)
159- *
160- * <p>See also:
161- * https://developer.android.com/training/camera2/camera-preview-large-screens#orientation_calculation
162- *
163- * @param orientation The {@link PlatformChannel.DeviceOrientation} value that is to be converted
164- * into degrees.
165- * @return The device's video orientation in clockwise degrees.
166- */
167- public int getVideoOrientation (@ Nullable PlatformChannel .DeviceOrientation orientation ) {
168- int angle = 0 ;
169-
170- // Fallback to device orientation when the orientation value is null.
171- if (orientation == null ) {
172- orientation = getUIOrientation ();
173- }
174-
175- switch (orientation ) {
176- case PORTRAIT_UP :
177- angle = 0 ;
178- break ;
179- case PORTRAIT_DOWN :
180- angle = 180 ;
181- break ;
182- case LANDSCAPE_LEFT :
183- angle = 270 ;
184- break ;
185- case LANDSCAPE_RIGHT :
186- angle = 90 ;
187- break ;
188- }
189-
190- if (isFrontFacing ) {
191- angle *= -1 ;
192- }
193-
194- return (angle + sensorOrientation + 360 ) % 360 ;
195- }
196-
197- /** @return the last received UI orientation. */
198- public @ Nullable PlatformChannel .DeviceOrientation getLastUIOrientation () {
199- return this .lastOrientation ;
200- }
201-
20287 /**
20388 * Handles orientation changes based on change events triggered by the OrientationIntentFilter.
20489 *
@@ -241,7 +126,7 @@ static void handleOrientationChange(
241126 @ SuppressWarnings ("deprecation" )
242127 @ VisibleForTesting
243128 PlatformChannel .DeviceOrientation getUIOrientation () {
244- final int rotation = getDisplay (). getRotation ();
129+ final int rotation = getDefaultRotation ();
245130 final int orientation = activity .getResources ().getConfiguration ().orientation ;
246131
247132 switch (orientation ) {
@@ -265,57 +150,18 @@ PlatformChannel.DeviceOrientation getUIOrientation() {
265150 }
266151
267152 /**
268- * Calculates the sensor orientation based on the supplied angle.
269- *
270- * <p>This method is visible for testing purposes only and should never be used outside this
271- * class.
272- *
273- * @param angle Orientation angle.
274- * @return The sensor orientation based on the supplied angle.
275- */
276- @ VisibleForTesting
277- PlatformChannel .DeviceOrientation calculateSensorOrientation (int angle ) {
278- final int tolerance = 45 ;
279- angle += tolerance ;
280-
281- // Orientation is 0 in the default orientation mode. This is portrait-mode for phones
282- // and landscape for tablets. We have to compensate for this by calculating the default
283- // orientation, and apply an offset accordingly.
284- int defaultDeviceOrientation = getDeviceDefaultOrientation ();
285- if (defaultDeviceOrientation == Configuration .ORIENTATION_LANDSCAPE ) {
286- angle += 90 ;
287- }
288- // Determine the orientation
289- angle = angle % 360 ;
290- return new PlatformChannel .DeviceOrientation [] {
291- PlatformChannel .DeviceOrientation .PORTRAIT_UP ,
292- PlatformChannel .DeviceOrientation .LANDSCAPE_LEFT ,
293- PlatformChannel .DeviceOrientation .PORTRAIT_DOWN ,
294- PlatformChannel .DeviceOrientation .LANDSCAPE_RIGHT ,
295- }
296- [angle / 90 ];
297- }
298-
299- /**
300- * Gets the default orientation of the device.
153+ * Gets default capture rotation for CameraX {@code UseCase}s.
301154 *
302- * <p>This method is visible for testing purposes only and should never be used outside this
303- * class.
155+ * <p>See
156+ * https://developer.android.com/reference/androidx/camera/core/ImageCapture#setTargetRotation(int),
157+ * for instance.
304158 *
305- * @return The default orientation of the device.
159+ * @return The rotation of the screen from its "natural" orientation; one of {@code
160+ * Surface.ROTATION_0}, {@code Surface.ROTATION_90}, {@code Surface.ROTATION_180}, {@code
161+ * Surface.ROTATION_270}
306162 */
307- @ VisibleForTesting
308- int getDeviceDefaultOrientation () {
309- Configuration config = activity .getResources ().getConfiguration ();
310- int rotation = getDisplay ().getRotation ();
311- if (((rotation == Surface .ROTATION_0 || rotation == Surface .ROTATION_180 )
312- && config .orientation == Configuration .ORIENTATION_LANDSCAPE )
313- || ((rotation == Surface .ROTATION_90 || rotation == Surface .ROTATION_270 )
314- && config .orientation == Configuration .ORIENTATION_PORTRAIT )) {
315- return Configuration .ORIENTATION_LANDSCAPE ;
316- } else {
317- return Configuration .ORIENTATION_PORTRAIT ;
318- }
163+ int getDefaultRotation () {
164+ return getDisplay ().getRotation ();
319165 }
320166
321167 /**
0 commit comments