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