-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMyPhoneNumberPlugin.java
More file actions
72 lines (62 loc) · 2.23 KB
/
MyPhoneNumberPlugin.java
File metadata and controls
72 lines (62 loc) · 2.23 KB
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
package com.TravelingTechGuy.ImAlive;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.util.Log;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import com.phonegap.api.PluginResult.Status;
/**
* @author Guy Vider
*
*/
public class MyPhoneNumberPlugin extends Plugin {
@Override
public PluginResult execute(String action, JSONArray data, String callbackId) {
Log.d("MyPhoneNumberPlugin", "Plugin called");
PluginResult result = null;
try {
JSONObject number = getMyPhoneNumber();
Log.d("MyPhoneNumberPlugin", "Returning "+ number.toString());
result = new PluginResult(Status.OK, number);
}
catch (Exception ex) {
Log.d("MyPhoneNumberPlugin", "Got Exception "+ ex.getMessage());
result = new PluginResult(Status.ERROR);
}
return result;
}
private JSONObject getMyPhoneNumber() throws JSONException {
Log.d("MyPhoneNumberPlugin", "at getMyPhoneNumber");
JSONObject result = new JSONObject();
TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
String number = tm.getLine1Number();
if(number.equals("") || number == null) {
Log.d("MyPhoneNumberPlugin", "We're on a non-phone device. Returning a hash of the UDID");
number = md5(tm.getDeviceId()).substring(0, 10);
}
Log.d("MyPhoneNumberPlugin", "Phone number=" + number);
result.put("phoneNumber", number);
return result;
}
private String md5(String s) {
try {
// Create MD5 Hash
MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
digest.update(s.getBytes());
byte messageDigest[] = digest.digest();
// Create Hex String
StringBuffer hexString = new StringBuffer();
for (int i=0; i<messageDigest.length; i++)
hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
Log.d("MyPhoneNumberPlugin", e.getMessage());
}
return "";
}
}