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