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 com.android.server.backup; 18 19 import android.app.IWallpaperManager; 20 import android.app.backup.BackupAgentHelper; 21 import android.app.backup.BackupDataInput; 22 import android.app.backup.BackupHelper; 23 import android.app.backup.FullBackup; 24 import android.app.backup.FullBackupDataOutput; 25 import android.app.backup.WallpaperBackupHelper; 26 import android.content.Context; 27 import android.os.Environment; 28 import android.os.ParcelFileDescriptor; 29 import android.os.RemoteException; 30 import android.os.ServiceManager; 31 import android.os.UserHandle; 32 import android.util.Slog; 33 34 import com.google.android.collect.Sets; 35 36 import java.io.File; 37 import java.io.IOException; 38 import java.util.Set; 39 40 /** 41 * Backup agent for various system-managed data. Wallpapers are now handled by a 42 * separate package, but we still process restores from legacy datasets here. 43 */ 44 public class SystemBackupAgent extends BackupAgentHelper { 45 private static final String TAG = "SystemBackupAgent"; 46 47 // Names of the helper tags within the dataset. Changing one of these names will 48 // break the ability to restore from datasets that predate the change. 49 private static final String WALLPAPER_HELPER = "wallpaper"; 50 private static final String SYNC_SETTINGS_HELPER = "account_sync_settings"; 51 private static final String PREFERRED_HELPER = "preferred_activities"; 52 private static final String NOTIFICATION_HELPER = "notifications"; 53 private static final String PERMISSION_HELPER = "permissions"; 54 private static final String USAGE_STATS_HELPER = "usage_stats"; 55 private static final String SHORTCUT_MANAGER_HELPER = "shortcut_manager"; 56 private static final String ACCOUNT_MANAGER_HELPER = "account_manager"; 57 private static final String SLICES_HELPER = "slices"; 58 private static final String PEOPLE_HELPER = "people"; 59 60 // These paths must match what the WallpaperManagerService uses. The leaf *_FILENAME 61 // are also used in the full-backup file format, so must not change unless steps are 62 // taken to support the legacy backed-up datasets. 63 private static final String WALLPAPER_IMAGE_FILENAME = "wallpaper"; 64 private static final String WALLPAPER_INFO_FILENAME = "wallpaper_info.xml"; 65 66 // TODO: Will need to change if backing up non-primary user's wallpaper 67 // TODO: http://b/22388012 68 private static final String WALLPAPER_IMAGE_DIR = 69 Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM).getAbsolutePath(); 70 public static final String WALLPAPER_IMAGE = 71 new File(Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM), 72 "wallpaper").getAbsolutePath(); 73 74 // TODO: Will need to change if backing up non-primary user's wallpaper 75 // TODO: http://b/22388012 76 private static final String WALLPAPER_INFO_DIR = 77 Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM).getAbsolutePath(); 78 public static final String WALLPAPER_INFO = 79 new File(Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM), 80 "wallpaper_info.xml").getAbsolutePath(); 81 // Use old keys to keep legacy data compatibility and avoid writing two wallpapers 82 private static final String WALLPAPER_IMAGE_KEY = WallpaperBackupHelper.WALLPAPER_IMAGE_KEY; 83 84 private static final Set<String> sEligibleForMultiUser = Sets.newArraySet( 85 PERMISSION_HELPER, NOTIFICATION_HELPER, SYNC_SETTINGS_HELPER); 86 87 private int mUserId = UserHandle.USER_SYSTEM; 88 89 @Override onCreate(UserHandle user)90 public void onCreate(UserHandle user) { 91 super.onCreate(user); 92 93 mUserId = user.getIdentifier(); 94 95 addHelper(SYNC_SETTINGS_HELPER, new AccountSyncSettingsBackupHelper(this, mUserId)); 96 addHelper(PREFERRED_HELPER, new PreferredActivityBackupHelper()); 97 addHelper(NOTIFICATION_HELPER, new NotificationBackupHelper(mUserId)); 98 addHelper(PERMISSION_HELPER, new PermissionBackupHelper(mUserId)); 99 addHelper(USAGE_STATS_HELPER, new UsageStatsBackupHelper(this)); 100 addHelper(SHORTCUT_MANAGER_HELPER, new ShortcutBackupHelper()); 101 addHelper(ACCOUNT_MANAGER_HELPER, new AccountManagerBackupHelper()); 102 addHelper(SLICES_HELPER, new SliceBackupHelper(this)); 103 addHelper(PEOPLE_HELPER, new PeopleBackupHelper(mUserId)); 104 } 105 106 @Override onFullBackup(FullBackupDataOutput data)107 public void onFullBackup(FullBackupDataOutput data) throws IOException { 108 // At present we don't back up anything 109 } 110 111 @Override onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)112 public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) 113 throws IOException { 114 // Slot in a restore helper for the older wallpaper backup schema to support restore 115 // from devices still generating data in that format. 116 //TODO(b/147732386): Add multi-display support for wallpaper backup. 117 addHelper(WALLPAPER_HELPER, new WallpaperBackupHelper(this, 118 new String[] { WALLPAPER_IMAGE_KEY})); 119 120 // On restore, we also support a long-ago wallpaper data schema "system_files" 121 addHelper("system_files", new WallpaperBackupHelper(this, 122 new String[] { WALLPAPER_IMAGE_KEY} )); 123 124 super.onRestore(data, appVersionCode, newState); 125 } 126 127 @Override addHelper(String keyPrefix, BackupHelper helper)128 public void addHelper(String keyPrefix, BackupHelper helper) { 129 if (mUserId != UserHandle.USER_SYSTEM && !sEligibleForMultiUser.contains(keyPrefix)) { 130 return; 131 } 132 133 super.addHelper(keyPrefix, helper); 134 } 135 136 /** 137 * Support for 'adb restore' of legacy archives 138 */ 139 @Override onRestoreFile(ParcelFileDescriptor data, long size, int type, String domain, String path, long mode, long mtime)140 public void onRestoreFile(ParcelFileDescriptor data, long size, 141 int type, String domain, String path, long mode, long mtime) 142 throws IOException { 143 Slog.i(TAG, "Restoring file domain=" + domain + " path=" + path); 144 145 // Bits to indicate postprocessing we may need to perform 146 boolean restoredWallpaper = false; 147 148 File outFile = null; 149 // Various domain+files we understand a priori 150 if (domain.equals(FullBackup.ROOT_TREE_TOKEN)) { 151 if (path.equals(WALLPAPER_INFO_FILENAME)) { 152 outFile = new File(WALLPAPER_INFO); 153 restoredWallpaper = true; 154 } else if (path.equals(WALLPAPER_IMAGE_FILENAME)) { 155 outFile = new File(WALLPAPER_IMAGE); 156 restoredWallpaper = true; 157 } 158 } 159 160 try { 161 if (outFile == null) { 162 Slog.w(TAG, "Skipping unrecognized system file: [ " + domain + " : " + path + " ]"); 163 } 164 FullBackup.restoreFile(data, size, type, mode, mtime, outFile); 165 166 if (restoredWallpaper) { 167 IWallpaperManager wallpaper = 168 (IWallpaperManager)ServiceManager.getService( 169 Context.WALLPAPER_SERVICE); 170 if (wallpaper != null) { 171 try { 172 wallpaper.settingsRestored(); 173 } catch (RemoteException re) { 174 Slog.e(TAG, "Couldn't restore settings\n" + re); 175 } 176 } 177 } 178 } catch (IOException e) { 179 if (restoredWallpaper) { 180 // Make sure we wind up in a good state 181 (new File(WALLPAPER_IMAGE)).delete(); 182 (new File(WALLPAPER_INFO)).delete(); 183 } 184 } 185 } 186 } 187