@@ -23,37 +23,46 @@ Licensed to the Apache Software Foundation (ASF) under one
2323import android .os .Build ;
2424
2525import org .apache .cordova .CallbackContext ;
26- import org .apache .cordova .CordovaArgs ;
2726import org .apache .cordova .CordovaPlugin ;
2827import org .apache .cordova .PermissionHelper ;
2928import org .apache .cordova .PluginResult ;
3029import org .apache .cordova .LOG ;
3130import org .json .JSONArray ;
3231import org .json .JSONException ;
3332
34- import javax .security .auth .callback .Callback ;
3533
3634public class Geolocation extends CordovaPlugin {
3735
3836 String TAG = "GeolocationPlugin" ;
3937 CallbackContext context ;
4038
41- String [] permissions = { Manifest .permission .ACCESS_COARSE_LOCATION , Manifest .permission .ACCESS_FINE_LOCATION };
39+
40+ String [] highAccuracyPermissions = { Manifest .permission .ACCESS_COARSE_LOCATION , Manifest .permission .ACCESS_FINE_LOCATION };
41+ String [] lowAccuracyPermissions = { Manifest .permission .ACCESS_COARSE_LOCATION };
42+ String [] permissionsToRequest ;
43+ String [] permissionsToCheck ;
4244
4345
4446 public boolean execute (String action , JSONArray args , CallbackContext callbackContext ) throws JSONException {
4547 LOG .d (TAG , "We are entering execute" );
4648 context = callbackContext ;
4749 if (action .equals ("getPermission" ))
4850 {
49- if (hasPermisssion ())
51+ boolean highAccuracy = args .getBoolean (0 );
52+ permissionsToCheck = highAccuracy ? highAccuracyPermissions : lowAccuracyPermissions ;
53+
54+ // Always request both FINE & COARSE permissions on API <= 31 due to bug in WebView that manifests on these versions
55+ // See https://bugs.chromium.org/p/chromium/issues/detail?id=1269362
56+ permissionsToRequest = Build .VERSION .SDK_INT <= 31 ? highAccuracyPermissions : permissionsToCheck ;
57+
58+ if (hasPermisssion (permissionsToCheck ))
5059 {
51- PluginResult r = new PluginResult (PluginResult .Status .OK );
60+ PluginResult r = new PluginResult (PluginResult .Status .OK , Build . VERSION . SDK_INT );
5261 context .sendPluginResult (r );
5362 return true ;
5463 }
5564 else {
56- PermissionHelper .requestPermissions (this , 0 , permissions );
65+ PermissionHelper .requestPermissions (this , 0 , permissionsToRequest );
5766 }
5867 return true ;
5968 }
@@ -67,8 +76,10 @@ public void onRequestPermissionResult(int requestCode, String[] permissions,
6776 PluginResult result ;
6877 //This is important if we're using Cordova without using Cordova, but we have the geolocation plugin installed
6978 if (context != null ) {
70- for (int r : grantResults ) {
71- if (r == PackageManager .PERMISSION_DENIED ) {
79+ for (int i =0 ; i <grantResults .length ; i ++) {
80+ int r = grantResults [i ];
81+ String p = permissions [i ];
82+ if (r == PackageManager .PERMISSION_DENIED && arrayContains (permissionsToCheck , p )) {
7283 LOG .d (TAG , "Permission Denied!" );
7384 result = new PluginResult (PluginResult .Status .ILLEGAL_ACCESS_EXCEPTION );
7485 context .sendPluginResult (result );
@@ -81,7 +92,7 @@ public void onRequestPermissionResult(int requestCode, String[] permissions,
8192 }
8293 }
8394
84- public boolean hasPermisssion () {
95+ public boolean hasPermisssion (String [] permissions ) {
8596 for (String p : permissions )
8697 {
8798 if (!PermissionHelper .hasPermission (this , p ))
@@ -99,9 +110,23 @@ public boolean hasPermisssion() {
99110
100111 public void requestPermissions (int requestCode )
101112 {
102- PermissionHelper .requestPermissions (this , requestCode , permissions );
113+ PermissionHelper .requestPermissions (this , requestCode , permissionsToRequest );
103114 }
104115
116+ //https://stackoverflow.com/a/12635769/777265
117+ private <T > boolean arrayContains (final T [] array , final T v ) {
118+ if (v == null ) {
119+ for (final T e : array )
120+ if (e == null )
121+ return true ;
122+ }
123+ else {
124+ for (final T e : array )
125+ if (e == v || v .equals (e ))
126+ return true ;
127+ }
105128
129+ return false ;
130+ }
106131
107132}
0 commit comments