-
Notifications
You must be signed in to change notification settings - Fork 660
Android 34 WorkManager Scheduler #6221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 6 commits
1f16596
dab5584
1cb0ed4
ca0644a
7c99278
3a4cb1c
15c5c90
bd1ad49
0564a10
1a47878
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| // Copyright 2024 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package com.google.android.datatransport.runtime.scheduling.jobscheduling; | ||
|
|
||
| import static android.util.Base64.DEFAULT; | ||
| import static android.util.Base64.encodeToString; | ||
|
|
||
| import android.content.Context; | ||
| import android.os.Build; | ||
| import androidx.annotation.RequiresApi; | ||
| import androidx.work.Data; | ||
| import androidx.work.OneTimeWorkRequest; | ||
| import androidx.work.WorkInfo; | ||
| import androidx.work.WorkManager; | ||
| import androidx.work.WorkRequest; | ||
| import com.google.android.datatransport.runtime.TransportContext; | ||
| import com.google.android.datatransport.runtime.logging.Logging; | ||
| import com.google.android.datatransport.runtime.scheduling.persistence.EventStore; | ||
| import com.google.android.datatransport.runtime.util.PriorityMapping; | ||
| import java.util.Map; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| @RequiresApi(api = Build.VERSION_CODES.UPSIDE_DOWN_CAKE) | ||
| public class WorkManagerScheduler implements WorkScheduler { | ||
emilypgoogle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private static final String LOG_TAG = "WorkManagerScheduler"; | ||
|
|
||
| static final String ATTEMPT_NUMBER = "attemptNumber"; | ||
| static final String BACKEND_NAME = "backendName"; | ||
| static final String EVENT_PRIORITY = "priority"; | ||
| static final String EXTRAS = "extras"; | ||
| private final Context context; | ||
|
|
||
| private final EventStore eventStore; | ||
|
|
||
| private final SchedulerConfig config; | ||
|
|
||
| public WorkManagerScheduler( | ||
| Context applicationContext, EventStore eventStore, SchedulerConfig config) { | ||
| this.context = applicationContext; | ||
| this.eventStore = eventStore; | ||
| this.config = config; | ||
| } | ||
|
|
||
| @Override | ||
| public void schedule(TransportContext transportContext, int attemptNumber) { | ||
| schedule(transportContext, attemptNumber, false); | ||
| } | ||
|
|
||
| @Override | ||
| public void schedule(TransportContext transportContext, int attemptNumber, boolean force) { | ||
| WorkManager manager = WorkManager.getInstance(context); | ||
|
|
||
| int jobId = WorkScheduler.getJobId(context, transportContext); | ||
| if (!force) { | ||
| try { | ||
| for (WorkInfo info : manager.getWorkInfosByTag("transport-" + jobId).get()) { | ||
emilypgoogle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (!info.getState().isFinished()) { | ||
| Logging.d( | ||
| LOG_TAG, | ||
| "Upload for context %s is already scheduled. Returning...", | ||
| transportContext); | ||
| return; | ||
| } | ||
| } | ||
| } catch (Exception e) { | ||
emilypgoogle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| Data.Builder dataBuilder = new Data.Builder(); | ||
| dataBuilder.putInt(ATTEMPT_NUMBER, attemptNumber); | ||
| dataBuilder.putString(BACKEND_NAME, transportContext.getBackendName()); | ||
| dataBuilder.putInt(EVENT_PRIORITY, PriorityMapping.toInt(transportContext.getPriority())); | ||
emilypgoogle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (transportContext.getExtras() != null) { | ||
| dataBuilder.putString(EXTRAS, encodeToString(transportContext.getExtras(), DEFAULT)); | ||
| } | ||
|
|
||
| long backendTime = eventStore.getNextCallTime(transportContext); | ||
| boolean hasPendingEvents = force && eventStore.hasPendingEventsFor(transportContext); | ||
|
|
||
| long scheduleDelay = | ||
| config.getScheduleDelay( | ||
| transportContext.getPriority(), backendTime, attemptNumber, hasPendingEvents); | ||
|
|
||
| Logging.d( | ||
| LOG_TAG, | ||
| "Scheduling upload for context %s in %dms(Backend next call timestamp %d). Attempt %d", | ||
| transportContext, | ||
| scheduleDelay, | ||
| backendTime, | ||
| attemptNumber); | ||
|
|
||
| WorkRequest request = | ||
| new OneTimeWorkRequest.Builder(WorkManagerSchedulerWorker.class) | ||
| .setInitialDelay(scheduleDelay, TimeUnit.MILLISECONDS) | ||
| .setInputData(dataBuilder.build()) | ||
| .addTag("transport-" + jobId) | ||
| .build(); | ||
| manager.enqueue(request); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // Copyright 2024 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package com.google.android.datatransport.runtime.scheduling.jobscheduling; | ||
|
|
||
| import android.content.Context; | ||
| import android.os.Build; | ||
| import android.util.Base64; | ||
| import androidx.annotation.NonNull; | ||
| import androidx.annotation.RequiresApi; | ||
| import androidx.work.Data; | ||
| import androidx.work.Worker; | ||
| import androidx.work.WorkerParameters; | ||
| import com.google.android.datatransport.runtime.TransportContext; | ||
| import com.google.android.datatransport.runtime.TransportRuntime; | ||
| import com.google.android.datatransport.runtime.util.PriorityMapping; | ||
|
|
||
| @RequiresApi(api = Build.VERSION_CODES.UPSIDE_DOWN_CAKE) | ||
| public class WorkManagerSchedulerWorker extends Worker { | ||
|
|
||
| public WorkManagerSchedulerWorker( | ||
| @NonNull Context context, @NonNull WorkerParameters workerParams) { | ||
| super(context, workerParams); | ||
| } | ||
|
|
||
| @NonNull | ||
| @Override | ||
| public Result doWork() { | ||
| Data data = getInputData(); | ||
| String backendName = data.getString(JobInfoScheduler.BACKEND_NAME); | ||
| String extras = data.getString(JobInfoScheduler.EXTRAS); | ||
|
|
||
| int priority = data.getInt(JobInfoScheduler.EVENT_PRIORITY, 0); | ||
| int attemptNumber = data.getInt(JobInfoScheduler.ATTEMPT_NUMBER, 0); | ||
| TransportRuntime.initialize(getApplicationContext()); | ||
| TransportContext.Builder transportContext = | ||
| TransportContext.builder() | ||
| .setBackendName(backendName) | ||
| .setPriority(PriorityMapping.valueOf(priority)); | ||
|
|
||
| if (extras != null) { | ||
| transportContext.setExtras(Base64.decode(extras, Base64.DEFAULT)); | ||
| } | ||
|
|
||
| TransportRuntime.getInstance() | ||
| .getUploader() | ||
| .upload(transportContext.build(), attemptNumber, () -> {}); | ||
| return Result.success(); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.