From 79392bbeae9c70f4a2ff1a81848b3d3087e53344 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 16 Oct 2020 10:24:08 +0800 Subject: [PATCH] Fix cannot download gfycat and redgifs videos. Fix fetching gfycat and redgifs videos failed in PostRecyclerViewAdapter and CommentAndPostRecyclerViewAdapter. Show progress bar when downloading reddit videos. --- .../Activity/ViewVideoActivity.java | 19 ++- .../Adapter/PostRecyclerViewAdapter.java | 8 +- .../infinityforreddit/Post/ParsePost.java | 12 +- .../Service/DownloadMediaService.java | 17 +-- .../Service/DownloadRedditVideoService.java | 122 +++++++++--------- app/src/main/res/values-de/strings.xml | 2 - app/src/main/res/values-es/strings.xml | 2 - app/src/main/res/values-fr/strings.xml | 2 - app/src/main/res/values-hi/strings.xml | 2 - app/src/main/res/values-hr/strings.xml | 2 - app/src/main/res/values-it/strings.xml | 2 - app/src/main/res/values-ja/strings.xml | 2 - app/src/main/res/values-pt-rBR/strings.xml | 2 - app/src/main/res/values/strings.xml | 5 - 14 files changed, 88 insertions(+), 111 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java index 946d92a5..a8cefd9f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewVideoActivity.java @@ -214,22 +214,27 @@ public class ViewVideoActivity extends AppCompatActivity { String videoUrl = savedInstanceState.getString(VIDEO_URI_STATE); if (videoUrl != null) { mVideoUri = Uri.parse(videoUrl); - videoDownloadUrl = savedInstanceState.getString(VIDEO_DOWNLOAD_URL_STATE); } + videoDownloadUrl = savedInstanceState.getString(VIDEO_DOWNLOAD_URL_STATE); } else { mVideoUri = intent.getData(); + videoDownloadUrl = intent.getStringExtra(EXTRA_VIDEO_DOWNLOAD_URL); + } + + String gfycatId = intent.getStringExtra(EXTRA_GFYCAT_ID); + if (gfycatId != null && gfycatId.contains("-")) { + gfycatId = gfycatId.substring(0, gfycatId.indexOf('-')); + } + if (videoType == VIDEO_TYPE_GFYCAT) { + videoFileName = "Gfycat-" + gfycatId + ".mp4"; + } else { + videoFileName = "Redgifs-" + gfycatId + ".mp4"; } if (mVideoUri == null) { - String gfycatId = intent.getStringExtra(EXTRA_GFYCAT_ID); - if (gfycatId != null && gfycatId.contains("-")) { - gfycatId = gfycatId.substring(0, gfycatId.indexOf('-')); - } if (videoType == VIDEO_TYPE_GFYCAT) { - videoFileName = "Gfycat-" + gfycatId + ".mp4"; loadGfycatOrRedgifsVideo(gfycatRetrofit, gfycatId, savedInstanceState, true); } else { - videoFileName = "Redgifs-" + gfycatId + ".mp4"; loadGfycatOrRedgifsVideo(redgifsRetrofit, gfycatId, savedInstanceState, false); } } else { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java index 1cbdc75e..53771d97 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java @@ -593,12 +593,16 @@ public class PostRecyclerViewAdapter extends PagedListAdapter 2) { + if (currentTime - time > 1000) { time = currentTime; updateNotification(0, (int) ((100 * bytesRead) / contentLength), null); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadRedditVideoService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadRedditVideoService.java index 6ae6350b..440441c7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadRedditVideoService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadRedditVideoService.java @@ -7,7 +7,6 @@ import android.app.PendingIntent; import android.app.Service; import android.content.ContentResolver; import android.content.ContentValues; -import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.media.MediaCodec; @@ -21,7 +20,6 @@ import android.os.Build; import android.os.Environment; import android.os.IBinder; import android.provider.MediaStore; -import android.util.Log; import androidx.annotation.NonNull; import androidx.core.app.NotificationCompat; @@ -82,6 +80,8 @@ public class DownloadRedditVideoService extends Service { @Inject CustomThemeWrapper customThemeWrapper; String resultFile; + private NotificationManagerCompat notificationManager; + private NotificationCompat.Builder builder; public DownloadRedditVideoService() { } @@ -95,27 +95,20 @@ public class DownloadRedditVideoService extends Service { public int onStartCommand(Intent intent, int flags, int startId) { ((Infinity) getApplication()).getAppComponent().inject(this); + notificationManager = NotificationManagerCompat.from(this); + builder = new NotificationCompat.Builder(this, NotificationUtils.CHANNEL_ID_DOWNLOAD_REDDIT_VIDEO); final DownloadProgressResponseBody.ProgressListener progressListener = new DownloadProgressResponseBody.ProgressListener() { - boolean firstUpdate = true; + long time = 0; @Override public void update(long bytesRead, long contentLength, boolean done) { - if (done) { - Log.i("adfasdf", "completed"); - } else { - if (firstUpdate) { - firstUpdate = false; - if (contentLength == -1) { - Log.i("adfasdf", "content-length: unknown"); - } else { - Log.i("adfasdf", "content-length: " + contentLength); - } - } - - Log.i("adfasdf", "bytes read " + bytesRead); - + if (!done) { if (contentLength != -1) { - Log.i("adfasdf", "progress: " + ((100 * bytesRead) / contentLength)); + long currentTime = System.currentTimeMillis(); + if (currentTime - time > 1000) { + time = currentTime; + updateNotification(0, (int) ((100 * bytesRead) / contentLength), null); + } } } } @@ -132,9 +125,6 @@ public class DownloadRedditVideoService extends Service { retrofit = retrofit.newBuilder().client(client).build(); - - - String videoUrl = intent.getStringExtra(EXTRA_VIDEO_URL); String audioUrl = videoUrl.substring(0, videoUrl.lastIndexOf('/')) + "/DASH_audio.mp4"; String subredditName = intent.getStringExtra(EXTRA_SUBREDDIT); @@ -154,7 +144,7 @@ public class DownloadRedditVideoService extends Service { startForeground( NotificationUtils.DOWNLOAD_REDDIT_VIDEO_NOTIFICATION_ID, - createNotification(R.string.downloading_reddit_video, fileNameWithoutExtension[0] + ".mp4", null) + createNotification(fileNameWithoutExtension[0] + ".mp4") ); DownloadFile downloadFile = retrofit.create(DownloadFile.class); @@ -179,12 +169,12 @@ public class DownloadRedditVideoService extends Service { String directoryPath = separateDownloadFolder ? directory.getAbsolutePath() + "/Infinity/" + subredditName + "/" : directory.getAbsolutePath() + "/Infinity/"; File infinityDir = new File(directoryPath); if (!infinityDir.exists() && !infinityDir.mkdir()) { - downloadFinished(null, destinationFileName, ERROR_CANNOT_GET_DESTINATION_DIRECTORY); + downloadFinished(null, ERROR_CANNOT_GET_DESTINATION_DIRECTORY); return; } destinationFileUriString = directoryPath + destinationFileName; } else { - downloadFinished(null, destinationFileName, ERROR_CANNOT_GET_DESTINATION_DIRECTORY); + downloadFinished(null, ERROR_CANNOT_GET_DESTINATION_DIRECTORY); return; } } else { @@ -198,21 +188,21 @@ public class DownloadRedditVideoService extends Service { if (separateDownloadFolder) { dir = DocumentFile.fromTreeUri(DownloadRedditVideoService.this, Uri.parse(destinationFileDirectory)); if (dir == null) { - downloadFinished(null, destinationFileName, ERROR_CANNOT_GET_DESTINATION_DIRECTORY); + downloadFinished(null, ERROR_CANNOT_GET_DESTINATION_DIRECTORY); return; } dir = dir.findFile(subredditName); if (dir == null) { dir = DocumentFile.fromTreeUri(DownloadRedditVideoService.this, Uri.parse(destinationFileDirectory)).createDirectory(subredditName); if (dir == null) { - downloadFinished(null, destinationFileName, ERROR_CANNOT_GET_DESTINATION_DIRECTORY); + downloadFinished(null, ERROR_CANNOT_GET_DESTINATION_DIRECTORY); return; } } } else { dir = DocumentFile.fromTreeUri(DownloadRedditVideoService.this, Uri.parse(destinationFileDirectory)); if (dir == null) { - downloadFinished(null, destinationFileName, ERROR_CANNOT_GET_DESTINATION_DIRECTORY); + downloadFinished(null, ERROR_CANNOT_GET_DESTINATION_DIRECTORY); return; } } @@ -225,13 +215,13 @@ public class DownloadRedditVideoService extends Service { } picFile = dir.createFile("video/*", fileNameWithoutExtension[0] + ".mp4"); if (picFile == null) { - downloadFinished(null, destinationFileName, ERROR_CANNOT_GET_DESTINATION_DIRECTORY); + downloadFinished(null, ERROR_CANNOT_GET_DESTINATION_DIRECTORY); return; } destinationFileUriString = picFile.getUri().toString(); } - updateNotification(R.string.downloading_reddit_video_audio_track, destinationFileName, null); + updateNotification(R.string.downloading_reddit_video_audio_track, 0, null); downloadFile.downloadFile(audioUrl).enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull Response audioResponse) { @@ -249,12 +239,12 @@ public class DownloadRedditVideoService extends Service { new File(videoFilePath).delete(); new File(audioFilePath).delete(); new File(outputFilePath).delete(); - downloadFinished(destinationFileUri, destinationFileName, errorCode); + downloadFinished(destinationFileUri, errorCode); } @Override public void updateProgressNotification(int stringResId) { - updateNotification(stringResId, destinationFileName, null); + updateNotification(stringResId, -1, null); } }).execute(); } else { @@ -267,12 +257,12 @@ public class DownloadRedditVideoService extends Service { @Override public void finished(Uri destinationFileUri, int errorCode) { new File(videoFilePath).delete(); - downloadFinished(destinationFileUri, destinationFileName, errorCode); + downloadFinished(destinationFileUri, errorCode); } @Override public void updateProgressNotification(int stringResId) { - updateNotification(stringResId, destinationFileName, null); + updateNotification(stringResId, -1, null); } }).execute(); } @@ -289,56 +279,56 @@ public class DownloadRedditVideoService extends Service { @Override public void finished(Uri destinationFileUri, int errorCode) { new File(videoFilePath).delete(); - downloadFinished(destinationFileUri, destinationFileName, errorCode); + downloadFinished(destinationFileUri, errorCode); } @Override public void updateProgressNotification(int stringResId) { - updateNotification(stringResId, destinationFileName, null); + updateNotification(stringResId, -1, null); } }).execute(); } }); } else { - downloadFinished(null, destinationFileName, ERROR_VIDEO_FILE_CANNOT_DOWNLOAD); + downloadFinished(null, ERROR_VIDEO_FILE_CANNOT_DOWNLOAD); } } @Override public void onFailure(@NonNull Call call, @NonNull Throwable t) { - downloadFinished(null, destinationFileName, ERROR_VIDEO_FILE_CANNOT_DOWNLOAD); + downloadFinished(null, ERROR_VIDEO_FILE_CANNOT_DOWNLOAD); } }); } else { - downloadFinished(null, destinationFileName, ERROR_CANNOT_GET_CACHE_DIRECTORY); + downloadFinished(null, ERROR_CANNOT_GET_CACHE_DIRECTORY); } return START_NOT_STICKY; } - private void downloadFinished(Uri destinationFileUri, String fileName, int errorCode) { + private void downloadFinished(Uri destinationFileUri, int errorCode) { if (errorCode != NO_ERROR) { switch (errorCode) { case ERROR_CANNOT_GET_CACHE_DIRECTORY: - updateNotification(R.string.downloading_reddit_video_failed_cannot_get_cache_directory, fileName, null); + updateNotification(R.string.downloading_reddit_video_failed_cannot_get_cache_directory, -1, null); break; case ERROR_VIDEO_FILE_CANNOT_DOWNLOAD: - updateNotification(R.string.downloading_reddit_video_failed_cannot_download_video, fileName, null); + updateNotification(R.string.downloading_reddit_video_failed_cannot_download_video, -1, null); break; case ERROR_VIDEO_FILE_CANNOT_SAVE: - updateNotification(R.string.downloading_reddit_video_failed_cannot_save_video, fileName, null); + updateNotification(R.string.downloading_reddit_video_failed_cannot_save_video, -1, null); break; case ERROR_AUDIO_FILE_CANNOT_SAVE: - updateNotification(R.string.downloading_reddit_video_failed_cannot_save_audio, fileName, null); + updateNotification(R.string.downloading_reddit_video_failed_cannot_save_audio, -1, null); break; case ERROR_MUX_FAILED: - updateNotification(R.string.downloading_reddit_video_failed_cannot_mux, fileName, null); + updateNotification(R.string.downloading_reddit_video_failed_cannot_mux, -1, null); break; case ERROR_MUXED_VIDEO_FILE_CANNOT_SAVE: - updateNotification(R.string.downloading_reddit_video_failed_cannot_save_mux_video, fileName, null); + updateNotification(R.string.downloading_reddit_video_failed_cannot_save_mux_video, -1, null); break; case ERROR_CANNOT_GET_DESTINATION_DIRECTORY: - updateNotification(R.string.downloading_media_failed_cannot_save_to_destination_directory, fileName, null); + updateNotification(R.string.downloading_media_failed_cannot_save_to_destination_directory, -1, null); break; } EventBus.getDefault().post(new DownloadRedditVideoEvent(false)); @@ -351,7 +341,7 @@ public class DownloadRedditVideoService extends Service { intent.setDataAndType(destinationFileUri, "video/*"); intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); - updateNotification(R.string.downloading_reddit_video_finished, fileName, pendingIntent); + updateNotification(R.string.downloading_reddit_video_finished, -1, pendingIntent); EventBus.getDefault().post(new DownloadRedditVideoEvent(true)); } ); @@ -359,22 +349,27 @@ public class DownloadRedditVideoService extends Service { stopForeground(false); } - private Notification createNotification(int stringResId, String fileName, PendingIntent pendingIntent) { - NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NotificationUtils.CHANNEL_ID_DOWNLOAD_REDDIT_VIDEO); - builder.setContentTitle(fileName).setContentText(getString(stringResId)); - if (pendingIntent != null) { - builder.setContentIntent(pendingIntent); - } + private Notification createNotification(String fileName) { + builder.setContentTitle(fileName).setContentText(getString(R.string.downloading_reddit_video)).setProgress(100, 0, false); return builder.setSmallIcon(R.drawable.ic_notification) .setColor(customThemeWrapper.getColorPrimaryLightTheme()) .build(); } - private void updateNotification(int stringResId, String fileName, PendingIntent pendingIntent) { - NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + private void updateNotification(int contentStringResId, int progress, PendingIntent pendingIntent) { if (notificationManager != null) { - notificationManager.notify(NotificationUtils.DOWNLOAD_REDDIT_VIDEO_NOTIFICATION_ID, - createNotification(stringResId, fileName, pendingIntent)); + if (progress < 0) { + builder.setProgress(0, 0, false); + } else { + builder.setProgress(100, progress, false); + } + if (contentStringResId != 0) { + builder.setContentText(getString(contentStringResId)); + } + if (pendingIntent != null) { + builder.setContentIntent(pendingIntent); + } + notificationManager.notify(NotificationUtils.DOWNLOAD_REDDIT_VIDEO_NOTIFICATION_ID, builder.build()); } } @@ -418,14 +413,12 @@ public class DownloadRedditVideoService extends Service { @Override protected Void doInBackground(Void... voids) { - publishProgress(R.string.downloading_reddit_video_save_video); String savedVideoFilePath = writeResponseBodyToDisk(videoResponse, videoFilePath); if (savedVideoFilePath == null) { errorCode = ERROR_VIDEO_FILE_CANNOT_SAVE; return null; } if (audioResponse != null) { - publishProgress(R.string.downloading_reddit_video_save_audio); String savedAudioFilePath = writeResponseBodyToDisk(audioResponse, audioFilePath); if (savedAudioFilePath == null) { errorCode = ERROR_AUDIO_FILE_CANNOT_SAVE; @@ -529,7 +522,7 @@ public class DownloadRedditVideoService extends Service { int audioTrack = muxer.addTrack(audioFormat); boolean sawEOS = false; int offset = 100; - int sampleSize = 2048 * 1024; + int sampleSize = 4096 * 1024; ByteBuffer videoBuf = ByteBuffer.allocate(sampleSize); ByteBuffer audioBuf = ByteBuffer.allocate(sampleSize); MediaCodec.BufferInfo videoBufferInfo = new MediaCodec.BufferInfo(); @@ -540,6 +533,7 @@ public class DownloadRedditVideoService extends Service { muxer.start(); + int muxedSize = 0; while (!sawEOS) { videoBufferInfo.offset = offset; videoBufferInfo.size = videoExtractor.readSampleData(videoBuf, offset); @@ -551,6 +545,7 @@ public class DownloadRedditVideoService extends Service { videoBufferInfo.presentationTimeUs = videoExtractor.getSampleTime(); videoBufferInfo.flags = videoExtractor.getSampleFlags(); muxer.writeSampleData(videoTrack, videoBuf, videoBufferInfo); + muxedSize += videoTrack; videoExtractor.advance(); } } @@ -571,10 +566,9 @@ public class DownloadRedditVideoService extends Service { } } - try { - muxer.stop(); - muxer.release(); - } catch (IllegalStateException ignore) {} + muxer.stop(); + muxer.release(); + } catch (IllegalStateException ignore) { } catch (IOException e) { e.printStackTrace(); return false; diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 2a4513c1..5cb295fe 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -764,8 +764,6 @@ Sie ist nicht krank oder so, aber sie könnte sich definitiv bessern." "Kann Bilder nicht laden" "Lade Videospur herunter" "Lade Audiospur herunter" - "Speichere Videospur" - "Speichere Tonspur" "Muxe Ton und Video" "Speichere Video" "Heruntergeladen" diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index c3fac9d6..ff2dd86d 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -758,8 +758,6 @@ Base: Indigo OLED" "Ocurrió un error cargando las imágenes" "Descargando Track Vídeo" "Descargando Track Audio" - "Guardando Audio" - "Guardado Audio Track" "Muxtiplexar Vídeo y Audio" "Guardando Vídeo" "Descargado" diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 923848b9..3dc93012 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -814,8 +814,6 @@ Basé sur le thème sombre Indigo" "Ne peut pas charger les images" "Téléchargement de la piste vidéo" "Téléchargement de la piste audio" - "Enregistrement de la piste vidéo" - "Enregistrement de la piste audio" "Muxage vidéo et audio" "Téléchargement de la vidéo" "Téléchargé" diff --git a/app/src/main/res/values-hi/strings.xml b/app/src/main/res/values-hi/strings.xml index 30329e50..8dfad5fd 100644 --- a/app/src/main/res/values-hi/strings.xml +++ b/app/src/main/res/values-hi/strings.xml @@ -783,8 +783,6 @@ https://play.google.com/store/apps/details?id=ml.docilealligator.infinityforredd "चित्र लोड नहीं हो सका " "वीडियो ट्रैक डाउनलोड हो रहा है " "ऑडियो ट्रैक डाउनलोड हो रहा है " - "वीडियो ट्रैक सहेजा जा रहा है " - "ऑडियो ट्रैक सहेजा जा रहा है " "वीडियो एवं ऑडियो का एकीकरण किया जा रहा है " "वीडियो सहेजी जा रही है " "वीडियो सेब हो गया" diff --git a/app/src/main/res/values-hr/strings.xml b/app/src/main/res/values-hr/strings.xml index 1f4cb520..bf916b5a 100644 --- a/app/src/main/res/values-hr/strings.xml +++ b/app/src/main/res/values-hr/strings.xml @@ -759,8 +759,6 @@ Baziraj na Indigo Amoled temi" "Nije moguće učitati slike" "Preuzimanje videozapisa" "Preuzimanje audio zapisa" - "Spremanje videozapisa" - "Spremanje audio zapisa" "Multipleksiranje videa i audija" "Spremanje videa" "Preuzeto" diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index 0ecbb0c4..d64e14dd 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -790,8 +790,6 @@ Non è malata o altro, ma potrebbe sicuramente stare meglio." "Impossibile caricare immagini" "Download Traccia Video" "Download Traccia Audio" - "Salvataggio Traccia Video" - "Salvataggio Traccia Audio" "Muxing Video e Audio" "Salvataggio Video" "Scaricato" diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index a3b8f3d4..bd4ce474 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -693,8 +693,6 @@ https://play.google.com/store "画像をロードできません" "動画トラックをダウンロード中" "音声トラックをダウンロード中" - "動画トラックを保存中" - "音声トラックを保存中" diff --git a/app/src/main/res/values-pt-rBR/strings.xml b/app/src/main/res/values-pt-rBR/strings.xml index 40b2e5f1..6e1f8037 100644 --- a/app/src/main/res/values-pt-rBR/strings.xml +++ b/app/src/main/res/values-pt-rBR/strings.xml @@ -754,8 +754,6 @@ Baseado no Tema Indigo Amoled" "Não foi possível carregar as imagens" "Executando download do vídeo" "Executando download do áudio" - "Salvando vídeo" - "Salvando áudio" "Sincronizando áudio e vídeo" "Salvando vídeo" "Baixado" diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 294f9fce..886d65c0 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -843,8 +843,6 @@ Downloading Video Track Downloading Audio Track - Saving Video Track - Saving Audio Track Muxing Video and Audio Saving Video Downloaded @@ -854,8 +852,6 @@ Download failed: cannot save audio to cache directory Download failed: cannot mux video and audio Download failed: cannot save the video to public directory - Downloading Video - Saving Video Downloading Downloaded @@ -885,7 +881,6 @@ View Full Markdown User flair selected - Cannot select user flair Select this user flair? Select Header Size