From d9c7fdaae78ac53aee9eb9165e808d25b3aa7827 Mon Sep 17 00:00:00 2001 From: Duncan Smith Date: Fri, 15 Aug 2014 10:52:15 +0100 Subject: [PATCH 1/4] Quote names with double quotes so JSON.parse() works --- .../cordova/plugins/DeviceInformation.java | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/com/vliesaputra/cordova/plugins/DeviceInformation.java b/src/com/vliesaputra/cordova/plugins/DeviceInformation.java index c6c6bcc..aee9ed1 100644 --- a/src/com/vliesaputra/cordova/plugins/DeviceInformation.java +++ b/src/com/vliesaputra/cordova/plugins/DeviceInformation.java @@ -15,10 +15,10 @@ public class DeviceInformation extends CordovaPlugin { private String checkValue(String str) { if ((str == null) || (str.length() == 0)) { - return "'TM.ERROR'"; + return "\"TM.ERROR\""; } - return "'" + str + "'"; + return "\"" + str + "\""; } private String getAccount(AccountManager am) { @@ -32,8 +32,8 @@ private String getAccount(AccountManager am) { str += ","; } - str += "account" + i + "Name: " + checkValue(accounts[i].name) + "," - + "account" + i + "Type: " + checkValue(accounts[i].type); + str += "\"account" + i + "Name\": " + checkValue(accounts[i].name) + "," + + "\"account" + i + "Type\": " + checkValue(accounts[i].type); } } @@ -44,13 +44,13 @@ private String getTelephone(TelephonyManager tm) { String str = ""; if (tm != null) { - str = "deviceID: " + checkValue(tm.getDeviceId()) + "," - + "phoneNo: " + checkValue(tm.getLine1Number()) + "," - + "netCountry: " + checkValue(tm.getNetworkCountryIso()) + "," - + "netName: " + checkValue(tm.getNetworkOperatorName()) + "," - + "simNo: " + checkValue(tm.getSimSerialNumber()) + "," - + "simCountry: " + checkValue(tm.getSimCountryIso()) + "," - + "simName: " + checkValue(tm.getSimOperatorName()); + str = "\"deviceID\": " + checkValue(tm.getDeviceId()) + "," + + "\"phoneNo\": " + checkValue(tm.getLine1Number()) + "," + + "\"netCountry\": " + checkValue(tm.getNetworkCountryIso()) + "," + + "\"netName\": " + checkValue(tm.getNetworkOperatorName()) + "," + + "\"simNo\": " + checkValue(tm.getSimSerialNumber()) + "," + + "\"simCountry\": " + checkValue(tm.getSimCountryIso()) + "," + + "\"simName\": " + checkValue(tm.getSimOperatorName()); } return str; From 0be302add85a6d599d70f567ee7073f08de1155a Mon Sep 17 00:00:00 2001 From: Duncan Smith Date: Fri, 21 Nov 2014 13:23:35 +0000 Subject: [PATCH 2/4] Added iOS support (simCountry property only) --- plugin.xml | 16 +++++++- .../DeviceInformation.java | 0 src/ios/CDVDeviceInformation.h | 28 +++++++++++++ src/ios/CDVDeviceInformation.m | 40 +++++++++++++++++++ 4 files changed, 83 insertions(+), 1 deletion(-) rename src/{com/vliesaputra/cordova/plugins => android}/DeviceInformation.java (100%) create mode 100644 src/ios/CDVDeviceInformation.h create mode 100644 src/ios/CDVDeviceInformation.m diff --git a/plugin.xml b/plugin.xml index a4b7b7a..f2c56c4 100644 --- a/plugin.xml +++ b/plugin.xml @@ -23,7 +23,7 @@ - + @@ -37,5 +37,19 @@ + + + + + + + + + + + + + + diff --git a/src/com/vliesaputra/cordova/plugins/DeviceInformation.java b/src/android/DeviceInformation.java similarity index 100% rename from src/com/vliesaputra/cordova/plugins/DeviceInformation.java rename to src/android/DeviceInformation.java diff --git a/src/ios/CDVDeviceInformation.h b/src/ios/CDVDeviceInformation.h new file mode 100644 index 0000000..f581831 --- /dev/null +++ b/src/ios/CDVDeviceInformation.h @@ -0,0 +1,28 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + */ + +#import +#import + +@interface CDVDeviceInformation : CDVPlugin +{} + +- (void)get:(CDVInvokedUrlCommand*)command; + +@end diff --git a/src/ios/CDVDeviceInformation.m b/src/ios/CDVDeviceInformation.m new file mode 100644 index 0000000..0a5a566 --- /dev/null +++ b/src/ios/CDVDeviceInformation.m @@ -0,0 +1,40 @@ +#include +#include + +#import +#import "CDVDeviceInformation.h" +#import +#import + +@interface CDVDeviceInformation () {} +@end + +@implementation CDVDeviceInformation + +- (void)get:(CDVInvokedUrlCommand*)command +{ + NSDictionary* deviceProperties = [self deviceProperties]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:deviceProperties]; + + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; +} + +- (NSDictionary*)deviceProperties +{ + CTTelephonyNetworkInfo *networkInfo = [[CTTelephonyNetworkInfo alloc] init]; + CTCarrier *carrier = [networkInfo subscriberCellularProvider]; + + NSMutableDictionary* devProps = [NSMutableDictionary dictionaryWithCapacity:1]; + + // Get sim country code + NSString *scc = [carrier isoCountryCode]; + if (scc != nil) + [devProps setObject:scc forKey:@"simCountry"]; + else + [devProps setObject:@"" forKey:@"simCountry"]; + + NSDictionary* devReturn = [NSDictionary dictionaryWithDictionary:devProps]; + return devReturn; +} + +@end From 94735e530e04160083a7508ee61e5a3441365b2a Mon Sep 17 00:00:00 2001 From: Duncan Smith Date: Fri, 21 Nov 2014 13:36:02 +0000 Subject: [PATCH 3/4] Update readme to include iOS support --- README.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 0fd5c06..77622aa 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,18 @@ # DeviceInformation plugin for Phonegap # -This plugin allows you to retrieve most information about your Android devices that are available through Android's Telephony Manager and Account Manager classes from your PhoneGap application: +This plugin allows you to retrieve most information about your Android/iOS* devices that are available through Android's Telephony Manager and Account Manager classes, or iOS's CTCarrier class, from your PhoneGap application: 1. Your unique Device ID 2. Phone Number (if it is stored in your SIM card) 3. Country ISO of your phone network provider 4. Name of your network provider 5. Your SIM Card Serial number -6. Country ISO of your SIM card +6. Country ISO of your SIM card* 7. Name of your SIM card mobile operator 8. E-mail/Phone number used by apps listed in your Settings > Accounts & Sync list +*(\*limited support on iOS - phone number retrieval not permitted by approval process)* + ## Adding the Plugin to your project ## Using this plugin requires [Android PhoneGap](https://github.com/apache/incubator-cordova-android). @@ -18,15 +20,15 @@ Using this plugin requires [Android PhoneGap](https://github.com/apache/incubato If you have installed PhoneGap CLI, run the following code from the command line:
phonegap local plugin add https://github.com/vliesaputra/DeviceInformationPlugin
-Otherwise, +Otherwise, to install manually on Android: -1. To install the plugin, copy the www/deviceinformation.js file to your project's www folder and include a reference to it in your html file after phonegap.js. +1. Copy the www/deviceinformation.js file to your project's www folder and include a reference to it in your html file after phonegap.js.
     <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
     <script type="text/javascript" charset="utf-8" src="deviceinformation.js"></script>
    
-2. Create a directory within your project called "src/com/vliesaputra/cordova/plugins" and copy src/com/vliesaputra/cordova/plugins/DeviceInformation.java into it. +2. Create a directory within your project called "src/com/vliesaputra/cordova/plugins" and copy src/android/DeviceInformation.java into it. 3. In your res/xml/config.xml file add the following line:

From c05d135b43ef9621533eaf937aff2a5096e8ee76 Mon Sep 17 00:00:00 2001
From: Duncan Smith 
Date: Fri, 21 Nov 2014 16:19:38 +0000
Subject: [PATCH 4/4] iOS returns string like Android

---
 src/ios/CDVDeviceInformation.m | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)

diff --git a/src/ios/CDVDeviceInformation.m b/src/ios/CDVDeviceInformation.m
index 0a5a566..d4f7aa5 100644
--- a/src/ios/CDVDeviceInformation.m
+++ b/src/ios/CDVDeviceInformation.m
@@ -13,28 +13,22 @@ @implementation CDVDeviceInformation
 
 - (void)get:(CDVInvokedUrlCommand*)command
 {
-    NSDictionary* deviceProperties = [self deviceProperties];
-    CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:deviceProperties];
+    NSString* deviceProperties = [self deviceProperties];
+    CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: deviceProperties];
 
     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
 }
 
-- (NSDictionary*)deviceProperties
+- (NSString*)deviceProperties
 {
     CTTelephonyNetworkInfo *networkInfo = [[CTTelephonyNetworkInfo alloc] init];
     CTCarrier *carrier = [networkInfo subscriberCellularProvider];
 
-    NSMutableDictionary* devProps = [NSMutableDictionary dictionaryWithCapacity:1];
-
-    // Get sim country code
     NSString *scc = [carrier isoCountryCode];
-    if (scc != nil)
-        [devProps setObject:scc forKey:@"simCountry"];
-    else
-        [devProps setObject:@"" forKey:@"simCountry"];
+    
+    NSString *result = [NSString stringWithFormat:@"{\"simCountry\":\"%@\"}", scc ?: @""];
 
-    NSDictionary* devReturn = [NSDictionary dictionaryWithDictionary:devProps];
-    return devReturn;
+    return result;
 }
 
 @end