From 64652b987d686bc1d9ed56d3c59bfdee2ffedde7 Mon Sep 17 00:00:00 2001
From: Srishti Rohatgi <53987325+srishti-R@users.noreply.github.com>
Date: Wed, 18 Oct 2023 08:46:33 +0530
Subject: [PATCH] fixes blank screen decrepancy when sharing items from
external apps (#5345)
* fixes blank screen decrepancy when sharing items from external apps
* reverts comment removal
---
.../nrw/commons/filepicker/Constants.java | 1 +
.../nrw/commons/upload/UploadActivity.java | 62 ++++++++++++++-----
app/src/main/res/values/strings.xml | 2 +
3 files changed, 50 insertions(+), 15 deletions(-)
diff --git a/app/src/main/java/fr/free/nrw/commons/filepicker/Constants.java b/app/src/main/java/fr/free/nrw/commons/filepicker/Constants.java
index cc04e39561..3c9299c1aa 100644
--- a/app/src/main/java/fr/free/nrw/commons/filepicker/Constants.java
+++ b/app/src/main/java/fr/free/nrw/commons/filepicker/Constants.java
@@ -8,6 +8,7 @@ public interface Constants {
*/
interface RequestCodes {
int LOCATION = 1;
+ int STORAGE = 2;
int FILE_PICKER_IMAGE_IDENTIFICATOR = 0b1101101100; //876
int SOURCE_CHOOSER = 1 << 15;
diff --git a/app/src/main/java/fr/free/nrw/commons/upload/UploadActivity.java b/app/src/main/java/fr/free/nrw/commons/upload/UploadActivity.java
index 69c67e15e8..06706df8a8 100644
--- a/app/src/main/java/fr/free/nrw/commons/upload/UploadActivity.java
+++ b/app/src/main/java/fr/free/nrw/commons/upload/UploadActivity.java
@@ -10,9 +10,12 @@
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
+import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Build;
+import android.os.Build.VERSION;
+import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.provider.Settings;
import android.util.DisplayMetrics;
@@ -21,6 +24,7 @@
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
+import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
@@ -38,6 +42,7 @@
import fr.free.nrw.commons.auth.LoginActivity;
import fr.free.nrw.commons.auth.SessionManager;
import fr.free.nrw.commons.contributions.ContributionController;
+import fr.free.nrw.commons.filepicker.Constants.RequestCodes;
import fr.free.nrw.commons.filepicker.UploadableFile;
import fr.free.nrw.commons.kvstore.JsonKvStore;
import fr.free.nrw.commons.location.LatLng;
@@ -59,7 +64,6 @@
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;
-import java.security.Permission;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@@ -150,11 +154,6 @@ protected void onCreate(Bundle savedInstanceState) {
compositeDisposable = new CompositeDisposable();
init();
nearbyPopupAnswers = new HashMap<>();
- PermissionUtils.checkPermissionsAndPerformAction(this,
- this::receiveSharedItems,
- R.string.storage_permission_title,
- R.string.write_storage_permission_rationale_for_image_share,
- PERMISSIONS_STORAGE);
//getting the current dpi of the device and if it is less than 320dp i.e. overlapping
//threshold, thumbnails automatically minimizes
DisplayMetrics metrics = getResources().getDisplayMetrics();
@@ -167,6 +166,7 @@ protected void onCreate(Bundle savedInstanceState) {
}
locationManager.requestLocationUpdatesFromProvider(LocationManager.GPS_PROVIDER);
locationManager.requestLocationUpdatesFromProvider(LocationManager.NETWORK_PROVIDER);
+ checkStoragePermissions();
}
private void init() {
@@ -232,7 +232,6 @@ protected void onResume() {
askUserToLogIn();
}
checkBlockStatus();
- checkStoragePermissions();
}
/**
@@ -255,14 +254,10 @@ protected void checkBlockStatus() {
private void checkStoragePermissions() {
final boolean hasAllPermissions = PermissionUtils.hasPermission(this, PERMISSIONS_STORAGE);
- if (!hasAllPermissions) {
- PermissionUtils.checkPermissionsAndPerformAction(this,
- () -> {
- //TODO handle this
- },
- R.string.storage_permission_title,
- R.string.write_storage_permission_rationale_for_image_share,
- PERMISSIONS_STORAGE);
+ if (hasAllPermissions) {
+ receiveSharedItems();
+ } else if (VERSION.SDK_INT >= VERSION_CODES.M) {
+ requestPermissions(PERMISSIONS_STORAGE, RequestCodes.STORAGE);
}
}
@@ -350,7 +345,44 @@ public void askUserToLogIn() {
startActivity(loginIntent);
}
+ @Override
+ public void onRequestPermissionsResult(final int requestCode,
+ @NonNull final String[] permissions,
+ @NonNull final int[] grantResults) {
+ boolean areAllGranted = false;
+ if (requestCode == RequestCodes.STORAGE) {
+ if (VERSION.SDK_INT >= VERSION_CODES.M) {
+ for (int i = 0; i < grantResults.length; i++) {
+ String permission = permissions[i];
+ areAllGranted = grantResults[i] == PackageManager.PERMISSION_GRANTED;
+ if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
+ boolean showRationale = shouldShowRequestPermissionRationale(permission);
+ if (!showRationale) {
+ DialogUtil.showAlertDialog(this,
+ getString(R.string.storage_permissions_denied),
+ getString(R.string.unable_to_share_upload_item),
+ getString(android.R.string.ok),
+ this::finish,
+ false);
+ } else {
+ DialogUtil.showAlertDialog(this,
+ getString(R.string.storage_permission_title),
+ getString(
+ R.string.write_storage_permission_rationale_for_image_share),
+ getString(android.R.string.ok),
+ this::checkStoragePermissions,
+ false);
+ }
+ }
+ }
+ if (areAllGranted) {
+ receiveSharedItems();
+ }
+ }
+ }
+ super.onRequestPermissionsResult(requestCode, permissions, grantResults);
+ }
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 0a7ef24992..85bb19bdc5 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -775,6 +775,8 @@ Upload your first media by tapping on the add button.
Swipe fast and long to perform these actions: \n- Left/Right: Go to previous/next \n- Up: Select\n- Down: Mark as not for upload.
To set up your leaderboard avatar, tap \"Set as avatar\" in the three-dots menu of any image.
The coordinates are not the exact coordinates, but the person who uploaded this picture thinks they are close enough.
+ Storage Permissions Denied
+ Unable to share this item
- %d image selected
- %d images selected