Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supporting Cordova 7 & 8 #2

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Windows image file caches
Thumbs.db
ehthumbs.db
.idea/

# Folder config file
Desktop.ini
Expand Down
43 changes: 32 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,54 @@ This plugin set / gets the Mobile data state.

This Plugin Requires https://github.com/Whebcraft/System_api.git

Install https://github.com/Whebcraft/Cordova-Mobile-Data.git
## Installation

## com.webcraft.mobiledata
```bash
cordova plugin add https://github.com/Whebcraft/Cordova-Mobile-Data.git
```

## **Enable:**

`@return - String, 'enabled'`<br>
`window.mobiledata.enable({success:dataEnabled});`
```javascript
cordova.plugins.MobileData.enable(function(data) {
data.enabled === true // enabled
}, function(error) {
// Error
});
```

---

## **Disable:**

`@return - String, 'disabled'`<br>
`window.mobiledata.disable({success:dataDisable});`
```javascript
cordova.plugins.MobileData.disable(function(data) {
data.enabled === false // disabled
}, function(error) {
// Error
});
```

---

## **Toggle:**

`@return - String, 'enabled/disabled'`<br>
`window.mobiledata.toggle({success:dataToggle});`

```javascript
cordova.plugins.MobileData.toggle(function(data) {
console.log(data.enabled);
}, function(error) {
// Error
});
```

---

## **Check:**

`@return - String, 'enabled/disabled'`<br>
`window.mobiledata.check({success:dataCheck});`
```javascript
cordova.plugins.MobileData.check(function() {
console.log(data.enabled);
}, function(error) {
// Error
});
```
22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "cordova-plugin-mobile-data",
"version": "1.0.2",
"description": "Cordova plugin for accessing Android WifiManager",
"cordova": {
"id": "cordova-plugin-mobile-data",
"platforms": [
"android"
]
},
"keywords": [
"ecosystem:cordova"
],
"repository": {
"type": "git",
"url": "git+https://github.com/Whebcraft/Cordova-Mobile-Data.git"
},
"bugs": {
"url": "https://github.com/Whebcraft/Cordova-Mobile-Data/issues"
},
"homepage": "https://github.com/Whebcraft/Cordova-Mobile-Data#readme"
}
21 changes: 9 additions & 12 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
<?xml version='1.0' encoding='utf-8'?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="com.webcraft.mobiledata"
version="1.0.0">
id="cordova-plugin-mobile-data"
version="1.0.2">

<name>Mobile Data Controls</name>

<engines>
<engine name="cordova" version=">=3.0.0" />
</engines>

<description>This plugin set / gets the Mobile data state.</description>
<author>Webcraft</author>
<keywords></keywords>

<license>CC</license>

<js-module src="www/mobiledata.js" name="mobiledata">
<clobbers target="window.mobiledata" />
<js-module src="www/mobiledata.js" name="MobileData">
<clobbers target="cordova.plugins.MobileData" />
</js-module>

<!-- android -->
<platform name="android">

<config-file target="res/xml/config.xml" parent="/*">
<feature name="mobiledata" >
<param name="android-package" value="com.webcraft.mobiledata.mobiledata"/>
<feature name="MobileData" >
<param name="android-package" value="com.webcraft.mobiledata.MobileData"/>
</feature>
</config-file>

<source-file src="src/android/mobiledata.java" target-dir="src/com/webcraft/mobiledata" />
<source-file src="src/android/MobileData.java" target-dir="src/com/webcraft/mobiledata" />

</platform>
</plugin>
76 changes: 76 additions & 0 deletions src/android/MobileData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.webcraft.mobiledata;

import android.content.Context;
import android.net.ConnectivityManager;
import android.telephony.TelephonyManager;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class MobileData extends CordovaPlugin {

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

try {

Context ctx = this.cordova.getActivity();

final ConnectivityManager conman = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
final Class<?> conmanClass = Class.forName(conman.getClass().getName());
final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
final Object iConnectivityManager = iConnectivityManagerField.get(conman);
final Class<?> iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());

final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);

TelephonyManager telephonyManager = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);

if (action.equals("check")) {

if (telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED ||
telephonyManager.getDataState() == TelephonyManager.DATA_SUSPENDED) {

callbackContext.success(new JSONObject().put("enabled", true));
} else if (telephonyManager.getDataState() == TelephonyManager.DATA_DISCONNECTED) {
callbackContext.success(new JSONObject().put("enabled", false));
}
}

if (action.equals("toggle")) {
if (telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED) {
setMobileDataEnabledMethod.invoke(iConnectivityManager, false);
callbackContext.success(new JSONObject().put("enabled", false));
} else if (telephonyManager.getDataState() == TelephonyManager.DATA_DISCONNECTED) {
setMobileDataEnabledMethod.invoke(iConnectivityManager, true);
callbackContext.success(new JSONObject().put("enabled", true));
}
}

if (action.equals("enable")) {
setMobileDataEnabledMethod.invoke(iConnectivityManager, true);
callbackContext.success(new JSONObject().put("enabled", true));
}


if (action.equals("disable")) {
setMobileDataEnabledMethod.invoke(iConnectivityManager, false);
callbackContext.success(new JSONObject().put("enabled", false));
}

} catch (Exception e) {
e.printStackTrace();
//callbackContext.error(e.message);
}

return true;
}
}
102 changes: 0 additions & 102 deletions src/android/mobiledata.java

This file was deleted.

50 changes: 14 additions & 36 deletions www/mobiledata.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,20 @@
var exec = require("cordova/exec");

module.exports = {

check:function(args) {
cordova.exec(
(!args.success) ? null : args.success,
function(error){alert('Mobile Data check Error:'+error)},
"mobiledata",
'check',
[]
);
check: function (success, failure) {
exec(success, failure, "MobileData", "check", []);
},

enable:function(args) {
cordova.exec(
(!args.success) ? null : args.success,
function(error){alert('Mobile Data enable Error:'+error)},
"mobiledata",
'enable',
[]
);

enable: function (success, failure) {
exec(success, failure, "MobileData", "enable", []);
},

disable:function(args) {
cordova.exec(
(!args.success) ? null : args.success,
function(error){alert('Mobile Data disable Error:'+error)},
"mobiledata",
'disable',
[]
);

disable: function (success, failure) {
exec(success, failure, "MobileData", "disable", []);
},

toggle:function(args) {
cordova.exec(
(!args.success) ? null : args.success,
function(error){alert('Mobile Data toggle Error:'+error)},
"mobiledata",
'toggle',
[]
);
}

toggle: function (success, failure) {
exec(success, failure, "MobileData", "toggle", []);
}
};