blob: cadc06eefd802466f44ce9c8631a22a83a861e34 [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
The Android Open Source Project7376fae2009-03-11 12:11:58 -070019import android.appwidget.AppWidgetHost;
Mike Cleronb87bd162009-10-30 16:36:56 -070020import android.appwidget.AppWidgetManager;
Bjorn Bringertcd8fec02010-01-14 13:26:43 +000021import android.appwidget.AppWidgetProviderInfo;
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;
Adam Cohen228da5a2011-07-27 22:23:47 -070033import android.content.res.Resources;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.database.Cursor;
35import android.database.SQLException;
Adam Cohen228da5a2011-07-27 22:23:47 -070036import android.database.sqlite.SQLiteDatabase;
37import android.database.sqlite.SQLiteOpenHelper;
38import android.database.sqlite.SQLiteQueryBuilder;
39import android.database.sqlite.SQLiteStatement;
Joe Onorato0589f0f2010-02-08 13:44:00 -080040import android.graphics.Bitmap;
41import android.graphics.BitmapFactory;
Adam Cohen228da5a2011-07-27 22:23:47 -070042import android.net.Uri;
43import android.provider.Settings;
44import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.util.Log;
Dan Sandlerab5fa3a2014-03-06 23:48:04 -050046import android.util.SparseArray;
Adam Cohen228da5a2011-07-27 22:23:47 -070047
Sunny Goyal0fe505b2014-08-06 09:55:36 -070048import com.android.launcher3.AutoInstallsLayout.LayoutParserCallback;
49import com.android.launcher3.LauncherSettings.Favorites;
Kenny Guyed131872014-04-30 03:02:21 +010050import com.android.launcher3.compat.UserHandleCompat;
51import com.android.launcher3.compat.UserManagerCompat;
Chris Wrene523e702013-10-09 10:36:55 -040052import com.android.launcher3.config.ProviderConfig;
Michael Jurka8b805b12012-04-18 14:23:14 -070053
Dan Sandlerd5024042014-01-09 15:01:33 -050054import java.io.File;
Mike Cleronb87bd162009-10-30 16:36:56 -070055import java.net.URISyntaxException;
Adam Cohen228da5a2011-07-27 22:23:47 -070056import java.util.ArrayList;
Adam Cohen71483f42014-05-15 14:04:01 -070057import java.util.Collections;
Dan Sandlerd5024042014-01-09 15:01:33 -050058import java.util.HashSet;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060public class LauncherProvider extends ContentProvider {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080061 private static final String TAG = "Launcher.LauncherProvider";
62 private static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063
Kenny Guyed131872014-04-30 03:02:21 +010064 private static final int DATABASE_VERSION = 20;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065
Adam Cohene25af792013-06-06 23:08:25 -070066 static final String OLD_AUTHORITY = "com.android.launcher2.settings";
Chris Wrene523e702013-10-09 10:36:55 -040067 static final String AUTHORITY = ProviderConfig.AUTHORITY;
Winson Chung3d503fb2011-07-13 17:25:49 -070068
Dan Sandlerf0b8dac2013-11-19 12:21:25 -050069 // Should we attempt to load anything from the com.android.launcher2 provider?
Dan Sandlerd5024042014-01-09 15:01:33 -050070 static final boolean IMPORT_LAUNCHER2_DATABASE = false;
Dan Sandlerf0b8dac2013-11-19 12:21:25 -050071
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072 static final String TABLE_FAVORITES = "favorites";
Adam Cohendcd297f2013-06-18 13:13:40 -070073 static final String TABLE_WORKSPACE_SCREENS = "workspaceScreens";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074 static final String PARAMETER_NOTIFY = "notify";
Winson Chungc763c4e2013-07-19 13:49:06 -070075 static final String UPGRADED_FROM_OLD_DATABASE =
76 "UPGRADED_FROM_OLD_DATABASE";
77 static final String EMPTY_DATABASE_CREATED =
78 "EMPTY_DATABASE_CREATED";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079
Adam Cohena043fa82014-07-23 14:49:38 -070080 private static final String URI_PARAM_IS_EXTERNAL_ADD = "isExternalAdd";
81
Anjali Koppal67e7cae2014-03-13 12:14:12 -070082 private LauncherProviderChangeListener mListener;
83
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -070084 /**
Romain Guy73b979d2009-06-09 12:57:21 -070085 * {@link Uri} triggered at any registered {@link android.database.ContentObserver} when
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -070086 * {@link AppWidgetHost#deleteHost()} is called during database creation.
87 * Use this to recall {@link AppWidgetHost#startListening()} if needed.
88 */
89 static final Uri CONTENT_APPWIDGET_RESET_URI =
90 Uri.parse("content://" + AUTHORITY + "/appWidgetReset");
Daniel Lehmannc3a80402012-04-23 21:35:11 -070091
Michael Jurkaa8c760d2011-04-28 14:59:33 -070092 private DatabaseHelper mOpenHelper;
Winson Chungc763c4e2013-07-19 13:49:06 -070093 private static boolean sJustLoadedFromOldDb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094
95 @Override
96 public boolean onCreate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040097 final Context context = getContext();
98 mOpenHelper = new DatabaseHelper(context);
99 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
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700136 private static long dbInsertAndCheck(DatabaseHelper helper,
137 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);
154 if (externalAdd != null && "true".equals(externalAdd)) {
155 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);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 if (rowId <= 0) return null;
164
165 uri = ContentUris.withAppendedId(uri, rowId);
166 sendNotify(uri);
167
168 return uri;
169 }
170
Adam Cohena043fa82014-07-23 14:49:38 -0700171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 @Override
173 public int bulkInsert(Uri uri, ContentValues[] values) {
174 SqlArguments args = new SqlArguments(uri);
175
176 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
177 db.beginTransaction();
178 try {
179 int numValues = values.length;
180 for (int i = 0; i < numValues; i++) {
Chris Wren1ada10d2013-09-13 18:01:38 -0400181 addModifiedTime(values[i]);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700182 if (dbInsertAndCheck(mOpenHelper, db, args.table, null, values[i]) < 0) {
183 return 0;
184 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 }
186 db.setTransactionSuccessful();
187 } finally {
188 db.endTransaction();
189 }
190
191 sendNotify(uri);
192 return values.length;
193 }
194
195 @Override
Yura085c8532014-02-11 15:15:29 +0000196 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
197 throws OperationApplicationException {
198 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
199 db.beginTransaction();
200 try {
201 ContentProviderResult[] result = super.applyBatch(operations);
202 db.setTransactionSuccessful();
203 return result;
204 } finally {
205 db.endTransaction();
206 }
207 }
208
209 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210 public int delete(Uri uri, String selection, String[] selectionArgs) {
211 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
212
213 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
214 int count = db.delete(args.table, args.where, args.args);
215 if (count > 0) sendNotify(uri);
216
217 return count;
218 }
219
220 @Override
221 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
222 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
223
Chris Wren1ada10d2013-09-13 18:01:38 -0400224 addModifiedTime(values);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
226 int count = db.update(args.table, values, args.where, args.args);
227 if (count > 0) sendNotify(uri);
228
229 return count;
230 }
231
232 private void sendNotify(Uri uri) {
233 String notify = uri.getQueryParameter(PARAMETER_NOTIFY);
234 if (notify == null || "true".equals(notify)) {
235 getContext().getContentResolver().notifyChange(uri, null);
236 }
Chris Wren1ada10d2013-09-13 18:01:38 -0400237
238 // always notify the backup agent
Chris Wren92aa4232013-10-04 11:29:36 -0400239 LauncherBackupAgentHelper.dataChanged(getContext());
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700240 if (mListener != null) {
241 mListener.onLauncherProviderChange();
242 }
Chris Wren1ada10d2013-09-13 18:01:38 -0400243 }
244
245 private void addModifiedTime(ContentValues values) {
246 values.put(LauncherSettings.ChangeLogColumns.MODIFIED, System.currentTimeMillis());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247 }
248
Adam Cohendcd297f2013-06-18 13:13:40 -0700249 public long generateNewItemId() {
250 return mOpenHelper.generateNewItemId();
251 }
252
Winson Chungc763c4e2013-07-19 13:49:06 -0700253 public void updateMaxItemId(long id) {
254 mOpenHelper.updateMaxItemId(id);
255 }
256
Adam Cohendcd297f2013-06-18 13:13:40 -0700257 public long generateNewScreenId() {
258 return mOpenHelper.generateNewScreenId();
259 }
260
261 // This is only required one time while loading the workspace during the
262 // upgrade path, and should never be called from anywhere else.
263 public void updateMaxScreenId(long maxScreenId) {
264 mOpenHelper.updateMaxScreenId(maxScreenId);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700265 }
266
Brian Muramatsu5524b492012-10-02 16:55:54 -0700267 /**
Adam Cohene25af792013-06-06 23:08:25 -0700268 * @param Should we load the old db for upgrade? first run only.
269 */
Winson Chungc763c4e2013-07-19 13:49:06 -0700270 synchronized public boolean justLoadedOldDb() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400271 String spKey = LauncherAppState.getSharedPreferencesKey();
Adam Cohene25af792013-06-06 23:08:25 -0700272 SharedPreferences sp = getContext().getSharedPreferences(spKey, Context.MODE_PRIVATE);
273
Winson Chungc763c4e2013-07-19 13:49:06 -0700274 boolean loadedOldDb = false || sJustLoadedFromOldDb;
Adam Cohendcd297f2013-06-18 13:13:40 -0700275
Winson Chungc763c4e2013-07-19 13:49:06 -0700276 sJustLoadedFromOldDb = false;
277 if (sp.getBoolean(UPGRADED_FROM_OLD_DATABASE, false)) {
Adam Cohene25af792013-06-06 23:08:25 -0700278
279 SharedPreferences.Editor editor = sp.edit();
Winson Chungc763c4e2013-07-19 13:49:06 -0700280 editor.remove(UPGRADED_FROM_OLD_DATABASE);
Adam Cohene25af792013-06-06 23:08:25 -0700281 editor.commit();
Winson Chungc763c4e2013-07-19 13:49:06 -0700282 loadedOldDb = true;
Adam Cohene25af792013-06-06 23:08:25 -0700283 }
Winson Chungc763c4e2013-07-19 13:49:06 -0700284 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -0700285 }
286
287 /**
Sunny Goyal42de82f2014-09-26 22:09:29 -0700288 * Clears all the data for a fresh start.
289 */
290 synchronized public void createEmptyDB() {
291 mOpenHelper.createEmptyDB(mOpenHelper.getWritableDatabase());
292 }
293
Sunny Goyal33d44382014-10-16 09:24:19 -0700294 public void clearFlagEmptyDbCreated() {
295 String spKey = LauncherAppState.getSharedPreferencesKey();
296 getContext().getSharedPreferences(spKey, Context.MODE_PRIVATE)
297 .edit()
298 .remove(EMPTY_DATABASE_CREATED)
299 .commit();
300 }
301
Sunny Goyal42de82f2014-09-26 22:09:29 -0700302 /**
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700303 * Loads the default workspace based on the following priority scheme:
304 * 1) From a package provided by play store
305 * 2) From a partner configuration APK, already in the system image
306 * 3) The default configuration for the particular device
Brian Muramatsu5524b492012-10-02 16:55:54 -0700307 */
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700308 synchronized public void loadDefaultFavoritesIfNecessary() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400309 String spKey = LauncherAppState.getSharedPreferencesKey();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700310 SharedPreferences sp = getContext().getSharedPreferences(spKey, Context.MODE_PRIVATE);
Adam Cohene25af792013-06-06 23:08:25 -0700311
Winson Chungc763c4e2013-07-19 13:49:06 -0700312 if (sp.getBoolean(EMPTY_DATABASE_CREATED, false)) {
Chris Wren5dee7af2013-12-20 17:22:11 -0500313 Log.d(TAG, "loading default workspace");
Michael Jurka45355c42012-10-08 13:21:35 +0200314
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700315 AutoInstallsLayout loader = AutoInstallsLayout.get(getContext(),
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700316 mOpenHelper.mAppWidgetHost, mOpenHelper);
317
318 if (loader == null) {
Adam Cohen9b8f51f2014-05-30 15:34:09 -0700319 final Partner partner = Partner.get(getContext().getPackageManager());
320 if (partner != null && partner.hasDefaultLayout()) {
321 final Resources partnerRes = partner.getResources();
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700322 int workspaceResId = partnerRes.getIdentifier(Partner.RES_DEFAULT_LAYOUT,
Adam Cohen9b8f51f2014-05-30 15:34:09 -0700323 "xml", partner.getPackageName());
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700324 if (workspaceResId != 0) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700325 loader = new DefaultLayoutParser(getContext(), mOpenHelper.mAppWidgetHost,
326 mOpenHelper, partnerRes, workspaceResId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700327 }
Adam Cohen9b8f51f2014-05-30 15:34:09 -0700328 }
329 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700330
Sunny Goyal9d219682014-10-23 14:21:02 -0700331 final boolean usingExternallyProvidedLayout = loader != null;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700332 if (loader == null) {
Sunny Goyal9d219682014-10-23 14:21:02 -0700333 loader = getDefaultLayoutParser();
Brian Muramatsu5524b492012-10-02 16:55:54 -0700334 }
Michael Jurkab85f8a42012-04-25 15:48:32 -0700335 // Populate favorites table with initial favorites
Sunny Goyal9d219682014-10-23 14:21:02 -0700336 if ((mOpenHelper.loadFavorites(mOpenHelper.getWritableDatabase(), loader) <= 0)
337 && usingExternallyProvidedLayout) {
338 // Unable to load external layout. Cleanup and load the internal layout.
339 createEmptyDB();
340 mOpenHelper.loadFavorites(mOpenHelper.getWritableDatabase(),
341 getDefaultLayoutParser());
342 }
Sunny Goyal33d44382014-10-16 09:24:19 -0700343 clearFlagEmptyDbCreated();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700344 }
345 }
346
Sunny Goyal9d219682014-10-23 14:21:02 -0700347 private DefaultLayoutParser getDefaultLayoutParser() {
348 int defaultLayout = LauncherAppState.getInstance()
349 .getDynamicGrid().getDeviceProfile().defaultLayoutId;
350 return new DefaultLayoutParser(getContext(), mOpenHelper.mAppWidgetHost,
351 mOpenHelper, getContext().getResources(), defaultLayout);
352 }
353
Dan Sandlerd5024042014-01-09 15:01:33 -0500354 public void migrateLauncher2Shortcuts() {
355 mOpenHelper.migrateLauncher2Shortcuts(mOpenHelper.getWritableDatabase(),
Jason Monk0bfcceb2014-03-21 15:42:06 -0400356 Uri.parse(getContext().getString(R.string.old_launcher_provider_uri)));
Dan Sandlerd5024042014-01-09 15:01:33 -0500357 }
358
Winson Chungc763c4e2013-07-19 13:49:06 -0700359 private static interface ContentValuesCallback {
360 public void onRow(ContentValues values);
361 }
362
Adam Cohen6dbe0492013-12-02 17:00:14 -0800363 private static boolean shouldImportLauncher2Database(Context context) {
364 boolean isTablet = context.getResources().getBoolean(R.bool.is_tablet);
365
366 // We don't import the old databse for tablets, as the grid size has changed.
367 return !isTablet && IMPORT_LAUNCHER2_DATABASE;
368 }
369
Dan Sandlerd5024042014-01-09 15:01:33 -0500370 public void deleteDatabase() {
371 // Are you sure? (y/n)
372 final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
Dan Sandler2b471742014-01-21 14:14:41 -0500373 final File dbFile = new File(db.getPath());
Dan Sandlerd5024042014-01-09 15:01:33 -0500374 mOpenHelper.close();
Dan Sandler2b471742014-01-21 14:14:41 -0500375 if (dbFile.exists()) {
376 SQLiteDatabase.deleteDatabase(dbFile);
377 }
Dan Sandlerd5024042014-01-09 15:01:33 -0500378 mOpenHelper = new DatabaseHelper(getContext());
379 }
380
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700381 private static class DatabaseHelper extends SQLiteOpenHelper implements LayoutParserCallback {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382 private final Context mContext;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700383 private final AppWidgetHost mAppWidgetHost;
Adam Cohendcd297f2013-06-18 13:13:40 -0700384 private long mMaxItemId = -1;
385 private long mMaxScreenId = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386
Winson Chung0b560dd2014-01-21 13:00:26 -0800387 private boolean mNewDbCreated = false;
388
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 DatabaseHelper(Context context) {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100390 super(context, LauncherFiles.LAUNCHER_DB, null, DATABASE_VERSION);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391 mContext = context;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700392 mAppWidgetHost = new AppWidgetHost(context, Launcher.APPWIDGET_HOST_ID);
Winson Chung3d503fb2011-07-13 17:25:49 -0700393
394 // In the case where neither onCreate nor onUpgrade gets called, we read the maxId from
395 // the DB here
Adam Cohendcd297f2013-06-18 13:13:40 -0700396 if (mMaxItemId == -1) {
397 mMaxItemId = initializeMaxItemId(getWritableDatabase());
398 }
399 if (mMaxScreenId == -1) {
400 mMaxScreenId = initializeMaxScreenId(getWritableDatabase());
Winson Chung3d503fb2011-07-13 17:25:49 -0700401 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800402 }
403
Winson Chung0b560dd2014-01-21 13:00:26 -0800404 public boolean wasNewDbCreated() {
405 return mNewDbCreated;
406 }
407
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700408 /**
409 * Send notification that we've deleted the {@link AppWidgetHost},
410 * probably as part of the initial database creation. The receiver may
411 * want to re-call {@link AppWidgetHost#startListening()} to ensure
412 * callbacks are correctly set.
413 */
414 private void sendAppWidgetResetNotify() {
415 final ContentResolver resolver = mContext.getContentResolver();
416 resolver.notifyChange(CONTENT_APPWIDGET_RESET_URI, null);
417 }
418
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800419 @Override
420 public void onCreate(SQLiteDatabase db) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800421 if (LOGD) Log.d(TAG, "creating new launcher database");
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700422
Adam Cohendcd297f2013-06-18 13:13:40 -0700423 mMaxItemId = 1;
424 mMaxScreenId = 0;
Winson Chung0b560dd2014-01-21 13:00:26 -0800425 mNewDbCreated = true;
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700426
Kenny Guyed131872014-04-30 03:02:21 +0100427 UserManagerCompat userManager = UserManagerCompat.getInstance(mContext);
428 long userSerialNumber = userManager.getSerialNumberForUser(
429 UserHandleCompat.myUserHandle());
430
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 db.execSQL("CREATE TABLE favorites (" +
432 "_id INTEGER PRIMARY KEY," +
433 "title TEXT," +
434 "intent TEXT," +
435 "container INTEGER," +
436 "screen INTEGER," +
437 "cellX INTEGER," +
438 "cellY INTEGER," +
439 "spanX INTEGER," +
440 "spanY INTEGER," +
441 "itemType INTEGER," +
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700442 "appWidgetId INTEGER NOT NULL DEFAULT -1," +
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443 "isShortcut INTEGER," +
444 "iconType INTEGER," +
445 "iconPackage TEXT," +
446 "iconResource TEXT," +
447 "icon BLOB," +
448 "uri TEXT," +
Chris Wrend5e66bf2013-09-16 14:02:29 -0400449 "displayMode INTEGER," +
Chris Wren1ada10d2013-09-13 18:01:38 -0400450 "appWidgetProvider TEXT," +
Chris Wrenf4d08112014-01-16 18:13:56 -0500451 "modified INTEGER NOT NULL DEFAULT 0," +
Kenny Guyed131872014-04-30 03:02:21 +0100452 "restored INTEGER NOT NULL DEFAULT 0," +
453 "profileId INTEGER DEFAULT " + userSerialNumber +
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 ");");
Adam Cohendcd297f2013-06-18 13:13:40 -0700455 addWorkspacesTable(db);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700457 // Database was just created, so wipe any previous widgets
458 if (mAppWidgetHost != null) {
459 mAppWidgetHost.deleteHost();
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700460 sendAppWidgetResetNotify();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700462
Adam Cohen6dbe0492013-12-02 17:00:14 -0800463 if (shouldImportLauncher2Database(mContext)) {
Dan Sandlerf0b8dac2013-11-19 12:21:25 -0500464 // Try converting the old database
465 ContentValuesCallback permuteScreensCb = new ContentValuesCallback() {
466 public void onRow(ContentValues values) {
467 int container = values.getAsInteger(LauncherSettings.Favorites.CONTAINER);
468 if (container == Favorites.CONTAINER_DESKTOP) {
469 int screen = values.getAsInteger(LauncherSettings.Favorites.SCREEN);
470 screen = (int) upgradeLauncherDb_permuteScreens(screen);
471 values.put(LauncherSettings.Favorites.SCREEN, screen);
472 }
473 }
474 };
475 Uri uri = Uri.parse("content://" + Settings.AUTHORITY +
476 "/old_favorites?notify=true");
477 if (!convertDatabase(db, uri, permuteScreensCb, true)) {
478 // Try and upgrade from the Launcher2 db
Jason Monk0bfcceb2014-03-21 15:42:06 -0400479 uri = Uri.parse(mContext.getString(R.string.old_launcher_provider_uri));
Dan Sandlerf0b8dac2013-11-19 12:21:25 -0500480 if (!convertDatabase(db, uri, permuteScreensCb, false)) {
481 // If we fail, then set a flag to load the default workspace
482 setFlagEmptyDbCreated();
483 return;
Winson Chungc763c4e2013-07-19 13:49:06 -0700484 }
485 }
Dan Sandlerf0b8dac2013-11-19 12:21:25 -0500486 // Right now, in non-default workspace cases, we want to run the final
487 // upgrade code (ie. to fix workspace screen indices -> ids, etc.), so
488 // set that flag too.
489 setFlagJustLoadedOldDb();
490 } else {
491 // Fresh and clean launcher DB.
492 mMaxItemId = initializeMaxItemId(db);
493 setFlagEmptyDbCreated();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800494 }
495 }
496
Adam Cohendcd297f2013-06-18 13:13:40 -0700497 private void addWorkspacesTable(SQLiteDatabase db) {
498 db.execSQL("CREATE TABLE " + TABLE_WORKSPACE_SCREENS + " (" +
499 LauncherSettings.WorkspaceScreens._ID + " INTEGER," +
Chris Wren1ada10d2013-09-13 18:01:38 -0400500 LauncherSettings.WorkspaceScreens.SCREEN_RANK + " INTEGER," +
501 LauncherSettings.ChangeLogColumns.MODIFIED + " INTEGER NOT NULL DEFAULT 0" +
Adam Cohendcd297f2013-06-18 13:13:40 -0700502 ");");
503 }
504
Adam Cohen119285e2014-04-02 16:59:08 -0700505 private void removeOrphanedItems(SQLiteDatabase db) {
Adam Cohenf9c14de2014-04-17 18:20:45 -0700506 // Delete items directly on the workspace who's screen id doesn't exist
507 // "DELETE FROM favorites WHERE screen NOT IN (SELECT _id FROM workspaceScreens)
508 // AND container = -100"
509 String removeOrphanedDesktopItems = "DELETE FROM " + TABLE_FAVORITES +
510 " WHERE " +
Adam Cohen119285e2014-04-02 16:59:08 -0700511 LauncherSettings.Favorites.SCREEN + " NOT IN (SELECT " +
Adam Cohenf9c14de2014-04-17 18:20:45 -0700512 LauncherSettings.WorkspaceScreens._ID + " FROM " + TABLE_WORKSPACE_SCREENS + ")" +
513 " AND " +
514 LauncherSettings.Favorites.CONTAINER + " = " +
515 LauncherSettings.Favorites.CONTAINER_DESKTOP;
516 db.execSQL(removeOrphanedDesktopItems);
517
518 // Delete items contained in folders which no longer exist (after above statement)
519 // "DELETE FROM favorites WHERE container <> -100 AND container <> -101 AND container
520 // NOT IN (SELECT _id FROM favorites WHERE itemType = 2)"
521 String removeOrphanedFolderItems = "DELETE FROM " + TABLE_FAVORITES +
522 " WHERE " +
523 LauncherSettings.Favorites.CONTAINER + " <> " +
524 LauncherSettings.Favorites.CONTAINER_DESKTOP +
525 " AND "
526 + LauncherSettings.Favorites.CONTAINER + " <> " +
527 LauncherSettings.Favorites.CONTAINER_HOTSEAT +
528 " AND "
529 + LauncherSettings.Favorites.CONTAINER + " NOT IN (SELECT " +
530 LauncherSettings.Favorites._ID + " FROM " + TABLE_FAVORITES +
531 " WHERE " + LauncherSettings.Favorites.ITEM_TYPE + " = " +
532 LauncherSettings.Favorites.ITEM_TYPE_FOLDER + ")";
533 db.execSQL(removeOrphanedFolderItems);
Adam Cohen119285e2014-04-02 16:59:08 -0700534 }
535
Winson Chungc763c4e2013-07-19 13:49:06 -0700536 private void setFlagJustLoadedOldDb() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400537 String spKey = LauncherAppState.getSharedPreferencesKey();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700538 SharedPreferences sp = mContext.getSharedPreferences(spKey, Context.MODE_PRIVATE);
539 SharedPreferences.Editor editor = sp.edit();
Winson Chungc763c4e2013-07-19 13:49:06 -0700540 editor.putBoolean(UPGRADED_FROM_OLD_DATABASE, true);
541 editor.putBoolean(EMPTY_DATABASE_CREATED, false);
Michael Jurkab85f8a42012-04-25 15:48:32 -0700542 editor.commit();
543 }
544
Winson Chungc763c4e2013-07-19 13:49:06 -0700545 private void setFlagEmptyDbCreated() {
546 String spKey = LauncherAppState.getSharedPreferencesKey();
547 SharedPreferences sp = mContext.getSharedPreferences(spKey, Context.MODE_PRIVATE);
548 SharedPreferences.Editor editor = sp.edit();
549 editor.putBoolean(EMPTY_DATABASE_CREATED, true);
550 editor.putBoolean(UPGRADED_FROM_OLD_DATABASE, false);
551 editor.commit();
552 }
553
554 // We rearrange the screens from the old launcher
555 // 12345 -> 34512
556 private long upgradeLauncherDb_permuteScreens(long screen) {
557 if (screen >= 2) {
558 return screen - 2;
559 } else {
560 return screen + 3;
561 }
562 }
563
564 private boolean convertDatabase(SQLiteDatabase db, Uri uri,
565 ContentValuesCallback cb, boolean deleteRows) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800566 if (LOGD) Log.d(TAG, "converting database from an older format, but not onUpgrade");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800567 boolean converted = false;
568
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800569 final ContentResolver resolver = mContext.getContentResolver();
570 Cursor cursor = null;
571
572 try {
573 cursor = resolver.query(uri, null, null, null, null);
574 } catch (Exception e) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700575 // Ignore
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800576 }
577
578 // We already have a favorites database in the old provider
Winson Chungc763c4e2013-07-19 13:49:06 -0700579 if (cursor != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800580 try {
Winson Chungc763c4e2013-07-19 13:49:06 -0700581 if (cursor.getCount() > 0) {
582 converted = copyFromCursor(db, cursor, cb) > 0;
583 if (converted && deleteRows) {
584 resolver.delete(uri, null, null);
585 }
586 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800587 } finally {
588 cursor.close();
589 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800590 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700591
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800592 if (converted) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700593 // Convert widgets from this import into widgets
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800594 if (LOGD) Log.d(TAG, "converted and now triggering widget upgrade");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800595 convertWidgets(db);
Winson Chungc763c4e2013-07-19 13:49:06 -0700596
597 // Update max item id
598 mMaxItemId = initializeMaxItemId(db);
599 if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800600 }
601
602 return converted;
603 }
604
Winson Chungc763c4e2013-07-19 13:49:06 -0700605 private int copyFromCursor(SQLiteDatabase db, Cursor c, ContentValuesCallback cb) {
Romain Guy73b979d2009-06-09 12:57:21 -0700606 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800607 final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
608 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
609 final int iconTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
610 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
611 final int iconPackageIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
612 final int iconResourceIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
613 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
614 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
615 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
616 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
617 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
618 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
619 final int displayModeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
620
621 ContentValues[] rows = new ContentValues[c.getCount()];
622 int i = 0;
623 while (c.moveToNext()) {
624 ContentValues values = new ContentValues(c.getColumnCount());
Romain Guy73b979d2009-06-09 12:57:21 -0700625 values.put(LauncherSettings.Favorites._ID, c.getLong(idIndex));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 values.put(LauncherSettings.Favorites.INTENT, c.getString(intentIndex));
627 values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex));
628 values.put(LauncherSettings.Favorites.ICON_TYPE, c.getInt(iconTypeIndex));
629 values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex));
630 values.put(LauncherSettings.Favorites.ICON_PACKAGE, c.getString(iconPackageIndex));
631 values.put(LauncherSettings.Favorites.ICON_RESOURCE, c.getString(iconResourceIndex));
632 values.put(LauncherSettings.Favorites.CONTAINER, c.getInt(containerIndex));
633 values.put(LauncherSettings.Favorites.ITEM_TYPE, c.getInt(itemTypeIndex));
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700634 values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800635 values.put(LauncherSettings.Favorites.SCREEN, c.getInt(screenIndex));
636 values.put(LauncherSettings.Favorites.CELLX, c.getInt(cellXIndex));
637 values.put(LauncherSettings.Favorites.CELLY, c.getInt(cellYIndex));
638 values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex));
639 values.put(LauncherSettings.Favorites.DISPLAY_MODE, c.getInt(displayModeIndex));
Winson Chungc763c4e2013-07-19 13:49:06 -0700640 if (cb != null) {
641 cb.onRow(values);
642 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800643 rows[i++] = values;
644 }
645
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646 int total = 0;
Winson Chungc763c4e2013-07-19 13:49:06 -0700647 if (i > 0) {
648 db.beginTransaction();
649 try {
650 int numValues = rows.length;
651 for (i = 0; i < numValues; i++) {
652 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, rows[i]) < 0) {
653 return 0;
654 } else {
655 total++;
656 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 }
Winson Chungc763c4e2013-07-19 13:49:06 -0700658 db.setTransactionSuccessful();
659 } finally {
660 db.endTransaction();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 }
663
664 return total;
665 }
666
667 @Override
668 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Winson Chungc763c4e2013-07-19 13:49:06 -0700669 if (LOGD) Log.d(TAG, "onUpgrade triggered: " + oldVersion);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700670
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 int version = oldVersion;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700672 if (version < 3) {
673 // upgrade 1,2 -> 3 added appWidgetId column
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800674 db.beginTransaction();
675 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700676 // Insert new column for holding appWidgetIds
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 db.execSQL("ALTER TABLE favorites " +
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700678 "ADD COLUMN appWidgetId INTEGER NOT NULL DEFAULT -1;");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 db.setTransactionSuccessful();
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700680 version = 3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 } catch (SQLException ex) {
682 // Old version remains, which means we wipe old data
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800683 Log.e(TAG, ex.getMessage(), ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 } finally {
685 db.endTransaction();
686 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700687
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800688 // Convert existing widgets only if table upgrade was successful
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700689 if (version == 3) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 convertWidgets(db);
691 }
692 }
Romain Guy73b979d2009-06-09 12:57:21 -0700693
694 if (version < 4) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800695 version = 4;
Romain Guy73b979d2009-06-09 12:57:21 -0700696 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700697
Romain Guy509cd6a2010-03-23 15:10:56 -0700698 // Where's version 5?
699 // - Donut and sholes on 2.0 shipped with version 4 of launcher1.
Daniel Sandler325dc232013-06-05 22:57:57 -0400700 // - Passion shipped on 2.1 with version 6 of launcher3
Romain Guy509cd6a2010-03-23 15:10:56 -0700701 // - Sholes shipped on 2.1r1 (aka Mr. 3) with version 5 of launcher 1
702 // but version 5 on there was the updateContactsShortcuts change
703 // which was version 6 in launcher 2 (first shipped on passion 2.1r1).
704 // The updateContactsShortcuts change is idempotent, so running it twice
705 // is okay so we'll do that when upgrading the devices that shipped with it.
706 if (version < 6) {
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800707 // We went from 3 to 5 screens. Move everything 1 to the right
708 db.beginTransaction();
709 try {
710 db.execSQL("UPDATE favorites SET screen=(screen + 1);");
711 db.setTransactionSuccessful();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800712 } catch (SQLException ex) {
713 // Old version remains, which means we wipe old data
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800714 Log.e(TAG, ex.getMessage(), ex);
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800715 } finally {
716 db.endTransaction();
717 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700718
Romain Guy509cd6a2010-03-23 15:10:56 -0700719 // We added the fast track.
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800720 if (updateContactsShortcuts(db)) {
721 version = 6;
722 }
723 }
Bjorn Bringert7984c942009-12-09 15:38:25 +0000724
725 if (version < 7) {
726 // Version 7 gets rid of the special search widget.
727 convertWidgets(db);
728 version = 7;
729 }
730
Joe Onorato0589f0f2010-02-08 13:44:00 -0800731 if (version < 8) {
732 // Version 8 (froyo) has the icons all normalized. This should
733 // already be the case in practice, but we now rely on it and don't
734 // resample the images each time.
735 normalizeIcons(db);
736 version = 8;
737 }
738
Winson Chung3d503fb2011-07-13 17:25:49 -0700739 if (version < 9) {
740 // The max id is not yet set at this point (onUpgrade is triggered in the ctor
741 // before it gets a change to get set, so we need to read it here when we use it)
Adam Cohendcd297f2013-06-18 13:13:40 -0700742 if (mMaxItemId == -1) {
743 mMaxItemId = initializeMaxItemId(db);
Winson Chung3d503fb2011-07-13 17:25:49 -0700744 }
745
746 // Add default hotseat icons
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700747 loadFavorites(db, new DefaultLayoutParser(mContext, mAppWidgetHost, this,
748 mContext.getResources(), R.xml.update_workspace));
Winson Chung3d503fb2011-07-13 17:25:49 -0700749 version = 9;
750 }
751
Daniel Lehmannd02402c2012-05-14 18:30:53 -0700752 // We bumped the version three time during JB, once to update the launch flags, once to
753 // update the override for the default launch animation and once to set the mimetype
754 // to improve startup performance
755 if (version < 12) {
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700756 // Contact shortcuts need a different set of flags to be launched now
757 // The updateContactsShortcuts change is idempotent, so we can keep using it like
758 // back in the Donut days
759 updateContactsShortcuts(db);
Daniel Lehmannd02402c2012-05-14 18:30:53 -0700760 version = 12;
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700761 }
762
Adam Cohendcd297f2013-06-18 13:13:40 -0700763 if (version < 13) {
764 // With the new shrink-wrapped and re-orderable workspaces, it makes sense
765 // to persist workspace screens and their relative order.
766 mMaxScreenId = 0;
767
768 // This will never happen in the wild, but when we switch to using workspace
769 // screen ids, redo the import from old launcher.
Winson Chungc763c4e2013-07-19 13:49:06 -0700770 sJustLoadedFromOldDb = true;
Adam Cohendcd297f2013-06-18 13:13:40 -0700771
772 addWorkspacesTable(db);
773 version = 13;
774 }
775
Chris Wrend5e66bf2013-09-16 14:02:29 -0400776 if (version < 14) {
777 db.beginTransaction();
778 try {
779 // Insert new column for holding widget provider name
780 db.execSQL("ALTER TABLE favorites " +
781 "ADD COLUMN appWidgetProvider TEXT;");
782 db.setTransactionSuccessful();
783 version = 14;
784 } catch (SQLException ex) {
785 // Old version remains, which means we wipe old data
786 Log.e(TAG, ex.getMessage(), ex);
787 } finally {
788 db.endTransaction();
789 }
790 }
791
Chris Wren1ada10d2013-09-13 18:01:38 -0400792 if (version < 15) {
793 db.beginTransaction();
794 try {
795 // Insert new column for holding update timestamp
796 db.execSQL("ALTER TABLE favorites " +
797 "ADD COLUMN modified INTEGER NOT NULL DEFAULT 0;");
798 db.execSQL("ALTER TABLE workspaceScreens " +
799 "ADD COLUMN modified INTEGER NOT NULL DEFAULT 0;");
800 db.setTransactionSuccessful();
801 version = 15;
802 } catch (SQLException ex) {
803 // Old version remains, which means we wipe old data
804 Log.e(TAG, ex.getMessage(), ex);
805 } finally {
806 db.endTransaction();
807 }
808 }
809
Chris Wrenf4d08112014-01-16 18:13:56 -0500810
811 if (version < 16) {
812 db.beginTransaction();
813 try {
814 // Insert new column for holding restore status
815 db.execSQL("ALTER TABLE favorites " +
816 "ADD COLUMN restored INTEGER NOT NULL DEFAULT 0;");
817 db.setTransactionSuccessful();
818 version = 16;
819 } catch (SQLException ex) {
820 // Old version remains, which means we wipe old data
821 Log.e(TAG, ex.getMessage(), ex);
822 } finally {
823 db.endTransaction();
824 }
825 }
826
Adam Cohen71e03b92014-02-21 14:09:53 -0800827 if (version < 17) {
828 // We use the db version upgrade here to identify users who may not have seen
829 // clings yet (because they weren't available), but for whom the clings are now
830 // available (tablet users). Because one of the possible cling flows (migration)
831 // is very destructive (wipes out workspaces), we want to prevent this from showing
832 // until clear data. We do so by marking that the clings have been shown.
833 LauncherClings.synchonouslyMarkFirstRunClingDismissed(mContext);
834 version = 17;
835 }
836
Adam Cohen119285e2014-04-02 16:59:08 -0700837 if (version < 18) {
Adam Cohenf9c14de2014-04-17 18:20:45 -0700838 // No-op
839 version = 18;
840 }
841
842 if (version < 19) {
Adam Cohen119285e2014-04-02 16:59:08 -0700843 // Due to a data loss bug, some users may have items associated with screen ids
844 // which no longer exist. Since this can cause other problems, and since the user
845 // will never see these items anyway, we use database upgrade as an opportunity to
846 // clean things up.
Adam Cohenf9c14de2014-04-17 18:20:45 -0700847 removeOrphanedItems(db);
848 version = 19;
Adam Cohen119285e2014-04-02 16:59:08 -0700849 }
850
Kenny Guyed131872014-04-30 03:02:21 +0100851 if (version < 20) {
852 // Add userId column
853 if (addProfileColumn(db)) {
854 version = 20;
855 }
856 // else old version remains, which means we wipe old data
857 }
858
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800859 if (version != DATABASE_VERSION) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800860 Log.w(TAG, "Destroying all old data.");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861 db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES);
Adam Cohendcd297f2013-06-18 13:13:40 -0700862 db.execSQL("DROP TABLE IF EXISTS " + TABLE_WORKSPACE_SCREENS);
863
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 onCreate(db);
865 }
866 }
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800867
Adam Cohen9b1d0622014-05-21 19:01:57 -0700868 @Override
869 public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
870 // This shouldn't happen -- throw our hands up in the air and start over.
871 Log.w(TAG, "Database version downgrade from: " + oldVersion + " to " + newVersion +
872 ". Wiping databse.");
Sunny Goyal42de82f2014-09-26 22:09:29 -0700873 createEmptyDB(db);
874 }
Adam Cohen9b1d0622014-05-21 19:01:57 -0700875
Sunny Goyal42de82f2014-09-26 22:09:29 -0700876
877 /**
878 * Clears all the data for a fresh start.
879 */
880 public void createEmptyDB(SQLiteDatabase db) {
Adam Cohen9b1d0622014-05-21 19:01:57 -0700881 db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES);
882 db.execSQL("DROP TABLE IF EXISTS " + TABLE_WORKSPACE_SCREENS);
883 onCreate(db);
884 }
885
Kenny Guyed131872014-04-30 03:02:21 +0100886 private boolean addProfileColumn(SQLiteDatabase db) {
887 db.beginTransaction();
888 try {
889 UserManagerCompat userManager = UserManagerCompat.getInstance(mContext);
890 // Default to the serial number of this user, for older
891 // shortcuts.
892 long userSerialNumber = userManager.getSerialNumberForUser(
893 UserHandleCompat.myUserHandle());
894 // Insert new column for holding user serial number
895 db.execSQL("ALTER TABLE favorites " +
896 "ADD COLUMN profileId INTEGER DEFAULT "
897 + userSerialNumber + ";");
898 db.setTransactionSuccessful();
899 } catch (SQLException ex) {
900 // Old version remains, which means we wipe old data
901 Log.e(TAG, ex.getMessage(), ex);
902 return false;
903 } finally {
904 db.endTransaction();
905 }
906 return true;
907 }
908
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800909 private boolean updateContactsShortcuts(SQLiteDatabase db) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800910 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE,
911 new int[] { Favorites.ITEM_TYPE_SHORTCUT });
912
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700913 Cursor c = null;
914 final String actionQuickContact = "com.android.contacts.action.QUICK_CONTACT";
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800915 db.beginTransaction();
916 try {
917 // Select and iterate through each matching widget
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700918 c = db.query(TABLE_FAVORITES,
919 new String[] { Favorites._ID, Favorites.INTENT },
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800920 selectWhere, null, null, null, null);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700921 if (c == null) return false;
922
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800923 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700924
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800925 final int idIndex = c.getColumnIndex(Favorites._ID);
926 final int intentIndex = c.getColumnIndex(Favorites.INTENT);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700927
928 while (c.moveToNext()) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800929 long favoriteId = c.getLong(idIndex);
930 final String intentUri = c.getString(intentIndex);
931 if (intentUri != null) {
932 try {
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700933 final Intent intent = Intent.parseUri(intentUri, 0);
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800934 android.util.Log.d("Home", intent.toString());
935 final Uri uri = intent.getData();
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700936 if (uri != null) {
937 final String data = uri.toString();
938 if ((Intent.ACTION_VIEW.equals(intent.getAction()) ||
939 actionQuickContact.equals(intent.getAction())) &&
940 (data.startsWith("content://contacts/people/") ||
941 data.startsWith("content://com.android.contacts/" +
942 "contacts/lookup/"))) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800943
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700944 final Intent newIntent = new Intent(actionQuickContact);
945 // When starting from the launcher, start in a new, cleared task
946 // CLEAR_WHEN_TASK_RESET cannot reset the root of a task, so we
947 // clear the whole thing preemptively here since
948 // QuickContactActivity will finish itself when launching other
949 // detail activities.
950 newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
951 Intent.FLAG_ACTIVITY_CLEAR_TASK);
Winson Chung2672ff92012-05-04 16:22:30 -0700952 newIntent.putExtra(
953 Launcher.INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION, true);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700954 newIntent.setData(uri);
Daniel Lehmannd02402c2012-05-14 18:30:53 -0700955 // Determine the type and also put that in the shortcut
956 // (that can speed up launch a bit)
957 newIntent.setDataAndType(uri, newIntent.resolveType(mContext));
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800958
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700959 final ContentValues values = new ContentValues();
960 values.put(LauncherSettings.Favorites.INTENT,
961 newIntent.toUri(0));
962
963 String updateWhere = Favorites._ID + "=" + favoriteId;
964 db.update(TABLE_FAVORITES, values, updateWhere, null);
965 }
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800966 }
967 } catch (RuntimeException ex) {
968 Log.e(TAG, "Problem upgrading shortcut", ex);
969 } catch (URISyntaxException e) {
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700970 Log.e(TAG, "Problem upgrading shortcut", e);
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800971 }
972 }
973 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700974
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800975 db.setTransactionSuccessful();
976 } catch (SQLException ex) {
977 Log.w(TAG, "Problem while upgrading contacts", ex);
978 return false;
979 } finally {
980 db.endTransaction();
981 if (c != null) {
982 c.close();
983 }
984 }
985
986 return true;
987 }
988
Joe Onorato0589f0f2010-02-08 13:44:00 -0800989 private void normalizeIcons(SQLiteDatabase db) {
990 Log.d(TAG, "normalizing icons");
991
Joe Onorato346e1292010-02-18 10:34:24 -0500992 db.beginTransaction();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800993 Cursor c = null;
Joe Onorato9690b392010-03-23 17:34:37 -0400994 SQLiteStatement update = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800995 try {
996 boolean logged = false;
Joe Onorato9690b392010-03-23 17:34:37 -0400997 update = db.compileStatement("UPDATE favorites "
Jeff Hamiltoneaf77d62010-02-13 00:08:17 -0600998 + "SET icon=? WHERE _id=?");
Joe Onorato0589f0f2010-02-08 13:44:00 -0800999
1000 c = db.rawQuery("SELECT _id, icon FROM favorites WHERE iconType=" +
1001 Favorites.ICON_TYPE_BITMAP, null);
1002
1003 final int idIndex = c.getColumnIndexOrThrow(Favorites._ID);
1004 final int iconIndex = c.getColumnIndexOrThrow(Favorites.ICON);
1005
1006 while (c.moveToNext()) {
1007 long id = c.getLong(idIndex);
1008 byte[] data = c.getBlob(iconIndex);
1009 try {
Sunny Goyal2fce90c2014-10-07 12:01:58 -07001010 Bitmap bitmap = Utilities.createIconBitmap(
Joe Onorato0589f0f2010-02-08 13:44:00 -08001011 BitmapFactory.decodeByteArray(data, 0, data.length),
1012 mContext);
1013 if (bitmap != null) {
1014 update.bindLong(1, id);
1015 data = ItemInfo.flattenBitmap(bitmap);
1016 if (data != null) {
1017 update.bindBlob(2, data);
1018 update.execute();
1019 }
1020 bitmap.recycle();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001021 }
1022 } catch (Exception e) {
1023 if (!logged) {
1024 Log.e(TAG, "Failed normalizing icon " + id, e);
1025 } else {
1026 Log.e(TAG, "Also failed normalizing icon " + id);
1027 }
1028 logged = true;
1029 }
1030 }
Bjorn Bringert3a928e42010-02-19 11:15:40 +00001031 db.setTransactionSuccessful();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001032 } catch (SQLException ex) {
1033 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
1034 } finally {
1035 db.endTransaction();
Joe Onorato9690b392010-03-23 17:34:37 -04001036 if (update != null) {
1037 update.close();
1038 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001039 if (c != null) {
1040 c.close();
1041 }
1042 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001043 }
1044
1045 // Generates a new ID to use for an object in your database. This method should be only
1046 // called from the main UI thread. As an exception, we do call it when we call the
1047 // constructor from the worker thread; however, this doesn't extend until after the
1048 // constructor is called, and we only pass a reference to LauncherProvider to LauncherApp
1049 // after that point
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001050 @Override
Adam Cohendcd297f2013-06-18 13:13:40 -07001051 public long generateNewItemId() {
1052 if (mMaxItemId < 0) {
1053 throw new RuntimeException("Error: max item id was not initialized");
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001054 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001055 mMaxItemId += 1;
1056 return mMaxItemId;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001057 }
1058
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001059 @Override
1060 public long insertAndCheck(SQLiteDatabase db, ContentValues values) {
1061 return dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values);
1062 }
1063
Winson Chungc763c4e2013-07-19 13:49:06 -07001064 public void updateMaxItemId(long id) {
1065 mMaxItemId = id + 1;
1066 }
1067
Chris Wren5dee7af2013-12-20 17:22:11 -05001068 public void checkId(String table, ContentValues values) {
1069 long id = values.getAsLong(LauncherSettings.BaseLauncherColumns._ID);
1070 if (table == LauncherProvider.TABLE_WORKSPACE_SCREENS) {
1071 mMaxScreenId = Math.max(id, mMaxScreenId);
1072 } else {
1073 mMaxItemId = Math.max(id, mMaxItemId);
1074 }
1075 }
1076
Adam Cohendcd297f2013-06-18 13:13:40 -07001077 private long initializeMaxItemId(SQLiteDatabase db) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001078 Cursor c = db.rawQuery("SELECT MAX(_id) FROM favorites", null);
1079
1080 // get the result
1081 final int maxIdIndex = 0;
1082 long id = -1;
1083 if (c != null && c.moveToNext()) {
1084 id = c.getLong(maxIdIndex);
1085 }
Michael Jurka5130e402011-10-13 04:55:35 -07001086 if (c != null) {
1087 c.close();
1088 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001089
1090 if (id == -1) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001091 throw new RuntimeException("Error: could not query max item id");
1092 }
1093
1094 return id;
1095 }
1096
1097 // Generates a new ID to use for an workspace screen in your database. This method
1098 // should be only called from the main UI thread. As an exception, we do call it when we
1099 // call the constructor from the worker thread; however, this doesn't extend until after the
1100 // constructor is called, and we only pass a reference to LauncherProvider to LauncherApp
1101 // after that point
1102 public long generateNewScreenId() {
1103 if (mMaxScreenId < 0) {
1104 throw new RuntimeException("Error: max screen id was not initialized");
1105 }
1106 mMaxScreenId += 1;
Winson Chunga90303b2013-11-15 13:05:06 -08001107 // Log to disk
1108 Launcher.addDumpLog(TAG, "11683562 - generateNewScreenId(): " + mMaxScreenId, true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001109 return mMaxScreenId;
1110 }
1111
1112 public void updateMaxScreenId(long maxScreenId) {
Winson Chunga90303b2013-11-15 13:05:06 -08001113 // Log to disk
1114 Launcher.addDumpLog(TAG, "11683562 - updateMaxScreenId(): " + maxScreenId, true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001115 mMaxScreenId = maxScreenId;
1116 }
1117
1118 private long initializeMaxScreenId(SQLiteDatabase db) {
1119 Cursor c = db.rawQuery("SELECT MAX(" + LauncherSettings.WorkspaceScreens._ID + ") FROM " + TABLE_WORKSPACE_SCREENS, null);
1120
1121 // get the result
1122 final int maxIdIndex = 0;
1123 long id = -1;
1124 if (c != null && c.moveToNext()) {
1125 id = c.getLong(maxIdIndex);
1126 }
1127 if (c != null) {
1128 c.close();
1129 }
1130
1131 if (id == -1) {
1132 throw new RuntimeException("Error: could not query max screen id");
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001133 }
1134
Winson Chunga90303b2013-11-15 13:05:06 -08001135 // Log to disk
1136 Launcher.addDumpLog(TAG, "11683562 - initializeMaxScreenId(): " + id, true);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001137 return id;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001138 }
1139
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001141 * Upgrade existing clock and photo frame widgets into their new widget
Bjorn Bringert93c45762009-12-16 13:19:47 +00001142 * equivalents.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143 */
1144 private void convertWidgets(SQLiteDatabase db) {
Bjorn Bringert34251342009-12-15 13:33:11 +00001145 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001146 final int[] bindSources = new int[] {
1147 Favorites.ITEM_TYPE_WIDGET_CLOCK,
1148 Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME,
Bjorn Bringert7984c942009-12-09 15:38:25 +00001149 Favorites.ITEM_TYPE_WIDGET_SEARCH,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001150 };
Bjorn Bringert7984c942009-12-09 15:38:25 +00001151
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE, bindSources);
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001153
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001154 Cursor c = null;
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001155
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001156 db.beginTransaction();
1157 try {
1158 // Select and iterate through each matching widget
Bjorn Bringert7984c942009-12-09 15:38:25 +00001159 c = db.query(TABLE_FAVORITES, new String[] { Favorites._ID, Favorites.ITEM_TYPE },
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160 selectWhere, null, null, null, null);
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001161
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001162 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001163
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001164 final ContentValues values = new ContentValues();
1165 while (c != null && c.moveToNext()) {
1166 long favoriteId = c.getLong(0);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001167 int favoriteType = c.getInt(1);
1168
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001169 // Allocate and update database with new appWidgetId
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001171 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001172
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001173 if (LOGD) {
1174 Log.d(TAG, "allocated appWidgetId=" + appWidgetId
1175 + " for favoriteId=" + favoriteId);
1176 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177 values.clear();
Bjorn Bringert7984c942009-12-09 15:38:25 +00001178 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
1179 values.put(Favorites.APPWIDGET_ID, appWidgetId);
1180
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181 // Original widgets might not have valid spans when upgrading
Bjorn Bringert7984c942009-12-09 15:38:25 +00001182 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
1183 values.put(LauncherSettings.Favorites.SPANX, 4);
1184 values.put(LauncherSettings.Favorites.SPANY, 1);
1185 } else {
1186 values.put(LauncherSettings.Favorites.SPANX, 2);
1187 values.put(LauncherSettings.Favorites.SPANY, 2);
1188 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189
1190 String updateWhere = Favorites._ID + "=" + favoriteId;
1191 db.update(TABLE_FAVORITES, values, updateWhere, null);
Bjorn Bringert34251342009-12-15 13:33:11 +00001192
Bjorn Bringert34251342009-12-15 13:33:11 +00001193 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_CLOCK) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001194 // TODO: check return value
1195 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
Bjorn Bringert34251342009-12-15 13:33:11 +00001196 new ComponentName("com.android.alarmclock",
1197 "com.android.alarmclock.AnalogAppWidgetProvider"));
1198 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001199 // TODO: check return value
1200 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
Bjorn Bringert34251342009-12-15 13:33:11 +00001201 new ComponentName("com.android.camera",
1202 "com.android.camera.PhotoAppWidgetProvider"));
1203 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001204 // TODO: check return value
1205 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
Bjorn Bringertcd8fec02010-01-14 13:26:43 +00001206 getSearchWidgetProvider());
Bjorn Bringert34251342009-12-15 13:33:11 +00001207 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001208 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001209 Log.e(TAG, "Problem allocating appWidgetId", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001210 }
1211 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001212
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001213 db.setTransactionSuccessful();
1214 } catch (SQLException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001215 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001216 } finally {
1217 db.endTransaction();
1218 if (c != null) {
1219 c.close();
1220 }
1221 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001222
1223 // Update max item id
1224 mMaxItemId = initializeMaxItemId(db);
1225 if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001226 }
1227
Adam Cohena043fa82014-07-23 14:49:38 -07001228 private boolean initializeExternalAdd(ContentValues values) {
1229 // 1. Ensure that externally added items have a valid item id
1230 long id = generateNewItemId();
1231 values.put(LauncherSettings.Favorites._ID, id);
1232
1233 // 2. In the case of an app widget, and if no app widget id is specified, we
1234 // attempt allocate and bind the widget.
1235 Integer itemType = values.getAsInteger(LauncherSettings.Favorites.ITEM_TYPE);
1236 if (itemType != null &&
1237 itemType.intValue() == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET &&
1238 !values.containsKey(LauncherSettings.Favorites.APPWIDGET_ID)) {
1239
1240 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
1241 ComponentName cn = ComponentName.unflattenFromString(
1242 values.getAsString(Favorites.APPWIDGET_PROVIDER));
1243
1244 if (cn != null) {
1245 try {
1246 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
Adam Cohen3ed316a2014-07-23 18:21:20 -07001247 values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId);
Adam Coheneb1ac422014-10-14 08:55:28 -07001248 if (!appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,cn)) {
Adam Cohend61a6382014-10-14 15:04:34 -07001249 return false;
Adam Cohena043fa82014-07-23 14:49:38 -07001250 }
1251 } catch (RuntimeException e) {
1252 Log.e(TAG, "Failed to initialize external widget", e);
Adam Coheneb1ac422014-10-14 08:55:28 -07001253 return false;
Adam Cohena043fa82014-07-23 14:49:38 -07001254 }
Adam Coheneb1ac422014-10-14 08:55:28 -07001255 } else {
1256 return false;
Adam Cohena043fa82014-07-23 14:49:38 -07001257 }
1258 }
Adam Cohen7ec3bbf2014-07-31 00:09:45 -07001259
1260 // Add screen id if not present
1261 long screenId = values.getAsLong(LauncherSettings.Favorites.SCREEN);
1262 if (!addScreenIdIfNecessary(screenId)) {
1263 return false;
1264 }
Adam Cohena043fa82014-07-23 14:49:38 -07001265 return true;
1266 }
1267
Adam Cohen7ec3bbf2014-07-31 00:09:45 -07001268 // Returns true of screen id exists, or if successfully added
1269 private boolean addScreenIdIfNecessary(long screenId) {
1270 if (!hasScreenId(screenId)) {
1271 int rank = getMaxScreenRank() + 1;
1272
1273 ContentValues v = new ContentValues();
1274 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1275 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, rank);
1276 if (dbInsertAndCheck(this, getWritableDatabase(),
1277 TABLE_WORKSPACE_SCREENS, null, v) < 0) {
1278 return false;
1279 }
1280 }
1281 return true;
1282 }
1283
1284 private boolean hasScreenId(long screenId) {
1285 SQLiteDatabase db = getWritableDatabase();
1286 Cursor c = db.rawQuery("SELECT * FROM " + TABLE_WORKSPACE_SCREENS + " WHERE "
1287 + LauncherSettings.WorkspaceScreens._ID + " = " + screenId, null);
1288 if (c != null) {
1289 int count = c.getCount();
1290 c.close();
1291 return count > 0;
1292 } else {
1293 return false;
1294 }
1295 }
1296
1297 private int getMaxScreenRank() {
1298 SQLiteDatabase db = getWritableDatabase();
1299 Cursor c = db.rawQuery("SELECT MAX(" + LauncherSettings.WorkspaceScreens.SCREEN_RANK
1300 + ") FROM " + TABLE_WORKSPACE_SCREENS, null);
1301
1302 // get the result
1303 final int maxRankIndex = 0;
1304 int rank = -1;
1305 if (c != null && c.moveToNext()) {
1306 rank = c.getInt(maxRankIndex);
1307 }
1308 if (c != null) {
1309 c.close();
1310 }
1311
1312 return rank;
1313 }
1314
Sunny Goyal3a5a9d12014-10-01 15:33:41 -07001315 private int loadFavorites(SQLiteDatabase db, AutoInstallsLayout loader) {
Adam Cohen71483f42014-05-15 14:04:01 -07001316 ArrayList<Long> screenIds = new ArrayList<Long>();
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001317 // TODO: Use multiple loaders with fall-back and transaction.
1318 int count = loader.loadLayout(db, screenIds);
Adam Cohen71483f42014-05-15 14:04:01 -07001319
1320 // Add the screens specified by the items above
1321 Collections.sort(screenIds);
1322 int rank = 0;
1323 ContentValues values = new ContentValues();
1324 for (Long id : screenIds) {
1325 values.clear();
1326 values.put(LauncherSettings.WorkspaceScreens._ID, id);
1327 values.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, rank);
1328 if (dbInsertAndCheck(this, db, TABLE_WORKSPACE_SCREENS, null, values) < 0) {
1329 throw new RuntimeException("Failed initialize screen table"
1330 + "from default layout");
1331 }
1332 rank++;
1333 }
1334
1335 // Ensure that the max ids are initialized
1336 mMaxItemId = initializeMaxItemId(db);
1337 mMaxScreenId = initializeMaxScreenId(db);
Adam Cohen7ec3bbf2014-07-31 00:09:45 -07001338
Adam Cohen71483f42014-05-15 14:04:01 -07001339 return count;
1340 }
1341
Bjorn Bringertcd8fec02010-01-14 13:26:43 +00001342 private ComponentName getSearchWidgetProvider() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08001343 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(mContext);
1344 return (searchProvider == null) ? null : searchProvider.provider;
Bjorn Bringertcd8fec02010-01-14 13:26:43 +00001345 }
1346
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001347 private void migrateLauncher2Shortcuts(SQLiteDatabase db, Uri uri) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001348 final ContentResolver resolver = mContext.getContentResolver();
1349 Cursor c = null;
1350 int count = 0;
1351 int curScreen = 0;
1352
1353 try {
1354 c = resolver.query(uri, null, null, null, "title ASC");
1355 } catch (Exception e) {
1356 // Ignore
1357 }
1358
Dan Sandlerd5024042014-01-09 15:01:33 -05001359 // We already have a favorites database in the old provider
1360 if (c != null) {
1361 try {
1362 if (c.getCount() > 0) {
1363 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1364 final int intentIndex
1365 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
1366 final int titleIndex
1367 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
1368 final int iconTypeIndex
1369 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
1370 final int iconIndex
1371 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1372 final int iconPackageIndex
1373 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
1374 final int iconResourceIndex
1375 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
1376 final int containerIndex
1377 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
1378 final int itemTypeIndex
1379 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
1380 final int screenIndex
1381 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
1382 final int cellXIndex
1383 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
1384 final int cellYIndex
1385 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
1386 final int uriIndex
1387 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1388 final int displayModeIndex
1389 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
Kenny Guyed131872014-04-30 03:02:21 +01001390 final int profileIndex
1391 = c.getColumnIndex(LauncherSettings.Favorites.PROFILE_ID);
Dan Sandlerd5024042014-01-09 15:01:33 -05001392
1393 int i = 0;
1394 int curX = 0;
1395 int curY = 0;
1396
1397 final LauncherAppState app = LauncherAppState.getInstance();
1398 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1399 final int width = (int) grid.numColumns;
1400 final int height = (int) grid.numRows;
1401 final int hotseatWidth = (int) grid.numHotseatIcons;
1402
1403 final HashSet<String> seenIntents = new HashSet<String>(c.getCount());
1404
Adam Cohen72960972014-01-15 18:13:55 -08001405 final ArrayList<ContentValues> shortcuts = new ArrayList<ContentValues>();
1406 final ArrayList<ContentValues> folders = new ArrayList<ContentValues>();
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001407 final SparseArray<ContentValues> hotseat = new SparseArray<ContentValues>();
Dan Sandlerd5024042014-01-09 15:01:33 -05001408
1409 while (c.moveToNext()) {
1410 final int itemType = c.getInt(itemTypeIndex);
1411 if (itemType != Favorites.ITEM_TYPE_APPLICATION
1412 && itemType != Favorites.ITEM_TYPE_SHORTCUT
1413 && itemType != Favorites.ITEM_TYPE_FOLDER) {
1414 continue;
1415 }
1416
1417 final int cellX = c.getInt(cellXIndex);
1418 final int cellY = c.getInt(cellYIndex);
1419 final int screen = c.getInt(screenIndex);
1420 int container = c.getInt(containerIndex);
1421 final String intentStr = c.getString(intentIndex);
Kenny Guyed131872014-04-30 03:02:21 +01001422
1423 UserManagerCompat userManager = UserManagerCompat.getInstance(mContext);
1424 UserHandleCompat userHandle;
1425 final long userSerialNumber;
1426 if (profileIndex != -1 && !c.isNull(profileIndex)) {
1427 userSerialNumber = c.getInt(profileIndex);
1428 userHandle = userManager.getUserForSerialNumber(userSerialNumber);
1429 } else {
1430 // Default to the serial number of this user, for older
1431 // shortcuts.
1432 userHandle = UserHandleCompat.myUserHandle();
1433 userSerialNumber = userManager.getSerialNumberForUser(userHandle);
1434 }
Dan Sandlerd5024042014-01-09 15:01:33 -05001435 Launcher.addDumpLog(TAG, "migrating \""
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001436 + c.getString(titleIndex) + "\" ("
1437 + cellX + "," + cellY + "@"
1438 + LauncherSettings.Favorites.containerToString(container)
1439 + "/" + screen
1440 + "): " + intentStr, true);
Dan Sandlerd5024042014-01-09 15:01:33 -05001441
1442 if (itemType != Favorites.ITEM_TYPE_FOLDER) {
Adam Cohen556f6132014-01-15 15:18:08 -08001443
1444 final Intent intent;
1445 final ComponentName cn;
1446 try {
1447 intent = Intent.parseUri(intentStr, 0);
1448 } catch (URISyntaxException e) {
1449 // bogus intent?
1450 Launcher.addDumpLog(TAG,
1451 "skipping invalid intent uri", true);
1452 continue;
1453 }
1454
1455 cn = intent.getComponent();
Dan Sandlerd5024042014-01-09 15:01:33 -05001456 if (TextUtils.isEmpty(intentStr)) {
1457 // no intent? no icon
1458 Launcher.addDumpLog(TAG, "skipping empty intent", true);
1459 continue;
Adam Cohen72960972014-01-15 18:13:55 -08001460 } else if (cn != null &&
Kenny Guyed131872014-04-30 03:02:21 +01001461 !LauncherModel.isValidPackageActivity(mContext, cn,
1462 userHandle)) {
Adam Cohen556f6132014-01-15 15:18:08 -08001463 // component no longer exists.
Adam Cohen72960972014-01-15 18:13:55 -08001464 Launcher.addDumpLog(TAG, "skipping item whose component " +
Adam Cohen556f6132014-01-15 15:18:08 -08001465 "no longer exists.", true);
1466 continue;
Adam Cohen72960972014-01-15 18:13:55 -08001467 } else if (container ==
1468 LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1469 // Dedupe icons directly on the workspace
1470
Adam Cohen556f6132014-01-15 15:18:08 -08001471 // Canonicalize
1472 // the Play Store sets the package parameter, but Launcher
Adam Cohena33f11e2014-10-24 12:20:20 -07001473 // does not, so we clear that out to keep them the same.
1474 // Also ignore intent flags for the purposes of deduping.
Adam Cohen556f6132014-01-15 15:18:08 -08001475 intent.setPackage(null);
Adam Cohena33f11e2014-10-24 12:20:20 -07001476 int flags = intent.getFlags();
1477 intent.setFlags(0);
Adam Cohen556f6132014-01-15 15:18:08 -08001478 final String key = intent.toUri(0);
Adam Cohena33f11e2014-10-24 12:20:20 -07001479 intent.setFlags(flags);
Adam Cohen556f6132014-01-15 15:18:08 -08001480 if (seenIntents.contains(key)) {
1481 Launcher.addDumpLog(TAG, "skipping duplicate", true);
Dan Sandlerd5024042014-01-09 15:01:33 -05001482 continue;
Adam Cohen556f6132014-01-15 15:18:08 -08001483 } else {
1484 seenIntents.add(key);
Dan Sandlerd5024042014-01-09 15:01:33 -05001485 }
1486 }
1487 }
1488
1489 ContentValues values = new ContentValues(c.getColumnCount());
1490 values.put(LauncherSettings.Favorites._ID, c.getInt(idIndex));
1491 values.put(LauncherSettings.Favorites.INTENT, intentStr);
1492 values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex));
1493 values.put(LauncherSettings.Favorites.ICON_TYPE,
1494 c.getInt(iconTypeIndex));
1495 values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex));
1496 values.put(LauncherSettings.Favorites.ICON_PACKAGE,
1497 c.getString(iconPackageIndex));
1498 values.put(LauncherSettings.Favorites.ICON_RESOURCE,
1499 c.getString(iconResourceIndex));
1500 values.put(LauncherSettings.Favorites.ITEM_TYPE, itemType);
1501 values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1);
1502 values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex));
1503 values.put(LauncherSettings.Favorites.DISPLAY_MODE,
1504 c.getInt(displayModeIndex));
Kenny Guyed131872014-04-30 03:02:21 +01001505 values.put(LauncherSettings.Favorites.PROFILE_ID, userSerialNumber);
Dan Sandlerd5024042014-01-09 15:01:33 -05001506
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001507 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1508 hotseat.put(screen, values);
Dan Sandlerd5024042014-01-09 15:01:33 -05001509 }
1510
1511 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1512 // In a folder or in the hotseat, preserve position
1513 values.put(LauncherSettings.Favorites.SCREEN, screen);
1514 values.put(LauncherSettings.Favorites.CELLX, cellX);
1515 values.put(LauncherSettings.Favorites.CELLY, cellY);
1516 } else {
Adam Cohen72960972014-01-15 18:13:55 -08001517 // For items contained directly on one of the workspace screen,
1518 // we'll determine their location (screen, x, y) in a second pass.
Dan Sandlerd5024042014-01-09 15:01:33 -05001519 }
1520
1521 values.put(LauncherSettings.Favorites.CONTAINER, container);
1522
Adam Cohen72960972014-01-15 18:13:55 -08001523 if (itemType != Favorites.ITEM_TYPE_FOLDER) {
1524 shortcuts.add(values);
1525 } else {
1526 folders.add(values);
1527 }
Dan Sandlerd5024042014-01-09 15:01:33 -05001528 }
1529
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001530 // Now that we have all the hotseat icons, let's go through them left-right
1531 // and assign valid locations for them in the new hotseat
1532 final int N = hotseat.size();
1533 for (int idx=0; idx<N; idx++) {
1534 int hotseatX = hotseat.keyAt(idx);
1535 ContentValues values = hotseat.valueAt(idx);
1536
1537 if (hotseatX == grid.hotseatAllAppsRank) {
1538 // let's drop this in the next available hole in the hotseat
1539 while (++hotseatX < hotseatWidth) {
1540 if (hotseat.get(hotseatX) == null) {
1541 // found a spot! move it here
1542 values.put(LauncherSettings.Favorites.SCREEN,
1543 hotseatX);
1544 break;
1545 }
1546 }
1547 }
1548 if (hotseatX >= hotseatWidth) {
1549 // no room for you in the hotseat? it's off to the desktop with you
1550 values.put(LauncherSettings.Favorites.CONTAINER,
1551 Favorites.CONTAINER_DESKTOP);
1552 }
1553 }
1554
Adam Cohen72960972014-01-15 18:13:55 -08001555 final ArrayList<ContentValues> allItems = new ArrayList<ContentValues>();
1556 // Folders first
1557 allItems.addAll(folders);
1558 // Then shortcuts
1559 allItems.addAll(shortcuts);
1560
1561 // Layout all the folders
1562 for (ContentValues values: allItems) {
1563 if (values.getAsInteger(LauncherSettings.Favorites.CONTAINER) !=
1564 LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1565 // Hotseat items and folder items have already had their
1566 // location information set. Nothing to be done here.
1567 continue;
1568 }
1569 values.put(LauncherSettings.Favorites.SCREEN, curScreen);
1570 values.put(LauncherSettings.Favorites.CELLX, curX);
1571 values.put(LauncherSettings.Favorites.CELLY, curY);
1572 curX = (curX + 1) % width;
1573 if (curX == 0) {
1574 curY = (curY + 1);
1575 }
1576 // Leave the last row of icons blank on every screen
1577 if (curY == height - 1) {
1578 curScreen = (int) generateNewScreenId();
1579 curY = 0;
1580 }
1581 }
1582
1583 if (allItems.size() > 0) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001584 db.beginTransaction();
1585 try {
Adam Cohen72960972014-01-15 18:13:55 -08001586 for (ContentValues row: allItems) {
1587 if (row == null) continue;
1588 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, row)
Dan Sandlerd5024042014-01-09 15:01:33 -05001589 < 0) {
1590 return;
1591 } else {
1592 count++;
1593 }
1594 }
1595 db.setTransactionSuccessful();
1596 } finally {
1597 db.endTransaction();
1598 }
1599 }
1600
1601 db.beginTransaction();
1602 try {
1603 for (i=0; i<=curScreen; i++) {
1604 final ContentValues values = new ContentValues();
1605 values.put(LauncherSettings.WorkspaceScreens._ID, i);
1606 values.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
1607 if (dbInsertAndCheck(this, db, TABLE_WORKSPACE_SCREENS, null, values)
1608 < 0) {
1609 return;
1610 }
1611 }
1612 db.setTransactionSuccessful();
1613 } finally {
1614 db.endTransaction();
1615 }
1616 }
1617 } finally {
1618 c.close();
1619 }
1620 }
1621
1622 Launcher.addDumpLog(TAG, "migrated " + count + " icons from Launcher2 into "
1623 + (curScreen+1) + " screens", true);
1624
1625 // ensure that new screens are created to hold these icons
1626 setFlagJustLoadedOldDb();
1627
1628 // Update max IDs; very important since we just grabbed IDs from another database
1629 mMaxItemId = initializeMaxItemId(db);
1630 mMaxScreenId = initializeMaxScreenId(db);
1631 if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId + " mMaxScreenId: " + mMaxScreenId);
1632 }
Mike Cleronb87bd162009-10-30 16:36:56 -07001633 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001634
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001635 /**
1636 * Build a query string that will match any row where the column matches
1637 * anything in the values list.
1638 */
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001639 private static String buildOrWhereString(String column, int[] values) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 StringBuilder selectWhere = new StringBuilder();
1641 for (int i = values.length - 1; i >= 0; i--) {
1642 selectWhere.append(column).append("=").append(values[i]);
1643 if (i > 0) {
1644 selectWhere.append(" OR ");
1645 }
1646 }
1647 return selectWhere.toString();
1648 }
1649
1650 static class SqlArguments {
1651 public final String table;
1652 public final String where;
1653 public final String[] args;
1654
1655 SqlArguments(Uri url, String where, String[] args) {
1656 if (url.getPathSegments().size() == 1) {
1657 this.table = url.getPathSegments().get(0);
1658 this.where = where;
1659 this.args = args;
1660 } else if (url.getPathSegments().size() != 2) {
1661 throw new IllegalArgumentException("Invalid URI: " + url);
1662 } else if (!TextUtils.isEmpty(where)) {
1663 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
1664 } else {
1665 this.table = url.getPathSegments().get(0);
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001666 this.where = "_id=" + ContentUris.parseId(url);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001667 this.args = null;
1668 }
1669 }
1670
1671 SqlArguments(Uri url) {
1672 if (url.getPathSegments().size() == 1) {
1673 table = url.getPathSegments().get(0);
1674 where = null;
1675 args = null;
1676 } else {
1677 throw new IllegalArgumentException("Invalid URI: " + url);
1678 }
1679 }
1680 }
Adam Cohen72960972014-01-15 18:13:55 -08001681}