From 331704e5ef7c7f4d2014799776e51fde75e1608c Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Wed, 26 Aug 2020 15:10:03 +0800 Subject: [PATCH] Fix onRequestPermissionsResult not called in media fragments after requesting storage permission. Use download manager to download in MediaDownloaderImpl. --- .../Fragment/ViewImgurImageFragment.java | 18 +++++++++++------- .../Fragment/ViewImgurVideoFragment.java | 6 ++---- .../ViewRedditGalleryImageOrGifFragment.java | 18 +++++++++++------- .../ViewRedditGalleryVideoFragment.java | 6 ++---- .../infinityforreddit/MediaDownloaderImpl.java | 14 ++++++++------ 5 files changed, 34 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurImageFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurImageFragment.java index d25408aa..b29ccf2d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurImageFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurImageFragment.java @@ -21,7 +21,6 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import androidx.core.content.FileProvider; import androidx.fragment.app.Fragment; @@ -166,15 +165,14 @@ public class ViewImgurImageFragment extends Fragment { // Permission is not granted // No explanation needed; request the permission - ActivityCompat.requestPermissions(activity, - new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, + requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE); } else { // Permission has already been granted - mediaDownloader.download(imgurMedia.getLink(), imgurMedia.getFileName(), getContext()); + download(); } } else { - mediaDownloader.download(imgurMedia.getLink(), imgurMedia.getFileName(), getContext()); + download(); } return true; @@ -234,15 +232,21 @@ public class ViewImgurImageFragment extends Fragment { return false; } + private void download() { + isDownloading = false; + + mediaDownloader.download(imgurMedia.getLink(), imgurMedia.getFileName(), getContext()); + } + @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE && grantResults.length > 0) { if (grantResults[0] == PackageManager.PERMISSION_DENIED) { Toast.makeText(activity, R.string.no_storage_permission, Toast.LENGTH_SHORT).show(); + isDownloading = false; } else if (grantResults[0] == PackageManager.PERMISSION_GRANTED && isDownloading) { - mediaDownloader.download(imgurMedia.getLink(), imgurMedia.getFileName(), getContext()); + download(); } - isDownloading = false; } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurVideoFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurVideoFragment.java index 30a00c21..1e968f57 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurVideoFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurVideoFragment.java @@ -21,7 +21,6 @@ import android.widget.LinearLayout; import android.widget.Toast; import androidx.annotation.NonNull; -import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; @@ -160,8 +159,7 @@ public class ViewImgurVideoFragment extends Fragment { // Permission is not granted // No explanation needed; request the permission - ActivityCompat.requestPermissions(activity, - new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, + requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE); } else { // Permission has already been granted @@ -180,10 +178,10 @@ public class ViewImgurVideoFragment extends Fragment { if (requestCode == PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE && grantResults.length > 0) { if (grantResults[0] == PackageManager.PERMISSION_DENIED) { Toast.makeText(activity, R.string.no_storage_permission, Toast.LENGTH_SHORT).show(); + isDownloading = false; } else if (grantResults[0] == PackageManager.PERMISSION_GRANTED && isDownloading) { download(); } - isDownloading = false; } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewRedditGalleryImageOrGifFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewRedditGalleryImageOrGifFragment.java index 97c60653..a3674051 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewRedditGalleryImageOrGifFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewRedditGalleryImageOrGifFragment.java @@ -21,7 +21,6 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import androidx.core.content.FileProvider; import androidx.fragment.app.Fragment; @@ -232,15 +231,14 @@ public class ViewRedditGalleryImageOrGifFragment extends Fragment { // Permission is not granted // No explanation needed; request the permission - ActivityCompat.requestPermissions(activity, - new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, + requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE); } else { // Permission has already been granted - mediaDownloader.download(media.url, media.fileName, getContext()); + download(); } } else { - mediaDownloader.download(media.url, media.fileName, getContext()); + download(); } return true; @@ -267,6 +265,12 @@ public class ViewRedditGalleryImageOrGifFragment extends Fragment { return false; } + private void download() { + isDownloading = false; + + mediaDownloader.download(media.url, media.fileName, getContext()); + } + private void shareImage() { glide.asBitmap().load(media.url).into(new CustomTarget() { @Override @@ -352,10 +356,10 @@ public class ViewRedditGalleryImageOrGifFragment extends Fragment { if (requestCode == PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE && grantResults.length > 0) { if (grantResults[0] == PackageManager.PERMISSION_DENIED) { Toast.makeText(activity, R.string.no_storage_permission, Toast.LENGTH_SHORT).show(); + isDownloading = false; } else if (grantResults[0] == PackageManager.PERMISSION_GRANTED && isDownloading) { - mediaDownloader.download(media.url, media.fileName, getContext()); + download(); } - isDownloading = false; } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewRedditGalleryVideoFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewRedditGalleryVideoFragment.java index 2a0ec80f..34962aab 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewRedditGalleryVideoFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewRedditGalleryVideoFragment.java @@ -21,7 +21,6 @@ import android.widget.LinearLayout; import android.widget.Toast; import androidx.annotation.NonNull; -import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; @@ -160,8 +159,7 @@ public class ViewRedditGalleryVideoFragment extends Fragment { // Permission is not granted // No explanation needed; request the permission - ActivityCompat.requestPermissions(activity, - new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, + requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE); } else { // Permission has already been granted @@ -180,10 +178,10 @@ public class ViewRedditGalleryVideoFragment extends Fragment { if (requestCode == PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE && grantResults.length > 0) { if (grantResults[0] == PackageManager.PERMISSION_DENIED) { Toast.makeText(activity, R.string.no_storage_permission, Toast.LENGTH_SHORT).show(); + isDownloading = false; } else if (grantResults[0] == PackageManager.PERMISSION_GRANTED && isDownloading) { download(); } - isDownloading = false; } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MediaDownloaderImpl.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MediaDownloaderImpl.java index 79002741..fd802284 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MediaDownloaderImpl.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MediaDownloaderImpl.java @@ -1,17 +1,19 @@ package ml.docilealligator.infinityforreddit; +import android.app.DownloadManager; import android.content.Context; -import android.content.Intent; +import android.net.Uri; import android.os.Build; +import android.os.Environment; import android.widget.Toast; -import ml.docilealligator.infinityforreddit.Service.DownloadVideoService; +import java.io.File; public class MediaDownloaderImpl implements MediaDownloader { @Override public void download(String url, String fileName, Context ctx) { - /*DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); + DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); request.setTitle(fileName); request.allowScanningByMediaScanner(); @@ -51,10 +53,10 @@ public class MediaDownloaderImpl implements MediaDownloader { } manager.enqueue(request); - Toast.makeText(ctx, R.string.download_started, Toast.LENGTH_SHORT).show();*/ + Toast.makeText(ctx, R.string.download_started, Toast.LENGTH_SHORT).show(); - Intent intent = new Intent(ctx, DownloadVideoService.class); + /*Intent intent = new Intent(ctx, DownloadVideoService.class); intent.putExtra(DownloadVideoService.EXTRA_VIDEO_URL, url); intent.putExtra(DownloadVideoService.EXTRA_FILE_NAME, fileName); intent.putExtra(DownloadVideoService.EXTRA_IS_REDDIT_VIDEO, false); @@ -64,6 +66,6 @@ public class MediaDownloaderImpl implements MediaDownloader { } else { ctx.startService(intent); } - Toast.makeText(ctx, R.string.download_started, Toast.LENGTH_SHORT).show(); + Toast.makeText(ctx, R.string.download_started, Toast.LENGTH_SHORT).show();*/ } }