diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ec5a398 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015 Peter Bakondy + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..7ac3f89 --- /dev/null +++ b/README.md @@ -0,0 +1,148 @@ +# cordova-plugin-sim + +This is a cordova plugin to get data from the SIM card like the carrier name, mcc, mnc and country code and other system dependent additional info. + + +## Installation + +``` +cordova plugin add com.pbakondy.sim +``` + + +## Supported Platforms + +- Android +- iOS +- Windows Phone 7 and 8 + + +## Usage + +``` +document.addEventListener("deviceready", onDeviceReady, false); + +function onDeviceReady() { + window.plugins.sim.getSimInfo(successCallback, errorCallback); +} +``` + +The plugin returns a JSON object. Return values: + +* `carrierName`: String - the Service Provider Name (SPN) +* `countryCode`: String - the ISO country code equivalent for the SIM provider's country code +* `mcc`: String - the MCC (mobile country code) of the provider of the SIM +* `mnc`: String - the MNC (mobile network code) of the provider of the SIM + +Field carrierName may remain empty, dependent on the mobile provider. + +On Windows Phone access to countryCode, MCC and MNC is not made provided (returns empty string). + +You can extract country and carrier data from MCC and MNC codes, read further on [Wikipedia](http://en.wikipedia.org/wiki/Mobile_country_code) and [ITU-T](http://www.itu.int/pub/T-SP-E.212B-2014). + + +## Android Quirks + +Additional return values: + +* `phoneNumber`: String - phone number string for line 1, for example, the [MSISDN](http://en.wikipedia.org/wiki/MSISDN) for a GSM phone +* `callState`: Number - the call state (cellular) on the device +* `dataActivity`: Number - the type of activity on a data connection (cellular) +* `networkType`: Number - the NETWORK_TYPE_xxxx for current data connection +* `phoneType`: Number - the device phone type. This indicates the type of radio used to transmit voice calls +* `simState`: Number - the state of the device SIM card + +Notice: the content of phoneNumber is unreliable (see [this](http://stackoverflow.com/questions/7922734/getting-reliable-msisdn-from-android-phone-voicemailnumber-line1number) and [this](http://stackoverflow.com/questions/25861064/retrieving-line1-number-from-telephonymanager-in-android) article). + + +### List of Call State Codes and Meanings + +| Code | Constant | Meaning +|-----:|:------------------------------|-------- +| 0 | `CALL_STATE_IDLE` | No activity +| 1 | `CALL_STATE_RINGING` | Ringing. A new call arrived and is ringing or waiting. In the latter case, another call is already active. +| 2 | `CALL_STATE_OFFHOOK` | Off-hook. At least one call exists that is dialing, active, or on hold, and no calls are ringing or waiting. + + +### List of Data Activity Codes and Meanings + +| Code | Constant | Meaning +|-----:|:------------------------------|-------- +| 0 | `DATA_ACTIVITY_NONE` | No traffic. +| 1 | `DATA_ACTIVITY_IN` | Currently receiving IP PPP traffic. +| 2 | `DATA_ACTIVITY_OUT` | Currently sending IP PPP traffic. +| 3 | `DATA_ACTIVITY_INOUT` | Currently both sending and receiving IP PPP traffic. +| 4 | `DATA_ACTIVITY_DORMANT` | Data connection is active, but physical link is down + + +### List of Network Type Codes and Meanings + +| Code | Constant | Meaning +|-----:|:------------------------------|-------- +| 0 | `NETWORK_TYPE_UNKNOWN` | unknown +| 1 | `NETWORK_TYPE_GPRS` | GPRS +| 2 | `NETWORK_TYPE_EDGE` | EDGE +| 3 | `NETWORK_TYPE_UMTS` | UMTS +| 4 | `NETWORK_TYPE_CDMA` | CDMA: Either IS95A or IS95B +| 5 | `NETWORK_TYPE_EVDO_0` | EVDO revision 0 +| 6 | `NETWORK_TYPE_EVDO_A` | EVDO revision A +| 7 | `NETWORK_TYPE_1xRTT` | 1xRTT +| 8 | `NETWORK_TYPE_HSDPA` | HSDPA +| 9 | `NETWORK_TYPE_HSUPA` | HSUPA +| 10 | `NETWORK_TYPE_HSPA` | HSPA +| 11 | `NETWORK_TYPE_IDEN` | iDen +| 12 | `NETWORK_TYPE_EVDO_B` | EVDO revision B +| 13 | `NETWORK_TYPE_LTE` | LTE +| 14 | `NETWORK_TYPE_EHRPD` | eHRPD +| 15 | `NETWORK_TYPE_HSPAP` | HSPA+ + + +### List of Phone Type Codes and Meanings + +| Code | Constant | Meaning +|-----:|:------------------------------|-------- +| 0 | `PHONE_TYPE_NONE` | none +| 1 | `PHONE_TYPE_GSM` | GSM +| 2 | `PHONE_TYPE_CDMA` | CDMA +| 3 | `PHONE_TYPE_SIP` | SIP + + +### List of SIM State Codes and Meanings + +| Code | Constant | Meaning +|-----:|:------------------------------|-------- +| 0 | `SIM_STATE_UNKNOWN` | Unknown. Signifies that the SIM is in transition between states. For example, when the user inputs the SIM pin under PIN_REQUIRED state, a query for sim status returns this state before turning to SIM_STATE_READY. +| 1 | `SIM_STATE_ABSENT` | No SIM card is available in the device +| 2 | `SIM_STATE_PIN_REQUIRED` | Locked: requires the user's SIM PIN to unlock +| 3 | `SIM_STATE_PUK_REQUIRED` | Locked: requires the user's SIM PUK to unlock +| 4 | `SIM_STATE_NETWORK_LOCKED` | Locked: requries a network PIN to unlock +| 5 | `SIM_STATE_READY` | Ready + + +## iOS Quirks + +Additional return value: + +* `allowsVOIP`: Boolean - indicates if the carrier allows VoIP calls to be made on its network + + +## Windows Phone Quirks + +Additional return values: + +* `isCellularDataEnabled`: Boolean - indicates whether the network is cellular data enabled +* `isCellularDataRoamingEnabled`: Boolean - indicates whether the network allows data roaming +* `IsNetworkAvailable`: Boolean - indicates whether the network is available +* `isWiFiEnabled`: Boolean - indicates whether the network is Wi-Fi enabled + + +## Author + +#### Peter Bakondy + +- https://github.com/pbakondy + + +## LICENSE + +cordova-plugin-sim is licensed under the MIT Open Source license. For more information, see the LICENSE file in this repository. diff --git a/plugin.xml b/plugin.xml new file mode 100644 index 0000000..be2de49 --- /dev/null +++ b/plugin.xml @@ -0,0 +1,79 @@ + + + SIM + A plugin to get the device's SIM data (carrier name, mcc mnc, country code, telephonenumber, etc) + MIT + cordova,sim,carrier,mcc,mnc,telephonenumber + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/android/com/pbakondy/Sim.java b/src/android/com/pbakondy/Sim.java new file mode 100644 index 0000000..5d98f9f --- /dev/null +++ b/src/android/com/pbakondy/Sim.java @@ -0,0 +1,72 @@ +// MCC and MNC codes on Wikipedia +// http://en.wikipedia.org/wiki/Mobile_country_code + +// Mobile Network Codes (MNC) for the international identification plan for public networks and subscriptions +// http://www.itu.int/pub/T-SP-E.212B-2014 + +// class TelephonyManager +// http://developer.android.com/reference/android/telephony/TelephonyManager.html + +package com.pbakondy; + +import android.app.Activity; +import org.apache.cordova.CallbackContext; +import org.apache.cordova.CordovaPlugin; +import org.json.JSONObject; +import org.json.JSONArray; +import org.json.JSONException; + +import android.content.Context; +import android.telephony.TelephonyManager; + +public class Sim extends CordovaPlugin { + + @Override + public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { + if (action.equals("getSimInfo")) { + Context context = this.cordova.getActivity().getApplicationContext(); + + TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); + + String phoneNumber = manager.getLine1Number(); + String countryCode = manager.getSimCountryIso(); + String simOperator = manager.getSimOperator(); + String carrierName = manager.getSimOperatorName(); + + int callState = manager.getCallState(); + int dataActivity = manager.getDataActivity(); + int networkType = manager.getNetworkType(); + int phoneType = manager.getPhoneType(); + int simState = manager.getSimState(); + + String mcc = ""; + String mnc = ""; + + if (simOperator.length() >= 3) { + mcc = simOperator.substring(0, 3); + mnc = simOperator.substring(3); + } + + + JSONObject result = new JSONObject(); + + result.put("carrierName", carrierName); + result.put("countryCode", countryCode); + result.put("mcc", mcc); + result.put("mnc", mnc); + result.put("phoneNumber", phoneNumber); + + result.put("callState", callState); + result.put("dataActivity", dataActivity); + result.put("networkType", networkType); + result.put("phoneType", phoneType); + result.put("simState", simState); + + callbackContext.success(result); + + return true; + } else { + return false; + } + } +} diff --git a/src/ios/Sim.h b/src/ios/Sim.h new file mode 100644 index 0000000..d14ff26 --- /dev/null +++ b/src/ios/Sim.h @@ -0,0 +1,9 @@ +#import +#import +#import + +@interface Sim : CDVPlugin + +- (void)getSimInfo:(CDVInvokedUrlCommand*)command; + +@end diff --git a/src/ios/Sim.m b/src/ios/Sim.m new file mode 100644 index 0000000..a971528 --- /dev/null +++ b/src/ios/Sim.m @@ -0,0 +1,53 @@ +// MCC and MNC codes on Wikipedia +// http://en.wikipedia.org/wiki/Mobile_country_code + +// Mobile Network Codes (MNC) for the international identification plan for public networks and subscriptions +// http://www.itu.int/pub/T-SP-E.212B-2014 + +// class CTCarrier +// https://developer.apple.com/library/prerelease/ios/documentation/NetworkingInternet/Reference/CTCarrier/index.html + +#import "Sim.h" +#import +#import + +@implementation Sim + +- (void)getSimInfo:(CDVInvokedUrlCommand*)command +{ + CTTelephonyNetworkInfo *netinfo = [[CTTelephonyNetworkInfo alloc] init]; + CTCarrier *carrier = [netinfo subscriberCellularProvider]; + + BOOL allowsVOIPResult = [carrier allowsVOIP]; + NSString *carrierNameResult = [carrier carrierName]; + NSString *carrierCountryResult = [carrier isoCountryCode]; + NSString *carrierCodeResult = [carrier mobileCountryCode]; + NSString *carrierNetworkResult = [carrier mobileNetworkCode]; + + if (!carrierNameResult) { + carrierNameResult = @""; + } + if (!carrierCountryResult) { + carrierCountryResult = @""; + } + if (!carrierCodeResult) { + carrierCodeResult = @""; + } + if (!carrierNetworkResult) { + carrierNetworkResult = @""; + } + + NSDictionary *simData = [NSDictionary dictionaryWithObjectsAndKeys: + @(allowsVOIPResult), @"allowsVOIP", + carrierNameResult, @"carrierName", + carrierCountryResult, @"countryCode", + carrierCodeResult, @"mcc", + carrierNetworkResult, @"mnc", + nil]; + + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:simData]; + + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; +} + +@end diff --git a/src/wp/Sim.cs b/src/wp/Sim.cs new file mode 100644 index 0000000..afaca87 --- /dev/null +++ b/src/wp/Sim.cs @@ -0,0 +1,72 @@ +// DeviceNetworkInformation +// https://msdn.microsoft.com/en-us/library/windows/apps/microsoft.phone.net.networkinformation.devicenetworkinformation(v=vs.105).aspx + +using System; +using Microsoft.Phone.Net.NetworkInformation; + +namespace WPCordovaClassLib.Cordova.Commands +{ + public class Sim : BaseCommand + { + public void getSimInfo(string notused) + { + + string res = String.Format("\"carrierName\":\"{0}\",\"countryCode\":\"\",\"mcc\":\"\",\"mnc\":\"\",\"isCellularDataEnabled\":\"{1}\",\"isCellularDataRoamingEnabled\":\"{2}\",\"IsNetworkAvailable\":\"{3}\",\"isWiFiEnabled\":\"{4}\"", + this.CellularMobileOperator, + this.IsCellularDataEnabled, + this.IsCellularDataRoamingEnabled, + this.IsNetworkAvailable, + this.IsWiFiEnabled); + + res = "{" + res + "}"; + + DispatchCommandResult(new PluginResult(PluginResult.Status.OK, res)); + } + + // Gets the name of the cellular mobile operator. + public string CellularMobileOperator + { + get + { + return DeviceNetworkInformation.CellularMobileOperator; + } + } + + // Gets a value indicating whether the network is cellular data enabled. + public bool IsCellularDataEnabled + { + get + { + return DeviceNetworkInformation.IsCellularDataEnabled; + } + } + + // Gets a value indicating whether the network allows data roaming. + public bool IsCellularDataRoamingEnabled + { + get + { + return DeviceNetworkInformation.IsCellularDataRoamingEnabled; + } + } + + // Gets a value indicating whether the network is available. + public bool IsNetworkAvailable + { + get + { + return DeviceNetworkInformation.IsNetworkAvailable; + } + } + + // Gets a value indicating whether the network is Wi-Fi enabled. + public bool IsWiFiEnabled + { + get + { + return DeviceNetworkInformation.IsWiFiEnabled; + } + } + + } +} diff --git a/www/sim.js b/www/sim.js new file mode 100644 index 0000000..7886ddc --- /dev/null +++ b/www/sim.js @@ -0,0 +1,14 @@ +var sim = { + getSimInfo: function (successCallback, errorCallback) { + cordova.exec(successCallback, errorCallback, 'Sim', 'getSimInfo', []); + } +} + +cordova.addConstructor(function () { + if (!window.plugins) { + window.plugins = {}; + } + + window.plugins.sim = sim; + return window.plugins.sim; +});