when LauncherProvider.DatabaseHelper.convertDatabase(SQLiteDatabase db) return true in Android default Launcher?

StackOverflow https://stackoverflow.com/questions/9374673

  •  28-10-2019
  •  | 
  •  

Question

I meet a problem when read the source code of Android 2.2 default Launcher.
the source code segment of LauncherProvider.DatabaseHelper:

@Override
public void onCreate(SQLiteDatabase db) {
    if (LOGD)
    Log.d(TAG, "creating new launcher database");

    db.execSQL("CREATE TABLE favorites (" + "_id INTEGER PRIMARY KEY,"
        + "title TEXT," + "intent TEXT," + "container INTEGER,"
        + "screen INTEGER," + "cellX INTEGER," + "cellY INTEGER,"
        + "spanX INTEGER," + "spanY INTEGER," + "itemType INTEGER,"
        + "appWidgetId INTEGER NOT NULL DEFAULT -1,"
        + "isShortcut INTEGER," + "iconType INTEGER,"
        + "iconPackage TEXT," + "iconResource TEXT," + "icon BLOB,"
        + "uri TEXT," + "displayMode INTEGER" + ");");

    // Database was just created, so wipe any previous widgets
    if (mAppWidgetHost != null) {
    mAppWidgetHost.deleteHost();
    sendAppWidgetResetNotify();
    }

    if (!convertDatabase(db)) {
    // Populate favorites table with initial favorites
    loadFavorites(db);
    }
}

private boolean convertDatabase(SQLiteDatabase db) {
    if (LOGD)
    Log.d(TAG,
        "converting database from an older format, but not onUpgrade");
    boolean converted = false;

    final Uri uri = Uri.parse("content://" + Settings.AUTHORITY
        + "/old_favorites?notify=true");
    final ContentResolver resolver = mContext.getContentResolver();
    Cursor cursor = null;

    try {
    cursor = resolver.query(uri, null, null, null, null);
    } catch (Exception e) {
    // Ignore
    }

    // We already have a favorites database in the old provider
    if (cursor != null && cursor.getCount() > 0) {
    try {
        converted = copyFromCursor(db, cursor) > 0;
    } finally {
        cursor.close();
    }

    if (converted) {
        resolver.delete(uri, null, null);
    }
    }

    if (converted) {
    // Convert widgets from this import into widgets
    if (LOGD)
        Log.d(TAG, "converted and now triggering widget upgrade");
    convertWidgets(db);
    }

    return converted;
}

onCreate() is called to create database when there is no database in system. At the tail of onCreate(), it calls convertDatabase(SQLiteDatabase db). And there have query codes in convertDatabase(SQLiteDatabase db).I am confused. Why it query in onCreate()? when convertDatabase(SQLiteDatabase db) will return true? My Android version is 2.2

Was it helpful?

Solution

Some context can be retrieved from log messages:

if (LOGD) Log.d(TAG, "converting database from an older format, but not onUpgrade");

If there was a database in old format, convertDatabase() copies all entries to the new one using copyFromCursor where cursor points to the old instance and then deletes it. So the method will return true if it has found and converted an instance of the database in the old format and false otherwise.

As the launcher actually called Launcher2 most likely it tries to find traces of the predecessor.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top