blob: f2d005e653797847e3665f4614d376beef62c71c [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
Bjorn Bringertcd8fec02010-01-14 13:26:43 +000019import android.app.SearchManager;
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;
Bjorn Bringertcd8fec02010-01-14 13:26:43 +000022import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.content.ComponentName;
Adam Cohen228da5a2011-07-27 22:23:47 -070024import android.content.ContentProvider;
Yura085c8532014-02-11 15:15:29 +000025import android.content.ContentProviderOperation;
26import android.content.ContentProviderResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.content.ContentResolver;
Adam Cohen228da5a2011-07-27 22:23:47 -070028import android.content.ContentUris;
29import android.content.ContentValues;
30import android.content.Context;
31import android.content.Intent;
Yura085c8532014-02-11 15:15:29 +000032import android.content.OperationApplicationException;
Michael Jurkab85f8a42012-04-25 15:48:32 -070033import android.content.SharedPreferences;
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;
40import android.database.sqlite.SQLiteStatement;
Joe Onorato0589f0f2010-02-08 13:44:00 -080041import android.graphics.Bitmap;
42import android.graphics.BitmapFactory;
Adam Cohen228da5a2011-07-27 22:23:47 -070043import android.net.Uri;
44import android.provider.Settings;
45import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.util.Log;
Dan Sandlerab5fa3a2014-03-06 23:48:04 -050047import android.util.SparseArray;
Adam Cohen228da5a2011-07-27 22:23:47 -070048
Sunny Goyal0fe505b2014-08-06 09:55:36 -070049import com.android.launcher3.AutoInstallsLayout.LayoutParserCallback;
50import com.android.launcher3.LauncherSettings.Favorites;
Kenny Guyed131872014-04-30 03:02:21 +010051import com.android.launcher3.compat.UserHandleCompat;
52import com.android.launcher3.compat.UserManagerCompat;
Chris Wrene523e702013-10-09 10:36:55 -040053import com.android.launcher3.config.ProviderConfig;
Michael Jurka8b805b12012-04-18 14:23:14 -070054
Dan Sandlerd5024042014-01-09 15:01:33 -050055import java.io.File;
Mike Cleronb87bd162009-10-30 16:36:56 -070056import java.net.URISyntaxException;
Adam Cohen228da5a2011-07-27 22:23:47 -070057import java.util.ArrayList;
Adam Cohen71483f42014-05-15 14:04:01 -070058import java.util.Collections;
Dan Sandlerd5024042014-01-09 15:01:33 -050059import java.util.HashSet;
Bjorn Bringertcd8fec02010-01-14 13:26:43 +000060import java.util.List;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062public class LauncherProvider extends ContentProvider {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080063 private static final String TAG = "Launcher.LauncherProvider";
64 private static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065
Kenny Guyed131872014-04-30 03:02:21 +010066 private static final int DATABASE_VERSION = 20;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067
Adam Cohene25af792013-06-06 23:08:25 -070068 static final String OLD_AUTHORITY = "com.android.launcher2.settings";
Chris Wrene523e702013-10-09 10:36:55 -040069 static final String AUTHORITY = ProviderConfig.AUTHORITY;
Winson Chung3d503fb2011-07-13 17:25:49 -070070
Dan Sandlerf0b8dac2013-11-19 12:21:25 -050071 // Should we attempt to load anything from the com.android.launcher2 provider?
Dan Sandlerd5024042014-01-09 15:01:33 -050072 static final boolean IMPORT_LAUNCHER2_DATABASE = false;
Dan Sandlerf0b8dac2013-11-19 12:21:25 -050073
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074 static final String TABLE_FAVORITES = "favorites";
Adam Cohendcd297f2013-06-18 13:13:40 -070075 static final String TABLE_WORKSPACE_SCREENS = "workspaceScreens";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076 static final String PARAMETER_NOTIFY = "notify";
Winson Chungc763c4e2013-07-19 13:49:06 -070077 static final String UPGRADED_FROM_OLD_DATABASE =
78 "UPGRADED_FROM_OLD_DATABASE";
79 static final String EMPTY_DATABASE_CREATED =
80 "EMPTY_DATABASE_CREATED";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081
Adam Cohena043fa82014-07-23 14:49:38 -070082 private static final String URI_PARAM_IS_EXTERNAL_ADD = "isExternalAdd";
83
Anjali Koppal67e7cae2014-03-13 12:14:12 -070084 private LauncherProviderChangeListener mListener;
85
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -070086 /**
Romain Guy73b979d2009-06-09 12:57:21 -070087 * {@link Uri} triggered at any registered {@link android.database.ContentObserver} when
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -070088 * {@link AppWidgetHost#deleteHost()} is called during database creation.
89 * Use this to recall {@link AppWidgetHost#startListening()} if needed.
90 */
91 static final Uri CONTENT_APPWIDGET_RESET_URI =
92 Uri.parse("content://" + AUTHORITY + "/appWidgetReset");
Daniel Lehmannc3a80402012-04-23 21:35:11 -070093
Michael Jurkaa8c760d2011-04-28 14:59:33 -070094 private DatabaseHelper mOpenHelper;
Winson Chungc763c4e2013-07-19 13:49:06 -070095 private static boolean sJustLoadedFromOldDb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096
97 @Override
98 public boolean onCreate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040099 final Context context = getContext();
100 mOpenHelper = new DatabaseHelper(context);
101 LauncherAppState.setLauncherProvider(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102 return true;
103 }
104
Winson Chung0b560dd2014-01-21 13:00:26 -0800105 public boolean wasNewDbCreated() {
106 return mOpenHelper.wasNewDbCreated();
107 }
108
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700109 public void setLauncherProviderChangeListener(LauncherProviderChangeListener listener) {
110 mListener = listener;
111 }
112
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113 @Override
114 public String getType(Uri uri) {
115 SqlArguments args = new SqlArguments(uri, null, null);
116 if (TextUtils.isEmpty(args.where)) {
117 return "vnd.android.cursor.dir/" + args.table;
118 } else {
119 return "vnd.android.cursor.item/" + args.table;
120 }
121 }
122
123 @Override
124 public Cursor query(Uri uri, String[] projection, String selection,
125 String[] selectionArgs, String sortOrder) {
126
127 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
128 SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
129 qb.setTables(args.table);
130
Romain Guy73b979d2009-06-09 12:57:21 -0700131 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 Cursor result = qb.query(db, projection, args.where, args.args, null, null, sortOrder);
133 result.setNotificationUri(getContext().getContentResolver(), uri);
134
135 return result;
136 }
137
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700138 private static long dbInsertAndCheck(DatabaseHelper helper,
139 SQLiteDatabase db, String table, String nullColumnHack, ContentValues values) {
Dan Sandlerd5024042014-01-09 15:01:33 -0500140 if (values == null) {
141 throw new RuntimeException("Error: attempting to insert null values");
142 }
Adam Cohen71483f42014-05-15 14:04:01 -0700143 if (!values.containsKey(LauncherSettings.ChangeLogColumns._ID)) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700144 throw new RuntimeException("Error: attempting to add item without specifying an id");
145 }
Chris Wren5dee7af2013-12-20 17:22:11 -0500146 helper.checkId(table, values);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700147 return db.insert(table, nullColumnHack, values);
148 }
149
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 @Override
151 public Uri insert(Uri uri, ContentValues initialValues) {
152 SqlArguments args = new SqlArguments(uri);
153
Adam Cohena043fa82014-07-23 14:49:38 -0700154 // In very limited cases, we support system|signature permission apps to add to the db
155 String externalAdd = uri.getQueryParameter(URI_PARAM_IS_EXTERNAL_ADD);
156 if (externalAdd != null && "true".equals(externalAdd)) {
157 if (!mOpenHelper.initializeExternalAdd(initialValues)) {
158 return null;
159 }
160 }
161
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
Chris Wren1ada10d2013-09-13 18:01:38 -0400163 addModifiedTime(initialValues);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700164 final long rowId = dbInsertAndCheck(mOpenHelper, db, args.table, null, initialValues);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 if (rowId <= 0) return null;
166
167 uri = ContentUris.withAppendedId(uri, rowId);
168 sendNotify(uri);
169
170 return uri;
171 }
172
Adam Cohena043fa82014-07-23 14:49:38 -0700173
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 @Override
175 public int bulkInsert(Uri uri, ContentValues[] values) {
176 SqlArguments args = new SqlArguments(uri);
177
178 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
179 db.beginTransaction();
180 try {
181 int numValues = values.length;
182 for (int i = 0; i < numValues; i++) {
Chris Wren1ada10d2013-09-13 18:01:38 -0400183 addModifiedTime(values[i]);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700184 if (dbInsertAndCheck(mOpenHelper, db, args.table, null, values[i]) < 0) {
185 return 0;
186 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 }
188 db.setTransactionSuccessful();
189 } finally {
190 db.endTransaction();
191 }
192
193 sendNotify(uri);
194 return values.length;
195 }
196
197 @Override
Yura085c8532014-02-11 15:15:29 +0000198 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
199 throws OperationApplicationException {
200 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
201 db.beginTransaction();
202 try {
203 ContentProviderResult[] result = super.applyBatch(operations);
204 db.setTransactionSuccessful();
205 return result;
206 } finally {
207 db.endTransaction();
208 }
209 }
210
211 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212 public int delete(Uri uri, String selection, String[] selectionArgs) {
213 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
214
215 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
216 int count = db.delete(args.table, args.where, args.args);
217 if (count > 0) sendNotify(uri);
218
219 return count;
220 }
221
222 @Override
223 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
224 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
225
Chris Wren1ada10d2013-09-13 18:01:38 -0400226 addModifiedTime(values);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
228 int count = db.update(args.table, values, args.where, args.args);
229 if (count > 0) sendNotify(uri);
230
231 return count;
232 }
233
234 private void sendNotify(Uri uri) {
235 String notify = uri.getQueryParameter(PARAMETER_NOTIFY);
236 if (notify == null || "true".equals(notify)) {
237 getContext().getContentResolver().notifyChange(uri, null);
238 }
Chris Wren1ada10d2013-09-13 18:01:38 -0400239
240 // always notify the backup agent
Chris Wren92aa4232013-10-04 11:29:36 -0400241 LauncherBackupAgentHelper.dataChanged(getContext());
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700242 if (mListener != null) {
243 mListener.onLauncherProviderChange();
244 }
Chris Wren1ada10d2013-09-13 18:01:38 -0400245 }
246
247 private void addModifiedTime(ContentValues values) {
248 values.put(LauncherSettings.ChangeLogColumns.MODIFIED, System.currentTimeMillis());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 }
250
Adam Cohendcd297f2013-06-18 13:13:40 -0700251 public long generateNewItemId() {
252 return mOpenHelper.generateNewItemId();
253 }
254
Winson Chungc763c4e2013-07-19 13:49:06 -0700255 public void updateMaxItemId(long id) {
256 mOpenHelper.updateMaxItemId(id);
257 }
258
Adam Cohendcd297f2013-06-18 13:13:40 -0700259 public long generateNewScreenId() {
260 return mOpenHelper.generateNewScreenId();
261 }
262
263 // This is only required one time while loading the workspace during the
264 // upgrade path, and should never be called from anywhere else.
265 public void updateMaxScreenId(long maxScreenId) {
266 mOpenHelper.updateMaxScreenId(maxScreenId);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700267 }
268
Brian Muramatsu5524b492012-10-02 16:55:54 -0700269 /**
Adam Cohene25af792013-06-06 23:08:25 -0700270 * @param Should we load the old db for upgrade? first run only.
271 */
Winson Chungc763c4e2013-07-19 13:49:06 -0700272 synchronized public boolean justLoadedOldDb() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400273 String spKey = LauncherAppState.getSharedPreferencesKey();
Adam Cohene25af792013-06-06 23:08:25 -0700274 SharedPreferences sp = getContext().getSharedPreferences(spKey, Context.MODE_PRIVATE);
275
Winson Chungc763c4e2013-07-19 13:49:06 -0700276 boolean loadedOldDb = false || sJustLoadedFromOldDb;
Adam Cohendcd297f2013-06-18 13:13:40 -0700277
Winson Chungc763c4e2013-07-19 13:49:06 -0700278 sJustLoadedFromOldDb = false;
279 if (sp.getBoolean(UPGRADED_FROM_OLD_DATABASE, false)) {
Adam Cohene25af792013-06-06 23:08:25 -0700280
281 SharedPreferences.Editor editor = sp.edit();
Winson Chungc763c4e2013-07-19 13:49:06 -0700282 editor.remove(UPGRADED_FROM_OLD_DATABASE);
Adam Cohene25af792013-06-06 23:08:25 -0700283 editor.commit();
Winson Chungc763c4e2013-07-19 13:49:06 -0700284 loadedOldDb = true;
Adam Cohene25af792013-06-06 23:08:25 -0700285 }
Winson Chungc763c4e2013-07-19 13:49:06 -0700286 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -0700287 }
288
289 /**
Sunny Goyal42de82f2014-09-26 22:09:29 -0700290 * Clears all the data for a fresh start.
291 */
292 synchronized public void createEmptyDB() {
293 mOpenHelper.createEmptyDB(mOpenHelper.getWritableDatabase());
294 }
295
296 /**
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700297 * Loads the default workspace based on the following priority scheme:
298 * 1) From a package provided by play store
299 * 2) From a partner configuration APK, already in the system image
300 * 3) The default configuration for the particular device
Brian Muramatsu5524b492012-10-02 16:55:54 -0700301 */
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700302 synchronized public void loadDefaultFavoritesIfNecessary() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400303 String spKey = LauncherAppState.getSharedPreferencesKey();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700304 SharedPreferences sp = getContext().getSharedPreferences(spKey, Context.MODE_PRIVATE);
Adam Cohene25af792013-06-06 23:08:25 -0700305
Winson Chungc763c4e2013-07-19 13:49:06 -0700306 if (sp.getBoolean(EMPTY_DATABASE_CREATED, false)) {
Chris Wren5dee7af2013-12-20 17:22:11 -0500307 Log.d(TAG, "loading default workspace");
Michael Jurka45355c42012-10-08 13:21:35 +0200308
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700309 AutoInstallsLayout loader = AutoInstallsLayout.get(getContext(),
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700310 mOpenHelper.mAppWidgetHost, mOpenHelper);
311
312 if (loader == null) {
Adam Cohen9b8f51f2014-05-30 15:34:09 -0700313 final Partner partner = Partner.get(getContext().getPackageManager());
314 if (partner != null && partner.hasDefaultLayout()) {
315 final Resources partnerRes = partner.getResources();
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700316 int workspaceResId = partnerRes.getIdentifier(Partner.RES_DEFAULT_LAYOUT,
Adam Cohen9b8f51f2014-05-30 15:34:09 -0700317 "xml", partner.getPackageName());
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700318 if (workspaceResId != 0) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700319 loader = new DefaultLayoutParser(getContext(), mOpenHelper.mAppWidgetHost,
320 mOpenHelper, partnerRes, workspaceResId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700321 }
Adam Cohen9b8f51f2014-05-30 15:34:09 -0700322 }
323 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700324
325 if (loader == null) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700326 loader = new DefaultLayoutParser(getContext(), mOpenHelper.mAppWidgetHost,
327 mOpenHelper, getContext().getResources(), getDefaultWorkspaceResourceId());
Brian Muramatsu5524b492012-10-02 16:55:54 -0700328 }
329
Michael Jurkab85f8a42012-04-25 15:48:32 -0700330 // Populate favorites table with initial favorites
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700331 SharedPreferences.Editor editor = sp.edit().remove(EMPTY_DATABASE_CREATED);
332 mOpenHelper.loadFavorites(mOpenHelper.getWritableDatabase(), loader);
Michael Jurkab85f8a42012-04-25 15:48:32 -0700333 editor.commit();
334 }
335 }
336
Dan Sandlerd5024042014-01-09 15:01:33 -0500337 public void migrateLauncher2Shortcuts() {
338 mOpenHelper.migrateLauncher2Shortcuts(mOpenHelper.getWritableDatabase(),
Jason Monk0bfcceb2014-03-21 15:42:06 -0400339 Uri.parse(getContext().getString(R.string.old_launcher_provider_uri)));
Dan Sandlerd5024042014-01-09 15:01:33 -0500340 }
341
Nilesh Agrawalda41ea62013-12-09 14:17:49 -0800342 private static int getDefaultWorkspaceResourceId() {
Winson Chungbe876472014-05-14 14:15:20 -0700343 LauncherAppState app = LauncherAppState.getInstance();
344 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Adam Cohencee8c662014-10-16 09:49:52 -0700345 return grid.defaultLayoutId;
Nilesh Agrawalda41ea62013-12-09 14:17:49 -0800346 }
347
Winson Chungc763c4e2013-07-19 13:49:06 -0700348 private static interface ContentValuesCallback {
349 public void onRow(ContentValues values);
350 }
351
Adam Cohen6dbe0492013-12-02 17:00:14 -0800352 private static boolean shouldImportLauncher2Database(Context context) {
353 boolean isTablet = context.getResources().getBoolean(R.bool.is_tablet);
354
355 // We don't import the old databse for tablets, as the grid size has changed.
356 return !isTablet && IMPORT_LAUNCHER2_DATABASE;
357 }
358
Dan Sandlerd5024042014-01-09 15:01:33 -0500359 public void deleteDatabase() {
360 // Are you sure? (y/n)
361 final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
Dan Sandler2b471742014-01-21 14:14:41 -0500362 final File dbFile = new File(db.getPath());
Dan Sandlerd5024042014-01-09 15:01:33 -0500363 mOpenHelper.close();
Dan Sandler2b471742014-01-21 14:14:41 -0500364 if (dbFile.exists()) {
365 SQLiteDatabase.deleteDatabase(dbFile);
366 }
Dan Sandlerd5024042014-01-09 15:01:33 -0500367 mOpenHelper = new DatabaseHelper(getContext());
368 }
369
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700370 private static class DatabaseHelper extends SQLiteOpenHelper implements LayoutParserCallback {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800371 private final Context mContext;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700372 private final AppWidgetHost mAppWidgetHost;
Adam Cohendcd297f2013-06-18 13:13:40 -0700373 private long mMaxItemId = -1;
374 private long mMaxScreenId = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375
Winson Chung0b560dd2014-01-21 13:00:26 -0800376 private boolean mNewDbCreated = false;
377
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 DatabaseHelper(Context context) {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100379 super(context, LauncherFiles.LAUNCHER_DB, null, DATABASE_VERSION);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380 mContext = context;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700381 mAppWidgetHost = new AppWidgetHost(context, Launcher.APPWIDGET_HOST_ID);
Winson Chung3d503fb2011-07-13 17:25:49 -0700382
383 // In the case where neither onCreate nor onUpgrade gets called, we read the maxId from
384 // the DB here
Adam Cohendcd297f2013-06-18 13:13:40 -0700385 if (mMaxItemId == -1) {
386 mMaxItemId = initializeMaxItemId(getWritableDatabase());
387 }
388 if (mMaxScreenId == -1) {
389 mMaxScreenId = initializeMaxScreenId(getWritableDatabase());
Winson Chung3d503fb2011-07-13 17:25:49 -0700390 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391 }
392
Winson Chung0b560dd2014-01-21 13:00:26 -0800393 public boolean wasNewDbCreated() {
394 return mNewDbCreated;
395 }
396
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700397 /**
398 * Send notification that we've deleted the {@link AppWidgetHost},
399 * probably as part of the initial database creation. The receiver may
400 * want to re-call {@link AppWidgetHost#startListening()} to ensure
401 * callbacks are correctly set.
402 */
403 private void sendAppWidgetResetNotify() {
404 final ContentResolver resolver = mContext.getContentResolver();
405 resolver.notifyChange(CONTENT_APPWIDGET_RESET_URI, null);
406 }
407
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 @Override
409 public void onCreate(SQLiteDatabase db) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800410 if (LOGD) Log.d(TAG, "creating new launcher database");
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700411
Adam Cohendcd297f2013-06-18 13:13:40 -0700412 mMaxItemId = 1;
413 mMaxScreenId = 0;
Winson Chung0b560dd2014-01-21 13:00:26 -0800414 mNewDbCreated = true;
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700415
Kenny Guyed131872014-04-30 03:02:21 +0100416 UserManagerCompat userManager = UserManagerCompat.getInstance(mContext);
417 long userSerialNumber = userManager.getSerialNumberForUser(
418 UserHandleCompat.myUserHandle());
419
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800420 db.execSQL("CREATE TABLE favorites (" +
421 "_id INTEGER PRIMARY KEY," +
422 "title TEXT," +
423 "intent TEXT," +
424 "container INTEGER," +
425 "screen INTEGER," +
426 "cellX INTEGER," +
427 "cellY INTEGER," +
428 "spanX INTEGER," +
429 "spanY INTEGER," +
430 "itemType INTEGER," +
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700431 "appWidgetId INTEGER NOT NULL DEFAULT -1," +
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800432 "isShortcut INTEGER," +
433 "iconType INTEGER," +
434 "iconPackage TEXT," +
435 "iconResource TEXT," +
436 "icon BLOB," +
437 "uri TEXT," +
Chris Wrend5e66bf2013-09-16 14:02:29 -0400438 "displayMode INTEGER," +
Chris Wren1ada10d2013-09-13 18:01:38 -0400439 "appWidgetProvider TEXT," +
Chris Wrenf4d08112014-01-16 18:13:56 -0500440 "modified INTEGER NOT NULL DEFAULT 0," +
Kenny Guyed131872014-04-30 03:02:21 +0100441 "restored INTEGER NOT NULL DEFAULT 0," +
442 "profileId INTEGER DEFAULT " + userSerialNumber +
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443 ");");
Adam Cohendcd297f2013-06-18 13:13:40 -0700444 addWorkspacesTable(db);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700446 // Database was just created, so wipe any previous widgets
447 if (mAppWidgetHost != null) {
448 mAppWidgetHost.deleteHost();
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700449 sendAppWidgetResetNotify();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700451
Adam Cohen6dbe0492013-12-02 17:00:14 -0800452 if (shouldImportLauncher2Database(mContext)) {
Dan Sandlerf0b8dac2013-11-19 12:21:25 -0500453 // Try converting the old database
454 ContentValuesCallback permuteScreensCb = new ContentValuesCallback() {
455 public void onRow(ContentValues values) {
456 int container = values.getAsInteger(LauncherSettings.Favorites.CONTAINER);
457 if (container == Favorites.CONTAINER_DESKTOP) {
458 int screen = values.getAsInteger(LauncherSettings.Favorites.SCREEN);
459 screen = (int) upgradeLauncherDb_permuteScreens(screen);
460 values.put(LauncherSettings.Favorites.SCREEN, screen);
461 }
462 }
463 };
464 Uri uri = Uri.parse("content://" + Settings.AUTHORITY +
465 "/old_favorites?notify=true");
466 if (!convertDatabase(db, uri, permuteScreensCb, true)) {
467 // Try and upgrade from the Launcher2 db
Jason Monk0bfcceb2014-03-21 15:42:06 -0400468 uri = Uri.parse(mContext.getString(R.string.old_launcher_provider_uri));
Dan Sandlerf0b8dac2013-11-19 12:21:25 -0500469 if (!convertDatabase(db, uri, permuteScreensCb, false)) {
470 // If we fail, then set a flag to load the default workspace
471 setFlagEmptyDbCreated();
472 return;
Winson Chungc763c4e2013-07-19 13:49:06 -0700473 }
474 }
Dan Sandlerf0b8dac2013-11-19 12:21:25 -0500475 // Right now, in non-default workspace cases, we want to run the final
476 // upgrade code (ie. to fix workspace screen indices -> ids, etc.), so
477 // set that flag too.
478 setFlagJustLoadedOldDb();
479 } else {
480 // Fresh and clean launcher DB.
481 mMaxItemId = initializeMaxItemId(db);
482 setFlagEmptyDbCreated();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800483 }
484 }
485
Adam Cohendcd297f2013-06-18 13:13:40 -0700486 private void addWorkspacesTable(SQLiteDatabase db) {
487 db.execSQL("CREATE TABLE " + TABLE_WORKSPACE_SCREENS + " (" +
488 LauncherSettings.WorkspaceScreens._ID + " INTEGER," +
Chris Wren1ada10d2013-09-13 18:01:38 -0400489 LauncherSettings.WorkspaceScreens.SCREEN_RANK + " INTEGER," +
490 LauncherSettings.ChangeLogColumns.MODIFIED + " INTEGER NOT NULL DEFAULT 0" +
Adam Cohendcd297f2013-06-18 13:13:40 -0700491 ");");
492 }
493
Adam Cohen119285e2014-04-02 16:59:08 -0700494 private void removeOrphanedItems(SQLiteDatabase db) {
Adam Cohenf9c14de2014-04-17 18:20:45 -0700495 // Delete items directly on the workspace who's screen id doesn't exist
496 // "DELETE FROM favorites WHERE screen NOT IN (SELECT _id FROM workspaceScreens)
497 // AND container = -100"
498 String removeOrphanedDesktopItems = "DELETE FROM " + TABLE_FAVORITES +
499 " WHERE " +
Adam Cohen119285e2014-04-02 16:59:08 -0700500 LauncherSettings.Favorites.SCREEN + " NOT IN (SELECT " +
Adam Cohenf9c14de2014-04-17 18:20:45 -0700501 LauncherSettings.WorkspaceScreens._ID + " FROM " + TABLE_WORKSPACE_SCREENS + ")" +
502 " AND " +
503 LauncherSettings.Favorites.CONTAINER + " = " +
504 LauncherSettings.Favorites.CONTAINER_DESKTOP;
505 db.execSQL(removeOrphanedDesktopItems);
506
507 // Delete items contained in folders which no longer exist (after above statement)
508 // "DELETE FROM favorites WHERE container <> -100 AND container <> -101 AND container
509 // NOT IN (SELECT _id FROM favorites WHERE itemType = 2)"
510 String removeOrphanedFolderItems = "DELETE FROM " + TABLE_FAVORITES +
511 " WHERE " +
512 LauncherSettings.Favorites.CONTAINER + " <> " +
513 LauncherSettings.Favorites.CONTAINER_DESKTOP +
514 " AND "
515 + LauncherSettings.Favorites.CONTAINER + " <> " +
516 LauncherSettings.Favorites.CONTAINER_HOTSEAT +
517 " AND "
518 + LauncherSettings.Favorites.CONTAINER + " NOT IN (SELECT " +
519 LauncherSettings.Favorites._ID + " FROM " + TABLE_FAVORITES +
520 " WHERE " + LauncherSettings.Favorites.ITEM_TYPE + " = " +
521 LauncherSettings.Favorites.ITEM_TYPE_FOLDER + ")";
522 db.execSQL(removeOrphanedFolderItems);
Adam Cohen119285e2014-04-02 16:59:08 -0700523 }
524
Winson Chungc763c4e2013-07-19 13:49:06 -0700525 private void setFlagJustLoadedOldDb() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400526 String spKey = LauncherAppState.getSharedPreferencesKey();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700527 SharedPreferences sp = mContext.getSharedPreferences(spKey, Context.MODE_PRIVATE);
528 SharedPreferences.Editor editor = sp.edit();
Winson Chungc763c4e2013-07-19 13:49:06 -0700529 editor.putBoolean(UPGRADED_FROM_OLD_DATABASE, true);
530 editor.putBoolean(EMPTY_DATABASE_CREATED, false);
Michael Jurkab85f8a42012-04-25 15:48:32 -0700531 editor.commit();
532 }
533
Winson Chungc763c4e2013-07-19 13:49:06 -0700534 private void setFlagEmptyDbCreated() {
535 String spKey = LauncherAppState.getSharedPreferencesKey();
536 SharedPreferences sp = mContext.getSharedPreferences(spKey, Context.MODE_PRIVATE);
537 SharedPreferences.Editor editor = sp.edit();
538 editor.putBoolean(EMPTY_DATABASE_CREATED, true);
539 editor.putBoolean(UPGRADED_FROM_OLD_DATABASE, false);
540 editor.commit();
541 }
542
543 // We rearrange the screens from the old launcher
544 // 12345 -> 34512
545 private long upgradeLauncherDb_permuteScreens(long screen) {
546 if (screen >= 2) {
547 return screen - 2;
548 } else {
549 return screen + 3;
550 }
551 }
552
553 private boolean convertDatabase(SQLiteDatabase db, Uri uri,
554 ContentValuesCallback cb, boolean deleteRows) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800555 if (LOGD) Log.d(TAG, "converting database from an older format, but not onUpgrade");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800556 boolean converted = false;
557
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800558 final ContentResolver resolver = mContext.getContentResolver();
559 Cursor cursor = null;
560
561 try {
562 cursor = resolver.query(uri, null, null, null, null);
563 } catch (Exception e) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700564 // Ignore
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800565 }
566
567 // We already have a favorites database in the old provider
Winson Chungc763c4e2013-07-19 13:49:06 -0700568 if (cursor != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800569 try {
Winson Chungc763c4e2013-07-19 13:49:06 -0700570 if (cursor.getCount() > 0) {
571 converted = copyFromCursor(db, cursor, cb) > 0;
572 if (converted && deleteRows) {
573 resolver.delete(uri, null, null);
574 }
575 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800576 } finally {
577 cursor.close();
578 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800579 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700580
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800581 if (converted) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700582 // Convert widgets from this import into widgets
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800583 if (LOGD) Log.d(TAG, "converted and now triggering widget upgrade");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800584 convertWidgets(db);
Winson Chungc763c4e2013-07-19 13:49:06 -0700585
586 // Update max item id
587 mMaxItemId = initializeMaxItemId(db);
588 if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800589 }
590
591 return converted;
592 }
593
Winson Chungc763c4e2013-07-19 13:49:06 -0700594 private int copyFromCursor(SQLiteDatabase db, Cursor c, ContentValuesCallback cb) {
Romain Guy73b979d2009-06-09 12:57:21 -0700595 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800596 final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
597 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
598 final int iconTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
599 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
600 final int iconPackageIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
601 final int iconResourceIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
602 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
603 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
604 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
605 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
606 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
607 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
608 final int displayModeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
609
610 ContentValues[] rows = new ContentValues[c.getCount()];
611 int i = 0;
612 while (c.moveToNext()) {
613 ContentValues values = new ContentValues(c.getColumnCount());
Romain Guy73b979d2009-06-09 12:57:21 -0700614 values.put(LauncherSettings.Favorites._ID, c.getLong(idIndex));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800615 values.put(LauncherSettings.Favorites.INTENT, c.getString(intentIndex));
616 values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex));
617 values.put(LauncherSettings.Favorites.ICON_TYPE, c.getInt(iconTypeIndex));
618 values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex));
619 values.put(LauncherSettings.Favorites.ICON_PACKAGE, c.getString(iconPackageIndex));
620 values.put(LauncherSettings.Favorites.ICON_RESOURCE, c.getString(iconResourceIndex));
621 values.put(LauncherSettings.Favorites.CONTAINER, c.getInt(containerIndex));
622 values.put(LauncherSettings.Favorites.ITEM_TYPE, c.getInt(itemTypeIndex));
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700623 values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800624 values.put(LauncherSettings.Favorites.SCREEN, c.getInt(screenIndex));
625 values.put(LauncherSettings.Favorites.CELLX, c.getInt(cellXIndex));
626 values.put(LauncherSettings.Favorites.CELLY, c.getInt(cellYIndex));
627 values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex));
628 values.put(LauncherSettings.Favorites.DISPLAY_MODE, c.getInt(displayModeIndex));
Winson Chungc763c4e2013-07-19 13:49:06 -0700629 if (cb != null) {
630 cb.onRow(values);
631 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632 rows[i++] = values;
633 }
634
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800635 int total = 0;
Winson Chungc763c4e2013-07-19 13:49:06 -0700636 if (i > 0) {
637 db.beginTransaction();
638 try {
639 int numValues = rows.length;
640 for (i = 0; i < numValues; i++) {
641 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, rows[i]) < 0) {
642 return 0;
643 } else {
644 total++;
645 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646 }
Winson Chungc763c4e2013-07-19 13:49:06 -0700647 db.setTransactionSuccessful();
648 } finally {
649 db.endTransaction();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651 }
652
653 return total;
654 }
655
656 @Override
657 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Winson Chungc763c4e2013-07-19 13:49:06 -0700658 if (LOGD) Log.d(TAG, "onUpgrade triggered: " + oldVersion);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700659
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 int version = oldVersion;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700661 if (version < 3) {
662 // upgrade 1,2 -> 3 added appWidgetId column
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 db.beginTransaction();
664 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700665 // Insert new column for holding appWidgetIds
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800666 db.execSQL("ALTER TABLE favorites " +
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700667 "ADD COLUMN appWidgetId INTEGER NOT NULL DEFAULT -1;");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 db.setTransactionSuccessful();
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700669 version = 3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 } catch (SQLException ex) {
671 // Old version remains, which means we wipe old data
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800672 Log.e(TAG, ex.getMessage(), ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 } finally {
674 db.endTransaction();
675 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700676
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 // Convert existing widgets only if table upgrade was successful
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700678 if (version == 3) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 convertWidgets(db);
680 }
681 }
Romain Guy73b979d2009-06-09 12:57:21 -0700682
683 if (version < 4) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800684 version = 4;
Romain Guy73b979d2009-06-09 12:57:21 -0700685 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700686
Romain Guy509cd6a2010-03-23 15:10:56 -0700687 // Where's version 5?
688 // - Donut and sholes on 2.0 shipped with version 4 of launcher1.
Daniel Sandler325dc232013-06-05 22:57:57 -0400689 // - Passion shipped on 2.1 with version 6 of launcher3
Romain Guy509cd6a2010-03-23 15:10:56 -0700690 // - Sholes shipped on 2.1r1 (aka Mr. 3) with version 5 of launcher 1
691 // but version 5 on there was the updateContactsShortcuts change
692 // which was version 6 in launcher 2 (first shipped on passion 2.1r1).
693 // The updateContactsShortcuts change is idempotent, so running it twice
694 // is okay so we'll do that when upgrading the devices that shipped with it.
695 if (version < 6) {
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800696 // We went from 3 to 5 screens. Move everything 1 to the right
697 db.beginTransaction();
698 try {
699 db.execSQL("UPDATE favorites SET screen=(screen + 1);");
700 db.setTransactionSuccessful();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800701 } catch (SQLException ex) {
702 // Old version remains, which means we wipe old data
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800703 Log.e(TAG, ex.getMessage(), ex);
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800704 } finally {
705 db.endTransaction();
706 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700707
Romain Guy509cd6a2010-03-23 15:10:56 -0700708 // We added the fast track.
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800709 if (updateContactsShortcuts(db)) {
710 version = 6;
711 }
712 }
Bjorn Bringert7984c942009-12-09 15:38:25 +0000713
714 if (version < 7) {
715 // Version 7 gets rid of the special search widget.
716 convertWidgets(db);
717 version = 7;
718 }
719
Joe Onorato0589f0f2010-02-08 13:44:00 -0800720 if (version < 8) {
721 // Version 8 (froyo) has the icons all normalized. This should
722 // already be the case in practice, but we now rely on it and don't
723 // resample the images each time.
724 normalizeIcons(db);
725 version = 8;
726 }
727
Winson Chung3d503fb2011-07-13 17:25:49 -0700728 if (version < 9) {
729 // The max id is not yet set at this point (onUpgrade is triggered in the ctor
730 // 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 -0700731 if (mMaxItemId == -1) {
732 mMaxItemId = initializeMaxItemId(db);
Winson Chung3d503fb2011-07-13 17:25:49 -0700733 }
734
735 // Add default hotseat icons
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700736 loadFavorites(db, new DefaultLayoutParser(mContext, mAppWidgetHost, this,
737 mContext.getResources(), R.xml.update_workspace));
Winson Chung3d503fb2011-07-13 17:25:49 -0700738 version = 9;
739 }
740
Daniel Lehmannd02402c2012-05-14 18:30:53 -0700741 // We bumped the version three time during JB, once to update the launch flags, once to
742 // update the override for the default launch animation and once to set the mimetype
743 // to improve startup performance
744 if (version < 12) {
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700745 // Contact shortcuts need a different set of flags to be launched now
746 // The updateContactsShortcuts change is idempotent, so we can keep using it like
747 // back in the Donut days
748 updateContactsShortcuts(db);
Daniel Lehmannd02402c2012-05-14 18:30:53 -0700749 version = 12;
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700750 }
751
Adam Cohendcd297f2013-06-18 13:13:40 -0700752 if (version < 13) {
753 // With the new shrink-wrapped and re-orderable workspaces, it makes sense
754 // to persist workspace screens and their relative order.
755 mMaxScreenId = 0;
756
757 // This will never happen in the wild, but when we switch to using workspace
758 // screen ids, redo the import from old launcher.
Winson Chungc763c4e2013-07-19 13:49:06 -0700759 sJustLoadedFromOldDb = true;
Adam Cohendcd297f2013-06-18 13:13:40 -0700760
761 addWorkspacesTable(db);
762 version = 13;
763 }
764
Chris Wrend5e66bf2013-09-16 14:02:29 -0400765 if (version < 14) {
766 db.beginTransaction();
767 try {
768 // Insert new column for holding widget provider name
769 db.execSQL("ALTER TABLE favorites " +
770 "ADD COLUMN appWidgetProvider TEXT;");
771 db.setTransactionSuccessful();
772 version = 14;
773 } catch (SQLException ex) {
774 // Old version remains, which means we wipe old data
775 Log.e(TAG, ex.getMessage(), ex);
776 } finally {
777 db.endTransaction();
778 }
779 }
780
Chris Wren1ada10d2013-09-13 18:01:38 -0400781 if (version < 15) {
782 db.beginTransaction();
783 try {
784 // Insert new column for holding update timestamp
785 db.execSQL("ALTER TABLE favorites " +
786 "ADD COLUMN modified INTEGER NOT NULL DEFAULT 0;");
787 db.execSQL("ALTER TABLE workspaceScreens " +
788 "ADD COLUMN modified INTEGER NOT NULL DEFAULT 0;");
789 db.setTransactionSuccessful();
790 version = 15;
791 } catch (SQLException ex) {
792 // Old version remains, which means we wipe old data
793 Log.e(TAG, ex.getMessage(), ex);
794 } finally {
795 db.endTransaction();
796 }
797 }
798
Chris Wrenf4d08112014-01-16 18:13:56 -0500799
800 if (version < 16) {
801 db.beginTransaction();
802 try {
803 // Insert new column for holding restore status
804 db.execSQL("ALTER TABLE favorites " +
805 "ADD COLUMN restored INTEGER NOT NULL DEFAULT 0;");
806 db.setTransactionSuccessful();
807 version = 16;
808 } catch (SQLException ex) {
809 // Old version remains, which means we wipe old data
810 Log.e(TAG, ex.getMessage(), ex);
811 } finally {
812 db.endTransaction();
813 }
814 }
815
Adam Cohen71e03b92014-02-21 14:09:53 -0800816 if (version < 17) {
817 // We use the db version upgrade here to identify users who may not have seen
818 // clings yet (because they weren't available), but for whom the clings are now
819 // available (tablet users). Because one of the possible cling flows (migration)
820 // is very destructive (wipes out workspaces), we want to prevent this from showing
821 // until clear data. We do so by marking that the clings have been shown.
822 LauncherClings.synchonouslyMarkFirstRunClingDismissed(mContext);
823 version = 17;
824 }
825
Adam Cohen119285e2014-04-02 16:59:08 -0700826 if (version < 18) {
Adam Cohenf9c14de2014-04-17 18:20:45 -0700827 // No-op
828 version = 18;
829 }
830
831 if (version < 19) {
Adam Cohen119285e2014-04-02 16:59:08 -0700832 // Due to a data loss bug, some users may have items associated with screen ids
833 // which no longer exist. Since this can cause other problems, and since the user
834 // will never see these items anyway, we use database upgrade as an opportunity to
835 // clean things up.
Adam Cohenf9c14de2014-04-17 18:20:45 -0700836 removeOrphanedItems(db);
837 version = 19;
Adam Cohen119285e2014-04-02 16:59:08 -0700838 }
839
Kenny Guyed131872014-04-30 03:02:21 +0100840 if (version < 20) {
841 // Add userId column
842 if (addProfileColumn(db)) {
843 version = 20;
844 }
845 // else old version remains, which means we wipe old data
846 }
847
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800848 if (version != DATABASE_VERSION) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800849 Log.w(TAG, "Destroying all old data.");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850 db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES);
Adam Cohendcd297f2013-06-18 13:13:40 -0700851 db.execSQL("DROP TABLE IF EXISTS " + TABLE_WORKSPACE_SCREENS);
852
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853 onCreate(db);
854 }
855 }
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800856
Adam Cohen9b1d0622014-05-21 19:01:57 -0700857 @Override
858 public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
859 // This shouldn't happen -- throw our hands up in the air and start over.
860 Log.w(TAG, "Database version downgrade from: " + oldVersion + " to " + newVersion +
861 ". Wiping databse.");
Sunny Goyal42de82f2014-09-26 22:09:29 -0700862 createEmptyDB(db);
863 }
Adam Cohen9b1d0622014-05-21 19:01:57 -0700864
Sunny Goyal42de82f2014-09-26 22:09:29 -0700865
866 /**
867 * Clears all the data for a fresh start.
868 */
869 public void createEmptyDB(SQLiteDatabase db) {
Adam Cohen9b1d0622014-05-21 19:01:57 -0700870 db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES);
871 db.execSQL("DROP TABLE IF EXISTS " + TABLE_WORKSPACE_SCREENS);
872 onCreate(db);
873 }
874
Kenny Guyed131872014-04-30 03:02:21 +0100875 private boolean addProfileColumn(SQLiteDatabase db) {
876 db.beginTransaction();
877 try {
878 UserManagerCompat userManager = UserManagerCompat.getInstance(mContext);
879 // Default to the serial number of this user, for older
880 // shortcuts.
881 long userSerialNumber = userManager.getSerialNumberForUser(
882 UserHandleCompat.myUserHandle());
883 // Insert new column for holding user serial number
884 db.execSQL("ALTER TABLE favorites " +
885 "ADD COLUMN profileId INTEGER DEFAULT "
886 + userSerialNumber + ";");
887 db.setTransactionSuccessful();
888 } catch (SQLException ex) {
889 // Old version remains, which means we wipe old data
890 Log.e(TAG, ex.getMessage(), ex);
891 return false;
892 } finally {
893 db.endTransaction();
894 }
895 return true;
896 }
897
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800898 private boolean updateContactsShortcuts(SQLiteDatabase db) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800899 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE,
900 new int[] { Favorites.ITEM_TYPE_SHORTCUT });
901
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700902 Cursor c = null;
903 final String actionQuickContact = "com.android.contacts.action.QUICK_CONTACT";
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800904 db.beginTransaction();
905 try {
906 // Select and iterate through each matching widget
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700907 c = db.query(TABLE_FAVORITES,
908 new String[] { Favorites._ID, Favorites.INTENT },
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800909 selectWhere, null, null, null, null);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700910 if (c == null) return false;
911
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800912 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700913
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800914 final int idIndex = c.getColumnIndex(Favorites._ID);
915 final int intentIndex = c.getColumnIndex(Favorites.INTENT);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700916
917 while (c.moveToNext()) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800918 long favoriteId = c.getLong(idIndex);
919 final String intentUri = c.getString(intentIndex);
920 if (intentUri != null) {
921 try {
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700922 final Intent intent = Intent.parseUri(intentUri, 0);
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800923 android.util.Log.d("Home", intent.toString());
924 final Uri uri = intent.getData();
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700925 if (uri != null) {
926 final String data = uri.toString();
927 if ((Intent.ACTION_VIEW.equals(intent.getAction()) ||
928 actionQuickContact.equals(intent.getAction())) &&
929 (data.startsWith("content://contacts/people/") ||
930 data.startsWith("content://com.android.contacts/" +
931 "contacts/lookup/"))) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800932
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700933 final Intent newIntent = new Intent(actionQuickContact);
934 // When starting from the launcher, start in a new, cleared task
935 // CLEAR_WHEN_TASK_RESET cannot reset the root of a task, so we
936 // clear the whole thing preemptively here since
937 // QuickContactActivity will finish itself when launching other
938 // detail activities.
939 newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
940 Intent.FLAG_ACTIVITY_CLEAR_TASK);
Winson Chung2672ff92012-05-04 16:22:30 -0700941 newIntent.putExtra(
942 Launcher.INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION, true);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700943 newIntent.setData(uri);
Daniel Lehmannd02402c2012-05-14 18:30:53 -0700944 // Determine the type and also put that in the shortcut
945 // (that can speed up launch a bit)
946 newIntent.setDataAndType(uri, newIntent.resolveType(mContext));
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800947
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700948 final ContentValues values = new ContentValues();
949 values.put(LauncherSettings.Favorites.INTENT,
950 newIntent.toUri(0));
951
952 String updateWhere = Favorites._ID + "=" + favoriteId;
953 db.update(TABLE_FAVORITES, values, updateWhere, null);
954 }
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800955 }
956 } catch (RuntimeException ex) {
957 Log.e(TAG, "Problem upgrading shortcut", ex);
958 } catch (URISyntaxException e) {
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700959 Log.e(TAG, "Problem upgrading shortcut", e);
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800960 }
961 }
962 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700963
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800964 db.setTransactionSuccessful();
965 } catch (SQLException ex) {
966 Log.w(TAG, "Problem while upgrading contacts", ex);
967 return false;
968 } finally {
969 db.endTransaction();
970 if (c != null) {
971 c.close();
972 }
973 }
974
975 return true;
976 }
977
Joe Onorato0589f0f2010-02-08 13:44:00 -0800978 private void normalizeIcons(SQLiteDatabase db) {
979 Log.d(TAG, "normalizing icons");
980
Joe Onorato346e1292010-02-18 10:34:24 -0500981 db.beginTransaction();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800982 Cursor c = null;
Joe Onorato9690b392010-03-23 17:34:37 -0400983 SQLiteStatement update = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800984 try {
985 boolean logged = false;
Joe Onorato9690b392010-03-23 17:34:37 -0400986 update = db.compileStatement("UPDATE favorites "
Jeff Hamiltoneaf77d62010-02-13 00:08:17 -0600987 + "SET icon=? WHERE _id=?");
Joe Onorato0589f0f2010-02-08 13:44:00 -0800988
989 c = db.rawQuery("SELECT _id, icon FROM favorites WHERE iconType=" +
990 Favorites.ICON_TYPE_BITMAP, null);
991
992 final int idIndex = c.getColumnIndexOrThrow(Favorites._ID);
993 final int iconIndex = c.getColumnIndexOrThrow(Favorites.ICON);
994
995 while (c.moveToNext()) {
996 long id = c.getLong(idIndex);
997 byte[] data = c.getBlob(iconIndex);
998 try {
Sunny Goyal2fce90c2014-10-07 12:01:58 -0700999 Bitmap bitmap = Utilities.createIconBitmap(
Joe Onorato0589f0f2010-02-08 13:44:00 -08001000 BitmapFactory.decodeByteArray(data, 0, data.length),
1001 mContext);
1002 if (bitmap != null) {
1003 update.bindLong(1, id);
1004 data = ItemInfo.flattenBitmap(bitmap);
1005 if (data != null) {
1006 update.bindBlob(2, data);
1007 update.execute();
1008 }
1009 bitmap.recycle();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001010 }
1011 } catch (Exception e) {
1012 if (!logged) {
1013 Log.e(TAG, "Failed normalizing icon " + id, e);
1014 } else {
1015 Log.e(TAG, "Also failed normalizing icon " + id);
1016 }
1017 logged = true;
1018 }
1019 }
Bjorn Bringert3a928e42010-02-19 11:15:40 +00001020 db.setTransactionSuccessful();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001021 } catch (SQLException ex) {
1022 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
1023 } finally {
1024 db.endTransaction();
Joe Onorato9690b392010-03-23 17:34:37 -04001025 if (update != null) {
1026 update.close();
1027 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001028 if (c != null) {
1029 c.close();
1030 }
1031 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001032 }
1033
1034 // Generates a new ID to use for an object in your database. This method should be only
1035 // called from the main UI thread. As an exception, we do call it when we call the
1036 // constructor from the worker thread; however, this doesn't extend until after the
1037 // constructor is called, and we only pass a reference to LauncherProvider to LauncherApp
1038 // after that point
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001039 @Override
Adam Cohendcd297f2013-06-18 13:13:40 -07001040 public long generateNewItemId() {
1041 if (mMaxItemId < 0) {
1042 throw new RuntimeException("Error: max item id was not initialized");
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001043 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001044 mMaxItemId += 1;
1045 return mMaxItemId;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001046 }
1047
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001048 @Override
1049 public long insertAndCheck(SQLiteDatabase db, ContentValues values) {
1050 return dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values);
1051 }
1052
Winson Chungc763c4e2013-07-19 13:49:06 -07001053 public void updateMaxItemId(long id) {
1054 mMaxItemId = id + 1;
1055 }
1056
Chris Wren5dee7af2013-12-20 17:22:11 -05001057 public void checkId(String table, ContentValues values) {
1058 long id = values.getAsLong(LauncherSettings.BaseLauncherColumns._ID);
1059 if (table == LauncherProvider.TABLE_WORKSPACE_SCREENS) {
1060 mMaxScreenId = Math.max(id, mMaxScreenId);
1061 } else {
1062 mMaxItemId = Math.max(id, mMaxItemId);
1063 }
1064 }
1065
Adam Cohendcd297f2013-06-18 13:13:40 -07001066 private long initializeMaxItemId(SQLiteDatabase db) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001067 Cursor c = db.rawQuery("SELECT MAX(_id) FROM favorites", null);
1068
1069 // get the result
1070 final int maxIdIndex = 0;
1071 long id = -1;
1072 if (c != null && c.moveToNext()) {
1073 id = c.getLong(maxIdIndex);
1074 }
Michael Jurka5130e402011-10-13 04:55:35 -07001075 if (c != null) {
1076 c.close();
1077 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001078
1079 if (id == -1) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001080 throw new RuntimeException("Error: could not query max item id");
1081 }
1082
1083 return id;
1084 }
1085
1086 // Generates a new ID to use for an workspace screen in your database. This method
1087 // should be only called from the main UI thread. As an exception, we do call it when we
1088 // call the constructor from the worker thread; however, this doesn't extend until after the
1089 // constructor is called, and we only pass a reference to LauncherProvider to LauncherApp
1090 // after that point
1091 public long generateNewScreenId() {
1092 if (mMaxScreenId < 0) {
1093 throw new RuntimeException("Error: max screen id was not initialized");
1094 }
1095 mMaxScreenId += 1;
Winson Chunga90303b2013-11-15 13:05:06 -08001096 // Log to disk
1097 Launcher.addDumpLog(TAG, "11683562 - generateNewScreenId(): " + mMaxScreenId, true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001098 return mMaxScreenId;
1099 }
1100
1101 public void updateMaxScreenId(long maxScreenId) {
Winson Chunga90303b2013-11-15 13:05:06 -08001102 // Log to disk
1103 Launcher.addDumpLog(TAG, "11683562 - updateMaxScreenId(): " + maxScreenId, true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001104 mMaxScreenId = maxScreenId;
1105 }
1106
1107 private long initializeMaxScreenId(SQLiteDatabase db) {
1108 Cursor c = db.rawQuery("SELECT MAX(" + LauncherSettings.WorkspaceScreens._ID + ") FROM " + TABLE_WORKSPACE_SCREENS, null);
1109
1110 // get the result
1111 final int maxIdIndex = 0;
1112 long id = -1;
1113 if (c != null && c.moveToNext()) {
1114 id = c.getLong(maxIdIndex);
1115 }
1116 if (c != null) {
1117 c.close();
1118 }
1119
1120 if (id == -1) {
1121 throw new RuntimeException("Error: could not query max screen id");
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001122 }
1123
Winson Chunga90303b2013-11-15 13:05:06 -08001124 // Log to disk
1125 Launcher.addDumpLog(TAG, "11683562 - initializeMaxScreenId(): " + id, true);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001126 return id;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001127 }
1128
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001130 * Upgrade existing clock and photo frame widgets into their new widget
Bjorn Bringert93c45762009-12-16 13:19:47 +00001131 * equivalents.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132 */
1133 private void convertWidgets(SQLiteDatabase db) {
Bjorn Bringert34251342009-12-15 13:33:11 +00001134 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001135 final int[] bindSources = new int[] {
1136 Favorites.ITEM_TYPE_WIDGET_CLOCK,
1137 Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME,
Bjorn Bringert7984c942009-12-09 15:38:25 +00001138 Favorites.ITEM_TYPE_WIDGET_SEARCH,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001139 };
Bjorn Bringert7984c942009-12-09 15:38:25 +00001140
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE, bindSources);
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001142
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143 Cursor c = null;
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001144
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001145 db.beginTransaction();
1146 try {
1147 // Select and iterate through each matching widget
Bjorn Bringert7984c942009-12-09 15:38:25 +00001148 c = db.query(TABLE_FAVORITES, new String[] { Favorites._ID, Favorites.ITEM_TYPE },
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001149 selectWhere, null, null, null, null);
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001150
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001151 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001152
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001153 final ContentValues values = new ContentValues();
1154 while (c != null && c.moveToNext()) {
1155 long favoriteId = c.getLong(0);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001156 int favoriteType = c.getInt(1);
1157
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001158 // Allocate and update database with new appWidgetId
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001159 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001160 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001161
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001162 if (LOGD) {
1163 Log.d(TAG, "allocated appWidgetId=" + appWidgetId
1164 + " for favoriteId=" + favoriteId);
1165 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001166 values.clear();
Bjorn Bringert7984c942009-12-09 15:38:25 +00001167 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
1168 values.put(Favorites.APPWIDGET_ID, appWidgetId);
1169
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 // Original widgets might not have valid spans when upgrading
Bjorn Bringert7984c942009-12-09 15:38:25 +00001171 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
1172 values.put(LauncherSettings.Favorites.SPANX, 4);
1173 values.put(LauncherSettings.Favorites.SPANY, 1);
1174 } else {
1175 values.put(LauncherSettings.Favorites.SPANX, 2);
1176 values.put(LauncherSettings.Favorites.SPANY, 2);
1177 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178
1179 String updateWhere = Favorites._ID + "=" + favoriteId;
1180 db.update(TABLE_FAVORITES, values, updateWhere, null);
Bjorn Bringert34251342009-12-15 13:33:11 +00001181
Bjorn Bringert34251342009-12-15 13:33:11 +00001182 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_CLOCK) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001183 // TODO: check return value
1184 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
Bjorn Bringert34251342009-12-15 13:33:11 +00001185 new ComponentName("com.android.alarmclock",
1186 "com.android.alarmclock.AnalogAppWidgetProvider"));
1187 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001188 // TODO: check return value
1189 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
Bjorn Bringert34251342009-12-15 13:33:11 +00001190 new ComponentName("com.android.camera",
1191 "com.android.camera.PhotoAppWidgetProvider"));
1192 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001193 // TODO: check return value
1194 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
Bjorn Bringertcd8fec02010-01-14 13:26:43 +00001195 getSearchWidgetProvider());
Bjorn Bringert34251342009-12-15 13:33:11 +00001196 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001198 Log.e(TAG, "Problem allocating appWidgetId", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199 }
1200 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001201
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202 db.setTransactionSuccessful();
1203 } catch (SQLException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001204 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 } finally {
1206 db.endTransaction();
1207 if (c != null) {
1208 c.close();
1209 }
1210 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001211
1212 // Update max item id
1213 mMaxItemId = initializeMaxItemId(db);
1214 if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215 }
1216
Adam Cohena043fa82014-07-23 14:49:38 -07001217 private boolean initializeExternalAdd(ContentValues values) {
1218 // 1. Ensure that externally added items have a valid item id
1219 long id = generateNewItemId();
1220 values.put(LauncherSettings.Favorites._ID, id);
1221
1222 // 2. In the case of an app widget, and if no app widget id is specified, we
1223 // attempt allocate and bind the widget.
1224 Integer itemType = values.getAsInteger(LauncherSettings.Favorites.ITEM_TYPE);
1225 if (itemType != null &&
1226 itemType.intValue() == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET &&
1227 !values.containsKey(LauncherSettings.Favorites.APPWIDGET_ID)) {
1228
1229 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
1230 ComponentName cn = ComponentName.unflattenFromString(
1231 values.getAsString(Favorites.APPWIDGET_PROVIDER));
1232
1233 if (cn != null) {
1234 try {
1235 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
Adam Cohen3ed316a2014-07-23 18:21:20 -07001236 values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId);
Adam Coheneb1ac422014-10-14 08:55:28 -07001237 if (!appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,cn)) {
Adam Cohend61a6382014-10-14 15:04:34 -07001238 return false;
Adam Cohena043fa82014-07-23 14:49:38 -07001239 }
1240 } catch (RuntimeException e) {
1241 Log.e(TAG, "Failed to initialize external widget", e);
Adam Coheneb1ac422014-10-14 08:55:28 -07001242 return false;
Adam Cohena043fa82014-07-23 14:49:38 -07001243 }
Adam Coheneb1ac422014-10-14 08:55:28 -07001244 } else {
1245 return false;
Adam Cohena043fa82014-07-23 14:49:38 -07001246 }
1247 }
Adam Cohen7ec3bbf2014-07-31 00:09:45 -07001248
1249 // Add screen id if not present
1250 long screenId = values.getAsLong(LauncherSettings.Favorites.SCREEN);
1251 if (!addScreenIdIfNecessary(screenId)) {
1252 return false;
1253 }
Adam Cohena043fa82014-07-23 14:49:38 -07001254 return true;
1255 }
1256
Adam Cohen7ec3bbf2014-07-31 00:09:45 -07001257 // Returns true of screen id exists, or if successfully added
1258 private boolean addScreenIdIfNecessary(long screenId) {
1259 if (!hasScreenId(screenId)) {
1260 int rank = getMaxScreenRank() + 1;
1261
1262 ContentValues v = new ContentValues();
1263 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1264 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, rank);
1265 if (dbInsertAndCheck(this, getWritableDatabase(),
1266 TABLE_WORKSPACE_SCREENS, null, v) < 0) {
1267 return false;
1268 }
1269 }
1270 return true;
1271 }
1272
1273 private boolean hasScreenId(long screenId) {
1274 SQLiteDatabase db = getWritableDatabase();
1275 Cursor c = db.rawQuery("SELECT * FROM " + TABLE_WORKSPACE_SCREENS + " WHERE "
1276 + LauncherSettings.WorkspaceScreens._ID + " = " + screenId, null);
1277 if (c != null) {
1278 int count = c.getCount();
1279 c.close();
1280 return count > 0;
1281 } else {
1282 return false;
1283 }
1284 }
1285
1286 private int getMaxScreenRank() {
1287 SQLiteDatabase db = getWritableDatabase();
1288 Cursor c = db.rawQuery("SELECT MAX(" + LauncherSettings.WorkspaceScreens.SCREEN_RANK
1289 + ") FROM " + TABLE_WORKSPACE_SCREENS, null);
1290
1291 // get the result
1292 final int maxRankIndex = 0;
1293 int rank = -1;
1294 if (c != null && c.moveToNext()) {
1295 rank = c.getInt(maxRankIndex);
1296 }
1297 if (c != null) {
1298 c.close();
1299 }
1300
1301 return rank;
1302 }
1303
Sunny Goyal3a5a9d12014-10-01 15:33:41 -07001304 private int loadFavorites(SQLiteDatabase db, AutoInstallsLayout loader) {
Adam Cohen71483f42014-05-15 14:04:01 -07001305 ArrayList<Long> screenIds = new ArrayList<Long>();
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001306 // TODO: Use multiple loaders with fall-back and transaction.
1307 int count = loader.loadLayout(db, screenIds);
Adam Cohen71483f42014-05-15 14:04:01 -07001308
1309 // Add the screens specified by the items above
1310 Collections.sort(screenIds);
1311 int rank = 0;
1312 ContentValues values = new ContentValues();
1313 for (Long id : screenIds) {
1314 values.clear();
1315 values.put(LauncherSettings.WorkspaceScreens._ID, id);
1316 values.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, rank);
1317 if (dbInsertAndCheck(this, db, TABLE_WORKSPACE_SCREENS, null, values) < 0) {
1318 throw new RuntimeException("Failed initialize screen table"
1319 + "from default layout");
1320 }
1321 rank++;
1322 }
1323
1324 // Ensure that the max ids are initialized
1325 mMaxItemId = initializeMaxItemId(db);
1326 mMaxScreenId = initializeMaxScreenId(db);
Adam Cohen7ec3bbf2014-07-31 00:09:45 -07001327
Adam Cohen71483f42014-05-15 14:04:01 -07001328 return count;
1329 }
1330
Bjorn Bringertcd8fec02010-01-14 13:26:43 +00001331 private ComponentName getSearchWidgetProvider() {
1332 SearchManager searchManager =
1333 (SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE);
1334 ComponentName searchComponent = searchManager.getGlobalSearchActivity();
1335 if (searchComponent == null) return null;
1336 return getProviderInPackage(searchComponent.getPackageName());
1337 }
1338
1339 /**
1340 * Gets an appwidget provider from the given package. If the package contains more than
1341 * one appwidget provider, an arbitrary one is returned.
1342 */
1343 private ComponentName getProviderInPackage(String packageName) {
1344 AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
1345 List<AppWidgetProviderInfo> providers = appWidgetManager.getInstalledProviders();
1346 if (providers == null) return null;
1347 final int providerCount = providers.size();
1348 for (int i = 0; i < providerCount; i++) {
1349 ComponentName provider = providers.get(i).provider;
1350 if (provider != null && provider.getPackageName().equals(packageName)) {
1351 return provider;
1352 }
1353 }
1354 return null;
1355 }
1356
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001357 private void migrateLauncher2Shortcuts(SQLiteDatabase db, Uri uri) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001358 final ContentResolver resolver = mContext.getContentResolver();
1359 Cursor c = null;
1360 int count = 0;
1361 int curScreen = 0;
1362
1363 try {
1364 c = resolver.query(uri, null, null, null, "title ASC");
1365 } catch (Exception e) {
1366 // Ignore
1367 }
1368
Dan Sandlerd5024042014-01-09 15:01:33 -05001369 // We already have a favorites database in the old provider
1370 if (c != null) {
1371 try {
1372 if (c.getCount() > 0) {
1373 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1374 final int intentIndex
1375 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
1376 final int titleIndex
1377 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
1378 final int iconTypeIndex
1379 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
1380 final int iconIndex
1381 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1382 final int iconPackageIndex
1383 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
1384 final int iconResourceIndex
1385 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
1386 final int containerIndex
1387 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
1388 final int itemTypeIndex
1389 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
1390 final int screenIndex
1391 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
1392 final int cellXIndex
1393 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
1394 final int cellYIndex
1395 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
1396 final int uriIndex
1397 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1398 final int displayModeIndex
1399 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
Kenny Guyed131872014-04-30 03:02:21 +01001400 final int profileIndex
1401 = c.getColumnIndex(LauncherSettings.Favorites.PROFILE_ID);
Dan Sandlerd5024042014-01-09 15:01:33 -05001402
1403 int i = 0;
1404 int curX = 0;
1405 int curY = 0;
1406
1407 final LauncherAppState app = LauncherAppState.getInstance();
1408 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1409 final int width = (int) grid.numColumns;
1410 final int height = (int) grid.numRows;
1411 final int hotseatWidth = (int) grid.numHotseatIcons;
1412
1413 final HashSet<String> seenIntents = new HashSet<String>(c.getCount());
1414
Adam Cohen72960972014-01-15 18:13:55 -08001415 final ArrayList<ContentValues> shortcuts = new ArrayList<ContentValues>();
1416 final ArrayList<ContentValues> folders = new ArrayList<ContentValues>();
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001417 final SparseArray<ContentValues> hotseat = new SparseArray<ContentValues>();
Dan Sandlerd5024042014-01-09 15:01:33 -05001418
1419 while (c.moveToNext()) {
1420 final int itemType = c.getInt(itemTypeIndex);
1421 if (itemType != Favorites.ITEM_TYPE_APPLICATION
1422 && itemType != Favorites.ITEM_TYPE_SHORTCUT
1423 && itemType != Favorites.ITEM_TYPE_FOLDER) {
1424 continue;
1425 }
1426
1427 final int cellX = c.getInt(cellXIndex);
1428 final int cellY = c.getInt(cellYIndex);
1429 final int screen = c.getInt(screenIndex);
1430 int container = c.getInt(containerIndex);
1431 final String intentStr = c.getString(intentIndex);
Kenny Guyed131872014-04-30 03:02:21 +01001432
1433 UserManagerCompat userManager = UserManagerCompat.getInstance(mContext);
1434 UserHandleCompat userHandle;
1435 final long userSerialNumber;
1436 if (profileIndex != -1 && !c.isNull(profileIndex)) {
1437 userSerialNumber = c.getInt(profileIndex);
1438 userHandle = userManager.getUserForSerialNumber(userSerialNumber);
1439 } else {
1440 // Default to the serial number of this user, for older
1441 // shortcuts.
1442 userHandle = UserHandleCompat.myUserHandle();
1443 userSerialNumber = userManager.getSerialNumberForUser(userHandle);
1444 }
Dan Sandlerd5024042014-01-09 15:01:33 -05001445 Launcher.addDumpLog(TAG, "migrating \""
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001446 + c.getString(titleIndex) + "\" ("
1447 + cellX + "," + cellY + "@"
1448 + LauncherSettings.Favorites.containerToString(container)
1449 + "/" + screen
1450 + "): " + intentStr, true);
Dan Sandlerd5024042014-01-09 15:01:33 -05001451
1452 if (itemType != Favorites.ITEM_TYPE_FOLDER) {
Adam Cohen556f6132014-01-15 15:18:08 -08001453
1454 final Intent intent;
1455 final ComponentName cn;
1456 try {
1457 intent = Intent.parseUri(intentStr, 0);
1458 } catch (URISyntaxException e) {
1459 // bogus intent?
1460 Launcher.addDumpLog(TAG,
1461 "skipping invalid intent uri", true);
1462 continue;
1463 }
1464
1465 cn = intent.getComponent();
Dan Sandlerd5024042014-01-09 15:01:33 -05001466 if (TextUtils.isEmpty(intentStr)) {
1467 // no intent? no icon
1468 Launcher.addDumpLog(TAG, "skipping empty intent", true);
1469 continue;
Adam Cohen72960972014-01-15 18:13:55 -08001470 } else if (cn != null &&
Kenny Guyed131872014-04-30 03:02:21 +01001471 !LauncherModel.isValidPackageActivity(mContext, cn,
1472 userHandle)) {
Adam Cohen556f6132014-01-15 15:18:08 -08001473 // component no longer exists.
Adam Cohen72960972014-01-15 18:13:55 -08001474 Launcher.addDumpLog(TAG, "skipping item whose component " +
Adam Cohen556f6132014-01-15 15:18:08 -08001475 "no longer exists.", true);
1476 continue;
Adam Cohen72960972014-01-15 18:13:55 -08001477 } else if (container ==
1478 LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1479 // Dedupe icons directly on the workspace
1480
Adam Cohen556f6132014-01-15 15:18:08 -08001481 // Canonicalize
1482 // the Play Store sets the package parameter, but Launcher
Adam Cohena33f11e2014-10-24 12:20:20 -07001483 // does not, so we clear that out to keep them the same.
1484 // Also ignore intent flags for the purposes of deduping.
Adam Cohen556f6132014-01-15 15:18:08 -08001485 intent.setPackage(null);
Adam Cohena33f11e2014-10-24 12:20:20 -07001486 int flags = intent.getFlags();
1487 intent.setFlags(0);
Adam Cohen556f6132014-01-15 15:18:08 -08001488 final String key = intent.toUri(0);
Adam Cohena33f11e2014-10-24 12:20:20 -07001489 intent.setFlags(flags);
Adam Cohen556f6132014-01-15 15:18:08 -08001490 if (seenIntents.contains(key)) {
1491 Launcher.addDumpLog(TAG, "skipping duplicate", true);
Dan Sandlerd5024042014-01-09 15:01:33 -05001492 continue;
Adam Cohen556f6132014-01-15 15:18:08 -08001493 } else {
1494 seenIntents.add(key);
Dan Sandlerd5024042014-01-09 15:01:33 -05001495 }
1496 }
1497 }
1498
1499 ContentValues values = new ContentValues(c.getColumnCount());
1500 values.put(LauncherSettings.Favorites._ID, c.getInt(idIndex));
1501 values.put(LauncherSettings.Favorites.INTENT, intentStr);
1502 values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex));
1503 values.put(LauncherSettings.Favorites.ICON_TYPE,
1504 c.getInt(iconTypeIndex));
1505 values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex));
1506 values.put(LauncherSettings.Favorites.ICON_PACKAGE,
1507 c.getString(iconPackageIndex));
1508 values.put(LauncherSettings.Favorites.ICON_RESOURCE,
1509 c.getString(iconResourceIndex));
1510 values.put(LauncherSettings.Favorites.ITEM_TYPE, itemType);
1511 values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1);
1512 values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex));
1513 values.put(LauncherSettings.Favorites.DISPLAY_MODE,
1514 c.getInt(displayModeIndex));
Kenny Guyed131872014-04-30 03:02:21 +01001515 values.put(LauncherSettings.Favorites.PROFILE_ID, userSerialNumber);
Dan Sandlerd5024042014-01-09 15:01:33 -05001516
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001517 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1518 hotseat.put(screen, values);
Dan Sandlerd5024042014-01-09 15:01:33 -05001519 }
1520
1521 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1522 // In a folder or in the hotseat, preserve position
1523 values.put(LauncherSettings.Favorites.SCREEN, screen);
1524 values.put(LauncherSettings.Favorites.CELLX, cellX);
1525 values.put(LauncherSettings.Favorites.CELLY, cellY);
1526 } else {
Adam Cohen72960972014-01-15 18:13:55 -08001527 // For items contained directly on one of the workspace screen,
1528 // we'll determine their location (screen, x, y) in a second pass.
Dan Sandlerd5024042014-01-09 15:01:33 -05001529 }
1530
1531 values.put(LauncherSettings.Favorites.CONTAINER, container);
1532
Adam Cohen72960972014-01-15 18:13:55 -08001533 if (itemType != Favorites.ITEM_TYPE_FOLDER) {
1534 shortcuts.add(values);
1535 } else {
1536 folders.add(values);
1537 }
Dan Sandlerd5024042014-01-09 15:01:33 -05001538 }
1539
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001540 // Now that we have all the hotseat icons, let's go through them left-right
1541 // and assign valid locations for them in the new hotseat
1542 final int N = hotseat.size();
1543 for (int idx=0; idx<N; idx++) {
1544 int hotseatX = hotseat.keyAt(idx);
1545 ContentValues values = hotseat.valueAt(idx);
1546
1547 if (hotseatX == grid.hotseatAllAppsRank) {
1548 // let's drop this in the next available hole in the hotseat
1549 while (++hotseatX < hotseatWidth) {
1550 if (hotseat.get(hotseatX) == null) {
1551 // found a spot! move it here
1552 values.put(LauncherSettings.Favorites.SCREEN,
1553 hotseatX);
1554 break;
1555 }
1556 }
1557 }
1558 if (hotseatX >= hotseatWidth) {
1559 // no room for you in the hotseat? it's off to the desktop with you
1560 values.put(LauncherSettings.Favorites.CONTAINER,
1561 Favorites.CONTAINER_DESKTOP);
1562 }
1563 }
1564
Adam Cohen72960972014-01-15 18:13:55 -08001565 final ArrayList<ContentValues> allItems = new ArrayList<ContentValues>();
1566 // Folders first
1567 allItems.addAll(folders);
1568 // Then shortcuts
1569 allItems.addAll(shortcuts);
1570
1571 // Layout all the folders
1572 for (ContentValues values: allItems) {
1573 if (values.getAsInteger(LauncherSettings.Favorites.CONTAINER) !=
1574 LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1575 // Hotseat items and folder items have already had their
1576 // location information set. Nothing to be done here.
1577 continue;
1578 }
1579 values.put(LauncherSettings.Favorites.SCREEN, curScreen);
1580 values.put(LauncherSettings.Favorites.CELLX, curX);
1581 values.put(LauncherSettings.Favorites.CELLY, curY);
1582 curX = (curX + 1) % width;
1583 if (curX == 0) {
1584 curY = (curY + 1);
1585 }
1586 // Leave the last row of icons blank on every screen
1587 if (curY == height - 1) {
1588 curScreen = (int) generateNewScreenId();
1589 curY = 0;
1590 }
1591 }
1592
1593 if (allItems.size() > 0) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001594 db.beginTransaction();
1595 try {
Adam Cohen72960972014-01-15 18:13:55 -08001596 for (ContentValues row: allItems) {
1597 if (row == null) continue;
1598 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, row)
Dan Sandlerd5024042014-01-09 15:01:33 -05001599 < 0) {
1600 return;
1601 } else {
1602 count++;
1603 }
1604 }
1605 db.setTransactionSuccessful();
1606 } finally {
1607 db.endTransaction();
1608 }
1609 }
1610
1611 db.beginTransaction();
1612 try {
1613 for (i=0; i<=curScreen; i++) {
1614 final ContentValues values = new ContentValues();
1615 values.put(LauncherSettings.WorkspaceScreens._ID, i);
1616 values.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
1617 if (dbInsertAndCheck(this, db, TABLE_WORKSPACE_SCREENS, null, values)
1618 < 0) {
1619 return;
1620 }
1621 }
1622 db.setTransactionSuccessful();
1623 } finally {
1624 db.endTransaction();
1625 }
1626 }
1627 } finally {
1628 c.close();
1629 }
1630 }
1631
1632 Launcher.addDumpLog(TAG, "migrated " + count + " icons from Launcher2 into "
1633 + (curScreen+1) + " screens", true);
1634
1635 // ensure that new screens are created to hold these icons
1636 setFlagJustLoadedOldDb();
1637
1638 // Update max IDs; very important since we just grabbed IDs from another database
1639 mMaxItemId = initializeMaxItemId(db);
1640 mMaxScreenId = initializeMaxScreenId(db);
1641 if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId + " mMaxScreenId: " + mMaxScreenId);
1642 }
Mike Cleronb87bd162009-10-30 16:36:56 -07001643 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001644
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001645 /**
1646 * Build a query string that will match any row where the column matches
1647 * anything in the values list.
1648 */
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001649 private static String buildOrWhereString(String column, int[] values) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001650 StringBuilder selectWhere = new StringBuilder();
1651 for (int i = values.length - 1; i >= 0; i--) {
1652 selectWhere.append(column).append("=").append(values[i]);
1653 if (i > 0) {
1654 selectWhere.append(" OR ");
1655 }
1656 }
1657 return selectWhere.toString();
1658 }
1659
1660 static class SqlArguments {
1661 public final String table;
1662 public final String where;
1663 public final String[] args;
1664
1665 SqlArguments(Uri url, String where, String[] args) {
1666 if (url.getPathSegments().size() == 1) {
1667 this.table = url.getPathSegments().get(0);
1668 this.where = where;
1669 this.args = args;
1670 } else if (url.getPathSegments().size() != 2) {
1671 throw new IllegalArgumentException("Invalid URI: " + url);
1672 } else if (!TextUtils.isEmpty(where)) {
1673 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
1674 } else {
1675 this.table = url.getPathSegments().get(0);
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001676 this.where = "_id=" + ContentUris.parseId(url);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001677 this.args = null;
1678 }
1679 }
1680
1681 SqlArguments(Uri url) {
1682 if (url.getPathSegments().size() == 1) {
1683 table = url.getPathSegments().get(0);
1684 where = null;
1685 args = null;
1686 } else {
1687 throw new IllegalArgumentException("Invalid URI: " + url);
1688 }
1689 }
1690 }
Adam Cohen72960972014-01-15 18:13:55 -08001691}