-
Notifications
You must be signed in to change notification settings - Fork 610
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
105 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
{ | ||
"packages": [], | ||
"packages": [ | ||
"network" | ||
], | ||
"command": { | ||
"version": { | ||
"allowBranch": "main", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
|
||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.capacitorjs.plugins.network.network"> | ||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | ||
</manifest> | ||
|
8 changes: 0 additions & 8 deletions
8
network/android/src/main/java/com/capacitorjs/plugins/network/Network.java
This file was deleted.
Oops, something went wrong.
105 changes: 99 additions & 6 deletions
105
network/android/src/main/java/com/capacitorjs/plugins/network/NetworkPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,114 @@ | ||
package com.capacitorjs.plugins.network; | ||
|
||
import android.content.BroadcastReceiver; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.IntentFilter; | ||
import android.net.ConnectivityManager; | ||
import android.net.NetworkInfo; | ||
import android.Manifest; | ||
import com.getcapacitor.JSObject; | ||
import com.getcapacitor.Logger; | ||
import com.getcapacitor.NativePlugin; | ||
import com.getcapacitor.Plugin; | ||
import com.getcapacitor.PluginCall; | ||
import com.getcapacitor.PluginMethod; | ||
|
||
@NativePlugin(name = "Network") | ||
@NativePlugin(name = "Network", permissions = { Manifest.permission.ACCESS_NETWORK_STATE }) | ||
public class NetworkPlugin extends Plugin { | ||
private Network implementation = new Network(); | ||
public static final String NETWORK_CHANGE_EVENT = "networkStatusChange"; | ||
private static final String PERMISSION_NOT_SET = Manifest.permission.ACCESS_NETWORK_STATE + " not set in AndroidManifest.xml"; | ||
|
||
private ConnectivityManager cm; | ||
private BroadcastReceiver receiver; | ||
|
||
/** | ||
* Monitor for network status changes and fire our event. | ||
*/ | ||
@Override | ||
@SuppressWarnings("MissingPermission") | ||
public void load() { | ||
cm = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE); | ||
|
||
receiver = | ||
new BroadcastReceiver() { | ||
|
||
@Override | ||
public void onReceive(Context context, Intent intent) { | ||
if (hasRequiredPermissions()) { | ||
notifyListeners(NETWORK_CHANGE_EVENT, getStatusJSObject(cm.getActiveNetworkInfo())); | ||
} else { | ||
Logger.error(getLogTag(), PERMISSION_NOT_SET, null); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Get current network status information | ||
* @param call | ||
*/ | ||
@SuppressWarnings("MissingPermission") | ||
@PluginMethod | ||
public void echo(PluginCall call) { | ||
String value = call.getString("value"); | ||
public void getStatus(PluginCall call) { | ||
if (hasRequiredPermissions()) { | ||
ConnectivityManager cm = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE); | ||
|
||
NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); | ||
|
||
call.success(getStatusJSObject(activeNetwork)); | ||
} else { | ||
call.error(PERMISSION_NOT_SET); | ||
} | ||
} | ||
|
||
/** | ||
* Register the IntentReceiver on resume | ||
*/ | ||
@Override | ||
protected void handleOnResume() { | ||
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); | ||
getActivity().registerReceiver(receiver, filter); | ||
} | ||
|
||
/** | ||
* Unregister the IntentReceiver on pause to avoid leaking it | ||
*/ | ||
@Override | ||
protected void handleOnPause() { | ||
getActivity().unregisterReceiver(receiver); | ||
} | ||
|
||
/** | ||
* Transform a NetworkInfo object into our JSObject for returning to client | ||
* @param info | ||
* @return | ||
*/ | ||
private JSObject getStatusJSObject(NetworkInfo info) { | ||
JSObject ret = new JSObject(); | ||
ret.put("value", implementation.echo(value)); | ||
call.success(ret); | ||
if (info == null) { | ||
ret.put("connected", false); | ||
ret.put("connectionType", "none"); | ||
} else { | ||
ret.put("connected", info.isConnected()); | ||
ret.put("connectionType", getNormalizedTypeName(info)); | ||
} | ||
return ret; | ||
} | ||
|
||
/** | ||
* Convert the Android-specific naming for network types into our cross-platform type | ||
* @param info | ||
* @return | ||
*/ | ||
private String getNormalizedTypeName(NetworkInfo info) { | ||
String typeName = info.getTypeName(); | ||
if (typeName.equals("WIFI")) { | ||
return "wifi"; | ||
} | ||
if (typeName.equals("MOBILE")) { | ||
return "cellular"; | ||
} | ||
return "none"; | ||
} | ||
} |