Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2009 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 android.app;
     18 
     19 import android.app.backup.BackupAgent;
     20 import android.app.backup.BackupDataInput;
     21 import android.app.backup.BackupDataOutput;
     22 import android.app.backup.FileBackupHelper;
     23 import android.os.ParcelFileDescriptor;
     24 import android.util.Log;
     25 
     26 import java.io.File;
     27 import java.util.ArrayList;
     28 import java.util.LinkedList;
     29 
     30 /**
     31  * Backs up an application's entire /data/data/<package>/... file system.  This
     32  * class is used by the desktop full backup mechanism and is not intended for direct
     33  * use by applications.
     34  *
     35  * {@hide}
     36  */
     37 
     38 public class FullBackupAgent extends BackupAgent {
     39     // !!! TODO: turn off debugging
     40     private static final String TAG = "FullBackupAgent";
     41     private static final boolean DEBUG = true;
     42 
     43     @Override
     44     public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
     45             ParcelFileDescriptor newState) {
     46         LinkedList<File> dirsToScan = new LinkedList<File>();
     47         ArrayList<String> allFiles = new ArrayList<String>();
     48 
     49         // build the list of files in the app's /data/data tree
     50         dirsToScan.add(getFilesDir());
     51         if (DEBUG) Log.v(TAG, "Backing up dir tree @ " + getFilesDir().getAbsolutePath() + " :");
     52         while (dirsToScan.size() > 0) {
     53             File dir = dirsToScan.removeFirst();
     54             File[] contents = dir.listFiles();
     55             if (contents != null) {
     56                 for (File f : contents) {
     57                     if (f.isDirectory()) {
     58                         dirsToScan.add(f);
     59                     } else if (f.isFile()) {
     60                         if (DEBUG) Log.v(TAG, "    " + f.getAbsolutePath());
     61                         allFiles.add(f.getAbsolutePath());
     62                     }
     63                 }
     64             }
     65         }
     66 
     67         // That's the file set; now back it all up
     68         FileBackupHelper helper = new FileBackupHelper(this,
     69                 allFiles.toArray(new String[allFiles.size()]));
     70         helper.performBackup(oldState, data, newState);
     71     }
     72 
     73     @Override
     74     public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) {
     75     }
     76 }
     77