The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
Tracy Zhou | f601872 | 2020-02-06 16:37:16 -0800 | [diff] [blame] | 19 | import static com.android.launcher3.provider.LauncherDbUtils.copyTable; |
Sunny Goyal | 161a214 | 2018-10-29 14:02:20 -0700 | [diff] [blame] | 20 | import static com.android.launcher3.provider.LauncherDbUtils.dropTable; |
| 21 | import static com.android.launcher3.provider.LauncherDbUtils.tableExists; |
| 22 | |
Sunny Goyal | 22ca9ec | 2017-05-18 15:03:13 -0700 | [diff] [blame] | 23 | import android.annotation.TargetApi; |
Jon Miranda | 2bc6b93 | 2019-03-07 15:24:46 -0800 | [diff] [blame] | 24 | import android.app.backup.BackupManager; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 25 | import android.appwidget.AppWidgetHost; |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 26 | import android.appwidget.AppWidgetManager; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 27 | import android.content.ComponentName; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 28 | import android.content.ContentProvider; |
Yura | 085c853 | 2014-02-11 15:15:29 +0000 | [diff] [blame] | 29 | import android.content.ContentProviderOperation; |
| 30 | import android.content.ContentProviderResult; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 31 | import android.content.ContentUris; |
| 32 | import android.content.ContentValues; |
| 33 | import android.content.Context; |
| 34 | import android.content.Intent; |
Yura | 085c853 | 2014-02-11 15:15:29 +0000 | [diff] [blame] | 35 | import android.content.OperationApplicationException; |
Michael Jurka | b85f8a4 | 2012-04-25 15:48:32 -0700 | [diff] [blame] | 36 | import android.content.SharedPreferences; |
Sunny Goyal | c0f03d9 | 2019-03-22 14:13:36 -0700 | [diff] [blame] | 37 | import android.content.pm.ProviderInfo; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 38 | import android.content.res.Resources; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 39 | import android.database.Cursor; |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 40 | import android.database.DatabaseUtils; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 41 | import android.database.SQLException; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 42 | import android.database.sqlite.SQLiteDatabase; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 43 | import android.database.sqlite.SQLiteQueryBuilder; |
Sunny Goyal | 0b03778 | 2015-04-02 10:27:03 -0700 | [diff] [blame] | 44 | import android.database.sqlite.SQLiteStatement; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 45 | import android.net.Uri; |
Sunny Goyal | 7779d62 | 2015-06-11 16:18:39 -0700 | [diff] [blame] | 46 | import android.os.Binder; |
Sunny Goyal | 22ca9ec | 2017-05-18 15:03:13 -0700 | [diff] [blame] | 47 | import android.os.Build; |
Sunny Goyal | b2d46ce | 2015-03-26 11:32:11 -0700 | [diff] [blame] | 48 | import android.os.Bundle; |
Sunny Goyal | 7779d62 | 2015-06-11 16:18:39 -0700 | [diff] [blame] | 49 | import android.os.Process; |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 50 | import android.os.UserHandle; |
Sunny Goyal | 337c81f | 2019-12-10 12:19:13 -0800 | [diff] [blame] | 51 | import android.os.UserManager; |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 52 | import android.provider.BaseColumns; |
Sunny Goyal | c0f03d9 | 2019-03-22 14:13:36 -0700 | [diff] [blame] | 53 | import android.provider.Settings; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 54 | import android.text.TextUtils; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 55 | import android.util.Log; |
Sunny Goyal | c0f03d9 | 2019-03-22 14:13:36 -0700 | [diff] [blame] | 56 | import android.util.Xml; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 57 | |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 58 | import com.android.launcher3.AutoInstallsLayout.LayoutParserCallback; |
| 59 | import com.android.launcher3.LauncherSettings.Favorites; |
Sunny Goyal | a9e2f5a | 2016-06-10 12:22:04 -0700 | [diff] [blame] | 60 | import com.android.launcher3.config.FeatureFlags; |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 61 | import com.android.launcher3.logging.FileLog; |
Sunny Goyal | 05f3088 | 2017-05-03 12:42:18 -0700 | [diff] [blame] | 62 | import com.android.launcher3.model.DbDowngradeHelper; |
Sunny Goyal | 337c81f | 2019-12-10 12:19:13 -0800 | [diff] [blame] | 63 | import com.android.launcher3.pm.UserCache; |
Sunny Goyal | a9e2f5a | 2016-06-10 12:22:04 -0700 | [diff] [blame] | 64 | import com.android.launcher3.provider.LauncherDbUtils; |
Sunny Goyal | dbfc901 | 2017-04-17 16:58:36 -0700 | [diff] [blame] | 65 | import com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction; |
Sunny Goyal | e8f7d5a | 2016-05-24 11:30:14 -0700 | [diff] [blame] | 66 | import com.android.launcher3.provider.RestoreDbTask; |
Sunny Goyal | c0f03d9 | 2019-03-22 14:13:36 -0700 | [diff] [blame] | 67 | import com.android.launcher3.util.IOUtils; |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 68 | import com.android.launcher3.util.IntArray; |
| 69 | import com.android.launcher3.util.IntSet; |
Sunny Goyal | 19026b2 | 2018-03-09 14:37:37 -0800 | [diff] [blame] | 70 | import com.android.launcher3.util.NoLocaleSQLiteHelper; |
Sunny Goyal | 9dbb27c | 2019-07-17 15:12:56 -0700 | [diff] [blame] | 71 | import com.android.launcher3.util.PackageManagerHelper; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 72 | import com.android.launcher3.util.Thunk; |
Yogisha Dixit | 741fae9 | 2021-02-22 14:03:44 +0000 | [diff] [blame] | 73 | import com.android.launcher3.widget.LauncherAppWidgetHost; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 74 | |
Sunny Goyal | c0f03d9 | 2019-03-22 14:13:36 -0700 | [diff] [blame] | 75 | import org.xmlpull.v1.XmlPullParser; |
| 76 | |
Sunny Goyal | 05f3088 | 2017-05-03 12:42:18 -0700 | [diff] [blame] | 77 | import java.io.File; |
Hyunyoung Song | 6aa3729 | 2017-02-06 10:46:24 -0800 | [diff] [blame] | 78 | import java.io.FileDescriptor; |
Sunny Goyal | c0f03d9 | 2019-03-22 14:13:36 -0700 | [diff] [blame] | 79 | import java.io.InputStream; |
Hyunyoung Song | 6aa3729 | 2017-02-06 10:46:24 -0800 | [diff] [blame] | 80 | import java.io.PrintWriter; |
Sunny Goyal | c0f03d9 | 2019-03-22 14:13:36 -0700 | [diff] [blame] | 81 | import java.io.StringReader; |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 82 | import java.net.URISyntaxException; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 83 | import java.util.ArrayList; |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 84 | import java.util.Arrays; |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 85 | import java.util.Locale; |
Pinyao Ting | 0807c94 | 2020-04-28 13:58:45 -0700 | [diff] [blame] | 86 | import java.util.concurrent.TimeUnit; |
Tracy Zhou | c000045 | 2020-03-17 18:28:38 -0700 | [diff] [blame] | 87 | import java.util.function.Supplier; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 88 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 89 | public class LauncherProvider extends ContentProvider { |
Sunny Goyal | c74e419 | 2015-09-08 14:01:03 -0700 | [diff] [blame] | 90 | private static final String TAG = "LauncherProvider"; |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 91 | private static final boolean LOGD = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 92 | |
Sunny Goyal | 05f3088 | 2017-05-03 12:42:18 -0700 | [diff] [blame] | 93 | private static final String DOWNGRADE_SCHEMA_FILE = "downgrade_schema.json"; |
Pinyao Ting | 0807c94 | 2020-04-28 13:58:45 -0700 | [diff] [blame] | 94 | private static final long RESTORE_BACKUP_TABLE_DELAY = TimeUnit.SECONDS.toMillis(30); |
Sunny Goyal | 05f3088 | 2017-05-03 12:42:18 -0700 | [diff] [blame] | 95 | |
Sunny Goyal | 3c7d55b | 2017-04-13 12:01:47 -0700 | [diff] [blame] | 96 | /** |
| 97 | * Represents the schema of the database. Changes in scheme need not be backwards compatible. |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 98 | * When increasing the scheme version, ensure that downgrade_schema.json is updated |
Sunny Goyal | 3c7d55b | 2017-04-13 12:01:47 -0700 | [diff] [blame] | 99 | */ |
Alex Chau | e09067c | 2022-01-10 16:13:13 +0000 | [diff] [blame] | 100 | public static final int SCHEMA_VERSION = 31; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 101 | |
Sunny Goyal | 4276e7b | 2018-11-26 23:44:41 -0800 | [diff] [blame] | 102 | public static final String AUTHORITY = BuildConfig.APPLICATION_ID + ".settings"; |
Pinyao Ting | 96186af | 2020-07-20 11:03:39 -0700 | [diff] [blame] | 103 | public static final String KEY_LAYOUT_PROVIDER_AUTHORITY = "KEY_LAYOUT_PROVIDER_AUTHORITY"; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 104 | |
Schneider Victor-tulias | fb252e7 | 2022-02-10 11:10:21 -0800 | [diff] [blame] | 105 | private static final int TEST_WORKSPACE_LAYOUT_RES_XML = R.xml.default_test_workspace; |
| 106 | |
Sunny Goyal | e87e6ab | 2014-11-21 22:42:53 -0800 | [diff] [blame] | 107 | static final String EMPTY_DATABASE_CREATED = "EMPTY_DATABASE_CREATED"; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 108 | |
Sunny Goyal | f076eae | 2016-01-11 12:25:10 -0800 | [diff] [blame] | 109 | protected DatabaseHelper mOpenHelper; |
Pinyao Ting | 96186af | 2020-07-20 11:03:39 -0700 | [diff] [blame] | 110 | protected String mProviderAuthority; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 111 | |
Pinyao Ting | 0807c94 | 2020-04-28 13:58:45 -0700 | [diff] [blame] | 112 | private long mLastRestoreTimestamp = 0L; |
| 113 | |
Schneider Victor-tulias | fb252e7 | 2022-02-10 11:10:21 -0800 | [diff] [blame] | 114 | private boolean mUseTestWorkspaceLayout; |
| 115 | |
Hyunyoung Song | 6aa3729 | 2017-02-06 10:46:24 -0800 | [diff] [blame] | 116 | /** |
| 117 | * $ adb shell dumpsys activity provider com.android.launcher3 |
| 118 | */ |
| 119 | @Override |
| 120 | public void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
| 121 | LauncherAppState appState = LauncherAppState.getInstanceNoCreate(); |
| 122 | if (appState == null || !appState.getModel().isModelLoaded()) { |
| 123 | return; |
| 124 | } |
| 125 | appState.getModel().dumpState("", fd, writer, args); |
| 126 | } |
| 127 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 128 | @Override |
| 129 | public boolean onCreate() { |
Zak Cohen | 3eeb41d | 2020-02-14 14:15:13 -0800 | [diff] [blame] | 130 | if (FeatureFlags.IS_STUDIO_BUILD) { |
Sunny Goyal | e26d100 | 2016-06-20 14:52:14 -0700 | [diff] [blame] | 131 | Log.d(TAG, "Launcher process started"); |
| 132 | } |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 133 | |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 134 | // The content provider exists for the entire duration of the launcher main process and |
Sunny Goyal | 66f2b35 | 2018-02-09 10:57:12 -0800 | [diff] [blame] | 135 | // is the first component to get created. |
| 136 | MainProcessInitializer.initialize(getContext().getApplicationContext()); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 137 | return true; |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public String getType(Uri uri) { |
| 142 | SqlArguments args = new SqlArguments(uri, null, null); |
| 143 | if (TextUtils.isEmpty(args.where)) { |
| 144 | return "vnd.android.cursor.dir/" + args.table; |
| 145 | } else { |
| 146 | return "vnd.android.cursor.item/" + args.table; |
| 147 | } |
| 148 | } |
| 149 | |
Sunny Goyal | f076eae | 2016-01-11 12:25:10 -0800 | [diff] [blame] | 150 | /** |
| 151 | * Overridden in tests |
| 152 | */ |
| 153 | protected synchronized void createDbIfNotExists() { |
Sunny Goyal | d3849d1 | 2015-10-29 10:28:32 -0700 | [diff] [blame] | 154 | if (mOpenHelper == null) { |
Tracy Zhou | ed5f308 | 2020-04-20 01:13:26 -0700 | [diff] [blame] | 155 | mOpenHelper = DatabaseHelper.createDatabaseHelper( |
| 156 | getContext(), false /* forMigration */); |
Sunny Goyal | e8f7d5a | 2016-05-24 11:30:14 -0700 | [diff] [blame] | 157 | |
Sunny Goyal | 68031ca | 2021-08-02 12:23:44 -0700 | [diff] [blame] | 158 | RestoreDbTask.restoreIfNeeded(getContext(), mOpenHelper); |
Sunny Goyal | d3849d1 | 2015-10-29 10:28:32 -0700 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
Tracy Zhou | c000045 | 2020-03-17 18:28:38 -0700 | [diff] [blame] | 162 | private synchronized boolean prepForMigration(String dbFile, String targetTableName, |
| 163 | Supplier<DatabaseHelper> src, Supplier<DatabaseHelper> dst) { |
| 164 | if (TextUtils.equals(dbFile, mOpenHelper.getDatabaseName())) { |
Alex Chau | 0adc366 | 2022-07-04 13:05:38 +0100 | [diff] [blame^] | 165 | Log.e(TAG, "prepForMigration - target db is same as current: " + dbFile); |
Tracy Zhou | 7df93d2 | 2020-01-27 13:44:06 -0800 | [diff] [blame] | 166 | return false; |
| 167 | } |
| 168 | |
Tracy Zhou | c000045 | 2020-03-17 18:28:38 -0700 | [diff] [blame] | 169 | final DatabaseHelper helper = src.get(); |
| 170 | mOpenHelper = dst.get(); |
| 171 | copyTable(helper.getReadableDatabase(), Favorites.TABLE_NAME, |
| 172 | mOpenHelper.getWritableDatabase(), targetTableName, getContext()); |
| 173 | helper.close(); |
Tracy Zhou | 7df93d2 | 2020-01-27 13:44:06 -0800 | [diff] [blame] | 174 | return true; |
| 175 | } |
| 176 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 177 | @Override |
| 178 | public Cursor query(Uri uri, String[] projection, String selection, |
| 179 | String[] selectionArgs, String sortOrder) { |
Sunny Goyal | d3849d1 | 2015-10-29 10:28:32 -0700 | [diff] [blame] | 180 | createDbIfNotExists(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 181 | |
| 182 | SqlArguments args = new SqlArguments(uri, selection, selectionArgs); |
| 183 | SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); |
| 184 | qb.setTables(args.table); |
| 185 | |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 186 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 187 | Cursor result = qb.query(db, projection, args.where, args.args, null, null, sortOrder); |
Pinyao Ting | 0960b45 | 2020-10-14 11:17:04 -0700 | [diff] [blame] | 188 | final Bundle extra = new Bundle(); |
| 189 | extra.putString(LauncherSettings.Settings.EXTRA_DB_NAME, mOpenHelper.getDatabaseName()); |
| 190 | result.setExtras(extra); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 191 | result.setNotificationUri(getContext().getContentResolver(), uri); |
| 192 | |
| 193 | return result; |
| 194 | } |
| 195 | |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 196 | @Thunk static int dbInsertAndCheck(DatabaseHelper helper, |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 197 | SQLiteDatabase db, String table, String nullColumnHack, ContentValues values) { |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 198 | if (values == null) { |
| 199 | throw new RuntimeException("Error: attempting to insert null values"); |
| 200 | } |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 201 | if (!values.containsKey(LauncherSettings.Favorites._ID)) { |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 202 | throw new RuntimeException("Error: attempting to add item without specifying an id"); |
| 203 | } |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 204 | helper.checkId(values); |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 205 | return (int) db.insert(table, nullColumnHack, values); |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Sunny Goyal | d106418 | 2015-08-13 12:08:30 -0700 | [diff] [blame] | 208 | private void reloadLauncherIfExternal() { |
Sunny Goyal | 8c48d8b | 2019-01-25 15:10:18 -0800 | [diff] [blame] | 209 | if (Binder.getCallingPid() != Process.myPid()) { |
Sunny Goyal | d106418 | 2015-08-13 12:08:30 -0700 | [diff] [blame] | 210 | LauncherAppState app = LauncherAppState.getInstanceNoCreate(); |
| 211 | if (app != null) { |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 212 | app.getModel().forceReload(); |
Sunny Goyal | d106418 | 2015-08-13 12:08:30 -0700 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 217 | @Override |
| 218 | public Uri insert(Uri uri, ContentValues initialValues) { |
Sunny Goyal | d3849d1 | 2015-10-29 10:28:32 -0700 | [diff] [blame] | 219 | createDbIfNotExists(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 220 | SqlArguments args = new SqlArguments(uri); |
| 221 | |
Sunny Goyal | d106418 | 2015-08-13 12:08:30 -0700 | [diff] [blame] | 222 | // In very limited cases, we support system|signature permission apps to modify the db. |
| 223 | if (Binder.getCallingPid() != Process.myPid()) { |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 224 | if (!initializeExternalAdd(initialValues)) { |
Adam Cohen | a043fa8 | 2014-07-23 14:49:38 -0700 | [diff] [blame] | 225 | return null; |
| 226 | } |
| 227 | } |
| 228 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 229 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 230 | addModifiedTime(initialValues); |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 231 | final int rowId = dbInsertAndCheck(mOpenHelper, db, args.table, null, initialValues); |
Sunny Goyal | e72f3d5 | 2015-03-02 14:24:21 -0800 | [diff] [blame] | 232 | if (rowId < 0) return null; |
Tracy Zhou | 7df93d2 | 2020-01-27 13:44:06 -0800 | [diff] [blame] | 233 | onAddOrDeleteOp(db); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 234 | |
| 235 | uri = ContentUris.withAppendedId(uri, rowId); |
Sunny Goyal | 8c48d8b | 2019-01-25 15:10:18 -0800 | [diff] [blame] | 236 | reloadLauncherIfExternal(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 237 | return uri; |
| 238 | } |
| 239 | |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 240 | private boolean initializeExternalAdd(ContentValues values) { |
| 241 | // 1. Ensure that externally added items have a valid item id |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 242 | int id = mOpenHelper.generateNewItemId(); |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 243 | values.put(LauncherSettings.Favorites._ID, id); |
| 244 | |
| 245 | // 2. In the case of an app widget, and if no app widget id is specified, we |
| 246 | // attempt allocate and bind the widget. |
| 247 | Integer itemType = values.getAsInteger(LauncherSettings.Favorites.ITEM_TYPE); |
| 248 | if (itemType != null && |
| 249 | itemType.intValue() == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET && |
| 250 | !values.containsKey(LauncherSettings.Favorites.APPWIDGET_ID)) { |
| 251 | |
| 252 | final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getContext()); |
| 253 | ComponentName cn = ComponentName.unflattenFromString( |
| 254 | values.getAsString(Favorites.APPWIDGET_PROVIDER)); |
| 255 | |
| 256 | if (cn != null) { |
| 257 | try { |
Sunny Goyal | 55fddc8 | 2017-04-06 15:02:52 -0700 | [diff] [blame] | 258 | AppWidgetHost widgetHost = mOpenHelper.newLauncherWidgetHost(); |
| 259 | int appWidgetId = widgetHost.allocateAppWidgetId(); |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 260 | values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId); |
| 261 | if (!appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,cn)) { |
Sunny Goyal | 55fddc8 | 2017-04-06 15:02:52 -0700 | [diff] [blame] | 262 | widgetHost.deleteAppWidgetId(appWidgetId); |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 263 | return false; |
| 264 | } |
| 265 | } catch (RuntimeException e) { |
| 266 | Log.e(TAG, "Failed to initialize external widget", e); |
| 267 | return false; |
| 268 | } |
| 269 | } else { |
| 270 | return false; |
| 271 | } |
| 272 | } |
| 273 | |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 274 | return true; |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 275 | } |
| 276 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 277 | @Override |
| 278 | public int bulkInsert(Uri uri, ContentValues[] values) { |
Sunny Goyal | d3849d1 | 2015-10-29 10:28:32 -0700 | [diff] [blame] | 279 | createDbIfNotExists(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 280 | SqlArguments args = new SqlArguments(uri); |
| 281 | |
| 282 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
Sunny Goyal | dbfc901 | 2017-04-17 16:58:36 -0700 | [diff] [blame] | 283 | try (SQLiteTransaction t = new SQLiteTransaction(db)) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 284 | int numValues = values.length; |
| 285 | for (int i = 0; i < numValues; i++) { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 286 | addModifiedTime(values[i]); |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 287 | if (dbInsertAndCheck(mOpenHelper, db, args.table, null, values[i]) < 0) { |
| 288 | return 0; |
| 289 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 290 | } |
Tracy Zhou | 7df93d2 | 2020-01-27 13:44:06 -0800 | [diff] [blame] | 291 | onAddOrDeleteOp(db); |
Sunny Goyal | dbfc901 | 2017-04-17 16:58:36 -0700 | [diff] [blame] | 292 | t.commit(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 293 | } |
| 294 | |
Sunny Goyal | d106418 | 2015-08-13 12:08:30 -0700 | [diff] [blame] | 295 | reloadLauncherIfExternal(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 296 | return values.length; |
| 297 | } |
| 298 | |
Sunny Goyal | 161a214 | 2018-10-29 14:02:20 -0700 | [diff] [blame] | 299 | @TargetApi(Build.VERSION_CODES.M) |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 300 | @Override |
Yura | 085c853 | 2014-02-11 15:15:29 +0000 | [diff] [blame] | 301 | public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) |
| 302 | throws OperationApplicationException { |
Sunny Goyal | d3849d1 | 2015-10-29 10:28:32 -0700 | [diff] [blame] | 303 | createDbIfNotExists(); |
Sunny Goyal | dbfc901 | 2017-04-17 16:58:36 -0700 | [diff] [blame] | 304 | try (SQLiteTransaction t = new SQLiteTransaction(mOpenHelper.getWritableDatabase())) { |
Sunny Goyal | 8c48d8b | 2019-01-25 15:10:18 -0800 | [diff] [blame] | 305 | boolean isAddOrDelete = false; |
Sunny Goyal | 161a214 | 2018-10-29 14:02:20 -0700 | [diff] [blame] | 306 | |
| 307 | final int numOperations = operations.size(); |
| 308 | final ContentProviderResult[] results = new ContentProviderResult[numOperations]; |
| 309 | for (int i = 0; i < numOperations; i++) { |
| 310 | ContentProviderOperation op = operations.get(i); |
| 311 | results[i] = op.apply(this, results, i); |
| 312 | |
| 313 | isAddOrDelete |= (op.isInsert() || op.isDelete()) && |
| 314 | results[i].count != null && results[i].count > 0; |
| 315 | } |
| 316 | if (isAddOrDelete) { |
Tracy Zhou | 7df93d2 | 2020-01-27 13:44:06 -0800 | [diff] [blame] | 317 | onAddOrDeleteOp(t.getDb()); |
Sunny Goyal | 161a214 | 2018-10-29 14:02:20 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Sunny Goyal | dbfc901 | 2017-04-17 16:58:36 -0700 | [diff] [blame] | 320 | t.commit(); |
Sunny Goyal | d106418 | 2015-08-13 12:08:30 -0700 | [diff] [blame] | 321 | reloadLauncherIfExternal(); |
Sunny Goyal | 161a214 | 2018-10-29 14:02:20 -0700 | [diff] [blame] | 322 | return results; |
Yura | 085c853 | 2014-02-11 15:15:29 +0000 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
| 326 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 327 | public int delete(Uri uri, String selection, String[] selectionArgs) { |
Sunny Goyal | d3849d1 | 2015-10-29 10:28:32 -0700 | [diff] [blame] | 328 | createDbIfNotExists(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 329 | SqlArguments args = new SqlArguments(uri, selection, selectionArgs); |
| 330 | |
| 331 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 332 | |
Louis Begin | a9c21c6 | 2016-08-15 15:18:41 -0700 | [diff] [blame] | 333 | if (Binder.getCallingPid() != Process.myPid() |
| 334 | && Favorites.TABLE_NAME.equalsIgnoreCase(args.table)) { |
Sunny Goyal | 55fddc8 | 2017-04-06 15:02:52 -0700 | [diff] [blame] | 335 | mOpenHelper.removeGhostWidgets(mOpenHelper.getWritableDatabase()); |
Louis Begin | a9c21c6 | 2016-08-15 15:18:41 -0700 | [diff] [blame] | 336 | } |
| 337 | int count = db.delete(args.table, args.where, args.args); |
| 338 | if (count > 0) { |
Tracy Zhou | 7df93d2 | 2020-01-27 13:44:06 -0800 | [diff] [blame] | 339 | onAddOrDeleteOp(db); |
Louis Begin | a9c21c6 | 2016-08-15 15:18:41 -0700 | [diff] [blame] | 340 | reloadLauncherIfExternal(); |
| 341 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 342 | return count; |
| 343 | } |
| 344 | |
| 345 | @Override |
| 346 | public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { |
Sunny Goyal | d3849d1 | 2015-10-29 10:28:32 -0700 | [diff] [blame] | 347 | createDbIfNotExists(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 348 | SqlArguments args = new SqlArguments(uri, selection, selectionArgs); |
| 349 | |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 350 | addModifiedTime(values); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 351 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 352 | int count = db.update(args.table, values, args.where, args.args); |
Sunny Goyal | d106418 | 2015-08-13 12:08:30 -0700 | [diff] [blame] | 353 | reloadLauncherIfExternal(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 354 | return count; |
| 355 | } |
| 356 | |
Sunny Goyal | 7779d62 | 2015-06-11 16:18:39 -0700 | [diff] [blame] | 357 | @Override |
Tony Wickham | 827cef2 | 2016-03-17 15:39:39 -0700 | [diff] [blame] | 358 | public Bundle call(String method, final String arg, final Bundle extras) { |
Sunny Goyal | 7779d62 | 2015-06-11 16:18:39 -0700 | [diff] [blame] | 359 | if (Binder.getCallingUid() != Process.myUid()) { |
| 360 | return null; |
| 361 | } |
Sunny Goyal | d3849d1 | 2015-10-29 10:28:32 -0700 | [diff] [blame] | 362 | createDbIfNotExists(); |
Sunny Goyal | 7779d62 | 2015-06-11 16:18:39 -0700 | [diff] [blame] | 363 | |
| 364 | switch (method) { |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 365 | case LauncherSettings.Settings.METHOD_CLEAR_EMPTY_DB_FLAG: { |
| 366 | clearFlagEmptyDbCreated(); |
| 367 | return null; |
| 368 | } |
Sunny Goyal | a5c8a9e | 2016-07-08 08:32:44 -0700 | [diff] [blame] | 369 | case LauncherSettings.Settings.METHOD_WAS_EMPTY_DB_CREATED : { |
| 370 | Bundle result = new Bundle(); |
| 371 | result.putBoolean(LauncherSettings.Settings.EXTRA_VALUE, |
Pinyao Ting | 96186af | 2020-07-20 11:03:39 -0700 | [diff] [blame] | 372 | Utilities.getPrefs(getContext()).getBoolean( |
| 373 | mOpenHelper.getKey(EMPTY_DATABASE_CREATED), false)); |
Sunny Goyal | a5c8a9e | 2016-07-08 08:32:44 -0700 | [diff] [blame] | 374 | return result; |
| 375 | } |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 376 | case LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS: { |
| 377 | Bundle result = new Bundle(); |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 378 | result.putIntArray(LauncherSettings.Settings.EXTRA_VALUE, deleteEmptyFolders() |
| 379 | .toArray()); |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 380 | return result; |
| 381 | } |
| 382 | case LauncherSettings.Settings.METHOD_NEW_ITEM_ID: { |
| 383 | Bundle result = new Bundle(); |
Tracy Zhou | 7df93d2 | 2020-01-27 13:44:06 -0800 | [diff] [blame] | 384 | result.putInt(LauncherSettings.Settings.EXTRA_VALUE, |
| 385 | mOpenHelper.generateNewItemId()); |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 386 | return result; |
| 387 | } |
| 388 | case LauncherSettings.Settings.METHOD_NEW_SCREEN_ID: { |
| 389 | Bundle result = new Bundle(); |
Tracy Zhou | 7df93d2 | 2020-01-27 13:44:06 -0800 | [diff] [blame] | 390 | result.putInt(LauncherSettings.Settings.EXTRA_VALUE, |
Andras Kloczl | 953eb80 | 2021-10-15 21:16:48 +0100 | [diff] [blame] | 391 | mOpenHelper.getNewScreenId()); |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 392 | return result; |
| 393 | } |
| 394 | case LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB: { |
Sunny Goyal | e05b08f | 2017-02-23 18:30:22 -0800 | [diff] [blame] | 395 | mOpenHelper.createEmptyDB(mOpenHelper.getWritableDatabase()); |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 396 | return null; |
| 397 | } |
Schneider Victor-tulias | fb252e7 | 2022-02-10 11:10:21 -0800 | [diff] [blame] | 398 | case LauncherSettings.Settings.METHOD_SET_USE_TEST_WORKSPACE_LAYOUT_FLAG: { |
| 399 | mUseTestWorkspaceLayout = true; |
| 400 | return null; |
| 401 | } |
| 402 | case LauncherSettings.Settings.METHOD_CLEAR_USE_TEST_WORKSPACE_LAYOUT_FLAG: { |
| 403 | mUseTestWorkspaceLayout = false; |
| 404 | return null; |
| 405 | } |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 406 | case LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES: { |
| 407 | loadDefaultFavoritesIfNecessary(); |
| 408 | return null; |
| 409 | } |
Sunny Goyal | 55fddc8 | 2017-04-06 15:02:52 -0700 | [diff] [blame] | 410 | case LauncherSettings.Settings.METHOD_REMOVE_GHOST_WIDGETS: { |
| 411 | mOpenHelper.removeGhostWidgets(mOpenHelper.getWritableDatabase()); |
| 412 | return null; |
| 413 | } |
Sunny Goyal | 161a214 | 2018-10-29 14:02:20 -0700 | [diff] [blame] | 414 | case LauncherSettings.Settings.METHOD_NEW_TRANSACTION: { |
| 415 | Bundle result = new Bundle(); |
| 416 | result.putBinder(LauncherSettings.Settings.EXTRA_VALUE, |
| 417 | new SQLiteTransaction(mOpenHelper.getWritableDatabase())); |
| 418 | return result; |
| 419 | } |
| 420 | case LauncherSettings.Settings.METHOD_REFRESH_BACKUP_TABLE: { |
Pinyao Ting | 26c4e23 | 2020-04-15 22:25:06 -0700 | [diff] [blame] | 421 | mOpenHelper.mBackupTableExists = tableExists(mOpenHelper.getReadableDatabase(), |
| 422 | Favorites.BACKUP_TABLE_NAME); |
Sunny Goyal | 161a214 | 2018-10-29 14:02:20 -0700 | [diff] [blame] | 423 | return null; |
| 424 | } |
Samuel Fufa | f667a13 | 2020-05-29 14:47:42 -0700 | [diff] [blame] | 425 | case LauncherSettings.Settings.METHOD_REFRESH_HOTSEAT_RESTORE_TABLE: { |
| 426 | mOpenHelper.mHotseatRestoreTableExists = tableExists( |
| 427 | mOpenHelper.getReadableDatabase(), Favorites.HYBRID_HOTSEAT_BACKUP_TABLE); |
| 428 | return null; |
| 429 | } |
Pinyao Ting | ba9c557 | 2019-09-24 14:25:46 -0700 | [diff] [blame] | 430 | case LauncherSettings.Settings.METHOD_RESTORE_BACKUP_TABLE: { |
Pinyao Ting | 0807c94 | 2020-04-28 13:58:45 -0700 | [diff] [blame] | 431 | final long ts = System.currentTimeMillis(); |
| 432 | if (ts - mLastRestoreTimestamp > RESTORE_BACKUP_TABLE_DELAY) { |
| 433 | mLastRestoreTimestamp = ts; |
| 434 | RestoreDbTask.restoreIfPossible( |
| 435 | getContext(), mOpenHelper, new BackupManager(getContext())); |
| 436 | } |
Pinyao Ting | ba9c557 | 2019-09-24 14:25:46 -0700 | [diff] [blame] | 437 | return null; |
| 438 | } |
Tracy Zhou | 7df93d2 | 2020-01-27 13:44:06 -0800 | [diff] [blame] | 439 | case LauncherSettings.Settings.METHOD_UPDATE_CURRENT_OPEN_HELPER: { |
Sunny Goyal | 278939f | 2021-07-28 14:35:47 -0700 | [diff] [blame] | 440 | Bundle result = new Bundle(); |
| 441 | result.putBoolean(LauncherSettings.Settings.EXTRA_VALUE, |
| 442 | prepForMigration( |
Alex Chau | fe9a07b | 2022-03-08 21:48:34 +0000 | [diff] [blame] | 443 | arg /* dbFile */, |
Sunny Goyal | 278939f | 2021-07-28 14:35:47 -0700 | [diff] [blame] | 444 | Favorites.TMP_TABLE, |
| 445 | () -> mOpenHelper, |
| 446 | () -> DatabaseHelper.createDatabaseHelper( |
| 447 | getContext(), true /* forMigration */))); |
| 448 | return result; |
Tracy Zhou | c000045 | 2020-03-17 18:28:38 -0700 | [diff] [blame] | 449 | } |
| 450 | case LauncherSettings.Settings.METHOD_PREP_FOR_PREVIEW: { |
Sunny Goyal | 278939f | 2021-07-28 14:35:47 -0700 | [diff] [blame] | 451 | Bundle result = new Bundle(); |
| 452 | result.putBoolean(LauncherSettings.Settings.EXTRA_VALUE, |
| 453 | prepForMigration( |
| 454 | arg /* dbFile */, |
| 455 | Favorites.PREVIEW_TABLE_NAME, |
| 456 | () -> DatabaseHelper.createDatabaseHelper( |
| 457 | getContext(), arg, true /* forMigration */), |
| 458 | () -> mOpenHelper)); |
| 459 | return result; |
Pinyao Ting | 96186af | 2020-07-20 11:03:39 -0700 | [diff] [blame] | 460 | } |
| 461 | case LauncherSettings.Settings.METHOD_SWITCH_DATABASE: { |
| 462 | if (TextUtils.equals(arg, mOpenHelper.getDatabaseName())) return null; |
| 463 | final DatabaseHelper helper = mOpenHelper; |
| 464 | if (extras == null || !extras.containsKey(KEY_LAYOUT_PROVIDER_AUTHORITY)) { |
| 465 | mProviderAuthority = null; |
| 466 | } else { |
| 467 | mProviderAuthority = extras.getString(KEY_LAYOUT_PROVIDER_AUTHORITY); |
| 468 | } |
| 469 | mOpenHelper = DatabaseHelper.createDatabaseHelper( |
| 470 | getContext(), arg, false /* forMigration */); |
| 471 | helper.close(); |
| 472 | LauncherAppState app = LauncherAppState.getInstanceNoCreate(); |
| 473 | if (app == null) return null; |
| 474 | app.getModel().forceReload(); |
| 475 | return null; |
Tracy Zhou | 7df93d2 | 2020-01-27 13:44:06 -0800 | [diff] [blame] | 476 | } |
Sunny Goyal | 7779d62 | 2015-06-11 16:18:39 -0700 | [diff] [blame] | 477 | } |
| 478 | return null; |
| 479 | } |
| 480 | |
Tracy Zhou | 7df93d2 | 2020-01-27 13:44:06 -0800 | [diff] [blame] | 481 | private void onAddOrDeleteOp(SQLiteDatabase db) { |
Pinyao Ting | 26c4e23 | 2020-04-15 22:25:06 -0700 | [diff] [blame] | 482 | mOpenHelper.onAddOrDeleteOp(db); |
Tracy Zhou | 7df93d2 | 2020-01-27 13:44:06 -0800 | [diff] [blame] | 483 | } |
| 484 | |
Sunny Goyal | b1622cc | 2015-06-10 16:00:42 -0700 | [diff] [blame] | 485 | /** |
| 486 | * Deletes any empty folder from the DB. |
| 487 | * @return Ids of deleted folders. |
| 488 | */ |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 489 | private IntArray deleteEmptyFolders() { |
Sunny Goyal | b1622cc | 2015-06-10 16:00:42 -0700 | [diff] [blame] | 490 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
Sunny Goyal | dbfc901 | 2017-04-17 16:58:36 -0700 | [diff] [blame] | 491 | try (SQLiteTransaction t = new SQLiteTransaction(db)) { |
Sunny Goyal | b1622cc | 2015-06-10 16:00:42 -0700 | [diff] [blame] | 492 | // Select folders whose id do not match any container value. |
| 493 | String selection = LauncherSettings.Favorites.ITEM_TYPE + " = " |
| 494 | + LauncherSettings.Favorites.ITEM_TYPE_FOLDER + " AND " |
| 495 | + LauncherSettings.Favorites._ID + " NOT IN (SELECT " + |
| 496 | LauncherSettings.Favorites.CONTAINER + " FROM " |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 497 | + Favorites.TABLE_NAME + ")"; |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 498 | |
Alex Chau | ac03843 | 2021-11-04 16:49:59 +0000 | [diff] [blame] | 499 | IntArray folderIds = LauncherDbUtils.queryIntArray(false, db, Favorites.TABLE_NAME, |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 500 | Favorites._ID, selection, null, null); |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 501 | if (!folderIds.isEmpty()) { |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 502 | db.delete(Favorites.TABLE_NAME, Utilities.createDbSelectionQuery( |
Sunny Goyal | b1622cc | 2015-06-10 16:00:42 -0700 | [diff] [blame] | 503 | LauncherSettings.Favorites._ID, folderIds), null); |
| 504 | } |
Sunny Goyal | dbfc901 | 2017-04-17 16:58:36 -0700 | [diff] [blame] | 505 | t.commit(); |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 506 | return folderIds; |
Sunny Goyal | b1622cc | 2015-06-10 16:00:42 -0700 | [diff] [blame] | 507 | } catch (SQLException ex) { |
| 508 | Log.e(TAG, ex.getMessage(), ex); |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 509 | return new IntArray(); |
Sunny Goyal | b1622cc | 2015-06-10 16:00:42 -0700 | [diff] [blame] | 510 | } |
Sunny Goyal | b1622cc | 2015-06-10 16:00:42 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 513 | @Thunk static void addModifiedTime(ContentValues values) { |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 514 | values.put(LauncherSettings.Favorites.MODIFIED, System.currentTimeMillis()); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 515 | } |
| 516 | |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 517 | private void clearFlagEmptyDbCreated() { |
Pinyao Ting | 96186af | 2020-07-20 11:03:39 -0700 | [diff] [blame] | 518 | Utilities.getPrefs(getContext()).edit() |
| 519 | .remove(mOpenHelper.getKey(EMPTY_DATABASE_CREATED)).commit(); |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Sunny Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 522 | /** |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 523 | * Loads the default workspace based on the following priority scheme: |
Sunny Goyal | b2d46ce | 2015-03-26 11:32:11 -0700 | [diff] [blame] | 524 | * 1) From the app restrictions |
| 525 | * 2) From a package provided by play store |
| 526 | * 3) From a partner configuration APK, already in the system image |
| 527 | * 4) The default configuration for the particular device |
Brian Muramatsu | 5524b49 | 2012-10-02 16:55:54 -0700 | [diff] [blame] | 528 | */ |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 529 | synchronized private void loadDefaultFavoritesIfNecessary() { |
Sunny Goyal | f725824 | 2015-10-19 16:59:07 -0700 | [diff] [blame] | 530 | SharedPreferences sp = Utilities.getPrefs(getContext()); |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 531 | |
Pinyao Ting | 96186af | 2020-07-20 11:03:39 -0700 | [diff] [blame] | 532 | if (sp.getBoolean(mOpenHelper.getKey(EMPTY_DATABASE_CREATED), false)) { |
Chris Wren | 5dee7af | 2013-12-20 17:22:11 -0500 | [diff] [blame] | 533 | Log.d(TAG, "loading default workspace"); |
Michael Jurka | 45355c4 | 2012-10-08 13:21:35 +0200 | [diff] [blame] | 534 | |
Sunny Goyal | 55fddc8 | 2017-04-06 15:02:52 -0700 | [diff] [blame] | 535 | AppWidgetHost widgetHost = mOpenHelper.newLauncherWidgetHost(); |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 536 | AutoInstallsLayout loader = createWorkspaceLoaderFromAppRestriction(widgetHost); |
Sunny Goyal | b2d46ce | 2015-03-26 11:32:11 -0700 | [diff] [blame] | 537 | if (loader == null) { |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 538 | loader = AutoInstallsLayout.get(getContext(),widgetHost, mOpenHelper); |
Sunny Goyal | b2d46ce | 2015-03-26 11:32:11 -0700 | [diff] [blame] | 539 | } |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 540 | if (loader == null) { |
Adam Cohen | 9b8f51f | 2014-05-30 15:34:09 -0700 | [diff] [blame] | 541 | final Partner partner = Partner.get(getContext().getPackageManager()); |
| 542 | if (partner != null && partner.hasDefaultLayout()) { |
| 543 | final Resources partnerRes = partner.getResources(); |
Adam Cohen | 4ae96ce | 2014-08-29 15:05:48 -0700 | [diff] [blame] | 544 | int workspaceResId = partnerRes.getIdentifier(Partner.RES_DEFAULT_LAYOUT, |
Adam Cohen | 9b8f51f | 2014-05-30 15:34:09 -0700 | [diff] [blame] | 545 | "xml", partner.getPackageName()); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 546 | if (workspaceResId != 0) { |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 547 | loader = new DefaultLayoutParser(getContext(), widgetHost, |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 548 | mOpenHelper, partnerRes, workspaceResId); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 549 | } |
Adam Cohen | 9b8f51f | 2014-05-30 15:34:09 -0700 | [diff] [blame] | 550 | } |
| 551 | } |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 552 | |
Sunny Goyal | 9d21968 | 2014-10-23 14:21:02 -0700 | [diff] [blame] | 553 | final boolean usingExternallyProvidedLayout = loader != null; |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 554 | if (loader == null) { |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 555 | loader = getDefaultLayoutParser(widgetHost); |
Brian Muramatsu | 5524b49 | 2012-10-02 16:55:54 -0700 | [diff] [blame] | 556 | } |
Sunny Goyal | c6c8fef | 2015-03-04 09:51:18 -0800 | [diff] [blame] | 557 | |
| 558 | // There might be some partially restored DB items, due to buggy restore logic in |
| 559 | // previous versions of launcher. |
Sunny Goyal | e05b08f | 2017-02-23 18:30:22 -0800 | [diff] [blame] | 560 | mOpenHelper.createEmptyDB(mOpenHelper.getWritableDatabase()); |
Michael Jurka | b85f8a4 | 2012-04-25 15:48:32 -0700 | [diff] [blame] | 561 | // Populate favorites table with initial favorites |
Sunny Goyal | 9d21968 | 2014-10-23 14:21:02 -0700 | [diff] [blame] | 562 | if ((mOpenHelper.loadFavorites(mOpenHelper.getWritableDatabase(), loader) <= 0) |
| 563 | && usingExternallyProvidedLayout) { |
| 564 | // Unable to load external layout. Cleanup and load the internal layout. |
Sunny Goyal | e05b08f | 2017-02-23 18:30:22 -0800 | [diff] [blame] | 565 | mOpenHelper.createEmptyDB(mOpenHelper.getWritableDatabase()); |
Sunny Goyal | 9d21968 | 2014-10-23 14:21:02 -0700 | [diff] [blame] | 566 | mOpenHelper.loadFavorites(mOpenHelper.getWritableDatabase(), |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 567 | getDefaultLayoutParser(widgetHost)); |
Sunny Goyal | 9d21968 | 2014-10-23 14:21:02 -0700 | [diff] [blame] | 568 | } |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 569 | clearFlagEmptyDbCreated(); |
Michael Jurka | b85f8a4 | 2012-04-25 15:48:32 -0700 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | |
Sunny Goyal | b2d46ce | 2015-03-26 11:32:11 -0700 | [diff] [blame] | 573 | /** |
| 574 | * Creates workspace loader from an XML resource listed in the app restrictions. |
| 575 | * |
| 576 | * @return the loader if the restrictions are set and the resource exists; null otherwise. |
| 577 | */ |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 578 | private AutoInstallsLayout createWorkspaceLoaderFromAppRestriction(AppWidgetHost widgetHost) { |
Sunny Goyal | b2d46ce | 2015-03-26 11:32:11 -0700 | [diff] [blame] | 579 | Context ctx = getContext(); |
Pinyao Ting | 96186af | 2020-07-20 11:03:39 -0700 | [diff] [blame] | 580 | final String authority; |
| 581 | if (!TextUtils.isEmpty(mProviderAuthority)) { |
| 582 | authority = mProviderAuthority; |
| 583 | } else { |
| 584 | authority = Settings.Secure.getString(ctx.getContentResolver(), |
| 585 | "launcher3.layout.provider"); |
| 586 | } |
Sunny Goyal | c0f03d9 | 2019-03-22 14:13:36 -0700 | [diff] [blame] | 587 | if (TextUtils.isEmpty(authority)) { |
Sunny Goyal | 35ca873 | 2015-04-06 10:45:31 -0700 | [diff] [blame] | 588 | return null; |
| 589 | } |
Sunny Goyal | b2d46ce | 2015-03-26 11:32:11 -0700 | [diff] [blame] | 590 | |
Sunny Goyal | c0f03d9 | 2019-03-22 14:13:36 -0700 | [diff] [blame] | 591 | ProviderInfo pi = ctx.getPackageManager().resolveContentProvider(authority, 0); |
| 592 | if (pi == null) { |
| 593 | Log.e(TAG, "No provider found for authority " + authority); |
| 594 | return null; |
Sunny Goyal | b2d46ce | 2015-03-26 11:32:11 -0700 | [diff] [blame] | 595 | } |
Sunny Goyal | 4b5b0eb | 2019-12-02 17:00:35 -0800 | [diff] [blame] | 596 | Uri uri = getLayoutUri(authority, ctx); |
Sunny Goyal | c0f03d9 | 2019-03-22 14:13:36 -0700 | [diff] [blame] | 597 | try (InputStream in = ctx.getContentResolver().openInputStream(uri)) { |
| 598 | // Read the full xml so that we fail early in case of any IO error. |
| 599 | String layout = new String(IOUtils.toByteArray(in)); |
| 600 | XmlPullParser parser = Xml.newPullParser(); |
| 601 | parser.setInput(new StringReader(layout)); |
| 602 | |
| 603 | Log.d(TAG, "Loading layout from " + authority); |
| 604 | return new AutoInstallsLayout(ctx, widgetHost, mOpenHelper, |
| 605 | ctx.getPackageManager().getResourcesForApplication(pi.applicationInfo), |
| 606 | () -> parser, AutoInstallsLayout.TAG_WORKSPACE); |
| 607 | } catch (Exception e) { |
| 608 | Log.e(TAG, "Error getting layout stream from: " + authority , e); |
| 609 | return null; |
| 610 | } |
Sunny Goyal | b2d46ce | 2015-03-26 11:32:11 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Sunny Goyal | 4b5b0eb | 2019-12-02 17:00:35 -0800 | [diff] [blame] | 613 | public static Uri getLayoutUri(String authority, Context ctx) { |
| 614 | InvariantDeviceProfile grid = LauncherAppState.getIDP(ctx); |
| 615 | return new Uri.Builder().scheme("content").authority(authority).path("launcher_layout") |
| 616 | .appendQueryParameter("version", "1") |
| 617 | .appendQueryParameter("gridWidth", Integer.toString(grid.numColumns)) |
| 618 | .appendQueryParameter("gridHeight", Integer.toString(grid.numRows)) |
Tony Wickham | b87f3cd | 2021-04-07 15:02:37 -0700 | [diff] [blame] | 619 | .appendQueryParameter("hotseatSize", Integer.toString(grid.numDatabaseHotseatIcons)) |
Sunny Goyal | 4b5b0eb | 2019-12-02 17:00:35 -0800 | [diff] [blame] | 620 | .build(); |
| 621 | } |
| 622 | |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 623 | private DefaultLayoutParser getDefaultLayoutParser(AppWidgetHost widgetHost) { |
Adam Cohen | 2782449 | 2017-09-22 17:10:55 -0700 | [diff] [blame] | 624 | InvariantDeviceProfile idp = LauncherAppState.getIDP(getContext()); |
Schneider Victor-tulias | fb252e7 | 2022-02-10 11:10:21 -0800 | [diff] [blame] | 625 | int defaultLayout = mUseTestWorkspaceLayout |
| 626 | ? TEST_WORKSPACE_LAYOUT_RES_XML : idp.defaultLayoutId; |
Adam Cohen | 2782449 | 2017-09-22 17:10:55 -0700 | [diff] [blame] | 627 | |
Sunny Goyal | 337c81f | 2019-12-10 12:19:13 -0800 | [diff] [blame] | 628 | if (getContext().getSystemService(UserManager.class).isDemoUser() |
| 629 | && idp.demoModeLayoutId != 0) { |
Adam Cohen | 2782449 | 2017-09-22 17:10:55 -0700 | [diff] [blame] | 630 | defaultLayout = idp.demoModeLayoutId; |
| 631 | } |
| 632 | |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 633 | return new DefaultLayoutParser(getContext(), widgetHost, |
Sunny Goyal | 9d21968 | 2014-10-23 14:21:02 -0700 | [diff] [blame] | 634 | mOpenHelper, getContext().getResources(), defaultLayout); |
| 635 | } |
| 636 | |
Sunny Goyal | d3849d1 | 2015-10-29 10:28:32 -0700 | [diff] [blame] | 637 | /** |
Sunny Goyal | f076eae | 2016-01-11 12:25:10 -0800 | [diff] [blame] | 638 | * The class is subclassed in tests to create an in-memory db. |
| 639 | */ |
Tracy Zhou | 7df93d2 | 2020-01-27 13:44:06 -0800 | [diff] [blame] | 640 | public static class DatabaseHelper extends NoLocaleSQLiteHelper implements |
| 641 | LayoutParserCallback { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 642 | private final Context mContext; |
Tracy Zhou | ed5f308 | 2020-04-20 01:13:26 -0700 | [diff] [blame] | 643 | private final boolean mForMigration; |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 644 | private int mMaxItemId = -1; |
Sunny Goyal | 161a214 | 2018-10-29 14:02:20 -0700 | [diff] [blame] | 645 | private boolean mBackupTableExists; |
Samuel Fufa | f667a13 | 2020-05-29 14:47:42 -0700 | [diff] [blame] | 646 | private boolean mHotseatRestoreTableExists; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 647 | |
Tracy Zhou | ed5f308 | 2020-04-20 01:13:26 -0700 | [diff] [blame] | 648 | static DatabaseHelper createDatabaseHelper(Context context, boolean forMigration) { |
| 649 | return createDatabaseHelper(context, null, forMigration); |
Tracy Zhou | c000045 | 2020-03-17 18:28:38 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Tracy Zhou | ed5f308 | 2020-04-20 01:13:26 -0700 | [diff] [blame] | 652 | static DatabaseHelper createDatabaseHelper(Context context, String dbName, |
| 653 | boolean forMigration) { |
Tracy Zhou | c000045 | 2020-03-17 18:28:38 -0700 | [diff] [blame] | 654 | if (dbName == null) { |
Sunny Goyal | 278939f | 2021-07-28 14:35:47 -0700 | [diff] [blame] | 655 | dbName = InvariantDeviceProfile.INSTANCE.get(context).dbFile; |
Tracy Zhou | c000045 | 2020-03-17 18:28:38 -0700 | [diff] [blame] | 656 | } |
Tracy Zhou | ed5f308 | 2020-04-20 01:13:26 -0700 | [diff] [blame] | 657 | DatabaseHelper databaseHelper = new DatabaseHelper(context, dbName, forMigration); |
Sunny Goyal | 6fb929e | 2015-09-23 11:40:53 -0700 | [diff] [blame] | 658 | // Table creation sometimes fails silently, which leads to a crash loop. |
| 659 | // This way, we will try to create a table every time after crash, so the device |
| 660 | // would eventually be able to recover. |
Tracy Zhou | c000045 | 2020-03-17 18:28:38 -0700 | [diff] [blame] | 661 | if (!tableExists(databaseHelper.getReadableDatabase(), Favorites.TABLE_NAME)) { |
Sunny Goyal | 6fb929e | 2015-09-23 11:40:53 -0700 | [diff] [blame] | 662 | Log.e(TAG, "Tables are missing after onCreate has been called. Trying to recreate"); |
| 663 | // This operation is a no-op if the table already exists. |
Tracy Zhou | c000045 | 2020-03-17 18:28:38 -0700 | [diff] [blame] | 664 | databaseHelper.addFavoritesTable(databaseHelper.getWritableDatabase(), true); |
Sunny Goyal | 6fb929e | 2015-09-23 11:40:53 -0700 | [diff] [blame] | 665 | } |
Samuel Fufa | f667a13 | 2020-05-29 14:47:42 -0700 | [diff] [blame] | 666 | databaseHelper.mHotseatRestoreTableExists = tableExists( |
| 667 | databaseHelper.getReadableDatabase(), Favorites.HYBRID_HOTSEAT_BACKUP_TABLE); |
Sunny Goyal | 6fb929e | 2015-09-23 11:40:53 -0700 | [diff] [blame] | 668 | |
Tracy Zhou | c000045 | 2020-03-17 18:28:38 -0700 | [diff] [blame] | 669 | databaseHelper.initIds(); |
| 670 | return databaseHelper; |
Sunny Goyal | 7eab3cc | 2016-03-18 17:42:55 -0700 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | /** |
Sunny Goyal | c190dbf | 2016-05-05 14:37:05 -0700 | [diff] [blame] | 674 | * Constructor used in tests and for restore. |
Sunny Goyal | 7eab3cc | 2016-03-18 17:42:55 -0700 | [diff] [blame] | 675 | */ |
Tracy Zhou | ed5f308 | 2020-04-20 01:13:26 -0700 | [diff] [blame] | 676 | public DatabaseHelper(Context context, String dbName, boolean forMigration) { |
Tracy Zhou | 7df93d2 | 2020-01-27 13:44:06 -0800 | [diff] [blame] | 677 | super(context, dbName, SCHEMA_VERSION); |
Sunny Goyal | 7eab3cc | 2016-03-18 17:42:55 -0700 | [diff] [blame] | 678 | mContext = context; |
Tracy Zhou | ed5f308 | 2020-04-20 01:13:26 -0700 | [diff] [blame] | 679 | mForMigration = forMigration; |
Sunny Goyal | 7eab3cc | 2016-03-18 17:42:55 -0700 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | protected void initIds() { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 683 | // In the case where neither onCreate nor onUpgrade gets called, we read the maxId from |
| 684 | // the DB here |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 685 | if (mMaxItemId == -1) { |
| 686 | mMaxItemId = initializeMaxItemId(getWritableDatabase()); |
| 687 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | @Override |
| 691 | public void onCreate(SQLiteDatabase db) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 692 | if (LOGD) Log.d(TAG, "creating new launcher database"); |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 693 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 694 | mMaxItemId = 1; |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 695 | |
Sunny Goyal | 6fb929e | 2015-09-23 11:40:53 -0700 | [diff] [blame] | 696 | addFavoritesTable(db, false); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 697 | |
Sunny Goyal | e87e6ab | 2014-11-21 22:42:53 -0800 | [diff] [blame] | 698 | // Fresh and clean launcher DB. |
| 699 | mMaxItemId = initializeMaxItemId(db); |
Tracy Zhou | ed5f308 | 2020-04-20 01:13:26 -0700 | [diff] [blame] | 700 | if (!mForMigration) { |
| 701 | onEmptyDbCreated(); |
| 702 | } |
Sunny Goyal | f076eae | 2016-01-11 12:25:10 -0800 | [diff] [blame] | 703 | } |
| 704 | |
Sunny Goyal | 161a214 | 2018-10-29 14:02:20 -0700 | [diff] [blame] | 705 | protected void onAddOrDeleteOp(SQLiteDatabase db) { |
Pinyao Ting | 26c4e23 | 2020-04-15 22:25:06 -0700 | [diff] [blame] | 706 | if (mBackupTableExists) { |
Sunny Goyal | 161a214 | 2018-10-29 14:02:20 -0700 | [diff] [blame] | 707 | dropTable(db, Favorites.BACKUP_TABLE_NAME); |
| 708 | mBackupTableExists = false; |
| 709 | } |
Samuel Fufa | f667a13 | 2020-05-29 14:47:42 -0700 | [diff] [blame] | 710 | if (mHotseatRestoreTableExists) { |
| 711 | dropTable(db, Favorites.HYBRID_HOTSEAT_BACKUP_TABLE); |
| 712 | mHotseatRestoreTableExists = false; |
| 713 | } |
Sunny Goyal | 161a214 | 2018-10-29 14:02:20 -0700 | [diff] [blame] | 714 | } |
| 715 | |
Sunny Goyal | f076eae | 2016-01-11 12:25:10 -0800 | [diff] [blame] | 716 | /** |
Pinyao Ting | 96186af | 2020-07-20 11:03:39 -0700 | [diff] [blame] | 717 | * Re-composite given key in respect to database. If the current db is |
| 718 | * {@link LauncherFiles#LAUNCHER_DB}, return the key as-is. Otherwise append the db name to |
| 719 | * given key. e.g. consider key="EMPTY_DATABASE_CREATED", dbName="minimal.db", the returning |
| 720 | * string will be "EMPTY_DATABASE_CREATED@minimal.db". |
| 721 | */ |
| 722 | String getKey(final String key) { |
| 723 | if (TextUtils.equals(getDatabaseName(), LauncherFiles.LAUNCHER_DB)) { |
| 724 | return key; |
| 725 | } |
| 726 | return key + "@" + getDatabaseName(); |
| 727 | } |
| 728 | |
| 729 | /** |
Sunny Goyal | f076eae | 2016-01-11 12:25:10 -0800 | [diff] [blame] | 730 | * Overriden in tests. |
| 731 | */ |
| 732 | protected void onEmptyDbCreated() { |
| 733 | // Set the flag for empty DB |
Pinyao Ting | 96186af | 2020-07-20 11:03:39 -0700 | [diff] [blame] | 734 | Utilities.getPrefs(mContext).edit().putBoolean(getKey(EMPTY_DATABASE_CREATED), true) |
| 735 | .commit(); |
Sunny Goyal | f076eae | 2016-01-11 12:25:10 -0800 | [diff] [blame] | 736 | } |
| 737 | |
Jon Miranda | 2bc6b93 | 2019-03-07 15:24:46 -0800 | [diff] [blame] | 738 | public long getSerialNumberForUser(UserHandle user) { |
Sunny Goyal | 337c81f | 2019-12-10 12:19:13 -0800 | [diff] [blame] | 739 | return UserCache.INSTANCE.get(mContext).getSerialNumberForUser(user); |
Jon Miranda | 2bc6b93 | 2019-03-07 15:24:46 -0800 | [diff] [blame] | 740 | } |
| 741 | |
Sunny Goyal | e8f7d5a | 2016-05-24 11:30:14 -0700 | [diff] [blame] | 742 | public long getDefaultUserSerial() { |
Jon Miranda | 2bc6b93 | 2019-03-07 15:24:46 -0800 | [diff] [blame] | 743 | return getSerialNumberForUser(Process.myUserHandle()); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 744 | } |
| 745 | |
Sunny Goyal | 6fb929e | 2015-09-23 11:40:53 -0700 | [diff] [blame] | 746 | private void addFavoritesTable(SQLiteDatabase db, boolean optional) { |
Sunny Goyal | c190dbf | 2016-05-05 14:37:05 -0700 | [diff] [blame] | 747 | Favorites.addTableToDb(db, getDefaultUserSerial(), optional); |
Sunny Goyal | 6fb929e | 2015-09-23 11:40:53 -0700 | [diff] [blame] | 748 | } |
| 749 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 750 | @Override |
Sunny Goyal | 3c7d55b | 2017-04-13 12:01:47 -0700 | [diff] [blame] | 751 | public void onOpen(SQLiteDatabase db) { |
| 752 | super.onOpen(db); |
Sunny Goyal | 05f3088 | 2017-05-03 12:42:18 -0700 | [diff] [blame] | 753 | |
| 754 | File schemaFile = mContext.getFileStreamPath(DOWNGRADE_SCHEMA_FILE); |
| 755 | if (!schemaFile.exists()) { |
| 756 | handleOneTimeDataUpgrade(db); |
Sunny Goyal | 3c7d55b | 2017-04-13 12:01:47 -0700 | [diff] [blame] | 757 | } |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 758 | DbDowngradeHelper.updateSchemaFile(schemaFile, SCHEMA_VERSION, mContext); |
Sunny Goyal | 3c7d55b | 2017-04-13 12:01:47 -0700 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | /** |
Sunny Goyal | 05f3088 | 2017-05-03 12:42:18 -0700 | [diff] [blame] | 762 | * One-time data updated before support of onDowngrade was added. This update is backwards |
| 763 | * compatible and can safely be run multiple times. |
| 764 | * Note: No new logic should be added here after release, as the new logic might not get |
| 765 | * executed on an existing device. |
| 766 | * TODO: Move this to db upgrade path, once the downgrade path is released. |
Sunny Goyal | 3c7d55b | 2017-04-13 12:01:47 -0700 | [diff] [blame] | 767 | */ |
Sunny Goyal | 05f3088 | 2017-05-03 12:42:18 -0700 | [diff] [blame] | 768 | protected void handleOneTimeDataUpgrade(SQLiteDatabase db) { |
| 769 | // Remove "profile extra" |
Sunny Goyal | 337c81f | 2019-12-10 12:19:13 -0800 | [diff] [blame] | 770 | UserCache um = UserCache.INSTANCE.get(mContext); |
Sunny Goyal | 05f3088 | 2017-05-03 12:42:18 -0700 | [diff] [blame] | 771 | for (UserHandle user : um.getUserProfiles()) { |
| 772 | long serial = um.getSerialNumberForUser(user); |
| 773 | String sql = "update favorites set intent = replace(intent, " |
| 774 | + "';l.profile=" + serial + ";', ';') where itemType = 0;"; |
| 775 | db.execSQL(sql); |
Sunny Goyal | 3c7d55b | 2017-04-13 12:01:47 -0700 | [diff] [blame] | 776 | } |
| 777 | } |
| 778 | |
| 779 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 780 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 781 | if (LOGD) Log.d(TAG, "onUpgrade triggered: " + oldVersion); |
Sunny Goyal | a2cc624 | 2015-01-14 14:23:02 -0800 | [diff] [blame] | 782 | switch (oldVersion) { |
| 783 | // The version cannot be lower that 12, as Launcher3 never supported a lower |
Sunny Goyal | e87e6ab | 2014-11-21 22:42:53 -0800 | [diff] [blame] | 784 | // version of the DB. |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 785 | case 12: |
| 786 | // No-op |
Sunny Goyal | a2cc624 | 2015-01-14 14:23:02 -0800 | [diff] [blame] | 787 | case 13: { |
Sunny Goyal | dbfc901 | 2017-04-17 16:58:36 -0700 | [diff] [blame] | 788 | try (SQLiteTransaction t = new SQLiteTransaction(db)) { |
Sunny Goyal | a2cc624 | 2015-01-14 14:23:02 -0800 | [diff] [blame] | 789 | // Insert new column for holding widget provider name |
| 790 | db.execSQL("ALTER TABLE favorites " + |
| 791 | "ADD COLUMN appWidgetProvider TEXT;"); |
Sunny Goyal | dbfc901 | 2017-04-17 16:58:36 -0700 | [diff] [blame] | 792 | t.commit(); |
Sunny Goyal | a2cc624 | 2015-01-14 14:23:02 -0800 | [diff] [blame] | 793 | } catch (SQLException ex) { |
| 794 | Log.e(TAG, ex.getMessage(), ex); |
| 795 | // Old version remains, which means we wipe old data |
| 796 | break; |
Sunny Goyal | a2cc624 | 2015-01-14 14:23:02 -0800 | [diff] [blame] | 797 | } |
| 798 | } |
| 799 | case 14: { |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 800 | if (!addIntegerColumn(db, Favorites.MODIFIED, 0)) { |
Sunny Goyal | a2cc624 | 2015-01-14 14:23:02 -0800 | [diff] [blame] | 801 | // Old version remains, which means we wipe old data |
| 802 | break; |
Sunny Goyal | a2cc624 | 2015-01-14 14:23:02 -0800 | [diff] [blame] | 803 | } |
| 804 | } |
| 805 | case 15: { |
Sunny Goyal | 5d85c44 | 2015-03-10 13:14:47 -0700 | [diff] [blame] | 806 | if (!addIntegerColumn(db, Favorites.RESTORED, 0)) { |
Sunny Goyal | a2cc624 | 2015-01-14 14:23:02 -0800 | [diff] [blame] | 807 | // Old version remains, which means we wipe old data |
| 808 | break; |
Sunny Goyal | a2cc624 | 2015-01-14 14:23:02 -0800 | [diff] [blame] | 809 | } |
| 810 | } |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 811 | case 16: |
Sunny Goyal | 10629b0 | 2016-09-01 12:50:11 -0700 | [diff] [blame] | 812 | // No-op |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 813 | case 17: |
Sunny Goyal | a2cc624 | 2015-01-14 14:23:02 -0800 | [diff] [blame] | 814 | // No-op |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 815 | case 18: |
| 816 | // No-op |
Sunny Goyal | a2cc624 | 2015-01-14 14:23:02 -0800 | [diff] [blame] | 817 | case 19: { |
| 818 | // Add userId column |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 819 | if (!addIntegerColumn(db, Favorites.PROFILE_ID, getDefaultUserSerial())) { |
Sunny Goyal | a2cc624 | 2015-01-14 14:23:02 -0800 | [diff] [blame] | 820 | // Old version remains, which means we wipe old data |
| 821 | break; |
| 822 | } |
| 823 | } |
| 824 | case 20: |
| 825 | if (!updateFolderItemsRank(db, true)) { |
| 826 | break; |
| 827 | } |
Sunny Goyal | d2f3819 | 2015-02-25 10:46:34 -0800 | [diff] [blame] | 828 | case 21: |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 829 | // No-op |
Sunny Goyal | d2f3819 | 2015-02-25 10:46:34 -0800 | [diff] [blame] | 830 | case 22: { |
Sunny Goyal | 5d85c44 | 2015-03-10 13:14:47 -0700 | [diff] [blame] | 831 | if (!addIntegerColumn(db, Favorites.OPTIONS, 0)) { |
| 832 | // Old version remains, which means we wipe old data |
| 833 | break; |
| 834 | } |
| 835 | } |
Sunny Goyal | 0b03778 | 2015-04-02 10:27:03 -0700 | [diff] [blame] | 836 | case 23: |
Sunny Goyal | 5c97f51 | 2015-05-19 16:03:28 -0700 | [diff] [blame] | 837 | // No-op |
Sunny Goyal | e2fba6c | 2015-05-12 10:39:59 -0700 | [diff] [blame] | 838 | case 24: |
Tony Mak | 1b6826c | 2018-02-27 15:06:12 +0000 | [diff] [blame] | 839 | // No-op |
Sunny Goyal | 5c97f51 | 2015-05-19 16:03:28 -0700 | [diff] [blame] | 840 | case 25: |
| 841 | convertShortcutsToLauncherActivities(db); |
Sunny Goyal | a9e2f5a | 2016-06-10 12:22:04 -0700 | [diff] [blame] | 842 | case 26: |
Sunny Goyal | 278939f | 2021-07-28 14:35:47 -0700 | [diff] [blame] | 843 | // QSB was moved to the grid. Ignore overlapping items |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 844 | case 27: { |
| 845 | // Update the favorites table so that the screen ids are ordered based on |
| 846 | // workspace page rank. |
Alex Chau | ac03843 | 2021-11-04 16:49:59 +0000 | [diff] [blame] | 847 | IntArray finalScreens = LauncherDbUtils.queryIntArray(false, db, |
| 848 | "workspaceScreens", BaseColumns._ID, null, null, "screenRank"); |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 849 | int[] original = finalScreens.toArray(); |
| 850 | Arrays.sort(original); |
| 851 | String updatemap = ""; |
| 852 | for (int i = 0; i < original.length; i++) { |
| 853 | if (finalScreens.get(i) != original[i]) { |
| 854 | updatemap += String.format(Locale.ENGLISH, " WHEN %1$s=%2$d THEN %3$d", |
| 855 | Favorites.SCREEN, finalScreens.get(i), original[i]); |
| 856 | } |
| 857 | } |
| 858 | if (!TextUtils.isEmpty(updatemap)) { |
| 859 | String query = String.format(Locale.ENGLISH, |
| 860 | "UPDATE %1$s SET %2$s=CASE %3$s ELSE %2$s END WHERE %4$s = %5$d", |
| 861 | Favorites.TABLE_NAME, Favorites.SCREEN, updatemap, |
| 862 | Favorites.CONTAINER, Favorites.CONTAINER_DESKTOP); |
| 863 | db.execSQL(query); |
| 864 | } |
Sunny Goyal | 161a214 | 2018-10-29 14:02:20 -0700 | [diff] [blame] | 865 | dropTable(db, "workspaceScreens"); |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 866 | } |
Yogisha Dixit | 658c5da | 2021-05-24 23:23:15 +0100 | [diff] [blame] | 867 | case 28: { |
| 868 | boolean columnAdded = addIntegerColumn( |
| 869 | db, Favorites.APPWIDGET_SOURCE, Favorites.CONTAINER_UNKNOWN); |
| 870 | if (!columnAdded) { |
| 871 | // Old version remains, which means we wipe old data |
| 872 | break; |
| 873 | } |
| 874 | } |
| 875 | case 29: { |
Andras Kloczl | 929e3a2 | 2021-09-06 03:30:30 +0100 | [diff] [blame] | 876 | // Remove widget panel related leftover workspace items |
| 877 | db.delete(Favorites.TABLE_NAME, Utilities.createDbSelectionQuery( |
| 878 | Favorites.SCREEN, IntArray.wrap(-777, -778)), null); |
| 879 | } |
| 880 | case 30: { |
Alex Chau | e09067c | 2022-01-10 16:13:13 +0000 | [diff] [blame] | 881 | if (FeatureFlags.QSB_ON_FIRST_SCREEN) { |
| 882 | // Clean up first row in screen 0 as it might contain junk data. |
| 883 | Log.d(TAG, "Cleaning up first row"); |
| 884 | db.delete(Favorites.TABLE_NAME, |
| 885 | String.format(Locale.ENGLISH, |
| 886 | "%1$s = %2$d AND %3$s = %4$d AND %5$s = %6$d", |
| 887 | Favorites.SCREEN, 0, |
| 888 | Favorites.CONTAINER, Favorites.CONTAINER_DESKTOP, |
| 889 | Favorites.CELLY, 0), null); |
| 890 | } |
| 891 | return; |
| 892 | } |
| 893 | case 31: { |
Sunny Goyal | a2cc624 | 2015-01-14 14:23:02 -0800 | [diff] [blame] | 894 | // DB Upgraded successfully |
| 895 | return; |
Yogisha Dixit | 658c5da | 2021-05-24 23:23:15 +0100 | [diff] [blame] | 896 | } |
Chris Wren | d5e66bf | 2013-09-16 14:02:29 -0400 | [diff] [blame] | 897 | } |
| 898 | |
Sunny Goyal | a2cc624 | 2015-01-14 14:23:02 -0800 | [diff] [blame] | 899 | // DB was not upgraded |
| 900 | Log.w(TAG, "Destroying all old data."); |
| 901 | createEmptyDB(db); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 902 | } |
Romain Guy | 7eb9e5e | 2009-12-02 20:10:07 -0800 | [diff] [blame] | 903 | |
Adam Cohen | 9b1d062 | 2014-05-21 19:01:57 -0700 | [diff] [blame] | 904 | @Override |
| 905 | public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
Sunny Goyal | 05f3088 | 2017-05-03 12:42:18 -0700 | [diff] [blame] | 906 | try { |
| 907 | DbDowngradeHelper.parse(mContext.getFileStreamPath(DOWNGRADE_SCHEMA_FILE)) |
| 908 | .onDowngrade(db, oldVersion, newVersion); |
| 909 | } catch (Exception e) { |
| 910 | Log.d(TAG, "Unable to downgrade from: " + oldVersion + " to " + newVersion + |
| 911 | ". Wiping databse.", e); |
| 912 | createEmptyDB(db); |
Sunny Goyal | 3c7d55b | 2017-04-13 12:01:47 -0700 | [diff] [blame] | 913 | } |
Sunny Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 914 | } |
Adam Cohen | 9b1d062 | 2014-05-21 19:01:57 -0700 | [diff] [blame] | 915 | |
Sunny Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 916 | /** |
| 917 | * Clears all the data for a fresh start. |
| 918 | */ |
| 919 | public void createEmptyDB(SQLiteDatabase db) { |
Sunny Goyal | dbfc901 | 2017-04-17 16:58:36 -0700 | [diff] [blame] | 920 | try (SQLiteTransaction t = new SQLiteTransaction(db)) { |
Sunny Goyal | 161a214 | 2018-10-29 14:02:20 -0700 | [diff] [blame] | 921 | dropTable(db, Favorites.TABLE_NAME); |
| 922 | dropTable(db, "workspaceScreens"); |
Sunny Goyal | e05b08f | 2017-02-23 18:30:22 -0800 | [diff] [blame] | 923 | onCreate(db); |
Sunny Goyal | dbfc901 | 2017-04-17 16:58:36 -0700 | [diff] [blame] | 924 | t.commit(); |
Sunny Goyal | e05b08f | 2017-02-23 18:30:22 -0800 | [diff] [blame] | 925 | } |
Adam Cohen | 9b1d062 | 2014-05-21 19:01:57 -0700 | [diff] [blame] | 926 | } |
| 927 | |
Sunny Goyal | d2f3819 | 2015-02-25 10:46:34 -0800 | [diff] [blame] | 928 | /** |
Sunny Goyal | 55fddc8 | 2017-04-06 15:02:52 -0700 | [diff] [blame] | 929 | * Removes widgets which are registered to the Launcher's host, but are not present |
| 930 | * in our model. |
| 931 | */ |
| 932 | public void removeGhostWidgets(SQLiteDatabase db) { |
| 933 | // Get all existing widget ids. |
| 934 | final AppWidgetHost host = newLauncherWidgetHost(); |
| 935 | final int[] allWidgets; |
| 936 | try { |
Sunny Goyal | 22ca9ec | 2017-05-18 15:03:13 -0700 | [diff] [blame] | 937 | // Although the method was defined in O, it has existed since the beginning of time, |
| 938 | // so it might work on older platforms as well. |
| 939 | allWidgets = host.getAppWidgetIds(); |
| 940 | } catch (IncompatibleClassChangeError e) { |
Sunny Goyal | 55fddc8 | 2017-04-06 15:02:52 -0700 | [diff] [blame] | 941 | Log.e(TAG, "getAppWidgetIds not supported", e); |
| 942 | return; |
| 943 | } |
Alex Chau | ac03843 | 2021-11-04 16:49:59 +0000 | [diff] [blame] | 944 | final IntSet validWidgets = IntSet.wrap(LauncherDbUtils.queryIntArray(false, db, |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 945 | Favorites.TABLE_NAME, Favorites.APPWIDGET_ID, |
| 946 | "itemType=" + Favorites.ITEM_TYPE_APPWIDGET, null, null)); |
Sunny Goyal | dbfc901 | 2017-04-17 16:58:36 -0700 | [diff] [blame] | 947 | for (int widgetId : allWidgets) { |
| 948 | if (!validWidgets.contains(widgetId)) { |
| 949 | try { |
| 950 | FileLog.d(TAG, "Deleting invalid widget " + widgetId); |
| 951 | host.deleteAppWidgetId(widgetId); |
| 952 | } catch (RuntimeException e) { |
| 953 | // Ignore |
| 954 | } |
| 955 | } |
Sunny Goyal | 55fddc8 | 2017-04-06 15:02:52 -0700 | [diff] [blame] | 956 | } |
| 957 | } |
| 958 | |
| 959 | /** |
Sunny Goyal | 0b03778 | 2015-04-02 10:27:03 -0700 | [diff] [blame] | 960 | * Replaces all shortcuts of type {@link Favorites#ITEM_TYPE_SHORTCUT} which have a valid |
| 961 | * launcher activity target with {@link Favorites#ITEM_TYPE_APPLICATION}. |
| 962 | */ |
Sunny Goyal | 316490e | 2015-06-02 09:38:28 -0700 | [diff] [blame] | 963 | @Thunk void convertShortcutsToLauncherActivities(SQLiteDatabase db) { |
Sunny Goyal | dbfc901 | 2017-04-17 16:58:36 -0700 | [diff] [blame] | 964 | try (SQLiteTransaction t = new SQLiteTransaction(db); |
| 965 | // Only consider the primary user as other users can't have a shortcut. |
| 966 | Cursor c = db.query(Favorites.TABLE_NAME, |
| 967 | new String[] { Favorites._ID, Favorites.INTENT}, |
| 968 | "itemType=" + Favorites.ITEM_TYPE_SHORTCUT + |
| 969 | " AND profileId=" + getDefaultUserSerial(), |
| 970 | null, null, null, null); |
| 971 | SQLiteStatement updateStmt = db.compileStatement("UPDATE favorites SET itemType=" |
| 972 | + Favorites.ITEM_TYPE_APPLICATION + " WHERE _id=?") |
| 973 | ) { |
Sunny Goyal | 0b03778 | 2015-04-02 10:27:03 -0700 | [diff] [blame] | 974 | final int idIndex = c.getColumnIndexOrThrow(Favorites._ID); |
| 975 | final int intentIndex = c.getColumnIndexOrThrow(Favorites.INTENT); |
| 976 | |
| 977 | while (c.moveToNext()) { |
| 978 | String intentDescription = c.getString(intentIndex); |
| 979 | Intent intent; |
| 980 | try { |
| 981 | intent = Intent.parseUri(intentDescription, 0); |
| 982 | } catch (URISyntaxException e) { |
| 983 | Log.e(TAG, "Unable to parse intent", e); |
| 984 | continue; |
| 985 | } |
| 986 | |
Sunny Goyal | 9dbb27c | 2019-07-17 15:12:56 -0700 | [diff] [blame] | 987 | if (!PackageManagerHelper.isLauncherAppTarget(intent)) { |
Sunny Goyal | 0b03778 | 2015-04-02 10:27:03 -0700 | [diff] [blame] | 988 | continue; |
| 989 | } |
| 990 | |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 991 | int id = c.getInt(idIndex); |
Sunny Goyal | 0b03778 | 2015-04-02 10:27:03 -0700 | [diff] [blame] | 992 | updateStmt.bindLong(1, id); |
Sunny Goyal | c22841b | 2015-07-13 19:59:50 -0700 | [diff] [blame] | 993 | updateStmt.executeUpdateDelete(); |
Sunny Goyal | 0b03778 | 2015-04-02 10:27:03 -0700 | [diff] [blame] | 994 | } |
Sunny Goyal | dbfc901 | 2017-04-17 16:58:36 -0700 | [diff] [blame] | 995 | t.commit(); |
Sunny Goyal | 0b03778 | 2015-04-02 10:27:03 -0700 | [diff] [blame] | 996 | } catch (SQLException ex) { |
| 997 | Log.w(TAG, "Error deduping shortcuts", ex); |
Sunny Goyal | 0b03778 | 2015-04-02 10:27:03 -0700 | [diff] [blame] | 998 | } |
| 999 | } |
| 1000 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 1001 | @Thunk boolean updateFolderItemsRank(SQLiteDatabase db, boolean addRankColumn) { |
Sunny Goyal | dbfc901 | 2017-04-17 16:58:36 -0700 | [diff] [blame] | 1002 | try (SQLiteTransaction t = new SQLiteTransaction(db)) { |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 1003 | if (addRankColumn) { |
| 1004 | // Insert new column for holding rank |
| 1005 | db.execSQL("ALTER TABLE favorites ADD COLUMN rank INTEGER NOT NULL DEFAULT 0;"); |
| 1006 | } |
| 1007 | |
| 1008 | // Get a map for folder ID to folder width |
| 1009 | Cursor c = db.rawQuery("SELECT container, MAX(cellX) FROM favorites" |
| 1010 | + " WHERE container IN (SELECT _id FROM favorites WHERE itemType = ?)" |
| 1011 | + " GROUP BY container;", |
| 1012 | new String[] {Integer.toString(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}); |
| 1013 | |
| 1014 | while (c.moveToNext()) { |
Sunny Goyal | c87775d | 2015-02-10 19:52:36 -0800 | [diff] [blame] | 1015 | db.execSQL("UPDATE favorites SET rank=cellX+(cellY*?) WHERE " |
| 1016 | + "container=? AND cellX IS NOT NULL AND cellY IS NOT NULL;", |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 1017 | new Object[] {c.getLong(1) + 1, c.getLong(0)}); |
| 1018 | } |
| 1019 | |
| 1020 | c.close(); |
Sunny Goyal | dbfc901 | 2017-04-17 16:58:36 -0700 | [diff] [blame] | 1021 | t.commit(); |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 1022 | } catch (SQLException ex) { |
| 1023 | // Old version remains, which means we wipe old data |
| 1024 | Log.e(TAG, ex.getMessage(), ex); |
| 1025 | return false; |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 1026 | } |
| 1027 | return true; |
| 1028 | } |
| 1029 | |
Sunny Goyal | 5d85c44 | 2015-03-10 13:14:47 -0700 | [diff] [blame] | 1030 | private boolean addIntegerColumn(SQLiteDatabase db, String columnName, long defaultValue) { |
Sunny Goyal | dbfc901 | 2017-04-17 16:58:36 -0700 | [diff] [blame] | 1031 | try (SQLiteTransaction t = new SQLiteTransaction(db)) { |
Sunny Goyal | 5d85c44 | 2015-03-10 13:14:47 -0700 | [diff] [blame] | 1032 | db.execSQL("ALTER TABLE favorites ADD COLUMN " |
| 1033 | + columnName + " INTEGER NOT NULL DEFAULT " + defaultValue + ";"); |
Sunny Goyal | dbfc901 | 2017-04-17 16:58:36 -0700 | [diff] [blame] | 1034 | t.commit(); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1035 | } catch (SQLException ex) { |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1036 | Log.e(TAG, ex.getMessage(), ex); |
| 1037 | return false; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1038 | } |
| 1039 | return true; |
| 1040 | } |
| 1041 | |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 1042 | // Generates a new ID to use for an object in your database. This method should be only |
| 1043 | // called from the main UI thread. As an exception, we do call it when we call the |
| 1044 | // constructor from the worker thread; however, this doesn't extend until after the |
| 1045 | // constructor is called, and we only pass a reference to LauncherProvider to LauncherApp |
| 1046 | // after that point |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 1047 | @Override |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 1048 | public int generateNewItemId() { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1049 | if (mMaxItemId < 0) { |
| 1050 | throw new RuntimeException("Error: max item id was not initialized"); |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 1051 | } |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1052 | mMaxItemId += 1; |
| 1053 | return mMaxItemId; |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
Sunny Goyal | 55fddc8 | 2017-04-06 15:02:52 -0700 | [diff] [blame] | 1056 | public AppWidgetHost newLauncherWidgetHost() { |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 1057 | return new LauncherAppWidgetHost(mContext); |
Sunny Goyal | 55fddc8 | 2017-04-06 15:02:52 -0700 | [diff] [blame] | 1058 | } |
| 1059 | |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 1060 | @Override |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 1061 | public int insertAndCheck(SQLiteDatabase db, ContentValues values) { |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 1062 | return dbInsertAndCheck(this, db, Favorites.TABLE_NAME, null, values); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 1065 | public void checkId(ContentValues values) { |
| 1066 | int id = values.getAsInteger(Favorites._ID); |
| 1067 | mMaxItemId = Math.max(id, mMaxItemId); |
Chris Wren | 5dee7af | 2013-12-20 17:22:11 -0500 | [diff] [blame] | 1068 | } |
| 1069 | |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 1070 | private int initializeMaxItemId(SQLiteDatabase db) { |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 1071 | return getMaxId(db, "SELECT MAX(%1$s) FROM %2$s", Favorites._ID, Favorites.TABLE_NAME); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1072 | } |
| 1073 | |
Andras Kloczl | 953eb80 | 2021-10-15 21:16:48 +0100 | [diff] [blame] | 1074 | // Returns a new ID to use for an workspace screen in your database that is greater than all |
| 1075 | // existing screen IDs. |
| 1076 | private int getNewScreenId() { |
| 1077 | return getMaxId(getWritableDatabase(), |
| 1078 | "SELECT MAX(%1$s) FROM %2$s WHERE %3$s = %4$d AND %1$s >= 0", |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 1079 | Favorites.SCREEN, Favorites.TABLE_NAME, Favorites.CONTAINER, |
Andras Kloczl | 953eb80 | 2021-10-15 21:16:48 +0100 | [diff] [blame] | 1080 | Favorites.CONTAINER_DESKTOP) + 1; |
Adam Cohen | 7ec3bbf | 2014-07-31 00:09:45 -0700 | [diff] [blame] | 1081 | } |
| 1082 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 1083 | @Thunk int loadFavorites(SQLiteDatabase db, AutoInstallsLayout loader) { |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 1084 | // TODO: Use multiple loaders with fall-back and transaction. |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 1085 | int count = loader.loadLayout(db, new IntArray()); |
Adam Cohen | 71483f4 | 2014-05-15 14:04:01 -0700 | [diff] [blame] | 1086 | |
| 1087 | // Ensure that the max ids are initialized |
| 1088 | mMaxItemId = initializeMaxItemId(db); |
Adam Cohen | 71483f4 | 2014-05-15 14:04:01 -0700 | [diff] [blame] | 1089 | return count; |
| 1090 | } |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 1091 | } |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 1092 | |
Sunny Goyal | fe4e4b9 | 2015-03-04 10:43:45 -0800 | [diff] [blame] | 1093 | /** |
| 1094 | * @return the max _id in the provided table. |
| 1095 | */ |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 1096 | @Thunk static int getMaxId(SQLiteDatabase db, String query, Object... args) { |
Pinyao Ting | 0646dde | 2020-11-24 11:35:24 -0800 | [diff] [blame] | 1097 | int max = 0; |
| 1098 | try (SQLiteStatement prog = db.compileStatement( |
| 1099 | String.format(Locale.ENGLISH, query, args))) { |
| 1100 | max = (int) DatabaseUtils.longForQuery(prog, null); |
| 1101 | if (max < 0) { |
| 1102 | throw new RuntimeException("Error: could not query max id"); |
| 1103 | } |
| 1104 | } catch (IllegalArgumentException exception) { |
| 1105 | String message = exception.getMessage(); |
| 1106 | if (message.contains("re-open") && message.contains("already-closed")) { |
| 1107 | // Don't crash trying to end a transaction an an already closed DB. See b/173162852. |
| 1108 | } else { |
| 1109 | throw exception; |
| 1110 | } |
Sunny Goyal | fe4e4b9 | 2015-03-04 10:43:45 -0800 | [diff] [blame] | 1111 | } |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 1112 | return max; |
Sunny Goyal | fe4e4b9 | 2015-03-04 10:43:45 -0800 | [diff] [blame] | 1113 | } |
| 1114 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1115 | static class SqlArguments { |
| 1116 | public final String table; |
| 1117 | public final String where; |
| 1118 | public final String[] args; |
| 1119 | |
| 1120 | SqlArguments(Uri url, String where, String[] args) { |
| 1121 | if (url.getPathSegments().size() == 1) { |
| 1122 | this.table = url.getPathSegments().get(0); |
| 1123 | this.where = where; |
| 1124 | this.args = args; |
| 1125 | } else if (url.getPathSegments().size() != 2) { |
| 1126 | throw new IllegalArgumentException("Invalid URI: " + url); |
| 1127 | } else if (!TextUtils.isEmpty(where)) { |
| 1128 | throw new UnsupportedOperationException("WHERE clause not supported: " + url); |
| 1129 | } else { |
| 1130 | this.table = url.getPathSegments().get(0); |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 1131 | this.where = "_id=" + ContentUris.parseId(url); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1132 | this.args = null; |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | SqlArguments(Uri url) { |
| 1137 | if (url.getPathSegments().size() == 1) { |
| 1138 | table = url.getPathSegments().get(0); |
| 1139 | where = null; |
| 1140 | args = null; |
| 1141 | } else { |
| 1142 | throw new IllegalArgumentException("Invalid URI: " + url); |
| 1143 | } |
| 1144 | } |
| 1145 | } |
Adam Cohen | 7296097 | 2014-01-15 18:13:55 -0800 | [diff] [blame] | 1146 | } |