DeskClockBackupAgent.java revision 1f53186f7d1d449382b2e429cd35e3312f9eb97e
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 17package com.android.deskclock; 18 19import android.app.backup.BackupAgent; 20import android.app.backup.BackupDataInput; 21import android.app.backup.BackupDataOutput; 22import android.os.ParcelFileDescriptor; 23import android.support.annotation.NonNull; 24 25import com.android.deskclock.alarms.AlarmStateManager; 26import com.android.deskclock.provider.Alarm; 27import com.android.deskclock.provider.AlarmInstance; 28 29import java.io.File; 30import java.io.IOException; 31import java.util.Calendar; 32import java.util.List; 33 34public class DeskClockBackupAgent extends BackupAgent { 35 36 private static final String TAG = "DeskClockBackupAgent"; 37 38 @Override 39 public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, 40 ParcelFileDescriptor newState) throws IOException { } 41 42 @Override 43 public void onRestore(BackupDataInput data, int appVersionCode, 44 ParcelFileDescriptor newState) throws IOException { } 45 46 @Override 47 public void onRestoreFile(@NonNull ParcelFileDescriptor data, long size, File destination, 48 int type, long mode, long mtime) throws IOException { 49 // The preference file on the backup device may not be the same on the restore device. 50 // Massage the file name here before writing it. 51 if (destination.getName().endsWith("_preferences.xml")) { 52 final String prefFileName = getPackageName() + "_preferences.xml"; 53 destination = new File(destination.getParentFile(), prefFileName); 54 } 55 56 super.onRestoreFile(data, size, destination, type, mode, mtime); 57 } 58 59 @Override 60 public void onRestoreFinished() { 61 // Now that alarms have been restored, schedule them in AlarmManager. 62 final List<Alarm> alarms = Alarm.getAlarms(getContentResolver(), null); 63 64 final Calendar now = Calendar.getInstance(); 65 for (Alarm alarm : alarms) { 66 // Remove any instances that may currently exist for the alarm; 67 // these aren't relevant on the restore device and we'll recreate them below. 68 AlarmStateManager.deleteAllInstances(this, alarm.id); 69 70 if (alarm.enabled) { 71 // Create the next alarm instance to schedule. 72 AlarmInstance alarmInstance = alarm.createInstanceAfter(now); 73 74 // Add the next alarm instance to the database. 75 alarmInstance = AlarmInstance.addInstance(getContentResolver(), alarmInstance); 76 77 // Schedule the next alarm instance in AlarmManager. 78 AlarmStateManager.registerInstance(this, alarmInstance, true); 79 LogUtils.i(TAG, "DeskClockBackupAgent scheduled alarm instance: %s", alarmInstance); 80 } 81 } 82 } 83}