Support download location in ViewImgurImageFragment and ViewRedditGalleryImageOrGifFragment.

This commit is contained in:
Alex Ning 2020-09-06 12:19:08 +08:00
parent cefc21fd1b
commit cb0d662ed3
4 changed files with 66 additions and 12 deletions

View File

@ -1,5 +1,6 @@
package ml.docilealligator.infinityforreddit.Activity; package ml.docilealligator.infinityforreddit.Activity;
import android.Manifest;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
@ -21,6 +22,8 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider; import androidx.core.content.FileProvider;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
@ -277,16 +280,28 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa
finish(); finish();
return true; return true;
case R.id.action_download_view_image_or_gif_activity: case R.id.action_download_view_image_or_gif_activity:
Intent intent = new Intent(this, DownloadImageService.class); if (isDownloading) {
intent.putExtra(DownloadImageService.EXTRA_URL, mImageUrl); return false;
intent.putExtra(DownloadImageService.EXTRA_IS_GIF, isGif); }
intent.putExtra(DownloadImageService.EXTRA_FILE_NAME, mImageFileName);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { isDownloading = true;
startForegroundService(intent);
} else { if (Build.VERSION.SDK_INT >= 23) {
startService(intent); if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
// No explanation needed; request the permission
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE);
} else {
// Permission has already been granted
download();
}
} else {
download();
} }
Toast.makeText(this, R.string.download_started, Toast.LENGTH_SHORT).show();
return true; return true;
case R.id.action_share_view_image_or_gif_activity: case R.id.action_share_view_image_or_gif_activity:
@ -321,6 +336,21 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa
return false; return false;
} }
private void download() {
isDownloading = false;
Intent intent = new Intent(this, DownloadImageService.class);
intent.putExtra(DownloadImageService.EXTRA_URL, mImageUrl);
intent.putExtra(DownloadImageService.EXTRA_IS_GIF, isGif);
intent.putExtra(DownloadImageService.EXTRA_FILE_NAME, mImageFileName);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent);
} else {
startService(intent);
}
Toast.makeText(this, R.string.download_started, Toast.LENGTH_SHORT).show();
}
private void shareImage() { private void shareImage() {
glide.asBitmap().load(mImageUrl).into(new CustomTarget<Bitmap>() { glide.asBitmap().load(mImageUrl).into(new CustomTarget<Bitmap>() {

View File

@ -374,6 +374,10 @@ public class ViewVideoActivity extends AppCompatActivity {
finish(); finish();
return true; return true;
case R.id.action_download_view_video_activity: case R.id.action_download_view_video_activity:
if (isDownloading) {
return false;
}
if (videoDownloadUrl == null) { if (videoDownloadUrl == null) {
Toast.makeText(this, R.string.fetching_video_info_please_wait, Toast.LENGTH_SHORT).show(); Toast.makeText(this, R.string.fetching_video_info_please_wait, Toast.LENGTH_SHORT).show();
return true; return true;

View File

@ -48,6 +48,7 @@ import ml.docilealligator.infinityforreddit.ImgurMedia;
import ml.docilealligator.infinityforreddit.MediaDownloader; import ml.docilealligator.infinityforreddit.MediaDownloader;
import ml.docilealligator.infinityforreddit.MediaDownloaderImpl; import ml.docilealligator.infinityforreddit.MediaDownloaderImpl;
import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.Service.DownloadImageService;
import ml.docilealligator.infinityforreddit.SetAsWallpaperCallback; import ml.docilealligator.infinityforreddit.SetAsWallpaperCallback;
public class ViewImgurImageFragment extends Fragment { public class ViewImgurImageFragment extends Fragment {
@ -235,7 +236,16 @@ public class ViewImgurImageFragment extends Fragment {
private void download() { private void download() {
isDownloading = false; isDownloading = false;
mediaDownloader.download(imgurMedia.getLink(), imgurMedia.getFileName(), getContext()); Intent intent = new Intent(activity, DownloadImageService.class);
intent.putExtra(DownloadImageService.EXTRA_URL, imgurMedia.getLink());
intent.putExtra(DownloadImageService.EXTRA_IS_GIF, false);
intent.putExtra(DownloadImageService.EXTRA_FILE_NAME, imgurMedia.getFileName());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
activity.startForegroundService(intent);
} else {
activity.startService(intent);
}
Toast.makeText(activity, R.string.download_started, Toast.LENGTH_SHORT).show();
} }
@Override @Override

View File

@ -46,14 +46,15 @@ import java.io.File;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import ml.docilealligator.infinityforreddit.Activity.ViewRedditGalleryActivity; import ml.docilealligator.infinityforreddit.Activity.ViewRedditGalleryActivity;
import ml.docilealligator.infinityforreddit.AsyncTask.SaveGIFToFileAsyncTask;
import ml.docilealligator.infinityforreddit.AsyncTask.SaveBitmapImageToFileAsyncTask; import ml.docilealligator.infinityforreddit.AsyncTask.SaveBitmapImageToFileAsyncTask;
import ml.docilealligator.infinityforreddit.AsyncTask.SaveGIFToFileAsyncTask;
import ml.docilealligator.infinityforreddit.BottomSheetFragment.SetAsWallpaperBottomSheetFragment; import ml.docilealligator.infinityforreddit.BottomSheetFragment.SetAsWallpaperBottomSheetFragment;
import ml.docilealligator.infinityforreddit.BuildConfig; import ml.docilealligator.infinityforreddit.BuildConfig;
import ml.docilealligator.infinityforreddit.MediaDownloader; import ml.docilealligator.infinityforreddit.MediaDownloader;
import ml.docilealligator.infinityforreddit.MediaDownloaderImpl; import ml.docilealligator.infinityforreddit.MediaDownloaderImpl;
import ml.docilealligator.infinityforreddit.Post.Post; import ml.docilealligator.infinityforreddit.Post.Post;
import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.Service.DownloadImageService;
import ml.docilealligator.infinityforreddit.SetAsWallpaperCallback; import ml.docilealligator.infinityforreddit.SetAsWallpaperCallback;
public class ViewRedditGalleryImageOrGifFragment extends Fragment { public class ViewRedditGalleryImageOrGifFragment extends Fragment {
@ -268,7 +269,16 @@ public class ViewRedditGalleryImageOrGifFragment extends Fragment {
private void download() { private void download() {
isDownloading = false; isDownloading = false;
mediaDownloader.download(media.url, media.fileName, getContext()); Intent intent = new Intent(activity, DownloadImageService.class);
intent.putExtra(DownloadImageService.EXTRA_URL, media.url);
intent.putExtra(DownloadImageService.EXTRA_IS_GIF, media.mediaType == Post.Gallery.TYPE_GIF);
intent.putExtra(DownloadImageService.EXTRA_FILE_NAME, media.fileName);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
activity.startForegroundService(intent);
} else {
activity.startService(intent);
}
Toast.makeText(activity, R.string.download_started, Toast.LENGTH_SHORT).show();
} }
private void shareImage() { private void shareImage() {