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