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.server.companion.association;
18 
19 import static java.util.concurrent.TimeUnit.DAYS;
20 
21 import android.app.job.JobInfo;
22 import android.app.job.JobParameters;
23 import android.app.job.JobScheduler;
24 import android.app.job.JobService;
25 import android.content.ComponentName;
26 import android.content.Context;
27 import android.util.Slog;
28 
29 import com.android.server.LocalServices;
30 import com.android.server.companion.CompanionDeviceManagerServiceInternal;
31 
32 /**
33  * A Job Service responsible for clean up self-managed associations if it's idle for 90 days.
34  *
35  * The job will be executed only if the device is charging and in idle mode due to the application
36  * will be killed if association/role are revoked. See {@link DisassociationProcessor}
37  */
38 public class InactiveAssociationsRemovalService extends JobService {
39 
40     private static final String TAG = "CDM_InactiveAssociationsRemovalService";
41     private static final String JOB_NAMESPACE = "companion";
42     private static final int JOB_ID = 1;
43     private static final long ONE_DAY_INTERVAL = DAYS.toMillis(1);
44 
45     @Override
onStartJob(final JobParameters params)46     public boolean onStartJob(final JobParameters params) {
47         Slog.i(TAG, "Execute the Association Removal job");
48 
49         LocalServices.getService(CompanionDeviceManagerServiceInternal.class)
50                 .removeInactiveSelfManagedAssociations();
51 
52         jobFinished(params, false);
53         return true;
54     }
55 
56     @Override
onStopJob(final JobParameters params)57     public boolean onStopJob(final JobParameters params) {
58         Slog.i(TAG, "Association removal job stopped; id=" + params.getJobId()
59                 + ", reason="
60                 + JobParameters.getInternalReasonCodeDescription(
61                 params.getInternalStopReasonCode()));
62         return false;
63     }
64 
65     /**
66      * Schedule this job.
67      */
schedule(Context context)68     public static void schedule(Context context) {
69         Slog.i(TAG, "Scheduling the Association Removal job");
70         final JobScheduler jobScheduler =
71                 context.getSystemService(JobScheduler.class).forNamespace(JOB_NAMESPACE);
72         final JobInfo job = new JobInfo.Builder(JOB_ID,
73                 new ComponentName(context, InactiveAssociationsRemovalService.class))
74                 .setRequiresCharging(true)
75                 .setRequiresDeviceIdle(true)
76                 .setPeriodic(ONE_DAY_INTERVAL)
77                 .build();
78         jobScheduler.schedule(job);
79     }
80 }
81