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