diff options
author | Alex Ning <chineseperson5@gmail.com> | 2020-11-19 16:08:54 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2020-11-19 16:08:54 +0000 |
commit | 121da395922f810543022fe7bf4c79f55b914a0b (patch) | |
tree | 79819ba2b3b148d7e6e102236467025a4cc49b84 /app/src/main/java/ml/docilealligator/infinityforreddit | |
parent | 1a8e888243782d042969134375cebb8bcd3b1af5 (diff) | |
download | infinity-for-reddit-121da395922f810543022fe7bf4c79f55b914a0b.tar infinity-for-reddit-121da395922f810543022fe7bf4c79f55b914a0b.tar.gz infinity-for-reddit-121da395922f810543022fe7bf4c79f55b914a0b.tar.bz2 infinity-for-reddit-121da395922f810543022fe7bf4c79f55b914a0b.tar.lz infinity-for-reddit-121da395922f810543022fe7bf4c79f55b914a0b.tar.xz infinity-for-reddit-121da395922f810543022fe7bf4c79f55b914a0b.tar.zst infinity-for-reddit-121da395922f810543022fe7bf4c79f55b914a0b.zip |
Fix crash when downloading media. Version 4.1.0-beta6.
Diffstat (limited to 'app/src/main/java/ml/docilealligator/infinityforreddit')
3 files changed, 57 insertions, 57 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadMediaService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadMediaService.java index bd650cbe..7e933906 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadMediaService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadMediaService.java @@ -33,12 +33,12 @@ import java.io.OutputStream; import javax.inject.Inject; import javax.inject.Named; -import ml.docilealligator.infinityforreddit.apis.DownloadFile; -import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.DownloadProgressResponseBody; -import ml.docilealligator.infinityforreddit.events.DownloadMediaEvent; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.apis.DownloadFile; +import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.events.DownloadMediaEvent; import ml.docilealligator.infinityforreddit.utils.NotificationUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import okhttp3.OkHttpClient; @@ -133,9 +133,6 @@ public class DownloadMediaService 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, getNotificationChannelId()); - downloadFinished = false; String fileUrl = intent.getStringExtra(EXTRA_URL); final String[] fileName = {intent.getStringExtra(EXTRA_FILE_NAME)}; @@ -143,32 +140,8 @@ public class DownloadMediaService extends Service { mediaType = intent.getIntExtra(EXTRA_MEDIA_TYPE, EXTRA_MEDIA_TYPE_IMAGE); mimeType = mediaType == EXTRA_MEDIA_TYPE_VIDEO ? "video/*" : "image/*"; - final DownloadProgressResponseBody.ProgressListener progressListener = new DownloadProgressResponseBody.ProgressListener() { - long time = 0; - - @Override public void update(long bytesRead, long contentLength, boolean done) { - if (!done) { - if (contentLength != -1) { - long currentTime = System.currentTimeMillis(); - if (currentTime - time > 1000) { - time = currentTime; - updateNotification(0, (int) ((100 * bytesRead) / contentLength), null); - } - } - } - } - }; - - OkHttpClient client = new OkHttpClient.Builder() - .addNetworkInterceptor(chain -> { - okhttp3.Response originalResponse = chain.proceed(chain.request()); - return originalResponse.newBuilder() - .body(new DownloadProgressResponseBody(originalResponse.body(), progressListener)) - .build(); - }) - .build(); - - retrofit = retrofit.newBuilder().client(client).build(); + notificationManager = NotificationManagerCompat.from(this); + builder = new NotificationCompat.Builder(this, getNotificationChannelId()); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel serviceChannel; @@ -201,6 +174,33 @@ public class DownloadMediaService extends Service { ); } + final DownloadProgressResponseBody.ProgressListener progressListener = new DownloadProgressResponseBody.ProgressListener() { + long time = 0; + + @Override public void update(long bytesRead, long contentLength, boolean done) { + if (!done) { + if (contentLength != -1) { + long currentTime = System.currentTimeMillis(); + if (currentTime - time > 1000) { + time = currentTime; + updateNotification(0, (int) ((100 * bytesRead) / contentLength), null); + } + } + } + } + }; + + OkHttpClient client = new OkHttpClient.Builder() + .addNetworkInterceptor(chain -> { + okhttp3.Response originalResponse = chain.proceed(chain.request()); + return originalResponse.newBuilder() + .body(new DownloadProgressResponseBody(originalResponse.body(), progressListener)) + .build(); + }) + .build(); + + retrofit = retrofit.newBuilder().client(client).build(); + boolean separateDownloadFolder = mSharedPreferences.getBoolean(SharedPreferencesUtils.SEPARATE_FOLDER_FOR_EACH_SUBREDDIT, false); retrofit.create(DownloadFile.class).downloadFile(fileUrl).enqueue(new Callback<ResponseBody>() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadRedditVideoService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadRedditVideoService.java index 5b744a9d..091877cd 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadRedditVideoService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadRedditVideoService.java @@ -39,12 +39,12 @@ import java.nio.ByteBuffer; import javax.inject.Inject; import javax.inject.Named; -import ml.docilealligator.infinityforreddit.apis.DownloadFile; -import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.DownloadProgressResponseBody; -import ml.docilealligator.infinityforreddit.events.DownloadRedditVideoEvent; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.apis.DownloadFile; +import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.events.DownloadRedditVideoEvent; import ml.docilealligator.infinityforreddit.utils.NotificationUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import okhttp3.OkHttpClient; @@ -95,9 +95,30 @@ public class DownloadRedditVideoService extends Service { public int onStartCommand(Intent intent, int flags, int startId) { ((Infinity) getApplication()).getAppComponent().inject(this); + String videoUrl = intent.getStringExtra(EXTRA_VIDEO_URL); + String audioUrl = videoUrl.substring(0, videoUrl.lastIndexOf('/')) + "/DASH_audio.mp4"; + String subredditName = intent.getStringExtra(EXTRA_SUBREDDIT); + final String[] fileNameWithoutExtension = {subredditName + "-" + intent.getStringExtra(EXTRA_POST_ID)}; + notificationManager = NotificationManagerCompat.from(this); builder = new NotificationCompat.Builder(this, NotificationUtils.CHANNEL_ID_DOWNLOAD_REDDIT_VIDEO); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationChannel serviceChannel; + serviceChannel = new NotificationChannel( + NotificationUtils.CHANNEL_ID_DOWNLOAD_REDDIT_VIDEO, + NotificationUtils.CHANNEL_DOWNLOAD_REDDIT_VIDEO, + NotificationManager.IMPORTANCE_LOW + ); + + notificationManager.createNotificationChannel(serviceChannel); + } + + startForeground( + NotificationUtils.DOWNLOAD_REDDIT_VIDEO_NOTIFICATION_ID, + createNotification(fileNameWithoutExtension[0] + ".mp4") + ); + final DownloadProgressResponseBody.ProgressListener progressListener = new DownloadProgressResponseBody.ProgressListener() { long time = 0; @@ -125,27 +146,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); - final String[] fileNameWithoutExtension = {subredditName + "-" + intent.getStringExtra(EXTRA_POST_ID)}; - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - NotificationChannel serviceChannel; - serviceChannel = new NotificationChannel( - NotificationUtils.CHANNEL_ID_DOWNLOAD_REDDIT_VIDEO, - NotificationUtils.CHANNEL_DOWNLOAD_REDDIT_VIDEO, - NotificationManager.IMPORTANCE_LOW - ); - - notificationManager.createNotificationChannel(serviceChannel); - } - - startForeground( - NotificationUtils.DOWNLOAD_REDDIT_VIDEO_NOTIFICATION_ID, - createNotification(fileNameWithoutExtension[0] + ".mp4") - ); - DownloadFile downloadFile = retrofit.create(DownloadFile.class); boolean separateDownloadFolder = sharedPreferences.getBoolean(SharedPreferencesUtils.SEPARATE_FOLDER_FOR_EACH_SUBREDDIT, false); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/APIUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/APIUtils.java index ae1b767c..04929bc5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/APIUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/APIUtils.java @@ -41,7 +41,7 @@ public class APIUtils { public static final String AUTHORIZATION_KEY = "Authorization"; public static final String AUTHORIZATION_BASE = "bearer "; public static final String USER_AGENT_KEY = "User-Agent"; - public static final String USER_AGENT = "android:ml.docilealligator.infinityforreddit:v4.1.0-beta5 (by /u/Hostilenemy)"; + public static final String USER_AGENT = "android:ml.docilealligator.infinityforreddit:v4.1.0-beta6 (by /u/Hostilenemy)"; public static final String GRANT_TYPE_KEY = "grant_type"; public static final String GRANT_TYPE_REFRESH_TOKEN = "refresh_token"; |