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