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

[connectivity] Android Code Inspection and Clean up #3051

Merged
merged 21 commits into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
ed86673
[share] MethodCallHandler.java uses unchecked or unsafe operations
hamdikahloun Sep 7, 2020
cd49311
[share] MethodCallHandler.java uses unchecked or unsafe operations
hamdikahloun Sep 7, 2020
99b5950
[share] MethodCallHandler.java uses unchecked or unsafe operations
hamdikahloun Sep 7, 2020
2f51f5f
[share] MethodCallHandler.java uses unchecked or unsafe operations
hamdikahloun Sep 7, 2020
02d7ef0
[share] MethodCallHandler.java uses unchecked or unsafe operations
hamdikahloun Sep 7, 2020
6e87f1d
[share] MethodCallHandler.java uses unchecked or unsafe operations
hamdikahloun Sep 7, 2020
c68a55d
[share] MethodCallHandler.java uses unchecked or unsafe operations
hamdikahloun Sep 10, 2020
90529c8
[share] MethodCallHandler.java uses unchecked or unsafe operations
hamdikahloun Sep 10, 2020
cbc56a0
[share] MethodCallHandler.java uses unchecked or unsafe operations
hamdikahloun Sep 10, 2020
714864f
Update MethodCallHandler.java
hamdikahloun Sep 10, 2020
070697a
Merge remote-tracking branch 'upstream/master'
hamdikahloun Sep 13, 2020
6299816
Handle deprecation & unchecked warning as error
hamdikahloun Sep 13, 2020
2442092
Update MethodCallHandler.java
hamdikahloun Sep 14, 2020
c906be1
Update MethodCallHandler.java
hamdikahloun Sep 14, 2020
cffb37c
Merge remote-tracking branch 'upstream/master'
hamdikahloun Sep 18, 2020
dab8122
Android registerDefaultNetworkCallback
hamdikahloun Sep 18, 2020
49a9ba5
Android registerDefaultNetworkCallback
hamdikahloun Sep 18, 2020
317f497
Android registerDefaultNetworkCallback
hamdikahloun Sep 18, 2020
3e7e3e1
Update CHANGELOG.md
hamdikahloun Sep 25, 2020
226041b
Update pubspec.yaml
hamdikahloun Sep 25, 2020
3dbe924
Merge branch 'master' into connectivity
hamdikahloun Sep 25, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/connectivity/connectivity/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.4.9+4

* Update README with the updated information about WifiInfo on Android O or higher.
* Android: Avoiding uses or overrides a deprecated API

## 0.4.9+3

* Keep handling deprecated Android v1 classes for backward compatibility.
Expand Down
8 changes: 8 additions & 0 deletions packages/connectivity/connectivity/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ dispose() {

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.

To successfully get WiFi Name or Wi-Fi BSSID starting with Android O, ensure all of the following conditions are met:

* If your app is targeting Android 10 (API level 29) SDK or higher, your app has the ACCESS_FINE_LOCATION permission.

* 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.

* Location services are enabled on the device (under Settings > Location).

You can get wi-fi related information using:

```dart
Expand Down
5 changes: 5 additions & 0 deletions packages/connectivity/connectivity/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
group 'io.flutter.plugins.connectivity'
version '1.0-SNAPSHOT'
def args = ["-Xlint:deprecation","-Xlint:unchecked","-Werror"]

buildscript {
repositories {
Expand All @@ -19,6 +20,10 @@ rootProject.allprojects {
}
}

project.getTasks().withType(JavaCompile){
options.compilerArgs.addAll(args)
}

apply plugin: 'com.android.library'

android {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,8 @@ private String getNetworkTypeLegacy() {
return "none";
}
}

public ConnectivityManager getConnectivityManager() {
return connectivityManager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.Network;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.RequiresApi;
import io.flutter.plugin.common.EventChannel;

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

ConnectivityBroadcastReceiver(Context context, Connectivity connectivity) {
this.context = context;
Expand All @@ -33,12 +40,20 @@ class ConnectivityBroadcastReceiver extends BroadcastReceiver
@Override
public void onListen(Object arguments, EventChannel.EventSink events) {
this.events = events;
context.registerReceiver(this, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
connectivity.getConnectivityManager().registerDefaultNetworkCallback(getNetworkCallback());
} else {
context.registerReceiver(this, new IntentFilter(CONNECTIVITY_ACTION));
}
}

@Override
public void onCancel(Object arguments) {
context.unregisterReceiver(this);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
connectivity.getConnectivityManager().unregisterNetworkCallback(getNetworkCallback());
} else {
context.unregisterReceiver(this);
}
}

@Override
Expand All @@ -47,4 +62,30 @@ public void onReceive(Context context, Intent intent) {
events.success(connectivity.getNetworkType());
}
}

@RequiresApi(api = Build.VERSION_CODES.N)
ConnectivityManager.NetworkCallback getNetworkCallback() {
return new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
sendEvent();
}

@Override
public void onLost(Network network) {
sendEvent();
}
};
}

private void sendEvent() {
Runnable runnable =
new Runnable() {
@Override
public void run() {
events.success(connectivity.getNetworkType());
}
};
mainHandler.post(runnable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ private void setupChannels(BinaryMessenger messenger, Context context) {
eventChannel = new EventChannel(messenger, "plugins.flutter.io/connectivity_status");
ConnectivityManager connectivityManager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiManager wifiManager =
(WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);

Connectivity connectivity = new Connectivity(connectivityManager, wifiManager);

Expand Down
2 changes: 1 addition & 1 deletion packages/connectivity/connectivity/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ homepage: https://github.com/flutter/plugins/tree/master/packages/connectivity/c
# 0.4.y+z is compatible with 1.0.0, if you land a breaking change bump
# the version to 2.0.0.
# See more details: https://github.com/flutter/flutter/wiki/Package-migration-to-1.0.0
version: 0.4.9+3
version: 0.4.9+4

flutter:
plugin:
Expand Down