diff --git a/.gitignore b/.gitignore index 96374c4..e739945 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # Windows image file caches Thumbs.db ehthumbs.db +.idea/ # Folder config file Desktop.ini diff --git a/README.md b/README.md index d0c18b1..d907abb 100644 --- a/README.md +++ b/README.md @@ -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'`
-`window.mobiledata.enable({success:dataEnabled});` +```javascript +cordova.plugins.MobileData.enable(function(data) { + data.enabled === true // enabled +}, function(error) { + // Error +}); +``` --- ## **Disable:** -`@return - String, 'disabled'`
-`window.mobiledata.disable({success:dataDisable});` +```javascript +cordova.plugins.MobileData.disable(function(data) { + data.enabled === false // disabled +}, function(error) { + // Error +}); +``` --- ## **Toggle:** -`@return - String, 'enabled/disabled'`
-`window.mobiledata.toggle({success:dataToggle});` - +```javascript +cordova.plugins.MobileData.toggle(function(data) { + console.log(data.enabled); +}, function(error) { + // Error +}); +``` --- ## **Check:** -`@return - String, 'enabled/disabled'`
-`window.mobiledata.check({success:dataCheck});` +```javascript +cordova.plugins.MobileData.check(function() { + console.log(data.enabled); +}, function(error) { + // Error +}); +``` \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..5625fc2 --- /dev/null +++ b/package.json @@ -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" +} \ No newline at end of file diff --git a/plugin.xml b/plugin.xml index 9f9b640..8306e2d 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,34 +1,31 @@ - + + id="cordova-plugin-mobile-data" + version="1.0.2"> Mobile Data Controls - - - - This plugin set / gets the Mobile data state. Webcraft CC - - + + - - + + - + \ No newline at end of file diff --git a/src/android/MobileData.java b/src/android/MobileData.java new file mode 100644 index 0000000..7be2bc1 --- /dev/null +++ b/src/android/MobileData.java @@ -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; + } +} \ No newline at end of file diff --git a/src/android/mobiledata.java b/src/android/mobiledata.java deleted file mode 100644 index 117be3f..0000000 --- a/src/android/mobiledata.java +++ /dev/null @@ -1,102 +0,0 @@ -package com.webcraft.mobiledata; - -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - -import org.apache.cordova.CallbackContext; -import org.apache.cordova.CordovaPlugin; -import android.content.Context; -import android.net.ConnectivityManager; -import android.telephony.TelephonyManager; - -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){ - callbackContext.success(new JSONObject().put("returnVal", "enabled")); - } - - if(telephonyManager.getDataState() == TelephonyManager.DATA_DISCONNECTED){ - callbackContext.success(new JSONObject().put("returnVal", "disabled")); - } - - if(telephonyManager.getDataState() == TelephonyManager.DATA_SUSPENDED){ - callbackContext.success(new JSONObject().put("returnVal", "enabled")); - } - } - - if(action.equals("toggle")){ - if(telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED){ - setMobileDataEnabledMethod.invoke(iConnectivityManager, false); - callbackContext.success(new JSONObject().put("returnVal", "disabled")); - } - - if(telephonyManager.getDataState() == TelephonyManager.DATA_DISCONNECTED){ - setMobileDataEnabledMethod.invoke(iConnectivityManager, true); - callbackContext.success(new JSONObject().put("returnVal", "enabled")); - } - } - - if(action.equals("enable")){ - setMobileDataEnabledMethod.invoke(iConnectivityManager, true); - callbackContext.success(new JSONObject().put("returnVal", "enabled")); - } - - - if(action.equals("disable")){ - setMobileDataEnabledMethod.invoke(iConnectivityManager, false); - callbackContext.success(new JSONObject().put("returnVal", "disabled")); - } - - } catch (SecurityException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (NoSuchMethodException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (ClassNotFoundException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IllegalArgumentException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IllegalAccessException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (InvocationTargetException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (NoSuchFieldException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - return true; - - - } -} \ No newline at end of file diff --git a/www/mobiledata.js b/www/mobiledata.js index 933005a..ad93f04 100644 --- a/www/mobiledata.js +++ b/www/mobiledata.js @@ -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", []); + } }; \ No newline at end of file