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