-
Notifications
You must be signed in to change notification settings - Fork 96
/
UniqueDeviceID.java
109 lines (91 loc) · 4.06 KB
/
UniqueDeviceID.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package hu.dpal.phonegap.plugins;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.json.JSONArray;
import org.json.JSONException;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
import android.util.Log;
import java.lang.reflect.Method;
public class UniqueDeviceID extends CordovaPlugin {
public static final String TAG = "UniqueDeviceID";
public CallbackContext callbackContext;
public static final int REQUEST_READ_PHONE_STATE = 0;
protected final static String permission = Manifest.permission.READ_PHONE_STATE;
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
this.callbackContext = callbackContext;
try {
if (action.equals("get")) {
if(this.hasPermission(permission)){
getDeviceId();
}else{
this.requestPermission(this, REQUEST_READ_PHONE_STATE, permission);
}
}else {
this.callbackContext.error("Invalid action");
return false;
}
}catch(Exception e ) {
this.callbackContext.error("Exception occurred: ".concat(e.getMessage()));
return false;
}
return true;
}
public void onRequestPermissionResult(int requestCode, String[] permissions,
int[] grantResults) throws JSONException {
if(requestCode == REQUEST_READ_PHONE_STATE){
getDeviceId();
}
}
protected void getDeviceId(){
try {
Context context = cordova.getActivity().getApplicationContext();
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String uuid;
String androidID = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
String deviceID = tm.getDeviceId();
String simID = tm.getSimSerialNumber();
if ("9774d56d682e549c".equals(androidID) || androidID == null) {
androidID = "";
}
if (deviceID == null) {
deviceID = "";
}
if (simID == null) {
simID = "";
}
uuid = androidID + deviceID + simID;
uuid = String.format("%32s", uuid).replace(' ', '0');
uuid = uuid.substring(0, 32);
uuid = uuid.replaceAll("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})", "$1-$2-$3-$4-$5");
this.callbackContext.success(uuid);
}catch(Exception e ) {
this.callbackContext.error("Exception occurred: ".concat(e.getMessage()));
}
}
private boolean hasPermission(String permission) throws Exception{
boolean hasPermission = true;
Method method = null;
try {
method = cordova.getClass().getMethod("hasPermission", permission.getClass());
Boolean bool = (Boolean) method.invoke(cordova, permission);
hasPermission = bool.booleanValue();
} catch (NoSuchMethodException e) {
Log.w(TAG, "Cordova v" + CordovaWebView.CORDOVA_VERSION + " does not support API 23 runtime permissions so defaulting to GRANTED for " + permission);
}
return hasPermission;
}
private void requestPermission(CordovaPlugin plugin, int requestCode, String permission) throws Exception{
try {
java.lang.reflect.Method method = cordova.getClass().getMethod("requestPermission", org.apache.cordova.CordovaPlugin.class ,int.class, java.lang.String.class);
method.invoke(cordova, plugin, requestCode, permission);
} catch (NoSuchMethodException e) {
throw new Exception("requestPermission() method not found in CordovaInterface implementation of Cordova v" + CordovaWebView.CORDOVA_VERSION);
}
}
}