Skip to content

Commit

Permalink
Compat users upgrade from Lawnchair 13
Browse files Browse the repository at this point in the history
  • Loading branch information
Goooler committed Dec 22, 2023
1 parent a24b3f9 commit 49e0b28
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/com/android/launcher3/provider/LauncherDbUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
import com.android.launcher3.util.IntArray;
import com.android.launcher3.util.IntSet;

import java.util.ArrayList;

/**
* A set of utility methods for Launcher DB used for DB updates and migration.
*/
Expand Down Expand Up @@ -171,6 +173,10 @@ public static void migrateLegacyShortcuts(Context context, SQLiteDatabase db) {
null);
}

// Compat users upgrade from Lawnchair 13.
removeColumn(db, Favorites.TABLE_NAME, "iconPackage");
removeColumn(db, Favorites.TABLE_NAME, "iconResource");

// Drop the unused columns
db.execSQL("ALTER TABLE " + Favorites.TABLE_NAME + " DROP COLUMN iconPackage;");
db.execSQL("ALTER TABLE " + Favorites.TABLE_NAME + " DROP COLUMN iconResource;");
Expand Down Expand Up @@ -200,4 +206,28 @@ public SQLiteDatabase getDb() {
return mDb;
}
}

private static void removeColumn(SQLiteDatabase db, String tableName, String columnName) {
final var columns = new ArrayList<>();
// Get all column names from the table
try (Cursor c = db.rawQuery("PRAGMA table_info(" + tableName + ")", null)) {
while (c.moveToNext()) {
String column = c.getString(c.getColumnIndex("name"));
if (!column.equals(columnName)) {
columns.add(column);
}
}
}

// Create a new table without the column
String newTable = tableName + "_new";
String columnsSeparated = TextUtils.join(",", columns);
db.execSQL("CREATE TABLE " + newTable + " AS SELECT " + columnsSeparated + " FROM " + tableName);

// Drop the old table
db.execSQL("DROP TABLE " + tableName);

// Rename the new table to the old table's name
db.execSQL("ALTER TABLE " + newTable + " RENAME TO " + tableName);
}
}

0 comments on commit 49e0b28

Please sign in to comment.