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