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.
Files changed (75) hide show
  1. package/.github/FUNDING.yml +6 -0
  2. package/.github/ISSUE_TEMPLATE/bug_report.md +123 -0
  3. package/.github/ISSUE_TEMPLATE/documentation-issue.md +36 -0
  4. package/.github/ISSUE_TEMPLATE/feature_request.md +41 -0
  5. package/.github/PULL_REQUEST_TEMPLATE.md +37 -0
  6. package/.github/stale.yml +17 -0
  7. package/CHANGELOG.md +390 -0
  8. package/README.md +3709 -0
  9. package/cordova.plugins.diagnostic.d.ts +1113 -0
  10. package/package.json +54 -0
  11. package/plugin.xml +496 -0
  12. package/scripts/apply-modules.js +165 -0
  13. package/scripts/logger.js +94 -0
  14. package/src/android/Diagnostic.java +856 -0
  15. package/src/android/Diagnostic_Bluetooth.java +297 -0
  16. package/src/android/Diagnostic_Camera.java +134 -0
  17. package/src/android/Diagnostic_External_Storage.java +273 -0
  18. package/src/android/Diagnostic_Location.java +319 -0
  19. package/src/android/Diagnostic_NFC.java +270 -0
  20. package/src/android/Diagnostic_Notifications.java +157 -0
  21. package/src/android/Diagnostic_Wifi.java +155 -0
  22. package/src/ios/Diagnostic.h +56 -0
  23. package/src/ios/Diagnostic.m +282 -0
  24. package/src/ios/Diagnostic_Bluetooth.h +24 -0
  25. package/src/ios/Diagnostic_Bluetooth.m +170 -0
  26. package/src/ios/Diagnostic_Calendar.h +24 -0
  27. package/src/ios/Diagnostic_Calendar.m +94 -0
  28. package/src/ios/Diagnostic_Camera.h +27 -0
  29. package/src/ios/Diagnostic_Camera.m +194 -0
  30. package/src/ios/Diagnostic_Contacts.h +24 -0
  31. package/src/ios/Diagnostic_Contacts.m +93 -0
  32. package/src/ios/Diagnostic_Location.h +31 -0
  33. package/src/ios/Diagnostic_Location.m +284 -0
  34. package/src/ios/Diagnostic_Microphone.h +21 -0
  35. package/src/ios/Diagnostic_Microphone.m +97 -0
  36. package/src/ios/Diagnostic_Motion.h +27 -0
  37. package/src/ios/Diagnostic_Motion.m +143 -0
  38. package/src/ios/Diagnostic_Notifications.h +22 -0
  39. package/src/ios/Diagnostic_Notifications.m +235 -0
  40. package/src/ios/Diagnostic_Reminders.h +24 -0
  41. package/src/ios/Diagnostic_Reminders.m +93 -0
  42. package/src/ios/Diagnostic_Wifi.h +19 -0
  43. package/src/ios/Diagnostic_Wifi.m +108 -0
  44. package/src/windows/diagnosticProxy.bluetooth.js +23 -0
  45. package/src/windows/diagnosticProxy.camera.js +35 -0
  46. package/src/windows/diagnosticProxy.js +137 -0
  47. package/src/windows/diagnosticProxy.location.js +54 -0
  48. package/src/windows/diagnosticProxy.wifi.js +18 -0
  49. package/www/android/diagnostic.bluetooth.js +211 -0
  50. package/www/android/diagnostic.calendar.js +90 -0
  51. package/www/android/diagnostic.camera.js +203 -0
  52. package/www/android/diagnostic.contacts.js +91 -0
  53. package/www/android/diagnostic.external_storage.js +102 -0
  54. package/www/android/diagnostic.js +1309 -0
  55. package/www/android/diagnostic.location.js +282 -0
  56. package/www/android/diagnostic.microphone.js +89 -0
  57. package/www/android/diagnostic.nfc.js +127 -0
  58. package/www/android/diagnostic.notifications.js +74 -0
  59. package/www/android/diagnostic.wifi.js +90 -0
  60. package/www/ios/diagnostic.bluetooth.js +127 -0
  61. package/www/ios/diagnostic.calendar.js +97 -0
  62. package/www/ios/diagnostic.camera.js +212 -0
  63. package/www/ios/diagnostic.contacts.js +98 -0
  64. package/www/ios/diagnostic.js +990 -0
  65. package/www/ios/diagnostic.location.js +236 -0
  66. package/www/ios/diagnostic.microphone.js +99 -0
  67. package/www/ios/diagnostic.motion.js +160 -0
  68. package/www/ios/diagnostic.notifications.js +189 -0
  69. package/www/ios/diagnostic.reminders.js +97 -0
  70. package/www/ios/diagnostic.wifi.js +80 -0
  71. package/www/windows/diagnostic.bluetooth.js +51 -0
  72. package/www/windows/diagnostic.camera.js +41 -0
  73. package/www/windows/diagnostic.js +169 -0
  74. package/www/windows/diagnostic.location.js +35 -0
  75. package/www/windows/diagnostic.wifi.js +51 -0
@@ -0,0 +1,297 @@
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.bluetooth.BluetoothAdapter;
26
+ import android.content.BroadcastReceiver;
27
+ import android.content.Context;
28
+ import android.content.Intent;
29
+ import android.content.IntentFilter;
30
+ import android.content.pm.PackageManager;
31
+ import android.os.Build;
32
+ import android.provider.Settings;
33
+ import android.util.Log;
34
+
35
+ import org.apache.cordova.CallbackContext;
36
+ import org.apache.cordova.CordovaInterface;
37
+ import org.apache.cordova.CordovaPlugin;
38
+ import org.apache.cordova.CordovaWebView;
39
+ import org.json.JSONArray;
40
+ import org.json.JSONException;
41
+
42
+ /**
43
+ * Diagnostic plugin implementation for Android
44
+ */
45
+ public class Diagnostic_Bluetooth extends CordovaPlugin{
46
+
47
+
48
+ /*************
49
+ * Constants *
50
+ *************/
51
+
52
+ /**
53
+ * Current state of Bluetooth hardware is unknown
54
+ */
55
+ protected static final String BLUETOOTH_STATE_UNKNOWN = "unknown";
56
+
57
+ /**
58
+ * Current state of Bluetooth hardware is ON
59
+ */
60
+ protected static final String BLUETOOTH_STATE_POWERED_ON = "powered_on";
61
+
62
+ /**
63
+ * Current state of Bluetooth hardware is OFF
64
+ */
65
+ protected static final String BLUETOOTH_STATE_POWERED_OFF = "powered_off";
66
+
67
+ /**
68
+ * Current state of Bluetooth hardware is transitioning to ON
69
+ */
70
+ protected static final String BLUETOOTH_STATE_POWERING_ON = "powering_on";
71
+
72
+ /**
73
+ * Current state of Bluetooth hardware is transitioning to OFF
74
+ */
75
+ protected static final String BLUETOOTH_STATE_POWERING_OFF = "powering_off";
76
+
77
+ /**
78
+ * Tag for debug log messages
79
+ */
80
+ public static final String TAG = "Diagnostic_Bluetooth";
81
+
82
+ private String currentBluetoothState = null;
83
+
84
+
85
+ /*************
86
+ * Variables *
87
+ *************/
88
+
89
+ /**
90
+ * Singleton class instance
91
+ */
92
+ public static Diagnostic_Bluetooth instance = null;
93
+
94
+ private Diagnostic diagnostic;
95
+
96
+ /**
97
+ * Current Cordova callback context (on this thread)
98
+ */
99
+ protected CallbackContext currentContext;
100
+
101
+
102
+ /*************
103
+ * Public API
104
+ ************/
105
+
106
+ /**
107
+ * Constructor.
108
+ */
109
+ public Diagnostic_Bluetooth() {}
110
+
111
+ /**
112
+ * Sets the context of the Command. This can then be used to do things like
113
+ * get file paths associated with the Activity.
114
+ *
115
+ * @param cordova The context of the main Activity.
116
+ * @param webView The CordovaWebView Cordova is running in.
117
+ */
118
+ public void initialize(CordovaInterface cordova, CordovaWebView webView) {
119
+ Log.d(TAG, "initialize()");
120
+ instance = this;
121
+ diagnostic = Diagnostic.getInstance();
122
+
123
+ try {
124
+ diagnostic.applicationContext.registerReceiver(bluetoothStateChangeReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
125
+ currentBluetoothState = getBluetoothState();
126
+ }catch(Exception e){
127
+ diagnostic.logWarning("Unable to register Bluetooth state change receiver: " + e.getMessage());
128
+ }
129
+
130
+ super.initialize(cordova, webView);
131
+ }
132
+
133
+ /**
134
+ * Called on destroying activity
135
+ */
136
+ public void onDestroy() {
137
+ try {
138
+ diagnostic.applicationContext.unregisterReceiver(bluetoothStateChangeReceiver);
139
+ }catch(Exception e){
140
+ diagnostic.logWarning("Unable to unregister Bluetooth state change receiver: " + e.getMessage());
141
+ }
142
+ }
143
+
144
+ /**
145
+ * Executes the request and returns PluginResult.
146
+ *
147
+ * @param action The action to execute.
148
+ * @param args JSONArry of arguments for the plugin.
149
+ * @param callbackContext The callback id used when calling back into JavaScript.
150
+ * @return True if the action was valid, false if not.
151
+ */
152
+ public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
153
+ currentContext = callbackContext;
154
+
155
+ try {
156
+ if (action.equals("switchToBluetoothSettings")){
157
+ switchToBluetoothSettings();
158
+ callbackContext.success();
159
+ } else if(action.equals("isBluetoothAvailable")) {
160
+ callbackContext.success(isBluetoothAvailable() ? 1 : 0);
161
+ } else if(action.equals("isBluetoothEnabled")) {
162
+ callbackContext.success(isBluetoothEnabled() ? 1 : 0);
163
+ } else if(action.equals("hasBluetoothSupport")) {
164
+ callbackContext.success(hasBluetoothSupport() ? 1 : 0);
165
+ } else if(action.equals("hasBluetoothLESupport")) {
166
+ callbackContext.success(hasBluetoothLESupport() ? 1 : 0);
167
+ } else if(action.equals("hasBluetoothLEPeripheralSupport")) {
168
+ callbackContext.success(hasBluetoothLEPeripheralSupport() ? 1 : 0);
169
+ } else if(action.equals("setBluetoothState")) {
170
+ setBluetoothState(args.getBoolean(0));
171
+ callbackContext.success();
172
+ } else if(action.equals("getBluetoothState")) {
173
+ callbackContext.success(getBluetoothState());
174
+ } else {
175
+ diagnostic.handleError("Invalid action");
176
+ return false;
177
+ }
178
+ }catch(Exception e ) {
179
+ diagnostic.handleError("Exception occurred: ".concat(e.getMessage()));
180
+ return false;
181
+ }
182
+ return true;
183
+ }
184
+
185
+
186
+
187
+
188
+ /************
189
+ * Internals
190
+ ***********/
191
+
192
+ public void switchToBluetoothSettings() {
193
+ diagnostic.logDebug("Switch to Bluetooth Settings");
194
+ Intent settingsIntent = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS);
195
+ cordova.getActivity().startActivity(settingsIntent);
196
+ }
197
+
198
+ public boolean isBluetoothAvailable() {
199
+ boolean result = hasBluetoothSupport() && isBluetoothEnabled();
200
+ return result;
201
+ }
202
+
203
+ public boolean isBluetoothEnabled() {
204
+ BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
205
+ boolean result = mBluetoothAdapter != null && mBluetoothAdapter.isEnabled();
206
+ return result;
207
+ }
208
+
209
+ public boolean hasBluetoothSupport() {
210
+ PackageManager pm = this.cordova.getActivity().getPackageManager();
211
+ boolean result = pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);
212
+ return result;
213
+ }
214
+
215
+ public boolean hasBluetoothLESupport() {
216
+ PackageManager pm = this.cordova.getActivity().getPackageManager();
217
+ boolean result = pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE);
218
+ return result;
219
+ }
220
+
221
+ public boolean hasBluetoothLEPeripheralSupport() {
222
+ if (Build.VERSION.SDK_INT >=21){
223
+ BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
224
+ boolean result = mBluetoothAdapter != null && mBluetoothAdapter.isMultipleAdvertisementSupported();
225
+ return result;
226
+ }
227
+ return false;
228
+ }
229
+
230
+ public static boolean setBluetoothState(boolean enable) {
231
+ BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
232
+ boolean isEnabled = bluetoothAdapter.isEnabled();
233
+ if (enable && !isEnabled) {
234
+ return bluetoothAdapter.enable();
235
+ }
236
+ else if(!enable && isEnabled) {
237
+ return bluetoothAdapter.disable();
238
+ }
239
+ return true;
240
+ }
241
+
242
+ public String getBluetoothState(){
243
+
244
+ String bluetoothState = BLUETOOTH_STATE_UNKNOWN;
245
+ if(hasBluetoothSupport()){
246
+ BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
247
+ if(mBluetoothAdapter == null){
248
+ diagnostic.logWarning("Bluetooth adapter unavailable or not found");
249
+ return BLUETOOTH_STATE_UNKNOWN;
250
+ }
251
+ int state = mBluetoothAdapter.getState();
252
+ switch(state){
253
+ case BluetoothAdapter.STATE_OFF:
254
+ bluetoothState = BLUETOOTH_STATE_POWERED_OFF;
255
+ break;
256
+ case BluetoothAdapter.STATE_ON:
257
+ bluetoothState = BLUETOOTH_STATE_POWERED_ON;
258
+ break;
259
+ case BluetoothAdapter.STATE_TURNING_OFF:
260
+ bluetoothState = BLUETOOTH_STATE_POWERING_OFF;
261
+ break;
262
+ case BluetoothAdapter.STATE_TURNING_ON:
263
+ bluetoothState = BLUETOOTH_STATE_POWERING_ON;
264
+ break;
265
+ }
266
+ }
267
+ return bluetoothState;
268
+ }
269
+
270
+ public void notifyBluetoothStateChange(){
271
+ try {
272
+ String newState = getBluetoothState();
273
+ if(!newState.equals(currentBluetoothState)){
274
+ diagnostic.logDebug("Bluetooth state changed to: " + newState);
275
+ diagnostic.executePluginJavascript("bluetooth._onBluetoothStateChange(\""+newState+"\");");
276
+ currentBluetoothState = newState;
277
+ }
278
+ }catch(Exception e){
279
+ diagnostic.logError("Error retrieving current Bluetooth state on Bluetooth state change: "+e.toString());
280
+ }
281
+ }
282
+
283
+ /************
284
+ * Overrides
285
+ ***********/
286
+
287
+ protected final BroadcastReceiver bluetoothStateChangeReceiver = new BroadcastReceiver() {
288
+ @Override
289
+ public void onReceive(Context context, Intent intent) {
290
+ final String action = intent.getAction();
291
+ if(instance != null && action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)){
292
+ Log.v(TAG, "bluetoothStateChangeReceiver");
293
+ instance.notifyBluetoothStateChange();
294
+ }
295
+ }
296
+ };
297
+ }
@@ -0,0 +1,134 @@
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.pm.PackageManager;
26
+ import android.hardware.Camera;
27
+ import android.util.Log;
28
+
29
+ import org.apache.cordova.CallbackContext;
30
+ import org.apache.cordova.CordovaInterface;
31
+ import org.apache.cordova.CordovaPlugin;
32
+ import org.apache.cordova.CordovaWebView;
33
+ import org.json.JSONArray;
34
+ import org.json.JSONException;
35
+
36
+ /**
37
+ * Diagnostic plugin implementation for Android
38
+ */
39
+ public class Diagnostic_Camera extends CordovaPlugin{
40
+
41
+
42
+ /*************
43
+ * Constants *
44
+ *************/
45
+
46
+
47
+ /**
48
+ * Tag for debug log messages
49
+ */
50
+ public static final String TAG = "Diagnostic_Camera";
51
+
52
+
53
+ /*************
54
+ * Variables *
55
+ *************/
56
+
57
+ /**
58
+ * Singleton class instance
59
+ */
60
+ public static Diagnostic_Camera instance = null;
61
+
62
+ private Diagnostic diagnostic;
63
+
64
+ /**
65
+ * Current Cordova callback context (on this thread)
66
+ */
67
+ protected CallbackContext currentContext;
68
+
69
+
70
+ /*************
71
+ * Public API
72
+ ************/
73
+
74
+ /**
75
+ * Constructor.
76
+ */
77
+ public Diagnostic_Camera() {}
78
+
79
+ /**
80
+ * Sets the context of the Command. This can then be used to do things like
81
+ * get file paths associated with the Activity.
82
+ *
83
+ * @param cordova The context of the main Activity.
84
+ * @param webView The CordovaWebView Cordova is running in.
85
+ */
86
+ public void initialize(CordovaInterface cordova, CordovaWebView webView) {
87
+ Log.d(TAG, "initialize()");
88
+ instance = this;
89
+ diagnostic = Diagnostic.getInstance();
90
+
91
+ super.initialize(cordova, webView);
92
+ }
93
+
94
+
95
+ /**
96
+ * Executes the request and returns PluginResult.
97
+ *
98
+ * @param action The action to execute.
99
+ * @param args JSONArry of arguments for the plugin.
100
+ * @param callbackContext The callback id used when calling back into JavaScript.
101
+ * @return True if the action was valid, false if not.
102
+ */
103
+ public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
104
+ currentContext = callbackContext;
105
+
106
+ try {
107
+ if(action.equals("isCameraPresent")) {
108
+ callbackContext.success(isCameraPresent() ? 1 : 0);
109
+ } else {
110
+ diagnostic.handleError("Invalid action");
111
+ return false;
112
+ }
113
+ }catch(Exception e ) {
114
+ diagnostic.handleError("Exception occurred: ".concat(e.getMessage()));
115
+ return false;
116
+ }
117
+ return true;
118
+ }
119
+
120
+ public boolean isCameraPresent() {
121
+ int numberOfCameras = Camera.getNumberOfCameras();
122
+ PackageManager pm = this.cordova.getActivity().getPackageManager();
123
+ final boolean deviceHasCameraFlag = pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);
124
+ boolean result = (deviceHasCameraFlag && numberOfCameras>0 );
125
+ return result;
126
+ }
127
+
128
+
129
+ /************
130
+ * Internals
131
+ ***********/
132
+
133
+
134
+ }
@@ -0,0 +1,273 @@
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.os.Build;
26
+ import android.os.Environment;
27
+ import android.os.StatFs;
28
+ import androidx.core.os.EnvironmentCompat;
29
+ import android.util.Log;
30
+
31
+ import org.apache.cordova.CallbackContext;
32
+ import org.apache.cordova.CordovaInterface;
33
+ import org.apache.cordova.CordovaPlugin;
34
+ import org.apache.cordova.CordovaWebView;
35
+ import org.json.JSONArray;
36
+ import org.json.JSONException;
37
+ import org.json.JSONObject;
38
+
39
+ import java.io.File;
40
+ import java.io.InputStream;
41
+ import java.util.ArrayList;
42
+ import java.util.List;
43
+
44
+ /**
45
+ * Diagnostic plugin implementation for Android
46
+ */
47
+ public class Diagnostic_External_Storage extends CordovaPlugin{
48
+
49
+
50
+ /*************
51
+ * Constants *
52
+ *************/
53
+
54
+
55
+ /**
56
+ * Tag for debug log messages
57
+ */
58
+ public static final String TAG = "Diagnostic_External_Storage";
59
+
60
+
61
+ /*************
62
+ * Variables *
63
+ *************/
64
+
65
+ /**
66
+ * Singleton class instance
67
+ */
68
+ public static Diagnostic_External_Storage instance = null;
69
+
70
+ private Diagnostic diagnostic;
71
+
72
+ /**
73
+ * Current Cordova callback context (on this thread)
74
+ */
75
+ protected CallbackContext currentContext;
76
+
77
+ protected static String externalStoragePermission = "READ_EXTERNAL_STORAGE";
78
+
79
+
80
+ /*************
81
+ * Public API
82
+ ************/
83
+
84
+ /**
85
+ * Constructor.
86
+ */
87
+ public Diagnostic_External_Storage() {}
88
+
89
+ /**
90
+ * Sets the context of the Command. This can then be used to do things like
91
+ * get file paths associated with the Activity.
92
+ *
93
+ * @param cordova The context of the main Activity.
94
+ * @param webView The CordovaWebView Cordova is running in.
95
+ */
96
+ public void initialize(CordovaInterface cordova, CordovaWebView webView) {
97
+ Log.d(TAG, "initialize()");
98
+ instance = this;
99
+ diagnostic = Diagnostic.getInstance();
100
+
101
+ super.initialize(cordova, webView);
102
+ }
103
+
104
+
105
+ /**
106
+ * Executes the request and returns PluginResult.
107
+ *
108
+ * @param action The action to execute.
109
+ * @param args JSONArry of arguments for the plugin.
110
+ * @param callbackContext The callback id used when calling back into JavaScript.
111
+ * @return True if the action was valid, false if not.
112
+ */
113
+ public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
114
+ currentContext = callbackContext;
115
+
116
+ try {
117
+ if(action.equals("getExternalSdCardDetails")) {
118
+ this.getExternalSdCardDetails();
119
+ } else {
120
+ diagnostic.handleError("Invalid action");
121
+ return false;
122
+ }
123
+ }catch(Exception e ) {
124
+ diagnostic.handleError("Exception occurred: ".concat(e.getMessage()));
125
+ return false;
126
+ }
127
+ return true;
128
+ }
129
+
130
+ public static void onReceivePermissionResult() throws JSONException{
131
+ instance._getExternalSdCardDetails();
132
+ }
133
+
134
+ /************
135
+ * Internals
136
+ ***********/
137
+
138
+ protected void getExternalSdCardDetails() throws Exception{
139
+ String permission = diagnostic.permissionsMap.get(externalStoragePermission);
140
+ if (diagnostic.hasPermission(permission)) {
141
+ _getExternalSdCardDetails();
142
+ } else {
143
+ diagnostic.requestRuntimePermission(permission, Diagnostic.GET_EXTERNAL_SD_CARD_DETAILS_PERMISSION_REQUEST);
144
+ }
145
+ }
146
+
147
+
148
+ protected void _getExternalSdCardDetails() throws JSONException {
149
+ String[] storageDirectories = getStorageDirectories();
150
+
151
+ JSONArray details = new JSONArray();
152
+ for(int i=0; i<storageDirectories.length; i++) {
153
+ String directory = storageDirectories[i];
154
+ File f = new File(directory);
155
+ JSONObject detail = new JSONObject();
156
+ if(f.canRead()){
157
+ detail.put("path", directory);
158
+ detail.put("filePath", "file://"+directory);
159
+ detail.put("canWrite", f.canWrite());
160
+ detail.put("freeSpace", getFreeSpaceInBytes(directory));
161
+ if(directory.contains("Android")){
162
+ detail.put("type", "application");
163
+ }else{
164
+ detail.put("type", "root");
165
+ }
166
+ details.put(detail);
167
+ }
168
+ }
169
+ currentContext.success(details);
170
+ }
171
+
172
+ /**
173
+ * Given a path return the number of free bytes in the filesystem containing the path.
174
+ *
175
+ * @param path to the file system
176
+ * @return free space in bytes
177
+ */
178
+ protected long getFreeSpaceInBytes(String path) {
179
+ try {
180
+ StatFs stat = new StatFs(path);
181
+ long blockSize = stat.getBlockSize();
182
+ long availableBlocks = stat.getAvailableBlocks();
183
+ return availableBlocks * blockSize;
184
+ } catch (IllegalArgumentException e) {
185
+ // The path was invalid. Just return 0 free bytes.
186
+ return 0;
187
+ }
188
+ }
189
+
190
+
191
+ /**
192
+ * Returns all available external SD-Cards in the system.
193
+ *
194
+ * @return paths to all available external SD-Cards in the system.
195
+ */
196
+ protected String[] getStorageDirectories() {
197
+
198
+ List<String> results = new ArrayList<String>();
199
+
200
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //Method 1 for KitKat & above
201
+ File[] externalDirs = this.cordova.getActivity().getApplicationContext().getExternalFilesDirs(null);
202
+
203
+ for (File file : externalDirs) {
204
+ if(file == null){
205
+ continue;
206
+ }
207
+ String applicationPath = file.getPath();
208
+ String rootPath = applicationPath.split("/Android")[0];
209
+
210
+ boolean addPath = false;
211
+
212
+ if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
213
+ addPath = Environment.isExternalStorageRemovable(file);
214
+ }
215
+ else{
216
+ addPath = Environment.MEDIA_MOUNTED.equals(EnvironmentCompat.getStorageState(file));
217
+ }
218
+
219
+ if(addPath){
220
+ results.add(rootPath);
221
+ results.add(applicationPath);
222
+ }
223
+ }
224
+ }
225
+
226
+ if(results.isEmpty()) { //Method 2 for all versions
227
+ // better variation of: http://stackoverflow.com/a/40123073/5002496
228
+ String output = "";
229
+ try {
230
+ final Process process = new ProcessBuilder().command("mount | grep /dev/block/vold")
231
+ .redirectErrorStream(true).start();
232
+ process.waitFor();
233
+ final InputStream is = process.getInputStream();
234
+ final byte[] buffer = new byte[1024];
235
+ while (is.read(buffer) != -1) {
236
+ output = output + new String(buffer);
237
+ }
238
+ is.close();
239
+ } catch (final Exception e) {
240
+ e.printStackTrace();
241
+ }
242
+ if(!output.trim().isEmpty()) {
243
+ String devicePoints[] = output.split("\n");
244
+ for(String voldPoint: devicePoints) {
245
+ results.add(voldPoint.split(" ")[2]);
246
+ }
247
+ }
248
+ }
249
+
250
+ //Below few lines is to remove paths which may not be external memory card, like OTG (feel free to comment them out)
251
+ if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
252
+ for (int i = 0; i < results.size(); i++) {
253
+ if (!results.get(i).toLowerCase().matches(".*[0-9a-f]{4}[-][0-9a-f]{4}.*")) {
254
+ diagnostic.logDebug(results.get(i) + " might not be extSDcard");
255
+ results.remove(i--);
256
+ }
257
+ }
258
+ } else {
259
+ for (int i = 0; i < results.size(); i++) {
260
+ if (!results.get(i).toLowerCase().contains("ext") && !results.get(i).toLowerCase().contains("sdcard")) {
261
+ diagnostic.logDebug(results.get(i)+" might not be extSDcard");
262
+ results.remove(i--);
263
+ }
264
+ }
265
+ }
266
+
267
+ String[] storageDirectories = new String[results.size()];
268
+ for(int i=0; i<results.size(); ++i) storageDirectories[i] = results.get(i);
269
+
270
+ return storageDirectories;
271
+ }
272
+
273
+ }