11package fr .free .nrw .commons .contributions ;
22
3+ import android .Manifest ;
34import android .content .ComponentName ;
45import android .content .Context ;
56import android .content .Intent ;
2021import android .support .v4 .content .Loader ;
2122import android .support .v4 .app .LoaderManager ;
2223import android .support .v4 .widget .CursorAdapter ;
23- import android .util . Log ;
24+ import android .support . v7 . app . AlertDialog ;
2425import android .view .LayoutInflater ;
2526import android .view .View ;
2627import android .view .ViewGroup ;
2728import android .widget .Adapter ;
2829import android .widget .AdapterView ;
30+ import android .widget .CheckBox ;
31+ import android .widget .CompoundButton ;
2932
3033import java .util .ArrayList ;
3134import java .util .concurrent .CountDownLatch ;
5053import fr .free .nrw .commons .notification .UnreadNotificationsCheckAsync ;
5154import fr .free .nrw .commons .settings .Prefs ;
5255import fr .free .nrw .commons .upload .UploadService ;
56+ import fr .free .nrw .commons .utils .DialogUtil ;
57+ import fr .free .nrw .commons .utils .ViewUtil ;
5358import io .reactivex .Observable ;
5459import io .reactivex .android .schedulers .AndroidSchedulers ;
5560import io .reactivex .disposables .CompositeDisposable ;
@@ -102,10 +107,11 @@ public class ContributionsFragment
102107 private LatLng curLatLng ;
103108
104109 private boolean firstLocationUpdate = true ;
105- private LocationServiceManager locationManager ;
110+ public LocationServiceManager locationManager ;
106111
107112 private boolean isFragmentAttachedBefore = false ;
108-
113+ private View checkBoxView ;
114+ private CheckBox checkBox ;
109115
110116 /**
111117 * Since we will need to use parent activity on onAuthCookieAcquired, we have to wait
@@ -137,6 +143,18 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
137143 public View onCreateView (LayoutInflater inflater , @ Nullable ViewGroup container , @ Nullable Bundle savedInstanceState ) {
138144 View view = inflater .inflate (R .layout .fragment_contributions , container , false );
139145 nearbyNoificationCardView = view .findViewById (R .id .card_view_nearby );
146+ checkBoxView = View .inflate (getActivity (), R .layout .nearby_permission_dialog , null );
147+ checkBox = (CheckBox ) checkBoxView .findViewById (R .id .never_ask_again );
148+ checkBox .setOnCheckedChangeListener (new CompoundButton .OnCheckedChangeListener () {
149+
150+ @ Override
151+ public void onCheckedChanged (CompoundButton buttonView , boolean isChecked ) {
152+ if (isChecked ) {
153+ // Do not ask for permission on activity start again
154+ prefs .edit ().putBoolean ("displayLocationPermissionForCardView" ,false ).apply ();
155+ }
156+ }
157+ });
140158
141159 if (savedInstanceState != null ) {
142160 mediaDetailPagerFragment = (MediaDetailPagerFragment )getChildFragmentManager ().findFragmentByTag (MEDIA_DETAIL_PAGER_FRAGMENT_TAG );
@@ -186,7 +204,9 @@ public void setContributionsListFragment() {
186204 // show nearby card view on contributions list is visible
187205 if (nearbyNoificationCardView != null ) {
188206 if (prefs .getBoolean ("displayNearbyCardView" , true )) {
189- nearbyNoificationCardView .setVisibility (View .VISIBLE );
207+ if (nearbyNoificationCardView .cardViewVisibilityState == NearbyNoificationCardView .CardViewVisibilityState .READY ) {
208+ nearbyNoificationCardView .setVisibility (View .VISIBLE );
209+ }
190210 } else {
191211 nearbyNoificationCardView .setVisibility (View .GONE );
192212 }
@@ -312,16 +332,24 @@ public void onAuthCookieAcquired(Intent uploadServiceIntent) {
312332
313333 @ Override
314334 public void onRequestPermissionsResult (int requestCode , @ NonNull String [] permissions , @ NonNull int [] grantResults ) {
335+ Timber .d ("onRequestPermissionsResult" );
315336 switch (requestCode ) {
316337 case LOCATION_REQUEST : {
317338 if (grantResults .length > 0 && grantResults [0 ] == PackageManager .PERMISSION_GRANTED ) {
318339 Timber .d ("Location permission granted, refreshing view" );
319340 // No need to display permission request button anymore
320- nearbyNoificationCardView .displayPermissionRequestButton (false );
321341 locationManager .registerLocationManager ();
322342 } else {
323- // Still ask for permission
324- nearbyNoificationCardView .displayPermissionRequestButton (true );
343+ if (prefs .getBoolean ("displayLocationPermissionForCardView" , true )) {
344+ // Still ask for permission
345+ DialogUtil .showAlertDialog (getActivity (),
346+ getString (R .string .nearby_card_permission_title ),
347+ getString (R .string .nearby_card_permission_explanation ),
348+ () -> displayYouWontSeeNearbyMessage (),
349+ () -> enableLocationPermission (),
350+ checkBoxView ,
351+ false );
352+ }
325353 }
326354 }
327355 break ;
@@ -499,9 +527,10 @@ public void onResume() {
499527
500528
501529 if (prefs .getBoolean ("displayNearbyCardView" , true )) {
502- nearbyNoificationCardView .cardViewVisibilityState = NearbyNoificationCardView .CardViewVisibilityState .LOADING ;
503- nearbyNoificationCardView .setVisibility (View .VISIBLE );
504530 checkGPS ();
531+ if (nearbyNoificationCardView .cardViewVisibilityState == NearbyNoificationCardView .CardViewVisibilityState .READY ) {
532+ nearbyNoificationCardView .setVisibility (View .VISIBLE );
533+ }
505534
506535 } else {
507536 // Hide nearby notification card view if related shared preferences is false
@@ -511,15 +540,22 @@ public void onResume() {
511540
512541 }
513542
514-
515543 /**
516544 * Check GPS to decide displaying request permission button or not.
517545 */
518546 private void checkGPS () {
519547 if (!locationManager .isProviderEnabled ()) {
520548 Timber .d ("GPS is not enabled" );
521549 nearbyNoificationCardView .permissionType = NearbyNoificationCardView .PermissionType .ENABLE_GPS ;
522- nearbyNoificationCardView .displayPermissionRequestButton (true );
550+ if (prefs .getBoolean ("displayLocationPermissionForCardView" , true )) {
551+ DialogUtil .showAlertDialog (getActivity (),
552+ getString (R .string .nearby_card_permission_title ),
553+ getString (R .string .nearby_card_permission_explanation ),
554+ () -> displayYouWontSeeNearbyMessage (),
555+ () -> enableGPS (),
556+ checkBoxView ,
557+ false );
558+ }
523559 } else {
524560 Timber .d ("GPS is enabled" );
525561 checkLocationPermission ();
@@ -530,20 +566,57 @@ private void checkLocationPermission() {
530566 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
531567 if (locationManager .isLocationPermissionGranted ()) {
532568 nearbyNoificationCardView .permissionType = NearbyNoificationCardView .PermissionType .NO_PERMISSION_NEEDED ;
533- nearbyNoificationCardView .displayPermissionRequestButton (false );
534569 locationManager .registerLocationManager ();
535570 } else {
536571 nearbyNoificationCardView .permissionType = NearbyNoificationCardView .PermissionType .ENABLE_LOCATION_PERMISSON ;
537- nearbyNoificationCardView .displayPermissionRequestButton (true );
572+ // If user didn't selected Don't ask again
573+ if (shouldShowRequestPermissionRationale (Manifest .permission .ACCESS_FINE_LOCATION )
574+ && prefs .getBoolean ("displayLocationPermissionForCardView" , true )) {
575+ DialogUtil .showAlertDialog (getActivity (),
576+ getString (R .string .nearby_card_permission_title ),
577+ getString (R .string .nearby_card_permission_explanation ),
578+ () -> displayYouWontSeeNearbyMessage (),
579+ () -> enableLocationPermission (),
580+ checkBoxView ,
581+ false );
582+ }
538583 }
539584 } else {
540585 // If device is under Marshmallow, we already checked for GPS
541586 nearbyNoificationCardView .permissionType = NearbyNoificationCardView .PermissionType .NO_PERMISSION_NEEDED ;
542- nearbyNoificationCardView .displayPermissionRequestButton (false );
543587 locationManager .registerLocationManager ();
544588 }
545589 }
546590
591+ private void enableLocationPermission () {
592+ if (!getActivity ().isFinishing ()) {
593+ ((MainActivity ) getActivity ()).locationManager .requestPermissions (getActivity ());
594+ }
595+ }
596+
597+ private void enableGPS () {
598+ new AlertDialog .Builder (getActivity ())
599+ .setMessage (R .string .gps_disabled )
600+ .setCancelable (false )
601+ .setPositiveButton (R .string .enable_gps ,
602+ (dialog , id ) -> {
603+ Intent callGPSSettingIntent = new Intent (
604+ android .provider .Settings .ACTION_LOCATION_SOURCE_SETTINGS );
605+ Timber .d ("Loaded settings page" );
606+ ((MainActivity ) getActivity ()).startActivityForResult (callGPSSettingIntent , 1 );
607+ })
608+ .setNegativeButton (R .string .menu_cancel_upload , (dialog , id ) -> {
609+ dialog .cancel ();
610+ displayYouWontSeeNearbyMessage ();
611+ })
612+ .create ()
613+ .show ();
614+ }
615+
616+ private void displayYouWontSeeNearbyMessage () {
617+ ViewUtil .showLongToast (getActivity (), getResources ().getString (R .string .unable_to_display_nearest_place ));
618+ }
619+
547620
548621 private void updateClosestNearbyCardViewInfo () {
549622 curLatLng = locationManager .getLastLocation ();
0 commit comments