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