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