2
2
3
3
import static fr .free .nrw .commons .contributions .ContributionController .ACTION_INTERNAL_UPLOADS ;
4
4
import static fr .free .nrw .commons .utils .PermissionUtils .PERMISSIONS_STORAGE ;
5
+ import static fr .free .nrw .commons .utils .PermissionUtils .checkPermissionsAndPerformAction ;
5
6
import static fr .free .nrw .commons .wikidata .WikidataConstants .PLACE_OBJECT ;
6
7
import static fr .free .nrw .commons .wikidata .WikidataConstants .SELECTED_NEARBY_PLACE ;
7
8
import static fr .free .nrw .commons .wikidata .WikidataConstants .SELECTED_NEARBY_PLACE_CATEGORY ;
@@ -146,6 +147,23 @@ public class UploadActivity extends BaseActivity implements UploadContract.View,
146
147
*/
147
148
public static HashMap <Place ,Boolean > nearbyPopupAnswers ;
148
149
150
+ /**
151
+ * A private boolean variable to control whether a permissions dialog should be shown
152
+ * when necessary. Initially, it is set to `true`, indicating that the permissions dialog
153
+ * should be displayed if permissions are missing and it is first time calling
154
+ * `checkStoragePermissions` method.
155
+ *
156
+ * This variable is used in the `checkStoragePermissions` method to determine whether to
157
+ * show a permissions dialog to the user if the required permissions are not granted.
158
+ *
159
+ * If `showPermissionsDialog` is set to `true` and the necessary permissions are missing,
160
+ * a permissions dialog will be displayed to request the required permissions. If set
161
+ * to `false`, the dialog won't be shown.
162
+ *
163
+ * @see UploadActivity#checkStoragePermissions()
164
+ */
165
+ private boolean showPermissionsDialog = true ;
166
+
149
167
@ SuppressLint ("CheckResult" )
150
168
@ Override
151
169
protected void onCreate (Bundle savedInstanceState ) {
@@ -169,7 +187,6 @@ protected void onCreate(Bundle savedInstanceState) {
169
187
}
170
188
locationManager .requestLocationUpdatesFromProvider (LocationManager .GPS_PROVIDER );
171
189
locationManager .requestLocationUpdatesFromProvider (LocationManager .NETWORK_PROVIDER );
172
- checkStoragePermissions ();
173
190
}
174
191
175
192
private void init () {
@@ -227,6 +244,12 @@ public boolean isLoggedIn() {
227
244
return sessionManager .isUserLoggedIn ();
228
245
}
229
246
247
+ @ Override
248
+ protected void onStart () {
249
+ super .onStart ();
250
+ checkStoragePermissions ();
251
+ }
252
+
230
253
@ Override
231
254
protected void onResume () {
232
255
super .onResume ();
@@ -235,6 +258,7 @@ protected void onResume() {
235
258
askUserToLogIn ();
236
259
}
237
260
checkBlockStatus ();
261
+ checkStoragePermissions ();
238
262
}
239
263
240
264
/**
@@ -255,13 +279,36 @@ protected void checkBlockStatus() {
255
279
true )));
256
280
}
257
281
258
- private void checkStoragePermissions () {
282
+ public void checkStoragePermissions () {
283
+ // Check if all required permissions are granted
259
284
final boolean hasAllPermissions = PermissionUtils .hasPermission (this , PERMISSIONS_STORAGE );
260
285
if (hasAllPermissions ) {
286
+ // All required permissions are granted, so enable UI elements and perform actions
261
287
receiveSharedItems ();
262
- } else if (VERSION .SDK_INT >= VERSION_CODES .M ) {
263
- requestPermissions (PERMISSIONS_STORAGE , RequestCodes .STORAGE );
288
+ cvContainerTopCard .setVisibility (View .VISIBLE );
289
+ } else {
290
+ // Permissions are missing
291
+ cvContainerTopCard .setVisibility (View .INVISIBLE );
292
+ if (showPermissionsDialog ){
293
+ checkPermissionsAndPerformAction (this ,
294
+ () -> {
295
+ cvContainerTopCard .setVisibility (View .VISIBLE );
296
+ this .receiveSharedItems ();
297
+ },() -> {
298
+ this .showPermissionsDialog = true ;
299
+ this .checkStoragePermissions ();
300
+ },
301
+ R .string .storage_permission_title ,
302
+ R .string .write_storage_permission_rationale_for_image_share ,
303
+ PERMISSIONS_STORAGE );
304
+ }
264
305
}
306
+ /* If all permissions are not granted and a dialog is already showing on screen
307
+ showPermissionsDialog will set to false making it not show dialog again onResume,
308
+ but if user Denies any permission showPermissionsDialog will be to true
309
+ and permissions dialog will be shown again.
310
+ */
311
+ this .showPermissionsDialog = hasAllPermissions ;
265
312
}
266
313
267
314
@ Override
@@ -721,6 +768,25 @@ protected void onDestroy() {
721
768
}
722
769
}
723
770
771
+ /**
772
+ * Get the value of the showPermissionDialog variable.
773
+ *
774
+ * @return {@code true} if Permission Dialog should be shown, {@code false} otherwise.
775
+ */
776
+ public boolean isShowPermissionsDialog () {
777
+ return showPermissionsDialog ;
778
+ }
779
+
780
+ /**
781
+ * Set the value of the showPermissionDialog variable.
782
+ *
783
+ * @param showPermissionsDialog {@code true} to indicate to show
784
+ * Permissions Dialog if permissions are missing, {@code false} otherwise.
785
+ */
786
+ public void setShowPermissionsDialog (final boolean showPermissionsDialog ) {
787
+ this .showPermissionsDialog = showPermissionsDialog ;
788
+ }
789
+
724
790
/**
725
791
* Overrides the back button to make sure the user is prepared to lose their progress
726
792
*/
0 commit comments