55import android .content .Context ;
66import android .content .Intent ;
77import android .content .ServiceConnection ;
8- import android .content .pm .PackageManager ;
98import android .database .Cursor ;
109import android .database .DataSetObserver ;
11- import android .os .Build ;
1210import android .os .Bundle ;
1311import 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+
1419import androidx .annotation .NonNull ;
1520import androidx .annotation .Nullable ;
21+ import androidx .cursoradapter .widget .CursorAdapter ;
1622import androidx .fragment .app .Fragment ;
1723import androidx .fragment .app .FragmentManager ;
1824import androidx .fragment .app .FragmentTransaction ;
1925import androidx .loader .app .LoaderManager ;
2026import androidx .loader .content .CursorLoader ;
2127import 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 ;
3028
3129import java .util .ArrayList ;
3230
5755import fr .free .nrw .commons .upload .UploadService ;
5856import fr .free .nrw .commons .utils .ConfigUtils ;
5957import fr .free .nrw .commons .utils .DialogUtil ;
58+ import fr .free .nrw .commons .utils .PermissionUtils ;
6059import fr .free .nrw .commons .utils .ViewUtil ;
6160import io .reactivex .Observable ;
6261import io .reactivex .android .schedulers .AndroidSchedulers ;
6665
6766import static fr .free .nrw .commons .contributions .ContributionDao .Table .ALL_FIELDS ;
6867import 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 ;
7069import static fr .free .nrw .commons .settings .Prefs .UPLOADS_SHOWING ;
7170import static fr .free .nrw .commons .utils .LengthUtils .formatDistanceBetween ;
7271
@@ -351,35 +350,6 @@ public void onAuthCookieAcquired(Intent uploadServiceIntent) {
351350
352351 }
353352
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- }
383353 /**
384354 * Replace whatever is in the current contributionsFragmentContainer view with
385355 * mediaDetailPagerFragment, and preserve previous state in back stack.
@@ -496,7 +466,7 @@ public void onResume() {
496466
497467
498468 if (store .getBoolean ("displayNearbyCardView" , true )) {
499- checkGPS ();
469+ checkPermissionsAndShowNearbyCardView ();
500470 if (nearbyNotificationCardView .cardViewVisibilityState == NearbyNotificationCardView .CardViewVisibilityState .READY ) {
501471 nearbyNotificationCardView .setVisibility (View .VISIBLE );
502472 }
@@ -509,77 +479,39 @@ public void onResume() {
509479 fetchCampaigns ();
510480 }
511481
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 ();
531490 }
532491 }
533492
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 );
558500 }
559501
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 ();
564505 }
565506
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 );
583515 }
584516
585517 private void displayYouWontSeeNearbyMessage () {
@@ -589,7 +521,6 @@ private void displayYouWontSeeNearbyMessage() {
589521
590522 private void updateClosestNearbyCardViewInfo () {
591523 curLatLng = locationManager .getLastLocation ();
592-
593524 compositeDisposable .add (Observable .fromCallable (() -> nearbyController
594525 .loadAttractionsFromLocation (curLatLng , curLatLng , true , false )) // thanks to boolean, it will only return closest result
595526 .subscribeOn (Schedulers .io ())
0 commit comments