1 /*
2  * Copyright 2020 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 package com.android.server.blob;
17 
18 import static com.android.server.blob.BlobStoreConfig.IDLE_JOB_ID;
19 import static com.android.server.blob.BlobStoreConfig.LOGV;
20 import static com.android.server.blob.BlobStoreConfig.TAG;
21 
22 import android.app.job.JobInfo;
23 import android.app.job.JobParameters;
24 import android.app.job.JobScheduler;
25 import android.app.job.JobService;
26 import android.content.ComponentName;
27 import android.content.Context;
28 import android.os.AsyncTask;
29 import android.util.Slog;
30 
31 import com.android.server.LocalServices;
32 
33 /**
34  * Maintenance job to clean up stale sessions and blobs.
35  */
36 public class BlobStoreIdleJobService extends JobService {
37     @Override
onStartJob(final JobParameters params)38     public boolean onStartJob(final JobParameters params) {
39         AsyncTask.execute(() -> {
40             final BlobStoreManagerInternal blobStoreManagerInternal = LocalServices.getService(
41                     BlobStoreManagerInternal.class);
42             blobStoreManagerInternal.onIdleMaintenance();
43             jobFinished(params, false);
44         });
45         return true;
46     }
47 
48     @Override
onStopJob(final JobParameters params)49     public boolean onStopJob(final JobParameters params) {
50         Slog.d(TAG, "Idle maintenance job is stopped; id=" + params.getJobId()
51                 + ", reason="
52                 + JobParameters.getInternalReasonCodeDescription(
53                         params.getInternalStopReasonCode()));
54         return false;
55     }
56 
schedule(Context context)57     static void schedule(Context context) {
58         final JobScheduler jobScheduler = (JobScheduler) context.getSystemService(
59                 Context.JOB_SCHEDULER_SERVICE);
60         final JobInfo job = new JobInfo.Builder(IDLE_JOB_ID,
61                 new ComponentName(context, BlobStoreIdleJobService.class))
62                         .setRequiresDeviceIdle(true)
63                         .setRequiresCharging(true)
64                         .setPeriodic(BlobStoreConfig.getIdleJobPeriodMs())
65                         .build();
66         jobScheduler.schedule(job);
67         if (LOGV) {
68             Slog.v(TAG, "Scheduling the idle maintenance job");
69         }
70     }
71 }
72