Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 5fcba33

Browse files
committed
Fix deprecation warning
1 parent e69f746 commit 5fcba33

File tree

4 files changed

+30
-19
lines changed

4 files changed

+30
-19
lines changed

packages/camera/camera/android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ project.getTasks().withType(JavaCompile){
2727
apply plugin: 'com.android.library'
2828

2929
android {
30-
compileSdkVersion 29
30+
compileSdkVersion 30
3131

3232
defaultConfig {
3333
minSdkVersion 21

packages/camera/camera/android/src/main/java/io/flutter/plugins/camera/Camera.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public Camera(
137137

138138
deviceOrientationListener =
139139
new DeviceOrientationManager(
140-
activity.getApplicationContext(), dartMessenger, isFrontFacing, sensorOrientation);
140+
activity, dartMessenger, isFrontFacing, sensorOrientation);
141141
deviceOrientationListener.start();
142142
}
143143

packages/camera/camera/android/src/main/java/io/flutter/plugins/camera/DeviceOrientationManager.java

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package io.flutter.plugins.camera;
22

3+
import android.app.Activity;
34
import android.content.BroadcastReceiver;
45
import android.content.Context;
56
import android.content.Intent;
67
import android.content.IntentFilter;
78
import android.content.res.Configuration;
89
import android.hardware.SensorManager;
10+
import android.os.Build.VERSION;
11+
import android.os.Build.VERSION_CODES;
912
import android.provider.Settings;
13+
import android.view.Display;
1014
import android.view.OrientationEventListener;
1115
import android.view.Surface;
1216
import 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
}

packages/camera/camera/example/android/app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ apply plugin: 'com.android.application'
2525
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
2626

2727
android {
28-
compileSdkVersion 29
28+
compileSdkVersion 30
2929

3030
lintOptions {
3131
disable 'InvalidPackage'

0 commit comments

Comments
 (0)