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