15eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate/*
25eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate * Copyright (C) 2014 The Android Open Source Project
35eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate *
45eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate * Licensed under the Apache License, Version 2.0 (the "License");
55eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate * you may not use this file except in compliance with the License.
65eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate * You may obtain a copy of the License at
75eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate *
85eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate *      http://www.apache.org/licenses/LICENSE-2.0
95eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate *
105eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate * Unless required by applicable law or agreed to in writing, software
115eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate * distributed under the License is distributed on an "AS IS" BASIS,
125eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate * See the License for the specific language governing permissions and
145eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate * limitations under the License.
155eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate */
165eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate
175eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tatepackage com.android.server.backup;
185eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate
195eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tateimport android.app.job.JobInfo;
205eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tateimport android.app.job.JobParameters;
215eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tateimport android.app.job.JobScheduler;
225eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tateimport android.app.job.JobService;
235eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tateimport android.content.ComponentName;
245eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tateimport android.content.Context;
255eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate
265eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tatepublic class FullBackupJob extends JobService {
275eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate    private static final String TAG = "FullBackupJob";
285eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate    private static final boolean DEBUG = true;
295eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate
305eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate    private static ComponentName sIdleService =
315eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate            new ComponentName("android", FullBackupJob.class.getName());
325eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate
335eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate    private static final int JOB_ID = 0x5038;
345eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate
355eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate    JobParameters mParams;
365eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate
375eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate    public static void schedule(Context ctx, long minDelay) {
385eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate        JobScheduler js = (JobScheduler) ctx.getSystemService(Context.JOB_SCHEDULER_SERVICE);
395eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate        JobInfo.Builder builder = new JobInfo.Builder(JOB_ID, sIdleService)
405eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate                .setRequiresDeviceIdle(true)
41d1c06753d045ad10e00e7aba53ee2adba0712cccMatthew Williams                .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
425eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate                .setRequiresCharging(true);
435eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate        if (minDelay > 0) {
445eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate            builder.setMinimumLatency(minDelay);
455eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate        }
465eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate        js.schedule(builder.build());
475eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate    }
485eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate
495eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate    // callback from the Backup Manager Service: it's finished its work for this pass
505eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate    public void finishBackupPass() {
515eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate        if (mParams != null) {
525eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate            jobFinished(mParams, false);
535eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate            mParams = null;
545eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate        }
555eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate    }
565eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate
575eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate    // ----- scheduled job interface -----
585eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate
595eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate    @Override
605eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate    public boolean onStartJob(JobParameters params) {
615eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate        mParams = params;
62bbe23b31dcd0eec8af5b5198970de7ff2a9ef79aChristopher Tate        Trampoline service = BackupManagerService.getInstance();
635eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate        return service.beginFullBackup(this);
645eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate    }
655eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate
665eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate    @Override
675eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate    public boolean onStopJob(JobParameters params) {
685eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate        if (mParams != null) {
695eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate            mParams = null;
70bbe23b31dcd0eec8af5b5198970de7ff2a9ef79aChristopher Tate            Trampoline service = BackupManagerService.getInstance();
715eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate            service.endFullBackup();
725eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate        }
735eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate        return false;
745eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate    }
755eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate
765eeb59cceb1f95813c548c1c5937f161c1ed3571Christopher Tate}
77