From 7443cf80953e65c95fae0f3edbadbfcb100f4748 Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Thu, 18 Jul 2024 16:45:11 -0400 Subject: Continue migrating SubmitPostService to JobService. Estimate network bytes. --- .../activities/PostGalleryActivity.java | 6 ++++-- .../activities/PostImageActivity.java | 15 ++++++++++++--- .../activities/PostLinkActivity.java | 19 +++++++++++++++---- .../activities/PostPollActivity.java | 8 +++++--- .../activities/PostTextActivity.java | 18 ++++++++++++++---- .../activities/PostVideoActivity.java | 16 ++++++++++++---- .../infinityforreddit/services/SubmitPostService.java | 10 ++++++---- 7 files changed, 68 insertions(+), 24 deletions(-) (limited to 'app/src/main') 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 cfef82a9..fc33566e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostGalleryActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostGalleryActivity.java @@ -619,10 +619,12 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee 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)); + + String payloadJSON = new Gson().toJson(payload); + extras.putString(SubmitPostService.EXTRA_REDDIT_GALLERY_PAYLOAD, payloadJSON); // TODO: jobId and uploadBytes - JobInfo jobInfo = SubmitPostService.constructJobInfo(this, 1, 100, extras); + JobInfo jobInfo = SubmitPostService.constructJobInfo(this, payloadJSON.length() * 2L, 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 b7468d53..353a599a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostImageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostImageActivity.java @@ -553,12 +553,21 @@ public class PostImageActivity extends BaseActivity implements FlairBottomSheetF ContextCompat.startForegroundService(this, intent);*/ + int contentEstimatedBytes = 0; PersistableBundle extras = new PersistableBundle(); + //TODO estimate image size 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()); + + String title = binding.postTitleEditTextPostImageActivity.getText().toString(); + contentEstimatedBytes += title.length() * 2; + extras.putString(SubmitPostService.EXTRA_TITLE, title); + + String content = binding.postContentEditTextPostImageActivity.getText().toString(); + contentEstimatedBytes += content.length() * 2; + extras.putString(SubmitPostService.EXTRA_CONTENT, content); + if (flair != null) { extras.putString(SubmitPostService.EXTRA_FLAIR, flair.getJSONModel()); } @@ -573,7 +582,7 @@ public class PostImageActivity extends BaseActivity implements FlairBottomSheetF } // TODO: jobId and uploadBytes - JobInfo jobInfo = SubmitPostService.constructJobInfo(this, 1, 100, extras); + JobInfo jobInfo = SubmitPostService.constructJobInfo(this, contentEstimatedBytes, 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 1e4c72b5..a4201dc1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostLinkActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostLinkActivity.java @@ -543,12 +543,23 @@ public class PostLinkActivity extends BaseActivity implements FlairBottomSheetFr intent.putExtra(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TEXT_OR_LINK); ContextCompat.startForegroundService(this, intent);*/ + int contentEstimatedBytes = 0; 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()); + + String title = binding.postTitleEditTextPostLinkActivity.getText().toString(); + contentEstimatedBytes += title.length() * 2; + extras.putString(SubmitPostService.EXTRA_TITLE, title); + + String content = binding.postContentEditTextPostLinkActivity.getText().toString(); + contentEstimatedBytes += content.length() * 2; + extras.putString(SubmitPostService.EXTRA_CONTENT, content); + + String link = binding.postLinkEditTextPostLinkActivity.getText().toString(); + contentEstimatedBytes += link.length() * 2; + extras.putString(SubmitPostService.EXTRA_URL, link); + extras.putString(SubmitPostService.EXTRA_KIND, APIUtils.KIND_LINK); if (flair != null) { extras.putString(SubmitPostService.EXTRA_FLAIR, flair.getJSONModel()); @@ -559,7 +570,7 @@ public class PostLinkActivity extends BaseActivity implements FlairBottomSheetFr extras.putInt(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TEXT_OR_LINK); // TODO: jobId and uploadBytes - JobInfo jobInfo = SubmitPostService.constructJobInfo(this, 1, 100, extras); + JobInfo jobInfo = SubmitPostService.constructJobInfo(this, contentEstimatedBytes, 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 4e65cc72..34437cdc 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostPollActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostPollActivity.java @@ -671,7 +671,8 @@ public class PostPollActivity extends BaseActivity implements FlairBottomSheetFr 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), + new RichTextJSONConverter().constructRichTextJSON(this, + binding.postContentEditTextPostPollActivity.getText().toString(), uploadedImages), null, binding.receivePostReplyNotificationsSwitchMaterialPostPollActivity.isChecked(), subredditIsUser ? "profile" : "subreddit"); } catch (JSONException e) { @@ -685,10 +686,11 @@ public class PostPollActivity extends BaseActivity implements FlairBottomSheetFr binding.receivePostReplyNotificationsSwitchMaterialPostPollActivity.isChecked(), subredditIsUser ? "profile" : "subreddit"); } - extras.putString(SubmitPostService.EXTRA_POLL_PAYLOAD, new Gson().toJson(payload)); + String payloadJSON = new Gson().toJson(payload); + extras.putString(SubmitPostService.EXTRA_POLL_PAYLOAD, payloadJSON); // TODO: jobId and uploadBytes - JobInfo jobInfo = SubmitPostService.constructJobInfo(this, 1, 100, extras); + JobInfo jobInfo = SubmitPostService.constructJobInfo(this, payloadJSON.length() * 2L, extras); ((JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE)).schedule(jobInfo); } 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 09310393..9fab8566 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostTextActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostTextActivity.java @@ -535,14 +535,24 @@ public class PostTextActivity extends BaseActivity implements FlairBottomSheetFr intent.putExtra(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TEXT_OR_LINK); ContextCompat.startForegroundService(this, intent);*/ + int contentEstimatedBytes = 0; 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()); + + String title = binding.postTitleEditTextPostTextActivity.getText().toString(); + contentEstimatedBytes += title.length() * 2; + extras.putString(SubmitPostService.EXTRA_TITLE, title); + + String content = binding.postTextContentEditTextPostTextActivity.getText().toString(); + contentEstimatedBytes += content.length() * 2; + extras.putString(SubmitPostService.EXTRA_CONTENT, content); + if (!uploadedImages.isEmpty()) { extras.putInt(SubmitPostService.EXTRA_IS_RICHTEXT_JSON, 1); - extras.putString(SubmitPostService.EXTRA_UPLOADED_IMAGES, UploadedImage.getArrayListJSONModel(uploadedImages)); + String uploadedImagesJSON = UploadedImage.getArrayListJSONModel(uploadedImages); + contentEstimatedBytes += uploadedImagesJSON.length() * 2; + extras.putString(SubmitPostService.EXTRA_UPLOADED_IMAGES, uploadedImagesJSON); } extras.putString(SubmitPostService.EXTRA_KIND, APIUtils.KIND_SELF); @@ -555,7 +565,7 @@ public class PostTextActivity extends BaseActivity implements FlairBottomSheetFr extras.putInt(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TEXT_OR_LINK); // TODO: jobId and uploadBytes - JobInfo jobInfo = SubmitPostService.constructJobInfo(this, 1, 100, extras); + JobInfo jobInfo = SubmitPostService.constructJobInfo(this, contentEstimatedBytes, extras); ((JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE)).schedule(jobInfo); } 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 d33157ca..20287832 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostVideoActivity.java @@ -21,7 +21,6 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.OptIn; -import androidx.core.content.ContextCompat; import androidx.media3.common.MediaItem; import androidx.media3.common.Player; import androidx.media3.common.util.UnstableApi; @@ -570,12 +569,21 @@ public class PostVideoActivity extends BaseActivity implements FlairBottomSheetF ContextCompat.startForegroundService(this, intent);*/ + int contentEstimatedBytes = 0; PersistableBundle extras = new PersistableBundle(); + //TODO estimate video size 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()); + + String title = binding.postTitleEditTextPostVideoActivity.getText().toString(); + contentEstimatedBytes += title.length() * 2; + extras.putString(SubmitPostService.EXTRA_TITLE, title); + + String content = binding.postContentEditTextPostVideoActivity.getText().toString(); + contentEstimatedBytes += content.length() * 2; + extras.putString(SubmitPostService.EXTRA_CONTENT, content); + if (flair != null) { extras.putString(SubmitPostService.EXTRA_FLAIR, flair.getJSONModel()); } @@ -585,7 +593,7 @@ public class PostVideoActivity extends BaseActivity implements FlairBottomSheetF extras.putInt(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TYPE_VIDEO); // TODO: jobId and uploadBytes - JobInfo jobInfo = SubmitPostService.constructJobInfo(this, 1, 100, extras); + JobInfo jobInfo = SubmitPostService.constructJobInfo(this, contentEstimatedBytes, 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 9c24e77a..6be41292 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/services/SubmitPostService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/services/SubmitPostService.java @@ -90,6 +90,8 @@ public class SubmitPostService extends JobService { 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 int JOB_ID = 1000; + @Inject @Named("no_oauth") Retrofit mRetrofit; @@ -116,16 +118,16 @@ public class SubmitPostService extends JobService { public SubmitPostService() { } - public static JobInfo constructJobInfo(Context context, int jobId, long uploadBytes, PersistableBundle extras) { + public static JobInfo constructJobInfo(Context context, long contentEstimatedBytes, PersistableBundle extras) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { - return new JobInfo.Builder(jobId, new ComponentName(context, SubmitPostService.class)) + return new JobInfo.Builder(JOB_ID++, new ComponentName(context, SubmitPostService.class)) .setUserInitiated(true) .setRequiredNetwork(new NetworkRequest.Builder().build()) - .setEstimatedNetworkBytes(0, uploadBytes) + .setEstimatedNetworkBytes(0, contentEstimatedBytes + 500) .setExtras(extras) .build(); } else { - return new JobInfo.Builder(jobId, new ComponentName(context, SubmitPostService.class)) + return new JobInfo.Builder(JOB_ID++, new ComponentName(context, SubmitPostService.class)) .build(); } } -- cgit v1.2.3