1 /*
2  * Copyright (C) 2015 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.backup;
18 
19 import android.app.AlarmManager;
20 import android.app.job.JobInfo;
21 import android.app.job.JobParameters;
22 import android.app.job.JobScheduler;
23 import android.app.job.JobService;
24 import android.content.ComponentName;
25 import android.content.Context;
26 import android.os.RemoteException;
27 import android.util.Slog;
28 
29 import java.util.Random;
30 
31 /**
32  * Job for scheduling key/value backup work.  This module encapsulates all
33  * of the policy around when those backup passes are executed.
34  */
35 public class KeyValueBackupJob extends JobService {
36     private static final String TAG = "KeyValueBackupJob";
37     private static ComponentName sKeyValueJobService =
38             new ComponentName("android", KeyValueBackupJob.class.getName());
39     private static final int JOB_ID = 0x5039;
40 
41     // Minimum wait time between backups even while we're on charger
42     static final long BATCH_INTERVAL = 4 * AlarmManager.INTERVAL_HOUR;
43 
44     // Random variation in next-backup scheduling time to avoid server load spikes
45     private static final int FUZZ_MILLIS = 10 * 60 * 1000;
46 
47     // Once someone asks for a backup, this is how long we hold off until we find
48     // an on-charging opportunity.  If we hit this max latency we will run the operation
49     // regardless.  Privileged callers can always trigger an immediate pass via
50     // BackupManager.backupNow().
51     private static final long MAX_DEFERRAL = AlarmManager.INTERVAL_DAY;
52 
53     private static boolean sScheduled = false;
54     private static long sNextScheduled = 0;
55 
schedule(Context ctx)56     public static void schedule(Context ctx) {
57         schedule(ctx, 0);
58     }
59 
schedule(Context ctx, long delay)60     public static void schedule(Context ctx, long delay) {
61         synchronized (KeyValueBackupJob.class) {
62             if (!sScheduled) {
63                 if (delay <= 0) {
64                     delay = BATCH_INTERVAL + new Random().nextInt(FUZZ_MILLIS);
65                 }
66                 if (BackupManagerService.DEBUG_SCHEDULING) {
67                     Slog.v(TAG, "Scheduling k/v pass in "
68                             + (delay / 1000 / 60) + " minutes");
69                 }
70                 JobScheduler js = (JobScheduler) ctx.getSystemService(Context.JOB_SCHEDULER_SERVICE);
71                 JobInfo.Builder builder = new JobInfo.Builder(JOB_ID, sKeyValueJobService)
72                         .setMinimumLatency(delay)
73                         .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
74                         .setRequiresCharging(true)
75                         .setOverrideDeadline(MAX_DEFERRAL);
76                 js.schedule(builder.build());
77 
78                 sNextScheduled = System.currentTimeMillis() + delay;
79                 sScheduled = true;
80             }
81         }
82     }
83 
cancel(Context ctx)84     public static void cancel(Context ctx) {
85         synchronized (KeyValueBackupJob.class) {
86             JobScheduler js = (JobScheduler) ctx.getSystemService(Context.JOB_SCHEDULER_SERVICE);
87             js.cancel(JOB_ID);
88             sNextScheduled = 0;
89             sScheduled = false;
90         }
91     }
92 
nextScheduled()93     public static long nextScheduled() {
94         synchronized (KeyValueBackupJob.class) {
95             return sNextScheduled;
96         }
97     }
98 
99     @Override
onStartJob(JobParameters params)100     public boolean onStartJob(JobParameters params) {
101         synchronized (KeyValueBackupJob.class) {
102             sNextScheduled = 0;
103             sScheduled = false;
104         }
105 
106         // Time to run a key/value backup!
107         Trampoline service = BackupManagerService.getInstance();
108         try {
109             service.backupNow();
110         } catch (RemoteException e) {}
111 
112         // This was just a trigger; ongoing wakelock management is done by the
113         // rest of the backup system.
114         return false;
115     }
116 
117     @Override
onStopJob(JobParameters params)118     public boolean onStopJob(JobParameters params) {
119         // Intentionally empty; the job starting was just a trigger
120         return false;
121     }
122 
123 }
124