@@ -191,6 +191,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
191
191
private Animation rotate_forward ;
192
192
193
193
private static final float ZOOM_LEVEL = 14f ;
194
+ private static final float ZOOM_OUT = 0f ;
194
195
private final String NETWORK_INTENT_ACTION = "android.net.conn.CONNECTIVITY_CHANGE" ;
195
196
private BroadcastReceiver broadcastReceiver ;
196
197
private boolean isNetworkErrorOccurred ;
@@ -206,6 +207,8 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
206
207
private final double CAMERA_TARGET_SHIFT_FACTOR_LANDSCAPE = 0.004 ;
207
208
208
209
private boolean isMapBoxReady ;
210
+ private boolean isPermissionDenied ;
211
+ private boolean recenterToUserLocation ;
209
212
private MapboxMap mapBox ;
210
213
IntentFilter intentFilter = new IntentFilter (NETWORK_INTENT_ACTION );
211
214
private Marker currentLocationMarker ;
@@ -259,6 +262,8 @@ public void onViewCreated(@NonNull final View view, @Nullable final Bundle saved
259
262
cameraMoveListener = () -> presenter .onCameraMove (mapBox .getCameraPosition ().target );
260
263
addCheckBoxCallback ();
261
264
presenter .attachView (this );
265
+ isPermissionDenied = false ;
266
+ recenterToUserLocation = false ;
262
267
initRvNearbyList ();
263
268
initThemePreferences ();
264
269
mapView .onCreate (savedInstanceState );
@@ -281,7 +286,7 @@ public void onViewCreated(@NonNull final View view, @Nullable final Bundle saved
281
286
performMapReadyActions ();
282
287
final CameraPosition cameraPosition = new CameraPosition .Builder ()
283
288
.target (new LatLng (51.50550 , -0.07520 ))
284
- .zoom (ZOOM_LEVEL )
289
+ .zoom (ZOOM_OUT )
285
290
.build ();
286
291
mapBoxMap .setCameraPosition (cameraPosition );
287
292
@@ -342,32 +347,43 @@ private void addCheckBoxCallback() {
342
347
343
348
private void performMapReadyActions () {
344
349
if (((MainActivity )getActivity ()).activeFragment == ActiveFragment .NEARBY && isMapBoxReady ) {
345
- checkPermissionsAndPerformAction (() -> {
346
- lastKnownLocation = locationManager .getLastLocation ();
347
- fr .free .nrw .commons .location .LatLng target =lastFocusLocation ;
348
- if (null ==lastFocusLocation ){
349
- target =lastKnownLocation ;
350
- }
351
- if (lastKnownLocation != null ) {
352
- final CameraPosition position = new CameraPosition .Builder ()
353
- .target (LocationUtils .commonsLatLngToMapBoxLatLng (target )) // Sets the new camera position
354
- .zoom (ZOOM_LEVEL ) // Same zoom level
355
- .build ();
356
- mapBox .moveCamera (CameraUpdateFactory .newCameraPosition (position ));
357
- }
358
- else if (locationManager .isGPSProviderEnabled ()||locationManager .isNetworkProviderEnabled ()){
359
- locationManager .requestLocationUpdatesFromProvider (LocationManager .NETWORK_PROVIDER );
360
- locationManager .requestLocationUpdatesFromProvider (LocationManager .GPS_PROVIDER );
361
- setProgressBarVisibility (true );
362
- }
363
- else {
364
- Toast .makeText (getContext (), getString (R .string .nearby_location_not_available ), Toast .LENGTH_LONG ).show ();
365
- }
366
- presenter .onMapReady ();
367
- registerUnregisterLocationListener (false );
350
+ if (!applicationKvStore .getBoolean ("doNotAskForLocationPermission" , false ) ||
351
+ PermissionUtils .hasPermission (getActivity (), Manifest .permission .ACCESS_FINE_LOCATION )){
352
+ checkPermissionsAndPerformAction ();
353
+ }else {
354
+ isPermissionDenied = true ;
368
355
addOnCameraMoveListener ();
369
- });
356
+ }
357
+ }
358
+ }
359
+
360
+ private void locationPermissionGranted () {
361
+ isPermissionDenied = false ;
362
+
363
+ applicationKvStore .putBoolean ("doNotAskForLocationPermission" , false );
364
+ lastKnownLocation = locationManager .getLastLocation ();
365
+ fr .free .nrw .commons .location .LatLng target =lastFocusLocation ;
366
+ if (null ==lastFocusLocation ){
367
+ target =lastKnownLocation ;
368
+ }
369
+ if (lastKnownLocation != null ) {
370
+ final CameraPosition position = new CameraPosition .Builder ()
371
+ .target (LocationUtils .commonsLatLngToMapBoxLatLng (target )) // Sets the new camera position
372
+ .zoom (ZOOM_LEVEL ) // Same zoom level
373
+ .build ();
374
+ mapBox .moveCamera (CameraUpdateFactory .newCameraPosition (position ));
370
375
}
376
+ else if (locationManager .isGPSProviderEnabled ()||locationManager .isNetworkProviderEnabled ()){
377
+ locationManager .requestLocationUpdatesFromProvider (LocationManager .NETWORK_PROVIDER );
378
+ locationManager .requestLocationUpdatesFromProvider (LocationManager .GPS_PROVIDER );
379
+ setProgressBarVisibility (true );
380
+ }
381
+ else {
382
+ Toast .makeText (getContext (), getString (R .string .nearby_location_not_available ), Toast .LENGTH_LONG ).show ();
383
+ }
384
+ presenter .onMapReady ();
385
+ registerUnregisterLocationListener (false );
386
+ addOnCameraMoveListener ();
371
387
}
372
388
373
389
@ Override
@@ -377,8 +393,29 @@ public void onResume() {
377
393
presenter .attachView (this );
378
394
registerNetworkReceiver ();
379
395
if (isResumed () && ((MainActivity )getActivity ()).activeFragment == ActiveFragment .NEARBY ) {
380
- startTheMap ();
396
+ if (!isPermissionDenied && !applicationKvStore .getBoolean ("doNotAskForLocationPermission" , false )){
397
+ startTheMap ();
398
+ }else {
399
+ startMapWithoutPermission ();
400
+ }
401
+ }
402
+ }
403
+
404
+ private void startMapWithoutPermission () {
405
+ mapView .onStart ();
406
+
407
+ applicationKvStore .putBoolean ("doNotAskForLocationPermission" , true );
408
+ lastKnownLocation = new fr .free .nrw .commons .location .LatLng (51.50550 ,-0.07520 ,1f );
409
+ final CameraPosition position = new CameraPosition .Builder ()
410
+ .target (LocationUtils .commonsLatLngToMapBoxLatLng (lastKnownLocation ))
411
+ .zoom (ZOOM_OUT )
412
+ .build ();
413
+ if (mapBox != null ){
414
+ mapBox .moveCamera (CameraUpdateFactory .newCameraPosition (position ));
415
+ addOnCameraMoveListener ();
381
416
}
417
+ presenter .onMapReady ();
418
+ removeCurrentLocationMarker ();
382
419
}
383
420
384
421
private void registerNetworkReceiver () {
@@ -795,11 +832,14 @@ public void listOptionMenuItemClicked() {
795
832
796
833
@ Override
797
834
public void populatePlaces (final fr .free .nrw .commons .location .LatLng curlatLng ) {
798
- if (curlatLng .equals (lastFocusLocation )|| lastFocusLocation == null ) { // Means we are checking around current location
835
+ if (curlatLng .equals (lastFocusLocation ) || lastFocusLocation == null || recenterToUserLocation ) { // Means we are checking around current location
799
836
populatePlacesForCurrentLocation (lastKnownLocation , curlatLng );
800
837
} else {
801
838
populatePlacesForAnotherLocation (lastKnownLocation , curlatLng );
802
839
}
840
+ if (recenterToUserLocation ) {
841
+ recenterToUserLocation = false ;
842
+ }
803
843
}
804
844
805
845
private void populatePlacesForCurrentLocation (final fr .free .nrw .commons .location .LatLng curlatLng ,
@@ -918,12 +958,12 @@ public void setTabItemContributions() {
918
958
}
919
959
920
960
@ Override
921
- public void checkPermissionsAndPerformAction (final Runnable runnable ) {
961
+ public void checkPermissionsAndPerformAction () {
922
962
Timber .d ("Checking permission and perfoming action" );
923
963
PermissionUtils .checkPermissionsAndPerformAction (getActivity (),
924
964
Manifest .permission .ACCESS_FINE_LOCATION ,
925
- runnable ,
926
- () -> (( MainActivity ) getActivity ()). setSelectedItemId ( NavTab . CONTRIBUTIONS . code ()) ,
965
+ () -> locationPermissionGranted () ,
966
+ () -> isPermissionDenied = true ,
927
967
R .string .location_permission_title ,
928
968
R .string .location_permission_rationale_nearby );
929
969
}
@@ -1095,7 +1135,7 @@ public void enableFABRecenter() {
1095
1135
*/
1096
1136
@ Override
1097
1137
public void addCurrentLocationMarker (final fr .free .nrw .commons .location .LatLng curLatLng ) {
1098
- if (null != curLatLng ) {
1138
+ if (null != curLatLng && ! isPermissionDenied ) {
1099
1139
ExecutorUtils .get ().submit (() -> {
1100
1140
mapView .post (() -> removeCurrentLocationMarker ());
1101
1141
Timber .d ("Adds current location marker" );
@@ -1141,8 +1181,15 @@ private void removeCurrentLocationMarker() {
1141
1181
@ Override
1142
1182
public void updateMapToTrackPosition (final fr .free .nrw .commons .location .LatLng curLatLng ) {
1143
1183
Timber .d ("Updates map camera to track user position" );
1144
- final CameraPosition cameraPosition = new CameraPosition .Builder ().target
1184
+ final CameraPosition cameraPosition ;
1185
+ if (isPermissionDenied ){
1186
+ cameraPosition = new CameraPosition .Builder ().target
1145
1187
(LocationUtils .commonsLatLngToMapBoxLatLng (curLatLng )).build ();
1188
+ }else {
1189
+ cameraPosition = new CameraPosition .Builder ().target
1190
+ (LocationUtils .commonsLatLngToMapBoxLatLng (curLatLng ))
1191
+ .zoom (ZOOM_LEVEL ).build ();
1192
+ }
1146
1193
if (null !=mapBox ) {
1147
1194
mapBox .setCameraPosition (cameraPosition );
1148
1195
mapBox .animateCamera (CameraUpdateFactory
@@ -1326,8 +1373,10 @@ private void setMapMarkerActions(final Marker selectedMarker) {
1326
1373
1327
1374
@ Override
1328
1375
public void recenterMap (final fr .free .nrw .commons .location .LatLng curLatLng ) {
1329
- if (curLatLng == null ) {
1330
- if (!(locationManager .isNetworkProviderEnabled () || locationManager .isGPSProviderEnabled ())) {
1376
+ if (isPermissionDenied || curLatLng == null ) {
1377
+ recenterToUserLocation = true ;
1378
+ checkPermissionsAndPerformAction ();
1379
+ if (!isPermissionDenied && !(locationManager .isNetworkProviderEnabled () || locationManager .isGPSProviderEnabled ())) {
1331
1380
showLocationOffDialog ();
1332
1381
}
1333
1382
return ;
0 commit comments