Merge "Removing all usage of LauncherProvider" into udc-qpr-dev

This commit is contained in:
Sunny Goyal authored and Android (Google) Code Review committed 2023-06-07 22:03:48 +00:00
commit cda1842e7a
26 files changed
+326 -518

No files matched your search

@@ -31,7 +31,7 @@ import androidx.annotation.Nullable;
import com.android.launcher3.BubbleTextView; import com.android.launcher3.BubbleTextView;
import com.android.launcher3.LauncherAppState; import com.android.launcher3.LauncherAppState;
import com.android.launcher3.LauncherSettings; import com.android.launcher3.LauncherModel;
import com.android.launcher3.ShortcutAndWidgetContainer; import com.android.launcher3.ShortcutAndWidgetContainer;
import com.android.launcher3.testing.shared.TestProtocol; import com.android.launcher3.testing.shared.TestProtocol;
@@ -189,10 +189,11 @@ public class DebugTestInformationHandler extends TestInformationHandler {
case TestProtocol.REQUEST_CLEAR_DATA: { case TestProtocol.REQUEST_CLEAR_DATA: {
final long identity = Binder.clearCallingIdentity(); final long identity = Binder.clearCallingIdentity();
try { try {
LauncherSettings.Settings.call(mContext.getContentResolver(), MODEL_EXECUTOR.execute(() -> {
LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB); LauncherModel model = LauncherAppState.getInstance(mContext).getModel();
MAIN_EXECUTOR.submit(() -> model.getModelDbController().createEmptyDB();
LauncherAppState.getInstance(mContext).getModel().forceReload()); MAIN_EXECUTOR.execute(model::forceReload);
});
return response; return response;
} finally { } finally {
Binder.restoreCallingIdentity(identity); Binder.restoreCallingIdentity(identity);
@@ -95,9 +95,7 @@ public class HotseatEduController {
} }
} }
if (pageId == -1) { if (pageId == -1) {
pageId = LauncherSettings.Settings.call(mLauncher.getContentResolver(), pageId = mLauncher.getModel().getModelDbController().getNewScreenId();
LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
.getInt(LauncherSettings.Settings.EXTRA_VALUE);
mNewScreens = IntArray.wrap(pageId); mNewScreens = IntArray.wrap(pageId);
} }
boolean isPortrait = !mLauncher.getDeviceProfile().isVerticalBarLayout(); boolean isPortrait = !mLauncher.getDeviceProfile().isVerticalBarLayout();
@@ -22,9 +22,10 @@ import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;
import android.content.Context; import android.content.Context;
import com.android.launcher3.LauncherAppState; import com.android.launcher3.LauncherAppState;
import com.android.launcher3.LauncherSettings; import com.android.launcher3.LauncherModel;
import com.android.launcher3.model.GridBackupTable; import com.android.launcher3.model.GridBackupTable;
import com.android.launcher3.provider.LauncherDbUtils; import com.android.launcher3.model.ModelDbController;
import com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction;
/** /**
* A helper class to manage migration revert restoration for hybrid hotseat * A helper class to manage migration revert restoration for hybrid hotseat
@@ -36,16 +37,13 @@ public class HotseatRestoreHelper {
*/ */
public static void createBackup(Context context) { public static void createBackup(Context context) {
MODEL_EXECUTOR.execute(() -> { MODEL_EXECUTOR.execute(() -> {
try (LauncherDbUtils.SQLiteTransaction transaction = (LauncherDbUtils.SQLiteTransaction) ModelDbController dbController = LauncherAppState.getInstance(context)
LauncherSettings.Settings.call( .getModel().getModelDbController();
context.getContentResolver(), try (SQLiteTransaction transaction = dbController.newTransaction()) {
LauncherSettings.Settings.METHOD_NEW_TRANSACTION)
.getBinder(LauncherSettings.Settings.EXTRA_VALUE)) {
GridBackupTable backupTable = new GridBackupTable(context, transaction.getDb()); GridBackupTable backupTable = new GridBackupTable(context, transaction.getDb());
backupTable.createCustomBackupTable(HYBRID_HOTSEAT_BACKUP_TABLE); backupTable.createCustomBackupTable(HYBRID_HOTSEAT_BACKUP_TABLE);
transaction.commit(); transaction.commit();
LauncherSettings.Settings.call(context.getContentResolver(), dbController.refreshHotseatRestoreTable();
LauncherSettings.Settings.METHOD_REFRESH_HOTSEAT_RESTORE_TABLE);
} }
}); });
} }
@@ -55,18 +53,15 @@ public class HotseatRestoreHelper {
*/ */
public static void restoreBackup(Context context) { public static void restoreBackup(Context context) {
MODEL_EXECUTOR.execute(() -> { MODEL_EXECUTOR.execute(() -> {
try (LauncherDbUtils.SQLiteTransaction transaction = (LauncherDbUtils.SQLiteTransaction) LauncherModel model = LauncherAppState.getInstance(context).getModel();
LauncherSettings.Settings.call( try (SQLiteTransaction transaction = model.getModelDbController().newTransaction()) {
context.getContentResolver(),
LauncherSettings.Settings.METHOD_NEW_TRANSACTION)
.getBinder(LauncherSettings.Settings.EXTRA_VALUE)) {
if (!tableExists(transaction.getDb(), HYBRID_HOTSEAT_BACKUP_TABLE)) { if (!tableExists(transaction.getDb(), HYBRID_HOTSEAT_BACKUP_TABLE)) {
return; return;
} }
GridBackupTable backupTable = new GridBackupTable(context, transaction.getDb()); GridBackupTable backupTable = new GridBackupTable(context, transaction.getDb());
backupTable.restoreFromCustomBackupTable(HYBRID_HOTSEAT_BACKUP_TABLE, true); backupTable.restoreFromCustomBackupTable(HYBRID_HOTSEAT_BACKUP_TABLE, true);
transaction.commit(); transaction.commit();
LauncherAppState.getInstance(context).getModel().forceReload(); model.forceReload();
} }
}); });
} }
@@ -17,8 +17,8 @@ package com.android.quickstep;
import static androidx.test.InstrumentationRegistry.getContext; import static androidx.test.InstrumentationRegistry.getContext;
import static androidx.test.InstrumentationRegistry.getInstrumentation; import static androidx.test.InstrumentationRegistry.getInstrumentation;
import static androidx.test.InstrumentationRegistry.getTargetContext;
import static com.android.launcher3.WorkspaceLayoutManager.FIRST_SCREEN_ID;
import static com.android.launcher3.testcomponent.TestCommandReceiver.EXTRA_VALUE; import static com.android.launcher3.testcomponent.TestCommandReceiver.EXTRA_VALUE;
import static com.android.launcher3.testcomponent.TestCommandReceiver.SET_LIST_VIEW_SERVICE_BINDER; import static com.android.launcher3.testcomponent.TestCommandReceiver.SET_LIST_VIEW_SERVICE_BINDER;
import static com.android.launcher3.util.WidgetUtils.createWidgetInfo; import static com.android.launcher3.util.WidgetUtils.createWidgetInfo;
@@ -32,7 +32,6 @@ import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetManager;
import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
@@ -42,14 +41,16 @@ import android.view.View;
import android.view.ViewConfiguration; import android.view.ViewConfiguration;
import android.widget.RemoteViews; import android.widget.RemoteViews;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest; import androidx.test.filters.LargeTest;
import androidx.test.filters.Suppress; import androidx.test.filters.Suppress;
import androidx.test.runner.AndroidJUnit4;
import androidx.test.uiautomator.By; import androidx.test.uiautomator.By;
import androidx.test.uiautomator.UiDevice; import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.Until; import androidx.test.uiautomator.Until;
import com.android.launcher3.LauncherSettings; import com.android.launcher3.LauncherAppState;
import com.android.launcher3.LauncherModel;
import com.android.launcher3.celllayout.FavoriteItemsTransaction;
import com.android.launcher3.model.data.LauncherAppWidgetInfo; import com.android.launcher3.model.data.LauncherAppWidgetInfo;
import com.android.launcher3.tapl.LaunchedAppState; import com.android.launcher3.tapl.LaunchedAppState;
import com.android.launcher3.testcomponent.ListViewService; import com.android.launcher3.testcomponent.ListViewService;
@@ -57,6 +58,7 @@ import com.android.launcher3.testcomponent.ListViewService.SimpleViewsFactory;
import com.android.launcher3.testcomponent.TestCommandReceiver; import com.android.launcher3.testcomponent.TestCommandReceiver;
import com.android.launcher3.ui.TaplTestsLauncher3; import com.android.launcher3.ui.TaplTestsLauncher3;
import com.android.launcher3.ui.TestViewHelpers; import com.android.launcher3.ui.TestViewHelpers;
import com.android.launcher3.util.Executors;
import com.android.launcher3.widget.LauncherAppWidgetProviderInfo; import com.android.launcher3.widget.LauncherAppWidgetProviderInfo;
import com.android.quickstep.NavigationModeSwitchRule.NavigationModeSwitch; import com.android.quickstep.NavigationModeSwitchRule.NavigationModeSwitch;
@@ -67,6 +69,7 @@ import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer; import org.mockito.stubbing.Answer;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.IntConsumer; import java.util.function.IntConsumer;
/** /**
@@ -84,9 +87,9 @@ import java.util.function.IntConsumer;
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
public class ViewInflationDuringSwipeUp extends AbstractQuickStepTest { public class ViewInflationDuringSwipeUp extends AbstractQuickStepTest {
private ContentResolver mResolver;
private SparseArray<ViewConfiguration> mConfigMap; private SparseArray<ViewConfiguration> mConfigMap;
private InitTracker mInitTracker; private InitTracker mInitTracker;
private LauncherModel mModel;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
@@ -101,8 +104,8 @@ public class ViewInflationDuringSwipeUp extends AbstractQuickStepTest {
TaplTestsLauncher3.initialize(this); TaplTestsLauncher3.initialize(this);
mResolver = mTargetContext.getContentResolver(); mModel = LauncherAppState.getInstance(mTargetContext).getModel();
LauncherSettings.Settings.call(mResolver, LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB); Executors.MODEL_EXECUTOR.submit(mModel.getModelDbController()::createEmptyDB).get();
// Get static configuration map // Get static configuration map
Field field = ViewConfiguration.class.getDeclaredField("sConfigurations"); Field field = ViewConfiguration.class.getDeclaredField("sConfigurations");
@@ -182,26 +185,30 @@ public class ViewInflationDuringSwipeUp extends AbstractQuickStepTest {
private void executeSwipeUpTestWithWidget(IntConsumer widgetIdCreationCallback, private void executeSwipeUpTestWithWidget(IntConsumer widgetIdCreationCallback,
IntConsumer updateBeforeSwipeUp, String finalWidgetText) { IntConsumer updateBeforeSwipeUp, String finalWidgetText) {
try { try {
// Clear all existing data
LauncherSettings.Settings.call(mResolver,
LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB);
LauncherSettings.Settings.call(mResolver,
LauncherSettings.Settings.METHOD_CLEAR_EMPTY_DB_FLAG);
LauncherAppWidgetProviderInfo info = TestViewHelpers.findWidgetProvider(false); LauncherAppWidgetProviderInfo info = TestViewHelpers.findWidgetProvider(false);
// Make sure the widget is big enough to show a list of items // Make sure the widget is big enough to show a list of items
info.minSpanX = 2; info.minSpanX = 2;
info.minSpanY = 2; info.minSpanY = 2;
info.spanX = 2; info.spanX = 2;
info.spanY = 2; info.spanY = 2;
LauncherAppWidgetInfo item = createWidgetInfo(info, getTargetContext(), true); AtomicInteger widgetId = new AtomicInteger();
new FavoriteItemsTransaction(mTargetContext)
.addItem(() -> {
LauncherAppWidgetInfo item = createWidgetInfo(info, mTargetContext, true);
item.screenId = FIRST_SCREEN_ID;
widgetId.set(item.appWidgetId);
return item;
})
.commitAndLoadHome(mLauncher);
addItemToScreen(item);
assertTrue("Widget is not present", assertTrue("Widget is not present",
mLauncher.goHome().tryGetWidget(info.label, DEFAULT_UI_TIMEOUT) != null); mLauncher.goHome().tryGetWidget(info.label, DEFAULT_UI_TIMEOUT) != null);
int widgetId = item.appWidgetId;
// Verify widget id // Verify widget id
widgetIdCreationCallback.accept(widgetId); widgetIdCreationCallback.accept(widgetId.get());
// Go to overview once so that all views are initialized and cached // Go to overview once so that all views are initialized and cached
startAppFast(resolveSystemApp(Intent.CATEGORY_APP_CALCULATOR)); startAppFast(resolveSystemApp(Intent.CATEGORY_APP_CALCULATOR));
@@ -214,7 +221,7 @@ public class ViewInflationDuringSwipeUp extends AbstractQuickStepTest {
LaunchedAppState launchedAppState = mLauncher.getLaunchedAppState(); LaunchedAppState launchedAppState = mLauncher.getLaunchedAppState();
// Update widget // Update widget
updateBeforeSwipeUp.accept(widgetId); updateBeforeSwipeUp.accept(widgetId.get());
launchedAppState.switchToOverview(); launchedAppState.switchToOverview();
assertEquals("Views inflated during swipe up", 0, mInitTracker.viewInitCount); assertEquals("Views inflated during swipe up", 0, mInitTracker.viewInitCount);
@@ -17,6 +17,8 @@
package com.android.launcher3; package com.android.launcher3;
import static com.android.launcher3.LauncherSettings.Favorites.ITEM_TYPE_APPLICATION; import static com.android.launcher3.LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
import static com.android.launcher3.LauncherSettings.Favorites.TABLE_NAME;
import static com.android.launcher3.provider.LauncherDbUtils.itemIdMatch;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.ContentValues; import android.content.ContentValues;
@@ -30,7 +32,6 @@ import android.content.res.Resources;
import android.content.res.Resources.NotFoundException; import android.content.res.Resources.NotFoundException;
import android.content.res.XmlResourceParser; import android.content.res.XmlResourceParser;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Process; import android.os.Process;
import android.text.TextUtils; import android.text.TextUtils;
@@ -44,7 +45,6 @@ import androidx.annotation.StringRes;
import androidx.annotation.WorkerThread; import androidx.annotation.WorkerThread;
import androidx.annotation.XmlRes; import androidx.annotation.XmlRes;
import com.android.launcher3.LauncherProvider.SqlArguments;
import com.android.launcher3.LauncherSettings.Favorites; import com.android.launcher3.LauncherSettings.Favorites;
import com.android.launcher3.model.data.AppInfo; import com.android.launcher3.model.data.AppInfo;
import com.android.launcher3.model.data.LauncherAppWidgetInfo; import com.android.launcher3.model.data.LauncherAppWidgetInfo;
@@ -619,9 +619,7 @@ public class AutoInstallsLayout {
// failed to add, and less than 2 were actually added // failed to add, and less than 2 were actually added
if (folderItems.size() < 2) { if (folderItems.size() < 2) {
// Delete the folder // Delete the folder
Uri uri = Favorites.getContentUri(folderId); mDb.delete(TABLE_NAME, itemIdMatch(folderId), null);
SqlArguments args = new SqlArguments(uri, null, null);
mDb.delete(args.table, args.where, args.args);
addedId = -1; addedId = -1;
// If we have a single item, promote it to where the folder // If we have a single item, promote it to where the folder
@@ -634,7 +632,7 @@ public class AutoInstallsLayout {
copyInteger(myValues, childValues, Favorites.CELLY); copyInteger(myValues, childValues, Favorites.CELLY);
addedId = folderItems.get(0); addedId = folderItems.get(0);
mDb.update(Favorites.TABLE_NAME, childValues, mDb.update(TABLE_NAME, childValues,
Favorites._ID + "=" + addedId, null); Favorites._ID + "=" + addedId, null);
} }
} }
@@ -143,6 +143,8 @@ public class LauncherModel extends LauncherApps.Callback implements InstallSessi
@NonNull @NonNull
private final ModelDelegate mModelDelegate; private final ModelDelegate mModelDelegate;
private int mLastLoadId = -1;
// Runnable to check if the shortcuts permission has changed. // Runnable to check if the shortcuts permission has changed.
@NonNull @NonNull
private final Runnable mDataValidationCheck = new Runnable() { private final Runnable mDataValidationCheck = new Runnable() {
@@ -553,6 +555,7 @@ public class LauncherModel extends LauncherApps.Callback implements InstallSessi
if (mLoaderTask != task) { if (mLoaderTask != task) {
throw new CancellationException("Loader already stopped"); throw new CancellationException("Loader already stopped");
} }
mLastLoadId++;
mTask = task; mTask = task;
mIsLoaderTaskRunning = true; mIsLoaderTaskRunning = true;
mModelLoaded = false; mModelLoaded = false;
@@ -722,4 +725,12 @@ public class LauncherModel extends LauncherApps.Callback implements InstallSessi
return mCallbacksList.toArray(new Callbacks[mCallbacksList.size()]); return mCallbacksList.toArray(new Callbacks[mCallbacksList.size()]);
} }
} }
/**
* Returns the ID for the last model load. If the load ID doesn't match for a transaction, the
* transaction should be ignored.
*/
public int getLastLoadId() {
return mLastLoadId;
}
} }
+50 -147
View File
@@ -16,21 +16,18 @@
package com.android.launcher3; package com.android.launcher3;
import android.annotation.TargetApi; import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;
import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetManager;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.ContentProvider; import android.content.ContentProvider;
import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.ContentUris; import android.content.ContentUris;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.OperationApplicationException;
import android.database.Cursor; import android.database.Cursor;
import android.database.sqlite.SQLiteQueryBuilder; import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri; import android.net.Uri;
import android.os.Binder; import android.os.Binder;
import android.os.Build;
import android.os.Bundle;
import android.os.Process; import android.os.Process;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
@@ -38,12 +35,11 @@ import android.util.Log;
import com.android.launcher3.LauncherSettings.Favorites; import com.android.launcher3.LauncherSettings.Favorites;
import com.android.launcher3.config.FeatureFlags; import com.android.launcher3.config.FeatureFlags;
import com.android.launcher3.model.ModelDbController; import com.android.launcher3.model.ModelDbController;
import com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction;
import com.android.launcher3.widget.LauncherWidgetHolder; import com.android.launcher3.widget.LauncherWidgetHolder;
import java.io.FileDescriptor; import java.io.FileDescriptor;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.ArrayList; import java.util.function.ToIntFunction;
public class LauncherProvider extends ContentProvider { public class LauncherProvider extends ContentProvider {
private static final String TAG = "LauncherProvider"; private static final String TAG = "LauncherProvider";
@@ -74,10 +70,6 @@ public class LauncherProvider extends ContentProvider {
return true; return true;
} }
public ModelDbController getModelDbController() {
return LauncherAppState.getInstance(getContext()).getModel().getModelDbController();
}
@Override @Override
public String getType(Uri uri) { public String getType(Uri uri) {
SqlArguments args = new SqlArguments(uri, null, null); SqlArguments args = new SqlArguments(uri, null, null);
@@ -91,180 +83,91 @@ public class LauncherProvider extends ContentProvider {
@Override @Override
public Cursor query(Uri uri, String[] projection, String selection, public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) { String[] selectionArgs, String sortOrder) {
SqlArguments args = new SqlArguments(uri, selection, selectionArgs); SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(args.table); qb.setTables(args.table);
Cursor result = getModelDbController().query( Cursor[] result = new Cursor[1];
args.table, projection, args.where, args.args, sortOrder); executeControllerTask(controller -> {
result.setNotificationUri(getContext().getContentResolver(), uri); result[0] = controller.query(args.table, projection, args.where, args.args, sortOrder);
return result; return 0;
} });
return result[0];
private void reloadLauncherIfExternal() {
if (Binder.getCallingPid() != Process.myPid()) {
LauncherAppState app = LauncherAppState.getInstanceNoCreate();
if (app != null) {
app.getModel().forceReload();
}
}
} }
@Override @Override
public Uri insert(Uri uri, ContentValues initialValues) { public Uri insert(Uri uri, ContentValues values) {
// In very limited cases, we support system|signature permission apps to modify the db. int rowId = executeControllerTask(controller -> {
if (Binder.getCallingPid() != Process.myPid()) { // 1. Ensure that externally added items have a valid item id
if (!initializeExternalAdd(initialValues)) { int id = controller.generateNewItemId();
return null; values.put(LauncherSettings.Favorites._ID, id);
}
}
SqlArguments args = new SqlArguments(uri); // 2. In the case of an app widget, and if no app widget id is specified, we
int rowId = getModelDbController().insert(args.table, initialValues); // attempt allocate and bind the widget.
if (rowId < 0) return null; Integer itemType = values.getAsInteger(Favorites.ITEM_TYPE);
if (itemType != null
&& itemType.intValue() == Favorites.ITEM_TYPE_APPWIDGET
&& !values.containsKey(Favorites.APPWIDGET_ID)) {
uri = ContentUris.withAppendedId(uri, rowId); ComponentName cn = ComponentName.unflattenFromString(
reloadLauncherIfExternal(); values.getAsString(Favorites.APPWIDGET_PROVIDER));
return uri; if (cn == null) {
} return 0;
}
private boolean initializeExternalAdd(ContentValues values) {
// 1. Ensure that externally added items have a valid item id
int id = getModelDbController().generateNewItemId();
values.put(LauncherSettings.Favorites._ID, id);
// 2. In the case of an app widget, and if no app widget id is specified, we
// attempt allocate and bind the widget.
Integer itemType = values.getAsInteger(LauncherSettings.Favorites.ITEM_TYPE);
if (itemType != null &&
itemType.intValue() == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET &&
!values.containsKey(LauncherSettings.Favorites.APPWIDGET_ID)) {
final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getContext());
ComponentName cn = ComponentName.unflattenFromString(
values.getAsString(Favorites.APPWIDGET_PROVIDER));
if (cn != null) {
LauncherWidgetHolder widgetHolder = LauncherWidgetHolder.newInstance(getContext()); LauncherWidgetHolder widgetHolder = LauncherWidgetHolder.newInstance(getContext());
try { try {
int appWidgetId = widgetHolder.allocateAppWidgetId(); int appWidgetId = widgetHolder.allocateAppWidgetId();
values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId); values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId);
if (!appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,cn)) { if (!AppWidgetManager.getInstance(getContext())
.bindAppWidgetIdIfAllowed(appWidgetId, cn)) {
widgetHolder.deleteAppWidgetId(appWidgetId); widgetHolder.deleteAppWidgetId(appWidgetId);
return false; return 0;
} }
} catch (RuntimeException e) { } catch (RuntimeException e) {
Log.e(TAG, "Failed to initialize external widget", e); Log.e(TAG, "Failed to initialize external widget", e);
return false; return 0;
} finally { } finally {
// Necessary to destroy the holder to free up possible activity context // Necessary to destroy the holder to free up possible activity context
widgetHolder.destroy(); widgetHolder.destroy();
} }
} else {
return false;
} }
}
return true; SqlArguments args = new SqlArguments(uri);
} return controller.insert(args.table, values);
});
@Override return rowId < 0 ? null : ContentUris.withAppendedId(uri, rowId);
public int bulkInsert(Uri uri, ContentValues[] values) {
SqlArguments args = new SqlArguments(uri);
getModelDbController().bulkInsert(args.table, values);
reloadLauncherIfExternal();
return values.length;
}
@TargetApi(Build.VERSION_CODES.M)
@Override
public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
throws OperationApplicationException {
try (SQLiteTransaction t = getModelDbController().newTransaction()) {
final int numOperations = operations.size();
final ContentProviderResult[] results = new ContentProviderResult[numOperations];
for (int i = 0; i < numOperations; i++) {
ContentProviderOperation op = operations.get(i);
results[i] = op.apply(this, results, i);
}
t.commit();
reloadLauncherIfExternal();
return results;
}
} }
@Override @Override
public int delete(Uri uri, String selection, String[] selectionArgs) { public int delete(Uri uri, String selection, String[] selectionArgs) {
SqlArguments args = new SqlArguments(uri, selection, selectionArgs); SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
int count = getModelDbController().delete(args.table, args.where, args.args); return executeControllerTask(c -> c.delete(args.table, args.where, args.args));
if (count > 0) {
reloadLauncherIfExternal();
}
return count;
} }
@Override @Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
SqlArguments args = new SqlArguments(uri, selection, selectionArgs); SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
int count = getModelDbController().update(args.table, values, args.where, args.args); return executeControllerTask(c -> c.update(args.table, values, args.where, args.args));
reloadLauncherIfExternal();
return count;
} }
@Override private int executeControllerTask(ToIntFunction<ModelDbController> task) {
public Bundle call(String method, final String arg, final Bundle extras) { if (Binder.getCallingPid() == Process.myPid()) {
if (Binder.getCallingUid() != Process.myUid()) { throw new IllegalArgumentException("Same process should call model directly");
return null;
} }
try {
switch (method) { return MODEL_EXECUTOR.submit(() -> {
case LauncherSettings.Settings.METHOD_CLEAR_EMPTY_DB_FLAG: { LauncherModel model = LauncherAppState.getInstance(getContext()).getModel();
getModelDbController().clearEmptyDbFlag(); int count = task.applyAsInt(model.getModelDbController());
return null; if (count > 0) {
} MAIN_EXECUTOR.submit(model::forceReload);
case LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS: { }
Bundle result = new Bundle(); return count;
result.putIntArray(LauncherSettings.Settings.EXTRA_VALUE, }).get();
getModelDbController().deleteEmptyFolders().toArray()); } catch (Exception e) {
return result; throw new IllegalStateException(e);
}
case LauncherSettings.Settings.METHOD_NEW_ITEM_ID: {
Bundle result = new Bundle();
result.putInt(LauncherSettings.Settings.EXTRA_VALUE,
getModelDbController().generateNewItemId());
return result;
}
case LauncherSettings.Settings.METHOD_NEW_SCREEN_ID: {
Bundle result = new Bundle();
result.putInt(LauncherSettings.Settings.EXTRA_VALUE,
getModelDbController().getNewScreenId());
return result;
}
case LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB: {
getModelDbController().createEmptyDB();
return null;
}
case LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES: {
getModelDbController().loadDefaultFavoritesIfNecessary();
return null;
}
case LauncherSettings.Settings.METHOD_REMOVE_GHOST_WIDGETS: {
getModelDbController().removeGhostWidgets();
return null;
}
case LauncherSettings.Settings.METHOD_NEW_TRANSACTION: {
Bundle result = new Bundle();
result.putBinder(LauncherSettings.Settings.EXTRA_VALUE,
getModelDbController().newTransaction());
return result;
}
case LauncherSettings.Settings.METHOD_REFRESH_HOTSEAT_RESTORE_TABLE: {
getModelDbController().refreshHotseatRestoreTable();
return null;
}
} }
return null;
} }
static class SqlArguments { static class SqlArguments {
@@ -16,10 +16,7 @@
package com.android.launcher3; package com.android.launcher3;
import android.content.ContentResolver;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.provider.BaseColumns; import android.provider.BaseColumns;
import com.android.launcher3.model.data.ItemInfo; import com.android.launcher3.model.data.ItemInfo;
@@ -154,24 +151,6 @@ public class LauncherSettings {
*/ */
public static final String TMP_TABLE = "favorites_tmp"; public static final String TMP_TABLE = "favorites_tmp";
/**
* The content:// style URL for "favorites" table
*/
public static final Uri CONTENT_URI = Uri.parse("content://"
+ LauncherProvider.AUTHORITY + "/" + TABLE_NAME);
/**
* The content:// style URL for a given row, identified by its id.
*
* @param id The row id.
*
* @return The unique content URL for the specified row.
*/
public static Uri getContentUri(int id) {
return Uri.parse("content://" + LauncherProvider.AUTHORITY
+ "/" + TABLE_NAME + "/" + id);
}
/** /**
* The container holding the favorite * The container holding the favorite
* <P>Type: INTEGER</P> * <P>Type: INTEGER</P>
@@ -339,42 +318,8 @@ public class LauncherSettings {
* Launcher settings * Launcher settings
*/ */
public static final class Settings { public static final class Settings {
public static final Uri CONTENT_URI = Uri.parse("content://" +
LauncherProvider.AUTHORITY + "/settings");
public static final String METHOD_CLEAR_EMPTY_DB_FLAG = "clear_empty_db_flag";
public static final String METHOD_DELETE_EMPTY_FOLDERS = "delete_empty_folders";
public static final String METHOD_NEW_ITEM_ID = "generate_new_item_id";
public static final String METHOD_NEW_SCREEN_ID = "generate_new_screen_id";
public static final String METHOD_CREATE_EMPTY_DB = "create_empty_db";
public static final String METHOD_LOAD_DEFAULT_FAVORITES = "load_default_favorites";
public static final String METHOD_REMOVE_GHOST_WIDGETS = "remove_ghost_widgets";
public static final String METHOD_NEW_TRANSACTION = "new_db_transaction";
public static final String METHOD_REFRESH_HOTSEAT_RESTORE_TABLE = "restore_hotseat_table";
public static final String EXTRA_VALUE = "value";
public static final String EXTRA_DB_NAME = "db_name";
public static final String LAYOUT_DIGEST_KEY = "launcher3.layout.provider.blob"; public static final String LAYOUT_DIGEST_KEY = "launcher3.layout.provider.blob";
public static final String LAYOUT_DIGEST_LABEL = "launcher-layout"; public static final String LAYOUT_DIGEST_LABEL = "launcher-layout";
public static final String LAYOUT_DIGEST_TAG = "ignore"; public static final String LAYOUT_DIGEST_TAG = "ignore";
public static Bundle call(ContentResolver cr, String method) {
return call(cr, method, null /* arg */);
}
public static Bundle call(ContentResolver cr, String method, String arg) {
return cr.call(CONTENT_URI, method, arg, null);
}
} }
} }
+2 -3
View File
@@ -897,9 +897,8 @@ public class Workspace<T extends View & PageIndicator> extends PagedView<T>
mWorkspaceScreens.remove(emptyScreenId); mWorkspaceScreens.remove(emptyScreenId);
mScreenOrder.removeValue(emptyScreenId); mScreenOrder.removeValue(emptyScreenId);
int newScreenId = LauncherSettings.Settings.call(getContext().getContentResolver(), int newScreenId = LauncherAppState.getInstance(getContext())
LauncherSettings.Settings.METHOD_NEW_SCREEN_ID) .getModel().getModelDbController().getNewScreenId();
.getInt(LauncherSettings.Settings.EXTRA_VALUE);
// Launcher database isn't aware of empty pages that are already bound, so we need to // Launcher database isn't aware of empty pages that are already bound, so we need to
// skip those IDs manually. // skip those IDs manually.
while (mWorkspaceScreens.containsKey(newScreenId)) { while (mWorkspaceScreens.containsKey(newScreenId)) {
@@ -106,6 +106,7 @@ public abstract class BaseLauncherBinder {
synchronized (mBgDataModel) { synchronized (mBgDataModel) {
if (incrementBindId) { if (incrementBindId) {
mBgDataModel.lastBindId++; mBgDataModel.lastBindId++;
mBgDataModel.lastLoadId = mApp.getModel().getLastLoadId();
} }
mMyBindingId = mBgDataModel.lastBindId; mMyBindingId = mBgDataModel.lastBindId;
return new DisjointWorkspaceBinder(workspacePages); return new DisjointWorkspaceBinder(workspacePages);
@@ -126,6 +127,7 @@ public abstract class BaseLauncherBinder {
mBgDataModel.extraItems.forEach(extraItems::add); mBgDataModel.extraItems.forEach(extraItems::add);
if (incrementBindId) { if (incrementBindId) {
mBgDataModel.lastBindId++; mBgDataModel.lastBindId++;
mBgDataModel.lastLoadId = mApp.getModel().getLastLoadId();
} }
mMyBindingId = mBgDataModel.lastBindId; mMyBindingId = mBgDataModel.lastBindId;
workspaceItemCount = mBgDataModel.itemsIdMap.size(); workspaceItemCount = mBgDataModel.itemsIdMap.size();
@@ -125,6 +125,11 @@ public class BgDataModel {
*/ */
public int lastBindId = 0; public int lastBindId = 0;
/**
* Load id for which the callbacks were successfully bound
*/
public int lastLoadId = -1;
/** /**
* Clears all the data * Clears all the data
*/ */
@@ -33,7 +33,6 @@ import static com.android.launcher3.util.PackageManagerHelper.isSystemApp;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.appwidget.AppWidgetProviderInfo; import android.appwidget.AppWidgetProviderInfo;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
@@ -61,7 +60,6 @@ import com.android.launcher3.InvariantDeviceProfile;
import com.android.launcher3.LauncherAppState; import com.android.launcher3.LauncherAppState;
import com.android.launcher3.LauncherModel; import com.android.launcher3.LauncherModel;
import com.android.launcher3.LauncherSettings.Favorites; import com.android.launcher3.LauncherSettings.Favorites;
import com.android.launcher3.LauncherSettings.Settings;
import com.android.launcher3.Utilities; import com.android.launcher3.Utilities;
import com.android.launcher3.config.FeatureFlags; import com.android.launcher3.config.FeatureFlags;
import com.android.launcher3.folder.Folder; import com.android.launcher3.folder.Folder;
@@ -361,16 +359,15 @@ public class LoaderTask implements Runnable {
String selection, String selection,
@Nullable LoaderMemoryLogger memoryLogger) { @Nullable LoaderMemoryLogger memoryLogger) {
final Context context = mApp.getContext(); final Context context = mApp.getContext();
final ContentResolver contentResolver = context.getContentResolver();
final PackageManagerHelper pmHelper = new PackageManagerHelper(context); final PackageManagerHelper pmHelper = new PackageManagerHelper(context);
final boolean isSafeMode = pmHelper.isSafeMode(); final boolean isSafeMode = pmHelper.isSafeMode();
final boolean isSdCardReady = Utilities.isBootCompleted(); final boolean isSdCardReady = Utilities.isBootCompleted();
final WidgetManagerHelper widgetHelper = new WidgetManagerHelper(context); final WidgetManagerHelper widgetHelper = new WidgetManagerHelper(context);
mApp.getModel().getModelDbController().tryMigrateDB(); ModelDbController dbController = mApp.getModel().getModelDbController();
dbController.tryMigrateDB();
Log.d(TAG, "loadWorkspace: loading default favorites"); Log.d(TAG, "loadWorkspace: loading default favorites");
mApp.getModel().getModelDbController().loadDefaultFavoritesIfNecessary(); dbController.loadDefaultFavoritesIfNecessary();
Settings.call(contentResolver, Settings.METHOD_LOAD_DEFAULT_FAVORITES);
synchronized (mBgDataModel) { synchronized (mBgDataModel) {
mBgDataModel.clear(); mBgDataModel.clear();
@@ -384,12 +381,11 @@ public class LoaderTask implements Runnable {
mFirstScreenBroadcast = new FirstScreenBroadcast(installingPkgs); mFirstScreenBroadcast = new FirstScreenBroadcast(installingPkgs);
mShortcutKeyToPinnedShortcuts = new HashMap<>(); mShortcutKeyToPinnedShortcuts = new HashMap<>();
ModelDbController dbController = mApp.getModel().getModelDbController();
final LoaderCursor c = new LoaderCursor( final LoaderCursor c = new LoaderCursor(
dbController.query(TABLE_NAME, null, selection, null, null), dbController.query(TABLE_NAME, null, selection, null, null),
mApp, mUserManagerState); mApp, mUserManagerState);
final Bundle extras = c.getExtras(); final Bundle extras = c.getExtras();
mDbName = extras == null ? null : extras.getString(Settings.EXTRA_DB_NAME); mDbName = extras == null ? null : extras.getString(ModelDbController.EXTRA_DB_NAME);
try { try {
final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>(); final LongSparseArray<Boolean> unlockedUsers = new LongSparseArray<>();
@@ -906,9 +902,7 @@ public class LoaderTask implements Runnable {
private void sanitizeFolders(boolean itemsDeleted) { private void sanitizeFolders(boolean itemsDeleted) {
if (itemsDeleted) { if (itemsDeleted) {
// Remove any empty folder // Remove any empty folder
int[] deletedFolderIds = Settings.call(mApp.getContext().getContentResolver(), IntArray deletedFolderIds = mApp.getModel().getModelDbController().deleteEmptyFolders();
Settings.METHOD_DELETE_EMPTY_FOLDERS)
.getIntArray(Settings.EXTRA_VALUE);
synchronized (mBgDataModel) { synchronized (mBgDataModel) {
for (int folderId : deletedFolderIds) { for (int folderId : deletedFolderIds) {
mBgDataModel.workspaceItems.remove(mBgDataModel.folders.get(folderId)); mBgDataModel.workspaceItems.remove(mBgDataModel.folders.get(folderId));
@@ -921,11 +915,9 @@ public class LoaderTask implements Runnable {
private void sanitizeWidgetsShortcutsAndPackages() { private void sanitizeWidgetsShortcutsAndPackages() {
Context context = mApp.getContext(); Context context = mApp.getContext();
ContentResolver contentResolver = context.getContentResolver();
// Remove any ghost widgets // Remove any ghost widgets
Settings.call(contentResolver, mApp.getModel().getModelDbController().removeGhostWidgets();
Settings.METHOD_REMOVE_GHOST_WIDGETS);
// Update pinned state of model shortcuts // Update pinned state of model shortcuts
mBgDataModel.updateShortcutPinnedState(context); mBgDataModel.updateShortcutPinnedState(context);
@@ -37,7 +37,6 @@ import android.database.Cursor;
import android.database.SQLException; import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.net.Uri; import android.net.Uri;
import android.os.Binder;
import android.os.Bundle; import android.os.Bundle;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import android.os.Process; import android.os.Process;
@@ -85,6 +84,7 @@ public class ModelDbController {
private static final String TAG = "LauncherProvider"; private static final String TAG = "LauncherProvider";
private static final String EMPTY_DATABASE_CREATED = "EMPTY_DATABASE_CREATED"; private static final String EMPTY_DATABASE_CREATED = "EMPTY_DATABASE_CREATED";
public static final String EXTRA_DB_NAME = "db_name";
protected DatabaseHelper mOpenHelper; protected DatabaseHelper mOpenHelper;
@@ -140,7 +140,7 @@ public class ModelDbController {
table, projection, selection, selectionArgs, null, null, sortOrder); table, projection, selection, selectionArgs, null, null, sortOrder);
final Bundle extra = new Bundle(); final Bundle extra = new Bundle();
extra.putString(LauncherSettings.Settings.EXTRA_DB_NAME, mOpenHelper.getDatabaseName()); extra.putString(EXTRA_DB_NAME, mOpenHelper.getDatabaseName());
result.setExtras(extra); result.setExtras(extra);
return result; return result;
} }
@@ -161,28 +161,6 @@ public class ModelDbController {
return rowId; return rowId;
} }
/**
* Similar to insert but for adding multiple values in a transaction.
*/
@WorkerThread
public int bulkInsert(String table, ContentValues[] values) {
createDbIfNotExists();
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
try (SQLiteTransaction t = new SQLiteTransaction(db)) {
int numValues = values.length;
for (int i = 0; i < numValues; i++) {
addModifiedTime(values[i]);
if (mOpenHelper.dbInsertAndCheck(db, table, values[i]) < 0) {
return 0;
}
}
onAddOrDeleteOp(db);
t.commit();
}
return values.length;
}
/** /**
* Refer {@link SQLiteDatabase#delete(String, String, String[])} * Refer {@link SQLiteDatabase#delete(String, String, String[])}
*/ */
@@ -191,10 +169,6 @@ public class ModelDbController {
createDbIfNotExists(); createDbIfNotExists();
SQLiteDatabase db = mOpenHelper.getWritableDatabase(); SQLiteDatabase db = mOpenHelper.getWritableDatabase();
if (Binder.getCallingPid() != Process.myPid()
&& Favorites.TABLE_NAME.equalsIgnoreCase(table)) {
mOpenHelper.removeGhostWidgets(mOpenHelper.getWritableDatabase());
}
int count = db.delete(table, selection, selectionArgs); int count = db.delete(table, selection, selectionArgs);
if (count > 0) { if (count > 0) {
onAddOrDeleteOp(db); onAddOrDeleteOp(db);
@@ -250,6 +224,7 @@ public class ModelDbController {
public void createEmptyDB() { public void createEmptyDB() {
createDbIfNotExists(); createDbIfNotExists();
mOpenHelper.createEmptyDB(mOpenHelper.getWritableDatabase()); mOpenHelper.createEmptyDB(mOpenHelper.getWritableDatabase());
LauncherPrefs.get(mContext).putSync(getEmptyDbCreatedKey().to(true));
} }
/** /**
@@ -304,6 +279,10 @@ public class ModelDbController {
*/ */
private boolean migrateGridIfNeeded() { private boolean migrateGridIfNeeded() {
createDbIfNotExists(); createDbIfNotExists();
if (LauncherPrefs.get(mContext).get(getEmptyDbCreatedKey())) {
// If we have already create a new DB, ignore migration
return false;
}
InvariantDeviceProfile idp = LauncherAppState.getIDP(mContext); InvariantDeviceProfile idp = LauncherAppState.getIDP(mContext);
if (!GridSizeMigrationUtil.needsToMigrate(mContext, idp)) { if (!GridSizeMigrationUtil.needsToMigrate(mContext, idp)) {
return true; return true;
@@ -374,7 +353,7 @@ public class ModelDbController {
} }
private void clearFlagEmptyDbCreated() { private void clearFlagEmptyDbCreated() {
LauncherPrefs.get(mContext).removeSync(getEmptyDbCreatedKey(mOpenHelper.getDatabaseName())); LauncherPrefs.get(mContext).removeSync(getEmptyDbCreatedKey());
} }
/** /**
@@ -388,7 +367,7 @@ public class ModelDbController {
public synchronized void loadDefaultFavoritesIfNecessary() { public synchronized void loadDefaultFavoritesIfNecessary() {
createDbIfNotExists(); createDbIfNotExists();
if (LauncherPrefs.get(mContext).get(getEmptyDbCreatedKey(mOpenHelper.getDatabaseName()))) { if (LauncherPrefs.get(mContext).get(getEmptyDbCreatedKey())) {
Log.d(TAG, "loading default workspace"); Log.d(TAG, "loading default workspace");
LauncherWidgetHolder widgetHolder = mOpenHelper.newLauncherWidgetHolder(); LauncherWidgetHolder widgetHolder = mOpenHelper.newLauncherWidgetHolder();
@@ -507,6 +486,10 @@ public class ModelDbController {
mOpenHelper, mContext.getResources(), defaultLayout); mOpenHelper, mContext.getResources(), defaultLayout);
} }
private ConstantItem<Boolean> getEmptyDbCreatedKey() {
return getEmptyDbCreatedKey(mOpenHelper.getDatabaseName());
}
/** /**
* Re-composite given key in respect to database. If the current db is * Re-composite given key in respect to database. If the current db is
* {@link LauncherFiles#LAUNCHER_DB}, return the key as-is. Otherwise append the db name to * {@link LauncherFiles#LAUNCHER_DB}, return the key as-is. Otherwise append the db name to
@@ -16,13 +16,12 @@
package com.android.launcher3.model; package com.android.launcher3.model;
import static com.android.launcher3.LauncherSettings.Favorites.TABLE_NAME;
import static com.android.launcher3.provider.LauncherDbUtils.itemIdMatch;
import static com.android.launcher3.util.Executors.MODEL_EXECUTOR; import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;
import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.Context; import android.content.Context;
import android.net.Uri;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
@@ -32,9 +31,7 @@ import androidx.annotation.Nullable;
import com.android.launcher3.LauncherAppState; import com.android.launcher3.LauncherAppState;
import com.android.launcher3.LauncherModel; import com.android.launcher3.LauncherModel;
import com.android.launcher3.LauncherModel.CallbackTask; import com.android.launcher3.LauncherModel.CallbackTask;
import com.android.launcher3.LauncherProvider;
import com.android.launcher3.LauncherSettings.Favorites; import com.android.launcher3.LauncherSettings.Favorites;
import com.android.launcher3.LauncherSettings.Settings;
import com.android.launcher3.Utilities; import com.android.launcher3.Utilities;
import com.android.launcher3.celllayout.CellPosMapper; import com.android.launcher3.celllayout.CellPosMapper;
import com.android.launcher3.celllayout.CellPosMapper.CellPos; import com.android.launcher3.celllayout.CellPosMapper.CellPos;
@@ -45,6 +42,7 @@ import com.android.launcher3.model.data.FolderInfo;
import com.android.launcher3.model.data.ItemInfo; import com.android.launcher3.model.data.ItemInfo;
import com.android.launcher3.model.data.LauncherAppWidgetInfo; import com.android.launcher3.model.data.LauncherAppWidgetInfo;
import com.android.launcher3.model.data.WorkspaceItemInfo; import com.android.launcher3.model.data.WorkspaceItemInfo;
import com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction;
import com.android.launcher3.util.ContentWriter; import com.android.launcher3.util.ContentWriter;
import com.android.launcher3.util.Executors; import com.android.launcher3.util.Executors;
import com.android.launcher3.util.ItemInfoMatcher; import com.android.launcher3.util.ItemInfoMatcher;
@@ -80,7 +78,7 @@ public class ModelWriter {
private final boolean mVerifyChanges; private final boolean mVerifyChanges;
// Keep track of delete operations that occur when an Undo option is present; we may not commit. // Keep track of delete operations that occur when an Undo option is present; we may not commit.
private final List<Runnable> mDeleteRunnables = new ArrayList<>(); private final List<ModelTask> mDeleteRunnables = new ArrayList<>();
private boolean mPreparingToUndo; private boolean mPreparingToUndo;
private final CellPosMapper mCellPosMapper; private final CellPosMapper mCellPosMapper;
@@ -217,8 +215,7 @@ public class ModelWriter {
item.spanX = spanX; item.spanX = spanX;
item.spanY = spanY; item.spanY = spanY;
notifyItemModified(item); notifyItemModified(item);
new UpdateItemRunnable(item, () ->
MODEL_EXECUTOR.execute(new UpdateItemRunnable(item, () ->
new ContentWriter(mContext) new ContentWriter(mContext)
.put(Favorites.CONTAINER, item.container) .put(Favorites.CONTAINER, item.container)
.put(Favorites.CELLX, item.cellX) .put(Favorites.CELLX, item.cellX)
@@ -226,7 +223,8 @@ public class ModelWriter {
.put(Favorites.RANK, item.rank) .put(Favorites.RANK, item.rank)
.put(Favorites.SPANX, item.spanX) .put(Favorites.SPANX, item.spanX)
.put(Favorites.SPANY, item.spanY) .put(Favorites.SPANY, item.spanY)
.put(Favorites.SCREEN, item.screenId))); .put(Favorites.SCREEN, item.screenId))
.executeOnModelThread();
} }
/** /**
@@ -234,11 +232,11 @@ public class ModelWriter {
*/ */
public void updateItemInDatabase(ItemInfo item) { public void updateItemInDatabase(ItemInfo item) {
notifyItemModified(item); notifyItemModified(item);
MODEL_EXECUTOR.execute(new UpdateItemRunnable(item, () -> { new UpdateItemRunnable(item, () -> {
ContentWriter writer = new ContentWriter(mContext); ContentWriter writer = new ContentWriter(mContext);
item.onAddToDatabase(writer); item.onAddToDatabase(writer);
return writer; return writer;
})); }).executeOnModelThread();
} }
private void notifyItemModified(ItemInfo item) { private void notifyItemModified(ItemInfo item) {
@@ -253,13 +251,12 @@ public class ModelWriter {
int container, int screenId, int cellX, int cellY) { int container, int screenId, int cellX, int cellY) {
updateItemInfoProps(item, container, screenId, cellX, cellY); updateItemInfoProps(item, container, screenId, cellX, cellY);
final ContentResolver cr = mContext.getContentResolver(); item.id = mModel.getModelDbController().generateNewItemId();
item.id = Settings.call(cr, Settings.METHOD_NEW_ITEM_ID).getInt(Settings.EXTRA_VALUE);
notifyOtherCallbacks(c -> c.bindItems(Collections.singletonList(item), false)); notifyOtherCallbacks(c -> c.bindItems(Collections.singletonList(item), false));
ModelVerifier verifier = new ModelVerifier(); ModelVerifier verifier = new ModelVerifier();
final StackTraceElement[] stackTrace = new Throwable().getStackTrace(); final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
MODEL_EXECUTOR.execute(() -> { newModelTask(() -> {
// Write the item on background thread, as some properties might have been updated in // Write the item on background thread, as some properties might have been updated in
// the background. // the background.
final ContentWriter writer = new ContentWriter(mContext); final ContentWriter writer = new ContentWriter(mContext);
@@ -272,7 +269,7 @@ public class ModelWriter {
mBgDataModel.addItem(mContext, item, true); mBgDataModel.addItem(mContext, item, true);
verifier.verifyModel(); verifier.verifyModel();
} }
}); }).executeOnModelThread();
} }
/** /**
@@ -303,15 +300,13 @@ public class ModelWriter {
Collectors.joining(",")) Collectors.joining(","))
+ ". Reason: [" + (TextUtils.isEmpty(reason) ? "unknown" : reason) + "]"); + ". Reason: [" + (TextUtils.isEmpty(reason) ? "unknown" : reason) + "]");
notifyDelete(items); notifyDelete(items);
enqueueDeleteRunnable(() -> { enqueueDeleteRunnable(newModelTask(() -> {
for (ItemInfo item : items) { for (ItemInfo item : items) {
final Uri uri = Favorites.getContentUri(item.id); mModel.getModelDbController().delete(TABLE_NAME, itemIdMatch(item.id), null);
mContext.getContentResolver().delete(uri, null, null);
mBgDataModel.removeItem(mContext, item); mBgDataModel.removeItem(mContext, item);
verifier.verifyModel(); verifier.verifyModel();
} }
}); }));
} }
/** /**
@@ -321,7 +316,7 @@ public class ModelWriter {
ModelVerifier verifier = new ModelVerifier(); ModelVerifier verifier = new ModelVerifier();
notifyDelete(Collections.singleton(info)); notifyDelete(Collections.singleton(info));
enqueueDeleteRunnable(() -> { enqueueDeleteRunnable(newModelTask(() -> {
mModel.getModelDbController().delete(Favorites.TABLE_NAME, mModel.getModelDbController().delete(Favorites.TABLE_NAME,
Favorites.CONTAINER + "=" + info.id, null); Favorites.CONTAINER + "=" + info.id, null);
mBgDataModel.removeItem(mContext, info.contents); mBgDataModel.removeItem(mContext, info.contents);
@@ -331,7 +326,7 @@ public class ModelWriter {
Favorites._ID + "=" + info.id, null); Favorites._ID + "=" + info.id, null);
mBgDataModel.removeItem(mContext, info); mBgDataModel.removeItem(mContext, info);
verifier.verifyModel(); verifier.verifyModel();
}); }));
} }
/** /**
@@ -343,7 +338,7 @@ public class ModelWriter {
if (holder != null && !info.isCustomWidget() && info.isWidgetIdAllocated()) { if (holder != null && !info.isCustomWidget() && info.isWidgetIdAllocated()) {
// Deleting an app widget ID is a void call but writes to disk before returning // Deleting an app widget ID is a void call but writes to disk before returning
// to the caller... // to the caller...
enqueueDeleteRunnable(() -> holder.deleteAppWidgetId(info.appWidgetId)); enqueueDeleteRunnable(newModelTask(() -> holder.deleteAppWidgetId(info.appWidgetId)));
} }
deleteItemFromDatabase(info, reason); deleteItemFromDatabase(info, reason);
} }
@@ -373,19 +368,17 @@ public class ModelWriter {
* {@link #commitDelete()} is called (or abandoned if {@link #abortDelete} is called). * {@link #commitDelete()} is called (or abandoned if {@link #abortDelete} is called).
* Otherwise, we run the Runnable immediately. * Otherwise, we run the Runnable immediately.
*/ */
private void enqueueDeleteRunnable(Runnable r) { private void enqueueDeleteRunnable(ModelTask r) {
if (mPreparingToUndo) { if (mPreparingToUndo) {
mDeleteRunnables.add(r); mDeleteRunnables.add(r);
} else { } else {
MODEL_EXECUTOR.execute(r); r.executeOnModelThread();
} }
} }
public void commitDelete() { public void commitDelete() {
mPreparingToUndo = false; mPreparingToUndo = false;
for (Runnable runnable : mDeleteRunnables) { mDeleteRunnables.forEach(ModelTask::executeOnModelThread);
MODEL_EXECUTOR.execute(runnable);
}
mDeleteRunnables.clear(); mDeleteRunnables.clear();
} }
@@ -426,10 +419,9 @@ public class ModelWriter {
} }
@Override @Override
public void run() { public void runImpl() {
Uri uri = Favorites.getContentUri(mItemId); mModel.getModelDbController().update(
mContext.getContentResolver().update(uri, mWriter.get().getValues(mContext), TABLE_NAME, mWriter.get().getValues(mContext), itemIdMatch(mItemId), null);
null, null);
updateItemArrays(mItem, mItemId); updateItemArrays(mItem, mItemId);
} }
} }
@@ -444,27 +436,24 @@ public class ModelWriter {
} }
@Override @Override
public void run() { public void runImpl() {
ArrayList<ContentProviderOperation> ops = new ArrayList<>(); try (SQLiteTransaction t = mModel.getModelDbController().newTransaction()) {
int count = mItems.size(); int count = mItems.size();
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
ItemInfo item = mItems.get(i); ItemInfo item = mItems.get(i);
final int itemId = item.id; final int itemId = item.id;
final Uri uri = Favorites.getContentUri(itemId); mModel.getModelDbController().update(
ContentValues values = mValues.get(i); TABLE_NAME, mValues.get(i), itemIdMatch(itemId), null);
updateItemArrays(item, itemId);
ops.add(ContentProviderOperation.newUpdate(uri).withValues(values).build()); }
updateItemArrays(item, itemId); t.commit();
}
try {
mContext.getContentResolver().applyBatch(LauncherProvider.AUTHORITY, ops);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
private abstract class UpdateItemBaseRunnable implements Runnable { private abstract class UpdateItemBaseRunnable extends ModelTask {
private final StackTraceElement[] mStackTrace; private final StackTraceElement[] mStackTrace;
private final ModelVerifier mVerifier = new ModelVerifier(); private final ModelVerifier mVerifier = new ModelVerifier();
@@ -515,6 +504,35 @@ public class ModelWriter {
} }
} }
private abstract class ModelTask implements Runnable {
private final int mLoadId = mBgDataModel.lastLoadId;
@Override
public final void run() {
if (mLoadId != mModel.getLastLoadId()) {
Log.d(TAG, "Model changed before the task could execute");
return;
}
runImpl();
}
public final void executeOnModelThread() {
MODEL_EXECUTOR.execute(this);
}
public abstract void runImpl();
}
private ModelTask newModelTask(Runnable r) {
return new ModelTask() {
@Override
public void runImpl() {
r.run();
}
};
}
/** /**
* Utility class to verify model updates are propagated properly to the callback. * Utility class to verify model updates are propagated properly to the callback.
*/ */
@@ -82,9 +82,7 @@ public class WorkspaceItemSpaceFinder {
if (!found) { if (!found) {
// Still no position found. Add a new screen to the end. // Still no position found. Add a new screen to the end.
screenId = LauncherSettings.Settings.call(app.getContext().getContentResolver(), screenId = app.getModel().getModelDbController().getNewScreenId();
LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
.getInt(LauncherSettings.Settings.EXTRA_VALUE);
// Save the screen id for binding in the workspace // Save the screen id for binding in the workspace
workspaceScreens.add(screenId); workspaceScreens.add(screenId);
@@ -28,7 +28,6 @@ import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.graphics.drawable.Icon; import android.graphics.drawable.Icon;
import android.os.Binder;
import android.os.PersistableBundle; import android.os.PersistableBundle;
import android.os.Process; import android.os.Process;
import android.os.UserManager; import android.os.UserManager;
@@ -50,6 +49,13 @@ import com.android.launcher3.util.IntSet;
*/ */
public class LauncherDbUtils { public class LauncherDbUtils {
/**
* Returns a string which can be used as a where clause for DB query to match the given itemId
*/
public static String itemIdMatch(int itemId) {
return "_id=" + itemId;
}
public static IntArray queryIntArray(boolean distinct, SQLiteDatabase db, String tableName, public static IntArray queryIntArray(boolean distinct, SQLiteDatabase db, String tableName,
String columnName, String selection, String groupBy, String orderBy) { String columnName, String selection, String groupBy, String orderBy) {
IntArray out = new IntArray(); IntArray out = new IntArray();
@@ -166,7 +172,7 @@ public class LauncherDbUtils {
/** /**
* Utility class to simplify managing sqlite transactions * Utility class to simplify managing sqlite transactions
*/ */
public static class SQLiteTransaction extends Binder implements AutoCloseable { public static class SQLiteTransaction implements AutoCloseable {
private final SQLiteDatabase mDb; private final SQLiteDatabase mDb;
public SQLiteTransaction(SQLiteDatabase db) { public SQLiteTransaction(SQLiteDatabase db) {
@@ -15,24 +15,28 @@
*/ */
package com.android.launcher3.celllayout; package com.android.launcher3.celllayout;
import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
import static com.android.launcher3.LauncherSettings.Favorites.TABLE_NAME; import static com.android.launcher3.LauncherSettings.Favorites.TABLE_NAME;
import static com.android.launcher3.util.Executors.MAIN_EXECUTOR; import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
import static com.android.launcher3.util.Executors.MODEL_EXECUTOR; import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;
import static com.android.launcher3.util.TestUtil.runOnExecutorSync; import static com.android.launcher3.util.TestUtil.runOnExecutorSync;
import android.content.ContentValues;
import android.content.Context; import android.content.Context;
import androidx.test.uiautomator.UiDevice;
import com.android.launcher3.LauncherAppState; import com.android.launcher3.LauncherAppState;
import com.android.launcher3.LauncherModel; import com.android.launcher3.LauncherModel;
import com.android.launcher3.LauncherSettings; import com.android.launcher3.LauncherSettings;
import com.android.launcher3.model.BgDataModel.Callbacks; import com.android.launcher3.model.BgDataModel.Callbacks;
import com.android.launcher3.model.ModelDbController; import com.android.launcher3.model.ModelDbController;
import com.android.launcher3.model.data.ItemInfo; import com.android.launcher3.model.data.ItemInfo;
import com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction;
import com.android.launcher3.tapl.LauncherInstrumentation;
import com.android.launcher3.util.ContentWriter; import com.android.launcher3.util.ContentWriter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier; import java.util.function.Supplier;
public class FavoriteItemsTransaction { public class FavoriteItemsTransaction {
@@ -67,15 +71,16 @@ public class FavoriteItemsTransaction {
controller.clearEmptyDbFlag(); controller.clearEmptyDbFlag();
// Add new data // Add new data
List<ContentValues> values = new ArrayList<>(); try (SQLiteTransaction transaction = controller.newTransaction()) {
int count = mItemsToSubmit.size(); int count = mItemsToSubmit.size();
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
ContentWriter writer = new ContentWriter(mContext); ContentWriter writer = new ContentWriter(mContext);
mItemsToSubmit.get(i).get().onAddToDatabase(writer); mItemsToSubmit.get(i).get().onAddToDatabase(writer);
writer.put(LauncherSettings.Favorites._ID, i); writer.put(LauncherSettings.Favorites._ID, i);
values.add(writer.getValues(mContext)); controller.insert(TABLE_NAME, writer.getValues(mContext));
}
transaction.commit();
} }
controller.bulkInsert(TABLE_NAME, values.toArray(new ContentValues[0]));
}); });
// Reload model // Reload model
@@ -91,4 +96,15 @@ public class FavoriteItemsTransaction {
runOnExecutorSync(MAIN_EXECUTOR, () -> { }); runOnExecutorSync(MAIN_EXECUTOR, () -> { });
runOnExecutorSync(MAIN_EXECUTOR, () -> model.removeCallbacks(mockCb)); runOnExecutorSync(MAIN_EXECUTOR, () -> model.removeCallbacks(mockCb));
} }
/**
* Commits the transaction and waits for home load
*/
public void commitAndLoadHome(LauncherInstrumentation inst) {
commit();
// Launch the home activity
UiDevice.getInstance(getInstrumentation()).pressHome();
inst.waitForLauncherInitialized();
}
} }
@@ -61,7 +61,6 @@ public class ReorderWidgets extends AbstractLauncherUiTest {
public void setup() throws Throwable { public void setup() throws Throwable {
mWorkspaceBuilder = new TestWorkspaceBuilder(mTargetContext); mWorkspaceBuilder = new TestWorkspaceBuilder(mTargetContext);
TaplTestsLauncher3.initialize(this); TaplTestsLauncher3.initialize(this);
clearHomescreen();
} }
/** /**
@@ -111,10 +110,10 @@ public class ReorderWidgets extends AbstractLauncherUiTest {
new FavoriteItemsTransaction(mTargetContext); new FavoriteItemsTransaction(mTargetContext);
transaction = buildWorkspaceFromBoards(testCase.mStart, transaction); transaction = buildWorkspaceFromBoards(testCase.mStart, transaction);
transaction.commit(); transaction.commit();
mLauncher.waitForLauncherInitialized();
// resetLoaderState triggers the launcher to start loading the workspace which allows // resetLoaderState triggers the launcher to start loading the workspace which allows
// waitForLauncherCondition to wait for that condition, otherwise the condition would // waitForLauncherCondition to wait for that condition, otherwise the condition would
// always be true and it wouldn't wait for the changes to be applied. // always be true and it wouldn't wait for the changes to be applied.
resetLoaderState();
waitForLauncherCondition("Workspace didn't finish loading", l -> !l.isWorkspaceLoading()); waitForLauncherCondition("Workspace didn't finish loading", l -> !l.isWorkspaceLoading());
Widget widget = mLauncher.getWorkspace().getWidgetAtCell(mainWidgetCellPos.getCellX(), Widget widget = mLauncher.getWorkspace().getWidgetAtCell(mainWidgetCellPos.getCellX(),
mainWidgetCellPos.getCellY()); mainWidgetCellPos.getCellY());
@@ -78,12 +78,6 @@ public class TestWorkspaceBuilder {
return transaction; return transaction;
} }
private int getID() {
return LauncherSettings.Settings.call(
mResolver, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
.getInt(LauncherSettings.Settings.EXTRA_VALUE);
}
private AppInfo getApp() { private AppInfo getApp() {
return new AppInfo(APP_COMPONENT_NAME, "test icon", mMyUser, return new AppInfo(APP_COMPONENT_NAME, "test icon", mMyUser,
AppInfo.makeLaunchIntent(APP_COMPONENT_NAME)); AppInfo.makeLaunchIntent(APP_COMPONENT_NAME));
@@ -127,7 +121,6 @@ public class TestWorkspaceBuilder {
return () -> { return () -> {
LauncherAppWidgetProviderInfo info = findWidgetProvider(false); LauncherAppWidgetProviderInfo info = findWidgetProvider(false);
LauncherAppWidgetInfo item = createWidgetInfo(info, getApplicationContext(), true); LauncherAppWidgetInfo item = createWidgetInfo(info, getApplicationContext(), true);
item.id = getID();
item.cellX = widgetRect.getCellX(); item.cellX = widgetRect.getCellX();
item.cellY = widgetRect.getCellY(); item.cellY = widgetRect.getCellY();
item.spanX = widgetRect.getSpanX(); item.spanX = widgetRect.getSpanX();
@@ -139,7 +132,6 @@ public class TestWorkspaceBuilder {
private ItemInfo createIconInCell(CellLayoutBoard.IconPoint iconPoint, int screenId) { private ItemInfo createIconInCell(CellLayoutBoard.IconPoint iconPoint, int screenId) {
WorkspaceItemInfo item = new WorkspaceItemInfo(getApp()); WorkspaceItemInfo item = new WorkspaceItemInfo(getApp());
item.id = getID();
item.screenId = screenId; item.screenId = screenId;
item.cellX = iconPoint.getCoord().x; item.cellX = iconPoint.getCoord().x;
item.cellY = iconPoint.getCoord().y; item.cellY = iconPoint.getCoord().y;
@@ -150,7 +142,6 @@ public class TestWorkspaceBuilder {
private ItemInfo getHotseatValues(int x) { private ItemInfo getHotseatValues(int x) {
WorkspaceItemInfo item = new WorkspaceItemInfo(getApp()); WorkspaceItemInfo item = new WorkspaceItemInfo(getApp());
item.id = getID();
item.cellX = x; item.cellX = x;
item.cellY = 0; item.cellY = 0;
item.minSpanY = item.minSpanX = item.spanX = item.spanY = 1; item.minSpanY = item.minSpanX = item.spanX = item.spanY = 1;
@@ -19,7 +19,6 @@ import static androidx.test.InstrumentationRegistry.getInstrumentation;
import static com.android.launcher3.ui.TaplTestsLauncher3.getAppPackageName; import static com.android.launcher3.ui.TaplTestsLauncher3.getAppPackageName;
import static com.android.launcher3.util.Executors.MAIN_EXECUTOR; import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
import static com.android.launcher3.util.TestUtil.getOnUiThread;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@@ -40,6 +39,7 @@ import android.os.Process;
import android.os.RemoteException; import android.os.RemoteException;
import android.os.UserHandle; import android.os.UserHandle;
import android.os.UserManager; import android.os.UserManager;
import android.system.OsConstants;
import android.util.Log; import android.util.Log;
import androidx.test.InstrumentationRegistry; import androidx.test.InstrumentationRegistry;
@@ -49,11 +49,8 @@ import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.Until; import androidx.test.uiautomator.Until;
import com.android.launcher3.Launcher; import com.android.launcher3.Launcher;
import com.android.launcher3.LauncherAppState;
import com.android.launcher3.LauncherSettings;
import com.android.launcher3.LauncherState; import com.android.launcher3.LauncherState;
import com.android.launcher3.Utilities; import com.android.launcher3.Utilities;
import com.android.launcher3.model.data.ItemInfo;
import com.android.launcher3.statemanager.StateManager; import com.android.launcher3.statemanager.StateManager;
import com.android.launcher3.tapl.HomeAllApps; import com.android.launcher3.tapl.HomeAllApps;
import com.android.launcher3.tapl.HomeAppIcon; import com.android.launcher3.tapl.HomeAppIcon;
@@ -66,7 +63,6 @@ import com.android.launcher3.util.LooperExecutor;
import com.android.launcher3.util.SimpleBroadcastReceiver; import com.android.launcher3.util.SimpleBroadcastReceiver;
import com.android.launcher3.util.TestUtil; import com.android.launcher3.util.TestUtil;
import com.android.launcher3.util.Wait; import com.android.launcher3.util.Wait;
import com.android.launcher3.util.WidgetUtils;
import com.android.launcher3.util.rule.FailureWatcher; import com.android.launcher3.util.rule.FailureWatcher;
import com.android.launcher3.util.rule.LauncherActivityRule; import com.android.launcher3.util.rule.LauncherActivityRule;
import com.android.launcher3.util.rule.SamplerRule; import com.android.launcher3.util.rule.SamplerRule;
@@ -83,8 +79,10 @@ import org.junit.rules.RuleChain;
import org.junit.rules.TestRule; import org.junit.rules.TestRule;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
@@ -298,37 +296,19 @@ public abstract class AbstractLauncherUiTest {
} }
/** /**
* Removes all icons from homescreen and hotseat. * Runs the callback on the UI thread and returns the result.
*/ */
public void clearHomescreen() { protected <T> T getOnUiThread(final Callable<T> callback) {
LauncherSettings.Settings.call(mTargetContext.getContentResolver(),
LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB);
LauncherSettings.Settings.call(mTargetContext.getContentResolver(),
LauncherSettings.Settings.METHOD_CLEAR_EMPTY_DB_FLAG);
resetLoaderState();
}
protected void resetLoaderState() {
try { try {
mMainThreadExecutor.execute( return mMainThreadExecutor.submit(callback).get(DEFAULT_UI_TIMEOUT,
() -> LauncherAppState.getInstance( TimeUnit.MILLISECONDS);
mTargetContext).getModel().forceReload()); } catch (TimeoutException e) {
} catch (Throwable t) { Log.e(TAG, "Timeout in getOnUiThread, sending SIGABRT", e);
throw new IllegalArgumentException(t); Process.sendSignal(Process.myPid(), OsConstants.SIGABRT);
throw new RuntimeException(e);
} catch (Throwable e) {
throw new RuntimeException(e);
} }
mLauncher.waitForLauncherInitialized();
}
/**
* Adds {@param item} on the homescreen on the 0th screen
*/
public void addItemToScreen(ItemInfo item) {
WidgetUtils.addItemToScreen(item, mTargetContext);
resetLoaderState();
// Launch the home activity
mDevice.pressHome();
mLauncher.waitForLauncherInitialized();
} }
protected <T> T getFromLauncher(Function<Launcher, T> f) { protected <T> T getFromLauncher(Function<Launcher, T> f) {
@@ -30,6 +30,7 @@ import android.view.View;
import androidx.test.filters.LargeTest; import androidx.test.filters.LargeTest;
import androidx.test.runner.AndroidJUnit4; import androidx.test.runner.AndroidJUnit4;
import com.android.launcher3.celllayout.FavoriteItemsTransaction;
import com.android.launcher3.model.data.ItemInfo; import com.android.launcher3.model.data.ItemInfo;
import com.android.launcher3.model.data.LauncherAppWidgetInfo; import com.android.launcher3.model.data.LauncherAppWidgetInfo;
import com.android.launcher3.testcomponent.WidgetConfigActivity; import com.android.launcher3.testcomponent.WidgetConfigActivity;
@@ -85,8 +86,7 @@ public class AddConfigWidgetTest extends AbstractLauncherUiTest {
* @param acceptConfig accept the config activity * @param acceptConfig accept the config activity
*/ */
private void runTest(boolean acceptConfig) throws Throwable { private void runTest(boolean acceptConfig) throws Throwable {
clearHomescreen(); new FavoriteItemsTransaction(mTargetContext).commitAndLoadHome(mLauncher);
mDevice.pressHome();
// Drag widget to homescreen // Drag widget to homescreen
WidgetConfigStartupMonitor monitor = new WidgetConfigStartupMonitor(); WidgetConfigStartupMonitor monitor = new WidgetConfigStartupMonitor();
@@ -23,9 +23,10 @@ import static org.junit.Assert.assertTrue;
import android.platform.test.annotations.IwTest; import android.platform.test.annotations.IwTest;
import android.platform.test.annotations.PlatinumTest; import android.platform.test.annotations.PlatinumTest;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest; import androidx.test.filters.LargeTest;
import androidx.test.runner.AndroidJUnit4;
import com.android.launcher3.celllayout.FavoriteItemsTransaction;
import com.android.launcher3.model.data.LauncherAppWidgetInfo; import com.android.launcher3.model.data.LauncherAppWidgetInfo;
import com.android.launcher3.tapl.Widget; import com.android.launcher3.tapl.Widget;
import com.android.launcher3.tapl.WidgetResizeFrame; import com.android.launcher3.tapl.WidgetResizeFrame;
@@ -55,8 +56,7 @@ public class AddWidgetTest extends AbstractLauncherUiTest {
@Test @Test
@PortraitLandscape @PortraitLandscape
public void testDragIcon() throws Throwable { public void testDragIcon() throws Throwable {
clearHomescreen(); new FavoriteItemsTransaction(mTargetContext).commitAndLoadHome(mLauncher);
mDevice.pressHome();
waitForLauncherCondition("Workspace didn't finish loading", l -> !l.isWorkspaceLoading()); waitForLauncherCondition("Workspace didn't finish loading", l -> !l.isWorkspaceLoading());
@@ -93,8 +93,8 @@ public class AddWidgetTest extends AbstractLauncherUiTest {
@Test @Test
@PortraitLandscape @PortraitLandscape
public void testDragCustomShortcut() throws Throwable { public void testDragCustomShortcut() throws Throwable {
clearHomescreen(); new FavoriteItemsTransaction(mTargetContext).commitAndLoadHome(mLauncher);
mDevice.pressHome();
mLauncher.getWorkspace().openAllWidgets() mLauncher.getWorkspace().openAllWidgets()
.getWidget("com.android.launcher3.testcomponent.CustomShortcutConfigActivity") .getWidget("com.android.launcher3.testcomponent.CustomShortcutConfigActivity")
.dragToWorkspace(false, true); .dragToWorkspace(false, true);
@@ -15,9 +15,13 @@
*/ */
package com.android.launcher3.ui.widget; package com.android.launcher3.ui.widget;
import static androidx.test.InstrumentationRegistry.getTargetContext; import static com.android.launcher3.LauncherSettings.Favorites.TABLE_NAME;
import static com.android.launcher3.WorkspaceLayoutManager.FIRST_SCREEN_ID;
import static com.android.launcher3.util.TestUtil.getOnUiThread; import static com.android.launcher3.model.data.LauncherAppWidgetInfo.FLAG_ID_NOT_VALID;
import static com.android.launcher3.model.data.LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY;
import static com.android.launcher3.model.data.LauncherAppWidgetInfo.FLAG_RESTORE_STARTED;
import static com.android.launcher3.provider.LauncherDbUtils.itemIdMatch;
import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;
import static com.android.launcher3.util.WidgetUtils.createWidgetInfo; import static com.android.launcher3.util.WidgetUtils.createWidgetInfo;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@@ -27,7 +31,6 @@ import static org.junit.Assert.assertTrue;
import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetManager;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.pm.PackageInstaller; import android.content.pm.PackageInstaller;
import android.content.pm.PackageInstaller.SessionParams; import android.content.pm.PackageInstaller.SessionParams;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
@@ -35,11 +38,14 @@ import android.database.Cursor;
import android.os.Bundle; import android.os.Bundle;
import android.widget.RemoteViews; import android.widget.RemoteViews;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest; import androidx.test.filters.LargeTest;
import androidx.test.runner.AndroidJUnit4;
import com.android.launcher3.LauncherAppState;
import com.android.launcher3.LauncherModel;
import com.android.launcher3.LauncherSettings; import com.android.launcher3.LauncherSettings;
import com.android.launcher3.R; import com.android.launcher3.R;
import com.android.launcher3.celllayout.FavoriteItemsTransaction;
import com.android.launcher3.model.data.LauncherAppWidgetInfo; import com.android.launcher3.model.data.LauncherAppWidgetInfo;
import com.android.launcher3.pm.InstallSessionHelper; import com.android.launcher3.pm.InstallSessionHelper;
import com.android.launcher3.tapl.Widget; import com.android.launcher3.tapl.Widget;
@@ -58,6 +64,7 @@ import org.junit.runner.RunWith;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.function.Consumer;
/** /**
* Tests for bind widget flow. * Tests for bind widget flow.
@@ -71,24 +78,18 @@ public class BindWidgetTest extends AbstractLauncherUiTest {
@Rule @Rule
public ShellCommandRule mGrantWidgetRule = ShellCommandRule.grantWidgetBind(); public ShellCommandRule mGrantWidgetRule = ShellCommandRule.grantWidgetBind();
private ContentResolver mResolver;
// Objects created during test, which should be cleaned up in the end. // Objects created during test, which should be cleaned up in the end.
private Cursor mCursor; private Cursor mCursor;
// App install session id. // App install session id.
private int mSessionId = -1; private int mSessionId = -1;
private LauncherModel mModel;
@Override @Override
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
mModel = LauncherAppState.getInstance(mTargetContext).getModel();
mResolver = mTargetContext.getContentResolver();
// Clear all existing data
LauncherSettings.Settings.call(mResolver, LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB);
LauncherSettings.Settings.call(mResolver,
LauncherSettings.Settings.METHOD_CLEAR_EMPTY_DB_FLAG);
} }
@After @After
@@ -104,34 +105,24 @@ public class BindWidgetTest extends AbstractLauncherUiTest {
@Test @Test
public void testBindNormalWidget_withConfig() { public void testBindNormalWidget_withConfig() {
LauncherAppWidgetProviderInfo info = TestViewHelpers.findWidgetProvider(true); LauncherAppWidgetProviderInfo info = addWidgetToScreen(true, true, i -> { });
LauncherAppWidgetInfo item = createWidgetInfo(info, getTargetContext(), true);
addItemToScreen(item);
verifyWidgetPresent(info); verifyWidgetPresent(info);
} }
@Test @Test
public void testBindNormalWidget_withoutConfig() { public void testBindNormalWidget_withoutConfig() {
LauncherAppWidgetProviderInfo info = TestViewHelpers.findWidgetProvider(false); LauncherAppWidgetProviderInfo info = addWidgetToScreen(false, true, i -> { });
LauncherAppWidgetInfo item = createWidgetInfo(info, getTargetContext(), true);
addItemToScreen(item);
verifyWidgetPresent(info); verifyWidgetPresent(info);
} }
@Test @Test
public void testUnboundWidget_removed() { public void testUnboundWidget_removed() {
LauncherAppWidgetProviderInfo info = TestViewHelpers.findWidgetProvider(false); LauncherAppWidgetProviderInfo info = addWidgetToScreen(false, false,
LauncherAppWidgetInfo item = createWidgetInfo(info, getTargetContext(), false); item -> item.appWidgetId = -33);
item.appWidgetId = -33;
addItemToScreen(item);
final Workspace workspace = mLauncher.getWorkspace(); final Workspace workspace = mLauncher.getWorkspace();
// Item deleted from db // Item deleted from db
mCursor = mResolver.query(LauncherSettings.Favorites.getContentUri(item.id), mCursor = queryItem();
null, null, null, null, null);
assertEquals(0, mCursor.getCount()); assertEquals(0, mCursor.getCount());
// The view does not exist // The view does not exist
@@ -141,36 +132,26 @@ public class BindWidgetTest extends AbstractLauncherUiTest {
@Test @Test
public void testPendingWidget_autoRestored() { public void testPendingWidget_autoRestored() {
// A non-restored widget with no config screen gets restored automatically. // A non-restored widget with no config screen gets restored automatically.
LauncherAppWidgetProviderInfo info = TestViewHelpers.findWidgetProvider(false);
// Do not bind the widget // Do not bind the widget
LauncherAppWidgetInfo item = createWidgetInfo(info, getTargetContext(), false); LauncherAppWidgetProviderInfo info = addWidgetToScreen(false, false,
item.restoreStatus = LauncherAppWidgetInfo.FLAG_ID_NOT_VALID; item -> item.restoreStatus = FLAG_ID_NOT_VALID);
addItemToScreen(item);
verifyWidgetPresent(info); verifyWidgetPresent(info);
} }
@Test @Test
public void testPendingWidget_withConfigScreen() { public void testPendingWidget_withConfigScreen() {
// A non-restored widget with config screen get bound and shows a 'Click to setup' UI. // A non-restored widget with config screen get bound and shows a 'Click to setup' UI.
LauncherAppWidgetProviderInfo info = TestViewHelpers.findWidgetProvider(true);
// Do not bind the widget // Do not bind the widget
LauncherAppWidgetInfo item = createWidgetInfo(info, getTargetContext(), false); LauncherAppWidgetProviderInfo info = addWidgetToScreen(true, false,
item.restoreStatus = LauncherAppWidgetInfo.FLAG_ID_NOT_VALID; item -> item.restoreStatus = FLAG_ID_NOT_VALID);
addItemToScreen(item);
verifyPendingWidgetPresent(); verifyPendingWidgetPresent();
// Item deleted from db mCursor = queryItem();
mCursor = mResolver.query(LauncherSettings.Favorites.getContentUri(item.id),
null, null, null, null, null);
mCursor.moveToNext(); mCursor.moveToNext();
// Widget has a valid Id now. // Widget has a valid Id now.
assertEquals(0, mCursor.getInt(mCursor.getColumnIndex(LauncherSettings.Favorites.RESTORED)) assertEquals(0, mCursor.getInt(mCursor.getColumnIndex(LauncherSettings.Favorites.RESTORED))
& LauncherAppWidgetInfo.FLAG_ID_NOT_VALID); & FLAG_ID_NOT_VALID);
assertNotNull(AppWidgetManager.getInstance(mTargetContext) assertNotNull(AppWidgetManager.getInstance(mTargetContext)
.getAppWidgetInfo(mCursor.getInt(mCursor.getColumnIndex( .getAppWidgetInfo(mCursor.getInt(mCursor.getColumnIndex(
LauncherSettings.Favorites.APPWIDGET_ID)))); LauncherSettings.Favorites.APPWIDGET_ID))));
@@ -186,7 +167,6 @@ public class BindWidgetTest extends AbstractLauncherUiTest {
appWidgetManager.updateAppWidgetOptions(appWidgetId, b); appWidgetManager.updateAppWidgetOptions(appWidgetId, b);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews); appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
// verify changes are reflected // verify changes are reflected
waitForLauncherCondition("App widget options did not update", waitForLauncherCondition("App widget options did not update",
l -> appWidgetManager.getAppWidgetOptions(appWidgetId).getBoolean( l -> appWidgetManager.getAppWidgetOptions(appWidgetId).getBoolean(
@@ -198,17 +178,12 @@ public class BindWidgetTest extends AbstractLauncherUiTest {
@Test @Test
public void testPendingWidget_notRestored_removed() { public void testPendingWidget_notRestored_removed() {
LauncherAppWidgetInfo item = getInvalidWidgetInfo(); addPendingItemToScreen(getInvalidWidgetInfo(), FLAG_ID_NOT_VALID | FLAG_PROVIDER_NOT_READY);
item.restoreStatus = LauncherAppWidgetInfo.FLAG_ID_NOT_VALID
| LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY;
addItemToScreen(item);
assertTrue("Pending widget exists", assertTrue("Pending widget exists",
mLauncher.getWorkspace().tryGetPendingWidget(0) == null); mLauncher.getWorkspace().tryGetPendingWidget(0) == null);
// Item deleted from db // Item deleted from db
mCursor = mResolver.query(LauncherSettings.Favorites.getContentUri(item.id), mCursor = queryItem();
null, null, null, null, null);
assertEquals(0, mCursor.getCount()); assertEquals(0, mCursor.getCount());
} }
@@ -216,32 +191,25 @@ public class BindWidgetTest extends AbstractLauncherUiTest {
public void testPendingWidget_notRestored_brokenInstall() { public void testPendingWidget_notRestored_brokenInstall() {
// A widget which is was being installed once, even if its not being // A widget which is was being installed once, even if its not being
// installed at the moment is not removed. // installed at the moment is not removed.
LauncherAppWidgetInfo item = getInvalidWidgetInfo(); addPendingItemToScreen(getInvalidWidgetInfo(),
item.restoreStatus = LauncherAppWidgetInfo.FLAG_ID_NOT_VALID FLAG_ID_NOT_VALID | FLAG_RESTORE_STARTED | FLAG_PROVIDER_NOT_READY);
| LauncherAppWidgetInfo.FLAG_RESTORE_STARTED
| LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY;
addItemToScreen(item);
verifyPendingWidgetPresent(); verifyPendingWidgetPresent();
// Verify item still exists in db // Verify item still exists in db
mCursor = mResolver.query(LauncherSettings.Favorites.getContentUri(item.id), mCursor = queryItem();
null, null, null, null, null);
assertEquals(1, mCursor.getCount()); assertEquals(1, mCursor.getCount());
// Widget still has an invalid id. // Widget still has an invalid id.
mCursor.moveToNext(); mCursor.moveToNext();
assertEquals(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID, assertEquals(FLAG_ID_NOT_VALID,
mCursor.getInt(mCursor.getColumnIndex(LauncherSettings.Favorites.RESTORED)) mCursor.getInt(mCursor.getColumnIndex(LauncherSettings.Favorites.RESTORED))
& LauncherAppWidgetInfo.FLAG_ID_NOT_VALID); & FLAG_ID_NOT_VALID);
} }
@Test @Test
public void testPendingWidget_notRestored_activeInstall() throws Exception { public void testPendingWidget_notRestored_activeInstall() throws Exception {
// A widget which is being installed is not removed // A widget which is being installed is not removed
LauncherAppWidgetInfo item = getInvalidWidgetInfo(); LauncherAppWidgetInfo item = getInvalidWidgetInfo();
item.restoreStatus = LauncherAppWidgetInfo.FLAG_ID_NOT_VALID
| LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY;
// Create an active installer session // Create an active installer session
SessionParams params = new SessionParams(SessionParams.MODE_FULL_INSTALL); SessionParams params = new SessionParams(SessionParams.MODE_FULL_INSTALL);
@@ -249,19 +217,18 @@ public class BindWidgetTest extends AbstractLauncherUiTest {
PackageInstaller installer = mTargetContext.getPackageManager().getPackageInstaller(); PackageInstaller installer = mTargetContext.getPackageManager().getPackageInstaller();
mSessionId = installer.createSession(params); mSessionId = installer.createSession(params);
addItemToScreen(item); addPendingItemToScreen(item, FLAG_ID_NOT_VALID | FLAG_PROVIDER_NOT_READY);
verifyPendingWidgetPresent(); verifyPendingWidgetPresent();
// Verify item still exists in db // Verify item still exists in db
mCursor = mResolver.query(LauncherSettings.Favorites.getContentUri(item.id), mCursor = queryItem();
null, null, null, null, null);
assertEquals(1, mCursor.getCount()); assertEquals(1, mCursor.getCount());
// Widget still has an invalid id. // Widget still has an invalid id.
mCursor.moveToNext(); mCursor.moveToNext();
assertEquals(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID, assertEquals(FLAG_ID_NOT_VALID,
mCursor.getInt(mCursor.getColumnIndex(LauncherSettings.Favorites.RESTORED)) mCursor.getInt(mCursor.getColumnIndex(LauncherSettings.Favorites.RESTORED))
& LauncherAppWidgetInfo.FLAG_ID_NOT_VALID); & FLAG_ID_NOT_VALID);
} }
private void verifyWidgetPresent(LauncherAppWidgetProviderInfo info) { private void verifyWidgetPresent(LauncherAppWidgetProviderInfo info) {
@@ -276,6 +243,28 @@ public class BindWidgetTest extends AbstractLauncherUiTest {
widget != null); widget != null);
} }
private void addPendingItemToScreen(LauncherAppWidgetInfo item, int restoreStatus) {
item.restoreStatus = restoreStatus;
item.screenId = FIRST_SCREEN_ID;
new FavoriteItemsTransaction(mTargetContext)
.addItem(() -> item)
.commitAndLoadHome(mLauncher);
}
private LauncherAppWidgetProviderInfo addWidgetToScreen(boolean hasConfigureScreen,
boolean bindWidget, Consumer<LauncherAppWidgetInfo> itemOverride) {
LauncherAppWidgetProviderInfo info = TestViewHelpers.findWidgetProvider(hasConfigureScreen);
new FavoriteItemsTransaction(mTargetContext)
.addItem(() -> {
LauncherAppWidgetInfo item = createWidgetInfo(info, mTargetContext, bindWidget);
item.screenId = FIRST_SCREEN_ID;
itemOverride.accept(item);
return item;
})
.commitAndLoadHome(mLauncher);
return info;
}
/** /**
* Returns a LauncherAppWidgetInfo with package name which is not present on the device * Returns a LauncherAppWidgetInfo with package name which is not present on the device
*/ */
@@ -313,4 +302,14 @@ public class BindWidgetTest extends AbstractLauncherUiTest {
item.container = LauncherSettings.Favorites.CONTAINER_DESKTOP; item.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
return item; return item;
} }
private Cursor queryItem() {
try {
return MODEL_EXECUTOR.submit(() ->
mModel.getModelDbController().query(
TABLE_NAME, null, itemIdMatch(0), null, null)).get();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
} }
@@ -33,6 +33,7 @@ import androidx.test.filters.LargeTest;
import androidx.test.runner.AndroidJUnit4; import androidx.test.runner.AndroidJUnit4;
import com.android.launcher3.LauncherSettings.Favorites; import com.android.launcher3.LauncherSettings.Favorites;
import com.android.launcher3.celllayout.FavoriteItemsTransaction;
import com.android.launcher3.model.data.ItemInfo; import com.android.launcher3.model.data.ItemInfo;
import com.android.launcher3.model.data.LauncherAppWidgetInfo; import com.android.launcher3.model.data.LauncherAppWidgetInfo;
import com.android.launcher3.model.data.WorkspaceItemInfo; import com.android.launcher3.model.data.WorkspaceItemInfo;
@@ -134,8 +135,7 @@ public class RequestPinItemTest extends AbstractLauncherUiTest {
private void runTest(String activityMethod, boolean isWidget, ItemOperator itemMatcher, private void runTest(String activityMethod, boolean isWidget, ItemOperator itemMatcher,
Intent... commandIntents) throws Throwable { Intent... commandIntents) throws Throwable {
clearHomescreen(); new FavoriteItemsTransaction(mTargetContext).commitAndLoadHome(mLauncher);
mDevice.pressHome();
// Open Pin item activity // Open Pin item activity
BlockingBroadcastReceiver openMonitor = new BlockingBroadcastReceiver( BlockingBroadcastReceiver openMonitor = new BlockingBroadcastReceiver(
@@ -53,7 +53,6 @@ import com.android.launcher3.LauncherAppState;
import com.android.launcher3.LauncherModel; import com.android.launcher3.LauncherModel;
import com.android.launcher3.LauncherModel.ModelUpdateTask; import com.android.launcher3.LauncherModel.ModelUpdateTask;
import com.android.launcher3.LauncherPrefs; import com.android.launcher3.LauncherPrefs;
import com.android.launcher3.LauncherProvider;
import com.android.launcher3.model.AllAppsList; import com.android.launcher3.model.AllAppsList;
import com.android.launcher3.model.BgDataModel; import com.android.launcher3.model.BgDataModel;
import com.android.launcher3.model.BgDataModel.Callbacks; import com.android.launcher3.model.BgDataModel.Callbacks;
@@ -241,12 +240,6 @@ public class LauncherModelHelper {
mPm = spy(getBaseContext().getPackageManager()); mPm = spy(getBaseContext().getPackageManager());
mDbDir = new File(getCacheDir(), UUID.randomUUID().toString()); mDbDir = new File(getCacheDir(), UUID.randomUUID().toString());
setupProvider(LauncherProvider.AUTHORITY, new LauncherProvider() {
@Override
public boolean onCreate() {
return true;
}
});
} }
@Override @Override
@@ -18,18 +18,14 @@ package com.android.launcher3.util;
import static androidx.test.core.app.ApplicationProvider.getApplicationContext; import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation; import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
import static com.android.launcher3.WorkspaceLayoutManager.FIRST_SCREEN_ID;
import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProviderInfo; import android.appwidget.AppWidgetProviderInfo;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.os.Process; import android.os.Process;
import com.android.launcher3.LauncherSettings; import com.android.launcher3.LauncherSettings;
import com.android.launcher3.model.data.ItemInfo;
import com.android.launcher3.model.data.LauncherAppWidgetInfo; import com.android.launcher3.model.data.LauncherAppWidgetInfo;
import com.android.launcher3.widget.LauncherAppWidgetProviderInfo; import com.android.launcher3.widget.LauncherAppWidgetProviderInfo;
import com.android.launcher3.widget.LauncherWidgetHolder; import com.android.launcher3.widget.LauncherWidgetHolder;
@@ -87,33 +83,6 @@ public class WidgetUtils {
return item; return item;
} }
/**
* 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));
}
/** /**
* Creates a {@link AppWidgetProviderInfo} for the provided component name * Creates a {@link AppWidgetProviderInfo} for the provided component name
*/ */