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; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 25 | import android.content.ContentResolver; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 26 | import android.content.ContentUris; |
| 27 | import android.content.ContentValues; |
| 28 | import android.content.Context; |
| 29 | import android.content.Intent; |
Yura | 085c853 | 2014-02-11 15:15:29 +0000 | [diff] [blame] | 30 | import android.content.OperationApplicationException; |
Michael Jurka | b85f8a4 | 2012-04-25 15:48:32 -0700 | [diff] [blame] | 31 | import android.content.SharedPreferences; |
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; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 38 | import android.net.Uri; |
Nilesh Agrawal | fde1185 | 2015-01-21 11:50:57 -0800 | [diff] [blame] | 39 | import android.os.StrictMode; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 40 | import android.text.TextUtils; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 41 | import android.util.Log; |
Dan Sandler | ab5fa3a | 2014-03-06 23:48:04 -0500 | [diff] [blame] | 42 | import android.util.SparseArray; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 43 | |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 44 | import com.android.launcher3.AutoInstallsLayout.LayoutParserCallback; |
| 45 | import com.android.launcher3.LauncherSettings.Favorites; |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 46 | import com.android.launcher3.compat.UserHandleCompat; |
| 47 | import com.android.launcher3.compat.UserManagerCompat; |
Chris Wren | e523e70 | 2013-10-09 10:36:55 -0400 | [diff] [blame] | 48 | import com.android.launcher3.config.ProviderConfig; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 49 | |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 50 | import java.io.File; |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 51 | import java.net.URISyntaxException; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 52 | import java.util.ArrayList; |
Adam Cohen | 71483f4 | 2014-05-15 14:04:01 -0700 | [diff] [blame] | 53 | import java.util.Collections; |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 54 | import java.util.HashSet; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 55 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 56 | public class LauncherProvider extends ContentProvider { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 57 | private static final String TAG = "Launcher.LauncherProvider"; |
| 58 | private static final boolean LOGD = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 59 | |
Sunny Goyal | e87e6ab | 2014-11-21 22:42:53 -0800 | [diff] [blame] | 60 | private static final int MIN_DATABASE_VERSION = 12; |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 61 | private static final int DATABASE_VERSION = 21; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 62 | |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 63 | static final String OLD_AUTHORITY = "com.android.launcher2.settings"; |
Chris Wren | e523e70 | 2013-10-09 10:36:55 -0400 | [diff] [blame] | 64 | static final String AUTHORITY = ProviderConfig.AUTHORITY; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 65 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 66 | static final String TABLE_FAVORITES = "favorites"; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 67 | static final String TABLE_WORKSPACE_SCREENS = "workspaceScreens"; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 68 | static final String PARAMETER_NOTIFY = "notify"; |
Sunny Goyal | e87e6ab | 2014-11-21 22:42:53 -0800 | [diff] [blame] | 69 | static final String UPGRADED_FROM_OLD_DATABASE = "UPGRADED_FROM_OLD_DATABASE"; |
| 70 | static final String EMPTY_DATABASE_CREATED = "EMPTY_DATABASE_CREATED"; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 71 | |
Adam Cohen | a043fa8 | 2014-07-23 14:49:38 -0700 | [diff] [blame] | 72 | private static final String URI_PARAM_IS_EXTERNAL_ADD = "isExternalAdd"; |
| 73 | |
Anjali Koppal | 67e7cae | 2014-03-13 12:14:12 -0700 | [diff] [blame] | 74 | private LauncherProviderChangeListener mListener; |
| 75 | |
Jeffrey Sharkey | 2bbcae1 | 2009-03-31 14:37:57 -0700 | [diff] [blame] | 76 | /** |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 77 | * {@link Uri} triggered at any registered {@link android.database.ContentObserver} when |
Jeffrey Sharkey | 2bbcae1 | 2009-03-31 14:37:57 -0700 | [diff] [blame] | 78 | * {@link AppWidgetHost#deleteHost()} is called during database creation. |
| 79 | * Use this to recall {@link AppWidgetHost#startListening()} if needed. |
| 80 | */ |
| 81 | static final Uri CONTENT_APPWIDGET_RESET_URI = |
| 82 | Uri.parse("content://" + AUTHORITY + "/appWidgetReset"); |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 83 | |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 84 | private DatabaseHelper mOpenHelper; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 85 | |
| 86 | @Override |
| 87 | public boolean onCreate() { |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 88 | final Context context = getContext(); |
Nilesh Agrawal | fde1185 | 2015-01-21 11:50:57 -0800 | [diff] [blame] | 89 | StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 90 | mOpenHelper = new DatabaseHelper(context); |
Nilesh Agrawal | fde1185 | 2015-01-21 11:50:57 -0800 | [diff] [blame] | 91 | StrictMode.setThreadPolicy(oldPolicy); |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 92 | LauncherAppState.setLauncherProvider(this); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 93 | return true; |
| 94 | } |
| 95 | |
Winson Chung | 0b560dd | 2014-01-21 13:00:26 -0800 | [diff] [blame] | 96 | public boolean wasNewDbCreated() { |
| 97 | return mOpenHelper.wasNewDbCreated(); |
| 98 | } |
| 99 | |
Anjali Koppal | 67e7cae | 2014-03-13 12:14:12 -0700 | [diff] [blame] | 100 | public void setLauncherProviderChangeListener(LauncherProviderChangeListener listener) { |
| 101 | mListener = listener; |
| 102 | } |
| 103 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 104 | @Override |
| 105 | public String getType(Uri uri) { |
| 106 | SqlArguments args = new SqlArguments(uri, null, null); |
| 107 | if (TextUtils.isEmpty(args.where)) { |
| 108 | return "vnd.android.cursor.dir/" + args.table; |
| 109 | } else { |
| 110 | return "vnd.android.cursor.item/" + args.table; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public Cursor query(Uri uri, String[] projection, String selection, |
| 116 | String[] selectionArgs, String sortOrder) { |
| 117 | |
| 118 | SqlArguments args = new SqlArguments(uri, selection, selectionArgs); |
| 119 | SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); |
| 120 | qb.setTables(args.table); |
| 121 | |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 122 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 123 | Cursor result = qb.query(db, projection, args.where, args.args, null, null, sortOrder); |
| 124 | result.setNotificationUri(getContext().getContentResolver(), uri); |
| 125 | |
| 126 | return result; |
| 127 | } |
| 128 | |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 129 | private static long dbInsertAndCheck(DatabaseHelper helper, |
| 130 | SQLiteDatabase db, String table, String nullColumnHack, ContentValues values) { |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 131 | if (values == null) { |
| 132 | throw new RuntimeException("Error: attempting to insert null values"); |
| 133 | } |
Adam Cohen | 71483f4 | 2014-05-15 14:04:01 -0700 | [diff] [blame] | 134 | if (!values.containsKey(LauncherSettings.ChangeLogColumns._ID)) { |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 135 | throw new RuntimeException("Error: attempting to add item without specifying an id"); |
| 136 | } |
Chris Wren | 5dee7af | 2013-12-20 17:22:11 -0500 | [diff] [blame] | 137 | helper.checkId(table, values); |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 138 | return db.insert(table, nullColumnHack, values); |
| 139 | } |
| 140 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 141 | @Override |
| 142 | public Uri insert(Uri uri, ContentValues initialValues) { |
| 143 | SqlArguments args = new SqlArguments(uri); |
| 144 | |
Adam Cohen | a043fa8 | 2014-07-23 14:49:38 -0700 | [diff] [blame] | 145 | // In very limited cases, we support system|signature permission apps to add to the db |
| 146 | String externalAdd = uri.getQueryParameter(URI_PARAM_IS_EXTERNAL_ADD); |
| 147 | if (externalAdd != null && "true".equals(externalAdd)) { |
| 148 | if (!mOpenHelper.initializeExternalAdd(initialValues)) { |
| 149 | return null; |
| 150 | } |
| 151 | } |
| 152 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 153 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 154 | addModifiedTime(initialValues); |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 155 | final long rowId = dbInsertAndCheck(mOpenHelper, db, args.table, null, initialValues); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 156 | if (rowId <= 0) return null; |
| 157 | |
| 158 | uri = ContentUris.withAppendedId(uri, rowId); |
| 159 | sendNotify(uri); |
| 160 | |
| 161 | return uri; |
| 162 | } |
| 163 | |
Adam Cohen | a043fa8 | 2014-07-23 14:49:38 -0700 | [diff] [blame] | 164 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 165 | @Override |
| 166 | public int bulkInsert(Uri uri, ContentValues[] values) { |
| 167 | SqlArguments args = new SqlArguments(uri); |
| 168 | |
| 169 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 170 | db.beginTransaction(); |
| 171 | try { |
| 172 | int numValues = values.length; |
| 173 | for (int i = 0; i < numValues; i++) { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 174 | addModifiedTime(values[i]); |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 175 | if (dbInsertAndCheck(mOpenHelper, db, args.table, null, values[i]) < 0) { |
| 176 | return 0; |
| 177 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 178 | } |
| 179 | db.setTransactionSuccessful(); |
| 180 | } finally { |
| 181 | db.endTransaction(); |
| 182 | } |
| 183 | |
| 184 | sendNotify(uri); |
| 185 | return values.length; |
| 186 | } |
| 187 | |
| 188 | @Override |
Yura | 085c853 | 2014-02-11 15:15:29 +0000 | [diff] [blame] | 189 | public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) |
| 190 | throws OperationApplicationException { |
| 191 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 192 | db.beginTransaction(); |
| 193 | try { |
| 194 | ContentProviderResult[] result = super.applyBatch(operations); |
| 195 | db.setTransactionSuccessful(); |
| 196 | return result; |
| 197 | } finally { |
| 198 | db.endTransaction(); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 203 | public int delete(Uri uri, String selection, String[] selectionArgs) { |
| 204 | SqlArguments args = new SqlArguments(uri, selection, selectionArgs); |
| 205 | |
| 206 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 207 | int count = db.delete(args.table, args.where, args.args); |
| 208 | if (count > 0) sendNotify(uri); |
| 209 | |
| 210 | return count; |
| 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { |
| 215 | SqlArguments args = new SqlArguments(uri, selection, selectionArgs); |
| 216 | |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 217 | addModifiedTime(values); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 218 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 219 | int count = db.update(args.table, values, args.where, args.args); |
| 220 | if (count > 0) sendNotify(uri); |
| 221 | |
| 222 | return count; |
| 223 | } |
| 224 | |
| 225 | private void sendNotify(Uri uri) { |
| 226 | String notify = uri.getQueryParameter(PARAMETER_NOTIFY); |
| 227 | if (notify == null || "true".equals(notify)) { |
| 228 | getContext().getContentResolver().notifyChange(uri, null); |
| 229 | } |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 230 | |
| 231 | // always notify the backup agent |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 232 | LauncherBackupAgentHelper.dataChanged(getContext()); |
Anjali Koppal | 67e7cae | 2014-03-13 12:14:12 -0700 | [diff] [blame] | 233 | if (mListener != null) { |
| 234 | mListener.onLauncherProviderChange(); |
| 235 | } |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | private void addModifiedTime(ContentValues values) { |
| 239 | values.put(LauncherSettings.ChangeLogColumns.MODIFIED, System.currentTimeMillis()); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 240 | } |
| 241 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 242 | public long generateNewItemId() { |
| 243 | return mOpenHelper.generateNewItemId(); |
| 244 | } |
| 245 | |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 246 | public void updateMaxItemId(long id) { |
| 247 | mOpenHelper.updateMaxItemId(id); |
| 248 | } |
| 249 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 250 | public long generateNewScreenId() { |
| 251 | return mOpenHelper.generateNewScreenId(); |
| 252 | } |
| 253 | |
| 254 | // This is only required one time while loading the workspace during the |
| 255 | // upgrade path, and should never be called from anywhere else. |
| 256 | public void updateMaxScreenId(long maxScreenId) { |
| 257 | mOpenHelper.updateMaxScreenId(maxScreenId); |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Brian Muramatsu | 5524b49 | 2012-10-02 16:55:54 -0700 | [diff] [blame] | 260 | /** |
Sunny Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 261 | * Clears all the data for a fresh start. |
| 262 | */ |
| 263 | synchronized public void createEmptyDB() { |
| 264 | mOpenHelper.createEmptyDB(mOpenHelper.getWritableDatabase()); |
| 265 | } |
| 266 | |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 267 | public void clearFlagEmptyDbCreated() { |
| 268 | String spKey = LauncherAppState.getSharedPreferencesKey(); |
| 269 | getContext().getSharedPreferences(spKey, Context.MODE_PRIVATE) |
| 270 | .edit() |
| 271 | .remove(EMPTY_DATABASE_CREATED) |
| 272 | .commit(); |
| 273 | } |
| 274 | |
Sunny Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 275 | /** |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 276 | * Loads the default workspace based on the following priority scheme: |
| 277 | * 1) From a package provided by play store |
| 278 | * 2) From a partner configuration APK, already in the system image |
| 279 | * 3) The default configuration for the particular device |
Brian Muramatsu | 5524b49 | 2012-10-02 16:55:54 -0700 | [diff] [blame] | 280 | */ |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 281 | synchronized public void loadDefaultFavoritesIfNecessary() { |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 282 | String spKey = LauncherAppState.getSharedPreferencesKey(); |
Michael Jurka | b85f8a4 | 2012-04-25 15:48:32 -0700 | [diff] [blame] | 283 | SharedPreferences sp = getContext().getSharedPreferences(spKey, Context.MODE_PRIVATE); |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 284 | |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 285 | if (sp.getBoolean(EMPTY_DATABASE_CREATED, false)) { |
Chris Wren | 5dee7af | 2013-12-20 17:22:11 -0500 | [diff] [blame] | 286 | Log.d(TAG, "loading default workspace"); |
Michael Jurka | 45355c4 | 2012-10-08 13:21:35 +0200 | [diff] [blame] | 287 | |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 288 | AutoInstallsLayout loader = AutoInstallsLayout.get(getContext(), |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 289 | mOpenHelper.mAppWidgetHost, mOpenHelper); |
| 290 | |
| 291 | if (loader == null) { |
Adam Cohen | 9b8f51f | 2014-05-30 15:34:09 -0700 | [diff] [blame] | 292 | final Partner partner = Partner.get(getContext().getPackageManager()); |
| 293 | if (partner != null && partner.hasDefaultLayout()) { |
| 294 | final Resources partnerRes = partner.getResources(); |
Adam Cohen | 4ae96ce | 2014-08-29 15:05:48 -0700 | [diff] [blame] | 295 | int workspaceResId = partnerRes.getIdentifier(Partner.RES_DEFAULT_LAYOUT, |
Adam Cohen | 9b8f51f | 2014-05-30 15:34:09 -0700 | [diff] [blame] | 296 | "xml", partner.getPackageName()); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 297 | if (workspaceResId != 0) { |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 298 | loader = new DefaultLayoutParser(getContext(), mOpenHelper.mAppWidgetHost, |
| 299 | mOpenHelper, partnerRes, workspaceResId); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 300 | } |
Adam Cohen | 9b8f51f | 2014-05-30 15:34:09 -0700 | [diff] [blame] | 301 | } |
| 302 | } |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 303 | |
Sunny Goyal | 9d21968 | 2014-10-23 14:21:02 -0700 | [diff] [blame] | 304 | final boolean usingExternallyProvidedLayout = loader != null; |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 305 | if (loader == null) { |
Sunny Goyal | 9d21968 | 2014-10-23 14:21:02 -0700 | [diff] [blame] | 306 | loader = getDefaultLayoutParser(); |
Brian Muramatsu | 5524b49 | 2012-10-02 16:55:54 -0700 | [diff] [blame] | 307 | } |
Michael Jurka | b85f8a4 | 2012-04-25 15:48:32 -0700 | [diff] [blame] | 308 | // Populate favorites table with initial favorites |
Sunny Goyal | 9d21968 | 2014-10-23 14:21:02 -0700 | [diff] [blame] | 309 | if ((mOpenHelper.loadFavorites(mOpenHelper.getWritableDatabase(), loader) <= 0) |
| 310 | && usingExternallyProvidedLayout) { |
| 311 | // Unable to load external layout. Cleanup and load the internal layout. |
| 312 | createEmptyDB(); |
| 313 | mOpenHelper.loadFavorites(mOpenHelper.getWritableDatabase(), |
| 314 | getDefaultLayoutParser()); |
| 315 | } |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 316 | clearFlagEmptyDbCreated(); |
Michael Jurka | b85f8a4 | 2012-04-25 15:48:32 -0700 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
Sunny Goyal | 9d21968 | 2014-10-23 14:21:02 -0700 | [diff] [blame] | 320 | private DefaultLayoutParser getDefaultLayoutParser() { |
| 321 | int defaultLayout = LauncherAppState.getInstance() |
| 322 | .getDynamicGrid().getDeviceProfile().defaultLayoutId; |
| 323 | return new DefaultLayoutParser(getContext(), mOpenHelper.mAppWidgetHost, |
| 324 | mOpenHelper, getContext().getResources(), defaultLayout); |
| 325 | } |
| 326 | |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 327 | public void migrateLauncher2Shortcuts() { |
| 328 | mOpenHelper.migrateLauncher2Shortcuts(mOpenHelper.getWritableDatabase(), |
Jason Monk | 0bfcceb | 2014-03-21 15:42:06 -0400 | [diff] [blame] | 329 | Uri.parse(getContext().getString(R.string.old_launcher_provider_uri))); |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 330 | } |
| 331 | |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 332 | public void updateFolderItemsRank() { |
| 333 | mOpenHelper.updateFolderItemsRank(mOpenHelper.getWritableDatabase(), false); |
| 334 | } |
| 335 | |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 336 | public void deleteDatabase() { |
| 337 | // Are you sure? (y/n) |
| 338 | final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
Dan Sandler | 2b47174 | 2014-01-21 14:14:41 -0500 | [diff] [blame] | 339 | final File dbFile = new File(db.getPath()); |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 340 | mOpenHelper.close(); |
Dan Sandler | 2b47174 | 2014-01-21 14:14:41 -0500 | [diff] [blame] | 341 | if (dbFile.exists()) { |
| 342 | SQLiteDatabase.deleteDatabase(dbFile); |
| 343 | } |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 344 | mOpenHelper = new DatabaseHelper(getContext()); |
| 345 | } |
| 346 | |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 347 | private static class DatabaseHelper extends SQLiteOpenHelper implements LayoutParserCallback { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 348 | private final Context mContext; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 349 | private final AppWidgetHost mAppWidgetHost; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 350 | private long mMaxItemId = -1; |
| 351 | private long mMaxScreenId = -1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 352 | |
Winson Chung | 0b560dd | 2014-01-21 13:00:26 -0800 | [diff] [blame] | 353 | private boolean mNewDbCreated = false; |
| 354 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 355 | DatabaseHelper(Context context) { |
Helena Josol | 4fbbb3e | 2014-10-06 16:06:46 +0100 | [diff] [blame] | 356 | super(context, LauncherFiles.LAUNCHER_DB, null, DATABASE_VERSION); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 357 | mContext = context; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 358 | mAppWidgetHost = new AppWidgetHost(context, Launcher.APPWIDGET_HOST_ID); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 359 | |
| 360 | // In the case where neither onCreate nor onUpgrade gets called, we read the maxId from |
| 361 | // the DB here |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 362 | if (mMaxItemId == -1) { |
| 363 | mMaxItemId = initializeMaxItemId(getWritableDatabase()); |
| 364 | } |
| 365 | if (mMaxScreenId == -1) { |
| 366 | mMaxScreenId = initializeMaxScreenId(getWritableDatabase()); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 367 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 368 | } |
| 369 | |
Winson Chung | 0b560dd | 2014-01-21 13:00:26 -0800 | [diff] [blame] | 370 | public boolean wasNewDbCreated() { |
| 371 | return mNewDbCreated; |
| 372 | } |
| 373 | |
Jeffrey Sharkey | 2bbcae1 | 2009-03-31 14:37:57 -0700 | [diff] [blame] | 374 | /** |
| 375 | * Send notification that we've deleted the {@link AppWidgetHost}, |
| 376 | * probably as part of the initial database creation. The receiver may |
| 377 | * want to re-call {@link AppWidgetHost#startListening()} to ensure |
| 378 | * callbacks are correctly set. |
| 379 | */ |
| 380 | private void sendAppWidgetResetNotify() { |
| 381 | final ContentResolver resolver = mContext.getContentResolver(); |
| 382 | resolver.notifyChange(CONTENT_APPWIDGET_RESET_URI, null); |
| 383 | } |
| 384 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 385 | @Override |
| 386 | public void onCreate(SQLiteDatabase db) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 387 | if (LOGD) Log.d(TAG, "creating new launcher database"); |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 388 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 389 | mMaxItemId = 1; |
| 390 | mMaxScreenId = 0; |
Winson Chung | 0b560dd | 2014-01-21 13:00:26 -0800 | [diff] [blame] | 391 | mNewDbCreated = true; |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 392 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 393 | UserManagerCompat userManager = UserManagerCompat.getInstance(mContext); |
| 394 | long userSerialNumber = userManager.getSerialNumberForUser( |
| 395 | UserHandleCompat.myUserHandle()); |
| 396 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 397 | db.execSQL("CREATE TABLE favorites (" + |
| 398 | "_id INTEGER PRIMARY KEY," + |
| 399 | "title TEXT," + |
| 400 | "intent TEXT," + |
| 401 | "container INTEGER," + |
| 402 | "screen INTEGER," + |
| 403 | "cellX INTEGER," + |
| 404 | "cellY INTEGER," + |
| 405 | "spanX INTEGER," + |
| 406 | "spanY INTEGER," + |
| 407 | "itemType INTEGER," + |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 408 | "appWidgetId INTEGER NOT NULL DEFAULT -1," + |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 409 | "isShortcut INTEGER," + |
| 410 | "iconType INTEGER," + |
| 411 | "iconPackage TEXT," + |
| 412 | "iconResource TEXT," + |
| 413 | "icon BLOB," + |
| 414 | "uri TEXT," + |
Chris Wren | d5e66bf | 2013-09-16 14:02:29 -0400 | [diff] [blame] | 415 | "displayMode INTEGER," + |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 416 | "appWidgetProvider TEXT," + |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 417 | "modified INTEGER NOT NULL DEFAULT 0," + |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 418 | "restored INTEGER NOT NULL DEFAULT 0," + |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 419 | "profileId INTEGER DEFAULT " + userSerialNumber + "," + |
| 420 | "rank INTEGER NOT NULL DEFAULT 0" + |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 421 | ");"); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 422 | addWorkspacesTable(db); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 423 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 424 | // Database was just created, so wipe any previous widgets |
| 425 | if (mAppWidgetHost != null) { |
| 426 | mAppWidgetHost.deleteHost(); |
Jeffrey Sharkey | 2bbcae1 | 2009-03-31 14:37:57 -0700 | [diff] [blame] | 427 | sendAppWidgetResetNotify(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 428 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 429 | |
Sunny Goyal | e87e6ab | 2014-11-21 22:42:53 -0800 | [diff] [blame] | 430 | // Fresh and clean launcher DB. |
| 431 | mMaxItemId = initializeMaxItemId(db); |
| 432 | setFlagEmptyDbCreated(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 433 | } |
| 434 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 435 | private void addWorkspacesTable(SQLiteDatabase db) { |
| 436 | db.execSQL("CREATE TABLE " + TABLE_WORKSPACE_SCREENS + " (" + |
| 437 | LauncherSettings.WorkspaceScreens._ID + " INTEGER," + |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 438 | LauncherSettings.WorkspaceScreens.SCREEN_RANK + " INTEGER," + |
| 439 | LauncherSettings.ChangeLogColumns.MODIFIED + " INTEGER NOT NULL DEFAULT 0" + |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 440 | ");"); |
| 441 | } |
| 442 | |
Adam Cohen | 119285e | 2014-04-02 16:59:08 -0700 | [diff] [blame] | 443 | private void removeOrphanedItems(SQLiteDatabase db) { |
Adam Cohen | f9c14de | 2014-04-17 18:20:45 -0700 | [diff] [blame] | 444 | // Delete items directly on the workspace who's screen id doesn't exist |
| 445 | // "DELETE FROM favorites WHERE screen NOT IN (SELECT _id FROM workspaceScreens) |
| 446 | // AND container = -100" |
| 447 | String removeOrphanedDesktopItems = "DELETE FROM " + TABLE_FAVORITES + |
| 448 | " WHERE " + |
Adam Cohen | 119285e | 2014-04-02 16:59:08 -0700 | [diff] [blame] | 449 | LauncherSettings.Favorites.SCREEN + " NOT IN (SELECT " + |
Adam Cohen | f9c14de | 2014-04-17 18:20:45 -0700 | [diff] [blame] | 450 | LauncherSettings.WorkspaceScreens._ID + " FROM " + TABLE_WORKSPACE_SCREENS + ")" + |
| 451 | " AND " + |
| 452 | LauncherSettings.Favorites.CONTAINER + " = " + |
| 453 | LauncherSettings.Favorites.CONTAINER_DESKTOP; |
| 454 | db.execSQL(removeOrphanedDesktopItems); |
| 455 | |
| 456 | // Delete items contained in folders which no longer exist (after above statement) |
| 457 | // "DELETE FROM favorites WHERE container <> -100 AND container <> -101 AND container |
| 458 | // NOT IN (SELECT _id FROM favorites WHERE itemType = 2)" |
| 459 | String removeOrphanedFolderItems = "DELETE FROM " + TABLE_FAVORITES + |
| 460 | " WHERE " + |
| 461 | LauncherSettings.Favorites.CONTAINER + " <> " + |
| 462 | LauncherSettings.Favorites.CONTAINER_DESKTOP + |
| 463 | " AND " |
| 464 | + LauncherSettings.Favorites.CONTAINER + " <> " + |
| 465 | LauncherSettings.Favorites.CONTAINER_HOTSEAT + |
| 466 | " AND " |
| 467 | + LauncherSettings.Favorites.CONTAINER + " NOT IN (SELECT " + |
| 468 | LauncherSettings.Favorites._ID + " FROM " + TABLE_FAVORITES + |
| 469 | " WHERE " + LauncherSettings.Favorites.ITEM_TYPE + " = " + |
| 470 | LauncherSettings.Favorites.ITEM_TYPE_FOLDER + ")"; |
| 471 | db.execSQL(removeOrphanedFolderItems); |
Adam Cohen | 119285e | 2014-04-02 16:59:08 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 474 | private void setFlagJustLoadedOldDb() { |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 475 | String spKey = LauncherAppState.getSharedPreferencesKey(); |
Michael Jurka | b85f8a4 | 2012-04-25 15:48:32 -0700 | [diff] [blame] | 476 | SharedPreferences sp = mContext.getSharedPreferences(spKey, Context.MODE_PRIVATE); |
| 477 | SharedPreferences.Editor editor = sp.edit(); |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 478 | editor.putBoolean(UPGRADED_FROM_OLD_DATABASE, true); |
| 479 | editor.putBoolean(EMPTY_DATABASE_CREATED, false); |
Michael Jurka | b85f8a4 | 2012-04-25 15:48:32 -0700 | [diff] [blame] | 480 | editor.commit(); |
| 481 | } |
| 482 | |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 483 | private void setFlagEmptyDbCreated() { |
| 484 | String spKey = LauncherAppState.getSharedPreferencesKey(); |
| 485 | SharedPreferences sp = mContext.getSharedPreferences(spKey, Context.MODE_PRIVATE); |
| 486 | SharedPreferences.Editor editor = sp.edit(); |
| 487 | editor.putBoolean(EMPTY_DATABASE_CREATED, true); |
| 488 | editor.putBoolean(UPGRADED_FROM_OLD_DATABASE, false); |
| 489 | editor.commit(); |
| 490 | } |
| 491 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 492 | @Override |
| 493 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 494 | if (LOGD) Log.d(TAG, "onUpgrade triggered: " + oldVersion); |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 495 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 496 | int version = oldVersion; |
Sunny Goyal | e87e6ab | 2014-11-21 22:42:53 -0800 | [diff] [blame] | 497 | if (version < MIN_DATABASE_VERSION) { |
| 498 | // The version cannot be lower that this, as Launcher3 never supported a lower |
| 499 | // version of the DB. |
| 500 | createEmptyDB(db); |
| 501 | version = DATABASE_VERSION; |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 504 | if (version < 13) { |
| 505 | // With the new shrink-wrapped and re-orderable workspaces, it makes sense |
| 506 | // to persist workspace screens and their relative order. |
| 507 | mMaxScreenId = 0; |
| 508 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 509 | addWorkspacesTable(db); |
| 510 | version = 13; |
| 511 | } |
| 512 | |
Chris Wren | d5e66bf | 2013-09-16 14:02:29 -0400 | [diff] [blame] | 513 | if (version < 14) { |
| 514 | db.beginTransaction(); |
| 515 | try { |
| 516 | // Insert new column for holding widget provider name |
| 517 | db.execSQL("ALTER TABLE favorites " + |
| 518 | "ADD COLUMN appWidgetProvider TEXT;"); |
| 519 | db.setTransactionSuccessful(); |
| 520 | version = 14; |
| 521 | } catch (SQLException ex) { |
| 522 | // Old version remains, which means we wipe old data |
| 523 | Log.e(TAG, ex.getMessage(), ex); |
| 524 | } finally { |
| 525 | db.endTransaction(); |
| 526 | } |
| 527 | } |
| 528 | |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 529 | if (version < 15) { |
| 530 | db.beginTransaction(); |
| 531 | try { |
| 532 | // Insert new column for holding update timestamp |
| 533 | db.execSQL("ALTER TABLE favorites " + |
| 534 | "ADD COLUMN modified INTEGER NOT NULL DEFAULT 0;"); |
| 535 | db.execSQL("ALTER TABLE workspaceScreens " + |
| 536 | "ADD COLUMN modified INTEGER NOT NULL DEFAULT 0;"); |
| 537 | db.setTransactionSuccessful(); |
| 538 | version = 15; |
| 539 | } catch (SQLException ex) { |
| 540 | // Old version remains, which means we wipe old data |
| 541 | Log.e(TAG, ex.getMessage(), ex); |
| 542 | } finally { |
| 543 | db.endTransaction(); |
| 544 | } |
| 545 | } |
| 546 | |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 547 | |
| 548 | if (version < 16) { |
| 549 | db.beginTransaction(); |
| 550 | try { |
| 551 | // Insert new column for holding restore status |
| 552 | db.execSQL("ALTER TABLE favorites " + |
| 553 | "ADD COLUMN restored INTEGER NOT NULL DEFAULT 0;"); |
| 554 | db.setTransactionSuccessful(); |
| 555 | version = 16; |
| 556 | } catch (SQLException ex) { |
| 557 | // Old version remains, which means we wipe old data |
| 558 | Log.e(TAG, ex.getMessage(), ex); |
| 559 | } finally { |
| 560 | db.endTransaction(); |
| 561 | } |
| 562 | } |
| 563 | |
Adam Cohen | 71e03b9 | 2014-02-21 14:09:53 -0800 | [diff] [blame] | 564 | if (version < 17) { |
| 565 | // We use the db version upgrade here to identify users who may not have seen |
| 566 | // clings yet (because they weren't available), but for whom the clings are now |
| 567 | // available (tablet users). Because one of the possible cling flows (migration) |
| 568 | // is very destructive (wipes out workspaces), we want to prevent this from showing |
| 569 | // until clear data. We do so by marking that the clings have been shown. |
| 570 | LauncherClings.synchonouslyMarkFirstRunClingDismissed(mContext); |
| 571 | version = 17; |
| 572 | } |
| 573 | |
Adam Cohen | 119285e | 2014-04-02 16:59:08 -0700 | [diff] [blame] | 574 | if (version < 18) { |
Adam Cohen | f9c14de | 2014-04-17 18:20:45 -0700 | [diff] [blame] | 575 | // No-op |
| 576 | version = 18; |
| 577 | } |
| 578 | |
| 579 | if (version < 19) { |
Adam Cohen | 119285e | 2014-04-02 16:59:08 -0700 | [diff] [blame] | 580 | // Due to a data loss bug, some users may have items associated with screen ids |
| 581 | // which no longer exist. Since this can cause other problems, and since the user |
| 582 | // will never see these items anyway, we use database upgrade as an opportunity to |
| 583 | // clean things up. |
Adam Cohen | f9c14de | 2014-04-17 18:20:45 -0700 | [diff] [blame] | 584 | removeOrphanedItems(db); |
| 585 | version = 19; |
Adam Cohen | 119285e | 2014-04-02 16:59:08 -0700 | [diff] [blame] | 586 | } |
| 587 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 588 | if (version < 20) { |
| 589 | // Add userId column |
| 590 | if (addProfileColumn(db)) { |
| 591 | version = 20; |
| 592 | } |
| 593 | // else old version remains, which means we wipe old data |
| 594 | } |
| 595 | |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 596 | if (version < 21) { |
| 597 | if (updateFolderItemsRank(db, true)) { |
| 598 | version = 21; |
| 599 | } |
| 600 | } |
| 601 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 602 | if (version != DATABASE_VERSION) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 603 | Log.w(TAG, "Destroying all old data."); |
Sunny Goyal | e87e6ab | 2014-11-21 22:42:53 -0800 | [diff] [blame] | 604 | createEmptyDB(db); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 605 | } |
| 606 | } |
Romain Guy | 7eb9e5e | 2009-12-02 20:10:07 -0800 | [diff] [blame] | 607 | |
Adam Cohen | 9b1d062 | 2014-05-21 19:01:57 -0700 | [diff] [blame] | 608 | @Override |
| 609 | public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
| 610 | // This shouldn't happen -- throw our hands up in the air and start over. |
| 611 | Log.w(TAG, "Database version downgrade from: " + oldVersion + " to " + newVersion + |
| 612 | ". Wiping databse."); |
Sunny Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 613 | createEmptyDB(db); |
| 614 | } |
Adam Cohen | 9b1d062 | 2014-05-21 19:01:57 -0700 | [diff] [blame] | 615 | |
Sunny Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 616 | |
| 617 | /** |
| 618 | * Clears all the data for a fresh start. |
| 619 | */ |
| 620 | public void createEmptyDB(SQLiteDatabase db) { |
Adam Cohen | 9b1d062 | 2014-05-21 19:01:57 -0700 | [diff] [blame] | 621 | db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES); |
| 622 | db.execSQL("DROP TABLE IF EXISTS " + TABLE_WORKSPACE_SCREENS); |
| 623 | onCreate(db); |
| 624 | } |
| 625 | |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 626 | private boolean updateFolderItemsRank(SQLiteDatabase db, boolean addRankColumn) { |
| 627 | db.beginTransaction(); |
| 628 | try { |
| 629 | if (addRankColumn) { |
| 630 | // Insert new column for holding rank |
| 631 | db.execSQL("ALTER TABLE favorites ADD COLUMN rank INTEGER NOT NULL DEFAULT 0;"); |
| 632 | } |
| 633 | |
| 634 | // Get a map for folder ID to folder width |
| 635 | Cursor c = db.rawQuery("SELECT container, MAX(cellX) FROM favorites" |
| 636 | + " WHERE container IN (SELECT _id FROM favorites WHERE itemType = ?)" |
| 637 | + " GROUP BY container;", |
| 638 | new String[] {Integer.toString(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)}); |
| 639 | |
| 640 | while (c.moveToNext()) { |
| 641 | db.execSQL("UPDATE favorites SET rank=cellX+(cellY*?) WHERE container=?;", |
| 642 | new Object[] {c.getLong(1) + 1, c.getLong(0)}); |
| 643 | } |
| 644 | |
| 645 | c.close(); |
| 646 | db.setTransactionSuccessful(); |
| 647 | } catch (SQLException ex) { |
| 648 | // Old version remains, which means we wipe old data |
| 649 | Log.e(TAG, ex.getMessage(), ex); |
| 650 | return false; |
| 651 | } finally { |
| 652 | db.endTransaction(); |
| 653 | } |
| 654 | return true; |
| 655 | } |
| 656 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 657 | private boolean addProfileColumn(SQLiteDatabase db) { |
| 658 | db.beginTransaction(); |
| 659 | try { |
| 660 | UserManagerCompat userManager = UserManagerCompat.getInstance(mContext); |
| 661 | // Default to the serial number of this user, for older |
| 662 | // shortcuts. |
| 663 | long userSerialNumber = userManager.getSerialNumberForUser( |
| 664 | UserHandleCompat.myUserHandle()); |
| 665 | // Insert new column for holding user serial number |
| 666 | db.execSQL("ALTER TABLE favorites " + |
| 667 | "ADD COLUMN profileId INTEGER DEFAULT " |
| 668 | + userSerialNumber + ";"); |
| 669 | db.setTransactionSuccessful(); |
| 670 | } catch (SQLException ex) { |
| 671 | // Old version remains, which means we wipe old data |
| 672 | Log.e(TAG, ex.getMessage(), ex); |
| 673 | return false; |
| 674 | } finally { |
| 675 | db.endTransaction(); |
| 676 | } |
| 677 | return true; |
| 678 | } |
| 679 | |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 680 | // Generates a new ID to use for an object in your database. This method should be only |
| 681 | // called from the main UI thread. As an exception, we do call it when we call the |
| 682 | // constructor from the worker thread; however, this doesn't extend until after the |
| 683 | // constructor is called, and we only pass a reference to LauncherProvider to LauncherApp |
| 684 | // after that point |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 685 | @Override |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 686 | public long generateNewItemId() { |
| 687 | if (mMaxItemId < 0) { |
| 688 | throw new RuntimeException("Error: max item id was not initialized"); |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 689 | } |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 690 | mMaxItemId += 1; |
| 691 | return mMaxItemId; |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 692 | } |
| 693 | |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 694 | @Override |
| 695 | public long insertAndCheck(SQLiteDatabase db, ContentValues values) { |
| 696 | return dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values); |
| 697 | } |
| 698 | |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 699 | public void updateMaxItemId(long id) { |
| 700 | mMaxItemId = id + 1; |
| 701 | } |
| 702 | |
Chris Wren | 5dee7af | 2013-12-20 17:22:11 -0500 | [diff] [blame] | 703 | public void checkId(String table, ContentValues values) { |
| 704 | long id = values.getAsLong(LauncherSettings.BaseLauncherColumns._ID); |
| 705 | if (table == LauncherProvider.TABLE_WORKSPACE_SCREENS) { |
| 706 | mMaxScreenId = Math.max(id, mMaxScreenId); |
| 707 | } else { |
| 708 | mMaxItemId = Math.max(id, mMaxItemId); |
| 709 | } |
| 710 | } |
| 711 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 712 | private long initializeMaxItemId(SQLiteDatabase db) { |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 713 | Cursor c = db.rawQuery("SELECT MAX(_id) FROM favorites", null); |
| 714 | |
| 715 | // get the result |
| 716 | final int maxIdIndex = 0; |
| 717 | long id = -1; |
| 718 | if (c != null && c.moveToNext()) { |
| 719 | id = c.getLong(maxIdIndex); |
| 720 | } |
Michael Jurka | 5130e40 | 2011-10-13 04:55:35 -0700 | [diff] [blame] | 721 | if (c != null) { |
| 722 | c.close(); |
| 723 | } |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 724 | |
| 725 | if (id == -1) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 726 | throw new RuntimeException("Error: could not query max item id"); |
| 727 | } |
| 728 | |
| 729 | return id; |
| 730 | } |
| 731 | |
| 732 | // Generates a new ID to use for an workspace screen in your database. This method |
| 733 | // should be only called from the main UI thread. As an exception, we do call it when we |
| 734 | // call the constructor from the worker thread; however, this doesn't extend until after the |
| 735 | // constructor is called, and we only pass a reference to LauncherProvider to LauncherApp |
| 736 | // after that point |
| 737 | public long generateNewScreenId() { |
| 738 | if (mMaxScreenId < 0) { |
| 739 | throw new RuntimeException("Error: max screen id was not initialized"); |
| 740 | } |
| 741 | mMaxScreenId += 1; |
Winson Chung | a90303b | 2013-11-15 13:05:06 -0800 | [diff] [blame] | 742 | // Log to disk |
| 743 | Launcher.addDumpLog(TAG, "11683562 - generateNewScreenId(): " + mMaxScreenId, true); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 744 | return mMaxScreenId; |
| 745 | } |
| 746 | |
| 747 | public void updateMaxScreenId(long maxScreenId) { |
Winson Chung | a90303b | 2013-11-15 13:05:06 -0800 | [diff] [blame] | 748 | // Log to disk |
| 749 | Launcher.addDumpLog(TAG, "11683562 - updateMaxScreenId(): " + maxScreenId, true); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 750 | mMaxScreenId = maxScreenId; |
| 751 | } |
| 752 | |
| 753 | private long initializeMaxScreenId(SQLiteDatabase db) { |
| 754 | Cursor c = db.rawQuery("SELECT MAX(" + LauncherSettings.WorkspaceScreens._ID + ") FROM " + TABLE_WORKSPACE_SCREENS, null); |
| 755 | |
| 756 | // get the result |
| 757 | final int maxIdIndex = 0; |
| 758 | long id = -1; |
| 759 | if (c != null && c.moveToNext()) { |
| 760 | id = c.getLong(maxIdIndex); |
| 761 | } |
| 762 | if (c != null) { |
| 763 | c.close(); |
| 764 | } |
| 765 | |
| 766 | if (id == -1) { |
| 767 | throw new RuntimeException("Error: could not query max screen id"); |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 768 | } |
| 769 | |
Winson Chung | a90303b | 2013-11-15 13:05:06 -0800 | [diff] [blame] | 770 | // Log to disk |
| 771 | Launcher.addDumpLog(TAG, "11683562 - initializeMaxScreenId(): " + id, true); |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 772 | return id; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 773 | } |
| 774 | |
Adam Cohen | a043fa8 | 2014-07-23 14:49:38 -0700 | [diff] [blame] | 775 | private boolean initializeExternalAdd(ContentValues values) { |
| 776 | // 1. Ensure that externally added items have a valid item id |
| 777 | long id = generateNewItemId(); |
| 778 | values.put(LauncherSettings.Favorites._ID, id); |
| 779 | |
| 780 | // 2. In the case of an app widget, and if no app widget id is specified, we |
| 781 | // attempt allocate and bind the widget. |
| 782 | Integer itemType = values.getAsInteger(LauncherSettings.Favorites.ITEM_TYPE); |
| 783 | if (itemType != null && |
| 784 | itemType.intValue() == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET && |
| 785 | !values.containsKey(LauncherSettings.Favorites.APPWIDGET_ID)) { |
| 786 | |
| 787 | final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext); |
| 788 | ComponentName cn = ComponentName.unflattenFromString( |
| 789 | values.getAsString(Favorites.APPWIDGET_PROVIDER)); |
| 790 | |
| 791 | if (cn != null) { |
| 792 | try { |
| 793 | int appWidgetId = mAppWidgetHost.allocateAppWidgetId(); |
Adam Cohen | 3ed316a | 2014-07-23 18:21:20 -0700 | [diff] [blame] | 794 | values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId); |
Adam Cohen | eb1ac42 | 2014-10-14 08:55:28 -0700 | [diff] [blame] | 795 | if (!appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,cn)) { |
Adam Cohen | d61a638 | 2014-10-14 15:04:34 -0700 | [diff] [blame] | 796 | return false; |
Adam Cohen | a043fa8 | 2014-07-23 14:49:38 -0700 | [diff] [blame] | 797 | } |
| 798 | } catch (RuntimeException e) { |
| 799 | Log.e(TAG, "Failed to initialize external widget", e); |
Adam Cohen | eb1ac42 | 2014-10-14 08:55:28 -0700 | [diff] [blame] | 800 | return false; |
Adam Cohen | a043fa8 | 2014-07-23 14:49:38 -0700 | [diff] [blame] | 801 | } |
Adam Cohen | eb1ac42 | 2014-10-14 08:55:28 -0700 | [diff] [blame] | 802 | } else { |
| 803 | return false; |
Adam Cohen | a043fa8 | 2014-07-23 14:49:38 -0700 | [diff] [blame] | 804 | } |
| 805 | } |
Adam Cohen | 7ec3bbf | 2014-07-31 00:09:45 -0700 | [diff] [blame] | 806 | |
| 807 | // Add screen id if not present |
| 808 | long screenId = values.getAsLong(LauncherSettings.Favorites.SCREEN); |
| 809 | if (!addScreenIdIfNecessary(screenId)) { |
| 810 | return false; |
| 811 | } |
Adam Cohen | a043fa8 | 2014-07-23 14:49:38 -0700 | [diff] [blame] | 812 | return true; |
| 813 | } |
| 814 | |
Adam Cohen | 7ec3bbf | 2014-07-31 00:09:45 -0700 | [diff] [blame] | 815 | // Returns true of screen id exists, or if successfully added |
| 816 | private boolean addScreenIdIfNecessary(long screenId) { |
| 817 | if (!hasScreenId(screenId)) { |
| 818 | int rank = getMaxScreenRank() + 1; |
| 819 | |
| 820 | ContentValues v = new ContentValues(); |
| 821 | v.put(LauncherSettings.WorkspaceScreens._ID, screenId); |
| 822 | v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, rank); |
| 823 | if (dbInsertAndCheck(this, getWritableDatabase(), |
| 824 | TABLE_WORKSPACE_SCREENS, null, v) < 0) { |
| 825 | return false; |
| 826 | } |
| 827 | } |
| 828 | return true; |
| 829 | } |
| 830 | |
| 831 | private boolean hasScreenId(long screenId) { |
| 832 | SQLiteDatabase db = getWritableDatabase(); |
| 833 | Cursor c = db.rawQuery("SELECT * FROM " + TABLE_WORKSPACE_SCREENS + " WHERE " |
| 834 | + LauncherSettings.WorkspaceScreens._ID + " = " + screenId, null); |
| 835 | if (c != null) { |
| 836 | int count = c.getCount(); |
| 837 | c.close(); |
| 838 | return count > 0; |
| 839 | } else { |
| 840 | return false; |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | private int getMaxScreenRank() { |
| 845 | SQLiteDatabase db = getWritableDatabase(); |
| 846 | Cursor c = db.rawQuery("SELECT MAX(" + LauncherSettings.WorkspaceScreens.SCREEN_RANK |
| 847 | + ") FROM " + TABLE_WORKSPACE_SCREENS, null); |
| 848 | |
| 849 | // get the result |
| 850 | final int maxRankIndex = 0; |
| 851 | int rank = -1; |
| 852 | if (c != null && c.moveToNext()) { |
| 853 | rank = c.getInt(maxRankIndex); |
| 854 | } |
| 855 | if (c != null) { |
| 856 | c.close(); |
| 857 | } |
| 858 | |
| 859 | return rank; |
| 860 | } |
| 861 | |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 862 | private int loadFavorites(SQLiteDatabase db, AutoInstallsLayout loader) { |
Adam Cohen | 71483f4 | 2014-05-15 14:04:01 -0700 | [diff] [blame] | 863 | ArrayList<Long> screenIds = new ArrayList<Long>(); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 864 | // TODO: Use multiple loaders with fall-back and transaction. |
| 865 | int count = loader.loadLayout(db, screenIds); |
Adam Cohen | 71483f4 | 2014-05-15 14:04:01 -0700 | [diff] [blame] | 866 | |
| 867 | // Add the screens specified by the items above |
| 868 | Collections.sort(screenIds); |
| 869 | int rank = 0; |
| 870 | ContentValues values = new ContentValues(); |
| 871 | for (Long id : screenIds) { |
| 872 | values.clear(); |
| 873 | values.put(LauncherSettings.WorkspaceScreens._ID, id); |
| 874 | values.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, rank); |
| 875 | if (dbInsertAndCheck(this, db, TABLE_WORKSPACE_SCREENS, null, values) < 0) { |
| 876 | throw new RuntimeException("Failed initialize screen table" |
| 877 | + "from default layout"); |
| 878 | } |
| 879 | rank++; |
| 880 | } |
| 881 | |
| 882 | // Ensure that the max ids are initialized |
| 883 | mMaxItemId = initializeMaxItemId(db); |
| 884 | mMaxScreenId = initializeMaxScreenId(db); |
Adam Cohen | 7ec3bbf | 2014-07-31 00:09:45 -0700 | [diff] [blame] | 885 | |
Adam Cohen | 71483f4 | 2014-05-15 14:04:01 -0700 | [diff] [blame] | 886 | return count; |
| 887 | } |
| 888 | |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 889 | private void migrateLauncher2Shortcuts(SQLiteDatabase db, Uri uri) { |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 890 | final ContentResolver resolver = mContext.getContentResolver(); |
| 891 | Cursor c = null; |
| 892 | int count = 0; |
| 893 | int curScreen = 0; |
| 894 | |
| 895 | try { |
| 896 | c = resolver.query(uri, null, null, null, "title ASC"); |
| 897 | } catch (Exception e) { |
| 898 | // Ignore |
| 899 | } |
| 900 | |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 901 | // We already have a favorites database in the old provider |
| 902 | if (c != null) { |
| 903 | try { |
| 904 | if (c.getCount() > 0) { |
| 905 | final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID); |
| 906 | final int intentIndex |
| 907 | = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT); |
| 908 | final int titleIndex |
| 909 | = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE); |
| 910 | final int iconTypeIndex |
| 911 | = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE); |
| 912 | final int iconIndex |
| 913 | = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON); |
| 914 | final int iconPackageIndex |
| 915 | = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE); |
| 916 | final int iconResourceIndex |
| 917 | = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE); |
| 918 | final int containerIndex |
| 919 | = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER); |
| 920 | final int itemTypeIndex |
| 921 | = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE); |
| 922 | final int screenIndex |
| 923 | = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN); |
| 924 | final int cellXIndex |
| 925 | = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX); |
| 926 | final int cellYIndex |
| 927 | = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY); |
| 928 | final int uriIndex |
| 929 | = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI); |
| 930 | final int displayModeIndex |
| 931 | = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 932 | final int profileIndex |
| 933 | = c.getColumnIndex(LauncherSettings.Favorites.PROFILE_ID); |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 934 | |
| 935 | int i = 0; |
| 936 | int curX = 0; |
| 937 | int curY = 0; |
| 938 | |
| 939 | final LauncherAppState app = LauncherAppState.getInstance(); |
| 940 | final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
| 941 | final int width = (int) grid.numColumns; |
| 942 | final int height = (int) grid.numRows; |
| 943 | final int hotseatWidth = (int) grid.numHotseatIcons; |
| 944 | |
| 945 | final HashSet<String> seenIntents = new HashSet<String>(c.getCount()); |
| 946 | |
Adam Cohen | 7296097 | 2014-01-15 18:13:55 -0800 | [diff] [blame] | 947 | final ArrayList<ContentValues> shortcuts = new ArrayList<ContentValues>(); |
| 948 | final ArrayList<ContentValues> folders = new ArrayList<ContentValues>(); |
Dan Sandler | ab5fa3a | 2014-03-06 23:48:04 -0500 | [diff] [blame] | 949 | final SparseArray<ContentValues> hotseat = new SparseArray<ContentValues>(); |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 950 | |
| 951 | while (c.moveToNext()) { |
| 952 | final int itemType = c.getInt(itemTypeIndex); |
| 953 | if (itemType != Favorites.ITEM_TYPE_APPLICATION |
| 954 | && itemType != Favorites.ITEM_TYPE_SHORTCUT |
| 955 | && itemType != Favorites.ITEM_TYPE_FOLDER) { |
| 956 | continue; |
| 957 | } |
| 958 | |
| 959 | final int cellX = c.getInt(cellXIndex); |
| 960 | final int cellY = c.getInt(cellYIndex); |
| 961 | final int screen = c.getInt(screenIndex); |
| 962 | int container = c.getInt(containerIndex); |
| 963 | final String intentStr = c.getString(intentIndex); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 964 | |
| 965 | UserManagerCompat userManager = UserManagerCompat.getInstance(mContext); |
| 966 | UserHandleCompat userHandle; |
| 967 | final long userSerialNumber; |
| 968 | if (profileIndex != -1 && !c.isNull(profileIndex)) { |
| 969 | userSerialNumber = c.getInt(profileIndex); |
| 970 | userHandle = userManager.getUserForSerialNumber(userSerialNumber); |
| 971 | } else { |
| 972 | // Default to the serial number of this user, for older |
| 973 | // shortcuts. |
| 974 | userHandle = UserHandleCompat.myUserHandle(); |
| 975 | userSerialNumber = userManager.getSerialNumberForUser(userHandle); |
| 976 | } |
Sunny Goyal | 416541c | 2014-11-14 11:59:57 -0800 | [diff] [blame] | 977 | |
| 978 | if (userHandle == null) { |
| 979 | Launcher.addDumpLog(TAG, "skipping deleted user", true); |
| 980 | continue; |
| 981 | } |
| 982 | |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 983 | Launcher.addDumpLog(TAG, "migrating \"" |
Dan Sandler | ab5fa3a | 2014-03-06 23:48:04 -0500 | [diff] [blame] | 984 | + c.getString(titleIndex) + "\" (" |
| 985 | + cellX + "," + cellY + "@" |
| 986 | + LauncherSettings.Favorites.containerToString(container) |
| 987 | + "/" + screen |
| 988 | + "): " + intentStr, true); |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 989 | |
| 990 | if (itemType != Favorites.ITEM_TYPE_FOLDER) { |
Adam Cohen | 556f613 | 2014-01-15 15:18:08 -0800 | [diff] [blame] | 991 | |
| 992 | final Intent intent; |
| 993 | final ComponentName cn; |
| 994 | try { |
| 995 | intent = Intent.parseUri(intentStr, 0); |
| 996 | } catch (URISyntaxException e) { |
| 997 | // bogus intent? |
| 998 | Launcher.addDumpLog(TAG, |
| 999 | "skipping invalid intent uri", true); |
| 1000 | continue; |
| 1001 | } |
| 1002 | |
| 1003 | cn = intent.getComponent(); |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 1004 | if (TextUtils.isEmpty(intentStr)) { |
| 1005 | // no intent? no icon |
| 1006 | Launcher.addDumpLog(TAG, "skipping empty intent", true); |
| 1007 | continue; |
Adam Cohen | 7296097 | 2014-01-15 18:13:55 -0800 | [diff] [blame] | 1008 | } else if (cn != null && |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1009 | !LauncherModel.isValidPackageActivity(mContext, cn, |
| 1010 | userHandle)) { |
Adam Cohen | 556f613 | 2014-01-15 15:18:08 -0800 | [diff] [blame] | 1011 | // component no longer exists. |
Adam Cohen | 7296097 | 2014-01-15 18:13:55 -0800 | [diff] [blame] | 1012 | Launcher.addDumpLog(TAG, "skipping item whose component " + |
Adam Cohen | 556f613 | 2014-01-15 15:18:08 -0800 | [diff] [blame] | 1013 | "no longer exists.", true); |
| 1014 | continue; |
Adam Cohen | 7296097 | 2014-01-15 18:13:55 -0800 | [diff] [blame] | 1015 | } else if (container == |
| 1016 | LauncherSettings.Favorites.CONTAINER_DESKTOP) { |
| 1017 | // Dedupe icons directly on the workspace |
| 1018 | |
Adam Cohen | 556f613 | 2014-01-15 15:18:08 -0800 | [diff] [blame] | 1019 | // Canonicalize |
| 1020 | // the Play Store sets the package parameter, but Launcher |
Adam Cohen | a33f11e | 2014-10-24 12:20:20 -0700 | [diff] [blame] | 1021 | // does not, so we clear that out to keep them the same. |
| 1022 | // Also ignore intent flags for the purposes of deduping. |
Adam Cohen | 556f613 | 2014-01-15 15:18:08 -0800 | [diff] [blame] | 1023 | intent.setPackage(null); |
Adam Cohen | a33f11e | 2014-10-24 12:20:20 -0700 | [diff] [blame] | 1024 | int flags = intent.getFlags(); |
| 1025 | intent.setFlags(0); |
Adam Cohen | 556f613 | 2014-01-15 15:18:08 -0800 | [diff] [blame] | 1026 | final String key = intent.toUri(0); |
Adam Cohen | a33f11e | 2014-10-24 12:20:20 -0700 | [diff] [blame] | 1027 | intent.setFlags(flags); |
Adam Cohen | 556f613 | 2014-01-15 15:18:08 -0800 | [diff] [blame] | 1028 | if (seenIntents.contains(key)) { |
| 1029 | Launcher.addDumpLog(TAG, "skipping duplicate", true); |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 1030 | continue; |
Adam Cohen | 556f613 | 2014-01-15 15:18:08 -0800 | [diff] [blame] | 1031 | } else { |
| 1032 | seenIntents.add(key); |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 1033 | } |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | ContentValues values = new ContentValues(c.getColumnCount()); |
| 1038 | values.put(LauncherSettings.Favorites._ID, c.getInt(idIndex)); |
| 1039 | values.put(LauncherSettings.Favorites.INTENT, intentStr); |
| 1040 | values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex)); |
| 1041 | values.put(LauncherSettings.Favorites.ICON_TYPE, |
| 1042 | c.getInt(iconTypeIndex)); |
| 1043 | values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex)); |
| 1044 | values.put(LauncherSettings.Favorites.ICON_PACKAGE, |
| 1045 | c.getString(iconPackageIndex)); |
| 1046 | values.put(LauncherSettings.Favorites.ICON_RESOURCE, |
| 1047 | c.getString(iconResourceIndex)); |
| 1048 | values.put(LauncherSettings.Favorites.ITEM_TYPE, itemType); |
| 1049 | values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1); |
| 1050 | values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex)); |
| 1051 | values.put(LauncherSettings.Favorites.DISPLAY_MODE, |
| 1052 | c.getInt(displayModeIndex)); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 1053 | values.put(LauncherSettings.Favorites.PROFILE_ID, userSerialNumber); |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 1054 | |
Dan Sandler | ab5fa3a | 2014-03-06 23:48:04 -0500 | [diff] [blame] | 1055 | if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) { |
| 1056 | hotseat.put(screen, values); |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) { |
| 1060 | // In a folder or in the hotseat, preserve position |
| 1061 | values.put(LauncherSettings.Favorites.SCREEN, screen); |
| 1062 | values.put(LauncherSettings.Favorites.CELLX, cellX); |
| 1063 | values.put(LauncherSettings.Favorites.CELLY, cellY); |
| 1064 | } else { |
Adam Cohen | 7296097 | 2014-01-15 18:13:55 -0800 | [diff] [blame] | 1065 | // For items contained directly on one of the workspace screen, |
| 1066 | // we'll determine their location (screen, x, y) in a second pass. |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | values.put(LauncherSettings.Favorites.CONTAINER, container); |
| 1070 | |
Adam Cohen | 7296097 | 2014-01-15 18:13:55 -0800 | [diff] [blame] | 1071 | if (itemType != Favorites.ITEM_TYPE_FOLDER) { |
| 1072 | shortcuts.add(values); |
| 1073 | } else { |
| 1074 | folders.add(values); |
| 1075 | } |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 1076 | } |
| 1077 | |
Dan Sandler | ab5fa3a | 2014-03-06 23:48:04 -0500 | [diff] [blame] | 1078 | // Now that we have all the hotseat icons, let's go through them left-right |
| 1079 | // and assign valid locations for them in the new hotseat |
| 1080 | final int N = hotseat.size(); |
| 1081 | for (int idx=0; idx<N; idx++) { |
| 1082 | int hotseatX = hotseat.keyAt(idx); |
| 1083 | ContentValues values = hotseat.valueAt(idx); |
| 1084 | |
| 1085 | if (hotseatX == grid.hotseatAllAppsRank) { |
| 1086 | // let's drop this in the next available hole in the hotseat |
| 1087 | while (++hotseatX < hotseatWidth) { |
| 1088 | if (hotseat.get(hotseatX) == null) { |
| 1089 | // found a spot! move it here |
| 1090 | values.put(LauncherSettings.Favorites.SCREEN, |
| 1091 | hotseatX); |
| 1092 | break; |
| 1093 | } |
| 1094 | } |
| 1095 | } |
| 1096 | if (hotseatX >= hotseatWidth) { |
| 1097 | // no room for you in the hotseat? it's off to the desktop with you |
| 1098 | values.put(LauncherSettings.Favorites.CONTAINER, |
| 1099 | Favorites.CONTAINER_DESKTOP); |
| 1100 | } |
| 1101 | } |
| 1102 | |
Adam Cohen | 7296097 | 2014-01-15 18:13:55 -0800 | [diff] [blame] | 1103 | final ArrayList<ContentValues> allItems = new ArrayList<ContentValues>(); |
| 1104 | // Folders first |
| 1105 | allItems.addAll(folders); |
| 1106 | // Then shortcuts |
| 1107 | allItems.addAll(shortcuts); |
| 1108 | |
| 1109 | // Layout all the folders |
| 1110 | for (ContentValues values: allItems) { |
| 1111 | if (values.getAsInteger(LauncherSettings.Favorites.CONTAINER) != |
| 1112 | LauncherSettings.Favorites.CONTAINER_DESKTOP) { |
| 1113 | // Hotseat items and folder items have already had their |
| 1114 | // location information set. Nothing to be done here. |
| 1115 | continue; |
| 1116 | } |
| 1117 | values.put(LauncherSettings.Favorites.SCREEN, curScreen); |
| 1118 | values.put(LauncherSettings.Favorites.CELLX, curX); |
| 1119 | values.put(LauncherSettings.Favorites.CELLY, curY); |
| 1120 | curX = (curX + 1) % width; |
| 1121 | if (curX == 0) { |
| 1122 | curY = (curY + 1); |
| 1123 | } |
| 1124 | // Leave the last row of icons blank on every screen |
| 1125 | if (curY == height - 1) { |
| 1126 | curScreen = (int) generateNewScreenId(); |
| 1127 | curY = 0; |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | if (allItems.size() > 0) { |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 1132 | db.beginTransaction(); |
| 1133 | try { |
Adam Cohen | 7296097 | 2014-01-15 18:13:55 -0800 | [diff] [blame] | 1134 | for (ContentValues row: allItems) { |
| 1135 | if (row == null) continue; |
| 1136 | if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, row) |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 1137 | < 0) { |
| 1138 | return; |
| 1139 | } else { |
| 1140 | count++; |
| 1141 | } |
| 1142 | } |
| 1143 | db.setTransactionSuccessful(); |
| 1144 | } finally { |
| 1145 | db.endTransaction(); |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | db.beginTransaction(); |
| 1150 | try { |
| 1151 | for (i=0; i<=curScreen; i++) { |
| 1152 | final ContentValues values = new ContentValues(); |
| 1153 | values.put(LauncherSettings.WorkspaceScreens._ID, i); |
| 1154 | values.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i); |
| 1155 | if (dbInsertAndCheck(this, db, TABLE_WORKSPACE_SCREENS, null, values) |
| 1156 | < 0) { |
| 1157 | return; |
| 1158 | } |
| 1159 | } |
| 1160 | db.setTransactionSuccessful(); |
| 1161 | } finally { |
| 1162 | db.endTransaction(); |
| 1163 | } |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 1164 | |
| 1165 | updateFolderItemsRank(db, false); |
Dan Sandler | d502404 | 2014-01-09 15:01:33 -0500 | [diff] [blame] | 1166 | } |
| 1167 | } finally { |
| 1168 | c.close(); |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | Launcher.addDumpLog(TAG, "migrated " + count + " icons from Launcher2 into " |
| 1173 | + (curScreen+1) + " screens", true); |
| 1174 | |
| 1175 | // ensure that new screens are created to hold these icons |
| 1176 | setFlagJustLoadedOldDb(); |
| 1177 | |
| 1178 | // Update max IDs; very important since we just grabbed IDs from another database |
| 1179 | mMaxItemId = initializeMaxItemId(db); |
| 1180 | mMaxScreenId = initializeMaxScreenId(db); |
| 1181 | if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId + " mMaxScreenId: " + mMaxScreenId); |
| 1182 | } |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 1183 | } |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 1184 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1185 | static class SqlArguments { |
| 1186 | public final String table; |
| 1187 | public final String where; |
| 1188 | public final String[] args; |
| 1189 | |
| 1190 | SqlArguments(Uri url, String where, String[] args) { |
| 1191 | if (url.getPathSegments().size() == 1) { |
| 1192 | this.table = url.getPathSegments().get(0); |
| 1193 | this.where = where; |
| 1194 | this.args = args; |
| 1195 | } else if (url.getPathSegments().size() != 2) { |
| 1196 | throw new IllegalArgumentException("Invalid URI: " + url); |
| 1197 | } else if (!TextUtils.isEmpty(where)) { |
| 1198 | throw new UnsupportedOperationException("WHERE clause not supported: " + url); |
| 1199 | } else { |
| 1200 | this.table = url.getPathSegments().get(0); |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 1201 | this.where = "_id=" + ContentUris.parseId(url); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1202 | this.args = null; |
| 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | SqlArguments(Uri url) { |
| 1207 | if (url.getPathSegments().size() == 1) { |
| 1208 | table = url.getPathSegments().get(0); |
| 1209 | where = null; |
| 1210 | args = null; |
| 1211 | } else { |
| 1212 | throw new IllegalArgumentException("Invalid URI: " + url); |
| 1213 | } |
| 1214 | } |
| 1215 | } |
Adam Cohen | 7296097 | 2014-01-15 18:13:55 -0800 | [diff] [blame] | 1216 | } |