Skip to content

Commit

Permalink
Android implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ikeith committed Aug 11, 2020
1 parent 76d6534 commit 43219c3
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 15 deletions.
4 changes: 3 additions & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"packages": [],
"packages": [
"network"
],
"command": {
"version": {
"allowBranch": "main",
Expand Down
2 changes: 2 additions & 0 deletions network/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ext {
junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.12'
androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.1.0'
androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.1.1'
androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.2.0'
}
Expand Down Expand Up @@ -50,6 +51,7 @@ repositories {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(':capacitor-android')
implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
testImplementation "junit:junit:$junitVersion"
androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
Expand Down
1 change: 1 addition & 0 deletions network/android/src/main/AndroidManifest.xml
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>

This file was deleted.

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";
}
}

0 comments on commit 43219c3

Please sign in to comment.