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