blob: 8bc89885594fd7b4822010d8303d4b82acfb0398 [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;
Sunny Goyale2fba6c2015-05-12 10:39:59 -070055import com.android.launcher3.util.ManagedProfileHeuristic;
Adam Cohen091440a2015-03-18 14:16:05 -070056import com.android.launcher3.util.Thunk;
Michael Jurka8b805b12012-04-18 14:23:14 -070057
Dan Sandlerd5024042014-01-09 15:01:33 -050058import java.io.File;
Mike Cleronb87bd162009-10-30 16:36:56 -070059import java.net.URISyntaxException;
Adam Cohen228da5a2011-07-27 22:23:47 -070060import java.util.ArrayList;
Adam Cohen71483f42014-05-15 14:04:01 -070061import java.util.Collections;
Dan Sandlerd5024042014-01-09 15:01:33 -050062import java.util.HashSet;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064public class LauncherProvider extends ContentProvider {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080065 private static final String TAG = "Launcher.LauncherProvider";
66 private static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067
Sunny Goyale2fba6c2015-05-12 10:39:59 -070068 private static final int DATABASE_VERSION = 25;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069
Adam Cohene25af792013-06-06 23:08:25 -070070 static final String OLD_AUTHORITY = "com.android.launcher2.settings";
Chris Wrene523e702013-10-09 10:36:55 -040071 static final String AUTHORITY = ProviderConfig.AUTHORITY;
Winson Chung3d503fb2011-07-13 17:25:49 -070072
Sunny Goyal18b640c2015-04-17 09:24:01 -070073 static final String TABLE_FAVORITES = LauncherSettings.Favorites.TABLE_NAME;
74 static final String TABLE_WORKSPACE_SCREENS = LauncherSettings.WorkspaceScreens.TABLE_NAME;
Sunny Goyale87e6ab2014-11-21 22:42:53 -080075 static final String EMPTY_DATABASE_CREATED = "EMPTY_DATABASE_CREATED";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076
Adam Cohena043fa82014-07-23 14:49:38 -070077 private static final String URI_PARAM_IS_EXTERNAL_ADD = "isExternalAdd";
78
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070079 private static final String RESTRICTION_PACKAGE_NAME = "workspace.configuration.package.name";
80
Anjali Koppal67e7cae2014-03-13 12:14:12 -070081 private LauncherProviderChangeListener mListener;
82
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -070083 /**
Romain Guy73b979d2009-06-09 12:57:21 -070084 * {@link Uri} triggered at any registered {@link android.database.ContentObserver} when
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -070085 * {@link AppWidgetHost#deleteHost()} is called during database creation.
86 * Use this to recall {@link AppWidgetHost#startListening()} if needed.
87 */
88 static final Uri CONTENT_APPWIDGET_RESET_URI =
89 Uri.parse("content://" + AUTHORITY + "/appWidgetReset");
Daniel Lehmannc3a80402012-04-23 21:35:11 -070090
Michael Jurkaa8c760d2011-04-28 14:59:33 -070091 private DatabaseHelper mOpenHelper;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092
93 @Override
94 public boolean onCreate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040095 final Context context = getContext();
Nilesh Agrawala258f812015-01-26 14:07:29 -080096 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
Daniel Sandlere4f98912013-06-25 15:13:26 -040097 mOpenHelper = new DatabaseHelper(context);
Nilesh Agrawalfde11852015-01-21 11:50:57 -080098 StrictMode.setThreadPolicy(oldPolicy);
Daniel Sandlere4f98912013-06-25 15:13:26 -040099 LauncherAppState.setLauncherProvider(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100 return true;
101 }
102
Winson Chung0b560dd2014-01-21 13:00:26 -0800103 public boolean wasNewDbCreated() {
104 return mOpenHelper.wasNewDbCreated();
105 }
106
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700107 public void setLauncherProviderChangeListener(LauncherProviderChangeListener listener) {
108 mListener = listener;
109 }
110
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111 @Override
112 public String getType(Uri uri) {
113 SqlArguments args = new SqlArguments(uri, null, null);
114 if (TextUtils.isEmpty(args.where)) {
115 return "vnd.android.cursor.dir/" + args.table;
116 } else {
117 return "vnd.android.cursor.item/" + args.table;
118 }
119 }
120
121 @Override
122 public Cursor query(Uri uri, String[] projection, String selection,
123 String[] selectionArgs, String sortOrder) {
124
125 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
126 SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
127 qb.setTables(args.table);
128
Romain Guy73b979d2009-06-09 12:57:21 -0700129 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 Cursor result = qb.query(db, projection, args.where, args.args, null, null, sortOrder);
131 result.setNotificationUri(getContext().getContentResolver(), uri);
132
133 return result;
134 }
135
Adam Cohen091440a2015-03-18 14:16:05 -0700136 @Thunk static long dbInsertAndCheck(DatabaseHelper helper,
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700137 SQLiteDatabase db, String table, String nullColumnHack, ContentValues values) {
Dan Sandlerd5024042014-01-09 15:01:33 -0500138 if (values == null) {
139 throw new RuntimeException("Error: attempting to insert null values");
140 }
Adam Cohen71483f42014-05-15 14:04:01 -0700141 if (!values.containsKey(LauncherSettings.ChangeLogColumns._ID)) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700142 throw new RuntimeException("Error: attempting to add item without specifying an id");
143 }
Chris Wren5dee7af2013-12-20 17:22:11 -0500144 helper.checkId(table, values);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700145 return db.insert(table, nullColumnHack, values);
146 }
147
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 @Override
149 public Uri insert(Uri uri, ContentValues initialValues) {
150 SqlArguments args = new SqlArguments(uri);
151
Adam Cohena043fa82014-07-23 14:49:38 -0700152 // In very limited cases, we support system|signature permission apps to add to the db
153 String externalAdd = uri.getQueryParameter(URI_PARAM_IS_EXTERNAL_ADD);
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700154 final boolean isExternalAll = externalAdd != null && "true".equals(externalAdd);
155 if (isExternalAll) {
Adam Cohena043fa82014-07-23 14:49:38 -0700156 if (!mOpenHelper.initializeExternalAdd(initialValues)) {
157 return null;
158 }
159 }
160
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
Chris Wren1ada10d2013-09-13 18:01:38 -0400162 addModifiedTime(initialValues);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700163 final long rowId = dbInsertAndCheck(mOpenHelper, db, args.table, null, initialValues);
Sunny Goyale72f3d52015-03-02 14:24:21 -0800164 if (rowId < 0) return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165
166 uri = ContentUris.withAppendedId(uri, rowId);
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700167 notifyListeners();
168
169 if (isExternalAll) {
170 LauncherAppState app = LauncherAppState.getInstanceNoCreate();
171 if (app != null) {
172 app.reloadWorkspace();
173 }
174 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175
176 return uri;
177 }
178
Adam Cohena043fa82014-07-23 14:49:38 -0700179
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 @Override
181 public int bulkInsert(Uri uri, ContentValues[] values) {
182 SqlArguments args = new SqlArguments(uri);
183
184 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
185 db.beginTransaction();
186 try {
187 int numValues = values.length;
188 for (int i = 0; i < numValues; i++) {
Chris Wren1ada10d2013-09-13 18:01:38 -0400189 addModifiedTime(values[i]);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700190 if (dbInsertAndCheck(mOpenHelper, db, args.table, null, values[i]) < 0) {
191 return 0;
192 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 }
194 db.setTransactionSuccessful();
195 } finally {
196 db.endTransaction();
197 }
198
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700199 notifyListeners();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 return values.length;
201 }
202
203 @Override
Yura085c8532014-02-11 15:15:29 +0000204 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
205 throws OperationApplicationException {
206 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
207 db.beginTransaction();
208 try {
209 ContentProviderResult[] result = super.applyBatch(operations);
210 db.setTransactionSuccessful();
211 return result;
212 } finally {
213 db.endTransaction();
214 }
215 }
216
217 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 public int delete(Uri uri, String selection, String[] selectionArgs) {
219 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
220
221 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
222 int count = db.delete(args.table, args.where, args.args);
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700223 if (count > 0) notifyListeners();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224
225 return count;
226 }
227
228 @Override
229 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
230 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
231
Chris Wren1ada10d2013-09-13 18:01:38 -0400232 addModifiedTime(values);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
234 int count = db.update(args.table, values, args.where, args.args);
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700235 if (count > 0) notifyListeners();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236
237 return count;
238 }
239
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700240 private void notifyListeners() {
Chris Wren1ada10d2013-09-13 18:01:38 -0400241 // always notify the backup agent
Chris Wren92aa4232013-10-04 11:29:36 -0400242 LauncherBackupAgentHelper.dataChanged(getContext());
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700243 if (mListener != null) {
244 mListener.onLauncherProviderChange();
245 }
Chris Wren1ada10d2013-09-13 18:01:38 -0400246 }
247
Adam Cohen091440a2015-03-18 14:16:05 -0700248 @Thunk static void addModifiedTime(ContentValues values) {
Chris Wren1ada10d2013-09-13 18:01:38 -0400249 values.put(LauncherSettings.ChangeLogColumns.MODIFIED, System.currentTimeMillis());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 }
251
Adam Cohendcd297f2013-06-18 13:13:40 -0700252 public long generateNewItemId() {
253 return mOpenHelper.generateNewItemId();
254 }
255
Winson Chungc763c4e2013-07-19 13:49:06 -0700256 public void updateMaxItemId(long id) {
257 mOpenHelper.updateMaxItemId(id);
258 }
259
Adam Cohendcd297f2013-06-18 13:13:40 -0700260 public long generateNewScreenId() {
261 return mOpenHelper.generateNewScreenId();
262 }
263
Brian Muramatsu5524b492012-10-02 16:55:54 -0700264 /**
Sunny Goyal42de82f2014-09-26 22:09:29 -0700265 * Clears all the data for a fresh start.
266 */
267 synchronized public void createEmptyDB() {
268 mOpenHelper.createEmptyDB(mOpenHelper.getWritableDatabase());
269 }
270
Sunny Goyal33d44382014-10-16 09:24:19 -0700271 public void clearFlagEmptyDbCreated() {
272 String spKey = LauncherAppState.getSharedPreferencesKey();
273 getContext().getSharedPreferences(spKey, Context.MODE_PRIVATE)
274 .edit()
275 .remove(EMPTY_DATABASE_CREATED)
276 .commit();
277 }
278
Sunny Goyal42de82f2014-09-26 22:09:29 -0700279 /**
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700280 * Loads the default workspace based on the following priority scheme:
Sunny Goyalb2d46ce2015-03-26 11:32:11 -0700281 * 1) From the app restrictions
282 * 2) From a package provided by play store
283 * 3) From a partner configuration APK, already in the system image
284 * 4) The default configuration for the particular device
Brian Muramatsu5524b492012-10-02 16:55:54 -0700285 */
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700286 synchronized public void loadDefaultFavoritesIfNecessary() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400287 String spKey = LauncherAppState.getSharedPreferencesKey();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700288 SharedPreferences sp = getContext().getSharedPreferences(spKey, Context.MODE_PRIVATE);
Adam Cohene25af792013-06-06 23:08:25 -0700289
Winson Chungc763c4e2013-07-19 13:49:06 -0700290 if (sp.getBoolean(EMPTY_DATABASE_CREATED, false)) {
Chris Wren5dee7af2013-12-20 17:22:11 -0500291 Log.d(TAG, "loading default workspace");
Michael Jurka45355c42012-10-08 13:21:35 +0200292
Sunny Goyalb2d46ce2015-03-26 11:32:11 -0700293 AutoInstallsLayout loader = createWorkspaceLoaderFromAppRestriction();
294 if (loader == null) {
295 loader = AutoInstallsLayout.get(getContext(),
296 mOpenHelper.mAppWidgetHost, mOpenHelper);
297 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700298 if (loader == null) {
Adam Cohen9b8f51f2014-05-30 15:34:09 -0700299 final Partner partner = Partner.get(getContext().getPackageManager());
300 if (partner != null && partner.hasDefaultLayout()) {
301 final Resources partnerRes = partner.getResources();
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700302 int workspaceResId = partnerRes.getIdentifier(Partner.RES_DEFAULT_LAYOUT,
Adam Cohen9b8f51f2014-05-30 15:34:09 -0700303 "xml", partner.getPackageName());
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700304 if (workspaceResId != 0) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700305 loader = new DefaultLayoutParser(getContext(), mOpenHelper.mAppWidgetHost,
306 mOpenHelper, partnerRes, workspaceResId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700307 }
Adam Cohen9b8f51f2014-05-30 15:34:09 -0700308 }
309 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700310
Sunny Goyal9d219682014-10-23 14:21:02 -0700311 final boolean usingExternallyProvidedLayout = loader != null;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700312 if (loader == null) {
Sunny Goyal9d219682014-10-23 14:21:02 -0700313 loader = getDefaultLayoutParser();
Brian Muramatsu5524b492012-10-02 16:55:54 -0700314 }
Sunny Goyalc6c8fef2015-03-04 09:51:18 -0800315
316 // There might be some partially restored DB items, due to buggy restore logic in
317 // previous versions of launcher.
318 createEmptyDB();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700319 // Populate favorites table with initial favorites
Sunny Goyal9d219682014-10-23 14:21:02 -0700320 if ((mOpenHelper.loadFavorites(mOpenHelper.getWritableDatabase(), loader) <= 0)
321 && usingExternallyProvidedLayout) {
322 // Unable to load external layout. Cleanup and load the internal layout.
323 createEmptyDB();
324 mOpenHelper.loadFavorites(mOpenHelper.getWritableDatabase(),
325 getDefaultLayoutParser());
326 }
Sunny Goyal33d44382014-10-16 09:24:19 -0700327 clearFlagEmptyDbCreated();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700328 }
329 }
330
Sunny Goyalb2d46ce2015-03-26 11:32:11 -0700331 /**
332 * Creates workspace loader from an XML resource listed in the app restrictions.
333 *
334 * @return the loader if the restrictions are set and the resource exists; null otherwise.
335 */
336 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
337 private AutoInstallsLayout createWorkspaceLoaderFromAppRestriction() {
338 // UserManager.getApplicationRestrictions() requires minSdkVersion >= 18
339 if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
340 return null;
341 }
342
343 Context ctx = getContext();
344 UserManager um = (UserManager) ctx.getSystemService(Context.USER_SERVICE);
345 Bundle bundle = um.getApplicationRestrictions(ctx.getPackageName());
Sunny Goyal35ca8732015-04-06 10:45:31 -0700346 if (bundle == null) {
347 return null;
348 }
Sunny Goyalb2d46ce2015-03-26 11:32:11 -0700349
Sunny Goyal35ca8732015-04-06 10:45:31 -0700350 String packageName = bundle.getString(RESTRICTION_PACKAGE_NAME);
Sunny Goyalb2d46ce2015-03-26 11:32:11 -0700351 if (packageName != null) {
352 try {
353 Resources targetResources = ctx.getPackageManager()
354 .getResourcesForApplication(packageName);
355 return AutoInstallsLayout.get(ctx, packageName, targetResources,
356 mOpenHelper.mAppWidgetHost, mOpenHelper);
357 } catch (NameNotFoundException e) {
358 Log.e(TAG, "Target package for restricted profile not found", e);
359 return null;
360 }
361 }
362 return null;
363 }
364
Sunny Goyal9d219682014-10-23 14:21:02 -0700365 private DefaultLayoutParser getDefaultLayoutParser() {
366 int defaultLayout = LauncherAppState.getInstance()
367 .getDynamicGrid().getDeviceProfile().defaultLayoutId;
368 return new DefaultLayoutParser(getContext(), mOpenHelper.mAppWidgetHost,
369 mOpenHelper, getContext().getResources(), defaultLayout);
370 }
371
Dan Sandlerd5024042014-01-09 15:01:33 -0500372 public void migrateLauncher2Shortcuts() {
373 mOpenHelper.migrateLauncher2Shortcuts(mOpenHelper.getWritableDatabase(),
Jason Monk0bfcceb2014-03-21 15:42:06 -0400374 Uri.parse(getContext().getString(R.string.old_launcher_provider_uri)));
Dan Sandlerd5024042014-01-09 15:01:33 -0500375 }
376
Sunny Goyal08f72612015-01-05 13:41:43 -0800377 public void updateFolderItemsRank() {
378 mOpenHelper.updateFolderItemsRank(mOpenHelper.getWritableDatabase(), false);
379 }
380
Dan Sandlerd5024042014-01-09 15:01:33 -0500381 public void deleteDatabase() {
382 // Are you sure? (y/n)
383 final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
Dan Sandler2b471742014-01-21 14:14:41 -0500384 final File dbFile = new File(db.getPath());
Dan Sandlerd5024042014-01-09 15:01:33 -0500385 mOpenHelper.close();
Dan Sandler2b471742014-01-21 14:14:41 -0500386 if (dbFile.exists()) {
387 SQLiteDatabase.deleteDatabase(dbFile);
388 }
Dan Sandlerd5024042014-01-09 15:01:33 -0500389 mOpenHelper = new DatabaseHelper(getContext());
390 }
391
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700392 private static class DatabaseHelper extends SQLiteOpenHelper implements LayoutParserCallback {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 private final Context mContext;
Adam Cohen091440a2015-03-18 14:16:05 -0700394 @Thunk final AppWidgetHost mAppWidgetHost;
Adam Cohendcd297f2013-06-18 13:13:40 -0700395 private long mMaxItemId = -1;
396 private long mMaxScreenId = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397
Winson Chung0b560dd2014-01-21 13:00:26 -0800398 private boolean mNewDbCreated = false;
399
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 DatabaseHelper(Context context) {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100401 super(context, LauncherFiles.LAUNCHER_DB, null, DATABASE_VERSION);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800402 mContext = context;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700403 mAppWidgetHost = new AppWidgetHost(context, Launcher.APPWIDGET_HOST_ID);
Winson Chung3d503fb2011-07-13 17:25:49 -0700404
405 // In the case where neither onCreate nor onUpgrade gets called, we read the maxId from
406 // the DB here
Adam Cohendcd297f2013-06-18 13:13:40 -0700407 if (mMaxItemId == -1) {
408 mMaxItemId = initializeMaxItemId(getWritableDatabase());
409 }
410 if (mMaxScreenId == -1) {
411 mMaxScreenId = initializeMaxScreenId(getWritableDatabase());
Winson Chung3d503fb2011-07-13 17:25:49 -0700412 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800413 }
414
Winson Chung0b560dd2014-01-21 13:00:26 -0800415 public boolean wasNewDbCreated() {
416 return mNewDbCreated;
417 }
418
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700419 /**
420 * Send notification that we've deleted the {@link AppWidgetHost},
421 * probably as part of the initial database creation. The receiver may
422 * want to re-call {@link AppWidgetHost#startListening()} to ensure
423 * callbacks are correctly set.
424 */
425 private void sendAppWidgetResetNotify() {
426 final ContentResolver resolver = mContext.getContentResolver();
427 resolver.notifyChange(CONTENT_APPWIDGET_RESET_URI, null);
428 }
429
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800430 @Override
431 public void onCreate(SQLiteDatabase db) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800432 if (LOGD) Log.d(TAG, "creating new launcher database");
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700433
Adam Cohendcd297f2013-06-18 13:13:40 -0700434 mMaxItemId = 1;
435 mMaxScreenId = 0;
Winson Chung0b560dd2014-01-21 13:00:26 -0800436 mNewDbCreated = true;
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700437
Kenny Guyed131872014-04-30 03:02:21 +0100438 UserManagerCompat userManager = UserManagerCompat.getInstance(mContext);
439 long userSerialNumber = userManager.getSerialNumberForUser(
440 UserHandleCompat.myUserHandle());
441
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800442 db.execSQL("CREATE TABLE favorites (" +
443 "_id INTEGER PRIMARY KEY," +
444 "title TEXT," +
445 "intent TEXT," +
446 "container INTEGER," +
447 "screen INTEGER," +
448 "cellX INTEGER," +
449 "cellY INTEGER," +
450 "spanX INTEGER," +
451 "spanY INTEGER," +
452 "itemType INTEGER," +
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700453 "appWidgetId INTEGER NOT NULL DEFAULT -1," +
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 "isShortcut INTEGER," +
455 "iconType INTEGER," +
456 "iconPackage TEXT," +
457 "iconResource TEXT," +
458 "icon BLOB," +
459 "uri TEXT," +
Chris Wrend5e66bf2013-09-16 14:02:29 -0400460 "displayMode INTEGER," +
Chris Wren1ada10d2013-09-13 18:01:38 -0400461 "appWidgetProvider TEXT," +
Chris Wrenf4d08112014-01-16 18:13:56 -0500462 "modified INTEGER NOT NULL DEFAULT 0," +
Kenny Guyed131872014-04-30 03:02:21 +0100463 "restored INTEGER NOT NULL DEFAULT 0," +
Sunny Goyal08f72612015-01-05 13:41:43 -0800464 "profileId INTEGER DEFAULT " + userSerialNumber + "," +
Sunny Goyal5d85c442015-03-10 13:14:47 -0700465 "rank INTEGER NOT NULL DEFAULT 0," +
466 "options INTEGER NOT NULL DEFAULT 0" +
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800467 ");");
Adam Cohendcd297f2013-06-18 13:13:40 -0700468 addWorkspacesTable(db);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700470 // Database was just created, so wipe any previous widgets
471 if (mAppWidgetHost != null) {
472 mAppWidgetHost.deleteHost();
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700473 sendAppWidgetResetNotify();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800474 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700475
Sunny Goyale87e6ab2014-11-21 22:42:53 -0800476 // Fresh and clean launcher DB.
477 mMaxItemId = initializeMaxItemId(db);
478 setFlagEmptyDbCreated();
Sunny Goyale2fba6c2015-05-12 10:39:59 -0700479
480 // When a new DB is created, remove all previously stored managed profile information.
481 ManagedProfileHeuristic.processAllUsers(Collections.EMPTY_LIST, mContext);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800482 }
483
Adam Cohendcd297f2013-06-18 13:13:40 -0700484 private void addWorkspacesTable(SQLiteDatabase db) {
485 db.execSQL("CREATE TABLE " + TABLE_WORKSPACE_SCREENS + " (" +
Sunny Goyald2f38192015-02-25 10:46:34 -0800486 LauncherSettings.WorkspaceScreens._ID + " INTEGER PRIMARY KEY," +
Chris Wren1ada10d2013-09-13 18:01:38 -0400487 LauncherSettings.WorkspaceScreens.SCREEN_RANK + " INTEGER," +
488 LauncherSettings.ChangeLogColumns.MODIFIED + " INTEGER NOT NULL DEFAULT 0" +
Adam Cohendcd297f2013-06-18 13:13:40 -0700489 ");");
490 }
491
Adam Cohen119285e2014-04-02 16:59:08 -0700492 private void removeOrphanedItems(SQLiteDatabase db) {
Adam Cohenf9c14de2014-04-17 18:20:45 -0700493 // Delete items directly on the workspace who's screen id doesn't exist
494 // "DELETE FROM favorites WHERE screen NOT IN (SELECT _id FROM workspaceScreens)
495 // AND container = -100"
496 String removeOrphanedDesktopItems = "DELETE FROM " + TABLE_FAVORITES +
497 " WHERE " +
Adam Cohen119285e2014-04-02 16:59:08 -0700498 LauncherSettings.Favorites.SCREEN + " NOT IN (SELECT " +
Adam Cohenf9c14de2014-04-17 18:20:45 -0700499 LauncherSettings.WorkspaceScreens._ID + " FROM " + TABLE_WORKSPACE_SCREENS + ")" +
500 " AND " +
501 LauncherSettings.Favorites.CONTAINER + " = " +
502 LauncherSettings.Favorites.CONTAINER_DESKTOP;
503 db.execSQL(removeOrphanedDesktopItems);
504
505 // Delete items contained in folders which no longer exist (after above statement)
506 // "DELETE FROM favorites WHERE container <> -100 AND container <> -101 AND container
507 // NOT IN (SELECT _id FROM favorites WHERE itemType = 2)"
508 String removeOrphanedFolderItems = "DELETE FROM " + TABLE_FAVORITES +
509 " WHERE " +
510 LauncherSettings.Favorites.CONTAINER + " <> " +
511 LauncherSettings.Favorites.CONTAINER_DESKTOP +
512 " AND "
513 + LauncherSettings.Favorites.CONTAINER + " <> " +
514 LauncherSettings.Favorites.CONTAINER_HOTSEAT +
515 " AND "
516 + LauncherSettings.Favorites.CONTAINER + " NOT IN (SELECT " +
517 LauncherSettings.Favorites._ID + " FROM " + TABLE_FAVORITES +
518 " WHERE " + LauncherSettings.Favorites.ITEM_TYPE + " = " +
519 LauncherSettings.Favorites.ITEM_TYPE_FOLDER + ")";
520 db.execSQL(removeOrphanedFolderItems);
Adam Cohen119285e2014-04-02 16:59:08 -0700521 }
522
Winson Chungc763c4e2013-07-19 13:49:06 -0700523 private void setFlagJustLoadedOldDb() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400524 String spKey = LauncherAppState.getSharedPreferencesKey();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700525 SharedPreferences sp = mContext.getSharedPreferences(spKey, Context.MODE_PRIVATE);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800526 sp.edit().putBoolean(EMPTY_DATABASE_CREATED, false).commit();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700527 }
528
Winson Chungc763c4e2013-07-19 13:49:06 -0700529 private void setFlagEmptyDbCreated() {
530 String spKey = LauncherAppState.getSharedPreferencesKey();
531 SharedPreferences sp = mContext.getSharedPreferences(spKey, Context.MODE_PRIVATE);
Sunny Goyal66cfdc22015-02-02 13:01:51 -0800532 sp.edit().putBoolean(EMPTY_DATABASE_CREATED, true).commit();
Winson Chungc763c4e2013-07-19 13:49:06 -0700533 }
534
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800535 @Override
536 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Winson Chungc763c4e2013-07-19 13:49:06 -0700537 if (LOGD) Log.d(TAG, "onUpgrade triggered: " + oldVersion);
Sunny Goyala2cc6242015-01-14 14:23:02 -0800538 switch (oldVersion) {
539 // The version cannot be lower that 12, as Launcher3 never supported a lower
Sunny Goyale87e6ab2014-11-21 22:42:53 -0800540 // version of the DB.
Sunny Goyala2cc6242015-01-14 14:23:02 -0800541 case 12: {
542 // With the new shrink-wrapped and re-orderable workspaces, it makes sense
543 // to persist workspace screens and their relative order.
544 mMaxScreenId = 0;
545 addWorkspacesTable(db);
546 }
547 case 13: {
548 db.beginTransaction();
549 try {
550 // Insert new column for holding widget provider name
551 db.execSQL("ALTER TABLE favorites " +
552 "ADD COLUMN appWidgetProvider TEXT;");
553 db.setTransactionSuccessful();
554 } catch (SQLException ex) {
555 Log.e(TAG, ex.getMessage(), ex);
556 // Old version remains, which means we wipe old data
557 break;
558 } finally {
559 db.endTransaction();
560 }
561 }
562 case 14: {
563 db.beginTransaction();
564 try {
565 // Insert new column for holding update timestamp
566 db.execSQL("ALTER TABLE favorites " +
567 "ADD COLUMN modified INTEGER NOT NULL DEFAULT 0;");
568 db.execSQL("ALTER TABLE workspaceScreens " +
569 "ADD COLUMN modified INTEGER NOT NULL DEFAULT 0;");
570 db.setTransactionSuccessful();
571 } catch (SQLException ex) {
572 Log.e(TAG, ex.getMessage(), ex);
573 // Old version remains, which means we wipe old data
574 break;
575 } finally {
576 db.endTransaction();
577 }
578 }
579 case 15: {
Sunny Goyal5d85c442015-03-10 13:14:47 -0700580 if (!addIntegerColumn(db, Favorites.RESTORED, 0)) {
Sunny Goyala2cc6242015-01-14 14:23:02 -0800581 // Old version remains, which means we wipe old data
582 break;
Sunny Goyala2cc6242015-01-14 14:23:02 -0800583 }
584 }
585 case 16: {
586 // We use the db version upgrade here to identify users who may not have seen
587 // clings yet (because they weren't available), but for whom the clings are now
588 // available (tablet users). Because one of the possible cling flows (migration)
589 // is very destructive (wipes out workspaces), we want to prevent this from showing
590 // until clear data. We do so by marking that the clings have been shown.
591 LauncherClings.synchonouslyMarkFirstRunClingDismissed(mContext);
592 }
593 case 17: {
594 // No-op
595 }
596 case 18: {
597 // Due to a data loss bug, some users may have items associated with screen ids
598 // which no longer exist. Since this can cause other problems, and since the user
599 // will never see these items anyway, we use database upgrade as an opportunity to
600 // clean things up.
601 removeOrphanedItems(db);
602 }
603 case 19: {
604 // Add userId column
605 if (!addProfileColumn(db)) {
606 // Old version remains, which means we wipe old data
607 break;
608 }
609 }
610 case 20:
611 if (!updateFolderItemsRank(db, true)) {
612 break;
613 }
Sunny Goyald2f38192015-02-25 10:46:34 -0800614 case 21:
615 // Recreate workspace table with screen id a primary key
616 if (!recreateWorkspaceTable(db)) {
617 break;
618 }
619 case 22: {
Sunny Goyal5d85c442015-03-10 13:14:47 -0700620 if (!addIntegerColumn(db, Favorites.OPTIONS, 0)) {
621 // Old version remains, which means we wipe old data
622 break;
623 }
624 }
Sunny Goyal0b037782015-04-02 10:27:03 -0700625 case 23:
626 convertShortcutsToLauncherActivities(db);
Sunny Goyale2fba6c2015-05-12 10:39:59 -0700627 case 24:
628 ManagedProfileHeuristic.markExistingUsersForNoFolderCreation(mContext);
629 case 25: {
Sunny Goyala2cc6242015-01-14 14:23:02 -0800630 // DB Upgraded successfully
631 return;
Chris Wrend5e66bf2013-09-16 14:02:29 -0400632 }
633 }
634
Sunny Goyala2cc6242015-01-14 14:23:02 -0800635 // DB was not upgraded
636 Log.w(TAG, "Destroying all old data.");
637 createEmptyDB(db);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638 }
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800639
Adam Cohen9b1d0622014-05-21 19:01:57 -0700640 @Override
641 public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
642 // This shouldn't happen -- throw our hands up in the air and start over.
643 Log.w(TAG, "Database version downgrade from: " + oldVersion + " to " + newVersion +
644 ". Wiping databse.");
Sunny Goyal42de82f2014-09-26 22:09:29 -0700645 createEmptyDB(db);
646 }
Adam Cohen9b1d0622014-05-21 19:01:57 -0700647
Sunny Goyal42de82f2014-09-26 22:09:29 -0700648 /**
649 * Clears all the data for a fresh start.
650 */
651 public void createEmptyDB(SQLiteDatabase db) {
Adam Cohen9b1d0622014-05-21 19:01:57 -0700652 db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES);
653 db.execSQL("DROP TABLE IF EXISTS " + TABLE_WORKSPACE_SCREENS);
654 onCreate(db);
655 }
656
Sunny Goyald2f38192015-02-25 10:46:34 -0800657 /**
Sunny Goyal0b037782015-04-02 10:27:03 -0700658 * Replaces all shortcuts of type {@link Favorites#ITEM_TYPE_SHORTCUT} which have a valid
659 * launcher activity target with {@link Favorites#ITEM_TYPE_APPLICATION}.
660 */
661 private void convertShortcutsToLauncherActivities(SQLiteDatabase db) {
662 db.beginTransaction();
663 Cursor c = null;
664 SQLiteStatement updateStmt = null;
665
666 try {
667 // Only consider the primary user as other users can't have a shortcut.
668 long userSerial = UserManagerCompat.getInstance(mContext)
669 .getSerialNumberForUser(UserHandleCompat.myUserHandle());
670 c = db.query(TABLE_FAVORITES, new String[] {
671 Favorites._ID,
672 Favorites.INTENT,
673 }, "itemType=" + Favorites.ITEM_TYPE_SHORTCUT + " AND profileId=" + userSerial,
674 null, null, null, null);
675
676 updateStmt = db.compileStatement("UPDATE favorites SET itemType="
677 + Favorites.ITEM_TYPE_APPLICATION + " WHERE _id=?");
678
679 final int idIndex = c.getColumnIndexOrThrow(Favorites._ID);
680 final int intentIndex = c.getColumnIndexOrThrow(Favorites.INTENT);
681
682 while (c.moveToNext()) {
683 String intentDescription = c.getString(intentIndex);
684 Intent intent;
685 try {
686 intent = Intent.parseUri(intentDescription, 0);
687 } catch (URISyntaxException e) {
688 Log.e(TAG, "Unable to parse intent", e);
689 continue;
690 }
691
692 if (!InstallShortcutReceiver.isLauncherActivity(intent, mContext)) {
693 continue;
694 }
695
696 long id = c.getLong(idIndex);
697 updateStmt.bindLong(1, id);
698 updateStmt.execute();
699 }
700 db.setTransactionSuccessful();
701 } catch (SQLException ex) {
702 Log.w(TAG, "Error deduping shortcuts", ex);
703 } finally {
704 db.endTransaction();
705 if (c != null) {
706 c.close();
707 }
708 if (updateStmt != null) {
709 updateStmt.close();
710 }
711 }
712 }
713
714 /**
Sunny Goyald2f38192015-02-25 10:46:34 -0800715 * Recreates workspace table and migrates data to the new table.
716 */
717 public boolean recreateWorkspaceTable(SQLiteDatabase db) {
718 db.beginTransaction();
719 try {
720 Cursor c = db.query(TABLE_WORKSPACE_SCREENS,
721 new String[] {LauncherSettings.WorkspaceScreens._ID},
722 null, null, null, null,
723 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
724 ArrayList<Long> sortedIDs = new ArrayList<Long>();
725 long maxId = 0;
726 try {
727 while (c.moveToNext()) {
728 Long id = c.getLong(0);
729 if (!sortedIDs.contains(id)) {
730 sortedIDs.add(id);
731 maxId = Math.max(maxId, id);
732 }
733 }
734 } finally {
735 c.close();
736 }
737
738 db.execSQL("DROP TABLE IF EXISTS " + TABLE_WORKSPACE_SCREENS);
739 addWorkspacesTable(db);
740
741 // Add all screen ids back
742 int total = sortedIDs.size();
743 for (int i = 0; i < total; i++) {
744 ContentValues values = new ContentValues();
745 values.put(LauncherSettings.WorkspaceScreens._ID, sortedIDs.get(i));
746 values.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
747 addModifiedTime(values);
748 db.insertOrThrow(TABLE_WORKSPACE_SCREENS, null, values);
749 }
750 db.setTransactionSuccessful();
751 mMaxScreenId = maxId;
752 } catch (SQLException ex) {
753 // Old version remains, which means we wipe old data
754 Log.e(TAG, ex.getMessage(), ex);
755 return false;
756 } finally {
757 db.endTransaction();
758 }
759 return true;
760 }
761
Adam Cohen091440a2015-03-18 14:16:05 -0700762 @Thunk boolean updateFolderItemsRank(SQLiteDatabase db, boolean addRankColumn) {
Sunny Goyal08f72612015-01-05 13:41:43 -0800763 db.beginTransaction();
764 try {
765 if (addRankColumn) {
766 // Insert new column for holding rank
767 db.execSQL("ALTER TABLE favorites ADD COLUMN rank INTEGER NOT NULL DEFAULT 0;");
768 }
769
770 // Get a map for folder ID to folder width
771 Cursor c = db.rawQuery("SELECT container, MAX(cellX) FROM favorites"
772 + " WHERE container IN (SELECT _id FROM favorites WHERE itemType = ?)"
773 + " GROUP BY container;",
774 new String[] {Integer.toString(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)});
775
776 while (c.moveToNext()) {
Sunny Goyalc87775d2015-02-10 19:52:36 -0800777 db.execSQL("UPDATE favorites SET rank=cellX+(cellY*?) WHERE "
778 + "container=? AND cellX IS NOT NULL AND cellY IS NOT NULL;",
Sunny Goyal08f72612015-01-05 13:41:43 -0800779 new Object[] {c.getLong(1) + 1, c.getLong(0)});
780 }
781
782 c.close();
783 db.setTransactionSuccessful();
784 } catch (SQLException ex) {
785 // Old version remains, which means we wipe old data
786 Log.e(TAG, ex.getMessage(), ex);
787 return false;
788 } finally {
789 db.endTransaction();
790 }
791 return true;
792 }
793
Kenny Guyed131872014-04-30 03:02:21 +0100794 private boolean addProfileColumn(SQLiteDatabase db) {
Sunny Goyal5d85c442015-03-10 13:14:47 -0700795 UserManagerCompat userManager = UserManagerCompat.getInstance(mContext);
796 // Default to the serial number of this user, for older
797 // shortcuts.
798 long userSerialNumber = userManager.getSerialNumberForUser(
799 UserHandleCompat.myUserHandle());
800 return addIntegerColumn(db, Favorites.PROFILE_ID, userSerialNumber);
801 }
802
803 private boolean addIntegerColumn(SQLiteDatabase db, String columnName, long defaultValue) {
Kenny Guyed131872014-04-30 03:02:21 +0100804 db.beginTransaction();
805 try {
Sunny Goyal5d85c442015-03-10 13:14:47 -0700806 db.execSQL("ALTER TABLE favorites ADD COLUMN "
807 + columnName + " INTEGER NOT NULL DEFAULT " + defaultValue + ";");
Kenny Guyed131872014-04-30 03:02:21 +0100808 db.setTransactionSuccessful();
809 } catch (SQLException ex) {
Kenny Guyed131872014-04-30 03:02:21 +0100810 Log.e(TAG, ex.getMessage(), ex);
811 return false;
812 } finally {
813 db.endTransaction();
814 }
815 return true;
816 }
817
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700818 // Generates a new ID to use for an object in your database. This method should be only
819 // called from the main UI thread. As an exception, we do call it when we call the
820 // constructor from the worker thread; however, this doesn't extend until after the
821 // constructor is called, and we only pass a reference to LauncherProvider to LauncherApp
822 // after that point
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700823 @Override
Adam Cohendcd297f2013-06-18 13:13:40 -0700824 public long generateNewItemId() {
825 if (mMaxItemId < 0) {
826 throw new RuntimeException("Error: max item id was not initialized");
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700827 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700828 mMaxItemId += 1;
829 return mMaxItemId;
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700830 }
831
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700832 @Override
833 public long insertAndCheck(SQLiteDatabase db, ContentValues values) {
834 return dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values);
835 }
836
Winson Chungc763c4e2013-07-19 13:49:06 -0700837 public void updateMaxItemId(long id) {
838 mMaxItemId = id + 1;
839 }
840
Chris Wren5dee7af2013-12-20 17:22:11 -0500841 public void checkId(String table, ContentValues values) {
842 long id = values.getAsLong(LauncherSettings.BaseLauncherColumns._ID);
843 if (table == LauncherProvider.TABLE_WORKSPACE_SCREENS) {
844 mMaxScreenId = Math.max(id, mMaxScreenId);
845 } else {
846 mMaxItemId = Math.max(id, mMaxItemId);
847 }
848 }
849
Adam Cohendcd297f2013-06-18 13:13:40 -0700850 private long initializeMaxItemId(SQLiteDatabase db) {
Sunny Goyalfe4e4b92015-03-04 10:43:45 -0800851 return getMaxId(db, TABLE_FAVORITES);
Adam Cohendcd297f2013-06-18 13:13:40 -0700852 }
853
854 // Generates a new ID to use for an workspace screen in your database. This method
855 // should be only called from the main UI thread. As an exception, we do call it when we
856 // call the constructor from the worker thread; however, this doesn't extend until after the
857 // constructor is called, and we only pass a reference to LauncherProvider to LauncherApp
858 // after that point
859 public long generateNewScreenId() {
860 if (mMaxScreenId < 0) {
861 throw new RuntimeException("Error: max screen id was not initialized");
862 }
863 mMaxScreenId += 1;
Winson Chunga90303b2013-11-15 13:05:06 -0800864 // Log to disk
865 Launcher.addDumpLog(TAG, "11683562 - generateNewScreenId(): " + mMaxScreenId, true);
Adam Cohendcd297f2013-06-18 13:13:40 -0700866 return mMaxScreenId;
867 }
868
Adam Cohendcd297f2013-06-18 13:13:40 -0700869 private long initializeMaxScreenId(SQLiteDatabase db) {
Sunny Goyalfe4e4b92015-03-04 10:43:45 -0800870 return getMaxId(db, TABLE_WORKSPACE_SCREENS);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800871 }
872
Adam Cohen091440a2015-03-18 14:16:05 -0700873 @Thunk boolean initializeExternalAdd(ContentValues values) {
Adam Cohena043fa82014-07-23 14:49:38 -0700874 // 1. Ensure that externally added items have a valid item id
875 long id = generateNewItemId();
876 values.put(LauncherSettings.Favorites._ID, id);
877
878 // 2. In the case of an app widget, and if no app widget id is specified, we
879 // attempt allocate and bind the widget.
880 Integer itemType = values.getAsInteger(LauncherSettings.Favorites.ITEM_TYPE);
881 if (itemType != null &&
882 itemType.intValue() == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET &&
883 !values.containsKey(LauncherSettings.Favorites.APPWIDGET_ID)) {
884
885 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
886 ComponentName cn = ComponentName.unflattenFromString(
887 values.getAsString(Favorites.APPWIDGET_PROVIDER));
888
889 if (cn != null) {
890 try {
891 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
Adam Cohen3ed316a2014-07-23 18:21:20 -0700892 values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId);
Adam Coheneb1ac422014-10-14 08:55:28 -0700893 if (!appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,cn)) {
Adam Cohend61a6382014-10-14 15:04:34 -0700894 return false;
Adam Cohena043fa82014-07-23 14:49:38 -0700895 }
896 } catch (RuntimeException e) {
897 Log.e(TAG, "Failed to initialize external widget", e);
Adam Coheneb1ac422014-10-14 08:55:28 -0700898 return false;
Adam Cohena043fa82014-07-23 14:49:38 -0700899 }
Adam Coheneb1ac422014-10-14 08:55:28 -0700900 } else {
901 return false;
Adam Cohena043fa82014-07-23 14:49:38 -0700902 }
903 }
Adam Cohen7ec3bbf2014-07-31 00:09:45 -0700904
905 // Add screen id if not present
906 long screenId = values.getAsLong(LauncherSettings.Favorites.SCREEN);
907 if (!addScreenIdIfNecessary(screenId)) {
908 return false;
909 }
Adam Cohena043fa82014-07-23 14:49:38 -0700910 return true;
911 }
912
Adam Cohen7ec3bbf2014-07-31 00:09:45 -0700913 // Returns true of screen id exists, or if successfully added
914 private boolean addScreenIdIfNecessary(long screenId) {
915 if (!hasScreenId(screenId)) {
916 int rank = getMaxScreenRank() + 1;
917
918 ContentValues v = new ContentValues();
919 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
920 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, rank);
921 if (dbInsertAndCheck(this, getWritableDatabase(),
922 TABLE_WORKSPACE_SCREENS, null, v) < 0) {
923 return false;
924 }
925 }
926 return true;
927 }
928
929 private boolean hasScreenId(long screenId) {
930 SQLiteDatabase db = getWritableDatabase();
931 Cursor c = db.rawQuery("SELECT * FROM " + TABLE_WORKSPACE_SCREENS + " WHERE "
932 + LauncherSettings.WorkspaceScreens._ID + " = " + screenId, null);
933 if (c != null) {
934 int count = c.getCount();
935 c.close();
936 return count > 0;
937 } else {
938 return false;
939 }
940 }
941
942 private int getMaxScreenRank() {
943 SQLiteDatabase db = getWritableDatabase();
944 Cursor c = db.rawQuery("SELECT MAX(" + LauncherSettings.WorkspaceScreens.SCREEN_RANK
945 + ") FROM " + TABLE_WORKSPACE_SCREENS, null);
946
947 // get the result
948 final int maxRankIndex = 0;
949 int rank = -1;
950 if (c != null && c.moveToNext()) {
951 rank = c.getInt(maxRankIndex);
952 }
953 if (c != null) {
954 c.close();
955 }
956
957 return rank;
958 }
959
Adam Cohen091440a2015-03-18 14:16:05 -0700960 @Thunk int loadFavorites(SQLiteDatabase db, AutoInstallsLayout loader) {
Adam Cohen71483f42014-05-15 14:04:01 -0700961 ArrayList<Long> screenIds = new ArrayList<Long>();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700962 // TODO: Use multiple loaders with fall-back and transaction.
963 int count = loader.loadLayout(db, screenIds);
Adam Cohen71483f42014-05-15 14:04:01 -0700964
965 // Add the screens specified by the items above
966 Collections.sort(screenIds);
967 int rank = 0;
968 ContentValues values = new ContentValues();
969 for (Long id : screenIds) {
970 values.clear();
971 values.put(LauncherSettings.WorkspaceScreens._ID, id);
972 values.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, rank);
973 if (dbInsertAndCheck(this, db, TABLE_WORKSPACE_SCREENS, null, values) < 0) {
974 throw new RuntimeException("Failed initialize screen table"
975 + "from default layout");
976 }
977 rank++;
978 }
979
980 // Ensure that the max ids are initialized
981 mMaxItemId = initializeMaxItemId(db);
982 mMaxScreenId = initializeMaxScreenId(db);
Adam Cohen7ec3bbf2014-07-31 00:09:45 -0700983
Adam Cohen71483f42014-05-15 14:04:01 -0700984 return count;
985 }
986
Adam Cohen091440a2015-03-18 14:16:05 -0700987 @Thunk void migrateLauncher2Shortcuts(SQLiteDatabase db, Uri uri) {
Dan Sandlerd5024042014-01-09 15:01:33 -0500988 final ContentResolver resolver = mContext.getContentResolver();
989 Cursor c = null;
990 int count = 0;
991 int curScreen = 0;
992
993 try {
994 c = resolver.query(uri, null, null, null, "title ASC");
995 } catch (Exception e) {
996 // Ignore
997 }
998
Dan Sandlerd5024042014-01-09 15:01:33 -0500999 // We already have a favorites database in the old provider
1000 if (c != null) {
1001 try {
1002 if (c.getCount() > 0) {
1003 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1004 final int intentIndex
1005 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
1006 final int titleIndex
1007 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
1008 final int iconTypeIndex
1009 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
1010 final int iconIndex
1011 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1012 final int iconPackageIndex
1013 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
1014 final int iconResourceIndex
1015 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
1016 final int containerIndex
1017 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
1018 final int itemTypeIndex
1019 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
1020 final int screenIndex
1021 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
1022 final int cellXIndex
1023 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
1024 final int cellYIndex
1025 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
1026 final int uriIndex
1027 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1028 final int displayModeIndex
1029 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
Kenny Guyed131872014-04-30 03:02:21 +01001030 final int profileIndex
1031 = c.getColumnIndex(LauncherSettings.Favorites.PROFILE_ID);
Dan Sandlerd5024042014-01-09 15:01:33 -05001032
1033 int i = 0;
1034 int curX = 0;
1035 int curY = 0;
1036
1037 final LauncherAppState app = LauncherAppState.getInstance();
1038 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1039 final int width = (int) grid.numColumns;
1040 final int height = (int) grid.numRows;
1041 final int hotseatWidth = (int) grid.numHotseatIcons;
1042
1043 final HashSet<String> seenIntents = new HashSet<String>(c.getCount());
1044
Adam Cohen72960972014-01-15 18:13:55 -08001045 final ArrayList<ContentValues> shortcuts = new ArrayList<ContentValues>();
1046 final ArrayList<ContentValues> folders = new ArrayList<ContentValues>();
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001047 final SparseArray<ContentValues> hotseat = new SparseArray<ContentValues>();
Dan Sandlerd5024042014-01-09 15:01:33 -05001048
1049 while (c.moveToNext()) {
1050 final int itemType = c.getInt(itemTypeIndex);
1051 if (itemType != Favorites.ITEM_TYPE_APPLICATION
1052 && itemType != Favorites.ITEM_TYPE_SHORTCUT
1053 && itemType != Favorites.ITEM_TYPE_FOLDER) {
1054 continue;
1055 }
1056
1057 final int cellX = c.getInt(cellXIndex);
1058 final int cellY = c.getInt(cellYIndex);
1059 final int screen = c.getInt(screenIndex);
1060 int container = c.getInt(containerIndex);
1061 final String intentStr = c.getString(intentIndex);
Kenny Guyed131872014-04-30 03:02:21 +01001062
1063 UserManagerCompat userManager = UserManagerCompat.getInstance(mContext);
1064 UserHandleCompat userHandle;
1065 final long userSerialNumber;
1066 if (profileIndex != -1 && !c.isNull(profileIndex)) {
1067 userSerialNumber = c.getInt(profileIndex);
1068 userHandle = userManager.getUserForSerialNumber(userSerialNumber);
1069 } else {
1070 // Default to the serial number of this user, for older
1071 // shortcuts.
1072 userHandle = UserHandleCompat.myUserHandle();
1073 userSerialNumber = userManager.getSerialNumberForUser(userHandle);
1074 }
Sunny Goyal416541c2014-11-14 11:59:57 -08001075
1076 if (userHandle == null) {
1077 Launcher.addDumpLog(TAG, "skipping deleted user", true);
1078 continue;
1079 }
1080
Dan Sandlerd5024042014-01-09 15:01:33 -05001081 Launcher.addDumpLog(TAG, "migrating \""
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001082 + c.getString(titleIndex) + "\" ("
1083 + cellX + "," + cellY + "@"
1084 + LauncherSettings.Favorites.containerToString(container)
1085 + "/" + screen
1086 + "): " + intentStr, true);
Dan Sandlerd5024042014-01-09 15:01:33 -05001087
1088 if (itemType != Favorites.ITEM_TYPE_FOLDER) {
Adam Cohen556f6132014-01-15 15:18:08 -08001089
1090 final Intent intent;
1091 final ComponentName cn;
1092 try {
1093 intent = Intent.parseUri(intentStr, 0);
1094 } catch (URISyntaxException e) {
1095 // bogus intent?
1096 Launcher.addDumpLog(TAG,
1097 "skipping invalid intent uri", true);
1098 continue;
1099 }
1100
1101 cn = intent.getComponent();
Dan Sandlerd5024042014-01-09 15:01:33 -05001102 if (TextUtils.isEmpty(intentStr)) {
1103 // no intent? no icon
1104 Launcher.addDumpLog(TAG, "skipping empty intent", true);
1105 continue;
Adam Cohen72960972014-01-15 18:13:55 -08001106 } else if (cn != null &&
Kenny Guyed131872014-04-30 03:02:21 +01001107 !LauncherModel.isValidPackageActivity(mContext, cn,
1108 userHandle)) {
Adam Cohen556f6132014-01-15 15:18:08 -08001109 // component no longer exists.
Adam Cohen72960972014-01-15 18:13:55 -08001110 Launcher.addDumpLog(TAG, "skipping item whose component " +
Adam Cohen556f6132014-01-15 15:18:08 -08001111 "no longer exists.", true);
1112 continue;
Adam Cohen72960972014-01-15 18:13:55 -08001113 } else if (container ==
1114 LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1115 // Dedupe icons directly on the workspace
1116
Adam Cohen556f6132014-01-15 15:18:08 -08001117 // Canonicalize
1118 // the Play Store sets the package parameter, but Launcher
Adam Cohena33f11e2014-10-24 12:20:20 -07001119 // does not, so we clear that out to keep them the same.
1120 // Also ignore intent flags for the purposes of deduping.
Adam Cohen556f6132014-01-15 15:18:08 -08001121 intent.setPackage(null);
Adam Cohena33f11e2014-10-24 12:20:20 -07001122 int flags = intent.getFlags();
1123 intent.setFlags(0);
Adam Cohen556f6132014-01-15 15:18:08 -08001124 final String key = intent.toUri(0);
Adam Cohena33f11e2014-10-24 12:20:20 -07001125 intent.setFlags(flags);
Adam Cohen556f6132014-01-15 15:18:08 -08001126 if (seenIntents.contains(key)) {
1127 Launcher.addDumpLog(TAG, "skipping duplicate", true);
Dan Sandlerd5024042014-01-09 15:01:33 -05001128 continue;
Adam Cohen556f6132014-01-15 15:18:08 -08001129 } else {
1130 seenIntents.add(key);
Dan Sandlerd5024042014-01-09 15:01:33 -05001131 }
1132 }
1133 }
1134
1135 ContentValues values = new ContentValues(c.getColumnCount());
1136 values.put(LauncherSettings.Favorites._ID, c.getInt(idIndex));
1137 values.put(LauncherSettings.Favorites.INTENT, intentStr);
1138 values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex));
1139 values.put(LauncherSettings.Favorites.ICON_TYPE,
1140 c.getInt(iconTypeIndex));
1141 values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex));
1142 values.put(LauncherSettings.Favorites.ICON_PACKAGE,
1143 c.getString(iconPackageIndex));
1144 values.put(LauncherSettings.Favorites.ICON_RESOURCE,
1145 c.getString(iconResourceIndex));
1146 values.put(LauncherSettings.Favorites.ITEM_TYPE, itemType);
1147 values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1);
1148 values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex));
1149 values.put(LauncherSettings.Favorites.DISPLAY_MODE,
1150 c.getInt(displayModeIndex));
Kenny Guyed131872014-04-30 03:02:21 +01001151 values.put(LauncherSettings.Favorites.PROFILE_ID, userSerialNumber);
Dan Sandlerd5024042014-01-09 15:01:33 -05001152
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001153 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1154 hotseat.put(screen, values);
Dan Sandlerd5024042014-01-09 15:01:33 -05001155 }
1156
1157 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1158 // In a folder or in the hotseat, preserve position
1159 values.put(LauncherSettings.Favorites.SCREEN, screen);
1160 values.put(LauncherSettings.Favorites.CELLX, cellX);
1161 values.put(LauncherSettings.Favorites.CELLY, cellY);
1162 } else {
Adam Cohen72960972014-01-15 18:13:55 -08001163 // For items contained directly on one of the workspace screen,
1164 // we'll determine their location (screen, x, y) in a second pass.
Dan Sandlerd5024042014-01-09 15:01:33 -05001165 }
1166
1167 values.put(LauncherSettings.Favorites.CONTAINER, container);
1168
Adam Cohen72960972014-01-15 18:13:55 -08001169 if (itemType != Favorites.ITEM_TYPE_FOLDER) {
1170 shortcuts.add(values);
1171 } else {
1172 folders.add(values);
1173 }
Dan Sandlerd5024042014-01-09 15:01:33 -05001174 }
1175
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001176 // Now that we have all the hotseat icons, let's go through them left-right
1177 // and assign valid locations for them in the new hotseat
1178 final int N = hotseat.size();
1179 for (int idx=0; idx<N; idx++) {
1180 int hotseatX = hotseat.keyAt(idx);
1181 ContentValues values = hotseat.valueAt(idx);
1182
1183 if (hotseatX == grid.hotseatAllAppsRank) {
1184 // let's drop this in the next available hole in the hotseat
1185 while (++hotseatX < hotseatWidth) {
1186 if (hotseat.get(hotseatX) == null) {
1187 // found a spot! move it here
1188 values.put(LauncherSettings.Favorites.SCREEN,
1189 hotseatX);
1190 break;
1191 }
1192 }
1193 }
1194 if (hotseatX >= hotseatWidth) {
1195 // no room for you in the hotseat? it's off to the desktop with you
1196 values.put(LauncherSettings.Favorites.CONTAINER,
1197 Favorites.CONTAINER_DESKTOP);
1198 }
1199 }
1200
Adam Cohen72960972014-01-15 18:13:55 -08001201 final ArrayList<ContentValues> allItems = new ArrayList<ContentValues>();
1202 // Folders first
1203 allItems.addAll(folders);
1204 // Then shortcuts
1205 allItems.addAll(shortcuts);
1206
1207 // Layout all the folders
1208 for (ContentValues values: allItems) {
1209 if (values.getAsInteger(LauncherSettings.Favorites.CONTAINER) !=
1210 LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1211 // Hotseat items and folder items have already had their
1212 // location information set. Nothing to be done here.
1213 continue;
1214 }
1215 values.put(LauncherSettings.Favorites.SCREEN, curScreen);
1216 values.put(LauncherSettings.Favorites.CELLX, curX);
1217 values.put(LauncherSettings.Favorites.CELLY, curY);
1218 curX = (curX + 1) % width;
1219 if (curX == 0) {
1220 curY = (curY + 1);
1221 }
1222 // Leave the last row of icons blank on every screen
1223 if (curY == height - 1) {
1224 curScreen = (int) generateNewScreenId();
1225 curY = 0;
1226 }
1227 }
1228
1229 if (allItems.size() > 0) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001230 db.beginTransaction();
1231 try {
Adam Cohen72960972014-01-15 18:13:55 -08001232 for (ContentValues row: allItems) {
1233 if (row == null) continue;
1234 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, row)
Dan Sandlerd5024042014-01-09 15:01:33 -05001235 < 0) {
1236 return;
1237 } else {
1238 count++;
1239 }
1240 }
1241 db.setTransactionSuccessful();
1242 } finally {
1243 db.endTransaction();
1244 }
1245 }
1246
1247 db.beginTransaction();
1248 try {
1249 for (i=0; i<=curScreen; i++) {
1250 final ContentValues values = new ContentValues();
1251 values.put(LauncherSettings.WorkspaceScreens._ID, i);
1252 values.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
1253 if (dbInsertAndCheck(this, db, TABLE_WORKSPACE_SCREENS, null, values)
1254 < 0) {
1255 return;
1256 }
1257 }
1258 db.setTransactionSuccessful();
1259 } finally {
1260 db.endTransaction();
1261 }
Sunny Goyal08f72612015-01-05 13:41:43 -08001262
1263 updateFolderItemsRank(db, false);
Dan Sandlerd5024042014-01-09 15:01:33 -05001264 }
1265 } finally {
1266 c.close();
1267 }
1268 }
1269
1270 Launcher.addDumpLog(TAG, "migrated " + count + " icons from Launcher2 into "
1271 + (curScreen+1) + " screens", true);
1272
1273 // ensure that new screens are created to hold these icons
1274 setFlagJustLoadedOldDb();
1275
1276 // Update max IDs; very important since we just grabbed IDs from another database
1277 mMaxItemId = initializeMaxItemId(db);
1278 mMaxScreenId = initializeMaxScreenId(db);
1279 if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId + " mMaxScreenId: " + mMaxScreenId);
1280 }
Mike Cleronb87bd162009-10-30 16:36:56 -07001281 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001282
Sunny Goyalfe4e4b92015-03-04 10:43:45 -08001283 /**
1284 * @return the max _id in the provided table.
1285 */
Adam Cohen091440a2015-03-18 14:16:05 -07001286 @Thunk static long getMaxId(SQLiteDatabase db, String table) {
Sunny Goyalfe4e4b92015-03-04 10:43:45 -08001287 Cursor c = db.rawQuery("SELECT MAX(_id) FROM " + table, null);
1288 // get the result
1289 long id = -1;
1290 if (c != null && c.moveToNext()) {
1291 id = c.getLong(0);
1292 }
1293 if (c != null) {
1294 c.close();
1295 }
1296
1297 if (id == -1) {
1298 throw new RuntimeException("Error: could not query max id in " + table);
1299 }
1300
1301 return id;
1302 }
1303
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 static class SqlArguments {
1305 public final String table;
1306 public final String where;
1307 public final String[] args;
1308
1309 SqlArguments(Uri url, String where, String[] args) {
1310 if (url.getPathSegments().size() == 1) {
1311 this.table = url.getPathSegments().get(0);
1312 this.where = where;
1313 this.args = args;
1314 } else if (url.getPathSegments().size() != 2) {
1315 throw new IllegalArgumentException("Invalid URI: " + url);
1316 } else if (!TextUtils.isEmpty(where)) {
1317 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
1318 } else {
1319 this.table = url.getPathSegments().get(0);
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001320 this.where = "_id=" + ContentUris.parseId(url);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321 this.args = null;
1322 }
1323 }
1324
1325 SqlArguments(Uri url) {
1326 if (url.getPathSegments().size() == 1) {
1327 table = url.getPathSegments().get(0);
1328 where = null;
1329 args = null;
1330 } else {
1331 throw new IllegalArgumentException("Invalid URI: " + url);
1332 }
1333 }
1334 }
Adam Cohen72960972014-01-15 18:13:55 -08001335}