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