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