1 /* 2 * Copyright (C) 2010 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.example.android.backuprestore; 18 19 import java.io.IOException; 20 21 import android.app.backup.BackupAgentHelper; 22 import android.app.backup.BackupDataInput; 23 import android.app.backup.BackupDataOutput; 24 import android.app.backup.FileBackupHelper; 25 import android.os.ParcelFileDescriptor; 26 27 /** 28 * This agent backs up the application's data using the BackupAgentHelper 29 * infrastructure. In this application's case, the backup data is merely 30 * a duplicate of the stored data file; that makes it a perfect candidate 31 * for backing up using the {@link android.app.backup.FileBackupHelper} class 32 * provided by the Android operating system. 33 * 34 * <p>"Backup helpers" are a general mechanism that an agent implementation 35 * uses by extending {@link BackupAgentHelper} rather than the basic 36 * {@link BackupAgent} class. 37 * 38 * <p>By itself, the FileBackupHelper is properly handling the backup and 39 * restore of the datafile that we've configured it with, but it does 40 * not know about the potential need to use locking around its access 41 * to it. However, it is straightforward to override 42 * {@link #onBackup()} and {@link #onRestore()} to supply the necessary locking 43 * around the helper's operation. 44 */ 45 public class FileHelperExampleAgent extends BackupAgentHelper { 46 /** 47 * The "key" string passed when adding a helper is a token used to 48 * disambiguate between entities supplied by multiple different helper 49 * objects. They only need to be unique among the helpers within this 50 * one agent class, not globally unique. 51 */ 52 static final String FILE_HELPER_KEY = "the_file"; 53 54 /** 55 * The {@link android.app.backup.FileBackupHelper FileBackupHelper} class 56 * does nearly all of the work for our use case: backup and restore of a 57 * file stored within our application's getFilesDir() location. It will 58 * also handle files stored at any subpath within that location. All we 59 * need to do is a bit of one-time configuration: installing the helper 60 * when this agent object is created. 61 */ 62 @Override onCreate()63 public void onCreate() { 64 // All we need to do when working within the BackupAgentHelper mechanism 65 // is to install the helper that will process and back up the files we 66 // care about. In this case, it's just one file. 67 FileBackupHelper helper = new FileBackupHelper(this, BackupRestoreActivity.DATA_FILE_NAME); 68 addHelper(FILE_HELPER_KEY, helper); 69 } 70 71 /** 72 * We want to ensure that the UI is not trying to rewrite the data file 73 * while we're reading it for backup, so we override this method to 74 * supply the necessary locking. 75 */ 76 @Override onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState)77 public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, 78 ParcelFileDescriptor newState) throws IOException { 79 // Hold the lock while the FileBackupHelper performs the backup operation 80 synchronized (BackupRestoreActivity.sDataLock) { 81 super.onBackup(oldState, data, newState); 82 } 83 } 84 85 /** 86 * Adding locking around the file rewrite that happens during restore is 87 * similarly straightforward. 88 */ 89 @Override onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)90 public void onRestore(BackupDataInput data, int appVersionCode, 91 ParcelFileDescriptor newState) throws IOException { 92 // Hold the lock while the FileBackupHelper restores the file from 93 // the data provided here. 94 synchronized (BackupRestoreActivity.sDataLock) { 95 super.onRestore(data, appVersionCode, newState); 96 } 97 } 98 } 99