Skip to content

Commit 26b2284

Browse files
hamdikahlounEgor
authored and
Egor
committed
[connectivity] Android Code Inspection and Clean up (flutter#3051)
1 parent cec30d4 commit 26b2284

File tree

7 files changed

+68
-4
lines changed

7 files changed

+68
-4
lines changed

packages/connectivity/connectivity/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.4.9+4
2+
3+
* Update README with the updated information about WifiInfo on Android O or higher.
4+
* Android: Avoiding uses or overrides a deprecated API
5+
16
## 0.4.9+3
27

38
* Keep handling deprecated Android v1 classes for backward compatibility.

packages/connectivity/connectivity/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ dispose() {
5959

6060
Note that connectivity changes are no longer communicated to Android apps in the background starting with Android O. *You should always check for connectivity status when your app is resumed.* The broadcast is only useful when your application is in the foreground.
6161

62+
To successfully get WiFi Name or Wi-Fi BSSID starting with Android O, ensure all of the following conditions are met:
63+
64+
* If your app is targeting Android 10 (API level 29) SDK or higher, your app has the ACCESS_FINE_LOCATION permission.
65+
66+
* If your app is targeting SDK lower than Android 10 (API level 29), your app has the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.
67+
68+
* Location services are enabled on the device (under Settings > Location).
69+
6270
You can get wi-fi related information using:
6371

6472
```dart

packages/connectivity/connectivity/android/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
group 'io.flutter.plugins.connectivity'
22
version '1.0-SNAPSHOT'
3+
def args = ["-Xlint:deprecation","-Xlint:unchecked","-Werror"]
34

45
buildscript {
56
repositories {
@@ -19,6 +20,10 @@ rootProject.allprojects {
1920
}
2021
}
2122

23+
project.getTasks().withType(JavaCompile){
24+
options.compilerArgs.addAll(args)
25+
}
26+
2227
apply plugin: 'com.android.library'
2328

2429
android {

packages/connectivity/connectivity/android/src/main/java/io/flutter/plugins/connectivity/Connectivity.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,8 @@ private String getNetworkTypeLegacy() {
103103
return "none";
104104
}
105105
}
106+
107+
public ConnectivityManager getConnectivityManager() {
108+
return connectivityManager;
109+
}
106110
}

packages/connectivity/connectivity/android/src/main/java/io/flutter/plugins/connectivity/ConnectivityBroadcastReceiver.java

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
import android.content.Intent;
1010
import android.content.IntentFilter;
1111
import android.net.ConnectivityManager;
12+
import android.net.Network;
13+
import android.os.Build;
14+
import android.os.Handler;
15+
import android.os.Looper;
16+
import androidx.annotation.RequiresApi;
1217
import io.flutter.plugin.common.EventChannel;
1318

1419
/**
@@ -24,6 +29,8 @@ class ConnectivityBroadcastReceiver extends BroadcastReceiver
2429
private Context context;
2530
private Connectivity connectivity;
2631
private EventChannel.EventSink events;
32+
private Handler mainHandler = new Handler(Looper.getMainLooper());
33+
public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
2734

2835
ConnectivityBroadcastReceiver(Context context, Connectivity connectivity) {
2936
this.context = context;
@@ -33,12 +40,20 @@ class ConnectivityBroadcastReceiver extends BroadcastReceiver
3340
@Override
3441
public void onListen(Object arguments, EventChannel.EventSink events) {
3542
this.events = events;
36-
context.registerReceiver(this, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
43+
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
44+
connectivity.getConnectivityManager().registerDefaultNetworkCallback(getNetworkCallback());
45+
} else {
46+
context.registerReceiver(this, new IntentFilter(CONNECTIVITY_ACTION));
47+
}
3748
}
3849

3950
@Override
4051
public void onCancel(Object arguments) {
41-
context.unregisterReceiver(this);
52+
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
53+
connectivity.getConnectivityManager().unregisterNetworkCallback(getNetworkCallback());
54+
} else {
55+
context.unregisterReceiver(this);
56+
}
4257
}
4358

4459
@Override
@@ -47,4 +62,30 @@ public void onReceive(Context context, Intent intent) {
4762
events.success(connectivity.getNetworkType());
4863
}
4964
}
65+
66+
@RequiresApi(api = Build.VERSION_CODES.N)
67+
ConnectivityManager.NetworkCallback getNetworkCallback() {
68+
return new ConnectivityManager.NetworkCallback() {
69+
@Override
70+
public void onAvailable(Network network) {
71+
sendEvent();
72+
}
73+
74+
@Override
75+
public void onLost(Network network) {
76+
sendEvent();
77+
}
78+
};
79+
}
80+
81+
private void sendEvent() {
82+
Runnable runnable =
83+
new Runnable() {
84+
@Override
85+
public void run() {
86+
events.success(connectivity.getNetworkType());
87+
}
88+
};
89+
mainHandler.post(runnable);
90+
}
5091
}

packages/connectivity/connectivity/android/src/main/java/io/flutter/plugins/connectivity/ConnectivityPlugin.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ private void setupChannels(BinaryMessenger messenger, Context context) {
4141
eventChannel = new EventChannel(messenger, "plugins.flutter.io/connectivity_status");
4242
ConnectivityManager connectivityManager =
4343
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
44-
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
44+
WifiManager wifiManager =
45+
(WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
4546

4647
Connectivity connectivity = new Connectivity(connectivityManager, wifiManager);
4748

packages/connectivity/connectivity/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ homepage: https://github.com/flutter/plugins/tree/master/packages/connectivity/c
55
# 0.4.y+z is compatible with 1.0.0, if you land a breaking change bump
66
# the version to 2.0.0.
77
# See more details: https://github.com/flutter/flutter/wiki/Package-migration-to-1.0.0
8-
version: 0.4.9+3
8+
version: 0.4.9+4
99

1010
flutter:
1111
plugin:

0 commit comments

Comments
 (0)