11package io .flutter .plugins .camera ;
22
3+ import android .app .Activity ;
34import android .content .BroadcastReceiver ;
45import android .content .Context ;
56import android .content .Intent ;
67import android .content .IntentFilter ;
78import android .content .res .Configuration ;
89import android .hardware .SensorManager ;
10+ import android .os .Build .VERSION ;
11+ import android .os .Build .VERSION_CODES ;
912import android .provider .Settings ;
13+ import android .view .Display ;
1014import android .view .OrientationEventListener ;
1115import android .view .Surface ;
1216import android .view .WindowManager ;
@@ -17,7 +21,7 @@ class DeviceOrientationManager {
1721 private static final IntentFilter orientationIntentFilter =
1822 new IntentFilter (Intent .ACTION_CONFIGURATION_CHANGED );
1923
20- private final Context context ;
24+ private final Activity activity ;
2125 private final DartMessenger messenger ;
2226 private final boolean isFrontFacing ;
2327 private final int sensorOrientation ;
@@ -26,8 +30,8 @@ class DeviceOrientationManager {
2630 private BroadcastReceiver broadcastReceiver ;
2731
2832 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 ;
3135 this .messenger = messenger ;
3236 this .isFrontFacing = isFrontFacing ;
3337 this .sensorOrientation = sensorOrientation ;
@@ -70,7 +74,7 @@ public int getMediaOrientation(PlatformChannel.DeviceOrientation orientation) {
7074 private void startSensorListener () {
7175 if (orientationEventListener != null ) return ;
7276 orientationEventListener =
73- new OrientationEventListener (context , SensorManager .SENSOR_DELAY_NORMAL ) {
77+ new OrientationEventListener (activity , SensorManager .SENSOR_DELAY_NORMAL ) {
7478 @ Override
7579 public void onOrientationChanged (int angle ) {
7680 if (!isSystemAutoRotationLocked ()) {
@@ -102,8 +106,8 @@ public void onReceive(Context context, Intent intent) {
102106 }
103107 }
104108 };
105- context .registerReceiver (broadcastReceiver , orientationIntentFilter );
106- broadcastReceiver .onReceive (context , null );
109+ activity .registerReceiver (broadcastReceiver , orientationIntentFilter );
110+ broadcastReceiver .onReceive (activity , null );
107111 }
108112
109113 private void stopSensorListener () {
@@ -114,22 +118,19 @@ private void stopSensorListener() {
114118
115119 private void stopUIListener () {
116120 if (broadcastReceiver == null ) return ;
117- context .unregisterReceiver (broadcastReceiver );
121+ activity .unregisterReceiver (broadcastReceiver );
118122 broadcastReceiver = null ;
119123 }
120124
121125 private boolean isSystemAutoRotationLocked () {
122126 return android .provider .Settings .System .getInt (
123- context .getContentResolver (), Settings .System .ACCELEROMETER_ROTATION , 0 )
127+ activity .getContentResolver (), Settings .System .ACCELEROMETER_ROTATION , 0 )
124128 != 1 ;
125129 }
126130
127131 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 ;
133134
134135 switch (orientation ) {
135136 case Configuration .ORIENTATION_PORTRAIT :
@@ -146,6 +147,7 @@ private PlatformChannel.DeviceOrientation getUIOrientation() {
146147 }
147148 default :
148149 return PlatformChannel .DeviceOrientation .PORTRAIT_UP ;
150+
149151 }
150152 }
151153
@@ -172,9 +174,8 @@ private PlatformChannel.DeviceOrientation calculateSensorOrientation(int angle)
172174 }
173175
174176 private int getDeviceDefaultOrientation () {
175- WindowManager windowManager = (WindowManager ) context .getSystemService (Context .WINDOW_SERVICE );
176- Configuration config = context .getResources ().getConfiguration ();
177- int rotation = windowManager .getDefaultDisplay ().getRotation ();
177+ Configuration config = activity .getResources ().getConfiguration ();
178+ int rotation = getDisplay ().getRotation ();
178179 if (((rotation == Surface .ROTATION_0 || rotation == Surface .ROTATION_180 )
179180 && config .orientation == Configuration .ORIENTATION_LANDSCAPE )
180181 || ((rotation == Surface .ROTATION_90 || rotation == Surface .ROTATION_270 )
@@ -184,4 +185,14 @@ private int getDeviceDefaultOrientation() {
184185 return Configuration .ORIENTATION_PORTRAIT ;
185186 }
186187 }
188+
189+ @ SuppressWarnings ("deprecation" )
190+ private Display getDisplay () {
191+ if (VERSION .SDK_INT >= VERSION_CODES .R ) {
192+ return activity .getDisplay ();
193+ } else {
194+ return ((WindowManager ) activity .getSystemService (Context .WINDOW_SERVICE ))
195+ .getDefaultDisplay ();
196+ }
197+ }
187198}
0 commit comments