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