• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.ondevicepersonalization.services.download.mdd;
18 
19 import static com.android.ondevicepersonalization.services.download.mdd.MddTaskScheduler.MDD_TASK_TAG_KEY;
20 
21 import static com.google.android.libraries.mobiledatadownload.TaskScheduler.WIFI_CHARGING_PERIODIC_TASK;
22 
23 import android.app.job.JobParameters;
24 import android.app.job.JobService;
25 import android.content.Context;
26 import android.os.PersistableBundle;
27 import android.util.Log;
28 
29 import com.android.ondevicepersonalization.services.OnDevicePersonalizationExecutors;
30 import com.android.ondevicepersonalization.services.download.OnDevicePersonalizationDownloadProcessingJobService;
31 
32 import com.google.android.libraries.mobiledatadownload.tracing.PropagatedFutures;
33 import com.google.common.util.concurrent.FutureCallback;
34 import com.google.common.util.concurrent.Futures;
35 import com.google.common.util.concurrent.ListenableFuture;
36 
37 /**
38  * MDD JobService. This will download MDD files in background tasks.
39  */
40 public class MddJobService extends JobService {
41     private static final String TAG = "MddJobService";
42 
43     private String mMddTaskTag;
44 
45     @Override
onStartJob(JobParameters params)46     public boolean onStartJob(JobParameters params) {
47         Log.d(TAG, "onStartJob()");
48 
49         // Get the mddTaskTag from input.
50         PersistableBundle extras = params.getExtras();
51         if (null == extras) {
52             Log.e(TAG, "can't find MDD task tag");
53             throw new IllegalArgumentException("Can't find MDD Tasks Tag!");
54         }
55         mMddTaskTag = extras.getString(MDD_TASK_TAG_KEY);
56 
57         ListenableFuture<Void> handleTaskFuture =
58                 PropagatedFutures.submitAsync(
59                         () -> MobileDataDownloadFactory.getMdd(this).handleTask(mMddTaskTag),
60                         OnDevicePersonalizationExecutors.getBackgroundExecutor());
61 
62         Context context = this;
63         Futures.addCallback(
64                 handleTaskFuture,
65                 new FutureCallback<Void>() {
66                     @Override
67                     public void onSuccess(Void result) {
68                         Log.d(TAG, "MddJobService.MddHandleTask succeeded!");
69                         OnDevicePersonalizationDownloadProcessingJobService.schedule(context);
70                         // Tell the JobScheduler that the job has completed and does not needs to be
71                         // rescheduled.
72                         if (WIFI_CHARGING_PERIODIC_TASK.equals(mMddTaskTag)) {
73                             jobFinished(params, /* wantsReschedule = */ false);
74                         }
75                     }
76 
77                     @Override
78                     public void onFailure(Throwable t) {
79                         Log.e(TAG, "Failed to handle JobService: " + params.getJobId(), t);
80                         //  When failure, also tell the JobScheduler that the job has completed and
81                         // does not need to be rescheduled.
82                         jobFinished(params, /* wantsReschedule = */ false);
83                     }
84                 },
85                 OnDevicePersonalizationExecutors.getBackgroundExecutor());
86 
87         return true;
88     }
89 
90     @Override
onStopJob(JobParameters params)91     public boolean onStopJob(JobParameters params) {
92         // Attempt to process any data downloaded before the worker was stopped.
93         if (WIFI_CHARGING_PERIODIC_TASK.equals(mMddTaskTag)) {
94             jobFinished(params, /* wantsReschedule = */ false);
95         }
96         // Reschedule the job since it ended before finishing
97         return true;
98     }
99 }
100