mirror of
				https://github.com/commons-app/apps-android-commons.git
				synced 2025-10-31 06:43:56 +01:00 
			
		
		
		
	Fixed Grey empty screen at Upload wizard (#5356)
* Fixed Grey empty screen at Upload wizard caption step after denying files permission * Empty commit * Fixed loop issue * Created docs for earlier commits * Fixed javadoc * Fixed spaces
This commit is contained in:
		
							parent
							
								
									6f96e8959b
								
							
						
					
					
						commit
						187872114c
					
				
					 3 changed files with 108 additions and 15 deletions
				
			
		|  | @ -2,6 +2,7 @@ package fr.free.nrw.commons.upload; | |||
| 
 | ||||
| import static fr.free.nrw.commons.contributions.ContributionController.ACTION_INTERNAL_UPLOADS; | ||||
| import static fr.free.nrw.commons.utils.PermissionUtils.PERMISSIONS_STORAGE; | ||||
| import static fr.free.nrw.commons.utils.PermissionUtils.checkPermissionsAndPerformAction; | ||||
| import static fr.free.nrw.commons.wikidata.WikidataConstants.PLACE_OBJECT; | ||||
| import static fr.free.nrw.commons.wikidata.WikidataConstants.SELECTED_NEARBY_PLACE; | ||||
| import static fr.free.nrw.commons.wikidata.WikidataConstants.SELECTED_NEARBY_PLACE_CATEGORY; | ||||
|  | @ -146,6 +147,23 @@ public class UploadActivity extends BaseActivity implements UploadContract.View, | |||
|      */ | ||||
|     public static HashMap<Place,Boolean> nearbyPopupAnswers; | ||||
| 
 | ||||
|     /** | ||||
|      * A private boolean variable to control whether a permissions dialog should be shown | ||||
|      * when necessary. Initially, it is set to `true`, indicating that the permissions dialog | ||||
|      * should be displayed if permissions are missing and it is first time calling | ||||
|      * `checkStoragePermissions` method. | ||||
|      * | ||||
|      * This variable is used in the `checkStoragePermissions` method to determine whether to | ||||
|      * show a permissions dialog to the user if the required permissions are not granted. | ||||
|      * | ||||
|      * If `showPermissionsDialog` is set to `true` and the necessary permissions are missing, | ||||
|      * a permissions dialog will be displayed to request the required permissions. If set | ||||
|      * to `false`, the dialog won't be shown. | ||||
|      * | ||||
|      * @see UploadActivity#checkStoragePermissions() | ||||
|      */ | ||||
|     private boolean showPermissionsDialog = true; | ||||
| 
 | ||||
|     @SuppressLint("CheckResult") | ||||
|     @Override | ||||
|     protected void onCreate(Bundle savedInstanceState) { | ||||
|  | @ -169,7 +187,6 @@ public class UploadActivity extends BaseActivity implements UploadContract.View, | |||
|         } | ||||
|         locationManager.requestLocationUpdatesFromProvider(LocationManager.GPS_PROVIDER); | ||||
|         locationManager.requestLocationUpdatesFromProvider(LocationManager.NETWORK_PROVIDER); | ||||
|         checkStoragePermissions(); | ||||
|     } | ||||
| 
 | ||||
|     private void init() { | ||||
|  | @ -227,6 +244,12 @@ public class UploadActivity extends BaseActivity implements UploadContract.View, | |||
|         return sessionManager.isUserLoggedIn(); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     protected void onStart() { | ||||
|         super.onStart(); | ||||
|         checkStoragePermissions(); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     protected void onResume() { | ||||
|         super.onResume(); | ||||
|  | @ -235,6 +258,7 @@ public class UploadActivity extends BaseActivity implements UploadContract.View, | |||
|             askUserToLogIn(); | ||||
|         } | ||||
|         checkBlockStatus(); | ||||
|         checkStoragePermissions(); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|  | @ -255,13 +279,36 @@ public class UploadActivity extends BaseActivity implements UploadContract.View, | |||
|                 true))); | ||||
|     } | ||||
| 
 | ||||
|     private void checkStoragePermissions() { | ||||
|     public void checkStoragePermissions() { | ||||
|         // Check if all required permissions are granted | ||||
|         final boolean hasAllPermissions = PermissionUtils.hasPermission(this, PERMISSIONS_STORAGE); | ||||
|         if (hasAllPermissions) { | ||||
|             // All required permissions are granted, so enable UI elements and perform actions | ||||
|             receiveSharedItems(); | ||||
|         } else if (VERSION.SDK_INT >= VERSION_CODES.M) { | ||||
|             requestPermissions(PERMISSIONS_STORAGE, RequestCodes.STORAGE); | ||||
|             cvContainerTopCard.setVisibility(View.VISIBLE); | ||||
|         } else { | ||||
|             // Permissions are missing | ||||
|             cvContainerTopCard.setVisibility(View.INVISIBLE); | ||||
|             if(showPermissionsDialog){ | ||||
|                 checkPermissionsAndPerformAction(this, | ||||
|                     () -> { | ||||
|                         cvContainerTopCard.setVisibility(View.VISIBLE); | ||||
|                         this.receiveSharedItems(); | ||||
|                     },() -> { | ||||
|                         this.showPermissionsDialog = true; | ||||
|                         this.checkStoragePermissions(); | ||||
|                         }, | ||||
|                     R.string.storage_permission_title, | ||||
|                     R.string.write_storage_permission_rationale_for_image_share, | ||||
|                     PERMISSIONS_STORAGE); | ||||
|             } | ||||
|         } | ||||
|         /* If all permissions are not granted and a dialog is already showing on screen | ||||
|          showPermissionsDialog will set to false making it not show dialog again onResume, | ||||
|          but if user Denies any permission showPermissionsDialog will be to true | ||||
|          and permissions dialog will be shown again. | ||||
|          */ | ||||
|         this.showPermissionsDialog = hasAllPermissions ; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|  | @ -721,6 +768,25 @@ public class UploadActivity extends BaseActivity implements UploadContract.View, | |||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Get the value of the showPermissionDialog variable. | ||||
|      * | ||||
|      * @return {@code true} if Permission Dialog should be shown, {@code false} otherwise. | ||||
|      */ | ||||
|     public boolean isShowPermissionsDialog() { | ||||
|         return showPermissionsDialog; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Set the value of the showPermissionDialog variable. | ||||
|      * | ||||
|      * @param showPermissionsDialog {@code true} to indicate to show | ||||
|      * Permissions Dialog if permissions are missing, {@code false} otherwise. | ||||
|      */ | ||||
|     public void setShowPermissionsDialog(final boolean showPermissionsDialog) { | ||||
|         this.showPermissionsDialog = showPermissionsDialog; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Overrides the back button to make sure the user is prepared to lose their progress | ||||
|      */ | ||||
|  |  | |||
|  | @ -7,6 +7,7 @@ import android.content.pm.PackageManager; | |||
| import android.net.Uri; | ||||
| import android.os.Build; | ||||
| import android.provider.Settings; | ||||
| import android.widget.Toast; | ||||
| import androidx.annotation.StringRes; | ||||
| import androidx.core.content.ContextCompat; | ||||
| import com.karumi.dexter.Dexter; | ||||
|  | @ -16,6 +17,7 @@ import com.karumi.dexter.listener.PermissionRequest; | |||
| import com.karumi.dexter.listener.multi.MultiplePermissionsListener; | ||||
| import fr.free.nrw.commons.CommonsApplication; | ||||
| import fr.free.nrw.commons.R; | ||||
| import fr.free.nrw.commons.upload.UploadActivity; | ||||
| import java.util.List; | ||||
| 
 | ||||
| 
 | ||||
|  | @ -80,14 +82,14 @@ public class PermissionUtils { | |||
|      * <p> | ||||
|      * Sample usage: | ||||
|      * <p> | ||||
|      * PermissionUtils.checkPermissionsAndPerformAction(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE, | ||||
|      * () -> initiateCameraUpload(activity), R.string.storage_permission_title, | ||||
|      * R.string.write_storage_permission_rationale); | ||||
|      * PermissionUtils.checkPermissionsAndPerformAction(activity, | ||||
|      * Manifest.permission.WRITE_EXTERNAL_STORAGE, () -> initiateCameraUpload(activity), | ||||
|      * R.string.storage_permission_title, R.string.write_storage_permission_rationale); | ||||
|      * <p> | ||||
|      * If you don't want the permission rationale to be shown then use: | ||||
|      * <p> | ||||
|      * PermissionUtils.checkPermissionsAndPerformAction(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE, | ||||
|      * () -> initiateCameraUpload(activity), - 1, -1); | ||||
|      * PermissionUtils.checkPermissionsAndPerformAction(activity, | ||||
|      * Manifest.permission.WRITE_EXTERNAL_STORAGE, () -> initiateCameraUpload(activity), - 1, -1); | ||||
|      * | ||||
|      * @param activity            activity requesting permissions | ||||
|      * @param permissions         the permissions array being requests | ||||
|  | @ -110,8 +112,9 @@ public class PermissionUtils { | |||
|      * <p> | ||||
|      * Sample usage: | ||||
|      * <p> | ||||
|      * PermissionUtils.checkPermissionsAndPerformAction(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE, | ||||
|      * () -> initiateCameraUpload(activity), () -> showMessage(), R.string.storage_permission_title, | ||||
|      * PermissionUtils.checkPermissionsAndPerformAction(activity, | ||||
|      * Manifest.permission.WRITE_EXTERNAL_STORAGE, () -> initiateCameraUpload(activity), () -> | ||||
|      * showMessage(), R.string.storage_permission_title, | ||||
|      * R.string.write_storage_permission_rationale); | ||||
|      * | ||||
|      * @param activity            activity requesting permissions | ||||
|  | @ -138,8 +141,15 @@ public class PermissionUtils { | |||
|                         // permission is denied permanently, we will show user a dialog message. | ||||
|                         DialogUtil.showAlertDialog(activity, activity.getString(rationaleTitle), | ||||
|                             activity.getString(rationaleMessage), | ||||
|                             activity.getString(R.string.navigation_item_settings), null, | ||||
|                             () -> askUserToManuallyEnablePermissionFromSettings(activity), null); | ||||
|                             activity.getString(R.string.navigation_item_settings), | ||||
|                             null, | ||||
|                             () -> { | ||||
|                                 askUserToManuallyEnablePermissionFromSettings(activity); | ||||
|                                 if (activity instanceof UploadActivity) { | ||||
|                                     ((UploadActivity) activity).setShowPermissionsDialog(true); | ||||
|                                 } | ||||
|                             }, null, null, | ||||
|                             !(activity instanceof UploadActivity)); | ||||
|                     } else { | ||||
|                         if (null != onPermissionDenied) { | ||||
|                             onPermissionDenied.run(); | ||||
|  | @ -158,8 +168,24 @@ public class PermissionUtils { | |||
|                         activity.getString(rationaleMessage), | ||||
|                         activity.getString(android.R.string.ok), | ||||
|                         activity.getString(android.R.string.cancel), | ||||
|                         token::continuePermissionRequest, | ||||
|                         token::cancelPermissionRequest, | ||||
|                         () -> { | ||||
|                             if (activity instanceof UploadActivity) { | ||||
|                                 ((UploadActivity) activity).setShowPermissionsDialog(true); | ||||
|                             } | ||||
|                             token.continuePermissionRequest(); | ||||
|                         } | ||||
|                         , | ||||
|                         () -> { | ||||
|                             Toast.makeText(activity.getApplicationContext(), | ||||
|                                     R.string.permissions_are_required_for_functionality, | ||||
|                                     Toast.LENGTH_LONG) | ||||
|                                 .show(); | ||||
|                             token.cancelPermissionRequest(); | ||||
|                             if (activity instanceof UploadActivity) { | ||||
|                                 activity.finish(); | ||||
|                             } | ||||
|                         } | ||||
|                         , | ||||
|                         null, | ||||
|                         false); | ||||
|                 } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Kanahia
						Kanahia