FullBackupJob.java revision 5eeb59cceb1f95813c548c1c5937f161c1ed3571
1/*
2 * Copyright (C) 2014 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
17package com.android.server.backup;
18
19import android.app.job.JobInfo;
20import android.app.job.JobParameters;
21import android.app.job.JobScheduler;
22import android.app.job.JobService;
23import android.app.job.JobInfo.NetworkType;
24import android.content.ComponentName;
25import android.content.Context;
26import android.util.Slog;
27
28public class FullBackupJob extends JobService {
29    private static final String TAG = "FullBackupJob";
30    private static final boolean DEBUG = true;
31
32    private static ComponentName sIdleService =
33            new ComponentName("android", FullBackupJob.class.getName());
34
35    private static final int JOB_ID = 0x5038;
36
37    JobParameters mParams;
38
39    public static void schedule(Context ctx, long minDelay) {
40        JobScheduler js = (JobScheduler) ctx.getSystemService(Context.JOB_SCHEDULER_SERVICE);
41        JobInfo.Builder builder = new JobInfo.Builder(JOB_ID, sIdleService)
42                .setRequiresDeviceIdle(true)
43                .setRequiredNetworkCapabilities(NetworkType.UNMETERED)
44                .setRequiresCharging(true);
45        if (minDelay > 0) {
46            builder.setMinimumLatency(minDelay);
47        }
48        js.schedule(builder.build());
49    }
50
51    // callback from the Backup Manager Service: it's finished its work for this pass
52    public void finishBackupPass() {
53        if (mParams != null) {
54            jobFinished(mParams, false);
55            mParams = null;
56        }
57    }
58
59    // ----- scheduled job interface -----
60
61    @Override
62    public boolean onStartJob(JobParameters params) {
63        mParams = params;
64        BackupManagerService service = BackupManagerService.getInstance();
65        return service.beginFullBackup(this);
66    }
67
68    @Override
69    public boolean onStopJob(JobParameters params) {
70        if (mParams != null) {
71            mParams = null;
72            BackupManagerService service = BackupManagerService.getInstance();
73            service.endFullBackup();
74        }
75        return false;
76    }
77
78}
79