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