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