Fix onRequestPermissionsResult not called in media fragments after requesting storage permission. Use download manager to download in MediaDownloaderImpl.

This commit is contained in:
Alex Ning 2020-08-26 15:10:03 +08:00
parent 2c0ed0834a
commit 331704e5ef
5 changed files with 34 additions and 28 deletions

View File

@ -21,7 +21,6 @@ import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider; import androidx.core.content.FileProvider;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
@ -166,15 +165,14 @@ public class ViewImgurImageFragment extends Fragment {
// Permission is not granted // Permission is not granted
// No explanation needed; request the permission // No explanation needed; request the permission
ActivityCompat.requestPermissions(activity, requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE); PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE);
} else { } else {
// Permission has already been granted // Permission has already been granted
mediaDownloader.download(imgurMedia.getLink(), imgurMedia.getFileName(), getContext()); download();
} }
} else { } else {
mediaDownloader.download(imgurMedia.getLink(), imgurMedia.getFileName(), getContext()); download();
} }
return true; return true;
@ -234,15 +232,21 @@ public class ViewImgurImageFragment extends Fragment {
return false; return false;
} }
private void download() {
isDownloading = false;
mediaDownloader.download(imgurMedia.getLink(), imgurMedia.getFileName(), getContext());
}
@Override @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE && grantResults.length > 0) { if (requestCode == PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE && grantResults.length > 0) {
if (grantResults[0] == PackageManager.PERMISSION_DENIED) { if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
Toast.makeText(activity, R.string.no_storage_permission, Toast.LENGTH_SHORT).show(); Toast.makeText(activity, R.string.no_storage_permission, Toast.LENGTH_SHORT).show();
} else if (grantResults[0] == PackageManager.PERMISSION_GRANTED && isDownloading) {
mediaDownloader.download(imgurMedia.getLink(), imgurMedia.getFileName(), getContext());
}
isDownloading = false; isDownloading = false;
} else if (grantResults[0] == PackageManager.PERMISSION_GRANTED && isDownloading) {
download();
}
} }
} }

View File

@ -21,7 +21,6 @@ import android.widget.LinearLayout;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
@ -160,8 +159,7 @@ public class ViewImgurVideoFragment extends Fragment {
// Permission is not granted // Permission is not granted
// No explanation needed; request the permission // No explanation needed; request the permission
ActivityCompat.requestPermissions(activity, requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE); PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE);
} else { } else {
// Permission has already been granted // 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 (requestCode == PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE && grantResults.length > 0) {
if (grantResults[0] == PackageManager.PERMISSION_DENIED) { if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
Toast.makeText(activity, R.string.no_storage_permission, Toast.LENGTH_SHORT).show(); Toast.makeText(activity, R.string.no_storage_permission, Toast.LENGTH_SHORT).show();
isDownloading = false;
} else if (grantResults[0] == PackageManager.PERMISSION_GRANTED && isDownloading) { } else if (grantResults[0] == PackageManager.PERMISSION_GRANTED && isDownloading) {
download(); download();
} }
isDownloading = false;
} }
} }

View File

@ -21,7 +21,6 @@ import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider; import androidx.core.content.FileProvider;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
@ -232,15 +231,14 @@ public class ViewRedditGalleryImageOrGifFragment extends Fragment {
// Permission is not granted // Permission is not granted
// No explanation needed; request the permission // No explanation needed; request the permission
ActivityCompat.requestPermissions(activity, requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE); PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE);
} else { } else {
// Permission has already been granted // Permission has already been granted
mediaDownloader.download(media.url, media.fileName, getContext()); download();
} }
} else { } else {
mediaDownloader.download(media.url, media.fileName, getContext()); download();
} }
return true; return true;
@ -267,6 +265,12 @@ public class ViewRedditGalleryImageOrGifFragment extends Fragment {
return false; return false;
} }
private void download() {
isDownloading = false;
mediaDownloader.download(media.url, media.fileName, getContext());
}
private void shareImage() { private void shareImage() {
glide.asBitmap().load(media.url).into(new CustomTarget<Bitmap>() { glide.asBitmap().load(media.url).into(new CustomTarget<Bitmap>() {
@Override @Override
@ -352,10 +356,10 @@ public class ViewRedditGalleryImageOrGifFragment extends Fragment {
if (requestCode == PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE && grantResults.length > 0) { if (requestCode == PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE && grantResults.length > 0) {
if (grantResults[0] == PackageManager.PERMISSION_DENIED) { if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
Toast.makeText(activity, R.string.no_storage_permission, Toast.LENGTH_SHORT).show(); Toast.makeText(activity, R.string.no_storage_permission, Toast.LENGTH_SHORT).show();
} else if (grantResults[0] == PackageManager.PERMISSION_GRANTED && isDownloading) {
mediaDownloader.download(media.url, media.fileName, getContext());
}
isDownloading = false; isDownloading = false;
} else if (grantResults[0] == PackageManager.PERMISSION_GRANTED && isDownloading) {
download();
}
} }
} }

View File

@ -21,7 +21,6 @@ import android.widget.LinearLayout;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
@ -160,8 +159,7 @@ public class ViewRedditGalleryVideoFragment extends Fragment {
// Permission is not granted // Permission is not granted
// No explanation needed; request the permission // No explanation needed; request the permission
ActivityCompat.requestPermissions(activity, requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE); PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE);
} else { } else {
// Permission has already been granted // 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 (requestCode == PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE && grantResults.length > 0) {
if (grantResults[0] == PackageManager.PERMISSION_DENIED) { if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
Toast.makeText(activity, R.string.no_storage_permission, Toast.LENGTH_SHORT).show(); Toast.makeText(activity, R.string.no_storage_permission, Toast.LENGTH_SHORT).show();
isDownloading = false;
} else if (grantResults[0] == PackageManager.PERMISSION_GRANTED && isDownloading) { } else if (grantResults[0] == PackageManager.PERMISSION_GRANTED && isDownloading) {
download(); download();
} }
isDownloading = false;
} }
} }

View File

@ -1,17 +1,19 @@
package ml.docilealligator.infinityforreddit; package ml.docilealligator.infinityforreddit;
import android.app.DownloadManager;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Environment;
import android.widget.Toast; import android.widget.Toast;
import ml.docilealligator.infinityforreddit.Service.DownloadVideoService; import java.io.File;
public class MediaDownloaderImpl implements MediaDownloader { public class MediaDownloaderImpl implements MediaDownloader {
@Override @Override
public void download(String url, String fileName, Context ctx) { 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.setTitle(fileName);
request.allowScanningByMediaScanner(); request.allowScanningByMediaScanner();
@ -51,10 +53,10 @@ public class MediaDownloaderImpl implements MediaDownloader {
} }
manager.enqueue(request); 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_VIDEO_URL, url);
intent.putExtra(DownloadVideoService.EXTRA_FILE_NAME, fileName); intent.putExtra(DownloadVideoService.EXTRA_FILE_NAME, fileName);
intent.putExtra(DownloadVideoService.EXTRA_IS_REDDIT_VIDEO, false); intent.putExtra(DownloadVideoService.EXTRA_IS_REDDIT_VIDEO, false);
@ -64,6 +66,6 @@ public class MediaDownloaderImpl implements MediaDownloader {
} else { } else {
ctx.startService(intent); 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();*/
} }
} }