Sunny Goyal | c190dbf | 2016-05-05 14:37:05 -0700 | [diff] [blame] | 1 | package com.android.launcher3; |
| 2 | |
| 3 | import android.app.backup.BackupAgent; |
| 4 | import android.app.backup.BackupDataInput; |
| 5 | import android.app.backup.BackupDataOutput; |
| 6 | import android.content.ContentValues; |
| 7 | import android.database.Cursor; |
| 8 | import android.database.sqlite.SQLiteDatabase; |
| 9 | import android.os.ParcelFileDescriptor; |
| 10 | |
| 11 | import com.android.launcher3.LauncherProvider.DatabaseHelper; |
| 12 | import com.android.launcher3.LauncherSettings.Favorites; |
| 13 | import com.android.launcher3.logging.FileLog; |
| 14 | |
| 15 | import java.io.InvalidObjectException; |
| 16 | |
| 17 | public class LauncherBackupAgent extends BackupAgent { |
| 18 | |
| 19 | private static final String TAG = "LauncherBackupAgent"; |
| 20 | |
| 21 | private static final String INFO_COLUMN_NAME = "name"; |
| 22 | private static final String INFO_COLUMN_DEFAULT_VALUE = "dflt_value"; |
| 23 | |
| 24 | @Override |
| 25 | public void onRestore( |
| 26 | BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) { |
| 27 | // Doesn't do incremental backup/restore |
| 28 | } |
| 29 | |
| 30 | @Override |
| 31 | public void onBackup( |
| 32 | ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState) { |
| 33 | // Doesn't do incremental backup/restore |
| 34 | } |
| 35 | |
| 36 | @Override |
| 37 | public void onRestoreFinished() { |
| 38 | DatabaseHelper helper = new DatabaseHelper(this, null, LauncherFiles.LAUNCHER_DB); |
| 39 | |
| 40 | if (!sanitizeDBSafely(helper)) { |
| 41 | helper.createEmptyDB(helper.getWritableDatabase()); |
| 42 | } |
| 43 | |
| 44 | try { |
| 45 | // Flush all logs before the process is killed. |
| 46 | FileLog.flushAll(null); |
| 47 | } catch (Exception e) { } |
| 48 | } |
| 49 | |
| 50 | private boolean sanitizeDBSafely(DatabaseHelper helper) { |
| 51 | SQLiteDatabase db = helper.getWritableDatabase(); |
| 52 | db.beginTransaction(); |
| 53 | try { |
| 54 | sanitizeDB(helper, db); |
| 55 | db.setTransactionSuccessful(); |
| 56 | return true; |
| 57 | } catch (Exception e) { |
| 58 | FileLog.e(TAG, "Failed to verify db", e); |
| 59 | return false; |
| 60 | } finally { |
| 61 | db.endTransaction(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Makes the following changes in the provider DB. |
| 67 | * 1. Removes all entries belonging to a managed profile as managed profiles |
| 68 | * cannot be restored. |
| 69 | * 2. Marks all entries as restored. The flags are updated during first load or as |
| 70 | * the restored apps get installed. |
| 71 | * 3. If the user serial for primary profile is different than that of the previous device, |
| 72 | * update the entries to the new profile id. |
| 73 | */ |
| 74 | private void sanitizeDB(DatabaseHelper helper, SQLiteDatabase db) throws Exception { |
| 75 | long oldProfileId = getDefaultProfileId(db); |
| 76 | // Delete all entries which do not belong to the main user |
| 77 | int itemsDeleted = db.delete( |
| 78 | Favorites.TABLE_NAME, "profileId != ?", new String[]{Long.toString(oldProfileId)}); |
| 79 | if (itemsDeleted > 0) { |
| 80 | FileLog.d(TAG, itemsDeleted + " items belonging to a managed profile, were deleted"); |
| 81 | } |
| 82 | |
| 83 | // Mark all items as restored. |
| 84 | ContentValues values = new ContentValues(); |
| 85 | values.put(Favorites.RESTORED, 1); |
| 86 | db.update(Favorites.TABLE_NAME, values, null, null); |
| 87 | |
| 88 | // Mark widgets with appropriate restore flag |
| 89 | values.put(Favorites.RESTORED, |
| 90 | LauncherAppWidgetInfo.FLAG_ID_NOT_VALID | |
| 91 | LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY | |
| 92 | LauncherAppWidgetInfo.FLAG_UI_NOT_READY); |
| 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 | */ |
| 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 for used in the favorites table of the provided db. |
| 121 | */ |
| 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 | } |