From 2c2c8fa991a4ac43e648c62fd2c2febd83eba790 Mon Sep 17 00:00:00 2001 From: Konrad Pozniak Date: Sun, 21 Apr 2019 22:59:49 +0200 Subject: [PATCH] fix downloading media on Android 5 (#1206) --- .../com/keylesspalace/tusky/BaseActivity.java | 52 +++++++++---------- .../keylesspalace/tusky/ViewMediaActivity.kt | 3 +- .../tusky/fragment/SFragment.java | 6 +-- 3 files changed, 27 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java b/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java index 7d9860ea..7f05f339 100644 --- a/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java @@ -25,7 +25,6 @@ import android.graphics.BitmapFactory; import android.graphics.Color; import android.graphics.PorterDuff; import android.graphics.drawable.Drawable; -import android.os.Build; import android.os.Bundle; import android.preference.PreferenceManager; import android.util.Log; @@ -48,6 +47,7 @@ import java.util.List; import javax.inject.Inject; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.StringRes; import androidx.appcompat.app.AlertDialog; @@ -65,7 +65,6 @@ public abstract class BaseActivity extends AppCompatActivity implements Injectab @Inject public AccountManager accountManager; - protected static final int BUILD_VERSION_ANY = -1; private static final int REQUESTER_NONE = Integer.MAX_VALUE; private HashMap requesters; @@ -227,42 +226,41 @@ public abstract class BaseActivity extends AppCompatActivity implements Injectab adapter.addAll(accounts); new AlertDialog.Builder(this) - .setTitle(dialogTitle) - .setAdapter(adapter, (dialogInterface, index) -> listener.onAccountSelected(accounts.get(index))) - .show(); + .setTitle(dialogTitle) + .setAdapter(adapter, (dialogInterface, index) -> listener.onAccountSelected(accounts.get(index))) + .show(); } @Override - public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requesters.containsKey(requestCode)) { PermissionRequester requester = requesters.remove(requestCode); requester.onRequestPermissionsResult(permissions, grantResults); } } - public void requestPermissions(String[] permissions, int minimumBuildVersion, PermissionRequester requester) { - if (minimumBuildVersion == BUILD_VERSION_ANY || Build.VERSION.SDK_INT >= minimumBuildVersion) { - ArrayList permissionsToRequest = new ArrayList<>(); - for(String permission: permissions) { - if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) { - permissionsToRequest.add(permission); - } - } - if (permissionsToRequest.isEmpty()) { - int[] permissionsAlreadyGranted = new int[permissions.length]; - for (int i = 0; i < permissionsAlreadyGranted.length; ++i) - permissionsAlreadyGranted[i] = PackageManager.PERMISSION_GRANTED; - requester.onRequestPermissionsResult(permissions, permissionsAlreadyGranted); - return; + public void requestPermissions(String[] permissions, PermissionRequester requester) { + ArrayList permissionsToRequest = new ArrayList<>(); + for(String permission: permissions) { + if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) { + permissionsToRequest.add(permission); } + } + if (permissionsToRequest.isEmpty()) { + int[] permissionsAlreadyGranted = new int[permissions.length]; + for (int i = 0; i < permissionsAlreadyGranted.length; ++i) + permissionsAlreadyGranted[i] = PackageManager.PERMISSION_GRANTED; + requester.onRequestPermissionsResult(permissions, permissionsAlreadyGranted); + return; + } - int newKey = requester == null ? REQUESTER_NONE : requesters.size(); - if (newKey != REQUESTER_NONE) { - requesters.put(newKey, requester); - } - String[] permissionsCopy = new String[permissionsToRequest.size()]; - permissionsToRequest.toArray(permissionsCopy); - ActivityCompat.requestPermissions(this, permissionsCopy, newKey); + int newKey = requester == null ? REQUESTER_NONE : requesters.size(); + if (newKey != REQUESTER_NONE) { + requesters.put(newKey, requester); } + String[] permissionsCopy = new String[permissionsToRequest.size()]; + permissionsToRequest.toArray(permissionsCopy); + ActivityCompat.requestPermissions(this, permissionsCopy, newKey); + } } diff --git a/app/src/main/java/com/keylesspalace/tusky/ViewMediaActivity.kt b/app/src/main/java/com/keylesspalace/tusky/ViewMediaActivity.kt index 6b95bfef..93b69e0a 100644 --- a/app/src/main/java/com/keylesspalace/tusky/ViewMediaActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/ViewMediaActivity.kt @@ -27,7 +27,6 @@ import android.content.pm.PackageManager import android.graphics.Bitmap import android.graphics.Color import android.net.Uri -import android.os.Build import android.os.Bundle import android.os.Environment import androidx.core.content.FileProvider @@ -217,7 +216,7 @@ class ViewMediaActivity : BaseActivity(), ViewImageFragment.PhotoActionsListener } private fun requestDownloadMedia() { - requestPermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), Build.VERSION_CODES.M) { _, grantResults -> + requestPermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { _, grantResults -> if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { downloadMedia() } else { diff --git a/app/src/main/java/com/keylesspalace/tusky/fragment/SFragment.java b/app/src/main/java/com/keylesspalace/tusky/fragment/SFragment.java index b52f21e2..a435e32f 100644 --- a/app/src/main/java/com/keylesspalace/tusky/fragment/SFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/fragment/SFragment.java @@ -23,7 +23,6 @@ import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; -import android.os.Build; import android.os.Environment; import android.text.SpannableStringBuilder; import android.text.Spanned; @@ -77,7 +76,6 @@ public abstract class SFragment extends BaseFragment implements Injectable { protected abstract void onReblog(final boolean reblog, final int position); private BottomSheetActivity bottomSheetActivity; - private Status pendingDownloadStatus; @Inject public MastodonApi mastodonApi; @@ -403,7 +401,6 @@ public abstract class SFragment extends BaseFragment implements Injectable { } private void downloadAllMedia(Status status) { - pendingDownloadStatus = null; Toast.makeText(getContext(), R.string.downloading_media, Toast.LENGTH_SHORT).show(); for(Attachment attachment: status.getAttachments()) { String url = attachment.getUrl(); @@ -418,9 +415,8 @@ public abstract class SFragment extends BaseFragment implements Injectable { } private void requestDownloadAllMedia(Status status) { - pendingDownloadStatus = status; String[] permissions = new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE }; - ((BaseActivity)getActivity()).requestPermissions(permissions, Build.VERSION_CODES.M, (permissions1, grantResults) -> { + ((BaseActivity)getActivity()).requestPermissions(permissions, (permissions1, grantResults) -> { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { downloadAllMedia(status); } else {