Skip to content

Commit 48049d7

Browse files
Start migrating DownloadMediaService to JobService.
1 parent ef9c647 commit 48049d7

File tree

8 files changed

+334
-233
lines changed

8 files changed

+334
-233
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@
161161
<service
162162
android:name=".services.DownloadMediaService"
163163
android:enabled="true"
164+
android:permission="android.permission.BIND_JOB_SERVICE"
164165
android:exported="false" />
165166

166167
<activity

app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewImageOrGifActivity.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package ml.docilealligator.infinityforreddit.activities;
22

33
import android.Manifest;
4+
import android.app.job.JobInfo;
5+
import android.app.job.JobScheduler;
6+
import android.content.Context;
47
import android.content.Intent;
58
import android.content.SharedPreferences;
69
import android.content.pm.PackageManager;
@@ -12,6 +15,7 @@
1215
import android.os.Build;
1316
import android.os.Bundle;
1417
import android.os.Handler;
18+
import android.os.PersistableBundle;
1519
import android.text.Html;
1620
import android.text.Spanned;
1721
import android.view.Menu;
@@ -355,13 +359,25 @@ private void requestPermissionAndDownload() {
355359
private void download() {
356360
isDownloading = false;
357361

358-
Intent intent = new Intent(this, DownloadMediaService.class);
362+
/*Intent intent = new Intent(this, DownloadMediaService.class);
359363
intent.putExtra(DownloadMediaService.EXTRA_URL, mImageUrl);
360364
intent.putExtra(DownloadMediaService.EXTRA_MEDIA_TYPE, isGif ? DownloadMediaService.EXTRA_MEDIA_TYPE_GIF : DownloadMediaService.EXTRA_MEDIA_TYPE_IMAGE);
361365
intent.putExtra(DownloadMediaService.EXTRA_FILE_NAME, mImageFileName);
362366
intent.putExtra(DownloadMediaService.EXTRA_SUBREDDIT_NAME, mSubredditName);
363367
intent.putExtra(DownloadMediaService.EXTRA_IS_NSFW, isNsfw);
364-
ContextCompat.startForegroundService(this, intent);
368+
ContextCompat.startForegroundService(this, intent);*/
369+
370+
PersistableBundle extras = new PersistableBundle();
371+
extras.putString(DownloadMediaService.EXTRA_URL, mImageUrl);
372+
extras.putInt(DownloadMediaService.EXTRA_MEDIA_TYPE, isGif ? DownloadMediaService.EXTRA_MEDIA_TYPE_GIF : DownloadMediaService.EXTRA_MEDIA_TYPE_IMAGE);
373+
extras.putString(DownloadMediaService.EXTRA_FILE_NAME, mImageFileName);
374+
extras.putString(DownloadMediaService.EXTRA_SUBREDDIT_NAME, mSubredditName);
375+
extras.putInt(DownloadMediaService.EXTRA_IS_NSFW, isNsfw ? 1 : 0);
376+
377+
//TODO: contentEstimatedBytes
378+
JobInfo jobInfo = DownloadMediaService.constructJobInfo(this, 5000000, extras);
379+
((JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE)).schedule(jobInfo);
380+
365381
Toast.makeText(this, R.string.download_started, Toast.LENGTH_SHORT).show();
366382
}
367383

app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
import android.Manifest;
99
import android.app.Dialog;
10+
import android.app.job.JobInfo;
11+
import android.app.job.JobScheduler;
12+
import android.content.Context;
1013
import android.content.Intent;
1114
import android.content.SharedPreferences;
1215
import android.content.pm.ActivityInfo;
@@ -22,6 +25,7 @@
2225
import android.os.Build;
2326
import android.os.Bundle;
2427
import android.os.Handler;
28+
import android.os.PersistableBundle;
2529
import android.provider.Settings;
2630
import android.view.Menu;
2731
import android.view.MenuItem;
@@ -107,7 +111,6 @@
107111
import ml.docilealligator.infinityforreddit.post.FetchPost;
108112
import ml.docilealligator.infinityforreddit.post.Post;
109113
import ml.docilealligator.infinityforreddit.services.DownloadMediaService;
110-
import ml.docilealligator.infinityforreddit.services.DownloadRedditVideoService;
111114
import ml.docilealligator.infinityforreddit.utils.APIUtils;
112115
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
113116
import ml.docilealligator.infinityforreddit.utils.Utils;
@@ -978,7 +981,7 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
978981
private void download() {
979982
isDownloading = false;
980983

981-
Intent intent;
984+
/*Intent intent;
982985
if (videoType != VIDEO_TYPE_NORMAL) {
983986
intent = new Intent(this, DownloadMediaService.class);
984987
if (post.getPostType() == Post.GIF_TYPE) {
@@ -1001,7 +1004,35 @@ private void download() {
10011004
intent.putExtra(DownloadRedditVideoService.EXTRA_SUBREDDIT, subredditName);
10021005
intent.putExtra(DownloadRedditVideoService.EXTRA_IS_NSFW, isNSFW);
10031006
}
1004-
ContextCompat.startForegroundService(this, intent);
1007+
ContextCompat.startForegroundService(this, intent);*/
1008+
1009+
if (videoType != VIDEO_TYPE_NORMAL) {
1010+
PersistableBundle extras = new PersistableBundle();
1011+
if (post.getPostType() == Post.GIF_TYPE) {
1012+
extras.putString(DownloadMediaService.EXTRA_URL, post.getVideoUrl());
1013+
extras.putInt(DownloadMediaService.EXTRA_MEDIA_TYPE, DownloadMediaService.EXTRA_MEDIA_TYPE_GIF);
1014+
extras.putString(DownloadMediaService.EXTRA_FILE_NAME, post.getSubredditName()
1015+
+ "-" + post.getId() + ".gif");
1016+
} else {
1017+
extras.putString(DownloadMediaService.EXTRA_URL, videoDownloadUrl);
1018+
extras.putInt(DownloadMediaService.EXTRA_MEDIA_TYPE, DownloadMediaService.EXTRA_MEDIA_TYPE_VIDEO);
1019+
extras.putString(DownloadMediaService.EXTRA_FILE_NAME, videoFileName);
1020+
}
1021+
1022+
extras.putString(DownloadMediaService.EXTRA_SUBREDDIT_NAME, subredditName);
1023+
extras.putInt(DownloadMediaService.EXTRA_IS_NSFW, isNSFW ? 1 : 0);
1024+
1025+
//TODO: contentEstimatedBytes
1026+
JobInfo jobInfo = DownloadMediaService.constructJobInfo(this, 5000000, extras);
1027+
((JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE)).schedule(jobInfo);
1028+
} else {
1029+
/* intent = new Intent(this, DownloadRedditVideoService.class);
1030+
intent.putExtra(DownloadRedditVideoService.EXTRA_VIDEO_URL, videoDownloadUrl);
1031+
intent.putExtra(DownloadRedditVideoService.EXTRA_POST_ID, id);
1032+
intent.putExtra(DownloadRedditVideoService.EXTRA_SUBREDDIT, subredditName);
1033+
intent.putExtra(DownloadRedditVideoService.EXTRA_IS_NSFW, isNSFW);*/
1034+
}
1035+
10051036
Toast.makeText(this, R.string.download_started, Toast.LENGTH_SHORT).show();
10061037
}
10071038

app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewImgurImageFragment.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package ml.docilealligator.infinityforreddit.fragments;
22

33
import android.Manifest;
4+
import android.app.job.JobInfo;
5+
import android.app.job.JobScheduler;
46
import android.content.Context;
57
import android.content.Intent;
68
import android.content.pm.PackageManager;
@@ -10,6 +12,7 @@
1012
import android.os.Build;
1113
import android.os.Bundle;
1214
import android.os.Handler;
15+
import android.os.PersistableBundle;
1316
import android.view.LayoutInflater;
1417
import android.view.Menu;
1518
import android.view.MenuInflater;
@@ -221,11 +224,21 @@ private void requestPermissionAndDownload() {
221224
private void download() {
222225
isDownloading = false;
223226

224-
Intent intent = new Intent(activity, DownloadMediaService.class);
227+
/*Intent intent = new Intent(activity, DownloadMediaService.class);
225228
intent.putExtra(DownloadMediaService.EXTRA_URL, imgurMedia.getLink());
226229
intent.putExtra(DownloadMediaService.EXTRA_MEDIA_TYPE, DownloadMediaService.EXTRA_MEDIA_TYPE_IMAGE);
227230
intent.putExtra(DownloadMediaService.EXTRA_FILE_NAME, imgurMedia.getFileName());
228-
ContextCompat.startForegroundService(activity, intent);
231+
ContextCompat.startForegroundService(activity, intent);*/
232+
233+
PersistableBundle extras = new PersistableBundle();
234+
extras.putString(DownloadMediaService.EXTRA_URL, imgurMedia.getLink());
235+
extras.putInt(DownloadMediaService.EXTRA_MEDIA_TYPE, DownloadMediaService.EXTRA_MEDIA_TYPE_IMAGE);
236+
extras.putString(DownloadMediaService.EXTRA_FILE_NAME, imgurMedia.getFileName());
237+
238+
//TODO: contentEstimatedBytes
239+
JobInfo jobInfo = DownloadMediaService.constructJobInfo(activity, 5000000, extras);
240+
((JobScheduler) activity.getSystemService(Context.JOB_SCHEDULER_SERVICE)).schedule(jobInfo);
241+
229242
Toast.makeText(activity, R.string.download_started, Toast.LENGTH_SHORT).show();
230243
}
231244

app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewImgurVideoFragment.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
package ml.docilealligator.infinityforreddit.fragments;
22

33
import android.Manifest;
4+
import android.app.job.JobInfo;
5+
import android.app.job.JobScheduler;
46
import android.content.Context;
5-
import android.content.Intent;
67
import android.content.SharedPreferences;
78
import android.content.pm.PackageManager;
89
import android.content.res.Configuration;
910
import android.graphics.drawable.Drawable;
1011
import android.media.AudioManager;
1112
import android.os.Build;
1213
import android.os.Bundle;
14+
import android.os.PersistableBundle;
1315
import android.view.LayoutInflater;
1416
import android.view.Menu;
1517
import android.view.MenuInflater;
@@ -258,11 +260,21 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
258260
private void download() {
259261
isDownloading = false;
260262

261-
Intent intent = new Intent(activity, DownloadMediaService.class);
263+
/*Intent intent = new Intent(activity, DownloadMediaService.class);
262264
intent.putExtra(DownloadMediaService.EXTRA_URL, imgurMedia.getLink());
263265
intent.putExtra(DownloadMediaService.EXTRA_MEDIA_TYPE, DownloadMediaService.EXTRA_MEDIA_TYPE_VIDEO);
264266
intent.putExtra(DownloadMediaService.EXTRA_FILE_NAME, imgurMedia.getFileName());
265-
ContextCompat.startForegroundService(activity, intent);
267+
ContextCompat.startForegroundService(activity, intent);*/
268+
269+
PersistableBundle extras = new PersistableBundle();
270+
extras.putString(DownloadMediaService.EXTRA_URL, imgurMedia.getLink());
271+
extras.putInt(DownloadMediaService.EXTRA_MEDIA_TYPE, DownloadMediaService.EXTRA_MEDIA_TYPE_VIDEO);
272+
extras.putString(DownloadMediaService.EXTRA_FILE_NAME, imgurMedia.getFileName());
273+
274+
//TODO: contentEstimatedBytes
275+
JobInfo jobInfo = DownloadMediaService.constructJobInfo(activity, 5000000, extras);
276+
((JobScheduler) activity.getSystemService(Context.JOB_SCHEDULER_SERVICE)).schedule(jobInfo);
277+
266278
Toast.makeText(activity, R.string.download_started, Toast.LENGTH_SHORT).show();
267279
}
268280

app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewRedditGalleryImageOrGifFragment.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package ml.docilealligator.infinityforreddit.fragments;
22

33
import android.Manifest;
4+
import android.app.job.JobInfo;
5+
import android.app.job.JobScheduler;
46
import android.content.Context;
57
import android.content.Intent;
68
import android.content.pm.PackageManager;
@@ -11,6 +13,7 @@
1113
import android.os.Build;
1214
import android.os.Bundle;
1315
import android.os.Handler;
16+
import android.os.PersistableBundle;
1417
import android.text.TextUtils;
1518
import android.text.util.Linkify;
1619
import android.view.LayoutInflater;
@@ -373,13 +376,25 @@ private void requestPermissionAndDownload() {
373376
private void download() {
374377
isDownloading = false;
375378

376-
Intent intent = new Intent(activity, DownloadMediaService.class);
379+
/*Intent intent = new Intent(activity, DownloadMediaService.class);
377380
intent.putExtra(DownloadMediaService.EXTRA_URL, media.hasFallback() ? media.fallbackUrl : media.url); // Retrieve original instead of the one additionally compressed by reddit
378381
intent.putExtra(DownloadMediaService.EXTRA_MEDIA_TYPE, media.mediaType == Post.Gallery.TYPE_GIF ? DownloadMediaService.EXTRA_MEDIA_TYPE_GIF: DownloadMediaService.EXTRA_MEDIA_TYPE_IMAGE);
379382
intent.putExtra(DownloadMediaService.EXTRA_FILE_NAME, media.fileName);
380383
intent.putExtra(DownloadMediaService.EXTRA_SUBREDDIT_NAME, subredditName);
381384
intent.putExtra(DownloadMediaService.EXTRA_IS_NSFW, isNsfw);
382-
ContextCompat.startForegroundService(activity, intent);
385+
ContextCompat.startForegroundService(activity, intent);*/
386+
387+
PersistableBundle extras = new PersistableBundle();
388+
extras.putString(DownloadMediaService.EXTRA_URL, media.hasFallback() ? media.fallbackUrl : media.url); // Retrieve original instead of the one additionally compressed by reddit
389+
extras.putInt(DownloadMediaService.EXTRA_MEDIA_TYPE, media.mediaType == Post.Gallery.TYPE_GIF ? DownloadMediaService.EXTRA_MEDIA_TYPE_GIF: DownloadMediaService.EXTRA_MEDIA_TYPE_IMAGE);
390+
extras.putString(DownloadMediaService.EXTRA_FILE_NAME, media.fileName);
391+
extras.putString(DownloadMediaService.EXTRA_SUBREDDIT_NAME, subredditName);
392+
extras.putInt(DownloadMediaService.EXTRA_IS_NSFW, isNsfw ? 1 : 0);
393+
394+
//TODO: contentEstimatedBytes
395+
JobInfo jobInfo = DownloadMediaService.constructJobInfo(activity, 5000000, extras);
396+
((JobScheduler) activity.getSystemService(Context.JOB_SCHEDULER_SERVICE)).schedule(jobInfo);
397+
383398
Toast.makeText(activity, R.string.download_started, Toast.LENGTH_SHORT).show();
384399
}
385400

app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewRedditGalleryVideoFragment.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
package ml.docilealligator.infinityforreddit.fragments;
22

33
import android.Manifest;
4+
import android.app.job.JobInfo;
5+
import android.app.job.JobScheduler;
46
import android.content.Context;
5-
import android.content.Intent;
67
import android.content.SharedPreferences;
78
import android.content.pm.PackageManager;
89
import android.content.res.Configuration;
910
import android.graphics.drawable.Drawable;
1011
import android.media.AudioManager;
1112
import android.os.Build;
1213
import android.os.Bundle;
14+
import android.os.PersistableBundle;
1315
import android.view.LayoutInflater;
1416
import android.view.Menu;
1517
import android.view.MenuInflater;
@@ -257,13 +259,25 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
257259
private void download() {
258260
isDownloading = false;
259261

260-
Intent intent = new Intent(activity, DownloadMediaService.class);
262+
/*Intent intent = new Intent(activity, DownloadMediaService.class);
261263
intent.putExtra(DownloadMediaService.EXTRA_URL, galleryVideo.url);
262264
intent.putExtra(DownloadMediaService.EXTRA_MEDIA_TYPE, DownloadMediaService.EXTRA_MEDIA_TYPE_VIDEO);
263265
intent.putExtra(DownloadMediaService.EXTRA_FILE_NAME, galleryVideo.fileName);
264266
intent.putExtra(DownloadMediaService.EXTRA_SUBREDDIT_NAME, subredditName);
265267
intent.putExtra(DownloadMediaService.EXTRA_IS_NSFW, isNsfw);
266-
ContextCompat.startForegroundService(activity, intent);
268+
ContextCompat.startForegroundService(activity, intent);*/
269+
270+
PersistableBundle extras = new PersistableBundle();
271+
extras.putString(DownloadMediaService.EXTRA_URL, galleryVideo.url);
272+
extras.putInt(DownloadMediaService.EXTRA_MEDIA_TYPE, DownloadMediaService.EXTRA_MEDIA_TYPE_VIDEO);
273+
extras.putString(DownloadMediaService.EXTRA_FILE_NAME, galleryVideo.fileName);
274+
extras.putString(DownloadMediaService.EXTRA_SUBREDDIT_NAME, subredditName);
275+
extras.putInt(DownloadMediaService.EXTRA_IS_NSFW, isNsfw ? 1 : 0);
276+
277+
//TODO: contentEstimatedBytes
278+
JobInfo jobInfo = DownloadMediaService.constructJobInfo(activity, 5000000, extras);
279+
((JobScheduler) activity.getSystemService(Context.JOB_SCHEDULER_SERVICE)).schedule(jobInfo);
280+
267281
Toast.makeText(activity, R.string.download_started, Toast.LENGTH_SHORT).show();
268282
}
269283

0 commit comments

Comments
 (0)