Search in sources :

Example 1 with ContentWriter

use of com.android.launcher3.util.ContentWriter in project android_packages_apps_Launcher3 by crdroidandroid.

the class ModelWriter method addItemToDatabase.

/**
 * Add an item to the database in a specified container. Sets the container, screen, cellX and
 * cellY fields of the item. Also assigns an ID to the item.
 */
public void addItemToDatabase(final ItemInfo item, int container, int screenId, int cellX, int cellY) {
    updateItemInfoProps(item, container, screenId, cellX, cellY);
    final ContentResolver cr = mContext.getContentResolver();
    item.id = Settings.call(cr, Settings.METHOD_NEW_ITEM_ID).getInt(Settings.EXTRA_VALUE);
    ModelVerifier verifier = new ModelVerifier();
    final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
    ((Executor) MODEL_EXECUTOR).execute(() -> {
        // Write the item on background thread, as some properties might have been updated in
        // the background.
        final ContentWriter writer = new ContentWriter(mContext);
        item.onAddToDatabase(writer);
        writer.put(Favorites._ID, item.id);
        cr.insert(Favorites.CONTENT_URI, writer.getValues(mContext));
        synchronized (mBgDataModel) {
            checkItemInfoLocked(item.id, item, stackTrace);
            mBgDataModel.addItem(mContext, item, true);
            verifier.verifyModel();
        }
    });
}
Also used : ContentWriter(com.android.launcher3.util.ContentWriter) Executor(java.util.concurrent.Executor) ContentResolver(android.content.ContentResolver)

Example 2 with ContentWriter

use of com.android.launcher3.util.ContentWriter in project android_packages_apps_Launcher3 by crdroidandroid.

the class WidgetUtils method addItemToScreen.

/**
 * Adds {@param item} on the homescreen on the 0th screen
 */
public static void addItemToScreen(ItemInfo item, Context targetContext) {
    ContentResolver resolver = targetContext.getContentResolver();
    int screenId = FIRST_SCREEN_ID;
    // Update the screen id counter for the provider.
    LauncherSettings.Settings.call(resolver, LauncherSettings.Settings.METHOD_NEW_SCREEN_ID);
    if (screenId > FIRST_SCREEN_ID) {
        screenId = FIRST_SCREEN_ID;
    }
    // Insert the item
    ContentWriter writer = new ContentWriter(targetContext);
    item.id = LauncherSettings.Settings.call(resolver, LauncherSettings.Settings.METHOD_NEW_ITEM_ID).getInt(LauncherSettings.Settings.EXTRA_VALUE);
    item.screenId = screenId;
    item.onAddToDatabase(writer);
    writer.put(LauncherSettings.Favorites._ID, item.id);
    resolver.insert(LauncherSettings.Favorites.CONTENT_URI, writer.getValues(targetContext));
}
Also used : ContentWriter(com.android.launcher3.util.ContentWriter) ContentResolver(android.content.ContentResolver)

Example 3 with ContentWriter

use of com.android.launcher3.util.ContentWriter in project android_packages_apps_Launcher3 by AOSPA.

the class ModelWriter method updateItemInDatabase.

/**
 * Update an item to the database in a specified container.
 */
public void updateItemInDatabase(ItemInfo item) {
    notifyItemModified(item);
    MODEL_EXECUTOR.execute(new UpdateItemRunnable(item, () -> {
        ContentWriter writer = new ContentWriter(mContext);
        item.onAddToDatabase(writer);
        return writer;
    }));
}
Also used : ContentWriter(com.android.launcher3.util.ContentWriter)

Example 4 with ContentWriter

use of com.android.launcher3.util.ContentWriter in project android_packages_apps_Trebuchet by LineageOS.

the class WidgetUtils method addItemToScreen.

/**
 * Adds {@param item} on the homescreen on the 0th screen
 */
public static void addItemToScreen(ItemInfo item, Context targetContext) {
    ContentResolver resolver = targetContext.getContentResolver();
    int screenId = FIRST_SCREEN_ID;
    // Update the screen id counter for the provider.
    LauncherSettings.Settings.call(resolver, LauncherSettings.Settings.METHOD_NEW_SCREEN_ID);
    if (screenId > FIRST_SCREEN_ID) {
        screenId = FIRST_SCREEN_ID;
    }
    // Insert the item
    ContentWriter writer = new ContentWriter(targetContext);
    item.id = LauncherSettings.Settings.call(resolver, LauncherSettings.Settings.METHOD_NEW_ITEM_ID).getInt(LauncherSettings.Settings.EXTRA_VALUE);
    item.screenId = screenId;
    item.onAddToDatabase(writer);
    writer.put(LauncherSettings.Favorites._ID, item.id);
    resolver.insert(LauncherSettings.Favorites.CONTENT_URI, writer.getValues(targetContext));
}
Also used : ContentWriter(com.android.launcher3.util.ContentWriter) ContentResolver(android.content.ContentResolver)

Example 5 with ContentWriter

use of com.android.launcher3.util.ContentWriter in project android_packages_apps_Trebuchet by LineageOS.

the class AppWidgetsRestoredReceiver method restoreAppWidgetIds.

/**
 * Updates the app widgets whose id has changed during the restore process.
 */
@WorkerThread
public static void restoreAppWidgetIds(Context context, int[] oldWidgetIds, int[] newWidgetIds) {
    AppWidgetHost appWidgetHost = new LauncherAppWidgetHost(context);
    if (WidgetsModel.GO_DISABLE_WIDGETS) {
        Log.e(TAG, "Skipping widget ID remap as widgets not supported");
        appWidgetHost.deleteHost();
        return;
    }
    if (!RestoreDbTask.isPending(context)) {
        // Someone has already gone through our DB once, probably LoaderTask. Skip any further
        // modifications of the DB.
        Log.e(TAG, "Skipping widget ID remap as DB already in use");
        for (int widgetId : newWidgetIds) {
            Log.d(TAG, "Deleting widgetId: " + widgetId);
            appWidgetHost.deleteAppWidgetId(widgetId);
        }
        return;
    }
    final ContentResolver cr = context.getContentResolver();
    final AppWidgetManager widgets = AppWidgetManager.getInstance(context);
    for (int i = 0; i < oldWidgetIds.length; i++) {
        Log.i(TAG, "Widget state restore id " + oldWidgetIds[i] + " => " + newWidgetIds[i]);
        final AppWidgetProviderInfo provider = widgets.getAppWidgetInfo(newWidgetIds[i]);
        final int state;
        if (LoaderTask.isValidProvider(provider)) {
            // This will ensure that we show 'Click to setup' UI if required.
            state = LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
        } else {
            state = LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY;
        }
        // b/135926478: Work profile widget restore is broken in platform. This forces us to
        // recreate the widget during loading with the correct host provider.
        long mainProfileId = UserCache.INSTANCE.get(context).getSerialNumberForUser(myUserHandle());
        String oldWidgetId = Integer.toString(oldWidgetIds[i]);
        int result = new ContentWriter(context, new ContentWriter.CommitParams("appWidgetId=? and (restored & 1) = 1 and profileId=?", new String[] { oldWidgetId, Long.toString(mainProfileId) })).put(LauncherSettings.Favorites.APPWIDGET_ID, newWidgetIds[i]).put(LauncherSettings.Favorites.RESTORED, state).commit();
        if (result == 0) {
            Cursor cursor = cr.query(Favorites.CONTENT_URI, new String[] { Favorites.APPWIDGET_ID }, "appWidgetId=?", new String[] { oldWidgetId }, null);
            try {
                if (!cursor.moveToFirst()) {
                    // The widget no long exists.
                    appWidgetHost.deleteAppWidgetId(newWidgetIds[i]);
                }
            } finally {
                cursor.close();
            }
        }
    }
    LauncherAppState app = LauncherAppState.getInstanceNoCreate();
    if (app != null) {
        app.getModel().forceReload();
    }
}
Also used : ContentWriter(com.android.launcher3.util.ContentWriter) AppWidgetHost(android.appwidget.AppWidgetHost) AppWidgetManager(android.appwidget.AppWidgetManager) AppWidgetProviderInfo(android.appwidget.AppWidgetProviderInfo) Cursor(android.database.Cursor) ContentResolver(android.content.ContentResolver) WorkerThread(androidx.annotation.WorkerThread)

Aggregations

ContentWriter (com.android.launcher3.util.ContentWriter)37 ContentResolver (android.content.ContentResolver)14 AppWidgetHost (android.appwidget.AppWidgetHost)6 AppWidgetManager (android.appwidget.AppWidgetManager)6 AppWidgetProviderInfo (android.appwidget.AppWidgetProviderInfo)6 Intent (android.content.Intent)6 Cursor (android.database.Cursor)6 Rect (android.graphics.Rect)6 WorkerThread (androidx.annotation.WorkerThread)6 GridOccupancy (com.android.launcher3.util.GridOccupancy)6 Executor (java.util.concurrent.Executor)6 WorkspaceItemInfo (com.android.launcher3.model.data.WorkspaceItemInfo)5 LauncherAppWidgetHost (com.android.launcher3.widget.LauncherAppWidgetHost)4 WorkspaceItemInfo (com.android.launcher3.WorkspaceItemInfo)1 ExecutorService (java.util.concurrent.ExecutorService)1