Home | History | Annotate | Download | only in launcher3
      1 /*
      2  * Copyright (C) 2013 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.launcher3;
     18 
     19 import android.app.backup.BackupAgentHelper;
     20 import android.app.backup.BackupDataInput;
     21 import android.app.backup.BackupManager;
     22 import android.content.Context;
     23 import android.database.Cursor;
     24 import android.os.ParcelFileDescriptor;
     25 import android.util.Log;
     26 
     27 import java.io.IOException;
     28 
     29 public class LauncherBackupAgentHelper extends BackupAgentHelper {
     30 
     31     private static final String TAG = "LauncherBackupAgentHelper";
     32 
     33     private static final String LAUNCHER_DATA_PREFIX = "L";
     34 
     35     static final boolean VERBOSE = true;
     36     static final boolean DEBUG = false;
     37 
     38     private static BackupManager sBackupManager;
     39 
     40     /**
     41      * Notify the backup manager that out database is dirty.
     42      *
     43      * <P>This does not force an immediate backup.
     44      *
     45      * @param context application context
     46      */
     47     public static void dataChanged(Context context) {
     48         if (sBackupManager == null) {
     49             sBackupManager = new BackupManager(context);
     50         }
     51         sBackupManager.dataChanged();
     52     }
     53 
     54     private LauncherBackupHelper mHelper;
     55 
     56     @Override
     57     public void onCreate() {
     58         super.onCreate();
     59         mHelper = new LauncherBackupHelper(this);
     60         addHelper(LAUNCHER_DATA_PREFIX, mHelper);
     61     }
     62 
     63     @Override
     64     public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
     65             throws IOException {
     66         if (!Utilities.isLmpOrAbove()) {
     67             // No restore for old devices.
     68             Log.i(TAG, "You shall not pass!!!");
     69             Log.d(TAG, "Restore is only supported on devices running Lollipop and above.");
     70             return;
     71         }
     72 
     73         // Clear dB before restore
     74         LauncherAppState.getLauncherProvider().createEmptyDB();
     75 
     76         boolean hasData;
     77         try {
     78             super.onRestore(data, appVersionCode, newState);
     79             // If no favorite was migrated, clear the data and start fresh.
     80             final Cursor c = getContentResolver().query(
     81                     LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, null, null, null, null);
     82             hasData = c.moveToNext();
     83             c.close();
     84         } catch (Exception e) {
     85             // If the restore fails, we should do a fresh start.
     86             Log.e(TAG, "Restore failed", e);
     87             hasData = false;
     88         }
     89 
     90         if (hasData && mHelper.restoreSuccessful) {
     91             LauncherAppState.getLauncherProvider().clearFlagEmptyDbCreated();
     92             LauncherClings.synchonouslyMarkFirstRunClingDismissed(this);
     93         } else {
     94             if (VERBOSE) Log.v(TAG, "Nothing was restored, clearing DB");
     95             LauncherAppState.getLauncherProvider().createEmptyDB();
     96         }
     97     }
     98 }
     99