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