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