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.notification;
18 
19 import static android.app.job.JobScheduler.RESULT_SUCCESS;
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.os.CancellationSignal;
28 import android.util.Slog;
29 
30 import com.android.server.LocalServices;
31 
32 import java.util.concurrent.TimeUnit;
33 
34 /**
35  * This service runs every twenty minutes to ensure the retention policy for notification history
36  * data.
37  */
38 public class NotificationHistoryJobService extends JobService {
39     private final static String TAG = "NotificationHistoryJob";
40     private static final long JOB_RUN_INTERVAL = TimeUnit.MINUTES.toMillis(20);
41 
42     static final int BASE_JOB_ID = 237039804;
43 
scheduleJob(Context context)44     static void scheduleJob(Context context) {
45         JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
46         if (jobScheduler.getPendingJob(BASE_JOB_ID) == null) {
47             ComponentName component =
48                     new ComponentName(context, NotificationHistoryJobService.class);
49             JobInfo newJob = new JobInfo.Builder(BASE_JOB_ID, component)
50                     .setRequiresDeviceIdle(false)
51                     .setPeriodic(JOB_RUN_INTERVAL)
52                     .build();
53             if (jobScheduler.schedule(newJob) != RESULT_SUCCESS) {
54                 Slog.w(TAG, "Failed to schedule history cleanup job");
55             }
56         }
57     }
58 
59     private CancellationSignal mSignal;
60 
61     @Override
onStartJob(JobParameters params)62     public boolean onStartJob(JobParameters params) {
63         mSignal = new CancellationSignal();
64         new Thread(() -> {
65             NotificationManagerInternal nmInternal =
66                     LocalServices.getService(NotificationManagerInternal.class);
67             nmInternal.cleanupHistoryFiles();
68             jobFinished(params, mSignal.isCanceled());
69         }).start();
70         return true;
71     }
72 
73     @Override
onStopJob(JobParameters params)74     public boolean onStopJob(JobParameters params) {
75         if (mSignal != null) {
76             mSignal.cancel();
77         }
78         return false;
79     }
80 }
81 
82