2009-03-03 19:32:27 -08:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2013-06-05 22:57:57 -04:00
|
|
|
package com.android.launcher3;
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2022-11-14 14:30:07 -08:00
|
|
|
import static com.android.launcher3.DefaultLayoutParser.RES_PARTNER_DEFAULT_LAYOUT;
|
2020-02-06 16:37:16 -08:00
|
|
|
import static com.android.launcher3.provider.LauncherDbUtils.copyTable;
|
2018-10-29 14:02:20 -07:00
|
|
|
import static com.android.launcher3.provider.LauncherDbUtils.dropTable;
|
|
|
|
|
import static com.android.launcher3.provider.LauncherDbUtils.tableExists;
|
|
|
|
|
|
2017-05-18 15:03:13 -07:00
|
|
|
import android.annotation.TargetApi;
|
2019-03-07 15:24:46 -08:00
|
|
|
import android.app.backup.BackupManager;
|
2009-10-30 16:36:56 -07:00
|
|
|
import android.appwidget.AppWidgetManager;
|
2011-07-27 22:23:47 -07:00
|
|
|
import android.content.ComponentName;
|
2009-03-03 19:32:27 -08:00
|
|
|
import android.content.ContentProvider;
|
2014-02-11 15:15:29 +00:00
|
|
|
import android.content.ContentProviderOperation;
|
|
|
|
|
import android.content.ContentProviderResult;
|
2011-07-27 22:23:47 -07:00
|
|
|
import android.content.ContentUris;
|
2009-03-03 19:32:27 -08:00
|
|
|
import android.content.ContentValues;
|
2011-07-27 22:23:47 -07:00
|
|
|
import android.content.Context;
|
2009-03-03 19:32:27 -08:00
|
|
|
import android.content.Intent;
|
2014-02-11 15:15:29 +00:00
|
|
|
import android.content.OperationApplicationException;
|
2012-04-25 15:48:32 -07:00
|
|
|
import android.content.SharedPreferences;
|
2019-03-22 14:13:36 -07:00
|
|
|
import android.content.pm.ProviderInfo;
|
2009-03-03 19:32:27 -08:00
|
|
|
import android.database.Cursor;
|
2018-12-07 11:43:47 -08:00
|
|
|
import android.database.DatabaseUtils;
|
2009-03-03 19:32:27 -08:00
|
|
|
import android.database.SQLException;
|
2011-07-27 22:23:47 -07:00
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
|
import android.database.sqlite.SQLiteQueryBuilder;
|
2015-04-02 10:27:03 -07:00
|
|
|
import android.database.sqlite.SQLiteStatement;
|
2009-03-03 19:32:27 -08:00
|
|
|
import android.net.Uri;
|
2015-06-11 16:18:39 -07:00
|
|
|
import android.os.Binder;
|
2017-05-18 15:03:13 -07:00
|
|
|
import android.os.Build;
|
2015-03-26 11:32:11 -07:00
|
|
|
import android.os.Bundle;
|
2015-06-11 16:18:39 -07:00
|
|
|
import android.os.Process;
|
2016-12-15 15:53:17 -08:00
|
|
|
import android.os.UserHandle;
|
2019-12-10 12:19:13 -08:00
|
|
|
import android.os.UserManager;
|
2018-12-07 11:43:47 -08:00
|
|
|
import android.provider.BaseColumns;
|
2019-03-22 14:13:36 -07:00
|
|
|
import android.provider.Settings;
|
2011-07-27 22:23:47 -07:00
|
|
|
import android.text.TextUtils;
|
|
|
|
|
import android.util.Log;
|
2019-03-22 14:13:36 -07:00
|
|
|
import android.util.Xml;
|
2010-02-24 20:01:46 -08:00
|
|
|
|
2022-10-25 15:17:58 -07:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
|
2014-08-06 09:55:36 -07:00
|
|
|
import com.android.launcher3.AutoInstallsLayout.LayoutParserCallback;
|
|
|
|
|
import com.android.launcher3.LauncherSettings.Favorites;
|
2016-06-10 12:22:04 -07:00
|
|
|
import com.android.launcher3.config.FeatureFlags;
|
2017-02-01 12:52:54 -08:00
|
|
|
import com.android.launcher3.logging.FileLog;
|
2017-05-03 12:42:18 -07:00
|
|
|
import com.android.launcher3.model.DbDowngradeHelper;
|
2019-12-10 12:19:13 -08:00
|
|
|
import com.android.launcher3.pm.UserCache;
|
2016-06-10 12:22:04 -07:00
|
|
|
import com.android.launcher3.provider.LauncherDbUtils;
|
2017-04-17 16:58:36 -07:00
|
|
|
import com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction;
|
2016-05-24 11:30:14 -07:00
|
|
|
import com.android.launcher3.provider.RestoreDbTask;
|
2019-03-22 14:13:36 -07:00
|
|
|
import com.android.launcher3.util.IOUtils;
|
2018-10-04 15:11:00 -07:00
|
|
|
import com.android.launcher3.util.IntArray;
|
|
|
|
|
import com.android.launcher3.util.IntSet;
|
2018-03-09 14:37:37 -08:00
|
|
|
import com.android.launcher3.util.NoLocaleSQLiteHelper;
|
2019-07-17 15:12:56 -07:00
|
|
|
import com.android.launcher3.util.PackageManagerHelper;
|
2022-11-14 14:30:07 -08:00
|
|
|
import com.android.launcher3.util.Partner;
|
2015-03-18 14:16:05 -07:00
|
|
|
import com.android.launcher3.util.Thunk;
|
2022-11-08 16:46:07 -08:00
|
|
|
import com.android.launcher3.widget.LauncherWidgetHolder;
|
2012-04-18 14:23:14 -07:00
|
|
|
|
2019-03-22 14:13:36 -07:00
|
|
|
import org.xmlpull.v1.XmlPullParser;
|
|
|
|
|
|
2017-05-03 12:42:18 -07:00
|
|
|
import java.io.File;
|
2017-02-06 10:46:24 -08:00
|
|
|
import java.io.FileDescriptor;
|
2019-03-22 14:13:36 -07:00
|
|
|
import java.io.InputStream;
|
2017-02-06 10:46:24 -08:00
|
|
|
import java.io.PrintWriter;
|
2019-03-22 14:13:36 -07:00
|
|
|
import java.io.StringReader;
|
2011-07-27 22:23:47 -07:00
|
|
|
import java.net.URISyntaxException;
|
|
|
|
|
import java.util.ArrayList;
|
2018-10-04 15:11:00 -07:00
|
|
|
import java.util.Arrays;
|
2018-12-07 11:43:47 -08:00
|
|
|
import java.util.Locale;
|
2020-04-28 13:58:45 -07:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2020-03-17 18:28:38 -07:00
|
|
|
import java.util.function.Supplier;
|
2022-10-03 17:46:03 -07:00
|
|
|
import java.util.stream.Collectors;
|
2010-03-04 13:03:17 -08:00
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
public class LauncherProvider extends ContentProvider {
|
2015-09-08 14:01:03 -07:00
|
|
|
private static final String TAG = "LauncherProvider";
|
2009-11-11 08:16:49 -08:00
|
|
|
private static final boolean LOGD = false;
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2017-05-03 12:42:18 -07:00
|
|
|
private static final String DOWNGRADE_SCHEMA_FILE = "downgrade_schema.json";
|
2020-04-28 13:58:45 -07:00
|
|
|
private static final long RESTORE_BACKUP_TABLE_DELAY = TimeUnit.SECONDS.toMillis(30);
|
2017-05-03 12:42:18 -07:00
|
|
|
|
2017-04-13 12:01:47 -07:00
|
|
|
/**
|
|
|
|
|
* Represents the schema of the database. Changes in scheme need not be backwards compatible.
|
2018-12-07 11:43:47 -08:00
|
|
|
* When increasing the scheme version, ensure that downgrade_schema.json is updated
|
2017-04-13 12:01:47 -07:00
|
|
|
*/
|
2022-01-10 16:13:13 +00:00
|
|
|
public static final int SCHEMA_VERSION = 31;
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2018-11-26 23:44:41 -08:00
|
|
|
public static final String AUTHORITY = BuildConfig.APPLICATION_ID + ".settings";
|
2020-07-20 11:03:39 -07:00
|
|
|
public static final String KEY_LAYOUT_PROVIDER_AUTHORITY = "KEY_LAYOUT_PROVIDER_AUTHORITY";
|
2011-07-13 17:25:49 -07:00
|
|
|
|
2022-02-10 11:10:21 -08:00
|
|
|
private static final int TEST_WORKSPACE_LAYOUT_RES_XML = R.xml.default_test_workspace;
|
2022-12-15 13:41:49 +00:00
|
|
|
private static final int TEST2_WORKSPACE_LAYOUT_RES_XML = R.xml.default_test2_workspace;
|
2022-02-10 11:10:21 -08:00
|
|
|
|
2014-11-21 22:42:53 -08:00
|
|
|
static final String EMPTY_DATABASE_CREATED = "EMPTY_DATABASE_CREATED";
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2016-01-11 12:25:10 -08:00
|
|
|
protected DatabaseHelper mOpenHelper;
|
2020-07-20 11:03:39 -07:00
|
|
|
protected String mProviderAuthority;
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2020-04-28 13:58:45 -07:00
|
|
|
private long mLastRestoreTimestamp = 0L;
|
|
|
|
|
|
2022-12-15 13:41:49 +00:00
|
|
|
private int mDefaultWorkspaceLayoutOverride = 0;
|
2022-02-10 11:10:21 -08:00
|
|
|
|
2017-02-06 10:46:24 -08:00
|
|
|
/**
|
|
|
|
|
* $ adb shell dumpsys activity provider com.android.launcher3
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
|
|
|
|
|
LauncherAppState appState = LauncherAppState.getInstanceNoCreate();
|
|
|
|
|
if (appState == null || !appState.getModel().isModelLoaded()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
appState.getModel().dumpState("", fd, writer, args);
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
@Override
|
|
|
|
|
public boolean onCreate() {
|
2020-02-14 14:15:13 -08:00
|
|
|
if (FeatureFlags.IS_STUDIO_BUILD) {
|
2016-06-20 14:52:14 -07:00
|
|
|
Log.d(TAG, "Launcher process started");
|
|
|
|
|
}
|
2016-05-10 12:28:59 -07:00
|
|
|
|
2017-02-01 12:52:54 -08:00
|
|
|
// The content provider exists for the entire duration of the launcher main process and
|
2018-02-09 10:57:12 -08:00
|
|
|
// is the first component to get created.
|
|
|
|
|
MainProcessInitializer.initialize(getContext().getApplicationContext());
|
2009-03-03 19:32:27 -08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String getType(Uri uri) {
|
|
|
|
|
SqlArguments args = new SqlArguments(uri, null, null);
|
|
|
|
|
if (TextUtils.isEmpty(args.where)) {
|
|
|
|
|
return "vnd.android.cursor.dir/" + args.table;
|
|
|
|
|
} else {
|
|
|
|
|
return "vnd.android.cursor.item/" + args.table;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-11 12:25:10 -08:00
|
|
|
/**
|
|
|
|
|
* Overridden in tests
|
|
|
|
|
*/
|
|
|
|
|
protected synchronized void createDbIfNotExists() {
|
2015-10-29 10:28:32 -07:00
|
|
|
if (mOpenHelper == null) {
|
2020-04-20 01:13:26 -07:00
|
|
|
mOpenHelper = DatabaseHelper.createDatabaseHelper(
|
|
|
|
|
getContext(), false /* forMigration */);
|
2016-05-24 11:30:14 -07:00
|
|
|
|
2021-08-02 12:23:44 -07:00
|
|
|
RestoreDbTask.restoreIfNeeded(getContext(), mOpenHelper);
|
2015-10-29 10:28:32 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 18:28:38 -07:00
|
|
|
private synchronized boolean prepForMigration(String dbFile, String targetTableName,
|
|
|
|
|
Supplier<DatabaseHelper> src, Supplier<DatabaseHelper> dst) {
|
|
|
|
|
if (TextUtils.equals(dbFile, mOpenHelper.getDatabaseName())) {
|
2022-07-04 13:05:38 +01:00
|
|
|
Log.e(TAG, "prepForMigration - target db is same as current: " + dbFile);
|
2020-01-27 13:44:06 -08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 18:28:38 -07:00
|
|
|
final DatabaseHelper helper = src.get();
|
|
|
|
|
mOpenHelper = dst.get();
|
|
|
|
|
copyTable(helper.getReadableDatabase(), Favorites.TABLE_NAME,
|
|
|
|
|
mOpenHelper.getWritableDatabase(), targetTableName, getContext());
|
|
|
|
|
helper.close();
|
2020-01-27 13:44:06 -08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
@Override
|
|
|
|
|
public Cursor query(Uri uri, String[] projection, String selection,
|
|
|
|
|
String[] selectionArgs, String sortOrder) {
|
2015-10-29 10:28:32 -07:00
|
|
|
createDbIfNotExists();
|
2009-03-03 19:32:27 -08:00
|
|
|
|
|
|
|
|
SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
|
|
|
|
|
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
|
|
|
|
|
qb.setTables(args.table);
|
|
|
|
|
|
2009-06-09 12:57:21 -07:00
|
|
|
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
|
2009-03-03 19:32:27 -08:00
|
|
|
Cursor result = qb.query(db, projection, args.where, args.args, null, null, sortOrder);
|
2020-10-14 11:17:04 -07:00
|
|
|
final Bundle extra = new Bundle();
|
|
|
|
|
extra.putString(LauncherSettings.Settings.EXTRA_DB_NAME, mOpenHelper.getDatabaseName());
|
|
|
|
|
result.setExtras(extra);
|
2009-03-03 19:32:27 -08:00
|
|
|
result.setNotificationUri(getContext().getContentResolver(), uri);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-04 15:11:00 -07:00
|
|
|
@Thunk static int dbInsertAndCheck(DatabaseHelper helper,
|
2011-04-28 14:59:33 -07:00
|
|
|
SQLiteDatabase db, String table, String nullColumnHack, ContentValues values) {
|
2014-01-09 15:01:33 -05:00
|
|
|
if (values == null) {
|
|
|
|
|
throw new RuntimeException("Error: attempting to insert null values");
|
|
|
|
|
}
|
2018-12-07 11:43:47 -08:00
|
|
|
if (!values.containsKey(LauncherSettings.Favorites._ID)) {
|
2011-04-28 14:59:33 -07:00
|
|
|
throw new RuntimeException("Error: attempting to add item without specifying an id");
|
|
|
|
|
}
|
2018-12-07 11:43:47 -08:00
|
|
|
helper.checkId(values);
|
2018-10-04 15:11:00 -07:00
|
|
|
return (int) db.insert(table, nullColumnHack, values);
|
2011-04-28 14:59:33 -07:00
|
|
|
}
|
|
|
|
|
|
2015-08-13 12:08:30 -07:00
|
|
|
private void reloadLauncherIfExternal() {
|
2019-01-25 15:10:18 -08:00
|
|
|
if (Binder.getCallingPid() != Process.myPid()) {
|
2015-08-13 12:08:30 -07:00
|
|
|
LauncherAppState app = LauncherAppState.getInstanceNoCreate();
|
|
|
|
|
if (app != null) {
|
2017-02-17 11:22:34 -08:00
|
|
|
app.getModel().forceReload();
|
2015-08-13 12:08:30 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
@Override
|
|
|
|
|
public Uri insert(Uri uri, ContentValues initialValues) {
|
2015-10-29 10:28:32 -07:00
|
|
|
createDbIfNotExists();
|
2009-03-03 19:32:27 -08:00
|
|
|
SqlArguments args = new SqlArguments(uri);
|
|
|
|
|
|
2015-08-13 12:08:30 -07:00
|
|
|
// In very limited cases, we support system|signature permission apps to modify the db.
|
|
|
|
|
if (Binder.getCallingPid() != Process.myPid()) {
|
2016-05-10 12:28:59 -07:00
|
|
|
if (!initializeExternalAdd(initialValues)) {
|
2014-07-23 14:49:38 -07:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
|
2013-09-13 18:01:38 -04:00
|
|
|
addModifiedTime(initialValues);
|
2018-10-04 15:11:00 -07:00
|
|
|
final int rowId = dbInsertAndCheck(mOpenHelper, db, args.table, null, initialValues);
|
2015-03-02 14:24:21 -08:00
|
|
|
if (rowId < 0) return null;
|
2020-01-27 13:44:06 -08:00
|
|
|
onAddOrDeleteOp(db);
|
2009-03-03 19:32:27 -08:00
|
|
|
|
|
|
|
|
uri = ContentUris.withAppendedId(uri, rowId);
|
2019-01-25 15:10:18 -08:00
|
|
|
reloadLauncherIfExternal();
|
2009-03-03 19:32:27 -08:00
|
|
|
return uri;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-10 12:28:59 -07:00
|
|
|
private boolean initializeExternalAdd(ContentValues values) {
|
|
|
|
|
// 1. Ensure that externally added items have a valid item id
|
2018-10-04 15:11:00 -07:00
|
|
|
int id = mOpenHelper.generateNewItemId();
|
2016-05-10 12:28:59 -07:00
|
|
|
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) {
|
2022-10-25 15:17:58 -07:00
|
|
|
LauncherWidgetHolder widgetHolder = mOpenHelper.newLauncherWidgetHolder();
|
2016-05-10 12:28:59 -07:00
|
|
|
try {
|
2022-10-25 15:17:58 -07:00
|
|
|
int appWidgetId = widgetHolder.allocateAppWidgetId();
|
2016-05-10 12:28:59 -07:00
|
|
|
values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId);
|
|
|
|
|
if (!appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,cn)) {
|
2022-10-25 15:17:58 -07:00
|
|
|
widgetHolder.deleteAppWidgetId(appWidgetId);
|
2016-05-10 12:28:59 -07:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
|
Log.e(TAG, "Failed to initialize external widget", e);
|
|
|
|
|
return false;
|
2022-10-25 15:17:58 -07:00
|
|
|
} finally {
|
|
|
|
|
// Necessary to destroy the holder to free up possible activity context
|
|
|
|
|
widgetHolder.destroy();
|
2016-05-10 12:28:59 -07:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-07 11:43:47 -08:00
|
|
|
return true;
|
2016-05-10 12:28:59 -07:00
|
|
|
}
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
@Override
|
|
|
|
|
public int bulkInsert(Uri uri, ContentValues[] values) {
|
2015-10-29 10:28:32 -07:00
|
|
|
createDbIfNotExists();
|
2009-03-03 19:32:27 -08:00
|
|
|
SqlArguments args = new SqlArguments(uri);
|
|
|
|
|
|
|
|
|
|
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
|
2017-04-17 16:58:36 -07:00
|
|
|
try (SQLiteTransaction t = new SQLiteTransaction(db)) {
|
2009-03-03 19:32:27 -08:00
|
|
|
int numValues = values.length;
|
|
|
|
|
for (int i = 0; i < numValues; i++) {
|
2013-09-13 18:01:38 -04:00
|
|
|
addModifiedTime(values[i]);
|
2011-04-28 14:59:33 -07:00
|
|
|
if (dbInsertAndCheck(mOpenHelper, db, args.table, null, values[i]) < 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
2020-01-27 13:44:06 -08:00
|
|
|
onAddOrDeleteOp(db);
|
2017-04-17 16:58:36 -07:00
|
|
|
t.commit();
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
|
2015-08-13 12:08:30 -07:00
|
|
|
reloadLauncherIfExternal();
|
2009-03-03 19:32:27 -08:00
|
|
|
return values.length;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-29 14:02:20 -07:00
|
|
|
@TargetApi(Build.VERSION_CODES.M)
|
2014-02-11 15:15:29 +00:00
|
|
|
@Override
|
|
|
|
|
public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
|
|
|
|
|
throws OperationApplicationException {
|
2015-10-29 10:28:32 -07:00
|
|
|
createDbIfNotExists();
|
2017-04-17 16:58:36 -07:00
|
|
|
try (SQLiteTransaction t = new SQLiteTransaction(mOpenHelper.getWritableDatabase())) {
|
2019-01-25 15:10:18 -08:00
|
|
|
boolean isAddOrDelete = false;
|
2018-10-29 14:02:20 -07:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
isAddOrDelete |= (op.isInsert() || op.isDelete()) &&
|
|
|
|
|
results[i].count != null && results[i].count > 0;
|
|
|
|
|
}
|
|
|
|
|
if (isAddOrDelete) {
|
2020-01-27 13:44:06 -08:00
|
|
|
onAddOrDeleteOp(t.getDb());
|
2018-10-29 14:02:20 -07:00
|
|
|
}
|
|
|
|
|
|
2017-04-17 16:58:36 -07:00
|
|
|
t.commit();
|
2015-08-13 12:08:30 -07:00
|
|
|
reloadLauncherIfExternal();
|
2018-10-29 14:02:20 -07:00
|
|
|
return results;
|
2014-02-11 15:15:29 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
@Override
|
|
|
|
|
public int delete(Uri uri, String selection, String[] selectionArgs) {
|
2015-10-29 10:28:32 -07:00
|
|
|
createDbIfNotExists();
|
2009-03-03 19:32:27 -08:00
|
|
|
SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
|
|
|
|
|
|
|
|
|
|
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
|
|
|
|
|
|
2016-08-15 15:18:41 -07:00
|
|
|
if (Binder.getCallingPid() != Process.myPid()
|
|
|
|
|
&& Favorites.TABLE_NAME.equalsIgnoreCase(args.table)) {
|
2017-04-06 15:02:52 -07:00
|
|
|
mOpenHelper.removeGhostWidgets(mOpenHelper.getWritableDatabase());
|
2016-08-15 15:18:41 -07:00
|
|
|
}
|
|
|
|
|
int count = db.delete(args.table, args.where, args.args);
|
|
|
|
|
if (count > 0) {
|
2020-01-27 13:44:06 -08:00
|
|
|
onAddOrDeleteOp(db);
|
2016-08-15 15:18:41 -07:00
|
|
|
reloadLauncherIfExternal();
|
|
|
|
|
}
|
2009-03-03 19:32:27 -08:00
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
|
2015-10-29 10:28:32 -07:00
|
|
|
createDbIfNotExists();
|
2009-03-03 19:32:27 -08:00
|
|
|
SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
|
|
|
|
|
|
2013-09-13 18:01:38 -04:00
|
|
|
addModifiedTime(values);
|
2009-03-03 19:32:27 -08:00
|
|
|
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
|
|
|
|
|
int count = db.update(args.table, values, args.where, args.args);
|
2015-08-13 12:08:30 -07:00
|
|
|
reloadLauncherIfExternal();
|
2009-03-03 19:32:27 -08:00
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-11 16:18:39 -07:00
|
|
|
@Override
|
2016-03-17 15:39:39 -07:00
|
|
|
public Bundle call(String method, final String arg, final Bundle extras) {
|
2015-06-11 16:18:39 -07:00
|
|
|
if (Binder.getCallingUid() != Process.myUid()) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2015-10-29 10:28:32 -07:00
|
|
|
createDbIfNotExists();
|
2015-06-11 16:18:39 -07:00
|
|
|
|
|
|
|
|
switch (method) {
|
2015-09-22 18:24:19 -07:00
|
|
|
case LauncherSettings.Settings.METHOD_CLEAR_EMPTY_DB_FLAG: {
|
|
|
|
|
clearFlagEmptyDbCreated();
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2016-07-08 08:32:44 -07:00
|
|
|
case LauncherSettings.Settings.METHOD_WAS_EMPTY_DB_CREATED : {
|
|
|
|
|
Bundle result = new Bundle();
|
|
|
|
|
result.putBoolean(LauncherSettings.Settings.EXTRA_VALUE,
|
2022-11-10 23:07:40 +00:00
|
|
|
LauncherPrefs.getPrefs(getContext()).getBoolean(
|
2020-07-20 11:03:39 -07:00
|
|
|
mOpenHelper.getKey(EMPTY_DATABASE_CREATED), false));
|
2016-07-08 08:32:44 -07:00
|
|
|
return result;
|
|
|
|
|
}
|
2015-09-22 18:24:19 -07:00
|
|
|
case LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS: {
|
|
|
|
|
Bundle result = new Bundle();
|
2018-10-04 15:11:00 -07:00
|
|
|
result.putIntArray(LauncherSettings.Settings.EXTRA_VALUE, deleteEmptyFolders()
|
|
|
|
|
.toArray());
|
2015-09-22 18:24:19 -07:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
case LauncherSettings.Settings.METHOD_NEW_ITEM_ID: {
|
|
|
|
|
Bundle result = new Bundle();
|
2020-01-27 13:44:06 -08:00
|
|
|
result.putInt(LauncherSettings.Settings.EXTRA_VALUE,
|
|
|
|
|
mOpenHelper.generateNewItemId());
|
2015-09-22 18:24:19 -07:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
case LauncherSettings.Settings.METHOD_NEW_SCREEN_ID: {
|
|
|
|
|
Bundle result = new Bundle();
|
2020-01-27 13:44:06 -08:00
|
|
|
result.putInt(LauncherSettings.Settings.EXTRA_VALUE,
|
2021-10-15 21:16:48 +01:00
|
|
|
mOpenHelper.getNewScreenId());
|
2015-09-22 18:24:19 -07:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
case LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB: {
|
2017-02-23 18:30:22 -08:00
|
|
|
mOpenHelper.createEmptyDB(mOpenHelper.getWritableDatabase());
|
2015-09-22 18:24:19 -07:00
|
|
|
return null;
|
|
|
|
|
}
|
2022-02-10 11:10:21 -08:00
|
|
|
case LauncherSettings.Settings.METHOD_SET_USE_TEST_WORKSPACE_LAYOUT_FLAG: {
|
2022-12-15 13:41:49 +00:00
|
|
|
switch (arg) {
|
|
|
|
|
case LauncherSettings.Settings.ARG_DEFAULT_WORKSPACE_LAYOUT_TEST:
|
|
|
|
|
mDefaultWorkspaceLayoutOverride = TEST_WORKSPACE_LAYOUT_RES_XML;
|
|
|
|
|
break;
|
|
|
|
|
case LauncherSettings.Settings.ARG_DEFAULT_WORKSPACE_LAYOUT_TEST2:
|
|
|
|
|
mDefaultWorkspaceLayoutOverride = TEST2_WORKSPACE_LAYOUT_RES_XML;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
mDefaultWorkspaceLayoutOverride = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-02-10 11:10:21 -08:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
case LauncherSettings.Settings.METHOD_CLEAR_USE_TEST_WORKSPACE_LAYOUT_FLAG: {
|
2022-12-15 13:41:49 +00:00
|
|
|
mDefaultWorkspaceLayoutOverride = 0;
|
2022-02-10 11:10:21 -08:00
|
|
|
return null;
|
|
|
|
|
}
|
2015-09-22 18:24:19 -07:00
|
|
|
case LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES: {
|
|
|
|
|
loadDefaultFavoritesIfNecessary();
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2017-04-06 15:02:52 -07:00
|
|
|
case LauncherSettings.Settings.METHOD_REMOVE_GHOST_WIDGETS: {
|
|
|
|
|
mOpenHelper.removeGhostWidgets(mOpenHelper.getWritableDatabase());
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2018-10-29 14:02:20 -07:00
|
|
|
case LauncherSettings.Settings.METHOD_NEW_TRANSACTION: {
|
|
|
|
|
Bundle result = new Bundle();
|
|
|
|
|
result.putBinder(LauncherSettings.Settings.EXTRA_VALUE,
|
|
|
|
|
new SQLiteTransaction(mOpenHelper.getWritableDatabase()));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
case LauncherSettings.Settings.METHOD_REFRESH_BACKUP_TABLE: {
|
2020-04-15 22:25:06 -07:00
|
|
|
mOpenHelper.mBackupTableExists = tableExists(mOpenHelper.getReadableDatabase(),
|
|
|
|
|
Favorites.BACKUP_TABLE_NAME);
|
2018-10-29 14:02:20 -07:00
|
|
|
return null;
|
|
|
|
|
}
|
2020-05-29 14:47:42 -07:00
|
|
|
case LauncherSettings.Settings.METHOD_REFRESH_HOTSEAT_RESTORE_TABLE: {
|
|
|
|
|
mOpenHelper.mHotseatRestoreTableExists = tableExists(
|
|
|
|
|
mOpenHelper.getReadableDatabase(), Favorites.HYBRID_HOTSEAT_BACKUP_TABLE);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2019-09-24 14:25:46 -07:00
|
|
|
case LauncherSettings.Settings.METHOD_RESTORE_BACKUP_TABLE: {
|
2020-04-28 13:58:45 -07:00
|
|
|
final long ts = System.currentTimeMillis();
|
|
|
|
|
if (ts - mLastRestoreTimestamp > RESTORE_BACKUP_TABLE_DELAY) {
|
|
|
|
|
mLastRestoreTimestamp = ts;
|
|
|
|
|
RestoreDbTask.restoreIfPossible(
|
|
|
|
|
getContext(), mOpenHelper, new BackupManager(getContext()));
|
|
|
|
|
}
|
2019-09-24 14:25:46 -07:00
|
|
|
return null;
|
|
|
|
|
}
|
2020-01-27 13:44:06 -08:00
|
|
|
case LauncherSettings.Settings.METHOD_UPDATE_CURRENT_OPEN_HELPER: {
|
2021-07-28 14:35:47 -07:00
|
|
|
Bundle result = new Bundle();
|
|
|
|
|
result.putBoolean(LauncherSettings.Settings.EXTRA_VALUE,
|
|
|
|
|
prepForMigration(
|
2022-03-08 21:48:34 +00:00
|
|
|
arg /* dbFile */,
|
2021-07-28 14:35:47 -07:00
|
|
|
Favorites.TMP_TABLE,
|
|
|
|
|
() -> mOpenHelper,
|
|
|
|
|
() -> DatabaseHelper.createDatabaseHelper(
|
|
|
|
|
getContext(), true /* forMigration */)));
|
|
|
|
|
return result;
|
2020-03-17 18:28:38 -07:00
|
|
|
}
|
|
|
|
|
case LauncherSettings.Settings.METHOD_PREP_FOR_PREVIEW: {
|
2021-07-28 14:35:47 -07:00
|
|
|
Bundle result = new Bundle();
|
|
|
|
|
result.putBoolean(LauncherSettings.Settings.EXTRA_VALUE,
|
|
|
|
|
prepForMigration(
|
|
|
|
|
arg /* dbFile */,
|
|
|
|
|
Favorites.PREVIEW_TABLE_NAME,
|
|
|
|
|
() -> DatabaseHelper.createDatabaseHelper(
|
|
|
|
|
getContext(), arg, true /* forMigration */),
|
|
|
|
|
() -> mOpenHelper));
|
|
|
|
|
return result;
|
2020-07-20 11:03:39 -07:00
|
|
|
}
|
|
|
|
|
case LauncherSettings.Settings.METHOD_SWITCH_DATABASE: {
|
|
|
|
|
if (TextUtils.equals(arg, mOpenHelper.getDatabaseName())) return null;
|
|
|
|
|
final DatabaseHelper helper = mOpenHelper;
|
|
|
|
|
if (extras == null || !extras.containsKey(KEY_LAYOUT_PROVIDER_AUTHORITY)) {
|
|
|
|
|
mProviderAuthority = null;
|
|
|
|
|
} else {
|
|
|
|
|
mProviderAuthority = extras.getString(KEY_LAYOUT_PROVIDER_AUTHORITY);
|
|
|
|
|
}
|
|
|
|
|
mOpenHelper = DatabaseHelper.createDatabaseHelper(
|
|
|
|
|
getContext(), arg, false /* forMigration */);
|
|
|
|
|
helper.close();
|
|
|
|
|
LauncherAppState app = LauncherAppState.getInstanceNoCreate();
|
|
|
|
|
if (app == null) return null;
|
|
|
|
|
app.getModel().forceReload();
|
|
|
|
|
return null;
|
2020-01-27 13:44:06 -08:00
|
|
|
}
|
2015-06-11 16:18:39 -07:00
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-27 13:44:06 -08:00
|
|
|
private void onAddOrDeleteOp(SQLiteDatabase db) {
|
2020-04-15 22:25:06 -07:00
|
|
|
mOpenHelper.onAddOrDeleteOp(db);
|
2020-01-27 13:44:06 -08:00
|
|
|
}
|
|
|
|
|
|
2015-06-10 16:00:42 -07:00
|
|
|
/**
|
|
|
|
|
* Deletes any empty folder from the DB.
|
|
|
|
|
* @return Ids of deleted folders.
|
|
|
|
|
*/
|
2018-10-04 15:11:00 -07:00
|
|
|
private IntArray deleteEmptyFolders() {
|
2015-06-10 16:00:42 -07:00
|
|
|
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
|
2017-04-17 16:58:36 -07:00
|
|
|
try (SQLiteTransaction t = new SQLiteTransaction(db)) {
|
2015-06-10 16:00:42 -07:00
|
|
|
// Select folders whose id do not match any container value.
|
|
|
|
|
String selection = LauncherSettings.Favorites.ITEM_TYPE + " = "
|
|
|
|
|
+ LauncherSettings.Favorites.ITEM_TYPE_FOLDER + " AND "
|
|
|
|
|
+ LauncherSettings.Favorites._ID + " NOT IN (SELECT " +
|
|
|
|
|
LauncherSettings.Favorites.CONTAINER + " FROM "
|
2016-05-10 12:28:59 -07:00
|
|
|
+ Favorites.TABLE_NAME + ")";
|
2018-12-07 11:43:47 -08:00
|
|
|
|
2021-11-04 16:49:59 +00:00
|
|
|
IntArray folderIds = LauncherDbUtils.queryIntArray(false, db, Favorites.TABLE_NAME,
|
2018-12-07 11:43:47 -08:00
|
|
|
Favorites._ID, selection, null, null);
|
2015-09-22 18:24:19 -07:00
|
|
|
if (!folderIds.isEmpty()) {
|
2016-05-10 12:28:59 -07:00
|
|
|
db.delete(Favorites.TABLE_NAME, Utilities.createDbSelectionQuery(
|
2015-06-10 16:00:42 -07:00
|
|
|
LauncherSettings.Favorites._ID, folderIds), null);
|
|
|
|
|
}
|
2017-04-17 16:58:36 -07:00
|
|
|
t.commit();
|
2018-12-07 11:43:47 -08:00
|
|
|
return folderIds;
|
2015-06-10 16:00:42 -07:00
|
|
|
} catch (SQLException ex) {
|
|
|
|
|
Log.e(TAG, ex.getMessage(), ex);
|
2018-12-07 11:43:47 -08:00
|
|
|
return new IntArray();
|
2015-06-10 16:00:42 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-18 14:16:05 -07:00
|
|
|
@Thunk static void addModifiedTime(ContentValues values) {
|
2018-12-07 11:43:47 -08:00
|
|
|
values.put(LauncherSettings.Favorites.MODIFIED, System.currentTimeMillis());
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
|
2015-09-22 18:24:19 -07:00
|
|
|
private void clearFlagEmptyDbCreated() {
|
2022-11-10 23:07:40 +00:00
|
|
|
LauncherPrefs.getPrefs(getContext()).edit()
|
2020-07-20 11:03:39 -07:00
|
|
|
.remove(mOpenHelper.getKey(EMPTY_DATABASE_CREATED)).commit();
|
2014-10-16 09:24:19 -07:00
|
|
|
}
|
|
|
|
|
|
2012-10-02 16:55:54 -07:00
|
|
|
/**
|
2014-08-06 09:55:36 -07:00
|
|
|
* Loads the default workspace based on the following priority scheme:
|
2015-03-26 11:32:11 -07:00
|
|
|
* 1) From the app restrictions
|
|
|
|
|
* 2) From a package provided by play store
|
|
|
|
|
* 3) From a partner configuration APK, already in the system image
|
|
|
|
|
* 4) The default configuration for the particular device
|
2012-10-02 16:55:54 -07:00
|
|
|
*/
|
2015-09-22 18:24:19 -07:00
|
|
|
synchronized private void loadDefaultFavoritesIfNecessary() {
|
2022-11-10 23:07:40 +00:00
|
|
|
SharedPreferences sp = LauncherPrefs.getPrefs(getContext());
|
2013-06-06 23:08:25 -07:00
|
|
|
|
2020-07-20 11:03:39 -07:00
|
|
|
if (sp.getBoolean(mOpenHelper.getKey(EMPTY_DATABASE_CREATED), false)) {
|
2013-12-20 17:22:11 -05:00
|
|
|
Log.d(TAG, "loading default workspace");
|
2012-10-08 13:21:35 +02:00
|
|
|
|
2022-10-25 15:17:58 -07:00
|
|
|
LauncherWidgetHolder widgetHolder = mOpenHelper.newLauncherWidgetHolder();
|
2023-02-13 15:12:32 -08:00
|
|
|
try {
|
|
|
|
|
AutoInstallsLayout loader = createWorkspaceLoaderFromAppRestriction(widgetHolder);
|
|
|
|
|
if (loader == null) {
|
|
|
|
|
loader = AutoInstallsLayout.get(getContext(), widgetHolder, mOpenHelper);
|
|
|
|
|
}
|
|
|
|
|
if (loader == null) {
|
|
|
|
|
final Partner partner = Partner.get(getContext().getPackageManager());
|
|
|
|
|
if (partner != null) {
|
|
|
|
|
int workspaceResId = partner.getXmlResId(RES_PARTNER_DEFAULT_LAYOUT);
|
|
|
|
|
if (workspaceResId != 0) {
|
|
|
|
|
loader = new DefaultLayoutParser(getContext(), widgetHolder,
|
|
|
|
|
mOpenHelper, partner.getResources(), workspaceResId);
|
|
|
|
|
}
|
2014-08-06 09:55:36 -07:00
|
|
|
}
|
2014-05-30 15:34:09 -07:00
|
|
|
}
|
2012-10-02 16:55:54 -07:00
|
|
|
|
2023-02-13 15:12:32 -08:00
|
|
|
final boolean usingExternallyProvidedLayout = loader != null;
|
|
|
|
|
if (loader == null) {
|
|
|
|
|
loader = getDefaultLayoutParser(widgetHolder);
|
|
|
|
|
}
|
2015-03-04 09:51:18 -08:00
|
|
|
|
2023-02-13 15:12:32 -08:00
|
|
|
// There might be some partially restored DB items, due to buggy restore logic in
|
|
|
|
|
// previous versions of launcher.
|
2017-02-23 18:30:22 -08:00
|
|
|
mOpenHelper.createEmptyDB(mOpenHelper.getWritableDatabase());
|
2023-02-13 15:12:32 -08:00
|
|
|
// Populate favorites table with initial favorites
|
|
|
|
|
if ((mOpenHelper.loadFavorites(mOpenHelper.getWritableDatabase(), loader) <= 0)
|
|
|
|
|
&& usingExternallyProvidedLayout) {
|
|
|
|
|
// Unable to load external layout. Cleanup and load the internal layout.
|
|
|
|
|
mOpenHelper.createEmptyDB(mOpenHelper.getWritableDatabase());
|
|
|
|
|
mOpenHelper.loadFavorites(mOpenHelper.getWritableDatabase(),
|
|
|
|
|
getDefaultLayoutParser(widgetHolder));
|
|
|
|
|
}
|
|
|
|
|
clearFlagEmptyDbCreated();
|
|
|
|
|
} finally {
|
|
|
|
|
widgetHolder.destroy();
|
2014-10-23 14:21:02 -07:00
|
|
|
}
|
2012-04-25 15:48:32 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-26 11:32:11 -07:00
|
|
|
/**
|
|
|
|
|
* Creates workspace loader from an XML resource listed in the app restrictions.
|
|
|
|
|
*
|
|
|
|
|
* @return the loader if the restrictions are set and the resource exists; null otherwise.
|
|
|
|
|
*/
|
2022-10-25 15:17:58 -07:00
|
|
|
private AutoInstallsLayout createWorkspaceLoaderFromAppRestriction(
|
|
|
|
|
LauncherWidgetHolder widgetHolder) {
|
2015-03-26 11:32:11 -07:00
|
|
|
Context ctx = getContext();
|
2020-07-20 11:03:39 -07:00
|
|
|
final String authority;
|
|
|
|
|
if (!TextUtils.isEmpty(mProviderAuthority)) {
|
|
|
|
|
authority = mProviderAuthority;
|
|
|
|
|
} else {
|
|
|
|
|
authority = Settings.Secure.getString(ctx.getContentResolver(),
|
|
|
|
|
"launcher3.layout.provider");
|
|
|
|
|
}
|
2019-03-22 14:13:36 -07:00
|
|
|
if (TextUtils.isEmpty(authority)) {
|
2015-04-06 10:45:31 -07:00
|
|
|
return null;
|
|
|
|
|
}
|
2015-03-26 11:32:11 -07:00
|
|
|
|
2019-03-22 14:13:36 -07:00
|
|
|
ProviderInfo pi = ctx.getPackageManager().resolveContentProvider(authority, 0);
|
|
|
|
|
if (pi == null) {
|
|
|
|
|
Log.e(TAG, "No provider found for authority " + authority);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2019-12-02 17:00:35 -08:00
|
|
|
Uri uri = getLayoutUri(authority, ctx);
|
2019-03-22 14:13:36 -07:00
|
|
|
try (InputStream in = ctx.getContentResolver().openInputStream(uri)) {
|
|
|
|
|
// Read the full xml so that we fail early in case of any IO error.
|
|
|
|
|
String layout = new String(IOUtils.toByteArray(in));
|
|
|
|
|
XmlPullParser parser = Xml.newPullParser();
|
|
|
|
|
parser.setInput(new StringReader(layout));
|
|
|
|
|
|
|
|
|
|
Log.d(TAG, "Loading layout from " + authority);
|
2022-10-25 15:17:58 -07:00
|
|
|
return new AutoInstallsLayout(ctx, widgetHolder, mOpenHelper,
|
2019-03-22 14:13:36 -07:00
|
|
|
ctx.getPackageManager().getResourcesForApplication(pi.applicationInfo),
|
|
|
|
|
() -> parser, AutoInstallsLayout.TAG_WORKSPACE);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e(TAG, "Error getting layout stream from: " + authority , e);
|
|
|
|
|
return null;
|
2015-03-26 11:32:11 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-02 17:00:35 -08:00
|
|
|
public static Uri getLayoutUri(String authority, Context ctx) {
|
|
|
|
|
InvariantDeviceProfile grid = LauncherAppState.getIDP(ctx);
|
|
|
|
|
return new Uri.Builder().scheme("content").authority(authority).path("launcher_layout")
|
|
|
|
|
.appendQueryParameter("version", "1")
|
|
|
|
|
.appendQueryParameter("gridWidth", Integer.toString(grid.numColumns))
|
|
|
|
|
.appendQueryParameter("gridHeight", Integer.toString(grid.numRows))
|
2021-04-07 15:02:37 -07:00
|
|
|
.appendQueryParameter("hotseatSize", Integer.toString(grid.numDatabaseHotseatIcons))
|
2019-12-02 17:00:35 -08:00
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-25 15:17:58 -07:00
|
|
|
private DefaultLayoutParser getDefaultLayoutParser(LauncherWidgetHolder widgetHolder) {
|
2017-09-22 17:10:55 -07:00
|
|
|
InvariantDeviceProfile idp = LauncherAppState.getIDP(getContext());
|
2022-12-15 13:41:49 +00:00
|
|
|
int defaultLayout = mDefaultWorkspaceLayoutOverride > 0
|
|
|
|
|
? mDefaultWorkspaceLayoutOverride : idp.defaultLayoutId;
|
2017-09-22 17:10:55 -07:00
|
|
|
|
2019-12-10 12:19:13 -08:00
|
|
|
if (getContext().getSystemService(UserManager.class).isDemoUser()
|
|
|
|
|
&& idp.demoModeLayoutId != 0) {
|
2017-09-22 17:10:55 -07:00
|
|
|
defaultLayout = idp.demoModeLayoutId;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-25 15:17:58 -07:00
|
|
|
return new DefaultLayoutParser(getContext(), widgetHolder,
|
2014-10-23 14:21:02 -07:00
|
|
|
mOpenHelper, getContext().getResources(), defaultLayout);
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-11 12:25:10 -08:00
|
|
|
/**
|
|
|
|
|
* The class is subclassed in tests to create an in-memory db.
|
|
|
|
|
*/
|
2020-01-27 13:44:06 -08:00
|
|
|
public static class DatabaseHelper extends NoLocaleSQLiteHelper implements
|
|
|
|
|
LayoutParserCallback {
|
2009-03-03 19:32:27 -08:00
|
|
|
private final Context mContext;
|
2020-04-20 01:13:26 -07:00
|
|
|
private final boolean mForMigration;
|
2018-10-04 15:11:00 -07:00
|
|
|
private int mMaxItemId = -1;
|
2018-10-29 14:02:20 -07:00
|
|
|
private boolean mBackupTableExists;
|
2020-05-29 14:47:42 -07:00
|
|
|
private boolean mHotseatRestoreTableExists;
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2020-04-20 01:13:26 -07:00
|
|
|
static DatabaseHelper createDatabaseHelper(Context context, boolean forMigration) {
|
|
|
|
|
return createDatabaseHelper(context, null, forMigration);
|
2020-03-17 18:28:38 -07:00
|
|
|
}
|
|
|
|
|
|
2020-04-20 01:13:26 -07:00
|
|
|
static DatabaseHelper createDatabaseHelper(Context context, String dbName,
|
|
|
|
|
boolean forMigration) {
|
2020-03-17 18:28:38 -07:00
|
|
|
if (dbName == null) {
|
2021-07-28 14:35:47 -07:00
|
|
|
dbName = InvariantDeviceProfile.INSTANCE.get(context).dbFile;
|
2020-03-17 18:28:38 -07:00
|
|
|
}
|
2020-04-20 01:13:26 -07:00
|
|
|
DatabaseHelper databaseHelper = new DatabaseHelper(context, dbName, forMigration);
|
2015-09-23 11:40:53 -07:00
|
|
|
// Table creation sometimes fails silently, which leads to a crash loop.
|
|
|
|
|
// This way, we will try to create a table every time after crash, so the device
|
|
|
|
|
// would eventually be able to recover.
|
2020-03-17 18:28:38 -07:00
|
|
|
if (!tableExists(databaseHelper.getReadableDatabase(), Favorites.TABLE_NAME)) {
|
2015-09-23 11:40:53 -07:00
|
|
|
Log.e(TAG, "Tables are missing after onCreate has been called. Trying to recreate");
|
|
|
|
|
// This operation is a no-op if the table already exists.
|
2020-03-17 18:28:38 -07:00
|
|
|
databaseHelper.addFavoritesTable(databaseHelper.getWritableDatabase(), true);
|
2015-09-23 11:40:53 -07:00
|
|
|
}
|
2020-05-29 14:47:42 -07:00
|
|
|
databaseHelper.mHotseatRestoreTableExists = tableExists(
|
|
|
|
|
databaseHelper.getReadableDatabase(), Favorites.HYBRID_HOTSEAT_BACKUP_TABLE);
|
2015-09-23 11:40:53 -07:00
|
|
|
|
2020-03-17 18:28:38 -07:00
|
|
|
databaseHelper.initIds();
|
|
|
|
|
return databaseHelper;
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
|
2016-01-11 12:25:10 -08:00
|
|
|
/**
|
2016-05-05 14:37:05 -07:00
|
|
|
* Constructor used in tests and for restore.
|
2016-01-11 12:25:10 -08:00
|
|
|
*/
|
2020-04-20 01:13:26 -07:00
|
|
|
public DatabaseHelper(Context context, String dbName, boolean forMigration) {
|
2020-01-27 13:44:06 -08:00
|
|
|
super(context, dbName, SCHEMA_VERSION);
|
2016-01-11 12:25:10 -08:00
|
|
|
mContext = context;
|
2020-04-20 01:13:26 -07:00
|
|
|
mForMigration = forMigration;
|
2016-03-18 17:42:55 -07:00
|
|
|
}
|
2016-01-11 12:25:10 -08:00
|
|
|
|
2016-03-18 17:42:55 -07:00
|
|
|
protected void initIds() {
|
|
|
|
|
// In the case where neither onCreate nor onUpgrade gets called, we read the maxId from
|
|
|
|
|
// the DB here
|
|
|
|
|
if (mMaxItemId == -1) {
|
|
|
|
|
mMaxItemId = initializeMaxItemId(getWritableDatabase());
|
|
|
|
|
}
|
2016-01-11 12:25:10 -08:00
|
|
|
}
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
@Override
|
|
|
|
|
public void onCreate(SQLiteDatabase db) {
|
2009-11-11 08:16:49 -08:00
|
|
|
if (LOGD) Log.d(TAG, "creating new launcher database");
|
2011-04-28 14:59:33 -07:00
|
|
|
|
2013-06-18 13:13:40 -07:00
|
|
|
mMaxItemId = 1;
|
2011-04-28 14:59:33 -07:00
|
|
|
|
2015-09-23 11:40:53 -07:00
|
|
|
addFavoritesTable(db, false);
|
|
|
|
|
|
|
|
|
|
// Fresh and clean launcher DB.
|
|
|
|
|
mMaxItemId = initializeMaxItemId(db);
|
2020-04-20 01:13:26 -07:00
|
|
|
if (!mForMigration) {
|
|
|
|
|
onEmptyDbCreated();
|
|
|
|
|
}
|
2016-01-11 12:25:10 -08:00
|
|
|
}
|
|
|
|
|
|
2018-10-29 14:02:20 -07:00
|
|
|
protected void onAddOrDeleteOp(SQLiteDatabase db) {
|
2020-04-15 22:25:06 -07:00
|
|
|
if (mBackupTableExists) {
|
2018-10-29 14:02:20 -07:00
|
|
|
dropTable(db, Favorites.BACKUP_TABLE_NAME);
|
|
|
|
|
mBackupTableExists = false;
|
2020-05-29 14:47:42 -07:00
|
|
|
}
|
|
|
|
|
if (mHotseatRestoreTableExists) {
|
|
|
|
|
dropTable(db, Favorites.HYBRID_HOTSEAT_BACKUP_TABLE);
|
|
|
|
|
mHotseatRestoreTableExists = false;
|
2018-10-29 14:02:20 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-20 11:03:39 -07:00
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
* given key. e.g. consider key="EMPTY_DATABASE_CREATED", dbName="minimal.db", the returning
|
|
|
|
|
* string will be "EMPTY_DATABASE_CREATED@minimal.db".
|
|
|
|
|
*/
|
|
|
|
|
String getKey(final String key) {
|
|
|
|
|
if (TextUtils.equals(getDatabaseName(), LauncherFiles.LAUNCHER_DB)) {
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
return key + "@" + getDatabaseName();
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-11 12:25:10 -08:00
|
|
|
/**
|
|
|
|
|
* Overriden in tests.
|
|
|
|
|
*/
|
|
|
|
|
protected void onEmptyDbCreated() {
|
|
|
|
|
// Set the flag for empty DB
|
2022-11-10 23:07:40 +00:00
|
|
|
LauncherPrefs.getPrefs(mContext).edit().putBoolean(getKey(EMPTY_DATABASE_CREATED), true)
|
2020-07-20 11:03:39 -07:00
|
|
|
.commit();
|
2015-09-23 11:40:53 -07:00
|
|
|
}
|
|
|
|
|
|
2019-03-07 15:24:46 -08:00
|
|
|
public long getSerialNumberForUser(UserHandle user) {
|
2019-12-10 12:19:13 -08:00
|
|
|
return UserCache.INSTANCE.get(mContext).getSerialNumberForUser(user);
|
2019-03-07 15:24:46 -08:00
|
|
|
}
|
|
|
|
|
|
2016-05-24 11:30:14 -07:00
|
|
|
public long getDefaultUserSerial() {
|
2019-03-07 15:24:46 -08:00
|
|
|
return getSerialNumberForUser(Process.myUserHandle());
|
2016-01-11 12:25:10 -08:00
|
|
|
}
|
2014-04-30 03:02:21 +01:00
|
|
|
|
2016-01-11 12:25:10 -08:00
|
|
|
private void addFavoritesTable(SQLiteDatabase db, boolean optional) {
|
2016-05-05 14:37:05 -07:00
|
|
|
Favorites.addTableToDb(db, getDefaultUserSerial(), optional);
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
|
2017-04-13 12:01:47 -07:00
|
|
|
@Override
|
|
|
|
|
public void onOpen(SQLiteDatabase db) {
|
|
|
|
|
super.onOpen(db);
|
2017-05-03 12:42:18 -07:00
|
|
|
|
|
|
|
|
File schemaFile = mContext.getFileStreamPath(DOWNGRADE_SCHEMA_FILE);
|
|
|
|
|
if (!schemaFile.exists()) {
|
|
|
|
|
handleOneTimeDataUpgrade(db);
|
2017-04-13 12:01:47 -07:00
|
|
|
}
|
2018-12-07 11:43:47 -08:00
|
|
|
DbDowngradeHelper.updateSchemaFile(schemaFile, SCHEMA_VERSION, mContext);
|
2017-04-13 12:01:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-05-03 12:42:18 -07:00
|
|
|
* One-time data updated before support of onDowngrade was added. This update is backwards
|
|
|
|
|
* compatible and can safely be run multiple times.
|
|
|
|
|
* Note: No new logic should be added here after release, as the new logic might not get
|
|
|
|
|
* executed on an existing device.
|
|
|
|
|
* TODO: Move this to db upgrade path, once the downgrade path is released.
|
2017-04-13 12:01:47 -07:00
|
|
|
*/
|
2017-05-03 12:42:18 -07:00
|
|
|
protected void handleOneTimeDataUpgrade(SQLiteDatabase db) {
|
|
|
|
|
// Remove "profile extra"
|
2019-12-10 12:19:13 -08:00
|
|
|
UserCache um = UserCache.INSTANCE.get(mContext);
|
2017-05-03 12:42:18 -07:00
|
|
|
for (UserHandle user : um.getUserProfiles()) {
|
|
|
|
|
long serial = um.getSerialNumberForUser(user);
|
|
|
|
|
String sql = "update favorites set intent = replace(intent, "
|
|
|
|
|
+ "';l.profile=" + serial + ";', ';') where itemType = 0;";
|
|
|
|
|
db.execSQL(sql);
|
2017-04-13 12:01:47 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
@Override
|
|
|
|
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
2013-07-19 13:49:06 -07:00
|
|
|
if (LOGD) Log.d(TAG, "onUpgrade triggered: " + oldVersion);
|
2015-01-14 14:23:02 -08:00
|
|
|
switch (oldVersion) {
|
|
|
|
|
// The version cannot be lower that 12, as Launcher3 never supported a lower
|
2014-11-21 22:42:53 -08:00
|
|
|
// version of the DB.
|
2018-12-07 11:43:47 -08:00
|
|
|
case 12:
|
|
|
|
|
// No-op
|
2015-01-14 14:23:02 -08:00
|
|
|
case 13: {
|
2017-04-17 16:58:36 -07:00
|
|
|
try (SQLiteTransaction t = new SQLiteTransaction(db)) {
|
2015-01-14 14:23:02 -08:00
|
|
|
// Insert new column for holding widget provider name
|
|
|
|
|
db.execSQL("ALTER TABLE favorites " +
|
|
|
|
|
"ADD COLUMN appWidgetProvider TEXT;");
|
2017-04-17 16:58:36 -07:00
|
|
|
t.commit();
|
2015-01-14 14:23:02 -08:00
|
|
|
} catch (SQLException ex) {
|
|
|
|
|
Log.e(TAG, ex.getMessage(), ex);
|
|
|
|
|
// Old version remains, which means we wipe old data
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-09-13 18:01:38 -04:00
|
|
|
}
|
2015-01-14 14:23:02 -08:00
|
|
|
case 14: {
|
2018-12-07 11:43:47 -08:00
|
|
|
if (!addIntegerColumn(db, Favorites.MODIFIED, 0)) {
|
2015-01-14 14:23:02 -08:00
|
|
|
// Old version remains, which means we wipe old data
|
|
|
|
|
break;
|
|
|
|
|
}
|
2014-01-16 18:13:56 -05:00
|
|
|
}
|
2015-01-14 14:23:02 -08:00
|
|
|
case 15: {
|
2015-03-10 13:14:47 -07:00
|
|
|
if (!addIntegerColumn(db, Favorites.RESTORED, 0)) {
|
2015-01-14 14:23:02 -08:00
|
|
|
// Old version remains, which means we wipe old data
|
|
|
|
|
break;
|
|
|
|
|
}
|
2014-04-30 03:02:21 +01:00
|
|
|
}
|
2018-12-07 11:43:47 -08:00
|
|
|
case 16:
|
2016-09-01 12:50:11 -07:00
|
|
|
// No-op
|
2018-12-07 11:43:47 -08:00
|
|
|
case 17:
|
|
|
|
|
// No-op
|
|
|
|
|
case 18:
|
2015-01-14 14:23:02 -08:00
|
|
|
// No-op
|
|
|
|
|
case 19: {
|
|
|
|
|
// Add userId column
|
2018-12-07 11:43:47 -08:00
|
|
|
if (!addIntegerColumn(db, Favorites.PROFILE_ID, getDefaultUserSerial())) {
|
2015-01-14 14:23:02 -08:00
|
|
|
// Old version remains, which means we wipe old data
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
case 20:
|
|
|
|
|
if (!updateFolderItemsRank(db, true)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2015-02-25 10:46:34 -08:00
|
|
|
case 21:
|
2018-12-07 11:43:47 -08:00
|
|
|
// No-op
|
2015-02-25 10:46:34 -08:00
|
|
|
case 22: {
|
2015-03-10 13:14:47 -07:00
|
|
|
if (!addIntegerColumn(db, Favorites.OPTIONS, 0)) {
|
|
|
|
|
// Old version remains, which means we wipe old data
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-04-02 10:27:03 -07:00
|
|
|
case 23:
|
2015-05-19 16:03:28 -07:00
|
|
|
// No-op
|
2015-05-12 10:39:59 -07:00
|
|
|
case 24:
|
2018-02-27 15:06:12 +00:00
|
|
|
// No-op
|
2015-05-19 16:03:28 -07:00
|
|
|
case 25:
|
|
|
|
|
convertShortcutsToLauncherActivities(db);
|
2016-06-10 12:22:04 -07:00
|
|
|
case 26:
|
2021-07-28 14:35:47 -07:00
|
|
|
// QSB was moved to the grid. Ignore overlapping items
|
2018-12-07 11:43:47 -08:00
|
|
|
case 27: {
|
|
|
|
|
// Update the favorites table so that the screen ids are ordered based on
|
|
|
|
|
// workspace page rank.
|
2021-11-04 16:49:59 +00:00
|
|
|
IntArray finalScreens = LauncherDbUtils.queryIntArray(false, db,
|
|
|
|
|
"workspaceScreens", BaseColumns._ID, null, null, "screenRank");
|
2018-12-07 11:43:47 -08:00
|
|
|
int[] original = finalScreens.toArray();
|
|
|
|
|
Arrays.sort(original);
|
|
|
|
|
String updatemap = "";
|
|
|
|
|
for (int i = 0; i < original.length; i++) {
|
|
|
|
|
if (finalScreens.get(i) != original[i]) {
|
|
|
|
|
updatemap += String.format(Locale.ENGLISH, " WHEN %1$s=%2$d THEN %3$d",
|
|
|
|
|
Favorites.SCREEN, finalScreens.get(i), original[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!TextUtils.isEmpty(updatemap)) {
|
|
|
|
|
String query = String.format(Locale.ENGLISH,
|
|
|
|
|
"UPDATE %1$s SET %2$s=CASE %3$s ELSE %2$s END WHERE %4$s = %5$d",
|
|
|
|
|
Favorites.TABLE_NAME, Favorites.SCREEN, updatemap,
|
|
|
|
|
Favorites.CONTAINER, Favorites.CONTAINER_DESKTOP);
|
|
|
|
|
db.execSQL(query);
|
|
|
|
|
}
|
2018-10-29 14:02:20 -07:00
|
|
|
dropTable(db, "workspaceScreens");
|
2018-12-07 11:43:47 -08:00
|
|
|
}
|
2021-05-24 23:23:15 +01:00
|
|
|
case 28: {
|
|
|
|
|
boolean columnAdded = addIntegerColumn(
|
|
|
|
|
db, Favorites.APPWIDGET_SOURCE, Favorites.CONTAINER_UNKNOWN);
|
|
|
|
|
if (!columnAdded) {
|
|
|
|
|
// Old version remains, which means we wipe old data
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
case 29: {
|
2021-09-06 03:30:30 +01:00
|
|
|
// Remove widget panel related leftover workspace items
|
|
|
|
|
db.delete(Favorites.TABLE_NAME, Utilities.createDbSelectionQuery(
|
|
|
|
|
Favorites.SCREEN, IntArray.wrap(-777, -778)), null);
|
|
|
|
|
}
|
|
|
|
|
case 30: {
|
2022-01-10 16:13:13 +00:00
|
|
|
if (FeatureFlags.QSB_ON_FIRST_SCREEN) {
|
|
|
|
|
// Clean up first row in screen 0 as it might contain junk data.
|
|
|
|
|
Log.d(TAG, "Cleaning up first row");
|
|
|
|
|
db.delete(Favorites.TABLE_NAME,
|
|
|
|
|
String.format(Locale.ENGLISH,
|
|
|
|
|
"%1$s = %2$d AND %3$s = %4$d AND %5$s = %6$d",
|
|
|
|
|
Favorites.SCREEN, 0,
|
|
|
|
|
Favorites.CONTAINER, Favorites.CONTAINER_DESKTOP,
|
|
|
|
|
Favorites.CELLY, 0), null);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
case 31: {
|
2015-01-14 14:23:02 -08:00
|
|
|
// DB Upgraded successfully
|
|
|
|
|
return;
|
2021-05-24 23:23:15 +01:00
|
|
|
}
|
2015-01-05 13:41:43 -08:00
|
|
|
}
|
|
|
|
|
|
2015-01-14 14:23:02 -08:00
|
|
|
// DB was not upgraded
|
|
|
|
|
Log.w(TAG, "Destroying all old data.");
|
|
|
|
|
createEmptyDB(db);
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
2009-12-02 20:10:07 -08:00
|
|
|
|
2014-05-21 19:01:57 -07:00
|
|
|
@Override
|
|
|
|
|
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
2017-05-03 12:42:18 -07:00
|
|
|
try {
|
|
|
|
|
DbDowngradeHelper.parse(mContext.getFileStreamPath(DOWNGRADE_SCHEMA_FILE))
|
|
|
|
|
.onDowngrade(db, oldVersion, newVersion);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.d(TAG, "Unable to downgrade from: " + oldVersion + " to " + newVersion +
|
|
|
|
|
". Wiping databse.", e);
|
|
|
|
|
createEmptyDB(db);
|
|
|
|
|
}
|
2014-09-26 22:09:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clears all the data for a fresh start.
|
|
|
|
|
*/
|
|
|
|
|
public void createEmptyDB(SQLiteDatabase db) {
|
2017-04-17 16:58:36 -07:00
|
|
|
try (SQLiteTransaction t = new SQLiteTransaction(db)) {
|
2018-10-29 14:02:20 -07:00
|
|
|
dropTable(db, Favorites.TABLE_NAME);
|
|
|
|
|
dropTable(db, "workspaceScreens");
|
2017-02-23 18:30:22 -08:00
|
|
|
onCreate(db);
|
2017-04-17 16:58:36 -07:00
|
|
|
t.commit();
|
2017-02-23 18:30:22 -08:00
|
|
|
}
|
2014-05-21 19:01:57 -07:00
|
|
|
}
|
|
|
|
|
|
2017-04-06 15:02:52 -07:00
|
|
|
/**
|
|
|
|
|
* Removes widgets which are registered to the Launcher's host, but are not present
|
|
|
|
|
* in our model.
|
|
|
|
|
*/
|
|
|
|
|
public void removeGhostWidgets(SQLiteDatabase db) {
|
|
|
|
|
// Get all existing widget ids.
|
2022-10-25 15:17:58 -07:00
|
|
|
final LauncherWidgetHolder holder = newLauncherWidgetHolder();
|
2017-04-06 15:02:52 -07:00
|
|
|
try {
|
2022-10-25 15:17:58 -07:00
|
|
|
final int[] allWidgets;
|
|
|
|
|
try {
|
|
|
|
|
// Although the method was defined in O, it has existed since the beginning of
|
|
|
|
|
// time, so it might work on older platforms as well.
|
|
|
|
|
allWidgets = holder.getAppWidgetIds();
|
|
|
|
|
} catch (IncompatibleClassChangeError e) {
|
|
|
|
|
Log.e(TAG, "getAppWidgetIds not supported", e);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
final IntSet validWidgets = IntSet.wrap(LauncherDbUtils.queryIntArray(false, db,
|
|
|
|
|
Favorites.TABLE_NAME, Favorites.APPWIDGET_ID,
|
|
|
|
|
"itemType=" + Favorites.ITEM_TYPE_APPWIDGET, null, null));
|
|
|
|
|
boolean isAnyWidgetRemoved = false;
|
|
|
|
|
for (int widgetId : allWidgets) {
|
|
|
|
|
if (!validWidgets.contains(widgetId)) {
|
|
|
|
|
try {
|
|
|
|
|
FileLog.d(TAG, "Deleting invalid widget " + widgetId);
|
|
|
|
|
holder.deleteAppWidgetId(widgetId);
|
|
|
|
|
isAnyWidgetRemoved = true;
|
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
|
// Ignore
|
|
|
|
|
}
|
2017-04-17 16:58:36 -07:00
|
|
|
}
|
|
|
|
|
}
|
2022-10-25 15:17:58 -07:00
|
|
|
if (isAnyWidgetRemoved) {
|
|
|
|
|
final String allWidgetsIds = Arrays.stream(allWidgets).mapToObj(String::valueOf)
|
|
|
|
|
.collect(Collectors.joining(",", "[", "]"));
|
|
|
|
|
final String validWidgetsIds = Arrays.stream(
|
|
|
|
|
validWidgets.getArray().toArray()).mapToObj(String::valueOf)
|
|
|
|
|
.collect(Collectors.joining(",", "[", "]"));
|
|
|
|
|
FileLog.d(TAG, "One or more widgets was removed. db_path=" + db.getPath()
|
|
|
|
|
+ " allWidgetsIds=" + allWidgetsIds
|
|
|
|
|
+ ", validWidgetsIds=" + validWidgetsIds);
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
holder.destroy();
|
2022-10-03 17:46:03 -07:00
|
|
|
}
|
2017-04-06 15:02:52 -07:00
|
|
|
}
|
|
|
|
|
|
2015-04-02 10:27:03 -07:00
|
|
|
/**
|
|
|
|
|
* Replaces all shortcuts of type {@link Favorites#ITEM_TYPE_SHORTCUT} which have a valid
|
|
|
|
|
* launcher activity target with {@link Favorites#ITEM_TYPE_APPLICATION}.
|
|
|
|
|
*/
|
2015-06-02 09:38:28 -07:00
|
|
|
@Thunk void convertShortcutsToLauncherActivities(SQLiteDatabase db) {
|
2017-04-17 16:58:36 -07:00
|
|
|
try (SQLiteTransaction t = new SQLiteTransaction(db);
|
|
|
|
|
// Only consider the primary user as other users can't have a shortcut.
|
|
|
|
|
Cursor c = db.query(Favorites.TABLE_NAME,
|
|
|
|
|
new String[] { Favorites._ID, Favorites.INTENT},
|
|
|
|
|
"itemType=" + Favorites.ITEM_TYPE_SHORTCUT +
|
|
|
|
|
" AND profileId=" + getDefaultUserSerial(),
|
|
|
|
|
null, null, null, null);
|
|
|
|
|
SQLiteStatement updateStmt = db.compileStatement("UPDATE favorites SET itemType="
|
|
|
|
|
+ Favorites.ITEM_TYPE_APPLICATION + " WHERE _id=?")
|
|
|
|
|
) {
|
2015-04-02 10:27:03 -07:00
|
|
|
final int idIndex = c.getColumnIndexOrThrow(Favorites._ID);
|
|
|
|
|
final int intentIndex = c.getColumnIndexOrThrow(Favorites.INTENT);
|
|
|
|
|
|
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
|
String intentDescription = c.getString(intentIndex);
|
|
|
|
|
Intent intent;
|
|
|
|
|
try {
|
|
|
|
|
intent = Intent.parseUri(intentDescription, 0);
|
|
|
|
|
} catch (URISyntaxException e) {
|
|
|
|
|
Log.e(TAG, "Unable to parse intent", e);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-17 15:12:56 -07:00
|
|
|
if (!PackageManagerHelper.isLauncherAppTarget(intent)) {
|
2015-04-02 10:27:03 -07:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-04 15:11:00 -07:00
|
|
|
int id = c.getInt(idIndex);
|
2015-04-02 10:27:03 -07:00
|
|
|
updateStmt.bindLong(1, id);
|
2015-07-13 19:59:50 -07:00
|
|
|
updateStmt.executeUpdateDelete();
|
2015-04-02 10:27:03 -07:00
|
|
|
}
|
2017-04-17 16:58:36 -07:00
|
|
|
t.commit();
|
2015-04-02 10:27:03 -07:00
|
|
|
} catch (SQLException ex) {
|
|
|
|
|
Log.w(TAG, "Error deduping shortcuts", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-18 14:16:05 -07:00
|
|
|
@Thunk boolean updateFolderItemsRank(SQLiteDatabase db, boolean addRankColumn) {
|
2017-04-17 16:58:36 -07:00
|
|
|
try (SQLiteTransaction t = new SQLiteTransaction(db)) {
|
2015-01-05 13:41:43 -08:00
|
|
|
if (addRankColumn) {
|
|
|
|
|
// Insert new column for holding rank
|
|
|
|
|
db.execSQL("ALTER TABLE favorites ADD COLUMN rank INTEGER NOT NULL DEFAULT 0;");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get a map for folder ID to folder width
|
|
|
|
|
Cursor c = db.rawQuery("SELECT container, MAX(cellX) FROM favorites"
|
|
|
|
|
+ " WHERE container IN (SELECT _id FROM favorites WHERE itemType = ?)"
|
|
|
|
|
+ " GROUP BY container;",
|
|
|
|
|
new String[] {Integer.toString(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)});
|
|
|
|
|
|
|
|
|
|
while (c.moveToNext()) {
|
2015-02-10 19:52:36 -08:00
|
|
|
db.execSQL("UPDATE favorites SET rank=cellX+(cellY*?) WHERE "
|
|
|
|
|
+ "container=? AND cellX IS NOT NULL AND cellY IS NOT NULL;",
|
2015-01-05 13:41:43 -08:00
|
|
|
new Object[] {c.getLong(1) + 1, c.getLong(0)});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.close();
|
2017-04-17 16:58:36 -07:00
|
|
|
t.commit();
|
2015-01-05 13:41:43 -08:00
|
|
|
} catch (SQLException ex) {
|
|
|
|
|
// Old version remains, which means we wipe old data
|
|
|
|
|
Log.e(TAG, ex.getMessage(), ex);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-10 13:14:47 -07:00
|
|
|
private boolean addIntegerColumn(SQLiteDatabase db, String columnName, long defaultValue) {
|
2017-04-17 16:58:36 -07:00
|
|
|
try (SQLiteTransaction t = new SQLiteTransaction(db)) {
|
2015-03-10 13:14:47 -07:00
|
|
|
db.execSQL("ALTER TABLE favorites ADD COLUMN "
|
|
|
|
|
+ columnName + " INTEGER NOT NULL DEFAULT " + defaultValue + ";");
|
2017-04-17 16:58:36 -07:00
|
|
|
t.commit();
|
2014-04-30 03:02:21 +01:00
|
|
|
} catch (SQLException ex) {
|
|
|
|
|
Log.e(TAG, ex.getMessage(), ex);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-28 14:59:33 -07:00
|
|
|
// Generates a new ID to use for an object in your database. This method should be only
|
|
|
|
|
// called from the main UI thread. As an exception, we do call it when we call the
|
|
|
|
|
// constructor from the worker thread; however, this doesn't extend until after the
|
|
|
|
|
// constructor is called, and we only pass a reference to LauncherProvider to LauncherApp
|
|
|
|
|
// after that point
|
2014-08-06 09:55:36 -07:00
|
|
|
@Override
|
2018-10-04 15:11:00 -07:00
|
|
|
public int generateNewItemId() {
|
2013-06-18 13:13:40 -07:00
|
|
|
if (mMaxItemId < 0) {
|
|
|
|
|
throw new RuntimeException("Error: max item id was not initialized");
|
2011-04-28 14:59:33 -07:00
|
|
|
}
|
2013-06-18 13:13:40 -07:00
|
|
|
mMaxItemId += 1;
|
|
|
|
|
return mMaxItemId;
|
2011-04-28 14:59:33 -07:00
|
|
|
}
|
|
|
|
|
|
2022-10-25 15:17:58 -07:00
|
|
|
/**
|
|
|
|
|
* @return A new {@link LauncherWidgetHolder} based on the current context
|
|
|
|
|
*/
|
|
|
|
|
@NonNull
|
|
|
|
|
public LauncherWidgetHolder newLauncherWidgetHolder() {
|
2022-11-08 17:13:13 -08:00
|
|
|
return LauncherWidgetHolder.newInstance(mContext);
|
2017-04-06 15:02:52 -07:00
|
|
|
}
|
|
|
|
|
|
2014-08-06 09:55:36 -07:00
|
|
|
@Override
|
2018-10-04 15:11:00 -07:00
|
|
|
public int insertAndCheck(SQLiteDatabase db, ContentValues values) {
|
2016-05-10 12:28:59 -07:00
|
|
|
return dbInsertAndCheck(this, db, Favorites.TABLE_NAME, null, values);
|
2014-08-06 09:55:36 -07:00
|
|
|
}
|
|
|
|
|
|
2018-12-07 11:43:47 -08:00
|
|
|
public void checkId(ContentValues values) {
|
|
|
|
|
int id = values.getAsInteger(Favorites._ID);
|
|
|
|
|
mMaxItemId = Math.max(id, mMaxItemId);
|
2013-12-20 17:22:11 -05:00
|
|
|
}
|
|
|
|
|
|
2018-10-04 15:11:00 -07:00
|
|
|
private int initializeMaxItemId(SQLiteDatabase db) {
|
2018-12-07 11:43:47 -08:00
|
|
|
return getMaxId(db, "SELECT MAX(%1$s) FROM %2$s", Favorites._ID, Favorites.TABLE_NAME);
|
2013-06-18 13:13:40 -07:00
|
|
|
}
|
|
|
|
|
|
2021-10-15 21:16:48 +01:00
|
|
|
// Returns a new ID to use for an workspace screen in your database that is greater than all
|
|
|
|
|
// existing screen IDs.
|
|
|
|
|
private int getNewScreenId() {
|
|
|
|
|
return getMaxId(getWritableDatabase(),
|
|
|
|
|
"SELECT MAX(%1$s) FROM %2$s WHERE %3$s = %4$d AND %1$s >= 0",
|
2018-12-07 11:43:47 -08:00
|
|
|
Favorites.SCREEN, Favorites.TABLE_NAME, Favorites.CONTAINER,
|
2021-10-15 21:16:48 +01:00
|
|
|
Favorites.CONTAINER_DESKTOP) + 1;
|
2014-07-31 00:09:45 -07:00
|
|
|
}
|
|
|
|
|
|
2015-03-18 14:16:05 -07:00
|
|
|
@Thunk int loadFavorites(SQLiteDatabase db, AutoInstallsLayout loader) {
|
2014-08-06 09:55:36 -07:00
|
|
|
// TODO: Use multiple loaders with fall-back and transaction.
|
2018-12-07 11:43:47 -08:00
|
|
|
int count = loader.loadLayout(db, new IntArray());
|
2014-05-15 14:04:01 -07:00
|
|
|
|
|
|
|
|
// Ensure that the max ids are initialized
|
|
|
|
|
mMaxItemId = initializeMaxItemId(db);
|
|
|
|
|
return count;
|
|
|
|
|
}
|
2009-10-30 16:36:56 -07:00
|
|
|
}
|
2012-04-23 21:35:11 -07:00
|
|
|
|
2015-03-04 10:43:45 -08:00
|
|
|
/**
|
|
|
|
|
* @return the max _id in the provided table.
|
|
|
|
|
*/
|
2018-12-07 11:43:47 -08:00
|
|
|
@Thunk static int getMaxId(SQLiteDatabase db, String query, Object... args) {
|
2020-11-24 11:35:24 -08:00
|
|
|
int max = 0;
|
|
|
|
|
try (SQLiteStatement prog = db.compileStatement(
|
|
|
|
|
String.format(Locale.ENGLISH, query, args))) {
|
|
|
|
|
max = (int) DatabaseUtils.longForQuery(prog, null);
|
|
|
|
|
if (max < 0) {
|
|
|
|
|
throw new RuntimeException("Error: could not query max id");
|
|
|
|
|
}
|
|
|
|
|
} catch (IllegalArgumentException exception) {
|
|
|
|
|
String message = exception.getMessage();
|
|
|
|
|
if (message.contains("re-open") && message.contains("already-closed")) {
|
|
|
|
|
// Don't crash trying to end a transaction an an already closed DB. See b/173162852.
|
|
|
|
|
} else {
|
|
|
|
|
throw exception;
|
|
|
|
|
}
|
2018-12-07 11:43:47 -08:00
|
|
|
}
|
|
|
|
|
return max;
|
2015-03-04 10:43:45 -08:00
|
|
|
}
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
static class SqlArguments {
|
|
|
|
|
public final String table;
|
|
|
|
|
public final String where;
|
|
|
|
|
public final String[] args;
|
|
|
|
|
|
|
|
|
|
SqlArguments(Uri url, String where, String[] args) {
|
|
|
|
|
if (url.getPathSegments().size() == 1) {
|
|
|
|
|
this.table = url.getPathSegments().get(0);
|
|
|
|
|
this.where = where;
|
|
|
|
|
this.args = args;
|
|
|
|
|
} else if (url.getPathSegments().size() != 2) {
|
|
|
|
|
throw new IllegalArgumentException("Invalid URI: " + url);
|
|
|
|
|
} else if (!TextUtils.isEmpty(where)) {
|
|
|
|
|
throw new UnsupportedOperationException("WHERE clause not supported: " + url);
|
|
|
|
|
} else {
|
|
|
|
|
this.table = url.getPathSegments().get(0);
|
2012-04-23 21:35:11 -07:00
|
|
|
this.where = "_id=" + ContentUris.parseId(url);
|
2009-03-03 19:32:27 -08:00
|
|
|
this.args = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SqlArguments(Uri url) {
|
|
|
|
|
if (url.getPathSegments().size() == 1) {
|
|
|
|
|
table = url.getPathSegments().get(0);
|
|
|
|
|
where = null;
|
|
|
|
|
args = null;
|
|
|
|
|
} else {
|
|
|
|
|
throw new IllegalArgumentException("Invalid URI: " + url);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-15 18:13:55 -08:00
|
|
|
}
|