1
1
package io .flutter .plugins .camera ;
2
2
3
+ import android .app .Activity ;
3
4
import android .content .BroadcastReceiver ;
4
5
import android .content .Context ;
5
6
import android .content .Intent ;
6
7
import android .content .IntentFilter ;
7
8
import android .content .res .Configuration ;
8
9
import android .hardware .SensorManager ;
10
+ import android .os .Build .VERSION ;
11
+ import android .os .Build .VERSION_CODES ;
9
12
import android .provider .Settings ;
13
+ import android .view .Display ;
10
14
import android .view .OrientationEventListener ;
11
15
import android .view .Surface ;
12
16
import android .view .WindowManager ;
@@ -17,7 +21,7 @@ class DeviceOrientationManager {
17
21
private static final IntentFilter orientationIntentFilter =
18
22
new IntentFilter (Intent .ACTION_CONFIGURATION_CHANGED );
19
23
20
- private final Context context ;
24
+ private final Activity activity ;
21
25
private final DartMessenger messenger ;
22
26
private final boolean isFrontFacing ;
23
27
private final int sensorOrientation ;
@@ -26,8 +30,8 @@ class DeviceOrientationManager {
26
30
private BroadcastReceiver broadcastReceiver ;
27
31
28
32
public DeviceOrientationManager (
29
- Context context , DartMessenger messenger , boolean isFrontFacing , int sensorOrientation ) {
30
- this .context = context ;
33
+ Activity activity , DartMessenger messenger , boolean isFrontFacing , int sensorOrientation ) {
34
+ this .activity = activity ;
31
35
this .messenger = messenger ;
32
36
this .isFrontFacing = isFrontFacing ;
33
37
this .sensorOrientation = sensorOrientation ;
@@ -70,7 +74,7 @@ public int getMediaOrientation(PlatformChannel.DeviceOrientation orientation) {
70
74
private void startSensorListener () {
71
75
if (orientationEventListener != null ) return ;
72
76
orientationEventListener =
73
- new OrientationEventListener (context , SensorManager .SENSOR_DELAY_NORMAL ) {
77
+ new OrientationEventListener (activity , SensorManager .SENSOR_DELAY_NORMAL ) {
74
78
@ Override
75
79
public void onOrientationChanged (int angle ) {
76
80
if (!isSystemAutoRotationLocked ()) {
@@ -102,8 +106,8 @@ public void onReceive(Context context, Intent intent) {
102
106
}
103
107
}
104
108
};
105
- context .registerReceiver (broadcastReceiver , orientationIntentFilter );
106
- broadcastReceiver .onReceive (context , null );
109
+ activity .registerReceiver (broadcastReceiver , orientationIntentFilter );
110
+ broadcastReceiver .onReceive (activity , null );
107
111
}
108
112
109
113
private void stopSensorListener () {
@@ -114,22 +118,19 @@ private void stopSensorListener() {
114
118
115
119
private void stopUIListener () {
116
120
if (broadcastReceiver == null ) return ;
117
- context .unregisterReceiver (broadcastReceiver );
121
+ activity .unregisterReceiver (broadcastReceiver );
118
122
broadcastReceiver = null ;
119
123
}
120
124
121
125
private boolean isSystemAutoRotationLocked () {
122
126
return android .provider .Settings .System .getInt (
123
- context .getContentResolver (), Settings .System .ACCELEROMETER_ROTATION , 0 )
127
+ activity .getContentResolver (), Settings .System .ACCELEROMETER_ROTATION , 0 )
124
128
!= 1 ;
125
129
}
126
130
127
131
private PlatformChannel .DeviceOrientation getUIOrientation () {
128
- final int rotation =
129
- ((WindowManager ) context .getSystemService (Context .WINDOW_SERVICE ))
130
- .getDefaultDisplay ()
131
- .getRotation ();
132
- final int orientation = context .getResources ().getConfiguration ().orientation ;
132
+ final int rotation = getDisplay ().getRotation ();
133
+ final int orientation = activity .getResources ().getConfiguration ().orientation ;
133
134
134
135
switch (orientation ) {
135
136
case Configuration .ORIENTATION_PORTRAIT :
@@ -172,9 +173,8 @@ private PlatformChannel.DeviceOrientation calculateSensorOrientation(int angle)
172
173
}
173
174
174
175
private int getDeviceDefaultOrientation () {
175
- WindowManager windowManager = (WindowManager ) context .getSystemService (Context .WINDOW_SERVICE );
176
- Configuration config = context .getResources ().getConfiguration ();
177
- int rotation = windowManager .getDefaultDisplay ().getRotation ();
176
+ Configuration config = activity .getResources ().getConfiguration ();
177
+ int rotation = getDisplay ().getRotation ();
178
178
if (((rotation == Surface .ROTATION_0 || rotation == Surface .ROTATION_180 )
179
179
&& config .orientation == Configuration .ORIENTATION_LANDSCAPE )
180
180
|| ((rotation == Surface .ROTATION_90 || rotation == Surface .ROTATION_270 )
@@ -184,4 +184,14 @@ private int getDeviceDefaultOrientation() {
184
184
return Configuration .ORIENTATION_PORTRAIT ;
185
185
}
186
186
}
187
+
188
+ @ SuppressWarnings ("deprecation" )
189
+ private Display getDisplay () {
190
+ if (VERSION .SDK_INT >= VERSION_CODES .R ) {
191
+ return activity .getDisplay ();
192
+ } else {
193
+ return ((WindowManager ) activity .getSystemService (Context .WINDOW_SERVICE ))
194
+ .getDefaultDisplay ();
195
+ }
196
+ }
187
197
}
0 commit comments