forked from pbakondy/cordova-plugin-sim
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Sim.java
272 lines (214 loc) · 10 KB
/
Sim.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// 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
// https://github.com/android/platform_frameworks_base/blob/master/telephony/java/android/telephony/TelephonyManager.java
// permissions
// http://developer.android.com/training/permissions/requesting.html
// Multiple SIM Card Support
// https://developer.android.com/about/versions/android-5.1.html
// class SubscriptionManager
// https://developer.android.com/reference/android/telephony/SubscriptionManager.html
// https://github.com/android/platform_frameworks_base/blob/master/telephony/java/android/telephony/SubscriptionManager.java
// class SubscriptionInfo
// https://developer.android.com/reference/android/telephony/SubscriptionInfo.html
// https://github.com/android/platform_frameworks_base/blob/master/telephony/java/android/telephony/SubscriptionInfo.java
// Cordova Permissions API
// https://cordova.apache.org/docs/en/latest/guide/platforms/android/plugin.html#android-permissions
package com.pbakondy;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.apache.cordova.LOG;
import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONException;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.Manifest;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import java.lang.reflect.Method;
import java.util.List;
public class Sim extends CordovaPlugin {
private static final String LOG_TAG = "CordovaPluginSim";
private static final String GET_SIM_INFO = "getSimInfo";
private static final String HAS_READ_PERMISSION = "hasReadPermission";
private static final String REQUEST_READ_PERMISSION = "requestReadPermission";
private CallbackContext callback;
@SuppressLint("HardwareIds")
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
callback = callbackContext;
if (GET_SIM_INFO.equals(action)) {
Context context = this.cordova.getActivity().getApplicationContext();
TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
// dual SIM detection with SubscriptionManager API
// requires API 22
// requires permission READ_PHONE_STATE
JSONArray sims = null;
Integer phoneCount = null;
Integer activeSubscriptionInfoCount = null;
Integer activeSubscriptionInfoCountMax = null;
try {
// TelephonyManager.getPhoneCount() requires API 23
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
phoneCount = manager.getActiveModemCount();
}
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
if (simPermissionGranted(Manifest.permission.READ_PHONE_STATE)) {
SubscriptionManager subscriptionManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
activeSubscriptionInfoCount = subscriptionManager.getActiveSubscriptionInfoCount();
activeSubscriptionInfoCountMax = subscriptionManager.getActiveSubscriptionInfoCountMax();
sims = new JSONArray();
List<SubscriptionInfo> subscriptionInfos = subscriptionManager.getActiveSubscriptionInfoList();
for (SubscriptionInfo subscriptionInfo : subscriptionInfos) {
CharSequence carrierName = subscriptionInfo.getCarrierName();
String countryIso = subscriptionInfo.getCountryIso();
int dataRoaming = subscriptionInfo.getDataRoaming(); // 1 is enabled ; 0 is disabled
CharSequence displayName = subscriptionInfo.getDisplayName();
String iccId = subscriptionInfo.getIccId();
int mcc = subscriptionInfo.getMcc();
int mnc = subscriptionInfo.getMnc();
String number = subscriptionInfo.getNumber();
int simSlotIndex = subscriptionInfo.getSimSlotIndex();
int subscriptionId = subscriptionInfo.getSubscriptionId();
boolean networkRoaming = subscriptionManager.isNetworkRoaming(simSlotIndex);
String deviceId = null;
// TelephonyManager.getDeviceId(slotId) requires API 23
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
deviceId = manager.getDeviceId(simSlotIndex);
}
JSONObject simData = new JSONObject();
simData.put("carrierName", carrierName.toString());
simData.put("displayName", displayName.toString());
simData.put("countryCode", countryIso);
simData.put("mcc", mcc);
simData.put("mnc", mnc);
simData.put("isNetworkRoaming", networkRoaming);
simData.put("isDataRoaming", (dataRoaming == 1));
simData.put("simSlotIndex", simSlotIndex);
simData.put("phoneNumber", number);
if (deviceId != null) {
simData.put("deviceId", deviceId);
}
simData.put("simSerialNumber", iccId);
simData.put("subscriptionId", subscriptionId);
sims.put(simData);
}
}
}
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
Log.w("manager result ", manager.toString());
String phoneNumber = null;
String countryCode = manager.getSimCountryIso();
String simOperator = manager.getSimOperator();
String carrierName = manager.getSimOperatorName();
String deviceId = null;
String deviceSoftwareVersion = null;
String simSerialNumber = null;
String subscriberId = null;
int dataActivity = manager.getDataActivity();
int phoneType = manager.getPhoneType();
int simState = manager.getSimState();
boolean isNetworkRoaming = manager.isNetworkRoaming();
// if (simPermissionGranted(Manifest.permission.READ_PHONE_STATE)) {
// phoneNumber = manager.getLine1Number();
// deviceId = manager.getDeviceId();
// deviceSoftwareVersion = manager.getDeviceSoftwareVersion();
// simSerialNumber = manager.getSimSerialNumber();
// subscriberId = manager.getSubscriberId();
// }
String mcc = "";
String mnc = "";
if (simOperator.length() >= 3) {
mcc = simOperator.substring(0, 3);
mnc = simOperator.substring(3);
}
JSONObject result = new JSONObject();
Log.w("sim result ", result.toString());
result.put("carrierName", carrierName);
result.put("countryCode", countryCode);
result.put("mcc", mcc);
result.put("mnc", mnc);
result.put("dataActivity", dataActivity);
result.put("phoneType", phoneType);
result.put("simState", simState);
result.put("isNetworkRoaming", isNetworkRoaming);
if (phoneCount != null) {
result.put("phoneCount", (int)phoneCount);
}
if (activeSubscriptionInfoCount != null) {
result.put("activeSubscriptionInfoCount", (int)activeSubscriptionInfoCount);
}
if (activeSubscriptionInfoCountMax != null) {
result.put("activeSubscriptionInfoCountMax", (int)activeSubscriptionInfoCountMax);
}
// if (simPermissionGranted(Manifest.permission.READ_PHONE_STATE)) {
// result.put("phoneNumber", phoneNumber);
// result.put("deviceId", deviceId);
// result.put("deviceSoftwareVersion", deviceSoftwareVersion);
// result.put("simSerialNumber", simSerialNumber);
// result.put("subscriberId", subscriberId);
// }
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
// android.telephony.SubscriptionManager#getDefaultDataSubscriptionId requires API 24
result.put("defaultDataSubscriptionId", SubscriptionManager.getDefaultDataSubscriptionId());
} else if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
// android.telephony.SubscriptionManager#getDefaultDataSubId requires API 22, must be accessed through reflection
try {
Method getDefaultDataSubId = SubscriptionManager.class.getDeclaredMethod("getDefaultDataSubId");
getDefaultDataSubId.setAccessible(true);
result.put("defaultDataSubscriptionId", getDefaultDataSubId.invoke(null));
} catch (Exception e) {
Log.w("Sim", e);
}
}
if (sims != null && sims.length() != 0) {
result.put("cards", sims);
}
callbackContext.success(result);
return true;
} else if (HAS_READ_PERMISSION.equals(action)) {
hasReadPermission();
return true;
} else if (REQUEST_READ_PERMISSION.equals(action)) {
requestReadPermission();
return true;
} else {
return false;
}
}
private void hasReadPermission() {
this.callback.sendPluginResult(new PluginResult(PluginResult.Status.OK,
simPermissionGranted(Manifest.permission.READ_PHONE_STATE)));
}
private void requestReadPermission() {
requestPermission(Manifest.permission.READ_PHONE_STATE);
}
private boolean simPermissionGranted(String type) {
if (Build.VERSION.SDK_INT < 23) {
return true;
}
return true;
}
private void requestPermission(String type) {
LOG.i(LOG_TAG, "requestPermission");
this.callback.success();
}
@Override
public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException
{
this.callback.success();
}
}