blob: f9f5ae1bbae5db8a4a0f16380e13f4ece97a3de5 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
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 Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070019import android.annotation.TargetApi;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070020import android.appwidget.AppWidgetHost;
Mike Cleronb87bd162009-10-30 16:36:56 -070021import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080022import android.content.ComponentName;
Adam Cohen228da5a2011-07-27 22:23:47 -070023import android.content.ContentProvider;
Yura085c8532014-02-11 15:15:29 +000024import android.content.ContentProviderOperation;
25import android.content.ContentProviderResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.ContentResolver;
Adam Cohen228da5a2011-07-27 22:23:47 -070027import android.content.ContentUris;
28import android.content.ContentValues;
29import android.content.Context;
30import android.content.Intent;
Yura085c8532014-02-11 15:15:29 +000031import android.content.OperationApplicationException;
Michael Jurkab85f8a42012-04-25 15:48:32 -070032import android.content.SharedPreferences;
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070033import android.content.pm.PackageManager.NameNotFoundException;
Adam Cohen228da5a2011-07-27 22:23:47 -070034import android.content.res.Resources;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.database.Cursor;
36import android.database.SQLException;
Adam Cohen228da5a2011-07-27 22:23:47 -070037import android.database.sqlite.SQLiteDatabase;
38import android.database.sqlite.SQLiteOpenHelper;
39import android.database.sqlite.SQLiteQueryBuilder;
Sunny Goyal0b037782015-04-02 10:27:03 -070040import android.database.sqlite.SQLiteStatement;
Adam Cohen228da5a2011-07-27 22:23:47 -070041import android.net.Uri;
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070042import android.os.Build;
43import android.os.Bundle;
Nilesh Agrawalfde11852015-01-21 11:50:57 -080044import android.os.StrictMode;
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070045import android.os.UserManager;
Adam Cohen228da5a2011-07-27 22:23:47 -070046import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.util.Log;
Dan Sandlerab5fa3a2014-03-06 23:48:04 -050048import android.util.SparseArray;
Adam Cohen228da5a2011-07-27 22:23:47 -070049
Sunny Goyal0fe505b2014-08-06 09:55:36 -070050import com.android.launcher3.AutoInstallsLayout.LayoutParserCallback;
51import com.android.launcher3.LauncherSettings.Favorites;
Kenny Guyed131872014-04-30 03:02:21 +010052import com.android.launcher3.compat.UserHandleCompat;
53import com.android.launcher3.compat.UserManagerCompat;
Chris Wrene523e702013-10-09 10:36:55 -040054import com.android.launcher3.config.ProviderConfig;
Adam Cohen091440a2015-03-18 14:16:05 -070055import com.android.launcher3.util.Thunk;
Michael Jurka8b805b12012-04-18 14:23:14 -070056
Dan Sandlerd5024042014-01-09 15:01:33 -050057import java.io.File;
Mike Cleronb87bd162009-10-30 16:36:56 -070058import java.net.URISyntaxException;
Adam Cohen228da5a2011-07-27 22:23:47 -070059import java.util.ArrayList;
Adam Cohen71483f42014-05-15 14:04:01 -070060import java.util.Collections;
Dan Sandlerd5024042014-01-09 15:01:33 -050061import java.util.HashSet;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063public class LauncherProvider extends ContentProvider {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080064 private static final String TAG = "Launcher.LauncherProvider";
65 private static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066
Sunny Goyal0b037782015-04-02 10:27:03 -070067 private static final int DATABASE_VERSION = 24;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068
Adam Cohene25af792013-06-06 23:08:25 -070069 static final String OLD_AUTHORITY = "com.android.launcher2.settings";
Chris Wrene523e702013-10-09 10:36:55 -040070 static final String AUTHORITY = ProviderConfig.AUTHORITY;
Winson Chung3d503fb2011-07-13 17:25:49 -070071
Sunny Goyal18b640c2015-04-17 09:24:01 -070072 static final String TABLE_FAVORITES = LauncherSettings.Favorites.TABLE_NAME;
73 static final String TABLE_WORKSPACE_SCREENS = LauncherSettings.WorkspaceScreens.TABLE_NAME;
Sunny Goyale87e6ab2014-11-21 22:42:53 -080074 static final String EMPTY_DATABASE_CREATED = "EMPTY_DATABASE_CREATED";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075
Adam Cohena043fa82014-07-23 14:49:38 -070076 private static final String URI_PARAM_IS_EXTERNAL_ADD = "isExternalAdd";
77
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070078 private static final String RESTRICTION_PACKAGE_NAME = "workspace.configuration.package.name";
79
Anjali Koppal67e7cae2014-03-13 12:14:12 -070080 private LauncherProviderChangeListener mListener;
81
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -070082 /**
Romain Guy73b979d2009-06-09 12:57:21 -070083 * {@link Uri} triggered at any registered {@link android.database.ContentObserver} when
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -070084 * {@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 Lehmannc3a80402012-04-23 21:35:11 -070089
Michael Jurkaa8c760d2011-04-28 14:59:33 -070090 private DatabaseHelper mOpenHelper;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091
92 @Override
93 public boolean onCreate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040094 final Context context = getContext();
Nilesh Agrawala258f812015-01-26 14:07:29 -080095 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
Daniel Sandlere4f98912013-06-25 15:13:26 -040096 mOpenHelper = new DatabaseHelper(context);
Nilesh Agrawalfde11852015-01-21 11:50:57 -080097 StrictMode.setThreadPolicy(oldPolicy);
Daniel Sandlere4f98912013-06-25 15:13:26 -040098 LauncherAppState.setLauncherProvider(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099 return true;
100 }
101
Winson Chung0b560dd2014-01-21 13:00:26 -0800102 public boolean wasNewDbCreated() {
103 return mOpenHelper.wasNewDbCreated();
104 }
105
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700106 public void setLauncherProviderChangeListener(LauncherProviderChangeListener listener) {
107 mListener = listener;
108 }
109
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110 @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 Guy73b979d2009-06-09 12:57:21 -0700128 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129 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 Cohen091440a2015-03-18 14:16:05 -0700135 @Thunk static long dbInsertAndCheck(DatabaseHelper helper,
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700136 SQLiteDatabase db, String table, String nullColumnHack, ContentValues values) {
Dan Sandlerd5024042014-01-09 15:01:33 -0500137 if (values == null) {
138 throw new RuntimeException("Error: attempting to insert null values");
139 }
Adam Cohen71483f42014-05-15 14:04:01 -0700140 if (!values.containsKey(LauncherSettings.ChangeLogColumns._ID)) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700141 throw new RuntimeException("Error: attempting to add item without specifying an id");
142 }
Chris Wren5dee7af2013-12-20 17:22:11 -0500143 helper.checkId(table, values);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700144 return db.insert(table, nullColumnHack, values);
145 }
146
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 @Override
148 public Uri insert(Uri uri, ContentValues initialValues) {
149 SqlArguments args = new SqlArguments(uri);
150
Adam Cohena043fa82014-07-23 14:49:38 -0700151 // 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 Goyal1d4a2df2015-03-30 11:11:46 -0700153 final boolean isExternalAll = externalAdd != null && "true".equals(externalAdd);
154 if (isExternalAll) {
Adam Cohena043fa82014-07-23 14:49:38 -0700155 if (!mOpenHelper.initializeExternalAdd(initialValues)) {
156 return null;
157 }
158 }
159
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
Chris Wren1ada10d2013-09-13 18:01:38 -0400161 addModifiedTime(initialValues);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700162 final long rowId = dbInsertAndCheck(mOpenHelper, db, args.table, null, initialValues);
Sunny Goyale72f3d52015-03-02 14:24:21 -0800163 if (rowId < 0) return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164
165 uri = ContentUris.withAppendedId(uri, rowId);
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700166 notifyListeners();
167
168 if (isExternalAll) {
169 LauncherAppState app = LauncherAppState.getInstanceNoCreate();
170 if (app != null) {
171 app.reloadWorkspace();
172 }
173 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174
175 return uri;
176 }
177
Adam Cohena043fa82014-07-23 14:49:38 -0700178
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 @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 Wren1ada10d2013-09-13 18:01:38 -0400188 addModifiedTime(values[i]);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700189 if (dbInsertAndCheck(mOpenHelper, db, args.table, null, values[i]) < 0) {
190 return 0;
191 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 }
193 db.setTransactionSuccessful();
194 } finally {
195 db.endTransaction();
196 }
197
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700198 notifyListeners();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 return values.length;
200 }
201
202 @Override
Yura085c8532014-02-11 15:15:29 +0000203 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 Project31dd5032009-03-03 19:32:27 -0800217 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 Goyal1d4a2df2015-03-30 11:11:46 -0700222 if (count > 0) notifyListeners();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223
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 Wren1ada10d2013-09-13 18:01:38 -0400231 addModifiedTime(values);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
233 int count = db.update(args.table, values, args.where, args.args);
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700234 if (count > 0) notifyListeners();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235
236 return count;
237 }
238
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700239 private void notifyListeners() {
Chris Wren1ada10d2013-09-13 18:01:38 -0400240 // always notify the backup agent
Chris Wren92aa4232013-10-04 11:29:36 -0400241 LauncherBackupAgentHelper.dataChanged(getContext());
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700242 if (mListener != null) {
243 mListener.onLauncherProviderChange();
244 }
Chris Wren1ada10d2013-09-13 18:01:38 -0400245 }
246
Adam Cohen091440a2015-03-18 14:16:05 -0700247 @Thunk static void addModifiedTime(ContentValues values) {
Chris Wren1ada10d2013-09-13 18:01:38 -0400248 values.put(LauncherSettings.ChangeLogColumns.MODIFIED, System.currentTimeMillis());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 }
250
Adam Cohendcd297f2013-06-18 13:13:40 -0700251 public long generateNewItemId() {
252 return mOpenHelper.generateNewItemId();
253 }
254
Winson Chungc763c4e2013-07-19 13:49:06 -0700255 public void updateMaxItemId(long id) {
256 mOpenHelper.updateMaxItemId(id);
257 }
258
Adam Cohendcd297f2013-06-18 13:13:40 -0700259 public long generateNewScreenId() {
260 return mOpenHelper.generateNewScreenId();
261 }
262
Brian Muramatsu5524b492012-10-02 16:55:54 -0700263 /**
Sunny Goyal42de82f2014-09-26 22:09:29 -0700264 * Clears all the data for a fresh start.
265 */
266 synchronized public void createEmptyDB() {
267 mOpenHelper.createEmptyDB(mOpenHelper.getWritableDatabase());
268 }
269
Sunny Goyal33d44382014-10-16 09:24:19 -0700270 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 Goyal42de82f2014-09-26 22:09:29 -0700278 /**
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700279 * Loads the default workspace based on the following priority scheme:
Sunny Goyalb2d46ce2015-03-26 11:32:11 -0700280 * 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 Muramatsu5524b492012-10-02 16:55:54 -0700284 */
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700285 synchronized public void loadDefaultFavoritesIfNecessary() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400286 String spKey = LauncherAppState.getSharedPreferencesKey();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700287 SharedPreferences sp = getContext().getSharedPreferences(spKey, Context.MODE_PRIVATE);
Adam Cohene25af792013-06-06 23:08:25 -0700288
Winson Chungc763c4e2013-07-19 13:49:06 -0700289 if (sp.getBoolean(EMPTY_DATABASE_CREATED, false)) {
Chris Wren5dee7af2013-12-20 17:22:11 -0500290 Log.d(TAG, "loading default workspace");
Michael Jurka45355c42012-10-08 13:21:35 +0200291
Sunny Goyalb2d46ce2015-03-26 11:32:11 -0700292 AutoInstallsLayout loader = createWorkspaceLoaderFromAppRestriction();
293 if (loader == null) {
294 loader = AutoInstallsLayout.get(getContext(),
295 mOpenHelper.mAppWidgetHost, mOpenHelper);
296 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700297 if (loader == null) {
Adam Cohen9b8f51f2014-05-30 15:34:09 -0700298 final Partner partner = Partner.get(getContext().getPackageManager());
299 if (partner != null && partner.hasDefaultLayout()) {
300 final Resources partnerRes = partner.getResources();
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700301 int workspaceResId = partnerRes.getIdentifier(Partner.RES_DEFAULT_LAYOUT,
Adam Cohen9b8f51f2014-05-30 15:34:09 -0700302 "xml", partner.getPackageName());
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700303 if (workspaceResId != 0) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700304 loader = new DefaultLayoutParser(getContext(), mOpenHelper.mAppWidgetHost,
305 mOpenHelper, partnerRes, workspaceResId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700306 }
Adam Cohen9b8f51f2014-05-30 15:34:09 -0700307 }
308 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700309
Sunny Goyal9d219682014-10-23 14:21:02 -0700310 final boolean usingExternallyProvidedLayout = loader != null;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700311 if (loader == null) {
Sunny Goyal9d219682014-10-23 14:21:02 -0700312 loader = getDefaultLayoutParser();
Brian Muramatsu5524b492012-10-02 16:55:54 -0700313 }
Sunny Goyalc6c8fef2015-03-04 09:51:18 -0800314
315 // There might be some partially restored DB items, due to buggy restore logic in
316 // previous versions of launcher.
317 createEmptyDB();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700318 // Populate favorites table with initial favorites
Sunny Goyal9d219682014-10-23 14:21:02 -0700319 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 Goyal33d44382014-10-16 09:24:19 -0700326 clearFlagEmptyDbCreated();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700327 }
328 }
329
Sunny Goyalb2d46ce2015-03-26 11:32:11 -0700330 /**
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 Goyal35ca8732015-04-06 10:45:31 -0700345 if (bundle == null) {
346 return null;
347 }
Sunny Goyalb2d46ce2015-03-26 11:32:11 -0700348
Sunny Goyal35ca8732015-04-06 10:45:31 -0700349 String packageName = bundle.getString(RESTRICTION_PACKAGE_NAME);
Sunny Goyalb2d46ce2015-03-26 11:32:11 -0700350 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 Goyal9d219682014-10-23 14:21:02 -0700364 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 Sandlerd5024042014-01-09 15:01:33 -0500371 public void migrateLauncher2Shortcuts() {
372 mOpenHelper.migrateLauncher2Shortcuts(mOpenHelper.getWritableDatabase(),
Jason Monk0bfcceb2014-03-21 15:42:06 -0400373 Uri.parse(getContext().getString(R.string.old_launcher_provider_uri)));
Dan Sandlerd5024042014-01-09 15:01:33 -0500374 }
375
Sunny Goyal08f72612015-01-05 13:41:43 -0800376 public void updateFolderItemsRank() {
377 mOpenHelper.updateFolderItemsRank(mOpenHelper.getWritableDatabase(), false);
378 }
379
Dan Sandlerd5024042014-01-09 15:01:33 -0500380 public void deleteDatabase() {
381 // Are you sure? (y/n)
382 final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
Dan Sandler2b471742014-01-21 14:14:41 -0500383 final File dbFile = new File(db.getPath());
Dan Sandlerd5024042014-01-09 15:01:33 -0500384 mOpenHelper.close();
Dan Sandler2b471742014-01-21 14:14:41 -0500385 if (dbFile.exists()) {
386 SQLiteDatabase.deleteDatabase(dbFile);
387 }
Dan Sandlerd5024042014-01-09 15:01:33 -0500388 mOpenHelper = new DatabaseHelper(getContext());
389 }
390
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700391 private static class DatabaseHelper extends SQLiteOpenHelper implements LayoutParserCallback {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 private final Context mContext;
Adam Cohen091440a2015-03-18 14:16:05 -0700393 @Thunk final AppWidgetHost mAppWidgetHost;
Adam Cohendcd297f2013-06-18 13:13:40 -0700394 private long mMaxItemId = -1;
395 private long mMaxScreenId = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396
Winson Chung0b560dd2014-01-21 13:00:26 -0800397 private boolean mNewDbCreated = false;
398
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800399 DatabaseHelper(Context context) {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100400 super(context, LauncherFiles.LAUNCHER_DB, null, DATABASE_VERSION);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 mContext = context;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700402 mAppWidgetHost = new AppWidgetHost(context, Launcher.APPWIDGET_HOST_ID);
Winson Chung3d503fb2011-07-13 17:25:49 -0700403
404 // In the case where neither onCreate nor onUpgrade gets called, we read the maxId from
405 // the DB here
Adam Cohendcd297f2013-06-18 13:13:40 -0700406 if (mMaxItemId == -1) {
407 mMaxItemId = initializeMaxItemId(getWritableDatabase());
408 }
409 if (mMaxScreenId == -1) {
410 mMaxScreenId = initializeMaxScreenId(getWritableDatabase());
Winson Chung3d503fb2011-07-13 17:25:49 -0700411 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800412 }
413
Winson Chung0b560dd2014-01-21 13:00:26 -0800414 public boolean wasNewDbCreated() {
415 return mNewDbCreated;
416 }
417
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700418 /**
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 Project31dd5032009-03-03 19:32:27 -0800429 @Override
430 public void onCreate(SQLiteDatabase db) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800431 if (LOGD) Log.d(TAG, "creating new launcher database");
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700432
Adam Cohendcd297f2013-06-18 13:13:40 -0700433 mMaxItemId = 1;
434 mMaxScreenId = 0;
Winson Chung0b560dd2014-01-21 13:00:26 -0800435 mNewDbCreated = true;
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700436
Kenny Guyed131872014-04-30 03:02:21 +0100437 UserManagerCompat userManager = UserManagerCompat.getInstance(mContext);
438 long userSerialNumber = userManager.getSerialNumberForUser(
439 UserHandleCompat.myUserHandle());
440
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 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 Projectca9475f2009-03-13 13:04:24 -0700452 "appWidgetId INTEGER NOT NULL DEFAULT -1," +
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 "isShortcut INTEGER," +
454 "iconType INTEGER," +
455 "iconPackage TEXT," +
456 "iconResource TEXT," +
457 "icon BLOB," +
458 "uri TEXT," +
Chris Wrend5e66bf2013-09-16 14:02:29 -0400459 "displayMode INTEGER," +
Chris Wren1ada10d2013-09-13 18:01:38 -0400460 "appWidgetProvider TEXT," +
Chris Wrenf4d08112014-01-16 18:13:56 -0500461 "modified INTEGER NOT NULL DEFAULT 0," +
Kenny Guyed131872014-04-30 03:02:21 +0100462 "restored INTEGER NOT NULL DEFAULT 0," +
Sunny Goyal08f72612015-01-05 13:41:43 -0800463 "profileId INTEGER DEFAULT " + userSerialNumber + "," +
Sunny Goyal5d85c442015-03-10 13:14:47 -0700464 "rank INTEGER NOT NULL DEFAULT 0," +
465 "options INTEGER NOT NULL DEFAULT 0" +
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466 ");");
Adam Cohendcd297f2013-06-18 13:13:40 -0700467 addWorkspacesTable(db);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800468
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700469 // Database was just created, so wipe any previous widgets
470 if (mAppWidgetHost != null) {
471 mAppWidgetHost.deleteHost();
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700472 sendAppWidgetResetNotify();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800473 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700474
Sunny Goyale87e6ab2014-11-21 22:42:53 -0800475 // Fresh and clean launcher DB.
476 mMaxItemId = initializeMaxItemId(db);
477 setFlagEmptyDbCreated();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800478 }
479
Adam Cohendcd297f2013-06-18 13:13:40 -0700480 private void addWorkspacesTable(SQLiteDatabase db) {
481 db.execSQL("CREATE TABLE " + TABLE_WORKSPACE_SCREENS + " (" +
Sunny Goyald2f38192015-02-25 10:46:34 -0800482 LauncherSettings.WorkspaceScreens._ID + " INTEGER PRIMARY KEY," +
Chris Wren1ada10d2013-09-13 18:01:38 -0400483 LauncherSettings.WorkspaceScreens.SCREEN_RANK + " INTEGER," +
484 LauncherSettings.ChangeLogColumns.MODIFIED + " INTEGER NOT NULL DEFAULT 0" +
Adam Cohendcd297f2013-06-18 13:13:40 -0700485 ");");
486 }
487
Adam Cohen119285e2014-04-02 16:59:08 -0700488 private void removeOrphanedItems(SQLiteDatabase db) {
Adam Cohenf9c14de2014-04-17 18:20:45 -0700489 // 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 Cohen119285e2014-04-02 16:59:08 -0700494 LauncherSettings.Favorites.SCREEN + " NOT IN (SELECT " +
Adam Cohenf9c14de2014-04-17 18:20:45 -0700495 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 Cohen119285e2014-04-02 16:59:08 -0700517 }
518
Winson Chungc763c4e2013-07-19 13:49:06 -0700519 private void setFlagJustLoadedOldDb() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400520 String spKey = LauncherAppState.getSharedPreferencesKey();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700521 SharedPreferences sp = mContext.getSharedPreferences(spKey, Context.MODE_PRIVATE);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800522 sp.edit().putBoolean(EMPTY_DATABASE_CREATED, false).commit();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700523 }
524
Winson Chungc763c4e2013-07-19 13:49:06 -0700525 private void setFlagEmptyDbCreated() {
526 String spKey = LauncherAppState.getSharedPreferencesKey();
527 SharedPreferences sp = mContext.getSharedPreferences(spKey, Context.MODE_PRIVATE);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800528 sp.edit().putBoolean(EMPTY_DATABASE_CREATED, true).commit();
Winson Chungc763c4e2013-07-19 13:49:06 -0700529 }
530
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800531 @Override
532 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Winson Chungc763c4e2013-07-19 13:49:06 -0700533 if (LOGD) Log.d(TAG, "onUpgrade triggered: " + oldVersion);
Sunny Goyala2cc6242015-01-14 14:23:02 -0800534 switch (oldVersion) {
535 // The version cannot be lower that 12, as Launcher3 never supported a lower
Sunny Goyale87e6ab2014-11-21 22:42:53 -0800536 // version of the DB.
Sunny Goyala2cc6242015-01-14 14:23:02 -0800537 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 Goyal5d85c442015-03-10 13:14:47 -0700576 if (!addIntegerColumn(db, Favorites.RESTORED, 0)) {
Sunny Goyala2cc6242015-01-14 14:23:02 -0800577 // Old version remains, which means we wipe old data
578 break;
Sunny Goyala2cc6242015-01-14 14:23:02 -0800579 }
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 Goyald2f38192015-02-25 10:46:34 -0800610 case 21:
611 // Recreate workspace table with screen id a primary key
612 if (!recreateWorkspaceTable(db)) {
613 break;
614 }
615 case 22: {
Sunny Goyal5d85c442015-03-10 13:14:47 -0700616 if (!addIntegerColumn(db, Favorites.OPTIONS, 0)) {
617 // Old version remains, which means we wipe old data
618 break;
619 }
620 }
Sunny Goyal0b037782015-04-02 10:27:03 -0700621 case 23:
622 convertShortcutsToLauncherActivities(db);
623 case 24: {
Sunny Goyala2cc6242015-01-14 14:23:02 -0800624 // DB Upgraded successfully
625 return;
Chris Wrend5e66bf2013-09-16 14:02:29 -0400626 }
627 }
628
Sunny Goyala2cc6242015-01-14 14:23:02 -0800629 // DB was not upgraded
630 Log.w(TAG, "Destroying all old data.");
631 createEmptyDB(db);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632 }
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800633
Adam Cohen9b1d0622014-05-21 19:01:57 -0700634 @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 Goyal42de82f2014-09-26 22:09:29 -0700639 createEmptyDB(db);
640 }
Adam Cohen9b1d0622014-05-21 19:01:57 -0700641
Sunny Goyal42de82f2014-09-26 22:09:29 -0700642 /**
643 * Clears all the data for a fresh start.
644 */
645 public void createEmptyDB(SQLiteDatabase db) {
Adam Cohen9b1d0622014-05-21 19:01:57 -0700646 db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES);
647 db.execSQL("DROP TABLE IF EXISTS " + TABLE_WORKSPACE_SCREENS);
648 onCreate(db);
649 }
650
Sunny Goyald2f38192015-02-25 10:46:34 -0800651 /**
Sunny Goyal0b037782015-04-02 10:27:03 -0700652 * 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 Goyald2f38192015-02-25 10:46:34 -0800709 * 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 Cohen091440a2015-03-18 14:16:05 -0700756 @Thunk boolean updateFolderItemsRank(SQLiteDatabase db, boolean addRankColumn) {
Sunny Goyal08f72612015-01-05 13:41:43 -0800757 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 Goyalc87775d2015-02-10 19:52:36 -0800771 db.execSQL("UPDATE favorites SET rank=cellX+(cellY*?) WHERE "
772 + "container=? AND cellX IS NOT NULL AND cellY IS NOT NULL;",
Sunny Goyal08f72612015-01-05 13:41:43 -0800773 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 Guyed131872014-04-30 03:02:21 +0100788 private boolean addProfileColumn(SQLiteDatabase db) {
Sunny Goyal5d85c442015-03-10 13:14:47 -0700789 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 Guyed131872014-04-30 03:02:21 +0100798 db.beginTransaction();
799 try {
Sunny Goyal5d85c442015-03-10 13:14:47 -0700800 db.execSQL("ALTER TABLE favorites ADD COLUMN "
801 + columnName + " INTEGER NOT NULL DEFAULT " + defaultValue + ";");
Kenny Guyed131872014-04-30 03:02:21 +0100802 db.setTransactionSuccessful();
803 } catch (SQLException ex) {
Kenny Guyed131872014-04-30 03:02:21 +0100804 Log.e(TAG, ex.getMessage(), ex);
805 return false;
806 } finally {
807 db.endTransaction();
808 }
809 return true;
810 }
811
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700812 // 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 Goyal0fe505b2014-08-06 09:55:36 -0700817 @Override
Adam Cohendcd297f2013-06-18 13:13:40 -0700818 public long generateNewItemId() {
819 if (mMaxItemId < 0) {
820 throw new RuntimeException("Error: max item id was not initialized");
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700821 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700822 mMaxItemId += 1;
823 return mMaxItemId;
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700824 }
825
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700826 @Override
827 public long insertAndCheck(SQLiteDatabase db, ContentValues values) {
828 return dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values);
829 }
830
Winson Chungc763c4e2013-07-19 13:49:06 -0700831 public void updateMaxItemId(long id) {
832 mMaxItemId = id + 1;
833 }
834
Chris Wren5dee7af2013-12-20 17:22:11 -0500835 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 Cohendcd297f2013-06-18 13:13:40 -0700844 private long initializeMaxItemId(SQLiteDatabase db) {
Sunny Goyalfe4e4b92015-03-04 10:43:45 -0800845 return getMaxId(db, TABLE_FAVORITES);
Adam Cohendcd297f2013-06-18 13:13:40 -0700846 }
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 Chunga90303b2013-11-15 13:05:06 -0800858 // Log to disk
859 Launcher.addDumpLog(TAG, "11683562 - generateNewScreenId(): " + mMaxScreenId, true);
Adam Cohendcd297f2013-06-18 13:13:40 -0700860 return mMaxScreenId;
861 }
862
Adam Cohendcd297f2013-06-18 13:13:40 -0700863 private long initializeMaxScreenId(SQLiteDatabase db) {
Sunny Goyalfe4e4b92015-03-04 10:43:45 -0800864 return getMaxId(db, TABLE_WORKSPACE_SCREENS);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800865 }
866
Adam Cohen091440a2015-03-18 14:16:05 -0700867 @Thunk boolean initializeExternalAdd(ContentValues values) {
Adam Cohena043fa82014-07-23 14:49:38 -0700868 // 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 Cohen3ed316a2014-07-23 18:21:20 -0700886 values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId);
Adam Coheneb1ac422014-10-14 08:55:28 -0700887 if (!appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,cn)) {
Adam Cohend61a6382014-10-14 15:04:34 -0700888 return false;
Adam Cohena043fa82014-07-23 14:49:38 -0700889 }
890 } catch (RuntimeException e) {
891 Log.e(TAG, "Failed to initialize external widget", e);
Adam Coheneb1ac422014-10-14 08:55:28 -0700892 return false;
Adam Cohena043fa82014-07-23 14:49:38 -0700893 }
Adam Coheneb1ac422014-10-14 08:55:28 -0700894 } else {
895 return false;
Adam Cohena043fa82014-07-23 14:49:38 -0700896 }
897 }
Adam Cohen7ec3bbf2014-07-31 00:09:45 -0700898
899 // Add screen id if not present
900 long screenId = values.getAsLong(LauncherSettings.Favorites.SCREEN);
901 if (!addScreenIdIfNecessary(screenId)) {
902 return false;
903 }
Adam Cohena043fa82014-07-23 14:49:38 -0700904 return true;
905 }
906
Adam Cohen7ec3bbf2014-07-31 00:09:45 -0700907 // 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 Cohen091440a2015-03-18 14:16:05 -0700954 @Thunk int loadFavorites(SQLiteDatabase db, AutoInstallsLayout loader) {
Adam Cohen71483f42014-05-15 14:04:01 -0700955 ArrayList<Long> screenIds = new ArrayList<Long>();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700956 // TODO: Use multiple loaders with fall-back and transaction.
957 int count = loader.loadLayout(db, screenIds);
Adam Cohen71483f42014-05-15 14:04:01 -0700958
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 Cohen7ec3bbf2014-07-31 00:09:45 -0700977
Adam Cohen71483f42014-05-15 14:04:01 -0700978 return count;
979 }
980
Adam Cohen091440a2015-03-18 14:16:05 -0700981 @Thunk void migrateLauncher2Shortcuts(SQLiteDatabase db, Uri uri) {
Dan Sandlerd5024042014-01-09 15:01:33 -0500982 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 Sandlerd5024042014-01-09 15:01:33 -0500993 // 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 Guyed131872014-04-30 03:02:21 +01001024 final int profileIndex
1025 = c.getColumnIndex(LauncherSettings.Favorites.PROFILE_ID);
Dan Sandlerd5024042014-01-09 15:01:33 -05001026
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 Cohen72960972014-01-15 18:13:55 -08001039 final ArrayList<ContentValues> shortcuts = new ArrayList<ContentValues>();
1040 final ArrayList<ContentValues> folders = new ArrayList<ContentValues>();
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001041 final SparseArray<ContentValues> hotseat = new SparseArray<ContentValues>();
Dan Sandlerd5024042014-01-09 15:01:33 -05001042
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 Guyed131872014-04-30 03:02:21 +01001056
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 Goyal416541c2014-11-14 11:59:57 -08001069
1070 if (userHandle == null) {
1071 Launcher.addDumpLog(TAG, "skipping deleted user", true);
1072 continue;
1073 }
1074
Dan Sandlerd5024042014-01-09 15:01:33 -05001075 Launcher.addDumpLog(TAG, "migrating \""
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001076 + c.getString(titleIndex) + "\" ("
1077 + cellX + "," + cellY + "@"
1078 + LauncherSettings.Favorites.containerToString(container)
1079 + "/" + screen
1080 + "): " + intentStr, true);
Dan Sandlerd5024042014-01-09 15:01:33 -05001081
1082 if (itemType != Favorites.ITEM_TYPE_FOLDER) {
Adam Cohen556f6132014-01-15 15:18:08 -08001083
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 Sandlerd5024042014-01-09 15:01:33 -05001096 if (TextUtils.isEmpty(intentStr)) {
1097 // no intent? no icon
1098 Launcher.addDumpLog(TAG, "skipping empty intent", true);
1099 continue;
Adam Cohen72960972014-01-15 18:13:55 -08001100 } else if (cn != null &&
Kenny Guyed131872014-04-30 03:02:21 +01001101 !LauncherModel.isValidPackageActivity(mContext, cn,
1102 userHandle)) {
Adam Cohen556f6132014-01-15 15:18:08 -08001103 // component no longer exists.
Adam Cohen72960972014-01-15 18:13:55 -08001104 Launcher.addDumpLog(TAG, "skipping item whose component " +
Adam Cohen556f6132014-01-15 15:18:08 -08001105 "no longer exists.", true);
1106 continue;
Adam Cohen72960972014-01-15 18:13:55 -08001107 } else if (container ==
1108 LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1109 // Dedupe icons directly on the workspace
1110
Adam Cohen556f6132014-01-15 15:18:08 -08001111 // Canonicalize
1112 // the Play Store sets the package parameter, but Launcher
Adam Cohena33f11e2014-10-24 12:20:20 -07001113 // does not, so we clear that out to keep them the same.
1114 // Also ignore intent flags for the purposes of deduping.
Adam Cohen556f6132014-01-15 15:18:08 -08001115 intent.setPackage(null);
Adam Cohena33f11e2014-10-24 12:20:20 -07001116 int flags = intent.getFlags();
1117 intent.setFlags(0);
Adam Cohen556f6132014-01-15 15:18:08 -08001118 final String key = intent.toUri(0);
Adam Cohena33f11e2014-10-24 12:20:20 -07001119 intent.setFlags(flags);
Adam Cohen556f6132014-01-15 15:18:08 -08001120 if (seenIntents.contains(key)) {
1121 Launcher.addDumpLog(TAG, "skipping duplicate", true);
Dan Sandlerd5024042014-01-09 15:01:33 -05001122 continue;
Adam Cohen556f6132014-01-15 15:18:08 -08001123 } else {
1124 seenIntents.add(key);
Dan Sandlerd5024042014-01-09 15:01:33 -05001125 }
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 Guyed131872014-04-30 03:02:21 +01001145 values.put(LauncherSettings.Favorites.PROFILE_ID, userSerialNumber);
Dan Sandlerd5024042014-01-09 15:01:33 -05001146
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001147 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1148 hotseat.put(screen, values);
Dan Sandlerd5024042014-01-09 15:01:33 -05001149 }
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 Cohen72960972014-01-15 18:13:55 -08001157 // For items contained directly on one of the workspace screen,
1158 // we'll determine their location (screen, x, y) in a second pass.
Dan Sandlerd5024042014-01-09 15:01:33 -05001159 }
1160
1161 values.put(LauncherSettings.Favorites.CONTAINER, container);
1162
Adam Cohen72960972014-01-15 18:13:55 -08001163 if (itemType != Favorites.ITEM_TYPE_FOLDER) {
1164 shortcuts.add(values);
1165 } else {
1166 folders.add(values);
1167 }
Dan Sandlerd5024042014-01-09 15:01:33 -05001168 }
1169
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001170 // 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 Cohen72960972014-01-15 18:13:55 -08001195 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 Sandlerd5024042014-01-09 15:01:33 -05001224 db.beginTransaction();
1225 try {
Adam Cohen72960972014-01-15 18:13:55 -08001226 for (ContentValues row: allItems) {
1227 if (row == null) continue;
1228 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, row)
Dan Sandlerd5024042014-01-09 15:01:33 -05001229 < 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 Goyal08f72612015-01-05 13:41:43 -08001256
1257 updateFolderItemsRank(db, false);
Dan Sandlerd5024042014-01-09 15:01:33 -05001258 }
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 Cleronb87bd162009-10-30 16:36:56 -07001275 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001276
Sunny Goyalfe4e4b92015-03-04 10:43:45 -08001277 /**
1278 * @return the max _id in the provided table.
1279 */
Adam Cohen091440a2015-03-18 14:16:05 -07001280 @Thunk static long getMaxId(SQLiteDatabase db, String table) {
Sunny Goyalfe4e4b92015-03-04 10:43:45 -08001281 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 Project31dd5032009-03-03 19:32:27 -08001298 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 Lehmannc3a80402012-04-23 21:35:11 -07001314 this.where = "_id=" + ContentUris.parseId(url);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 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 Cohen72960972014-01-15 18:13:55 -08001329}