diff options
author | Alex Ning <chineseperson5@gmail.com> | 2019-09-12 08:42:33 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2019-09-12 08:42:33 +0000 |
commit | 64390d4a5b2c118155d245721567a91181706e3f (patch) | |
tree | da47ece43d61f4ad82c9b5fa0609b37b24ba2fdd | |
parent | a5fcbdec7a7e545ff8adb618a6dc77515a8beaa5 (diff) | |
download | infinity-for-reddit-64390d4a5b2c118155d245721567a91181706e3f.tar infinity-for-reddit-64390d4a5b2c118155d245721567a91181706e3f.tar.gz infinity-for-reddit-64390d4a5b2c118155d245721567a91181706e3f.tar.bz2 infinity-for-reddit-64390d4a5b2c118155d245721567a91181706e3f.tar.lz infinity-for-reddit-64390d4a5b2c118155d245721567a91181706e3f.tar.xz infinity-for-reddit-64390d4a5b2c118155d245721567a91181706e3f.tar.zst infinity-for-reddit-64390d4a5b2c118155d245721567a91181706e3f.zip |
Use download manager to download images. Change image and video download location to Pictures for Android version >= Q. Fix dark theme in ViewMessageActivity.
7 files changed, 68 insertions, 128 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java index c4595c5e..95cb37dd 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java @@ -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); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Message.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Message.java index 1df3b233..8f4b7ae5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Message.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Message.java @@ -112,6 +112,10 @@ class Message { return isNew; } + public void setNew(boolean isNew) { + this.isNew = isNew; + } + public int getScore() { return score; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MessageRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MessageRecyclerViewAdapter.java index 3c627760..6427d22b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MessageRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MessageRecyclerViewAdapter.java @@ -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)); } }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java index db11bb3b..eff6c6c2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java @@ -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); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewImageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewImageActivity.java index 0e08dcb3..3f81263f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewImageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewImageActivity.java @@ -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; - - @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(); - } - } - - @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); - } else { - String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString(); - File directory = new File(path + "/Infinity/"); - if(!directory.exists()) { - if(!directory.mkdir()) { - saveSuccess = false; - return null; - } - } else { - if(directory.isFile()) { - if(!directory.delete() && !directory.mkdir()) { - saveSuccess = false; - return null; - } - } - } - - File file = new File(path + "/Infinity/", mImageFileName + ".jpg"); - int postfix = 1; - while(file.exists()) { - file = new File(path + "/Infinity/", mImageFileName + "-" + (postfix++) + ".jpg"); - } - 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.Request request = new DownloadManager.Request(Uri.parse(mImageUrl)); + request.setTitle(mImageFileName); + + request.allowScanningByMediaScanner(); + request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); + + //Android Q support + if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + 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()) { + saveToInfinityFolder = false; + } + } else { + if(directory.isFile()) { + if(!(directory.delete() && directory.mkdir())) { + saveToInfinityFolder = false; } + } + } - @Override - public void onLoadCleared(@Nullable Drawable placeholder) { - - } - }); + if(saveToInfinityFolder) { + request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES + "/Infinity/", mImageFileName); + } else { + request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, mImageFileName); + } + } + + DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); + + if(manager == null) { + Toast.makeText(this, R.string.download_failed, Toast.LENGTH_SHORT).show(); + return; + } + + manager.enqueue(request); + Toast.makeText(this, R.string.download_started, Toast.LENGTH_SHORT).show(); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewVideoActivity.java index e96f0cff..640db3d7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewVideoActivity.java @@ -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; } } } - request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES + "/Infinity/", mGifOrVideoFileName); + 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 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index cc7d32e1..340e90f9 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -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> |