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;
18 
19 import static com.android.server.companion.CompanionDeviceManagerService.TAG;
20 
21 import static java.util.concurrent.TimeUnit.DAYS;
22 
23 import android.app.job.JobInfo;
24 import android.app.job.JobParameters;
25 import android.app.job.JobScheduler;
26 import android.app.job.JobService;
27 import android.content.ComponentName;
28 import android.content.Context;
29 import android.util.Slog;
30 
31 import com.android.server.LocalServices;
32 
33 /**
34  * A Job Service responsible for clean up the Association.
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.
37  */
38 public class InactiveAssociationsRemovalService extends JobService {
39     private static final int JOB_ID = InactiveAssociationsRemovalService.class.hashCode();
40     private static final long ONE_DAY_INTERVAL = DAYS.toMillis(1);
41 
42     @Override
onStartJob(final JobParameters params)43     public boolean onStartJob(final JobParameters params) {
44         Slog.i(TAG, "Execute the Association Removal job");
45         // Special policy for selfManaged that need to revoke associations if the device
46         // does not connect for 90 days.
47         LocalServices.getService(CompanionDeviceManagerServiceInternal.class)
48                 .removeInactiveSelfManagedAssociations();
49         jobFinished(params, false);
50         return true;
51     }
52 
53     @Override
onStopJob(final JobParameters params)54     public boolean onStopJob(final JobParameters params) {
55         Slog.i(TAG, "Association removal job stopped; id=" + params.getJobId()
56                 + ", reason="
57                 + JobParameters.getInternalReasonCodeDescription(
58                 params.getInternalStopReasonCode()));
59         return false;
60     }
61 
schedule(Context context)62     static void schedule(Context context) {
63         Slog.i(TAG, "Scheduling the Association Removal job");
64         final JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
65         final JobInfo job = new JobInfo.Builder(JOB_ID,
66                 new ComponentName(context, InactiveAssociationsRemovalService.class))
67                 .setRequiresCharging(true)
68                 .setRequiresDeviceIdle(true)
69                 .setPeriodic(ONE_DAY_INTERVAL)
70                 .build();
71         jobScheduler.schedule(job);
72     }
73 }
74 
75