Home | History | Annotate | Download | only in backup
      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 android.app.backup;
     18 
     19 import android.app.WallpaperManager;
     20 import android.content.Context;
     21 import android.os.Environment;
     22 import android.os.ParcelFileDescriptor;
     23 import android.os.UserHandle;
     24 import android.util.Slog;
     25 
     26 import java.io.File;
     27 import java.io.FileInputStream;
     28 import java.io.IOException;
     29 
     30 /**
     31  * We no longer back up wallpapers with this helper, but we do need to process restores
     32  * of legacy backup payloads.  We just take the restored image as-is and apply it as the
     33  * system wallpaper using the public "set the wallpaper" API.
     34  *
     35  * @hide
     36  */
     37 public class WallpaperBackupHelper extends FileBackupHelperBase implements BackupHelper {
     38     private static final String TAG = "WallpaperBackupHelper";
     39     private static final boolean DEBUG = false;
     40 
     41     // Key that legacy wallpaper imagery was stored under
     42     public static final String WALLPAPER_IMAGE_KEY =
     43             "/data/data/com.android.settings/files/wallpaper";
     44     public static final String WALLPAPER_INFO_KEY = "/data/system/wallpaper_info.xml";
     45 
     46     // Stage file that the restored imagery is stored to prior to being applied
     47     // as the system wallpaper.
     48     private static final String STAGE_FILE =
     49             new File(Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM),
     50                     "wallpaper-tmp").getAbsolutePath();
     51 
     52     private final String[] mKeys;
     53     private final WallpaperManager mWpm;
     54 
     55     /**
     56      * Legacy wallpaper restores, from back when the imagery was stored under the
     57      * "android" system package as file key/value entities.
     58      *
     59      * @param context
     60      * @param files
     61      */
     62     public WallpaperBackupHelper(Context context, String[] keys) {
     63         super(context);
     64 
     65         mContext = context;
     66         mKeys = keys;
     67 
     68         mWpm = (WallpaperManager) context.getSystemService(Context.WALLPAPER_SERVICE);
     69     }
     70 
     71     /**
     72      * Based on oldState, determine which of the files from the application's data directory
     73      * need to be backed up, write them to the data stream, and fill in newState with the
     74      * state as it exists now.
     75      */
     76     @Override
     77     public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
     78             ParcelFileDescriptor newState) {
     79         // Intentionally no-op; we don't back up the wallpaper this way any more.
     80     }
     81 
     82     /**
     83      * Restore one absolute file entity from the restore stream.  If we're restoring the
     84      * magic wallpaper file, apply it as the system wallpaper.
     85      */
     86     @Override
     87     public void restoreEntity(BackupDataInputStream data) {
     88         final String key = data.getKey();
     89         if (isKeyInList(key, mKeys)) {
     90             if (key.equals(WALLPAPER_IMAGE_KEY)) {
     91                 // restore the file to the stage for inspection
     92                 File stage = new File(STAGE_FILE);
     93                 try {
     94                     if (writeFile(stage, data)) {
     95                         try (FileInputStream in = new FileInputStream(stage)) {
     96                             mWpm.setStream(in);
     97                         } catch (IOException e) {
     98                             Slog.e(TAG, "Unable to set restored wallpaper: " + e.getMessage());
     99                         }
    100                     } else {
    101                         Slog.e(TAG, "Unable to save restored wallpaper");
    102                     }
    103                 } finally {
    104                     stage.delete();
    105                 }
    106             }
    107         }
    108     }
    109 }
    110