1 /*
2  * Copyright (C) 2017 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.net.watchlist;
18 
19 import android.app.job.JobInfo;
20 import android.app.job.JobParameters;
21 import android.app.job.JobScheduler;
22 import android.app.job.JobService;
23 import android.content.ComponentName;
24 import android.content.Context;
25 import android.net.NetworkWatchlistManager;
26 import android.util.Slog;
27 
28 import java.util.concurrent.TimeUnit;
29 
30 /**
31  * A job that periodically report watchlist records.
32  */
33 public class ReportWatchlistJobService extends JobService {
34 
35     private static final boolean DEBUG = NetworkWatchlistService.DEBUG;
36     private static final String TAG = "WatchlistJobService";
37 
38     // Unique job id used in system service, other jobs should not use the same value.
39     public static final int REPORT_WATCHLIST_RECORDS_JOB_ID = 0xd7689;
40     public static final long REPORT_WATCHLIST_RECORDS_PERIOD_MILLIS =
41             TimeUnit.HOURS.toMillis(12);
42 
43     @Override
onStartJob(final JobParameters jobParameters)44     public boolean onStartJob(final JobParameters jobParameters) {
45         if (jobParameters.getJobId() != REPORT_WATCHLIST_RECORDS_JOB_ID) {
46             return false;
47         }
48         if (DEBUG) Slog.d(TAG, "Start scheduled job.");
49         new NetworkWatchlistManager(this).reportWatchlistIfNecessary();
50         jobFinished(jobParameters, false);
51         return true;
52     }
53 
54     @Override
onStopJob(JobParameters jobParameters)55     public boolean onStopJob(JobParameters jobParameters) {
56         return true; // Reschedule when possible.
57     }
58 
59     /**
60      * Schedule the {@link ReportWatchlistJobService} to run periodically.
61      */
schedule(Context context)62     public static void schedule(Context context) {
63         if (DEBUG) Slog.d(TAG, "Scheduling records aggregator task");
64         final JobScheduler scheduler =
65                 (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
66         scheduler.schedule(new JobInfo.Builder(REPORT_WATCHLIST_RECORDS_JOB_ID,
67                 new ComponentName(context, ReportWatchlistJobService.class))
68                 //.setOverrideDeadline(45 * 1000) // Schedule job soon, for testing.
69                 .setPeriodic(REPORT_WATCHLIST_RECORDS_PERIOD_MILLIS)
70                 .setRequiresDeviceIdle(true)
71                 .setRequiresBatteryNotLow(true)
72                 .setPersisted(false)
73                 .build());
74     }
75 
76 }
77