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 com .collectme .cordova .diagnostic ;
20
+
21
+ import java .util .TimeZone ;
22
+
23
+ import org .apache .cordova .CordovaWebView ;
24
+ import org .apache .cordova .CallbackContext ;
25
+ import org .apache .cordova .CordovaPlugin ;
26
+ import org .apache .cordova .LOG ;
27
+ import org .apache .cordova .CordovaInterface ;
28
+ import org .json .JSONArray ;
29
+ import org .json .JSONException ;
30
+ import org .json .JSONObject ;
31
+ import android .util .Log ;
32
+
33
+ import android .content .BroadcastReceiver ;
34
+ import android .content .Context ;
35
+ import android .content .Intent ;
36
+ import android .content .IntentFilter ;
37
+ import android .content .pm .PackageManager ;
38
+ import android .provider .Settings ;
39
+ import android .location .LocationManager ;
40
+ import android .location .LocationListener ;
41
+ import android .net .wifi .WifiManager ;
42
+
43
+ public class Diagnostic extends CordovaPlugin {
44
+ public static final String TAG = "Diagnostic" ;
45
+
46
+ /**
47
+ * Constructor.
48
+ */
49
+ public Diagnostic () {
50
+ }
51
+
52
+ /**
53
+ * Sets the context of the Command. This can then be used to do things like
54
+ * get file paths associated with the Activity.
55
+ *
56
+ * @param cordova The context of the main Activity.
57
+ * @param webView The CordovaWebView Cordova is running in.
58
+ */
59
+ public void initialize (CordovaInterface cordova , CordovaWebView webView ) {
60
+ super .initialize (cordova , webView );
61
+ }
62
+
63
+ /**
64
+ * Executes the request and returns PluginResult.
65
+ *
66
+ * @param action The action to execute.
67
+ * @param args JSONArry of arguments for the plugin.
68
+ * @param callbackContext The callback id used when calling back into JavaScript.
69
+ * @return True if the action was valid, false if not.
70
+ */
71
+ public boolean execute (String action , JSONArray args , CallbackContext callbackContext ) throws JSONException {
72
+ JSONObject r = new JSONObject ();
73
+
74
+ if (action .equals ("switchToLocationSettings" )){
75
+ switchToLocationSettings ();
76
+ callbackContext .success ();
77
+ } else if (action .equals ("isLocationEnabled" )) {
78
+ r .put ("success" , isGpsEnabled () || isNetworkEnabled ());
79
+ callbackContext .success (r );
80
+ } else if (action .equals ("isLocationAuthorized" ) || action .equals ("isLocationEnabledSetting" )) {
81
+ r .put ("success" , true );
82
+ callbackContext .success (r );
83
+ } else if (action .equals ("isWifiEnabled" )) {
84
+ r .put ("success" , isWifiEnabled ());
85
+ callbackContext .success (r );
86
+ } else if (action .equals ("isCameraEnabled" )) {
87
+ r .put ("success" , isCameraEnabled ());
88
+ callbackContext .success (r );
89
+ }
90
+ else {
91
+ return false ;
92
+ }
93
+ return true ;
94
+ }
95
+
96
+ /**
97
+ * Check device settings for GPS.
98
+ *
99
+ * @returns {boolean} The status of GPS in device settings.
100
+ */
101
+ public boolean isGpsEnabled () {
102
+ boolean result = isLocationProviderEnabled (LocationManager .GPS_PROVIDER );
103
+ Log .d (TAG , "GPS enabled: " + result );
104
+ return result ;
105
+ }
106
+
107
+ public boolean isNetworkEnabled () {
108
+ boolean result = isLocationProviderEnabled (LocationManager .NETWORK_PROVIDER );
109
+ Log .d (TAG , "Network enabled: " + result );
110
+ return result ;
111
+ }
112
+
113
+ public boolean isWifiEnabled () {
114
+ WifiManager wifiManager = (WifiManager ) this .cordova .getActivity ().getSystemService (Context .WIFI_SERVICE );
115
+ boolean result = wifiManager .isWifiEnabled ();
116
+ return result ;
117
+ }
118
+
119
+ public boolean isCameraEnabled () {
120
+ PackageManager pm = this .cordova .getActivity ().getPackageManager ();
121
+ boolean result = pm .hasSystemFeature (PackageManager .FEATURE_CAMERA );
122
+ return result ;
123
+ }
124
+
125
+ /**
126
+ * Requests that the user enable the location in device settings.
127
+ */
128
+ public void switchToLocationSettings () {
129
+ Log .d (TAG , "Switch to Location Settings" );
130
+ Intent settingsIntent = new Intent (Settings .ACTION_LOCATION_SOURCE_SETTINGS );
131
+ cordova .getActivity ().startActivity (settingsIntent );
132
+ }
133
+
134
+ private boolean isLocationProviderEnabled (String provider ) {
135
+ LocationManager locationManager = (LocationManager ) this .cordova .getActivity ().getSystemService (Context .LOCATION_SERVICE );
136
+ return locationManager .isProviderEnabled (provider );
137
+ }
138
+
139
+ }
0 commit comments