SystemBackupAgent.java revision 2efd2dbbac9eac89620683696c6076463c3a1cd6
19bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato/*
29bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato * Copyright (C) 2009 The Android Open Source Project
39bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato *
49bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato * Licensed under the Apache License, Version 2.0 (the "License");
59bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato * you may not use this file except in compliance with the License.
69bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato * You may obtain a copy of the License at
79bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato *
89bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato *      http://www.apache.org/licenses/LICENSE-2.0
99bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato *
109bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato * Unless required by applicable law or agreed to in writing, software
119bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato * distributed under the License is distributed on an "AS IS" BASIS,
129bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato * See the License for the specific language governing permissions and
149bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato * limitations under the License.
159bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato */
169bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato
179bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onoratopackage com.android.server;
189bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato
19a924dc0db952fe32509435fdb8dc9c84a9e181f3Dianne Hackborn
204528186e0d65fc68ef0dd1941aa2ac8aefcd55a3Christopher Tateimport android.app.backup.BackupDataInput;
214528186e0d65fc68ef0dd1941aa2ac8aefcd55a3Christopher Tateimport android.app.backup.BackupDataOutput;
22cc84c69726507a85116f5664e20e2ebfac76edbeChristopher Tateimport android.app.backup.BackupAgentHelper;
234a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tateimport android.app.backup.FullBackup;
242efd2dbbac9eac89620683696c6076463c3a1cd6Christopher Tateimport android.app.backup.FullBackupDataOutput;
253f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tateimport android.app.backup.WallpaperBackupHelper;
269bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onoratoimport android.content.Context;
279bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onoratoimport android.os.ParcelFileDescriptor;
289bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onoratoimport android.os.ServiceManager;
298a9b22056b13477f59df934928c00c58b5871c95Joe Onoratoimport android.util.Slog;
309bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato
313f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate
329bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onoratoimport java.io.File;
339bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onoratoimport java.io.IOException;
349bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato
359bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato/**
367c2bb66db77653c0a690962858cf105b0cb981d7Christopher Tate * Backup agent for various system-managed data, currently just the system wallpaper
379bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato */
38cc84c69726507a85116f5664e20e2ebfac76edbeChristopher Tatepublic class SystemBackupAgent extends BackupAgentHelper {
399bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato    private static final String TAG = "SystemBackupAgent";
409bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato
4175a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate    // These paths must match what the WallpaperManagerService uses.  The leaf *_FILENAME
4275a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate    // are also used in the full-backup file format, so must not change unless steps are
4375a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate    // taken to support the legacy backed-up datasets.
4475a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate    private static final String WALLPAPER_IMAGE_FILENAME = "wallpaper";
4575a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate    private static final String WALLPAPER_INFO_FILENAME = "wallpaper_info.xml";
4675a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate
474a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate    private static final String WALLPAPER_IMAGE_DIR = "/data/data/com.android.settings/files";
4875a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate    private static final String WALLPAPER_IMAGE = WALLPAPER_IMAGE_DIR + "/" + WALLPAPER_IMAGE_FILENAME;
4975a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate
504a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate    private static final String WALLPAPER_INFO_DIR = "/data/system";
5175a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate    private static final String WALLPAPER_INFO = WALLPAPER_INFO_DIR + "/" +  WALLPAPER_INFO_FILENAME;
5275a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate
539bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato
549bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato    @Override
557c2bb66db77653c0a690962858cf105b0cb981d7Christopher Tate    public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
567c2bb66db77653c0a690962858cf105b0cb981d7Christopher Tate            ParcelFileDescriptor newState) throws IOException {
577c2bb66db77653c0a690962858cf105b0cb981d7Christopher Tate        // We only back up the data under the current "wallpaper" schema with metadata
58541fa51e5ccba9e2def2632f9835db0c9407ec5cDan Egnor        WallpaperManagerService wallpaper = (WallpaperManagerService)ServiceManager.getService(
59541fa51e5ccba9e2def2632f9835db0c9407ec5cDan Egnor                Context.WALLPAPER_SERVICE);
60541fa51e5ccba9e2def2632f9835db0c9407ec5cDan Egnor        String[] files = new String[] { WALLPAPER_IMAGE, WALLPAPER_INFO };
61541fa51e5ccba9e2def2632f9835db0c9407ec5cDan Egnor        if (wallpaper != null && wallpaper.mName != null && wallpaper.mName.length() > 0) {
62541fa51e5ccba9e2def2632f9835db0c9407ec5cDan Egnor            // When the wallpaper has a name, back up the info by itself.
63541fa51e5ccba9e2def2632f9835db0c9407ec5cDan Egnor            // TODO: Don't rely on the innards of the service object like this!
64541fa51e5ccba9e2def2632f9835db0c9407ec5cDan Egnor            // TODO: Send a delete for any stored wallpaper image in this case?
65541fa51e5ccba9e2def2632f9835db0c9407ec5cDan Egnor            files = new String[] { WALLPAPER_INFO };
66541fa51e5ccba9e2def2632f9835db0c9407ec5cDan Egnor        }
673f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate        addHelper("wallpaper", new WallpaperBackupHelper(SystemBackupAgent.this, files));
687c2bb66db77653c0a690962858cf105b0cb981d7Christopher Tate        super.onBackup(oldState, data, newState);
699bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato    }
709bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato
712efd2dbbac9eac89620683696c6076463c3a1cd6Christopher Tate    @Override
722efd2dbbac9eac89620683696c6076463c3a1cd6Christopher Tate    public void onFullBackup(FullBackupDataOutput data) throws IOException {
732efd2dbbac9eac89620683696c6076463c3a1cd6Christopher Tate        // At present we back up only the wallpaper
742efd2dbbac9eac89620683696c6076463c3a1cd6Christopher Tate        fullWallpaperBackup(data);
7575a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate    }
7675a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate
772efd2dbbac9eac89620683696c6076463c3a1cd6Christopher Tate    private void fullWallpaperBackup(FullBackupDataOutput output) {
7875a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate        // Back up the data files directly.  We do them in this specific order --
7975a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate        // info file followed by image -- because then we need take no special
8075a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate        // steps during restore; the restore will happen properly when the individual
8175a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate        // files are restored piecemeal.
8275a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate        FullBackup.backupToTar(getPackageName(), FullBackup.ROOT_TREE_TOKEN, null,
832efd2dbbac9eac89620683696c6076463c3a1cd6Christopher Tate                WALLPAPER_INFO_DIR, WALLPAPER_INFO, output.getData());
8475a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate        FullBackup.backupToTar(getPackageName(), FullBackup.ROOT_TREE_TOKEN, null,
852efd2dbbac9eac89620683696c6076463c3a1cd6Christopher Tate                WALLPAPER_IMAGE_DIR, WALLPAPER_IMAGE, output.getData());
864a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate    }
874a627c71ff53a4fca1f961f4b1dcc0461df18a06Christopher Tate
889bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato    @Override
899bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato    public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
909bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato            throws IOException {
917c2bb66db77653c0a690962858cf105b0cb981d7Christopher Tate        // On restore, we also support a previous data schema "system_files"
923f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate        addHelper("wallpaper", new WallpaperBackupHelper(SystemBackupAgent.this,
937c2bb66db77653c0a690962858cf105b0cb981d7Christopher Tate                new String[] { WALLPAPER_IMAGE, WALLPAPER_INFO }));
943f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate        addHelper("system_files", new WallpaperBackupHelper(SystemBackupAgent.this,
957c2bb66db77653c0a690962858cf105b0cb981d7Christopher Tate                new String[] { WALLPAPER_IMAGE }));
967c2bb66db77653c0a690962858cf105b0cb981d7Christopher Tate
979bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato        try {
989bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato            super.onRestore(data, appVersionCode, newState);
999bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato
1008cc6a5026aeb5cf9cc36529426fe0cc66714f5fbDianne Hackborn            WallpaperManagerService wallpaper = (WallpaperManagerService)ServiceManager.getService(
1019bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato                    Context.WALLPAPER_SERVICE);
1029bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato            wallpaper.settingsRestored();
1039bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato        } catch (IOException ex) {
1043f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate            // If there was a failure, delete everything for the wallpaper, this is too aggressive,
1059bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato            // but this is hopefully a rare failure.
1068a9b22056b13477f59df934928c00c58b5871c95Joe Onorato            Slog.d(TAG, "restore failed", ex);
1079bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato            (new File(WALLPAPER_IMAGE)).delete();
1089bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato            (new File(WALLPAPER_INFO)).delete();
1099bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato        }
1109bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato    }
11175a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate
11275a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate    @Override
11375a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate    public void onRestoreFile(ParcelFileDescriptor data, long size,
11475a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate            int type, String domain, String path, long mode, long mtime)
11575a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate            throws IOException {
11675a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate        Slog.i(TAG, "Restoring file domain=" + domain + " path=" + path);
11775a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate
11875a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate        // Bits to indicate postprocessing we may need to perform
11975a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate        boolean restoredWallpaper = false;
12075a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate
12175a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate        File outFile = null;
12275a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate        // Various domain+files we understand a priori
12375a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate        if (domain.equals(FullBackup.ROOT_TREE_TOKEN)) {
12475a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate            if (path.equals(WALLPAPER_INFO_FILENAME)) {
12575a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate                outFile = new File(WALLPAPER_INFO);
12675a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate                restoredWallpaper = true;
12775a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate            } else if (path.equals(WALLPAPER_IMAGE_FILENAME)) {
12875a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate                outFile = new File(WALLPAPER_IMAGE);
12975a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate                restoredWallpaper = true;
13075a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate            }
13175a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate        }
13275a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate
13375a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate        try {
13475a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate            if (outFile == null) {
13575a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate                Slog.w(TAG, "Skipping unrecognized system file: [ " + domain + " : " + path + " ]");
13675a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate            }
13779ec80db70d788f35aa13346e4684ecbd401bd84Christopher Tate            FullBackup.restoreFile(data, size, type, mode, mtime, outFile);
13875a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate
13975a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate            if (restoredWallpaper) {
14075a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate                WallpaperManagerService wallpaper =
14175a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate                        (WallpaperManagerService)ServiceManager.getService(
14275a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate                        Context.WALLPAPER_SERVICE);
14375a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate                wallpaper.settingsRestored();
14475a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate            }
14575a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate        } catch (IOException e) {
14675a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate            if (restoredWallpaper) {
14775a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate                // Make sure we wind up in a good state
14875a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate                (new File(WALLPAPER_IMAGE)).delete();
14975a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate                (new File(WALLPAPER_INFO)).delete();
15075a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate            }
15175a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate        }
15275a99709accef8cf221fd436d646727e7c8dd1f1Christopher Tate    }
1539bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato}
154