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