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