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 { if(!isSwiping) { @@ -402,109 +392,47 @@ public class ViewImageActivity extends AppCompatActivity { } private void saveImage() { - Glide.with(this) - .asBitmap() - .load(mImageUrl) - .into(new CustomTarget() { - @SuppressLint("StaticFieldLeak") - @Override - public void onResourceReady(@NonNull final Bitmap resource, @Nullable Transition transition) { - new AsyncTask() { - 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); - } 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(); + //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 @@ Error processing image Error processing video - Download completed + Download Started. Check the notification for progress. Download Failed Load more comments