@@ -14,8 +14,6 @@ Licensed to the Apache Software Foundation (ASF) under one
14
14
specific language governing permissions and limitations
15
15
under the License.
16
16
*/
17
-
18
-
19
17
package org .apache .cordova .geolocation ;
20
18
21
19
import android .content .Context ;
@@ -32,27 +30,23 @@ Licensed to the Apache Software Foundation (ASF) under one
32
30
import org .json .JSONArray ;
33
31
import org .json .JSONException ;
34
32
35
-
36
33
public class Geolocation extends CordovaPlugin {
37
34
38
35
String TAG = "GeolocationPlugin" ;
39
36
CallbackContext context ;
40
37
41
-
42
- String [] highAccuracyPermissions = { Manifest .permission .ACCESS_COARSE_LOCATION , Manifest .permission .ACCESS_FINE_LOCATION };
43
- String [] lowAccuracyPermissions = { Manifest .permission .ACCESS_COARSE_LOCATION };
44
- String [] permissionsToRequest ;
38
+ String [] highAccuracyPermissions = {Manifest .permission .ACCESS_COARSE_LOCATION , Manifest .permission .ACCESS_FINE_LOCATION };
39
+ String [] lowAccuracyPermissions = {Manifest .permission .ACCESS_COARSE_LOCATION };
40
+ String [] permissionsToRequest ;
45
41
String [] permissionsToCheck ;
46
42
47
43
LocationManager manager ;
48
44
49
45
public boolean execute (String action , JSONArray args , CallbackContext callbackContext ) throws JSONException {
50
46
LOG .d (TAG , "We are entering execute" );
51
47
context = callbackContext ;
52
- if (action .equals ("getPermission" ))
53
- {
54
- if (!isLocationProviderAvailable ())
55
- {
48
+ if (action .equals ("getPermission" )) {
49
+ if (!isLocationProviderAvailable ()) {
56
50
LOG .d (TAG , "Location Provider Unavailable!" );
57
51
PluginResult result = new PluginResult (PluginResult .Status .ILLEGAL_ACCESS_EXCEPTION );
58
52
context .sendPluginResult (result );
@@ -66,36 +60,28 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
66
60
// See https://bugs.chromium.org/p/chromium/issues/detail?id=1269362
67
61
permissionsToRequest = Build .VERSION .SDK_INT <= 31 ? highAccuracyPermissions : permissionsToCheck ;
68
62
69
- if (hasPermission (permissionsToCheck ))
70
- {
63
+ if (hasPermission (permissionsToCheck )) {
71
64
PluginResult r = new PluginResult (PluginResult .Status .OK , Build .VERSION .SDK_INT );
72
65
context .sendPluginResult (r );
73
66
return true ;
74
- }
75
- else {
67
+ } else {
76
68
PermissionHelper .requestPermissions (this , 0 , permissionsToRequest );
77
69
}
78
70
return true ;
79
71
}
80
72
return false ;
81
73
}
82
74
83
-
84
- public void onRequestPermissionResult (int requestCode , String [] permissions ,
85
- int [] grantResults ) throws JSONException
86
- {
75
+ public void onRequestPermissionResult (int requestCode , String [] permissions , int [] grantResults ) {
87
76
PluginResult result ;
88
77
//This is important if we're using Cordova without using Cordova, but we have the geolocation plugin installed
89
- if (context != null ) {
90
- for (int i =0 ; i <grantResults .length ; i ++) {
91
- int r = grantResults [i ];
92
- String p = permissions [i ];
78
+ if (context != null ) {
79
+ for (int r : grantResults ) {
93
80
if (r == PackageManager .PERMISSION_GRANTED ) {
94
81
result = new PluginResult (PluginResult .Status .OK );
95
82
context .sendPluginResult (result );
96
83
return ;
97
84
}
98
-
99
85
}
100
86
LOG .d (TAG , "Permission Denied!" );
101
87
result = new PluginResult (PluginResult .Status .ILLEGAL_ACCESS_EXCEPTION );
@@ -104,10 +90,8 @@ public void onRequestPermissionResult(int requestCode, String[] permissions,
104
90
}
105
91
106
92
public boolean hasPermission (String [] permissions ) {
107
- for (String p : permissions )
108
- {
109
- if (PermissionHelper .hasPermission (this , p ))
110
- {
93
+ for (String p : permissions ) {
94
+ if (PermissionHelper .hasPermission (this , p )) {
111
95
return true ;
112
96
}
113
97
}
@@ -118,15 +102,12 @@ public boolean hasPermission(String[] permissions) {
118
102
* We override this so that we can access the permissions variable, which no longer exists in
119
103
* the parent class, since we can't initialize it reliably in the constructor!
120
104
*/
121
-
122
- public void requestPermissions (int requestCode )
123
- {
105
+ public void requestPermissions (int requestCode ) {
124
106
PermissionHelper .requestPermissions (this , requestCode , permissionsToRequest );
125
107
}
126
108
127
- private boolean isLocationProviderAvailable ()
128
- {
109
+ private boolean isLocationProviderAvailable () {
129
110
manager = (LocationManager ) this .cordova .getActivity ().getApplicationContext ().getSystemService (Context .LOCATION_SERVICE );
130
- return manager .isProviderEnabled (LocationManager .FUSED_PROVIDER );
111
+ return manager .isProviderEnabled (LocationManager .GPS_PROVIDER ) || manager . isProviderEnabled ( LocationManager . NETWORK_PROVIDER );
131
112
}
132
113
}
0 commit comments