5
5
import android .content .Context ;
6
6
import android .content .Intent ;
7
7
import android .content .ServiceConnection ;
8
- import android .content .pm .PackageManager ;
9
8
import android .database .Cursor ;
10
9
import android .database .DataSetObserver ;
11
- import android .os .Build ;
12
10
import android .os .Bundle ;
13
11
import android .os .IBinder ;
12
+ import android .view .LayoutInflater ;
13
+ import android .view .View ;
14
+ import android .view .ViewGroup ;
15
+ import android .widget .Adapter ;
16
+ import android .widget .CheckBox ;
17
+ import android .widget .Toast ;
18
+
14
19
import androidx .annotation .NonNull ;
15
20
import androidx .annotation .Nullable ;
21
+ import androidx .cursoradapter .widget .CursorAdapter ;
16
22
import androidx .fragment .app .Fragment ;
17
23
import androidx .fragment .app .FragmentManager ;
18
24
import androidx .fragment .app .FragmentTransaction ;
19
25
import androidx .loader .app .LoaderManager ;
20
26
import androidx .loader .content .CursorLoader ;
21
27
import androidx .loader .content .Loader ;
22
- import androidx .cursoradapter .widget .CursorAdapter ;
23
- import androidx .appcompat .app .AlertDialog ;
24
- import android .view .LayoutInflater ;
25
- import android .view .View ;
26
- import android .view .ViewGroup ;
27
- import android .widget .Adapter ;
28
- import android .widget .CheckBox ;
29
- import android .widget .Toast ;
30
28
31
29
import java .util .ArrayList ;
32
30
57
55
import fr .free .nrw .commons .upload .UploadService ;
58
56
import fr .free .nrw .commons .utils .ConfigUtils ;
59
57
import fr .free .nrw .commons .utils .DialogUtil ;
58
+ import fr .free .nrw .commons .utils .PermissionUtils ;
60
59
import fr .free .nrw .commons .utils .ViewUtil ;
61
60
import io .reactivex .Observable ;
62
61
import io .reactivex .android .schedulers .AndroidSchedulers ;
66
65
67
66
import static fr .free .nrw .commons .contributions .ContributionDao .Table .ALL_FIELDS ;
68
67
import static fr .free .nrw .commons .contributions .ContributionsContentProvider .BASE_URI ;
69
- import static fr .free .nrw .commons .location . LocationServiceManager . LOCATION_REQUEST ;
68
+ import static fr .free .nrw .commons .contributions . MainActivity . CONTRIBUTIONS_TAB_POSITION ;
70
69
import static fr .free .nrw .commons .settings .Prefs .UPLOADS_SHOWING ;
71
70
import static fr .free .nrw .commons .utils .LengthUtils .formatDistanceBetween ;
72
71
@@ -351,35 +350,6 @@ public void onAuthCookieAcquired(Intent uploadServiceIntent) {
351
350
352
351
}
353
352
354
- @ Override
355
- public void onRequestPermissionsResult (int requestCode , @ NonNull String [] permissions , @ NonNull int [] grantResults ) {
356
- Timber .d ("onRequestPermissionsResult" );
357
- switch (requestCode ) {
358
- case LOCATION_REQUEST : {
359
- if (grantResults .length > 0 && grantResults [0 ] == PackageManager .PERMISSION_GRANTED ) {
360
- Timber .d ("Location permission granted, refreshing view" );
361
- // No need to display permission request button anymore
362
- locationManager .registerLocationManager ();
363
- } else {
364
- if (store .getBoolean ("displayLocationPermissionForCardView" , true )) {
365
- // Still ask for permission
366
- DialogUtil .showAlertDialog (getActivity (),
367
- getString (R .string .nearby_card_permission_title ),
368
- getString (R .string .nearby_card_permission_explanation ),
369
- this ::displayYouWontSeeNearbyMessage ,
370
- this ::enableLocationPermission ,
371
- checkBoxView ,
372
- false );
373
- }
374
- }
375
- }
376
- break ;
377
-
378
- default :
379
- // This is needed to allow the request codes from the Fragments to be routed appropriately
380
- super .onRequestPermissionsResult (requestCode , permissions , grantResults );
381
- }
382
- }
383
353
/**
384
354
* Replace whatever is in the current contributionsFragmentContainer view with
385
355
* mediaDetailPagerFragment, and preserve previous state in back stack.
@@ -496,7 +466,7 @@ public void onResume() {
496
466
497
467
498
468
if (store .getBoolean ("displayNearbyCardView" , true )) {
499
- checkGPS ();
469
+ checkPermissionsAndShowNearbyCardView ();
500
470
if (nearbyNotificationCardView .cardViewVisibilityState == NearbyNotificationCardView .CardViewVisibilityState .READY ) {
501
471
nearbyNotificationCardView .setVisibility (View .VISIBLE );
502
472
}
@@ -509,77 +479,39 @@ public void onResume() {
509
479
fetchCampaigns ();
510
480
}
511
481
512
- /**
513
- * Check GPS to decide displaying request permission button or not.
514
- */
515
- private void checkGPS () {
516
- if (!locationManager .isProviderEnabled ()) {
517
- Timber .d ("GPS is not enabled" );
518
- nearbyNotificationCardView .permissionType = NearbyNotificationCardView .PermissionType .ENABLE_GPS ;
519
- if (store .getBoolean ("displayLocationPermissionForCardView" , true )) {
520
- DialogUtil .showAlertDialog (getActivity (),
521
- getString (R .string .nearby_card_permission_title ),
522
- getString (R .string .nearby_card_permission_explanation ),
523
- this ::displayYouWontSeeNearbyMessage ,
524
- this ::enableGPS ,
525
- checkBoxView ,
526
- false );
527
- }
528
- } else {
529
- Timber .d ("GPS is enabled" );
530
- checkLocationPermission ();
482
+ private void checkPermissionsAndShowNearbyCardView () {
483
+ if (PermissionUtils .hasPermission (getActivity (), Manifest .permission .ACCESS_FINE_LOCATION )) {
484
+ onLocationPermissionGranted ();
485
+ } else if (shouldShowRequestPermissionRationale (Manifest .permission .ACCESS_FINE_LOCATION )
486
+ && store .getBoolean ("displayLocationPermissionForCardView" , true )
487
+ && (((MainActivity ) getActivity ()).viewPager .getCurrentItem () == CONTRIBUTIONS_TAB_POSITION )) {
488
+ nearbyNotificationCardView .permissionType = NearbyNotificationCardView .PermissionType .ENABLE_LOCATION_PERMISSION ;
489
+ showNearbyCardPermissionRationale ();
531
490
}
532
491
}
533
492
534
- private void checkLocationPermission () {
535
- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
536
- if (locationManager .isLocationPermissionGranted (requireContext ())) {
537
- nearbyNotificationCardView .permissionType = NearbyNotificationCardView .PermissionType .NO_PERMISSION_NEEDED ;
538
- locationManager .registerLocationManager ();
539
- } else {
540
- nearbyNotificationCardView .permissionType = NearbyNotificationCardView .PermissionType .ENABLE_LOCATION_PERMISSION ;
541
- // If user didn't selected Don't ask again
542
- if (shouldShowRequestPermissionRationale (Manifest .permission .ACCESS_FINE_LOCATION )
543
- && store .getBoolean ("displayLocationPermissionForCardView" , true )) {
544
- DialogUtil .showAlertDialog (getActivity (),
545
- getString (R .string .nearby_card_permission_title ),
546
- getString (R .string .nearby_card_permission_explanation ),
547
- this ::displayYouWontSeeNearbyMessage ,
548
- this ::enableLocationPermission ,
549
- checkBoxView ,
550
- false );
551
- }
552
- }
553
- } else {
554
- // If device is under Marshmallow, we already checked for GPS
555
- nearbyNotificationCardView .permissionType = NearbyNotificationCardView .PermissionType .NO_PERMISSION_NEEDED ;
556
- locationManager .registerLocationManager ();
557
- }
493
+ private void requestLocationPermission () {
494
+ PermissionUtils .checkPermissionsAndPerformAction (getActivity (),
495
+ Manifest .permission .ACCESS_FINE_LOCATION ,
496
+ this ::onLocationPermissionGranted ,
497
+ this ::displayYouWontSeeNearbyMessage ,
498
+ -1 ,
499
+ -1 );
558
500
}
559
501
560
- private void enableLocationPermission () {
561
- if (!getActivity ().isFinishing ()) {
562
- ((MainActivity ) getActivity ()).locationManager .requestPermissions (getActivity ());
563
- }
502
+ private void onLocationPermissionGranted () {
503
+ nearbyNotificationCardView .permissionType = NearbyNotificationCardView .PermissionType .NO_PERMISSION_NEEDED ;
504
+ locationManager .registerLocationManager ();
564
505
}
565
506
566
- private void enableGPS () {
567
- new AlertDialog .Builder (getActivity ())
568
- .setMessage (R .string .gps_disabled )
569
- .setCancelable (false )
570
- .setPositiveButton (R .string .enable_gps ,
571
- (dialog , id ) -> {
572
- Intent callGPSSettingIntent = new Intent (
573
- android .provider .Settings .ACTION_LOCATION_SOURCE_SETTINGS );
574
- Timber .d ("Loaded settings page" );
575
- ((MainActivity ) getActivity ()).startActivityForResult (callGPSSettingIntent , 1 );
576
- })
577
- .setNegativeButton (R .string .menu_cancel_upload , (dialog , id ) -> {
578
- dialog .cancel ();
579
- displayYouWontSeeNearbyMessage ();
580
- })
581
- .create ()
582
- .show ();
507
+ private void showNearbyCardPermissionRationale () {
508
+ DialogUtil .showAlertDialog (getActivity (),
509
+ getString (R .string .nearby_card_permission_title ),
510
+ getString (R .string .nearby_card_permission_explanation ),
511
+ this ::displayYouWontSeeNearbyMessage ,
512
+ this ::requestLocationPermission ,
513
+ checkBoxView ,
514
+ false );
583
515
}
584
516
585
517
private void displayYouWontSeeNearbyMessage () {
@@ -589,7 +521,6 @@ private void displayYouWontSeeNearbyMessage() {
589
521
590
522
private void updateClosestNearbyCardViewInfo () {
591
523
curLatLng = locationManager .getLastLocation ();
592
-
593
524
compositeDisposable .add (Observable .fromCallable (() -> nearbyController
594
525
.loadAttractionsFromLocation (curLatLng , curLatLng , true , false )) // thanks to boolean, it will only return closest result
595
526
.subscribeOn (Schedulers .io ())
0 commit comments