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()); intent.putExtra(ViewVideoActivity.IS_DOWNLOADABLE_KEY, mPost.isDownloadableGifOrVideo());
if(mPost.isDownloadableGifOrVideo()) { if(mPost.isDownloadableGifOrVideo()) {
intent.putExtra(ViewVideoActivity.DOWNLOAD_URL_KEY, mPost.getGifOrVideoDownloadUrl()); 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()); intent.putExtra(ViewVideoActivity.ID_KEY, mPost.getId());
} }
mActivity.startActivity(intent); mActivity.startActivity(intent);
@ -451,7 +451,7 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie
intent.putExtra(ViewVideoActivity.IS_DOWNLOADABLE_KEY, mPost.isDownloadableGifOrVideo()); intent.putExtra(ViewVideoActivity.IS_DOWNLOADABLE_KEY, mPost.isDownloadableGifOrVideo());
if(mPost.isDownloadableGifOrVideo()) { if(mPost.isDownloadableGifOrVideo()) {
intent.putExtra(ViewVideoActivity.DOWNLOAD_URL_KEY, mPost.getGifOrVideoDownloadUrl()); 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()); intent.putExtra(ViewVideoActivity.ID_KEY, mPost.getId());
} }
mActivity.startActivity(intent); mActivity.startActivity(intent);

View File

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

View File

@ -127,7 +127,8 @@ class MessageRecyclerViewAdapter extends PagedListAdapter<Message, RecyclerView.
} }
if(message.isNew()) { 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(), ReadMessage.readMessage(mOauthRetrofit, mAccessToken, message.getFullname(),
new ReadMessage.ReadMessageListener() { new ReadMessage.ReadMessageListener() {
@ -136,6 +137,7 @@ class MessageRecyclerViewAdapter extends PagedListAdapter<Message, RecyclerView.
@Override @Override
public void readFailed() { public void readFailed() {
message.setNew(true);
((DataViewHolder) holder).itemView.setBackgroundColor(mResources.getColor(R.color.unreadMessageBackgroundColor)); ((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()); intent.putExtra(ViewVideoActivity.IS_DOWNLOADABLE_KEY, post.isDownloadableGifOrVideo());
if(post.isDownloadableGifOrVideo()) { if(post.isDownloadableGifOrVideo()) {
intent.putExtra(ViewVideoActivity.DOWNLOAD_URL_KEY, post.getGifOrVideoDownloadUrl()); 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); intent.putExtra(ViewVideoActivity.ID_KEY, id);
} }
mContext.startActivity(intent); mContext.startActivity(intent);
@ -441,7 +441,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
intent.putExtra(ViewVideoActivity.IS_DOWNLOADABLE_KEY, post.isDownloadableGifOrVideo()); intent.putExtra(ViewVideoActivity.IS_DOWNLOADABLE_KEY, post.isDownloadableGifOrVideo());
if(post.isDownloadableGifOrVideo()) { if(post.isDownloadableGifOrVideo()) {
intent.putExtra(ViewVideoActivity.DOWNLOAD_URL_KEY, post.getGifOrVideoDownloadUrl()); 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); intent.putExtra(ViewVideoActivity.ID_KEY, id);
} }
mContext.startActivity(intent); mContext.startActivity(intent);

View File

@ -4,22 +4,17 @@ import android.Manifest;
import android.animation.Animator; import android.animation.Animator;
import android.animation.ArgbEvaluator; import android.animation.ArgbEvaluator;
import android.animation.ValueAnimator; import android.animation.ValueAnimator;
import android.annotation.SuppressLint; import android.app.DownloadManager;
import android.content.ContentResolver; import android.content.Context;
import android.content.ContentValues;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.PorterDuff; import android.graphics.PorterDuff;
import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.os.ParcelFileDescriptor;
import android.provider.MediaStore;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.MotionEvent; 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.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener; import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions; 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.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.github.pwittchen.swipe.library.rx2.SimpleSwipeListener; import com.github.pwittchen.swipe.library.rx2.SimpleSwipeListener;
import com.github.pwittchen.swipe.library.rx2.Swipe; import com.github.pwittchen.swipe.library.rx2.Swipe;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
@ -99,8 +89,8 @@ public class ViewImageActivity extends AppCompatActivity {
setTitle(""); setTitle("");
Intent intent = getIntent(); Intent intent = getIntent();
mImageUrl = intent.getExtras().getString(IMAGE_URL_KEY); mImageUrl = intent.getStringExtra(IMAGE_URL_KEY);
mImageFileName = intent.getExtras().getString(FILE_NAME_KEY); mImageFileName = intent.getStringExtra(FILE_NAME_KEY) + ".jpg";
mLoadErrorLinearLayout.setOnClickListener(view -> { mLoadErrorLinearLayout.setOnClickListener(view -> {
if(!isSwiping) { if(!isSwiping) {
@ -402,109 +392,47 @@ public class ViewImageActivity extends AppCompatActivity {
} }
private void saveImage() { private void saveImage() {
Glide.with(this) DownloadManager.Request request = new DownloadManager.Request(Uri.parse(mImageUrl));
.asBitmap() request.setTitle(mImageFileName);
.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;
@Override request.allowScanningByMediaScanner();
protected void onPostExecute(Void aVoid) { request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
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();
}
}
@Override
protected Void doInBackground(Void... params) {
try {
//Android Q support //Android Q support
if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContentValues values = new ContentValues(); request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, mImageFileName);
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);
} else { } else {
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString(); String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
File directory = new File(path + "/Infinity/"); File directory = new File(path + "/Infinity/");
boolean saveToInfinityFolder = true;
if(!directory.exists()) { if(!directory.exists()) {
if(!directory.mkdir()) { if(!directory.mkdir()) {
saveSuccess = false; saveToInfinityFolder = false;
return null;
} }
} else { } else {
if(directory.isFile()) { if(directory.isFile()) {
if(!directory.delete() && !directory.mkdir()) { if(!(directory.delete() && directory.mkdir())) {
saveSuccess = false; saveToInfinityFolder = false;
return null;
} }
} }
} }
File file = new File(path + "/Infinity/", mImageFileName + ".jpg"); if(saveToInfinityFolder) {
int postfix = 1; request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES + "/Infinity/", mImageFileName);
while(file.exists()) { } else {
file = new File(path + "/Infinity/", mImageFileName + "-" + (postfix++) + ".jpg"); 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; DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
}
}.execute(); if(manager == null) {
Toast.makeText(this, R.string.download_failed, Toast.LENGTH_SHORT).show();
return;
} }
@Override manager.enqueue(request);
public void onLoadCleared(@Nullable Drawable placeholder) { Toast.makeText(this, R.string.download_started, Toast.LENGTH_SHORT).show();
}
});
} }
@Override @Override

View File

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

View File

@ -126,7 +126,7 @@
<string name="error_processing_image">Error processing image</string> <string name="error_processing_image">Error processing image</string>
<string name="error_processing_video">Error processing video</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="download_failed">Download Failed</string>
<string name="comment_load_more_comments">Load more comments</string> <string name="comment_load_more_comments">Load more comments</string>