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