Use download manager to download images. Change image and video download location to Pictures for Android version >= Q. Fix dark theme in ViewMessageActivity.

This commit is contained in:
Alex Ning 2019-09-12 16:42:33 +08:00
parent a5fcbdec7a
commit 64390d4a5b
7 changed files with 66 additions and 126 deletions

View File

@ -433,7 +433,7 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie
intent.putExtra(ViewVideoActivity.IS_DOWNLOADABLE_KEY, mPost.isDownloadableGifOrVideo());
if(mPost.isDownloadableGifOrVideo()) {
intent.putExtra(ViewVideoActivity.DOWNLOAD_URL_KEY, mPost.getGifOrVideoDownloadUrl());
intent.putExtra(ViewVideoActivity.SUBREDDIT_KEY, mPost.getSubredditNamePrefixed());
intent.putExtra(ViewVideoActivity.SUBREDDIT_KEY, mPost.getSubredditName());
intent.putExtra(ViewVideoActivity.ID_KEY, mPost.getId());
}
mActivity.startActivity(intent);
@ -451,7 +451,7 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie
intent.putExtra(ViewVideoActivity.IS_DOWNLOADABLE_KEY, mPost.isDownloadableGifOrVideo());
if(mPost.isDownloadableGifOrVideo()) {
intent.putExtra(ViewVideoActivity.DOWNLOAD_URL_KEY, mPost.getGifOrVideoDownloadUrl());
intent.putExtra(ViewVideoActivity.SUBREDDIT_KEY, mPost.getSubredditNamePrefixed());
intent.putExtra(ViewVideoActivity.SUBREDDIT_KEY, mPost.getSubredditName());
intent.putExtra(ViewVideoActivity.ID_KEY, mPost.getId());
}
mActivity.startActivity(intent);

View File

@ -112,6 +112,10 @@ class Message {
return isNew;
}
public void setNew(boolean isNew) {
this.isNew = isNew;
}
public int getScore() {
return score;
}

View File

@ -127,7 +127,8 @@ class MessageRecyclerViewAdapter extends PagedListAdapter<Message, RecyclerView.
}
if(message.isNew()) {
((DataViewHolder) holder).itemView.setBackgroundColor(mResources.getColor(android.R.color.white));
((DataViewHolder) holder).itemView.setBackgroundColor(mResources.getColor(R.color.backgroundColor));
message.setNew(false);
ReadMessage.readMessage(mOauthRetrofit, mAccessToken, message.getFullname(),
new ReadMessage.ReadMessageListener() {
@ -136,6 +137,7 @@ class MessageRecyclerViewAdapter extends PagedListAdapter<Message, RecyclerView.
@Override
public void readFailed() {
message.setNew(true);
((DataViewHolder) holder).itemView.setBackgroundColor(mResources.getColor(R.color.unreadMessageBackgroundColor));
}
});

View File

@ -423,7 +423,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
intent.putExtra(ViewVideoActivity.IS_DOWNLOADABLE_KEY, post.isDownloadableGifOrVideo());
if(post.isDownloadableGifOrVideo()) {
intent.putExtra(ViewVideoActivity.DOWNLOAD_URL_KEY, post.getGifOrVideoDownloadUrl());
intent.putExtra(ViewVideoActivity.SUBREDDIT_KEY, subredditNamePrefixed);
intent.putExtra(ViewVideoActivity.SUBREDDIT_KEY, subredditName);
intent.putExtra(ViewVideoActivity.ID_KEY, id);
}
mContext.startActivity(intent);
@ -441,7 +441,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
intent.putExtra(ViewVideoActivity.IS_DOWNLOADABLE_KEY, post.isDownloadableGifOrVideo());
if(post.isDownloadableGifOrVideo()) {
intent.putExtra(ViewVideoActivity.DOWNLOAD_URL_KEY, post.getGifOrVideoDownloadUrl());
intent.putExtra(ViewVideoActivity.SUBREDDIT_KEY, subredditNamePrefixed);
intent.putExtra(ViewVideoActivity.SUBREDDIT_KEY, subredditName);
intent.putExtra(ViewVideoActivity.ID_KEY, id);
}
mContext.startActivity(intent);

View File

@ -4,22 +4,17 @@ import android.Manifest;
import android.animation.Animator;
import android.animation.ArgbEvaluator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.app.DownloadManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.PorterDuff;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.ParcelFileDescriptor;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
@ -44,16 +39,11 @@ import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.github.pwittchen.swipe.library.rx2.SimpleSwipeListener;
import com.github.pwittchen.swipe.library.rx2.Swipe;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import butterknife.BindView;
import butterknife.ButterKnife;
@ -99,8 +89,8 @@ public class ViewImageActivity extends AppCompatActivity {
setTitle("");
Intent intent = getIntent();
mImageUrl = intent.getExtras().getString(IMAGE_URL_KEY);
mImageFileName = intent.getExtras().getString(FILE_NAME_KEY);
mImageUrl = intent.getStringExtra(IMAGE_URL_KEY);
mImageFileName = intent.getStringExtra(FILE_NAME_KEY) + ".jpg";
mLoadErrorLinearLayout.setOnClickListener(view -> {
if(!isSwiping) {
@ -402,109 +392,47 @@ public class ViewImageActivity extends AppCompatActivity {
}
private void saveImage() {
Glide.with(this)
.asBitmap()
.load(mImageUrl)
.into(new CustomTarget<Bitmap>() {
@SuppressLint("StaticFieldLeak")
@Override
public void onResourceReady(@NonNull final Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
new AsyncTask<Void, Void, Void>() {
private boolean saveSuccess = true;
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(mImageUrl));
request.setTitle(mImageFileName);
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
isDownloading = false;
if(saveSuccess) {
Toast.makeText(ViewImageActivity.this, R.string.download_completed, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(ViewImageActivity.this, R.string.download_failed, Toast.LENGTH_SHORT).show();
}
}
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
@Override
protected Void doInBackground(Void... params) {
try {
//Android Q support
if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DISPLAY_NAME, mImageFileName + ".jpg");
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.Images.Media.IS_PENDING, 1);
ContentResolver resolver = getContentResolver();
Uri collection = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
Uri item = resolver.insert(collection, values);
if(item == null) {
saveSuccess = false;
return null;
}
try (ParcelFileDescriptor pfd = resolver.openFileDescriptor(item, "w", null)) {
if(pfd == null) {
saveSuccess = false;
return null;
}
FileOutputStream outputStream = new FileOutputStream(pfd.getFileDescriptor());
resource.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.flush();
outputStream.close();
} catch (IOException e) {
saveSuccess = false;
e.printStackTrace();
}
values.clear();
values.put(MediaStore.Images.Media.IS_PENDING, 0);
resolver.update(item, values, null, null);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, mImageFileName);
} else {
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
File directory = new File(path + "/Infinity/");
boolean saveToInfinityFolder = true;
if(!directory.exists()) {
if(!directory.mkdir()) {
saveSuccess = false;
return null;
saveToInfinityFolder = false;
}
} else {
if(directory.isFile()) {
if(!directory.delete() && !directory.mkdir()) {
saveSuccess = false;
return null;
if(!(directory.delete() && directory.mkdir())) {
saveToInfinityFolder = false;
}
}
}
File file = new File(path + "/Infinity/", mImageFileName + ".jpg");
int postfix = 1;
while(file.exists()) {
file = new File(path + "/Infinity/", mImageFileName + "-" + (postfix++) + ".jpg");
if(saveToInfinityFolder) {
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES + "/Infinity/", mImageFileName);
} else {
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, mImageFileName);
}
OutputStream outputStream = new FileOutputStream(file);
resource.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.flush();
outputStream.close();
}
} catch (IOException e) {
saveSuccess = false;
e.printStackTrace();
}
return null;
}
}.execute();
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
if(manager == null) {
Toast.makeText(this, R.string.download_failed, Toast.LENGTH_SHORT).show();
return;
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
manager.enqueue(request);
Toast.makeText(this, R.string.download_started, Toast.LENGTH_SHORT).show();
}
@Override

View File

@ -116,9 +116,9 @@ public class ViewVideoActivity extends AppCompatActivity {
mIsHLSVideo = intent.getExtras().getBoolean(IS_HLS_VIDEO_KEY);
if(intent.getExtras().getBoolean(IS_DOWNLOADABLE_KEY)) {
mGifOrVideoFileName = intent.getExtras().getString(SUBREDDIT_KEY).substring(2)
+ "-" + intent.getExtras().getString(ID_KEY).substring(3) + ".gif";
mDownloadUrl = intent.getExtras().getString(DOWNLOAD_URL_KEY);
mGifOrVideoFileName = intent.getStringExtra(SUBREDDIT_KEY)
+ "-" + intent.getStringExtra(ID_KEY).substring(3) + ".gif";
mDownloadUrl = intent.getStringExtra(DOWNLOAD_URL_KEY);
}
final float pxHeight = getResources().getDisplayMetrics().heightPixels;
@ -395,23 +395,28 @@ public class ViewVideoActivity extends AppCompatActivity {
//Android Q support
if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, mGifOrVideoFileName);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, mGifOrVideoFileName);
} else {
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
File directory = new File(path + "/Infinity/");
boolean saveToInfinityFolder = true;
if(!directory.exists()) {
if(!directory.mkdir()) {
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, mGifOrVideoFileName);
saveToInfinityFolder = false;
}
} else {
if(directory.isFile()) {
if(!directory.delete() && !directory.mkdir()) {
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, mGifOrVideoFileName);
saveToInfinityFolder = false;
}
}
}
if(saveToInfinityFolder) {
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES + "/Infinity/", mGifOrVideoFileName);
} else {
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, mGifOrVideoFileName);
}
}
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
@ -422,6 +427,7 @@ public class ViewVideoActivity extends AppCompatActivity {
}
manager.enqueue(request);
Toast.makeText(this, R.string.download_started, Toast.LENGTH_SHORT).show();
}
@Override

View File

@ -126,7 +126,7 @@
<string name="error_processing_image">Error processing image</string>
<string name="error_processing_video">Error processing video</string>
<string name="download_completed">Download completed</string>
<string name="download_started">Download Started. Check the notification for progress.</string>
<string name="download_failed">Download Failed</string>
<string name="comment_load_more_comments">Load more comments</string>