diff options
author | Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> | 2024-07-18 20:23:14 +0000 |
---|---|---|
committer | Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> | 2024-07-18 20:23:14 +0000 |
commit | 78a1d81ae81aa609cd6adc2e67482b5835ca368c (patch) | |
tree | 6631fb9a0e2263986b98a2e134ac0a3f4d7023ec | |
parent | a43b2e8f525c627a858ea8e7af1f63f5516ac2d5 (diff) | |
download | infinity-for-reddit-78a1d81ae81aa609cd6adc2e67482b5835ca368c.tar infinity-for-reddit-78a1d81ae81aa609cd6adc2e67482b5835ca368c.tar.gz infinity-for-reddit-78a1d81ae81aa609cd6adc2e67482b5835ca368c.tar.bz2 infinity-for-reddit-78a1d81ae81aa609cd6adc2e67482b5835ca368c.tar.lz infinity-for-reddit-78a1d81ae81aa609cd6adc2e67482b5835ca368c.tar.xz infinity-for-reddit-78a1d81ae81aa609cd6adc2e67482b5835ca368c.tar.zst infinity-for-reddit-78a1d81ae81aa609cd6adc2e67482b5835ca368c.zip |
Starting migrating SubmitPostService to JobService.
Diffstat (limited to '')
12 files changed, 361 insertions, 127 deletions
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b9867479..d6d5aa9c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -23,6 +23,7 @@ <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> + <uses-permission android:name="android.permission.RUN_USER_INITIATED_JOBS" /> <application android:name=".Infinity" @@ -461,10 +462,12 @@ <service android:name=".services.SubmitPostService" android:enabled="true" + android:permission="android.permission.BIND_JOB_SERVICE" android:exported="false" /> <service android:name=".services.EditProfileService" android:enabled="true" + android:permission="android.permission.BIND_JOB_SERVICE" android:exported="false" /> <receiver android:name=".broadcastreceivers.DownloadedMediaDeleteActionBroadcastReceiver" /> diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Flair.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Flair.java index bd2590cf..28eb33ba 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Flair.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Flair.java @@ -3,6 +3,9 @@ package ml.docilealligator.infinityforreddit; import android.os.Parcel; import android.os.Parcelable; +import com.google.gson.Gson; +import com.google.gson.JsonParseException; + public class Flair implements Parcelable { public static final Creator<Flair> CREATOR = new Creator<Flair>() { @Override @@ -66,4 +69,12 @@ public class Flair implements Parcelable { parcel.writeString(text); parcel.writeByte((byte) (editable ? 1 : 0)); } + + public String getJSONModel() { + return new Gson().toJson(this); + } + + public static Flair fromJson(String json) throws JsonParseException { + return new Gson().fromJson(json, Flair.class); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/UploadedImage.java b/app/src/main/java/ml/docilealligator/infinityforreddit/UploadedImage.java index f966c934..093b7843 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/UploadedImage.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/UploadedImage.java @@ -3,6 +3,13 @@ package ml.docilealligator.infinityforreddit; import android.os.Parcel; import android.os.Parcelable; +import com.google.gson.Gson; +import com.google.gson.JsonParseException; +import com.google.gson.reflect.TypeToken; + +import java.util.ArrayList; +import java.util.List; + public class UploadedImage implements Parcelable { public String imageName; public String imageUrlOrKey; @@ -50,4 +57,12 @@ public class UploadedImage implements Parcelable { parcel.writeString(imageUrlOrKey); parcel.writeString(caption); } + + public static String getArrayListJSONModel(ArrayList<UploadedImage> uploadedImages) { + return new Gson().toJson(uploadedImages, new TypeToken<List<UploadedImage>>(){}.getType()); + } + + public static List<UploadedImage> fromListJson(String json) throws JsonParseException { + return new Gson().fromJson(json, new TypeToken<List<UploadedImage>>(){}.getType()); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/account/Account.java b/app/src/main/java/ml/docilealligator/infinityforreddit/account/Account.java index 67b40d2e..3eb88ebb 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/account/Account.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/account/Account.java @@ -9,6 +9,9 @@ import androidx.room.Entity; import androidx.room.Ignore; import androidx.room.PrimaryKey; +import com.google.gson.Gson; +import com.google.gson.JsonParseException; + @Entity(tableName = "accounts") public class Account implements Parcelable { public static final String ANONYMOUS_ACCOUNT = "-"; @@ -125,4 +128,12 @@ public class Account implements Parcelable { dest.writeString(code); dest.writeByte((byte) (isCurrentUser ? 1 : 0)); } + + public String getJSONModel() { + return new Gson().toJson(this); + } + + public static Account fromJson(String json) throws JsonParseException { + return new Gson().fromJson(json, Account.class); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostGalleryActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostGalleryActivity.java index 0123faf7..cfef82a9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostGalleryActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostGalleryActivity.java @@ -1,6 +1,9 @@ package ml.docilealligator.infinityforreddit.activities; +import android.app.job.JobInfo; +import android.app.job.JobScheduler; import android.content.ActivityNotFoundException; +import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; @@ -12,6 +15,7 @@ import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.os.Handler; +import android.os.PersistableBundle; import android.provider.MediaStore; import android.view.Menu; import android.view.MenuItem; @@ -587,7 +591,7 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee subredditName = binding.subredditNameTextViewPostGalleryActivity.getText().toString(); } - Intent intent = new Intent(this, SubmitPostService.class); + /*Intent intent = new Intent(this, SubmitPostService.class); intent.putExtra(SubmitPostService.EXTRA_ACCOUNT, selectedAccount); intent.putExtra(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName); intent.putExtra(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TYPE_GALLERY); @@ -601,7 +605,25 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee binding.receivePostReplyNotificationsSwitchMaterialPostGalleryActivity.isChecked(), flair, items); intent.putExtra(SubmitPostService.EXTRA_REDDIT_GALLERY_PAYLOAD, new Gson().toJson(payload)); - ContextCompat.startForegroundService(this, intent); + ContextCompat.startForegroundService(this, intent);*/ + + PersistableBundle extras = new PersistableBundle(); + extras.putString(SubmitPostService.EXTRA_ACCOUNT, selectedAccount.getJSONModel()); + extras.putString(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName); + extras.putInt(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TYPE_GALLERY); + ArrayList<RedditGalleryPayload.Item> items = new ArrayList<>(); + for (RedditGallerySubmissionRecyclerViewAdapter.RedditGalleryImageInfo i : redditGalleryImageInfoList) { + items.add(i.payload); + } + RedditGalleryPayload payload = new RedditGalleryPayload(subredditName, subredditIsUser ? "profile" : "subreddit", + binding.postTitleEditTextPostGalleryActivity.getText().toString(), + binding.postContentEditTextPostGalleryActivity.getText().toString(), isSpoiler, isNSFW, + binding.receivePostReplyNotificationsSwitchMaterialPostGalleryActivity.isChecked(), flair, items); + extras.putString(SubmitPostService.EXTRA_REDDIT_GALLERY_PAYLOAD, new Gson().toJson(payload)); + + // TODO: jobId and uploadBytes + JobInfo jobInfo = SubmitPostService.constructJobInfo(this, 1, 100, extras); + ((JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE)).schedule(jobInfo); return true; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostImageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostImageActivity.java index f821fafa..b7468d53 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostImageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostImageActivity.java @@ -1,6 +1,9 @@ package ml.docilealligator.infinityforreddit.activities; +import android.app.job.JobInfo; +import android.app.job.JobScheduler; import android.content.ActivityNotFoundException; +import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Resources; @@ -9,6 +12,7 @@ import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.os.Handler; +import android.os.PersistableBundle; import android.provider.MediaStore; import android.view.Menu; import android.view.MenuItem; @@ -16,7 +20,6 @@ import android.view.View; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.core.content.ContextCompat; import androidx.core.content.FileProvider; import androidx.recyclerview.widget.LinearLayoutManager; @@ -530,8 +533,8 @@ public class PostImageActivity extends BaseActivity implements FlairBottomSheetF subredditName = binding.subredditNameTextViewPostImageActivity.getText().toString(); } - Intent intent = new Intent(this, SubmitPostService.class); - intent.setData(imageUri); + /*Intent intent = new Intent(this, SubmitPostService.class); + intent.putExtra(SubmitPostService.EXTRA_MEDIA_URI, imageUri.toString()); intent.putExtra(SubmitPostService.EXTRA_ACCOUNT, selectedAccount); intent.putExtra(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName); intent.putExtra(SubmitPostService.EXTRA_TITLE, binding.postTitleEditTextPostImageActivity.getText().toString()); @@ -548,7 +551,30 @@ public class PostImageActivity extends BaseActivity implements FlairBottomSheetF } intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); - ContextCompat.startForegroundService(this, intent); + ContextCompat.startForegroundService(this, intent);*/ + + PersistableBundle extras = new PersistableBundle(); + extras.putString(SubmitPostService.EXTRA_MEDIA_URI, imageUri.toString()); + extras.putString(SubmitPostService.EXTRA_ACCOUNT, selectedAccount.getJSONModel()); + extras.putString(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName); + extras.putString(SubmitPostService.EXTRA_TITLE, binding.postTitleEditTextPostImageActivity.getText().toString()); + extras.putString(SubmitPostService.EXTRA_CONTENT, binding.postContentEditTextPostImageActivity.getText().toString()); + if (flair != null) { + extras.putString(SubmitPostService.EXTRA_FLAIR, flair.getJSONModel()); + } + extras.putInt(SubmitPostService.EXTRA_IS_SPOILER, isSpoiler ? 1 : 0); + extras.putInt(SubmitPostService.EXTRA_IS_NSFW, isNSFW ? 1 : 0); + extras.putInt(SubmitPostService.EXTRA_RECEIVE_POST_REPLY_NOTIFICATIONS, binding.receivePostReplyNotificationsSwitchMaterialPostImageActivity.isChecked() ? 1 : 0); + String mimeType = getContentResolver().getType(imageUri); + if (mimeType != null && mimeType.contains("gif")) { + extras.putInt(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TYPE_VIDEO); + } else { + extras.putInt(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TYPE_IMAGE); + } + + // TODO: jobId and uploadBytes + JobInfo jobInfo = SubmitPostService.constructJobInfo(this, 1, 100, extras); + ((JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE)).schedule(jobInfo); return true; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostLinkActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostLinkActivity.java index 85ef693b..1e4c72b5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostLinkActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostLinkActivity.java @@ -1,11 +1,15 @@ package ml.docilealligator.infinityforreddit.activities; +import android.app.job.JobInfo; +import android.app.job.JobScheduler; +import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Resources; import android.os.Build; import android.os.Bundle; import android.os.Handler; +import android.os.PersistableBundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -14,7 +18,6 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.core.content.ContextCompat; import androidx.recyclerview.widget.LinearLayoutManager; import com.bumptech.glide.Glide; @@ -526,7 +529,7 @@ public class PostLinkActivity extends BaseActivity implements FlairBottomSheetFr subredditName = binding.subredditNameTextViewPostLinkActivity.getText().toString(); } - Intent intent = new Intent(this, SubmitPostService.class); + /*Intent intent = new Intent(this, SubmitPostService.class); intent.putExtra(SubmitPostService.EXTRA_ACCOUNT, selectedAccount); intent.putExtra(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName); intent.putExtra(SubmitPostService.EXTRA_TITLE, binding.postTitleEditTextPostLinkActivity.getText().toString()); @@ -538,7 +541,26 @@ public class PostLinkActivity extends BaseActivity implements FlairBottomSheetFr intent.putExtra(SubmitPostService.EXTRA_IS_NSFW, isNSFW); intent.putExtra(SubmitPostService.EXTRA_RECEIVE_POST_REPLY_NOTIFICATIONS, binding.receivePostReplyNotificationsSwitchMaterialPostLinkActivity.isChecked()); intent.putExtra(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TEXT_OR_LINK); - ContextCompat.startForegroundService(this, intent); + ContextCompat.startForegroundService(this, intent);*/ + + PersistableBundle extras = new PersistableBundle(); + extras.putString(SubmitPostService.EXTRA_ACCOUNT, selectedAccount.getJSONModel()); + extras.putString(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName); + extras.putString(SubmitPostService.EXTRA_TITLE, binding.postTitleEditTextPostLinkActivity.getText().toString()); + extras.putString(SubmitPostService.EXTRA_CONTENT, binding.postContentEditTextPostLinkActivity.getText().toString()); + extras.putString(SubmitPostService.EXTRA_URL, binding.postLinkEditTextPostLinkActivity.getText().toString()); + extras.putString(SubmitPostService.EXTRA_KIND, APIUtils.KIND_LINK); + if (flair != null) { + extras.putString(SubmitPostService.EXTRA_FLAIR, flair.getJSONModel()); + } + extras.putInt(SubmitPostService.EXTRA_IS_SPOILER, isSpoiler ? 1 : 0); + extras.putInt(SubmitPostService.EXTRA_IS_NSFW, isNSFW ? 1 : 0); + extras.putInt(SubmitPostService.EXTRA_RECEIVE_POST_REPLY_NOTIFICATIONS, binding.receivePostReplyNotificationsSwitchMaterialPostLinkActivity.isChecked() ? 1 : 0); + extras.putInt(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TEXT_OR_LINK); + + // TODO: jobId and uploadBytes + JobInfo jobInfo = SubmitPostService.constructJobInfo(this, 1, 100, extras); + ((JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE)).schedule(jobInfo); return true; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostPollActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostPollActivity.java index 3a9905e8..4e65cc72 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostPollActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostPollActivity.java @@ -1,6 +1,9 @@ package ml.docilealligator.infinityforreddit.activities; +import android.app.job.JobInfo; +import android.app.job.JobScheduler; import android.content.ActivityNotFoundException; +import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.ColorStateList; @@ -12,6 +15,7 @@ import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.os.Handler; +import android.os.PersistableBundle; import android.provider.MediaStore; import android.view.Menu; import android.view.MenuItem; @@ -615,7 +619,7 @@ public class PostPollActivity extends BaseActivity implements FlairBottomSheetFr mPostingSnackbar.show(); - Intent intent = new Intent(this, SubmitPostService.class); + /*Intent intent = new Intent(this, SubmitPostService.class); intent.putExtra(SubmitPostService.EXTRA_ACCOUNT, selectedAccount); intent.putExtra(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName); intent.putExtra(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TYPE_POLL); @@ -648,7 +652,44 @@ public class PostPollActivity extends BaseActivity implements FlairBottomSheetFr } intent.putExtra(SubmitPostService.EXTRA_POLL_PAYLOAD, new Gson().toJson(payload)); - ContextCompat.startForegroundService(this, intent); + ContextCompat.startForegroundService(this, intent);*/ + + PersistableBundle extras = new PersistableBundle(); + extras.putString(SubmitPostService.EXTRA_ACCOUNT, selectedAccount.getJSONModel()); + extras.putString(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName); + extras.putInt(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TYPE_POLL); + + PollPayload payload; + if (!binding.postContentEditTextPostPollActivity.getText().toString().isEmpty()) { + if (uploadedImages.isEmpty()) { + payload = new PollPayload(subredditName, binding.postTitleEditTextPostPollActivity.getText().toString(), + optionList.toArray(new String[0]), (int) binding.votingLengthSliderPostPollActivity.getValue(), isNSFW, isSpoiler, flair, + null, binding.postContentEditTextPostPollActivity.getText().toString(), + binding.receivePostReplyNotificationsSwitchMaterialPostPollActivity.isChecked(), + subredditIsUser ? "profile" : "subreddit"); + } else { + try { + payload = new PollPayload(subredditName, binding.postTitleEditTextPostPollActivity.getText().toString(), + optionList.toArray(new String[0]), (int) binding.votingLengthSliderPostPollActivity.getValue(), isNSFW, isSpoiler, flair, + new RichTextJSONConverter().constructRichTextJSON(this, binding.postContentEditTextPostPollActivity.getText().toString(), uploadedImages), + null, binding.receivePostReplyNotificationsSwitchMaterialPostPollActivity.isChecked(), + subredditIsUser ? "profile" : "subreddit"); + } catch (JSONException e) { + Snackbar.make(binding.coordinatorLayoutPostPollActivity, R.string.convert_to_richtext_json_failed, Snackbar.LENGTH_SHORT).show(); + return; + } + } + } else { + payload = new PollPayload(subredditName, binding.postTitleEditTextPostPollActivity.getText().toString(), + optionList.toArray(new String[0]), (int) binding.votingLengthSliderPostPollActivity.getValue(), isNSFW, isSpoiler, flair, + binding.receivePostReplyNotificationsSwitchMaterialPostPollActivity.isChecked(), + subredditIsUser ? "profile" : "subreddit"); + } + extras.putString(SubmitPostService.EXTRA_POLL_PAYLOAD, new Gson().toJson(payload)); + + // TODO: jobId and uploadBytes + JobInfo jobInfo = SubmitPostService.constructJobInfo(this, 1, 100, extras); + ((JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE)).schedule(jobInfo); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostTextActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostTextActivity.java index c97820c1..09310393 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostTextActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostTextActivity.java @@ -1,6 +1,9 @@ package ml.docilealligator.infinityforreddit.activities; +import android.app.job.JobInfo; +import android.app.job.JobScheduler; import android.content.ActivityNotFoundException; +import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Resources; @@ -9,6 +12,7 @@ import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.os.Handler; +import android.os.PersistableBundle; import android.provider.MediaStore; import android.view.Menu; import android.view.MenuItem; @@ -17,7 +21,6 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.core.content.ContextCompat; import androidx.core.content.FileProvider; import androidx.recyclerview.widget.LinearLayoutManager; @@ -514,7 +517,7 @@ public class PostTextActivity extends BaseActivity implements FlairBottomSheetFr subredditName = binding.subredditNameTextViewPostTextActivity.getText().toString(); } - Intent intent = new Intent(this, SubmitPostService.class); + /*Intent intent = new Intent(this, SubmitPostService.class); intent.putExtra(SubmitPostService.EXTRA_ACCOUNT, selectedAccount); intent.putExtra(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName); intent.putExtra(SubmitPostService.EXTRA_TITLE, binding.postTitleEditTextPostTextActivity.getText().toString()); @@ -530,7 +533,30 @@ public class PostTextActivity extends BaseActivity implements FlairBottomSheetFr intent.putExtra(SubmitPostService.EXTRA_IS_NSFW, isNSFW); intent.putExtra(SubmitPostService.EXTRA_RECEIVE_POST_REPLY_NOTIFICATIONS, binding.receivePostReplyNotificationsSwitchMaterialPostTextActivity.isChecked()); intent.putExtra(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TEXT_OR_LINK); - ContextCompat.startForegroundService(this, intent); + ContextCompat.startForegroundService(this, intent);*/ + + PersistableBundle extras = new PersistableBundle(); + extras.putString(SubmitPostService.EXTRA_ACCOUNT, selectedAccount.getJSONModel()); + extras.putString(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName); + extras.putString(SubmitPostService.EXTRA_TITLE, binding.postTitleEditTextPostTextActivity.getText().toString()); + extras.putString(SubmitPostService.EXTRA_CONTENT, binding.postTextContentEditTextPostTextActivity.getText().toString()); + if (!uploadedImages.isEmpty()) { + extras.putInt(SubmitPostService.EXTRA_IS_RICHTEXT_JSON, 1); + extras.putString(SubmitPostService.EXTRA_UPLOADED_IMAGES, UploadedImage.getArrayListJSONModel(uploadedImages)); + } + + extras.putString(SubmitPostService.EXTRA_KIND, APIUtils.KIND_SELF); + if (flair != null) { + extras.putString(SubmitPostService.EXTRA_FLAIR, flair.getJSONModel()); + } + extras.putInt(SubmitPostService.EXTRA_IS_SPOILER, isSpoiler ? 1 : 0); + extras.putInt(SubmitPostService.EXTRA_IS_NSFW, isNSFW ? 1 : 0); + extras.putInt(SubmitPostService.EXTRA_RECEIVE_POST_REPLY_NOTIFICATIONS, binding.receivePostReplyNotificationsSwitchMaterialPostTextActivity.isChecked() ? 1 : 0); + extras.putInt(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TEXT_OR_LINK); + + // TODO: jobId and uploadBytes + JobInfo jobInfo = SubmitPostService.constructJobInfo(this, 1, 100, extras); + ((JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE)).schedule(jobInfo); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostVideoActivity.java index 692224b8..d33157ca 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostVideoActivity.java @@ -1,6 +1,9 @@ package ml.docilealligator.infinityforreddit.activities; +import android.app.job.JobInfo; +import android.app.job.JobScheduler; import android.content.ActivityNotFoundException; +import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Resources; @@ -8,6 +11,7 @@ import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.Handler; +import android.os.PersistableBundle; import android.provider.MediaStore; import android.view.Menu; import android.view.MenuItem; @@ -551,8 +555,8 @@ public class PostVideoActivity extends BaseActivity implements FlairBottomSheetF subredditName = binding.subredditNameTextViewPostVideoActivity.getText().toString(); } - Intent intent = new Intent(this, SubmitPostService.class); - intent.setData(videoUri); + /*Intent intent = new Intent(this, SubmitPostService.class); + intent.putExtra(SubmitPostService.EXTRA_MEDIA_URI, videoUri.toString()); intent.putExtra(SubmitPostService.EXTRA_ACCOUNT, selectedAccount); intent.putExtra(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName); intent.putExtra(SubmitPostService.EXTRA_TITLE, binding.postTitleEditTextPostVideoActivity.getText().toString()); @@ -564,7 +568,25 @@ public class PostVideoActivity extends BaseActivity implements FlairBottomSheetF intent.putExtra(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TYPE_VIDEO); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); - ContextCompat.startForegroundService(this, intent); + ContextCompat.startForegroundService(this, intent);*/ + + PersistableBundle extras = new PersistableBundle(); + extras.putString(SubmitPostService.EXTRA_MEDIA_URI, videoUri.toString()); + extras.putString(SubmitPostService.EXTRA_ACCOUNT, selectedAccount.getJSONModel()); + extras.putString(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName); + extras.putString(SubmitPostService.EXTRA_TITLE, binding.postTitleEditTextPostVideoActivity.getText().toString()); + extras.putString(SubmitPostService.EXTRA_CONTENT, binding.postContentEditTextPostVideoActivity.getText().toString()); + if (flair != null) { + extras.putString(SubmitPostService.EXTRA_FLAIR, flair.getJSONModel()); + } + extras.putInt(SubmitPostService.EXTRA_IS_SPOILER, isSpoiler ? 1 : 0); + extras.putInt(SubmitPostService.EXTRA_IS_NSFW, isNSFW ? 1 : 0); + extras.putInt(SubmitPostService.EXTRA_RECEIVE_POST_REPLY_NOTIFICATIONS, binding.receivePostReplyNotificationsSwitchMaterialPostVideoActivity.isChecked() ? 1 : 0); + extras.putInt(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TYPE_VIDEO); + + // TODO: jobId and uploadBytes + JobInfo jobInfo = SubmitPostService.constructJobInfo(this, 1, 100, extras); + ((JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE)).schedule(jobInfo); return true; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/services/SubmitPostService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/services/SubmitPostService.java index 72395162..9c24e77a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/services/SubmitPostService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/services/SubmitPostService.java @@ -1,21 +1,20 @@ package ml.docilealligator.infinityforreddit.services; import android.app.Notification; -import android.app.Service; -import android.content.Intent; +import android.app.job.JobInfo; +import android.app.job.JobParameters; +import android.app.job.JobService; +import android.content.ComponentName; +import android.content.Context; import android.content.SharedPreferences; import android.graphics.Bitmap; +import android.net.NetworkRequest; import android.net.Uri; -import android.os.Bundle; +import android.os.Build; import android.os.Handler; -import android.os.HandlerThread; -import android.os.IBinder; -import android.os.Looper; -import android.os.Message; -import android.os.Process; +import android.os.PersistableBundle; import android.widget.Toast; -import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.app.NotificationChannelCompat; import androidx.core.app.NotificationCompat; @@ -68,7 +67,7 @@ import okhttp3.OkHttpClient; import retrofit2.Response; import retrofit2.Retrofit; -public class SubmitPostService extends Service { +public class SubmitPostService extends JobService { public static final String EXTRA_ACCOUNT = "EA"; public static final String EXTRA_SUBREDDIT_NAME = "ESN"; public static final String EXTRA_TITLE = "ET"; @@ -84,14 +83,13 @@ public class SubmitPostService extends Service { public static final String EXTRA_IS_NSFW = "EIN"; public static final String EXTRA_RECEIVE_POST_REPLY_NOTIFICATIONS = "ERPRN"; public static final String EXTRA_POST_TYPE = "EPT"; + public static final String EXTRA_MEDIA_URI = "EMU"; public static final int EXTRA_POST_TEXT_OR_LINK = 0; public static final int EXTRA_POST_TYPE_IMAGE = 1; public static final int EXTRA_POST_TYPE_VIDEO = 2; public static final int EXTRA_POST_TYPE_GALLERY = 3; public static final int EXTRA_POST_TYPE_POLL = 4; public static final int EXTRA_POST_TYPE_CROSSPOST = 5; - - private static final String EXTRA_MEDIA_URI = "EMU"; @Inject @Named("no_oauth") Retrofit mRetrofit; @@ -114,49 +112,125 @@ public class SubmitPostService extends Service { @Inject Executor mExecutor; private Handler handler; - private ServiceHandler serviceHandler; public SubmitPostService() { } - @Override - public IBinder onBind(Intent intent) { - return null; + public static JobInfo constructJobInfo(Context context, int jobId, long uploadBytes, PersistableBundle extras) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { + return new JobInfo.Builder(jobId, new ComponentName(context, SubmitPostService.class)) + .setUserInitiated(true) + .setRequiredNetwork(new NetworkRequest.Builder().build()) + .setEstimatedNetworkBytes(0, uploadBytes) + .setExtras(extras) + .build(); + } else { + return new JobInfo.Builder(jobId, new ComponentName(context, SubmitPostService.class)) + .build(); + } } - // Handler that receives messages from the thread - private final class ServiceHandler extends Handler { - public ServiceHandler(Looper looper) { - super(looper); + @Override + public boolean onStartJob(JobParameters params) { + NotificationChannelCompat serviceChannel = + new NotificationChannelCompat.Builder( + NotificationUtils.CHANNEL_SUBMIT_POST, + NotificationManagerCompat.IMPORTANCE_LOW) + .setName(NotificationUtils.CHANNEL_SUBMIT_POST) + .build(); + + NotificationManagerCompat manager = NotificationManagerCompat.from(this); + manager.createNotificationChannel(serviceChannel); + + int randomNotificationIdOffset = new Random().nextInt(10000); + PersistableBundle bundle = params.getExtras(); + int postType = bundle.getInt(EXTRA_POST_TYPE, EXTRA_POST_TEXT_OR_LINK); + + if (postType == EXTRA_POST_TEXT_OR_LINK) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { + setNotification(params, + NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID + randomNotificationIdOffset, + createNotification(R.string.posting), + JobService.JOB_END_NOTIFICATION_POLICY_REMOVE); + } else { + manager.notify(NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID + randomNotificationIdOffset, + createNotification(R.string.posting)); + } + } else if (postType == EXTRA_POST_TYPE_CROSSPOST) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { + setNotification(params, + NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID + randomNotificationIdOffset, + createNotification(R.string.posting), + JobService.JOB_END_NOTIFICATION_POLICY_REMOVE); + } else { + manager.notify(NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID + randomNotificationIdOffset, + createNotification(R.string.posting)); + } + } else if (postType == EXTRA_POST_TYPE_IMAGE) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { + setNotification(params, + NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID + randomNotificationIdOffset, + createNotification(R.string.posting_image), + JobService.JOB_END_NOTIFICATION_POLICY_REMOVE); + } else { + manager.notify(NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID + randomNotificationIdOffset, + createNotification(R.string.posting_image)); + } + } else if (postType == EXTRA_POST_TYPE_VIDEO) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { + setNotification(params, + NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID + randomNotificationIdOffset, + createNotification(R.string.posting_video), + JobService.JOB_END_NOTIFICATION_POLICY_REMOVE); + } else { + manager.notify(NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID + randomNotificationIdOffset, + createNotification(R.string.posting_video)); + } + } else if (postType == EXTRA_POST_TYPE_GALLERY) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { + setNotification(params, + NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID + randomNotificationIdOffset, + createNotification(R.string.posting_gallery), + JobService.JOB_END_NOTIFICATION_POLICY_REMOVE); + } else { + manager.notify(NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID + randomNotificationIdOffset, + createNotification(R.string.posting_gallery)); + } + } else { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { + setNotification(params, + NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID + randomNotificationIdOffset, + createNotification(R.string.posting_poll), + JobService.JOB_END_NOTIFICATION_POLICY_REMOVE); + } else { + manager.notify(NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID + randomNotificationIdOffset, + createNotification(R.string.posting_poll)); + } } - @Override - public void handleMessage(@NonNull Message msg) { - super.handleMessage(msg); - Bundle bundle = msg.getData(); - Account account = bundle.getParcelable(EXTRA_ACCOUNT); + mExecutor.execute(() -> { + Account account = Account.fromJson(bundle.getString(EXTRA_ACCOUNT)); String subredditName = bundle.getString(EXTRA_SUBREDDIT_NAME); String title = bundle.getString(EXTRA_TITLE); - Flair flair = bundle.getParcelable(EXTRA_FLAIR); - boolean isSpoiler = bundle.getBoolean(EXTRA_IS_SPOILER, false); - boolean isNSFW = bundle.getBoolean(EXTRA_IS_NSFW, false); - boolean receivePostReplyNotifications = bundle.getBoolean(EXTRA_RECEIVE_POST_REPLY_NOTIFICATIONS, true); - int postType = bundle.getInt(EXTRA_POST_TYPE, EXTRA_POST_TEXT_OR_LINK); + Flair flair = Flair.fromJson(bundle.getString(EXTRA_FLAIR)); + boolean isSpoiler = bundle.getInt(EXTRA_IS_SPOILER, 0) == 1; + boolean isNSFW = bundle.getInt(EXTRA_IS_NSFW, 0) == 1; + boolean receivePostReplyNotifications = bundle.getInt(EXTRA_RECEIVE_POST_REPLY_NOTIFICATIONS, 1) == 1; Retrofit newAuthenticatorOauthRetrofit = mOauthRetrofit.newBuilder().client(new OkHttpClient.Builder().authenticator(new AnyAccountAccessTokenAuthenticator(mRetrofit, mRedditDataRoomDatabase, account, mCurrentAccountSharedPreferences)) - .connectTimeout(30, TimeUnit.SECONDS) - .readTimeout(30, TimeUnit.SECONDS) - .writeTimeout(30, TimeUnit.SECONDS) - .connectionPool(new ConnectionPool(0, 1, TimeUnit.NANOSECONDS)) - .build()) + .connectTimeout(30, TimeUnit.SECONDS) + .readTimeout(30, TimeUnit.SECONDS) + .writeTimeout(30, TimeUnit.SECONDS) + .connectionPool(new ConnectionPool(0, 1, TimeUnit.NANOSECONDS)) + .build()) .build(); if (postType == EXTRA_POST_TEXT_OR_LINK) { String content = bundle.getString(EXTRA_CONTENT, ""); - boolean isRichTextJSON = bundle.getBoolean(EXTRA_IS_RICHTEXT_JSON); + boolean isRichTextJSON = bundle.getInt(EXTRA_IS_RICHTEXT_JSON, 0) == 1; String kind = bundle.getString(EXTRA_KIND); if (isRichTextJSON) { - List<UploadedImage> uploadedImages = bundle.getParcelableArrayList(EXTRA_UPLOADED_IMAGES); + List<UploadedImage> uploadedImages = UploadedImage.fromListJson(bundle.getString(EXTRA_UPLOADED_IMAGES)); try { content = new RichTextJSONConverter().constructRichTextJSON(SubmitPostService.this, content, uploadedImages); } catch (JSONException e) { @@ -164,79 +238,39 @@ public class SubmitPostService extends Service { return; } } - submitTextOrLinkPost(newAuthenticatorOauthRetrofit, account, subredditName, title, content, + submitTextOrLinkPost(params, newAuthenticatorOauthRetrofit, account, subredditName, title, content, bundle.getString(EXTRA_URL), flair, isSpoiler, isNSFW, receivePostReplyNotifications, isRichTextJSON, kind); } else if (postType == EXTRA_POST_TYPE_CROSSPOST) { - submitCrosspost(mExecutor, handler, newAuthenticatorOauthRetrofit, account, subredditName, title, + submitCrosspost(params, mExecutor, handler, newAuthenticatorOauthRetrofit, account, subredditName, title, bundle.getString(EXTRA_CONTENT), flair, isSpoiler, isNSFW, receivePostReplyNotifications); } else if (postType == EXTRA_POST_TYPE_IMAGE) { Uri mediaUri = Uri.parse(bundle.getString(EXTRA_MEDIA_URI)); - submitImagePost(newAuthenticatorOauthRetrofit, account, mediaUri, subredditName, title, + submitImagePost(params, newAuthenticatorOauthRetrofit, account, mediaUri, subredditName, title, bundle.getString(EXTRA_CONTENT), flair, isSpoiler, isNSFW, receivePostReplyNotifications); } else if (postType == EXTRA_POST_TYPE_VIDEO) { Uri mediaUri = Uri.parse(bundle.getString(EXTRA_MEDIA_URI)); - submitVideoPost(newAuthenticatorOauthRetrofit, account, mediaUri, subredditName, title, + submitVideoPost(params, newAuthenticatorOauthRetrofit, account, mediaUri, subredditName, title, bundle.getString(EXTRA_CONTENT), flair, isSpoiler, isNSFW, receivePostReplyNotifications); } else if (postType == EXTRA_POST_TYPE_GALLERY) { - submitGalleryPost(newAuthenticatorOauthRetrofit, account, bundle.getString(EXTRA_REDDIT_GALLERY_PAYLOAD)); + submitGalleryPost(params, newAuthenticatorOauthRetrofit, account, bundle.getString(EXTRA_REDDIT_GALLERY_PAYLOAD)); } else { - submitPollPost(newAuthenticatorOauthRetrofit, account, bundle.getString(EXTRA_POLL_PAYLOAD)); + submitPollPost(params, newAuthenticatorOauthRetrofit, account, bundle.getString(EXTRA_POLL_PAYLOAD)); } - } + }); + + return true; } @Override - public void onCreate() { - ((Infinity) getApplication()).getAppComponent().inject(this); - // Start up the thread running the service. Note that we create a - // separate thread because the service normally runs in the process's - // main thread, which we don't want to block. We also make it - // background priority so CPU-intensive work doesn't disrupt our UI. - handler = new Handler(); - HandlerThread thread = new HandlerThread("ServiceStartArguments", - Process.THREAD_PRIORITY_BACKGROUND); - thread.start(); - - // Get the HandlerThread's Looper and use it for our Handler - serviceHandler = new ServiceHandler(thread.getLooper()); + public boolean onStopJob(JobParameters params) { + return false; } @Override - public int onStartCommand(Intent intent, int flags, int startId) { - NotificationChannelCompat serviceChannel = - new NotificationChannelCompat.Builder( - NotificationUtils.CHANNEL_SUBMIT_POST, - NotificationManagerCompat.IMPORTANCE_LOW) - .setName(NotificationUtils.CHANNEL_SUBMIT_POST) - .build(); - - NotificationManagerCompat manager = NotificationManagerCompat.from(this); - manager.createNotificationChannel(serviceChannel); - - int randomNotificationIdOffset = new Random().nextInt(10000); - int postType = intent.getIntExtra(EXTRA_POST_TYPE, EXTRA_POST_TEXT_OR_LINK); - Bundle bundle = intent.getExtras(); - - if (postType == EXTRA_POST_TEXT_OR_LINK) { - startForeground(NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID + randomNotificationIdOffset, createNotification(R.string.posting)); - } else if (postType == EXTRA_POST_TYPE_CROSSPOST) { - startForeground(NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID + randomNotificationIdOffset, createNotification(R.string.posting)); - } else if (postType == EXTRA_POST_TYPE_IMAGE) { - bundle.putString(EXTRA_MEDIA_URI, intent.getData().toString()); - startForeground(NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID + randomNotificationIdOffset, createNotification(R.string.posting_image)); - } else if (postType == EXTRA_POST_TYPE_VIDEO) { - bundle.putString(EXTRA_MEDIA_URI, intent.getData().toString()); - startForeground(NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID + randomNotificationIdOffset, createNotification(R.string.posting_video)); - } else { - startForeground(NotificationUtils.SUBMIT_POST_SERVICE_NOTIFICATION_ID + randomNotificationIdOffset, createNotification(R.string.posting_gallery)); - } - - Message msg = serviceHandler.obtainMessage(); - msg.setData(bundle); - serviceHandler.sendMessage(msg); - - return START_NOT_STICKY; + public void onCreate() { + ((Infinity) getApplication()).getAppComponent().inject(this); + handler = new Handler(); } private Notification createNotification(int stringResId) { @@ -248,7 +282,7 @@ public class SubmitPostService extends Service { .build(); } - private void submitTextOrLinkPost(Retrofit newAuthenticatorOauthRetrofit, Account selectedAccount, + private void submitTextOrLinkPost(JobParameters parameters, Retrofit newAuthenticatorOauthRetrofit, Account selectedAccount, String subredditName, String title, String content, @Nullable String url, Flair flair, boolean isSpoiler, boolean isNSFW, boolean receivePostReplyNotifications, boolean isRichtextJSON, @@ -260,19 +294,19 @@ public class SubmitPostService extends Service { public void submitSuccessful(Post post) { handler.post(() -> EventBus.getDefault().post(new SubmitTextOrLinkPostEvent(true, post, null))); - stopService(); + jobFinished(parameters, false); } @Override public void submitFailed(@Nullable String errorMessage) { handler.post(() -> EventBus.getDefault().post(new SubmitTextOrLinkPostEvent(false, null, errorMessage))); - stopService(); + jobFinished(parameters, false); } }); } - private void submitCrosspost(Executor executor, Handler handler, Retrofit newAuthenticatorOauthRetrofit, + private void submitCrosspost(JobParameters parameters, Executor executor, Handler handler, Retrofit newAuthenticatorOauthRetrofit, Account selectedAccount, String subredditName, String title, String content, Flair flair, boolean isSpoiler, boolean isNSFW, boolean receivePostReplyNotifications) { @@ -283,19 +317,19 @@ public class SubmitPostService extends Service { public void submitSuccessful(Post post) { handler.post(() -> EventBus.getDefault().post(new SubmitCrosspostEvent(true, post, null))); - stopService(); + jobFinished(parameters, false); } @Override public void submitFailed(@Nullable String errorMessage) { handler.post(() -> EventBus.getDefault().post(new SubmitCrosspostEvent(false, null, errorMessage))); - stopService(); + jobFinished(parameters, false); } }); } - private void submitImagePost(Retrofit newAuthenticatorOauthRetrofit, Account selectedAccount, Uri mediaUri, + private void submitImagePost(JobParameters parameters, Retrofit newAuthenticatorOauthRetrofit, Account selectedAccount, Uri mediaUri, String subredditName, String title, String content, Flair flair, boolean isSpoiler, boolean isNSFW, boolean receivePostReplyNotifications) { try { @@ -310,24 +344,24 @@ public class SubmitPostService extends Service { Toast.makeText(SubmitPostService.this, R.string.image_is_processing, Toast.LENGTH_SHORT).show(); }); - stopService(); + jobFinished(parameters, false); } @Override public void submitFailed(@Nullable String errorMessage) { handler.post(() -> EventBus.getDefault().post(new SubmitImagePostEvent(false, errorMessage))); - stopService(); + jobFinished(parameters, false); } }); } catch (ExecutionException | InterruptedException e) { e.printStackTrace(); handler.post(() -> EventBus.getDefault().post(new SubmitImagePostEvent(false, getString(R.string.error_processing_image)))); - stopService(); + jobFinished(parameters, false); } } - private void submitVideoPost(Retrofit newAuthenticatorOauthRetrofit, Account selectedAccount, Uri mediaUri, + private void submitVideoPost(JobParameters parameters, Retrofit newAuthenticatorOauthRetrofit, Account selectedAccount, Uri mediaUri, String subredditName, String title, String content, Flair flair, boolean isSpoiler, boolean isNSFW, boolean receivePostReplyNotifications) { try { @@ -361,30 +395,30 @@ public class SubmitPostService extends Service { }); - stopService(); + jobFinished(parameters, false); } @Override public void submitFailed(@Nullable String errorMessage) { handler.post(() -> EventBus.getDefault().post(new SubmitVideoOrGifPostEvent(false, false, errorMessage))); - stopService(); + jobFinished(parameters, false); } }); } else { handler.post(() -> EventBus.getDefault().post(new SubmitVideoOrGifPostEvent(false, true, null))); - stopService(); + jobFinished(parameters, false); } } catch (IOException | InterruptedException | ExecutionException e) { e.printStackTrace(); handler.post(() -> EventBus.getDefault().post(new SubmitVideoOrGifPostEvent(false, true, null))); - stopService(); + jobFinished(parameters, false); } } - private void submitGalleryPost(Retrofit newAuthenticatorOauthRetrofit, Account selectedAccount, String payload) { + private void submitGalleryPost(JobParameters parameters, Retrofit newAuthenticatorOauthRetrofit, Account selectedAccount, String payload) { try { Response<String> response = newAuthenticatorOauthRetrofit.create(RedditAPI.class).submitGalleryPost(APIUtils.getOAuthHeader(selectedAccount.getAccessToken()), payload).execute(); if (response.isSuccessful()) { @@ -416,11 +450,11 @@ public class SubmitPostService extends Service { e.printStackTrace(); handler.post(() -> EventBus.getDefault().post(new SubmitGalleryPostEvent(false, null, e.getMessage()))); } finally { - stopService(); + jobFinished(parameters, false); } } - private void submitPollPost(Retrofit newAuthenticatorOauthRetrofit, Account selectedAccount, String payload) { + private void submitPollPost(JobParameters parameters, Retrofit newAuthenticatorOauthRetrofit, Account selectedAccount, String payload) { try { Response<String> response = newAuthenticatorOauthRetrofit.create(RedditAPI.class).submitPollPost(APIUtils.getOAuthHeader(selectedAccount.getAccessToken()), payload).execute(); if (response.isSuccessful()) { @@ -452,7 +486,7 @@ public class SubmitPostService extends Service { e.printStackTrace(); handler.post(() -> EventBus.getDefault().post(new SubmitPollPostEvent(false, null, e.getMessage()))); } finally { - stopService(); + jobFinished(parameters, false); } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5f772352..9d6673ed 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -308,6 +308,7 @@ <string name="posting_video">Posting Video</string> <string name="posting_image">Posting Image</string> <string name="posting_gallery">Posting Gallery</string> + <string name="posting_poll">Posting Poll</string> <string name="please_wait">Please wait</string> <string name="add_account">Add an account</string> |