Skip to content

Commit

Permalink
(android) Add isMobileDataEnabled() to core module
Browse files Browse the repository at this point in the history
  • Loading branch information
dpa99c committed Oct 4, 2022
1 parent 613e898 commit 20acb90
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,31 @@ The function is passed a single string parameter containing the error message.
console.log(`Airplane mode is currently ${enabled ? 'enabled' : 'disabled'}%`);
});

### isMobileDataEnabled()

Platforms: Android and iOS

Checks if mobile (cellular) data is currently enabled on the device.

On Android this requires permission `<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />`


cordova.plugins.diagnostic.isMobileDataEnabled(successCallback, errorCallback);

#### Parameters

- {Function} successCallback - The callback which will be called when operation is successful.
The function is passed a single boolean parameter which is TRUE if mobile data is enabled.
- {Function} errorCallback - The callback which will be called when operation encounters an error.
The function is passed a single string parameter containing the error message.


#### Example usage

cordova.plugins.diagnostic.isMobileDataEnabled(function(enabled){
console.log(`Mobile data is currently ${enabled ? 'enabled' : 'disabled'}%`);
});

### getDeviceOSVersion()

Platforms: Android and iOS
Expand Down
13 changes: 13 additions & 0 deletions cordova.plugins.diagnostic.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,19 @@ interface Diagnostic {
errorCallback: (error: string) => void
) => void;

/**
* ANDROID ONLY
*
* Checks if mobile data is enabled on device.
*
* @param successCallback
* @param errorCallback
*/
isMobileDataEnabled?: (
successCallback: () => boolean,
errorCallback: (error: string) => void
) => void;

/**
* Returns details of the OS of the device on which the app is currently running
*
Expand Down
21 changes: 21 additions & 0 deletions src/android/Diagnostic.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Licensed to the Apache Software Foundation (ASF) under one
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.BatteryManager;
import android.os.Build;
Expand Down Expand Up @@ -278,6 +279,8 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
callbackContext.success(isADBModeEnabled() ? 1 : 0);
} else if(action.equals("isDeviceRooted")) {
callbackContext.success(isDeviceRooted() ? 1 : 0);
} else if(action.equals("isMobileDataEnabled")) {
callbackContext.success(isMobileDataEnabled() ? 1 : 0);
} else if(action.equals("restart")) {
this.restart(args);
} else if(action.equals("getArchitecture")) {
Expand Down Expand Up @@ -445,33 +448,51 @@ public boolean isDeviceRooted(){
return false;
}

// https://stackoverflow.com/a/12864897/777265
public boolean isMobileDataEnabled(){
boolean mobileDataEnabled = false; // Assume disabled
ConnectivityManager cm = (ConnectivityManager) cordova.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
try {
Class cmClass = Class.forName(cm.getClass().getName());
Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
method.setAccessible(true);
mobileDataEnabled = (Boolean)method.invoke(cm);
} catch (Exception e) {
logDebug(e.getMessage());
}
return mobileDataEnabled;
}

/************
* Internals
***********/

public void logDebug(String msg) {
if(msg == null) return;
if(debugEnabled){
Log.d(TAG, msg);
executeGlobalJavascript("console.log(\""+TAG+"[native]: "+escapeDoubleQuotes(msg)+"\")");
}
}

public void logInfo(String msg){
if(msg == null) return;
Log.i(TAG, msg);
if(debugEnabled){
executeGlobalJavascript("console.info(\""+TAG+"[native]: "+escapeDoubleQuotes(msg)+"\")");
}
}

public void logWarning(String msg){
if(msg == null) return;
Log.w(TAG, msg);
if(debugEnabled){
executeGlobalJavascript("console.warn(\""+TAG+"[native]: "+escapeDoubleQuotes(msg)+"\")");
}
}

public void logError(String msg){
if(msg == null) return;
Log.e(TAG, msg);
if(debugEnabled){
executeGlobalJavascript("console.error(\""+TAG+"[native]: "+escapeDoubleQuotes(msg)+"\")");
Expand Down
16 changes: 16 additions & 0 deletions www/android/diagnostic.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,22 @@ var Diagnostic = (function(){
[]);
};

/**
* Checks if mobile data is enabled on device.
*
* @param {Function} successCallback - The callback which will be called when the operation is successful.
* This callback function is passed a single boolean parameter which is TRUE if mobile data is enabled.
* @param {Function} errorCallback - The callback which will be called when the operation encounters an error.
* This callback function is passed a single string parameter containing the error message.
*/
Diagnostic.isMobileDataEnabled = function(successCallback, errorCallback) {
return cordova.exec(Diagnostic._ensureBoolean(successCallback),
errorCallback,
'Diagnostic',
'isMobileDataEnabled',
[]);
};

/**
* Returns details of the OS of the device on which the app is currently running
*
Expand Down

0 comments on commit 20acb90

Please sign in to comment.