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

Commit

Permalink
[connectivity] Fix IllegalArgumentException (#3235)
Browse files Browse the repository at this point in the history
* IllegalArgumentException

* IllegalArgumentException

* Update ConnectivityBroadcastReceiver.java

* Add onConnectivityChanged Test

* Format

* Format

* Test registerDefaultNetworkCallback

* Format

* Format

* null != networkCallback
  • Loading branch information
hamdikahloun authored Nov 6, 2020
1 parent b597aba commit da36984
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 25 deletions.
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 @@
## 2.0.2

* Android: Fix IllegalArgumentException.
* Android: Update Example project.

## 2.0.1

* Remove unused `test` dependency.
Expand Down
2 changes: 1 addition & 1 deletion packages/connectivity/connectivity/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
classpath 'com.android.tools.build:gradle:3.5.0'
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
import android.os.Build;

/** Reports connectivity related information such as connectivity type and wifi information. */
class Connectivity {
public class Connectivity {
private ConnectivityManager connectivityManager;

Connectivity(ConnectivityManager connectivityManager) {
public Connectivity(ConnectivityManager connectivityManager) {
this.connectivityManager = connectivityManager;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
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,15 +23,16 @@
* io.flutter.plugin.common.EventChannel#setStreamHandler(io.flutter.plugin.common.EventChannel.StreamHandler)}
* to set up the receiver.
*/
class ConnectivityBroadcastReceiver extends BroadcastReceiver
public class ConnectivityBroadcastReceiver extends BroadcastReceiver
implements EventChannel.StreamHandler {
private Context context;
private Connectivity connectivity;
private EventChannel.EventSink events;
private Handler mainHandler = new Handler(Looper.getMainLooper());
private ConnectivityManager.NetworkCallback networkCallback;
public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";

ConnectivityBroadcastReceiver(Context context, Connectivity connectivity) {
public ConnectivityBroadcastReceiver(Context context, Connectivity connectivity) {
this.context = context;
this.connectivity = connectivity;
}
Expand All @@ -41,7 +41,19 @@ class ConnectivityBroadcastReceiver extends BroadcastReceiver
public void onListen(Object arguments, EventChannel.EventSink events) {
this.events = events;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
connectivity.getConnectivityManager().registerDefaultNetworkCallback(getNetworkCallback());
networkCallback =
new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
sendEvent();
}

@Override
public void onLost(Network network) {
sendEvent();
}
};
connectivity.getConnectivityManager().registerDefaultNetworkCallback(networkCallback);
} else {
context.registerReceiver(this, new IntentFilter(CONNECTIVITY_ACTION));
}
Expand All @@ -50,7 +62,9 @@ public void onListen(Object arguments, EventChannel.EventSink events) {
@Override
public void onCancel(Object arguments) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
connectivity.getConnectivityManager().unregisterNetworkCallback(getNetworkCallback());
if (networkCallback != null) {
connectivity.getConnectivityManager().unregisterNetworkCallback(networkCallback);
}
} else {
context.unregisterReceiver(this);
}
Expand All @@ -63,19 +77,8 @@ public void onReceive(Context context, Intent intent) {
}
}

@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();
}
};
public ConnectivityManager.NetworkCallback getNetworkCallback() {
return networkCallback;
}

private void sendEvent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ android {
defaultConfig {
applicationId "io.flutter.plugins.connectivityexample"
minSdkVersion 16
targetSdkVersion 28
targetSdkVersion 29
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand All @@ -55,4 +55,6 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
testImplementation 'org.robolectric:robolectric:3.8'
testImplementation 'org.mockito:mockito-core:3.5.13'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.flutter.plugins.connectivityexample;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.spy;

import android.content.Context;
import android.net.ConnectivityManager;
import io.flutter.plugins.connectivity.Connectivity;
import io.flutter.plugins.connectivity.ConnectivityBroadcastReceiver;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;

@RunWith(RobolectricTestRunner.class)
public class ActivityTest {
private ConnectivityManager connectivityManager;

@Before
public void setUp() {
connectivityManager =
(ConnectivityManager)
RuntimeEnvironment.application.getSystemService(Context.CONNECTIVITY_SERVICE);
}

@Test
@Config(sdk = 24, manifest = Config.NONE)
public void networkCallbackNewApi() {
Context context = RuntimeEnvironment.application;
Connectivity connectivity = spy(new Connectivity(connectivityManager));
ConnectivityBroadcastReceiver broadcastReceiver =
spy(new ConnectivityBroadcastReceiver(context, connectivity));

broadcastReceiver.onListen(any(), any());
assertNotNull(broadcastReceiver.getNetworkCallback());
}

@Test
@Config(sdk = 23, manifest = Config.NONE)
public void networkCallbackLowApi() {
Context context = RuntimeEnvironment.application;
Connectivity connectivity = spy(new Connectivity(connectivityManager));
ConnectivityBroadcastReceiver broadcastReceiver =
spy(new ConnectivityBroadcastReceiver(context, connectivity));

broadcastReceiver.onListen(any(), any());
assertNull(broadcastReceiver.getNetworkCallback());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
classpath 'com.android.tools.build:gradle:3.5.0'
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip
2 changes: 1 addition & 1 deletion packages/connectivity/connectivity/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: connectivity
description: Flutter plugin for discovering the state of the network (WiFi &
mobile/cellular) connectivity on Android and iOS.
homepage: https://github.com/flutter/plugins/tree/master/packages/connectivity/connectivity
version: 2.0.1
version: 2.0.2

flutter:
plugin:
Expand Down

0 comments on commit da36984

Please sign in to comment.