Skip to content
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
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
# 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).

If you have installed PhoneGap CLI, run the following code from the command line:
<pre>phonegap local plugin add https://github.com/vliesaputra/DeviceInformationPlugin</pre>

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.
<pre>
&lt;script type="text/javascript" charset="utf-8" src="phonegap.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" charset="utf-8" src="deviceinformation.js"&gt;&lt;/script&gt;
</pre>

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:
<pre>
Expand Down
16 changes: 15 additions & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

<platform name="android">

<source-file src="src/com/vliesaputra/cordova/plugins/DeviceInformation.java" target-dir="src/com/vliesaputra/cordova/plugins" />
<source-file src="src/android/DeviceInformation.java" target-dir="src/com/vliesaputra/cordova/plugins" />

<config-file target="res/xml/config.xml" parent="/*">
<feature name="DeviceInformation">
Expand All @@ -37,5 +37,19 @@
</config-file>
</platform>

<!-- ios -->
<platform name="ios">
<config-file target="config.xml" parent="/*">
<feature name="DeviceInformation">
<param name="ios-package" value="CDVDeviceInformation"/>
</feature>
</config-file>

<framework src="CoreTelephony.framework" />

<header-file src="src/ios/CDVDeviceInformation.h" />
<source-file src="src/ios/CDVDeviceInformation.m" />
</platform>

</plugin>

Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
}

Expand All @@ -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;
Expand Down
28 changes: 28 additions & 0 deletions src/ios/CDVDeviceInformation.h
Original file line number Diff line number Diff line change
@@ -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 <UIKit/UIKit.h>
#import <Cordova/CDVPlugin.h>

@interface CDVDeviceInformation : CDVPlugin
{}

- (void)get:(CDVInvokedUrlCommand*)command;

@end
34 changes: 34 additions & 0 deletions src/ios/CDVDeviceInformation.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <sys/types.h>
#include <sys/sysctl.h>

#import <Cordova/CDV.h>
#import "CDVDeviceInformation.h"
#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>

@interface CDVDeviceInformation () {}
@end

@implementation CDVDeviceInformation

- (void)get:(CDVInvokedUrlCommand*)command
{
NSString* deviceProperties = [self deviceProperties];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: deviceProperties];

[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (NSString*)deviceProperties
{
CTTelephonyNetworkInfo *networkInfo = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = [networkInfo subscriberCellularProvider];

NSString *scc = [carrier isoCountryCode];

NSString *result = [NSString stringWithFormat:@"{\"simCountry\":\"%@\"}", scc ?: @""];

return result;
}

@end