1 /* 2 * Copyright (C) 2016 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.provider; 18 19 import android.content.ContentValues; 20 import android.content.Context; 21 import android.database.Cursor; 22 import android.database.sqlite.SQLiteDatabase; 23 24 import com.android.launcher3.LauncherAppWidgetInfo; 25 import com.android.launcher3.LauncherProvider.DatabaseHelper; 26 import com.android.launcher3.LauncherSettings.Favorites; 27 import com.android.launcher3.ShortcutInfo; 28 import com.android.launcher3.Utilities; 29 import com.android.launcher3.logging.FileLog; 30 import com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction; 31 import com.android.launcher3.util.LogConfig; 32 33 import java.io.InvalidObjectException; 34 35 /** 36 * Utility class to update DB schema after it has been restored. 37 * 38 * This task is executed when Launcher starts for the first time and not immediately after restore. 39 * This helps keep the model consistent if the launcher updates between restore and first startup. 40 */ 41 public class RestoreDbTask { 42 43 private static final String TAG = "RestoreDbTask"; 44 private static final String RESTORE_TASK_PENDING = "restore_task_pending"; 45 46 private static final String INFO_COLUMN_NAME = "name"; 47 private static final String INFO_COLUMN_DEFAULT_VALUE = "dflt_value"; 48 49 public static boolean performRestore(DatabaseHelper helper) { 50 SQLiteDatabase db = helper.getWritableDatabase(); 51 try (SQLiteTransaction t = new SQLiteTransaction(db)) { 52 new RestoreDbTask().sanitizeDB(helper, db); 53 t.commit(); 54 return true; 55 } catch (Exception e) { 56 FileLog.e(TAG, "Failed to verify db", e); 57 return false; 58 } 59 } 60 61 /** 62 * Makes the following changes in the provider DB. 63 * 1. Removes all entries belonging to a managed profile as managed profiles 64 * cannot be restored. 65 * 2. Marks all entries as restored. The flags are updated during first load or as 66 * the restored apps get installed. 67 * 3. If the user serial for primary profile is different than that of the previous device, 68 * update the entries to the new profile id. 69 */ 70 private void sanitizeDB(DatabaseHelper helper, SQLiteDatabase db) throws Exception { 71 long oldProfileId = getDefaultProfileId(db); 72 // Delete all entries which do not belong to the main user 73 int itemsDeleted = db.delete( 74 Favorites.TABLE_NAME, "profileId != ?", new String[]{Long.toString(oldProfileId)}); 75 if (itemsDeleted > 0) { 76 FileLog.d(TAG, itemsDeleted + " items belonging to a managed profile, were deleted"); 77 } 78 79 // Mark all items as restored. 80 boolean keepAllIcons = Utilities.isPropertyEnabled(LogConfig.KEEP_ALL_ICONS); 81 ContentValues values = new ContentValues(); 82 values.put(Favorites.RESTORED, ShortcutInfo.FLAG_RESTORED_ICON 83 | (keepAllIcons ? ShortcutInfo.FLAG_RESTORE_STARTED : 0)); 84 db.update(Favorites.TABLE_NAME, values, null, null); 85 86 // Mark widgets with appropriate restore flag 87 values.put(Favorites.RESTORED, LauncherAppWidgetInfo.FLAG_ID_NOT_VALID | 88 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY | 89 LauncherAppWidgetInfo.FLAG_UI_NOT_READY | 90 (keepAllIcons ? LauncherAppWidgetInfo.FLAG_RESTORE_STARTED : 0)); 91 db.update(Favorites.TABLE_NAME, values, "itemType = ?", 92 new String[]{Integer.toString(Favorites.ITEM_TYPE_APPWIDGET)}); 93 94 long myProfileId = helper.getDefaultUserSerial(); 95 if (Utilities.longCompare(oldProfileId, myProfileId) != 0) { 96 FileLog.d(TAG, "Changing primary user id from " + oldProfileId + " to " + myProfileId); 97 migrateProfileId(db, myProfileId); 98 } 99 } 100 101 /** 102 * Updates profile id of all entries and changes the default value for the column. 103 */ 104 protected void migrateProfileId(SQLiteDatabase db, long newProfileId) { 105 // Update existing entries. 106 ContentValues values = new ContentValues(); 107 values.put(Favorites.PROFILE_ID, newProfileId); 108 db.update(Favorites.TABLE_NAME, values, null, null); 109 110 // Change default value of the column. 111 db.execSQL("ALTER TABLE favorites RENAME TO favorites_old;"); 112 Favorites.addTableToDb(db, newProfileId, false); 113 db.execSQL("INSERT INTO favorites SELECT * FROM favorites_old;"); 114 db.execSQL("DROP TABLE favorites_old;"); 115 } 116 117 /** 118 * Returns the profile id used in the favorites table of the provided db. 119 */ 120 protected long getDefaultProfileId(SQLiteDatabase db) throws Exception { 121 try (Cursor c = db.rawQuery("PRAGMA table_info (favorites)", null)){ 122 int nameIndex = c.getColumnIndex(INFO_COLUMN_NAME); 123 while (c.moveToNext()) { 124 if (Favorites.PROFILE_ID.equals(c.getString(nameIndex))) { 125 return c.getLong(c.getColumnIndex(INFO_COLUMN_DEFAULT_VALUE)); 126 } 127 } 128 throw new InvalidObjectException("Table does not have a profile id column"); 129 } 130 } 131 132 public static boolean isPending(Context context) { 133 return Utilities.getPrefs(context).getBoolean(RESTORE_TASK_PENDING, false); 134 } 135 136 public static void setPending(Context context, boolean isPending) { 137 FileLog.d(TAG, "Restore data received through full backup " + isPending); 138 Utilities.getPrefs(context).edit().putBoolean(RESTORE_TASK_PENDING, isPending).commit(); 139 } 140 } 141