cordova.plugins.diagnostic 6.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/FUNDING.yml +6 -0
- package/.github/ISSUE_TEMPLATE/bug_report.md +123 -0
- package/.github/ISSUE_TEMPLATE/documentation-issue.md +36 -0
- package/.github/ISSUE_TEMPLATE/feature_request.md +41 -0
- package/.github/PULL_REQUEST_TEMPLATE.md +37 -0
- package/.github/stale.yml +17 -0
- package/CHANGELOG.md +390 -0
- package/README.md +3709 -0
- package/cordova.plugins.diagnostic.d.ts +1113 -0
- package/package.json +54 -0
- package/plugin.xml +496 -0
- package/scripts/apply-modules.js +165 -0
- package/scripts/logger.js +94 -0
- package/src/android/Diagnostic.java +856 -0
- package/src/android/Diagnostic_Bluetooth.java +297 -0
- package/src/android/Diagnostic_Camera.java +134 -0
- package/src/android/Diagnostic_External_Storage.java +273 -0
- package/src/android/Diagnostic_Location.java +319 -0
- package/src/android/Diagnostic_NFC.java +270 -0
- package/src/android/Diagnostic_Notifications.java +157 -0
- package/src/android/Diagnostic_Wifi.java +155 -0
- package/src/ios/Diagnostic.h +56 -0
- package/src/ios/Diagnostic.m +282 -0
- package/src/ios/Diagnostic_Bluetooth.h +24 -0
- package/src/ios/Diagnostic_Bluetooth.m +170 -0
- package/src/ios/Diagnostic_Calendar.h +24 -0
- package/src/ios/Diagnostic_Calendar.m +94 -0
- package/src/ios/Diagnostic_Camera.h +27 -0
- package/src/ios/Diagnostic_Camera.m +194 -0
- package/src/ios/Diagnostic_Contacts.h +24 -0
- package/src/ios/Diagnostic_Contacts.m +93 -0
- package/src/ios/Diagnostic_Location.h +31 -0
- package/src/ios/Diagnostic_Location.m +284 -0
- package/src/ios/Diagnostic_Microphone.h +21 -0
- package/src/ios/Diagnostic_Microphone.m +97 -0
- package/src/ios/Diagnostic_Motion.h +27 -0
- package/src/ios/Diagnostic_Motion.m +143 -0
- package/src/ios/Diagnostic_Notifications.h +22 -0
- package/src/ios/Diagnostic_Notifications.m +235 -0
- package/src/ios/Diagnostic_Reminders.h +24 -0
- package/src/ios/Diagnostic_Reminders.m +93 -0
- package/src/ios/Diagnostic_Wifi.h +19 -0
- package/src/ios/Diagnostic_Wifi.m +108 -0
- package/src/windows/diagnosticProxy.bluetooth.js +23 -0
- package/src/windows/diagnosticProxy.camera.js +35 -0
- package/src/windows/diagnosticProxy.js +137 -0
- package/src/windows/diagnosticProxy.location.js +54 -0
- package/src/windows/diagnosticProxy.wifi.js +18 -0
- package/www/android/diagnostic.bluetooth.js +211 -0
- package/www/android/diagnostic.calendar.js +90 -0
- package/www/android/diagnostic.camera.js +203 -0
- package/www/android/diagnostic.contacts.js +91 -0
- package/www/android/diagnostic.external_storage.js +102 -0
- package/www/android/diagnostic.js +1309 -0
- package/www/android/diagnostic.location.js +282 -0
- package/www/android/diagnostic.microphone.js +89 -0
- package/www/android/diagnostic.nfc.js +127 -0
- package/www/android/diagnostic.notifications.js +74 -0
- package/www/android/diagnostic.wifi.js +90 -0
- package/www/ios/diagnostic.bluetooth.js +127 -0
- package/www/ios/diagnostic.calendar.js +97 -0
- package/www/ios/diagnostic.camera.js +212 -0
- package/www/ios/diagnostic.contacts.js +98 -0
- package/www/ios/diagnostic.js +990 -0
- package/www/ios/diagnostic.location.js +236 -0
- package/www/ios/diagnostic.microphone.js +99 -0
- package/www/ios/diagnostic.motion.js +160 -0
- package/www/ios/diagnostic.notifications.js +189 -0
- package/www/ios/diagnostic.reminders.js +97 -0
- package/www/ios/diagnostic.wifi.js +80 -0
- package/www/windows/diagnostic.bluetooth.js +51 -0
- package/www/windows/diagnostic.camera.js +41 -0
- package/www/windows/diagnostic.js +169 -0
- package/www/windows/diagnostic.location.js +35 -0
- package/www/windows/diagnostic.wifi.js +51 -0
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Licensed to the Apache Software Foundation (ASF) under one
|
|
3
|
+
or more contributor license agreements. See the NOTICE file
|
|
4
|
+
distributed with this work for additional information
|
|
5
|
+
regarding copyright ownership. The ASF licenses this file
|
|
6
|
+
to you under the Apache License, Version 2.0 (the
|
|
7
|
+
"License"); you may not use this file except in compliance
|
|
8
|
+
with the License. You may obtain a copy of the License at
|
|
9
|
+
|
|
10
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
|
|
12
|
+
Unless required by applicable law or agreed to in writing,
|
|
13
|
+
software distributed under the License is distributed on an
|
|
14
|
+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
15
|
+
KIND, either express or implied. See the License for the
|
|
16
|
+
specific language governing permissions and limitations
|
|
17
|
+
under the License.
|
|
18
|
+
*/
|
|
19
|
+
package cordova.plugins;
|
|
20
|
+
|
|
21
|
+
/*
|
|
22
|
+
* Imports
|
|
23
|
+
*/
|
|
24
|
+
import org.apache.cordova.CordovaWebView;
|
|
25
|
+
import org.apache.cordova.CallbackContext;
|
|
26
|
+
import org.apache.cordova.CordovaPlugin;
|
|
27
|
+
import org.apache.cordova.CordovaInterface;
|
|
28
|
+
import org.apache.cordova.PluginResult;
|
|
29
|
+
import org.json.JSONArray;
|
|
30
|
+
import org.json.JSONException;
|
|
31
|
+
|
|
32
|
+
import android.content.BroadcastReceiver;
|
|
33
|
+
import android.content.IntentFilter;
|
|
34
|
+
import android.location.LocationManager;
|
|
35
|
+
import android.os.Build;
|
|
36
|
+
import android.util.Log;
|
|
37
|
+
|
|
38
|
+
import android.content.Context;
|
|
39
|
+
import android.content.Intent;
|
|
40
|
+
import android.provider.Settings;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Diagnostic plugin implementation for Android
|
|
44
|
+
*/
|
|
45
|
+
public class Diagnostic_Location extends CordovaPlugin{
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
/*************
|
|
49
|
+
* Constants *
|
|
50
|
+
*************/
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Tag for debug log messages
|
|
54
|
+
*/
|
|
55
|
+
public static final String TAG = "Diagnostic_Location";
|
|
56
|
+
|
|
57
|
+
private static String gpsLocationPermission = "ACCESS_FINE_LOCATION";
|
|
58
|
+
private static String networkLocationPermission = "ACCESS_COARSE_LOCATION";
|
|
59
|
+
private static String backgroundLocationPermission = "ACCESS_BACKGROUND_LOCATION";
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
private static final String LOCATION_MODE_HIGH_ACCURACY = "high_accuracy";
|
|
63
|
+
private static final String LOCATION_MODE_DEVICE_ONLY = "device_only";
|
|
64
|
+
private static final String LOCATION_MODE_BATTERY_SAVING = "battery_saving";
|
|
65
|
+
private static final String LOCATION_MODE_OFF = "location_off";
|
|
66
|
+
private static final String LOCATION_MODE_UNKNOWN = "unknown";
|
|
67
|
+
|
|
68
|
+
/*************
|
|
69
|
+
* Variables *
|
|
70
|
+
*************/
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Singleton class instance
|
|
74
|
+
*/
|
|
75
|
+
public static Diagnostic_Location instance = null;
|
|
76
|
+
|
|
77
|
+
private Diagnostic diagnostic;
|
|
78
|
+
|
|
79
|
+
public static LocationManager locationManager;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Current Cordova callback context (on this thread)
|
|
83
|
+
*/
|
|
84
|
+
protected CallbackContext currentContext;
|
|
85
|
+
|
|
86
|
+
private String currentLocationMode = null;
|
|
87
|
+
|
|
88
|
+
/*************
|
|
89
|
+
* Public API
|
|
90
|
+
************/
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Constructor.
|
|
94
|
+
*/
|
|
95
|
+
public Diagnostic_Location() {}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Sets the context of the Command. This can then be used to do things like
|
|
99
|
+
* get file paths associated with the Activity.
|
|
100
|
+
*
|
|
101
|
+
* @param cordova The context of the main Activity.
|
|
102
|
+
* @param webView The CordovaWebView Cordova is running in.
|
|
103
|
+
*/
|
|
104
|
+
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
|
|
105
|
+
Log.d(TAG, "initialize()");
|
|
106
|
+
instance = this;
|
|
107
|
+
diagnostic = Diagnostic.getInstance();
|
|
108
|
+
|
|
109
|
+
try {
|
|
110
|
+
diagnostic.applicationContext.registerReceiver(locationProviderChangedReceiver, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));
|
|
111
|
+
locationManager = (LocationManager) this.cordova.getActivity().getSystemService(Context.LOCATION_SERVICE);
|
|
112
|
+
}catch(Exception e){
|
|
113
|
+
diagnostic.logWarning("Unable to register Location Provider Change receiver: " + e.getMessage());
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
try {
|
|
117
|
+
currentLocationMode = getLocationModeName();
|
|
118
|
+
}catch(Exception e){
|
|
119
|
+
diagnostic.logWarning("Unable to get initial location mode: " + e.getMessage());
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
super.initialize(cordova, webView);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Called on destroying activity
|
|
127
|
+
*/
|
|
128
|
+
public void onDestroy() {
|
|
129
|
+
try {
|
|
130
|
+
diagnostic.applicationContext.unregisterReceiver(locationProviderChangedReceiver);
|
|
131
|
+
}catch(Exception e){
|
|
132
|
+
diagnostic.logWarning("Unable to unregister Location Provider Change receiver: " + e.getMessage());
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Executes the request and returns PluginResult.
|
|
138
|
+
*
|
|
139
|
+
* @param action The action to execute.
|
|
140
|
+
* @param args JSONArry of arguments for the plugin.
|
|
141
|
+
* @param callbackContext The callback id used when calling back into JavaScript.
|
|
142
|
+
* @return True if the action was valid, false if not.
|
|
143
|
+
*/
|
|
144
|
+
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
|
|
145
|
+
currentContext = callbackContext;
|
|
146
|
+
|
|
147
|
+
try {
|
|
148
|
+
if (action.equals("switchToLocationSettings")){
|
|
149
|
+
switchToLocationSettings();
|
|
150
|
+
callbackContext.success();
|
|
151
|
+
} else if(action.equals("isLocationAvailable")) {
|
|
152
|
+
callbackContext.success(isGpsLocationAvailable() || isNetworkLocationAvailable() ? 1 : 0);
|
|
153
|
+
} else if(action.equals("isLocationEnabled")) {
|
|
154
|
+
callbackContext.success(isGpsLocationEnabled() || isNetworkLocationEnabled() ? 1 : 0);
|
|
155
|
+
} else if(action.equals("isGpsLocationAvailable")) {
|
|
156
|
+
callbackContext.success(isGpsLocationAvailable() ? 1 : 0);
|
|
157
|
+
} else if(action.equals("isNetworkLocationAvailable")) {
|
|
158
|
+
callbackContext.success(isNetworkLocationAvailable() ? 1 : 0);
|
|
159
|
+
} else if(action.equals("isGpsLocationEnabled")) {
|
|
160
|
+
callbackContext.success(isGpsLocationEnabled() ? 1 : 0);
|
|
161
|
+
} else if(action.equals("isNetworkLocationEnabled")) {
|
|
162
|
+
callbackContext.success(isNetworkLocationEnabled() ? 1 : 0);
|
|
163
|
+
} else if(action.equals("getLocationMode")) {
|
|
164
|
+
callbackContext.success(getLocationModeName());
|
|
165
|
+
} else if(action.equals("requestLocationAuthorization")) {
|
|
166
|
+
requestLocationAuthorization(args, callbackContext);
|
|
167
|
+
}else {
|
|
168
|
+
diagnostic.handleError("Invalid action");
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
}catch(Exception e ) {
|
|
172
|
+
diagnostic.handleError("Exception occurred: ".concat(e.getMessage()));
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
public boolean isGpsLocationAvailable() throws Exception {
|
|
179
|
+
boolean result = isGpsLocationEnabled() && isLocationAuthorized();
|
|
180
|
+
diagnostic.logDebug("GPS location available: " + result);
|
|
181
|
+
return result;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
public boolean isGpsLocationEnabled() throws Exception {
|
|
185
|
+
int mode = getLocationMode();
|
|
186
|
+
boolean result = (mode == 3 || mode == 1);
|
|
187
|
+
diagnostic.logDebug("GPS location setting enabled: " + result);
|
|
188
|
+
return result;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
public boolean isNetworkLocationAvailable() throws Exception {
|
|
192
|
+
boolean result = isNetworkLocationEnabled() && isLocationAuthorized();
|
|
193
|
+
diagnostic.logDebug("Network location available: " + result);
|
|
194
|
+
return result;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
public boolean isNetworkLocationEnabled() throws Exception {
|
|
198
|
+
int mode = getLocationMode();
|
|
199
|
+
boolean result = (mode == 3 || mode == 2);
|
|
200
|
+
diagnostic.logDebug("Network location setting enabled: " + result);
|
|
201
|
+
return result;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
public String getLocationModeName() throws Exception {
|
|
205
|
+
String modeName;
|
|
206
|
+
int mode = getLocationMode();
|
|
207
|
+
switch(mode){
|
|
208
|
+
case Settings.Secure.LOCATION_MODE_HIGH_ACCURACY:
|
|
209
|
+
modeName = LOCATION_MODE_HIGH_ACCURACY;
|
|
210
|
+
break;
|
|
211
|
+
case Settings.Secure.LOCATION_MODE_SENSORS_ONLY:
|
|
212
|
+
modeName = LOCATION_MODE_DEVICE_ONLY;
|
|
213
|
+
break;
|
|
214
|
+
case Settings.Secure.LOCATION_MODE_BATTERY_SAVING:
|
|
215
|
+
modeName = LOCATION_MODE_BATTERY_SAVING;
|
|
216
|
+
break;
|
|
217
|
+
case Settings.Secure.LOCATION_MODE_OFF:
|
|
218
|
+
modeName = LOCATION_MODE_OFF;
|
|
219
|
+
break;
|
|
220
|
+
default:
|
|
221
|
+
modeName = LOCATION_MODE_UNKNOWN;
|
|
222
|
+
}
|
|
223
|
+
return modeName;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
public void notifyLocationStateChange(){
|
|
227
|
+
try {
|
|
228
|
+
String newMode = getLocationModeName();
|
|
229
|
+
if(!newMode.equals(currentLocationMode)){
|
|
230
|
+
diagnostic.logDebug("Location mode change to: " + newMode);
|
|
231
|
+
diagnostic.executePluginJavascript("location._onLocationStateChange(\"" + newMode +"\");");
|
|
232
|
+
currentLocationMode = newMode;
|
|
233
|
+
}
|
|
234
|
+
}catch(Exception e){
|
|
235
|
+
diagnostic.logError("Error retrieving current location mode on location state change: "+e.toString());
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
public void switchToLocationSettings() {
|
|
240
|
+
diagnostic.logDebug("Switch to Location Settings");
|
|
241
|
+
Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
|
|
242
|
+
cordova.getActivity().startActivity(settingsIntent);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
public void requestLocationAuthorization(JSONArray args, CallbackContext callbackContext) throws Exception{
|
|
246
|
+
JSONArray permissionsToRequest = new JSONArray();
|
|
247
|
+
boolean shouldRequestBackground = args.getBoolean(0);
|
|
248
|
+
|
|
249
|
+
permissionsToRequest.put(gpsLocationPermission);
|
|
250
|
+
permissionsToRequest.put(networkLocationPermission);
|
|
251
|
+
|
|
252
|
+
if(shouldRequestBackground && Build.VERSION.SDK_INT >= 29 ){
|
|
253
|
+
permissionsToRequest.put(backgroundLocationPermission);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
int requestId = Diagnostic.instance.storeContextByRequestId(callbackContext);
|
|
257
|
+
Diagnostic.instance._requestRuntimePermissions(permissionsToRequest, requestId);
|
|
258
|
+
|
|
259
|
+
PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
|
|
260
|
+
result.setKeepCallback(true);
|
|
261
|
+
callbackContext.sendPluginResult(result);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
/************
|
|
267
|
+
* Internals
|
|
268
|
+
***********/
|
|
269
|
+
/**
|
|
270
|
+
* Returns current location mode
|
|
271
|
+
*/
|
|
272
|
+
private int getLocationMode() throws Exception {
|
|
273
|
+
int mode;
|
|
274
|
+
if (Build.VERSION.SDK_INT >= 19){ // Kitkat and above
|
|
275
|
+
mode = Settings.Secure.getInt(this.cordova.getActivity().getContentResolver(), Settings.Secure.LOCATION_MODE);
|
|
276
|
+
}else{ // Pre-Kitkat
|
|
277
|
+
if(isLocationProviderEnabled(LocationManager.GPS_PROVIDER) && isLocationProviderEnabled(LocationManager.NETWORK_PROVIDER)){
|
|
278
|
+
mode = 3;
|
|
279
|
+
} else if(isLocationProviderEnabled(LocationManager.GPS_PROVIDER)){
|
|
280
|
+
mode = 1;
|
|
281
|
+
} else if(isLocationProviderEnabled(LocationManager.NETWORK_PROVIDER)){
|
|
282
|
+
mode = 2;
|
|
283
|
+
}else{
|
|
284
|
+
mode = 0;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
return mode;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
private boolean isLocationAuthorized() throws Exception {
|
|
291
|
+
boolean authorized = diagnostic.hasPermission(diagnostic.permissionsMap.get(gpsLocationPermission)) || diagnostic.hasPermission(diagnostic.permissionsMap.get(networkLocationPermission));
|
|
292
|
+
Log.v(TAG, "Location permission is "+(authorized ? "authorized" : "unauthorized"));
|
|
293
|
+
return authorized;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
private boolean isLocationProviderEnabled(String provider) {
|
|
297
|
+
return locationManager.isProviderEnabled(provider);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
/************
|
|
302
|
+
* Overrides
|
|
303
|
+
***********/
|
|
304
|
+
|
|
305
|
+
protected final BroadcastReceiver locationProviderChangedReceiver = new BroadcastReceiver() {
|
|
306
|
+
@Override
|
|
307
|
+
public void onReceive(Context context, Intent intent) {
|
|
308
|
+
try {
|
|
309
|
+
final String action = intent.getAction();
|
|
310
|
+
if(instance != null && action.equals(LocationManager.PROVIDERS_CHANGED_ACTION)){
|
|
311
|
+
Log.v(TAG, "onReceiveLocationProviderChange");
|
|
312
|
+
instance.notifyLocationStateChange();
|
|
313
|
+
}
|
|
314
|
+
} catch (Exception e) {
|
|
315
|
+
diagnostic.logError("Error receiving location provider state change: "+e.toString());
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
}
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Licensed to the Apache Software Foundation (ASF) under one
|
|
3
|
+
or more contributor license agreements. See the NOTICE file
|
|
4
|
+
distributed with this work for additional information
|
|
5
|
+
regarding copyright ownership. The ASF licenses this file
|
|
6
|
+
to you under the Apache License, Version 2.0 (the
|
|
7
|
+
"License"); you may not use this file except in compliance
|
|
8
|
+
with the License. You may obtain a copy of the License at
|
|
9
|
+
|
|
10
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
|
|
12
|
+
Unless required by applicable law or agreed to in writing,
|
|
13
|
+
software distributed under the License is distributed on an
|
|
14
|
+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
15
|
+
KIND, either express or implied. See the License for the
|
|
16
|
+
specific language governing permissions and limitations
|
|
17
|
+
under the License.
|
|
18
|
+
*/
|
|
19
|
+
package cordova.plugins;
|
|
20
|
+
|
|
21
|
+
/*
|
|
22
|
+
* Imports
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import android.content.BroadcastReceiver;
|
|
26
|
+
import android.content.Context;
|
|
27
|
+
import android.content.Intent;
|
|
28
|
+
import android.content.IntentFilter;
|
|
29
|
+
import android.provider.Settings;
|
|
30
|
+
import android.util.Log;
|
|
31
|
+
|
|
32
|
+
import org.apache.cordova.CallbackContext;
|
|
33
|
+
import org.apache.cordova.CordovaInterface;
|
|
34
|
+
import org.apache.cordova.CordovaPlugin;
|
|
35
|
+
import org.apache.cordova.CordovaWebView;
|
|
36
|
+
import org.json.JSONArray;
|
|
37
|
+
import org.json.JSONException;
|
|
38
|
+
|
|
39
|
+
import android.nfc.NfcAdapter;
|
|
40
|
+
import android.nfc.NfcManager;
|
|
41
|
+
import static android.nfc.NfcAdapter.EXTRA_ADAPTER_STATE;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Diagnostic plugin implementation for Android
|
|
45
|
+
*/
|
|
46
|
+
public class Diagnostic_NFC extends CordovaPlugin{
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
/*************
|
|
50
|
+
* Constants *
|
|
51
|
+
*************/
|
|
52
|
+
|
|
53
|
+
public static final int NFC_STATE_VALUE_UNKNOWN = 0;
|
|
54
|
+
public static final int NFC_STATE_VALUE_OFF = 1;
|
|
55
|
+
public static final int NFC_STATE_VALUE_TURNING_ON = 2;
|
|
56
|
+
public static final int NFC_STATE_VALUE_ON = 3;
|
|
57
|
+
public static final int NFC_STATE_VALUE_TURNING_OFF = 4;
|
|
58
|
+
|
|
59
|
+
public static final String NFC_STATE_UNKNOWN = "unknown";
|
|
60
|
+
public static final String NFC_STATE_OFF = "powered_off";
|
|
61
|
+
public static final String NFC_STATE_TURNING_ON = "powering_on";
|
|
62
|
+
public static final String NFC_STATE_ON = "powered_on";
|
|
63
|
+
public static final String NFC_STATE_TURNING_OFF = "powering_off";
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Tag for debug log messages
|
|
68
|
+
*/
|
|
69
|
+
public static final String TAG = "Diagnostic_NFC";
|
|
70
|
+
|
|
71
|
+
public static NfcManager nfcManager;
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
/*************
|
|
75
|
+
* Variables *
|
|
76
|
+
*************/
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Singleton class instance
|
|
80
|
+
*/
|
|
81
|
+
public static Diagnostic_NFC instance = null;
|
|
82
|
+
|
|
83
|
+
private Diagnostic diagnostic;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Current Cordova callback context (on this thread)
|
|
87
|
+
*/
|
|
88
|
+
protected CallbackContext currentContext;
|
|
89
|
+
|
|
90
|
+
protected String currentNFCState = NFC_STATE_UNKNOWN;
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
/*************
|
|
94
|
+
* Public API
|
|
95
|
+
************/
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Constructor.
|
|
99
|
+
*/
|
|
100
|
+
public Diagnostic_NFC() {}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Sets the context of the Command. This can then be used to do things like
|
|
104
|
+
* get file paths associated with the Activity.
|
|
105
|
+
*
|
|
106
|
+
* @param cordova The context of the main Activity.
|
|
107
|
+
* @param webView The CordovaWebView Cordova is running in.
|
|
108
|
+
*/
|
|
109
|
+
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
|
|
110
|
+
Log.d(TAG, "initialize()");
|
|
111
|
+
instance = this;
|
|
112
|
+
diagnostic = Diagnostic.getInstance();
|
|
113
|
+
|
|
114
|
+
try {
|
|
115
|
+
diagnostic.applicationContext.registerReceiver(NFCStateChangedReceiver, new IntentFilter(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED));
|
|
116
|
+
nfcManager = (NfcManager) diagnostic.applicationContext.getSystemService(Context.NFC_SERVICE);
|
|
117
|
+
}catch(Exception e){
|
|
118
|
+
diagnostic.logWarning("Unable to register NFC state change receiver: " + e.getMessage());
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
try {
|
|
122
|
+
currentNFCState = isNFCAvailable() ? NFC_STATE_ON : NFC_STATE_OFF;
|
|
123
|
+
}catch(Exception e){
|
|
124
|
+
diagnostic.logWarning("Unable to get initial NFC state: " + e.getMessage());
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
super.initialize(cordova, webView);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Called on destroying activity
|
|
132
|
+
*/
|
|
133
|
+
public void onDestroy() {
|
|
134
|
+
try {
|
|
135
|
+
diagnostic.applicationContext.unregisterReceiver(NFCStateChangedReceiver);
|
|
136
|
+
}catch(Exception e){
|
|
137
|
+
diagnostic.logWarning("Unable to unregister NFC state change receiver: " + e.getMessage());
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Executes the request and returns PluginResult.
|
|
144
|
+
*
|
|
145
|
+
* @param action The action to execute.
|
|
146
|
+
* @param args JSONArry of arguments for the plugin.
|
|
147
|
+
* @param callbackContext The callback id used when calling back into JavaScript.
|
|
148
|
+
* @return True if the action was valid, false if not.
|
|
149
|
+
*/
|
|
150
|
+
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
|
|
151
|
+
currentContext = callbackContext;
|
|
152
|
+
|
|
153
|
+
try {
|
|
154
|
+
if (action.equals("switchToNFCSettings")){
|
|
155
|
+
switchToNFCSettings();
|
|
156
|
+
callbackContext.success();
|
|
157
|
+
} else if(action.equals("isNFCPresent")) {
|
|
158
|
+
callbackContext.success(isNFCPresent() ? 1 : 0);
|
|
159
|
+
} else if(action.equals("isNFCEnabled")) {
|
|
160
|
+
callbackContext.success(isNFCEnabled() ? 1 : 0);
|
|
161
|
+
} else if(action.equals("isNFCAvailable")) {
|
|
162
|
+
callbackContext.success(isNFCAvailable() ? 1 : 0);
|
|
163
|
+
} else {
|
|
164
|
+
diagnostic.handleError("Invalid action");
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
}catch(Exception e ) {
|
|
168
|
+
diagnostic.handleError("Exception occurred: ".concat(e.getMessage()));
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
/************
|
|
176
|
+
* Internals
|
|
177
|
+
***********/
|
|
178
|
+
|
|
179
|
+
public void switchToNFCSettings() {
|
|
180
|
+
diagnostic.logDebug("Switch to NFC Settings");
|
|
181
|
+
Intent settingsIntent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
|
|
182
|
+
if (android.os.Build.VERSION.SDK_INT >= 16) {
|
|
183
|
+
settingsIntent = new Intent(android.provider.Settings.ACTION_NFC_SETTINGS);
|
|
184
|
+
}
|
|
185
|
+
cordova.getActivity().startActivity(settingsIntent);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
public boolean isNFCPresent() {
|
|
189
|
+
boolean result = false;
|
|
190
|
+
try {
|
|
191
|
+
NfcAdapter adapter = nfcManager.getDefaultAdapter();
|
|
192
|
+
result = adapter != null;
|
|
193
|
+
}catch(Exception e){
|
|
194
|
+
diagnostic.logError(e.getMessage());
|
|
195
|
+
}
|
|
196
|
+
return result;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
public boolean isNFCEnabled() {
|
|
200
|
+
boolean result = false;
|
|
201
|
+
try {
|
|
202
|
+
NfcAdapter adapter = nfcManager.getDefaultAdapter();
|
|
203
|
+
result = adapter != null && adapter.isEnabled();
|
|
204
|
+
}catch(Exception e){
|
|
205
|
+
diagnostic.logError(e.getMessage());
|
|
206
|
+
}
|
|
207
|
+
return result;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
public boolean isNFCAvailable() {
|
|
211
|
+
boolean result = isNFCPresent() && isNFCEnabled();
|
|
212
|
+
return result;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
public void notifyNFCStateChange(int stateValue){
|
|
216
|
+
String newState = getNFCState(stateValue);
|
|
217
|
+
try {
|
|
218
|
+
if(newState != currentNFCState){
|
|
219
|
+
diagnostic.logDebug("NFC state changed to: " + newState);
|
|
220
|
+
diagnostic.executePluginJavascript("nfc._onNFCStateChange(\"" + newState +"\");");
|
|
221
|
+
currentNFCState = newState;
|
|
222
|
+
}
|
|
223
|
+
}catch(Exception e){
|
|
224
|
+
diagnostic.logError("Error retrieving current NFC state on state change: "+e.toString());
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
public String getNFCState(int stateValue){
|
|
229
|
+
|
|
230
|
+
String state;
|
|
231
|
+
switch(stateValue){
|
|
232
|
+
case NFC_STATE_VALUE_OFF:
|
|
233
|
+
state = NFC_STATE_OFF;
|
|
234
|
+
break;
|
|
235
|
+
case NFC_STATE_VALUE_TURNING_ON:
|
|
236
|
+
state = NFC_STATE_TURNING_ON;
|
|
237
|
+
break;
|
|
238
|
+
case NFC_STATE_VALUE_ON:
|
|
239
|
+
state = NFC_STATE_ON;
|
|
240
|
+
break;
|
|
241
|
+
case NFC_STATE_VALUE_TURNING_OFF:
|
|
242
|
+
state = NFC_STATE_TURNING_OFF;
|
|
243
|
+
break;
|
|
244
|
+
default:
|
|
245
|
+
state = NFC_STATE_UNKNOWN;
|
|
246
|
+
}
|
|
247
|
+
return state;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/************
|
|
251
|
+
* Overrides
|
|
252
|
+
***********/
|
|
253
|
+
|
|
254
|
+
protected final BroadcastReceiver NFCStateChangedReceiver = new BroadcastReceiver() {
|
|
255
|
+
@Override
|
|
256
|
+
public void onReceive(Context context, Intent intent) {
|
|
257
|
+
try {
|
|
258
|
+
final String action = intent.getAction();
|
|
259
|
+
if(instance != null && action.equals(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED)){
|
|
260
|
+
|
|
261
|
+
Log.v(TAG, "onReceiveNFCStateChange");
|
|
262
|
+
final int stateValue = intent.getIntExtra(EXTRA_ADAPTER_STATE, -1);
|
|
263
|
+
instance.notifyNFCStateChange(stateValue);
|
|
264
|
+
}
|
|
265
|
+
} catch (Exception e) {
|
|
266
|
+
diagnostic.logError("Error receiving NFC state change: "+e.toString());
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
}
|