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

Commit 67697ab

Browse files
author
Chris Yang
authored
[Connectivity] migrate to the new android embedding (#2142)
1 parent ba5c774 commit 67697ab

File tree

17 files changed

+362
-182
lines changed

17 files changed

+362
-182
lines changed

packages/connectivity/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.4.5
2+
3+
* Support the v2 Android embedder.
4+
15
## 0.4.4+1
26

37
* Update and migrate iOS example project.

packages/connectivity/android/build.gradle

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,28 @@ android {
4545
disable 'InvalidPackage'
4646
}
4747
}
48+
49+
// TODO(amirh): Remove this hack once androidx.lifecycle is included on stable. https://github.com/flutter/flutter/issues/42348
50+
afterEvaluate {
51+
def containsEmbeddingDependencies = false
52+
for (def configuration : configurations.all) {
53+
for (def dependency : configuration.dependencies) {
54+
if (dependency.group == 'io.flutter' &&
55+
dependency.name.startsWith('flutter_embedding') &&
56+
dependency.isTransitive())
57+
{
58+
containsEmbeddingDependencies = true
59+
break
60+
}
61+
}
62+
}
63+
if (!containsEmbeddingDependencies) {
64+
android {
65+
dependencies {
66+
def lifecycle_version = "2.1.0"
67+
api "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
68+
api "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"
69+
}
70+
}
71+
}
72+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Copyright 2019 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
package io.flutter.plugins.connectivity;
6+
7+
import android.net.ConnectivityManager;
8+
import android.net.Network;
9+
import android.net.NetworkCapabilities;
10+
import android.net.NetworkInfo;
11+
import android.net.wifi.WifiInfo;
12+
import android.net.wifi.WifiManager;
13+
import android.os.Build;
14+
import androidx.annotation.NonNull;
15+
import androidx.annotation.Nullable;
16+
17+
/** Reports connectivity related information such as connectivity type and wifi information. */
18+
class Connectivity {
19+
private ConnectivityManager connectivityManager;
20+
private WifiManager wifiManager;
21+
22+
Connectivity(ConnectivityManager connectivityManager, WifiManager wifiManager) {
23+
this.connectivityManager = connectivityManager;
24+
this.wifiManager = wifiManager;
25+
}
26+
27+
@NonNull
28+
String getNetworkType() {
29+
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
30+
Network network = connectivityManager.getActiveNetwork();
31+
NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);
32+
if (capabilities == null) {
33+
return "none";
34+
}
35+
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
36+
|| capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) {
37+
return "wifi";
38+
}
39+
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
40+
return "mobile";
41+
}
42+
}
43+
44+
return getNetworkTypeLegacy();
45+
}
46+
47+
@Nullable
48+
String getWifiName() {
49+
WifiInfo wifiInfo = getWifiInfo();
50+
String ssid = null;
51+
if (wifiInfo != null) ssid = wifiInfo.getSSID();
52+
if (ssid != null) ssid = ssid.replaceAll("\"", ""); // Android returns "SSID"
53+
return ssid;
54+
}
55+
56+
@Nullable
57+
String getWifiBSSID() {
58+
WifiInfo wifiInfo = getWifiInfo();
59+
String bssid = null;
60+
if (wifiInfo != null) {
61+
bssid = wifiInfo.getBSSID();
62+
}
63+
return bssid;
64+
}
65+
66+
@Nullable
67+
String getWifiIPAddress() {
68+
WifiInfo wifiInfo = null;
69+
if (wifiManager != null) wifiInfo = wifiManager.getConnectionInfo();
70+
71+
String ip = null;
72+
int i_ip = 0;
73+
if (wifiInfo != null) i_ip = wifiInfo.getIpAddress();
74+
75+
if (i_ip != 0)
76+
ip =
77+
String.format(
78+
"%d.%d.%d.%d",
79+
(i_ip & 0xff), (i_ip >> 8 & 0xff), (i_ip >> 16 & 0xff), (i_ip >> 24 & 0xff));
80+
81+
return ip;
82+
}
83+
84+
@Nullable
85+
private WifiInfo getWifiInfo() {
86+
return wifiManager == null ? null : wifiManager.getConnectionInfo();
87+
}
88+
89+
@SuppressWarnings("deprecation")
90+
private String getNetworkTypeLegacy() {
91+
// handle type for Android versions less than Android 9
92+
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
93+
if (info == null || !info.isConnected()) {
94+
return "none";
95+
}
96+
int type = info.getType();
97+
switch (type) {
98+
case ConnectivityManager.TYPE_ETHERNET:
99+
case ConnectivityManager.TYPE_WIFI:
100+
case ConnectivityManager.TYPE_WIMAX:
101+
return "wifi";
102+
case ConnectivityManager.TYPE_MOBILE:
103+
case ConnectivityManager.TYPE_MOBILE_DUN:
104+
case ConnectivityManager.TYPE_MOBILE_HIPRI:
105+
return "mobile";
106+
default:
107+
return "none";
108+
}
109+
}
110+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2019 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
package io.flutter.plugins.connectivity;
6+
7+
import android.content.BroadcastReceiver;
8+
import android.content.Context;
9+
import android.content.Intent;
10+
import android.content.IntentFilter;
11+
import android.net.ConnectivityManager;
12+
import androidx.annotation.NonNull;
13+
import io.flutter.plugin.common.EventChannel;
14+
15+
/**
16+
* The ConnectivityBroadcastReceiver receives the connectivity updates and send them to the UIThread
17+
* through an {@link EventChannel.EventSink}
18+
*
19+
* <p>Use {@link
20+
* io.flutter.plugin.common.EventChannel#setStreamHandler(io.flutter.plugin.common.EventChannel.StreamHandler)}
21+
* to set up the receiver.
22+
*/
23+
class ConnectivityBroadcastReceiver extends BroadcastReceiver
24+
implements EventChannel.StreamHandler {
25+
private Context context;
26+
private Connectivity connectivity;
27+
private EventChannel.EventSink events;
28+
29+
ConnectivityBroadcastReceiver(@NonNull Context context, @NonNull Connectivity connectivity) {
30+
this.context = context;
31+
this.connectivity = connectivity;
32+
}
33+
34+
@Override
35+
public void onListen(Object arguments, EventChannel.EventSink events) {
36+
this.events = events;
37+
context.registerReceiver(this, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
38+
}
39+
40+
@Override
41+
public void onCancel(Object arguments) {
42+
context.unregisterReceiver(this);
43+
}
44+
45+
@Override
46+
public void onReceive(Context context, Intent intent) {
47+
if (events != null) {
48+
events.success(connectivity.getNetworkType());
49+
}
50+
}
51+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2019 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
package io.flutter.plugins.connectivity;
6+
7+
import androidx.annotation.NonNull;
8+
import io.flutter.plugin.common.MethodCall;
9+
import io.flutter.plugin.common.MethodChannel;
10+
11+
/**
12+
* The handler receives {@link MethodCall}s from the UIThread, gets the related information from
13+
* a @{@link Connectivity}, and then send the result back to the UIThread through the {@link
14+
* MethodChannel.Result}.
15+
*/
16+
class ConnectivityMethodChannelHandler implements MethodChannel.MethodCallHandler {
17+
18+
private Connectivity connectivity;
19+
20+
/**
21+
* Construct the ConnectivityMethodChannelHandler with a {@code connectivity}. The {@code
22+
* connectivity} must not be null.
23+
*/
24+
ConnectivityMethodChannelHandler(@NonNull Connectivity connectivity) {
25+
assert (connectivity != null);
26+
this.connectivity = connectivity;
27+
}
28+
29+
@Override
30+
public void onMethodCall(MethodCall call, MethodChannel.Result result) {
31+
switch (call.method) {
32+
case "check":
33+
result.success(connectivity.getNetworkType());
34+
break;
35+
case "wifiName":
36+
result.success(connectivity.getWifiName());
37+
break;
38+
case "wifiBSSID":
39+
result.success(connectivity.getWifiBSSID());
40+
break;
41+
case "wifiIPAddress":
42+
result.success(connectivity.getWifiIPAddress());
43+
break;
44+
default:
45+
result.notImplemented();
46+
break;
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)