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