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