blob: fe9bc171f8ade482a6b8e43375cb43b9d489fc3a [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
Sunny Goyal9d219682014-10-23 14:21:02 -0700325 final boolean usingExternallyProvidedLayout = loader != null;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700326 if (loader == null) {
Sunny Goyal9d219682014-10-23 14:21:02 -0700327 loader = getDefaultLayoutParser();
Brian Muramatsu5524b492012-10-02 16:55:54 -0700328 }
Michael Jurkab85f8a42012-04-25 15:48:32 -0700329 // Populate favorites table with initial favorites
Sunny Goyal9d219682014-10-23 14:21:02 -0700330 if ((mOpenHelper.loadFavorites(mOpenHelper.getWritableDatabase(), loader) <= 0)
331 && usingExternallyProvidedLayout) {
332 // Unable to load external layout. Cleanup and load the internal layout.
333 createEmptyDB();
334 mOpenHelper.loadFavorites(mOpenHelper.getWritableDatabase(),
335 getDefaultLayoutParser());
336 }
337 sp.edit().remove(EMPTY_DATABASE_CREATED).commit();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700338 }
339 }
340
Sunny Goyal9d219682014-10-23 14:21:02 -0700341 private DefaultLayoutParser getDefaultLayoutParser() {
342 int defaultLayout = LauncherAppState.getInstance()
343 .getDynamicGrid().getDeviceProfile().defaultLayoutId;
344 return new DefaultLayoutParser(getContext(), mOpenHelper.mAppWidgetHost,
345 mOpenHelper, getContext().getResources(), defaultLayout);
346 }
347
Dan Sandlerd5024042014-01-09 15:01:33 -0500348 public void migrateLauncher2Shortcuts() {
349 mOpenHelper.migrateLauncher2Shortcuts(mOpenHelper.getWritableDatabase(),
Jason Monk0bfcceb2014-03-21 15:42:06 -0400350 Uri.parse(getContext().getString(R.string.old_launcher_provider_uri)));
Dan Sandlerd5024042014-01-09 15:01:33 -0500351 }
352
Winson Chungc763c4e2013-07-19 13:49:06 -0700353 private static interface ContentValuesCallback {
354 public void onRow(ContentValues values);
355 }
356
Adam Cohen6dbe0492013-12-02 17:00:14 -0800357 private static boolean shouldImportLauncher2Database(Context context) {
358 boolean isTablet = context.getResources().getBoolean(R.bool.is_tablet);
359
360 // We don't import the old databse for tablets, as the grid size has changed.
361 return !isTablet && IMPORT_LAUNCHER2_DATABASE;
362 }
363
Dan Sandlerd5024042014-01-09 15:01:33 -0500364 public void deleteDatabase() {
365 // Are you sure? (y/n)
366 final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
Dan Sandler2b471742014-01-21 14:14:41 -0500367 final File dbFile = new File(db.getPath());
Dan Sandlerd5024042014-01-09 15:01:33 -0500368 mOpenHelper.close();
Dan Sandler2b471742014-01-21 14:14:41 -0500369 if (dbFile.exists()) {
370 SQLiteDatabase.deleteDatabase(dbFile);
371 }
Dan Sandlerd5024042014-01-09 15:01:33 -0500372 mOpenHelper = new DatabaseHelper(getContext());
373 }
374
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700375 private static class DatabaseHelper extends SQLiteOpenHelper implements LayoutParserCallback {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376 private final Context mContext;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700377 private final AppWidgetHost mAppWidgetHost;
Adam Cohendcd297f2013-06-18 13:13:40 -0700378 private long mMaxItemId = -1;
379 private long mMaxScreenId = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380
Winson Chung0b560dd2014-01-21 13:00:26 -0800381 private boolean mNewDbCreated = false;
382
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 DatabaseHelper(Context context) {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100384 super(context, LauncherFiles.LAUNCHER_DB, null, DATABASE_VERSION);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 mContext = context;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700386 mAppWidgetHost = new AppWidgetHost(context, Launcher.APPWIDGET_HOST_ID);
Winson Chung3d503fb2011-07-13 17:25:49 -0700387
388 // In the case where neither onCreate nor onUpgrade gets called, we read the maxId from
389 // the DB here
Adam Cohendcd297f2013-06-18 13:13:40 -0700390 if (mMaxItemId == -1) {
391 mMaxItemId = initializeMaxItemId(getWritableDatabase());
392 }
393 if (mMaxScreenId == -1) {
394 mMaxScreenId = initializeMaxScreenId(getWritableDatabase());
Winson Chung3d503fb2011-07-13 17:25:49 -0700395 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396 }
397
Winson Chung0b560dd2014-01-21 13:00:26 -0800398 public boolean wasNewDbCreated() {
399 return mNewDbCreated;
400 }
401
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700402 /**
403 * Send notification that we've deleted the {@link AppWidgetHost},
404 * probably as part of the initial database creation. The receiver may
405 * want to re-call {@link AppWidgetHost#startListening()} to ensure
406 * callbacks are correctly set.
407 */
408 private void sendAppWidgetResetNotify() {
409 final ContentResolver resolver = mContext.getContentResolver();
410 resolver.notifyChange(CONTENT_APPWIDGET_RESET_URI, null);
411 }
412
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800413 @Override
414 public void onCreate(SQLiteDatabase db) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800415 if (LOGD) Log.d(TAG, "creating new launcher database");
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700416
Adam Cohendcd297f2013-06-18 13:13:40 -0700417 mMaxItemId = 1;
418 mMaxScreenId = 0;
Winson Chung0b560dd2014-01-21 13:00:26 -0800419 mNewDbCreated = true;
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700420
Kenny Guyed131872014-04-30 03:02:21 +0100421 UserManagerCompat userManager = UserManagerCompat.getInstance(mContext);
422 long userSerialNumber = userManager.getSerialNumberForUser(
423 UserHandleCompat.myUserHandle());
424
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800425 db.execSQL("CREATE TABLE favorites (" +
426 "_id INTEGER PRIMARY KEY," +
427 "title TEXT," +
428 "intent TEXT," +
429 "container INTEGER," +
430 "screen INTEGER," +
431 "cellX INTEGER," +
432 "cellY INTEGER," +
433 "spanX INTEGER," +
434 "spanY INTEGER," +
435 "itemType INTEGER," +
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700436 "appWidgetId INTEGER NOT NULL DEFAULT -1," +
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 "isShortcut INTEGER," +
438 "iconType INTEGER," +
439 "iconPackage TEXT," +
440 "iconResource TEXT," +
441 "icon BLOB," +
442 "uri TEXT," +
Chris Wrend5e66bf2013-09-16 14:02:29 -0400443 "displayMode INTEGER," +
Chris Wren1ada10d2013-09-13 18:01:38 -0400444 "appWidgetProvider TEXT," +
Chris Wrenf4d08112014-01-16 18:13:56 -0500445 "modified INTEGER NOT NULL DEFAULT 0," +
Kenny Guyed131872014-04-30 03:02:21 +0100446 "restored INTEGER NOT NULL DEFAULT 0," +
447 "profileId INTEGER DEFAULT " + userSerialNumber +
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448 ");");
Adam Cohendcd297f2013-06-18 13:13:40 -0700449 addWorkspacesTable(db);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700451 // Database was just created, so wipe any previous widgets
452 if (mAppWidgetHost != null) {
453 mAppWidgetHost.deleteHost();
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700454 sendAppWidgetResetNotify();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700456
Adam Cohen6dbe0492013-12-02 17:00:14 -0800457 if (shouldImportLauncher2Database(mContext)) {
Dan Sandlerf0b8dac2013-11-19 12:21:25 -0500458 // Try converting the old database
459 ContentValuesCallback permuteScreensCb = new ContentValuesCallback() {
460 public void onRow(ContentValues values) {
461 int container = values.getAsInteger(LauncherSettings.Favorites.CONTAINER);
462 if (container == Favorites.CONTAINER_DESKTOP) {
463 int screen = values.getAsInteger(LauncherSettings.Favorites.SCREEN);
464 screen = (int) upgradeLauncherDb_permuteScreens(screen);
465 values.put(LauncherSettings.Favorites.SCREEN, screen);
466 }
467 }
468 };
469 Uri uri = Uri.parse("content://" + Settings.AUTHORITY +
470 "/old_favorites?notify=true");
471 if (!convertDatabase(db, uri, permuteScreensCb, true)) {
472 // Try and upgrade from the Launcher2 db
Jason Monk0bfcceb2014-03-21 15:42:06 -0400473 uri = Uri.parse(mContext.getString(R.string.old_launcher_provider_uri));
Dan Sandlerf0b8dac2013-11-19 12:21:25 -0500474 if (!convertDatabase(db, uri, permuteScreensCb, false)) {
475 // If we fail, then set a flag to load the default workspace
476 setFlagEmptyDbCreated();
477 return;
Winson Chungc763c4e2013-07-19 13:49:06 -0700478 }
479 }
Dan Sandlerf0b8dac2013-11-19 12:21:25 -0500480 // Right now, in non-default workspace cases, we want to run the final
481 // upgrade code (ie. to fix workspace screen indices -> ids, etc.), so
482 // set that flag too.
483 setFlagJustLoadedOldDb();
484 } else {
485 // Fresh and clean launcher DB.
486 mMaxItemId = initializeMaxItemId(db);
487 setFlagEmptyDbCreated();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800488 }
489 }
490
Adam Cohendcd297f2013-06-18 13:13:40 -0700491 private void addWorkspacesTable(SQLiteDatabase db) {
492 db.execSQL("CREATE TABLE " + TABLE_WORKSPACE_SCREENS + " (" +
493 LauncherSettings.WorkspaceScreens._ID + " INTEGER," +
Chris Wren1ada10d2013-09-13 18:01:38 -0400494 LauncherSettings.WorkspaceScreens.SCREEN_RANK + " INTEGER," +
495 LauncherSettings.ChangeLogColumns.MODIFIED + " INTEGER NOT NULL DEFAULT 0" +
Adam Cohendcd297f2013-06-18 13:13:40 -0700496 ");");
497 }
498
Adam Cohen119285e2014-04-02 16:59:08 -0700499 private void removeOrphanedItems(SQLiteDatabase db) {
Adam Cohenf9c14de2014-04-17 18:20:45 -0700500 // Delete items directly on the workspace who's screen id doesn't exist
501 // "DELETE FROM favorites WHERE screen NOT IN (SELECT _id FROM workspaceScreens)
502 // AND container = -100"
503 String removeOrphanedDesktopItems = "DELETE FROM " + TABLE_FAVORITES +
504 " WHERE " +
Adam Cohen119285e2014-04-02 16:59:08 -0700505 LauncherSettings.Favorites.SCREEN + " NOT IN (SELECT " +
Adam Cohenf9c14de2014-04-17 18:20:45 -0700506 LauncherSettings.WorkspaceScreens._ID + " FROM " + TABLE_WORKSPACE_SCREENS + ")" +
507 " AND " +
508 LauncherSettings.Favorites.CONTAINER + " = " +
509 LauncherSettings.Favorites.CONTAINER_DESKTOP;
510 db.execSQL(removeOrphanedDesktopItems);
511
512 // Delete items contained in folders which no longer exist (after above statement)
513 // "DELETE FROM favorites WHERE container <> -100 AND container <> -101 AND container
514 // NOT IN (SELECT _id FROM favorites WHERE itemType = 2)"
515 String removeOrphanedFolderItems = "DELETE FROM " + TABLE_FAVORITES +
516 " WHERE " +
517 LauncherSettings.Favorites.CONTAINER + " <> " +
518 LauncherSettings.Favorites.CONTAINER_DESKTOP +
519 " AND "
520 + LauncherSettings.Favorites.CONTAINER + " <> " +
521 LauncherSettings.Favorites.CONTAINER_HOTSEAT +
522 " AND "
523 + LauncherSettings.Favorites.CONTAINER + " NOT IN (SELECT " +
524 LauncherSettings.Favorites._ID + " FROM " + TABLE_FAVORITES +
525 " WHERE " + LauncherSettings.Favorites.ITEM_TYPE + " = " +
526 LauncherSettings.Favorites.ITEM_TYPE_FOLDER + ")";
527 db.execSQL(removeOrphanedFolderItems);
Adam Cohen119285e2014-04-02 16:59:08 -0700528 }
529
Winson Chungc763c4e2013-07-19 13:49:06 -0700530 private void setFlagJustLoadedOldDb() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400531 String spKey = LauncherAppState.getSharedPreferencesKey();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700532 SharedPreferences sp = mContext.getSharedPreferences(spKey, Context.MODE_PRIVATE);
533 SharedPreferences.Editor editor = sp.edit();
Winson Chungc763c4e2013-07-19 13:49:06 -0700534 editor.putBoolean(UPGRADED_FROM_OLD_DATABASE, true);
535 editor.putBoolean(EMPTY_DATABASE_CREATED, false);
Michael Jurkab85f8a42012-04-25 15:48:32 -0700536 editor.commit();
537 }
538
Winson Chungc763c4e2013-07-19 13:49:06 -0700539 private void setFlagEmptyDbCreated() {
540 String spKey = LauncherAppState.getSharedPreferencesKey();
541 SharedPreferences sp = mContext.getSharedPreferences(spKey, Context.MODE_PRIVATE);
542 SharedPreferences.Editor editor = sp.edit();
543 editor.putBoolean(EMPTY_DATABASE_CREATED, true);
544 editor.putBoolean(UPGRADED_FROM_OLD_DATABASE, false);
545 editor.commit();
546 }
547
548 // We rearrange the screens from the old launcher
549 // 12345 -> 34512
550 private long upgradeLauncherDb_permuteScreens(long screen) {
551 if (screen >= 2) {
552 return screen - 2;
553 } else {
554 return screen + 3;
555 }
556 }
557
558 private boolean convertDatabase(SQLiteDatabase db, Uri uri,
559 ContentValuesCallback cb, boolean deleteRows) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800560 if (LOGD) Log.d(TAG, "converting database from an older format, but not onUpgrade");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561 boolean converted = false;
562
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800563 final ContentResolver resolver = mContext.getContentResolver();
564 Cursor cursor = null;
565
566 try {
567 cursor = resolver.query(uri, null, null, null, null);
568 } catch (Exception e) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700569 // Ignore
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570 }
571
572 // We already have a favorites database in the old provider
Winson Chungc763c4e2013-07-19 13:49:06 -0700573 if (cursor != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800574 try {
Winson Chungc763c4e2013-07-19 13:49:06 -0700575 if (cursor.getCount() > 0) {
576 converted = copyFromCursor(db, cursor, cb) > 0;
577 if (converted && deleteRows) {
578 resolver.delete(uri, null, null);
579 }
580 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800581 } finally {
582 cursor.close();
583 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800584 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700585
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800586 if (converted) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700587 // Convert widgets from this import into widgets
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800588 if (LOGD) Log.d(TAG, "converted and now triggering widget upgrade");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800589 convertWidgets(db);
Winson Chungc763c4e2013-07-19 13:49:06 -0700590
591 // Update max item id
592 mMaxItemId = initializeMaxItemId(db);
593 if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800594 }
595
596 return converted;
597 }
598
Winson Chungc763c4e2013-07-19 13:49:06 -0700599 private int copyFromCursor(SQLiteDatabase db, Cursor c, ContentValuesCallback cb) {
Romain Guy73b979d2009-06-09 12:57:21 -0700600 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800601 final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
602 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
603 final int iconTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
604 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
605 final int iconPackageIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
606 final int iconResourceIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
607 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
608 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
609 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
610 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
611 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
612 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
613 final int displayModeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
614
615 ContentValues[] rows = new ContentValues[c.getCount()];
616 int i = 0;
617 while (c.moveToNext()) {
618 ContentValues values = new ContentValues(c.getColumnCount());
Romain Guy73b979d2009-06-09 12:57:21 -0700619 values.put(LauncherSettings.Favorites._ID, c.getLong(idIndex));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800620 values.put(LauncherSettings.Favorites.INTENT, c.getString(intentIndex));
621 values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex));
622 values.put(LauncherSettings.Favorites.ICON_TYPE, c.getInt(iconTypeIndex));
623 values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex));
624 values.put(LauncherSettings.Favorites.ICON_PACKAGE, c.getString(iconPackageIndex));
625 values.put(LauncherSettings.Favorites.ICON_RESOURCE, c.getString(iconResourceIndex));
626 values.put(LauncherSettings.Favorites.CONTAINER, c.getInt(containerIndex));
627 values.put(LauncherSettings.Favorites.ITEM_TYPE, c.getInt(itemTypeIndex));
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700628 values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800629 values.put(LauncherSettings.Favorites.SCREEN, c.getInt(screenIndex));
630 values.put(LauncherSettings.Favorites.CELLX, c.getInt(cellXIndex));
631 values.put(LauncherSettings.Favorites.CELLY, c.getInt(cellYIndex));
632 values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex));
633 values.put(LauncherSettings.Favorites.DISPLAY_MODE, c.getInt(displayModeIndex));
Winson Chungc763c4e2013-07-19 13:49:06 -0700634 if (cb != null) {
635 cb.onRow(values);
636 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 rows[i++] = values;
638 }
639
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 int total = 0;
Winson Chungc763c4e2013-07-19 13:49:06 -0700641 if (i > 0) {
642 db.beginTransaction();
643 try {
644 int numValues = rows.length;
645 for (i = 0; i < numValues; i++) {
646 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, rows[i]) < 0) {
647 return 0;
648 } else {
649 total++;
650 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651 }
Winson Chungc763c4e2013-07-19 13:49:06 -0700652 db.setTransactionSuccessful();
653 } finally {
654 db.endTransaction();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800655 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 }
657
658 return total;
659 }
660
661 @Override
662 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Winson Chungc763c4e2013-07-19 13:49:06 -0700663 if (LOGD) Log.d(TAG, "onUpgrade triggered: " + oldVersion);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700664
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 int version = oldVersion;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700666 if (version < 3) {
667 // upgrade 1,2 -> 3 added appWidgetId column
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 db.beginTransaction();
669 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700670 // Insert new column for holding appWidgetIds
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 db.execSQL("ALTER TABLE favorites " +
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700672 "ADD COLUMN appWidgetId INTEGER NOT NULL DEFAULT -1;");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 db.setTransactionSuccessful();
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700674 version = 3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 } catch (SQLException ex) {
676 // Old version remains, which means we wipe old data
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800677 Log.e(TAG, ex.getMessage(), ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800678 } finally {
679 db.endTransaction();
680 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700681
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682 // Convert existing widgets only if table upgrade was successful
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700683 if (version == 3) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 convertWidgets(db);
685 }
686 }
Romain Guy73b979d2009-06-09 12:57:21 -0700687
688 if (version < 4) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800689 version = 4;
Romain Guy73b979d2009-06-09 12:57:21 -0700690 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700691
Romain Guy509cd6a2010-03-23 15:10:56 -0700692 // Where's version 5?
693 // - Donut and sholes on 2.0 shipped with version 4 of launcher1.
Daniel Sandler325dc232013-06-05 22:57:57 -0400694 // - Passion shipped on 2.1 with version 6 of launcher3
Romain Guy509cd6a2010-03-23 15:10:56 -0700695 // - Sholes shipped on 2.1r1 (aka Mr. 3) with version 5 of launcher 1
696 // but version 5 on there was the updateContactsShortcuts change
697 // which was version 6 in launcher 2 (first shipped on passion 2.1r1).
698 // The updateContactsShortcuts change is idempotent, so running it twice
699 // is okay so we'll do that when upgrading the devices that shipped with it.
700 if (version < 6) {
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800701 // We went from 3 to 5 screens. Move everything 1 to the right
702 db.beginTransaction();
703 try {
704 db.execSQL("UPDATE favorites SET screen=(screen + 1);");
705 db.setTransactionSuccessful();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800706 } catch (SQLException ex) {
707 // Old version remains, which means we wipe old data
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800708 Log.e(TAG, ex.getMessage(), ex);
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800709 } finally {
710 db.endTransaction();
711 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700712
Romain Guy509cd6a2010-03-23 15:10:56 -0700713 // We added the fast track.
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800714 if (updateContactsShortcuts(db)) {
715 version = 6;
716 }
717 }
Bjorn Bringert7984c942009-12-09 15:38:25 +0000718
719 if (version < 7) {
720 // Version 7 gets rid of the special search widget.
721 convertWidgets(db);
722 version = 7;
723 }
724
Joe Onorato0589f0f2010-02-08 13:44:00 -0800725 if (version < 8) {
726 // Version 8 (froyo) has the icons all normalized. This should
727 // already be the case in practice, but we now rely on it and don't
728 // resample the images each time.
729 normalizeIcons(db);
730 version = 8;
731 }
732
Winson Chung3d503fb2011-07-13 17:25:49 -0700733 if (version < 9) {
734 // The max id is not yet set at this point (onUpgrade is triggered in the ctor
735 // 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 -0700736 if (mMaxItemId == -1) {
737 mMaxItemId = initializeMaxItemId(db);
Winson Chung3d503fb2011-07-13 17:25:49 -0700738 }
739
740 // Add default hotseat icons
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700741 loadFavorites(db, new DefaultLayoutParser(mContext, mAppWidgetHost, this,
742 mContext.getResources(), R.xml.update_workspace));
Winson Chung3d503fb2011-07-13 17:25:49 -0700743 version = 9;
744 }
745
Daniel Lehmannd02402c2012-05-14 18:30:53 -0700746 // We bumped the version three time during JB, once to update the launch flags, once to
747 // update the override for the default launch animation and once to set the mimetype
748 // to improve startup performance
749 if (version < 12) {
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700750 // Contact shortcuts need a different set of flags to be launched now
751 // The updateContactsShortcuts change is idempotent, so we can keep using it like
752 // back in the Donut days
753 updateContactsShortcuts(db);
Daniel Lehmannd02402c2012-05-14 18:30:53 -0700754 version = 12;
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700755 }
756
Adam Cohendcd297f2013-06-18 13:13:40 -0700757 if (version < 13) {
758 // With the new shrink-wrapped and re-orderable workspaces, it makes sense
759 // to persist workspace screens and their relative order.
760 mMaxScreenId = 0;
761
762 // This will never happen in the wild, but when we switch to using workspace
763 // screen ids, redo the import from old launcher.
Winson Chungc763c4e2013-07-19 13:49:06 -0700764 sJustLoadedFromOldDb = true;
Adam Cohendcd297f2013-06-18 13:13:40 -0700765
766 addWorkspacesTable(db);
767 version = 13;
768 }
769
Chris Wrend5e66bf2013-09-16 14:02:29 -0400770 if (version < 14) {
771 db.beginTransaction();
772 try {
773 // Insert new column for holding widget provider name
774 db.execSQL("ALTER TABLE favorites " +
775 "ADD COLUMN appWidgetProvider TEXT;");
776 db.setTransactionSuccessful();
777 version = 14;
778 } catch (SQLException ex) {
779 // Old version remains, which means we wipe old data
780 Log.e(TAG, ex.getMessage(), ex);
781 } finally {
782 db.endTransaction();
783 }
784 }
785
Chris Wren1ada10d2013-09-13 18:01:38 -0400786 if (version < 15) {
787 db.beginTransaction();
788 try {
789 // Insert new column for holding update timestamp
790 db.execSQL("ALTER TABLE favorites " +
791 "ADD COLUMN modified INTEGER NOT NULL DEFAULT 0;");
792 db.execSQL("ALTER TABLE workspaceScreens " +
793 "ADD COLUMN modified INTEGER NOT NULL DEFAULT 0;");
794 db.setTransactionSuccessful();
795 version = 15;
796 } catch (SQLException ex) {
797 // Old version remains, which means we wipe old data
798 Log.e(TAG, ex.getMessage(), ex);
799 } finally {
800 db.endTransaction();
801 }
802 }
803
Chris Wrenf4d08112014-01-16 18:13:56 -0500804
805 if (version < 16) {
806 db.beginTransaction();
807 try {
808 // Insert new column for holding restore status
809 db.execSQL("ALTER TABLE favorites " +
810 "ADD COLUMN restored INTEGER NOT NULL DEFAULT 0;");
811 db.setTransactionSuccessful();
812 version = 16;
813 } catch (SQLException ex) {
814 // Old version remains, which means we wipe old data
815 Log.e(TAG, ex.getMessage(), ex);
816 } finally {
817 db.endTransaction();
818 }
819 }
820
Adam Cohen71e03b92014-02-21 14:09:53 -0800821 if (version < 17) {
822 // We use the db version upgrade here to identify users who may not have seen
823 // clings yet (because they weren't available), but for whom the clings are now
824 // available (tablet users). Because one of the possible cling flows (migration)
825 // is very destructive (wipes out workspaces), we want to prevent this from showing
826 // until clear data. We do so by marking that the clings have been shown.
827 LauncherClings.synchonouslyMarkFirstRunClingDismissed(mContext);
828 version = 17;
829 }
830
Adam Cohen119285e2014-04-02 16:59:08 -0700831 if (version < 18) {
Adam Cohenf9c14de2014-04-17 18:20:45 -0700832 // No-op
833 version = 18;
834 }
835
836 if (version < 19) {
Adam Cohen119285e2014-04-02 16:59:08 -0700837 // Due to a data loss bug, some users may have items associated with screen ids
838 // which no longer exist. Since this can cause other problems, and since the user
839 // will never see these items anyway, we use database upgrade as an opportunity to
840 // clean things up.
Adam Cohenf9c14de2014-04-17 18:20:45 -0700841 removeOrphanedItems(db);
842 version = 19;
Adam Cohen119285e2014-04-02 16:59:08 -0700843 }
844
Kenny Guyed131872014-04-30 03:02:21 +0100845 if (version < 20) {
846 // Add userId column
847 if (addProfileColumn(db)) {
848 version = 20;
849 }
850 // else old version remains, which means we wipe old data
851 }
852
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853 if (version != DATABASE_VERSION) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800854 Log.w(TAG, "Destroying all old data.");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800855 db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES);
Adam Cohendcd297f2013-06-18 13:13:40 -0700856 db.execSQL("DROP TABLE IF EXISTS " + TABLE_WORKSPACE_SCREENS);
857
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 onCreate(db);
859 }
860 }
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800861
Adam Cohen9b1d0622014-05-21 19:01:57 -0700862 @Override
863 public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
864 // This shouldn't happen -- throw our hands up in the air and start over.
865 Log.w(TAG, "Database version downgrade from: " + oldVersion + " to " + newVersion +
866 ". Wiping databse.");
Sunny Goyal42de82f2014-09-26 22:09:29 -0700867 createEmptyDB(db);
868 }
Adam Cohen9b1d0622014-05-21 19:01:57 -0700869
Sunny Goyal42de82f2014-09-26 22:09:29 -0700870
871 /**
872 * Clears all the data for a fresh start.
873 */
874 public void createEmptyDB(SQLiteDatabase db) {
Adam Cohen9b1d0622014-05-21 19:01:57 -0700875 db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES);
876 db.execSQL("DROP TABLE IF EXISTS " + TABLE_WORKSPACE_SCREENS);
877 onCreate(db);
878 }
879
Kenny Guyed131872014-04-30 03:02:21 +0100880 private boolean addProfileColumn(SQLiteDatabase db) {
881 db.beginTransaction();
882 try {
883 UserManagerCompat userManager = UserManagerCompat.getInstance(mContext);
884 // Default to the serial number of this user, for older
885 // shortcuts.
886 long userSerialNumber = userManager.getSerialNumberForUser(
887 UserHandleCompat.myUserHandle());
888 // Insert new column for holding user serial number
889 db.execSQL("ALTER TABLE favorites " +
890 "ADD COLUMN profileId INTEGER DEFAULT "
891 + userSerialNumber + ";");
892 db.setTransactionSuccessful();
893 } catch (SQLException ex) {
894 // Old version remains, which means we wipe old data
895 Log.e(TAG, ex.getMessage(), ex);
896 return false;
897 } finally {
898 db.endTransaction();
899 }
900 return true;
901 }
902
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800903 private boolean updateContactsShortcuts(SQLiteDatabase db) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800904 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE,
905 new int[] { Favorites.ITEM_TYPE_SHORTCUT });
906
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700907 Cursor c = null;
908 final String actionQuickContact = "com.android.contacts.action.QUICK_CONTACT";
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800909 db.beginTransaction();
910 try {
911 // Select and iterate through each matching widget
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700912 c = db.query(TABLE_FAVORITES,
913 new String[] { Favorites._ID, Favorites.INTENT },
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800914 selectWhere, null, null, null, null);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700915 if (c == null) return false;
916
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800917 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700918
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800919 final int idIndex = c.getColumnIndex(Favorites._ID);
920 final int intentIndex = c.getColumnIndex(Favorites.INTENT);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700921
922 while (c.moveToNext()) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800923 long favoriteId = c.getLong(idIndex);
924 final String intentUri = c.getString(intentIndex);
925 if (intentUri != null) {
926 try {
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700927 final Intent intent = Intent.parseUri(intentUri, 0);
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800928 android.util.Log.d("Home", intent.toString());
929 final Uri uri = intent.getData();
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700930 if (uri != null) {
931 final String data = uri.toString();
932 if ((Intent.ACTION_VIEW.equals(intent.getAction()) ||
933 actionQuickContact.equals(intent.getAction())) &&
934 (data.startsWith("content://contacts/people/") ||
935 data.startsWith("content://com.android.contacts/" +
936 "contacts/lookup/"))) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800937
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700938 final Intent newIntent = new Intent(actionQuickContact);
939 // When starting from the launcher, start in a new, cleared task
940 // CLEAR_WHEN_TASK_RESET cannot reset the root of a task, so we
941 // clear the whole thing preemptively here since
942 // QuickContactActivity will finish itself when launching other
943 // detail activities.
944 newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
945 Intent.FLAG_ACTIVITY_CLEAR_TASK);
Winson Chung2672ff92012-05-04 16:22:30 -0700946 newIntent.putExtra(
947 Launcher.INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION, true);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700948 newIntent.setData(uri);
Daniel Lehmannd02402c2012-05-14 18:30:53 -0700949 // Determine the type and also put that in the shortcut
950 // (that can speed up launch a bit)
951 newIntent.setDataAndType(uri, newIntent.resolveType(mContext));
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800952
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700953 final ContentValues values = new ContentValues();
954 values.put(LauncherSettings.Favorites.INTENT,
955 newIntent.toUri(0));
956
957 String updateWhere = Favorites._ID + "=" + favoriteId;
958 db.update(TABLE_FAVORITES, values, updateWhere, null);
959 }
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800960 }
961 } catch (RuntimeException ex) {
962 Log.e(TAG, "Problem upgrading shortcut", ex);
963 } catch (URISyntaxException e) {
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700964 Log.e(TAG, "Problem upgrading shortcut", e);
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800965 }
966 }
967 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700968
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800969 db.setTransactionSuccessful();
970 } catch (SQLException ex) {
971 Log.w(TAG, "Problem while upgrading contacts", ex);
972 return false;
973 } finally {
974 db.endTransaction();
975 if (c != null) {
976 c.close();
977 }
978 }
979
980 return true;
981 }
982
Joe Onorato0589f0f2010-02-08 13:44:00 -0800983 private void normalizeIcons(SQLiteDatabase db) {
984 Log.d(TAG, "normalizing icons");
985
Joe Onorato346e1292010-02-18 10:34:24 -0500986 db.beginTransaction();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800987 Cursor c = null;
Joe Onorato9690b392010-03-23 17:34:37 -0400988 SQLiteStatement update = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800989 try {
990 boolean logged = false;
Joe Onorato9690b392010-03-23 17:34:37 -0400991 update = db.compileStatement("UPDATE favorites "
Jeff Hamiltoneaf77d62010-02-13 00:08:17 -0600992 + "SET icon=? WHERE _id=?");
Joe Onorato0589f0f2010-02-08 13:44:00 -0800993
994 c = db.rawQuery("SELECT _id, icon FROM favorites WHERE iconType=" +
995 Favorites.ICON_TYPE_BITMAP, null);
996
997 final int idIndex = c.getColumnIndexOrThrow(Favorites._ID);
998 final int iconIndex = c.getColumnIndexOrThrow(Favorites.ICON);
999
1000 while (c.moveToNext()) {
1001 long id = c.getLong(idIndex);
1002 byte[] data = c.getBlob(iconIndex);
1003 try {
Sunny Goyal2fce90c2014-10-07 12:01:58 -07001004 Bitmap bitmap = Utilities.createIconBitmap(
Joe Onorato0589f0f2010-02-08 13:44:00 -08001005 BitmapFactory.decodeByteArray(data, 0, data.length),
1006 mContext);
1007 if (bitmap != null) {
1008 update.bindLong(1, id);
1009 data = ItemInfo.flattenBitmap(bitmap);
1010 if (data != null) {
1011 update.bindBlob(2, data);
1012 update.execute();
1013 }
1014 bitmap.recycle();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001015 }
1016 } catch (Exception e) {
1017 if (!logged) {
1018 Log.e(TAG, "Failed normalizing icon " + id, e);
1019 } else {
1020 Log.e(TAG, "Also failed normalizing icon " + id);
1021 }
1022 logged = true;
1023 }
1024 }
Bjorn Bringert3a928e42010-02-19 11:15:40 +00001025 db.setTransactionSuccessful();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001026 } catch (SQLException ex) {
1027 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
1028 } finally {
1029 db.endTransaction();
Joe Onorato9690b392010-03-23 17:34:37 -04001030 if (update != null) {
1031 update.close();
1032 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001033 if (c != null) {
1034 c.close();
1035 }
1036 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001037 }
1038
1039 // Generates a new ID to use for an object in your database. This method should be only
1040 // called from the main UI thread. As an exception, we do call it when we call the
1041 // constructor from the worker thread; however, this doesn't extend until after the
1042 // constructor is called, and we only pass a reference to LauncherProvider to LauncherApp
1043 // after that point
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001044 @Override
Adam Cohendcd297f2013-06-18 13:13:40 -07001045 public long generateNewItemId() {
1046 if (mMaxItemId < 0) {
1047 throw new RuntimeException("Error: max item id was not initialized");
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001048 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001049 mMaxItemId += 1;
1050 return mMaxItemId;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001051 }
1052
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001053 @Override
1054 public long insertAndCheck(SQLiteDatabase db, ContentValues values) {
1055 return dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values);
1056 }
1057
Winson Chungc763c4e2013-07-19 13:49:06 -07001058 public void updateMaxItemId(long id) {
1059 mMaxItemId = id + 1;
1060 }
1061
Chris Wren5dee7af2013-12-20 17:22:11 -05001062 public void checkId(String table, ContentValues values) {
1063 long id = values.getAsLong(LauncherSettings.BaseLauncherColumns._ID);
1064 if (table == LauncherProvider.TABLE_WORKSPACE_SCREENS) {
1065 mMaxScreenId = Math.max(id, mMaxScreenId);
1066 } else {
1067 mMaxItemId = Math.max(id, mMaxItemId);
1068 }
1069 }
1070
Adam Cohendcd297f2013-06-18 13:13:40 -07001071 private long initializeMaxItemId(SQLiteDatabase db) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001072 Cursor c = db.rawQuery("SELECT MAX(_id) FROM favorites", null);
1073
1074 // get the result
1075 final int maxIdIndex = 0;
1076 long id = -1;
1077 if (c != null && c.moveToNext()) {
1078 id = c.getLong(maxIdIndex);
1079 }
Michael Jurka5130e402011-10-13 04:55:35 -07001080 if (c != null) {
1081 c.close();
1082 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001083
1084 if (id == -1) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001085 throw new RuntimeException("Error: could not query max item id");
1086 }
1087
1088 return id;
1089 }
1090
1091 // Generates a new ID to use for an workspace screen in your database. This method
1092 // should be only called from the main UI thread. As an exception, we do call it when we
1093 // call the constructor from the worker thread; however, this doesn't extend until after the
1094 // constructor is called, and we only pass a reference to LauncherProvider to LauncherApp
1095 // after that point
1096 public long generateNewScreenId() {
1097 if (mMaxScreenId < 0) {
1098 throw new RuntimeException("Error: max screen id was not initialized");
1099 }
1100 mMaxScreenId += 1;
Winson Chunga90303b2013-11-15 13:05:06 -08001101 // Log to disk
1102 Launcher.addDumpLog(TAG, "11683562 - generateNewScreenId(): " + mMaxScreenId, true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001103 return mMaxScreenId;
1104 }
1105
1106 public void updateMaxScreenId(long maxScreenId) {
Winson Chunga90303b2013-11-15 13:05:06 -08001107 // Log to disk
1108 Launcher.addDumpLog(TAG, "11683562 - updateMaxScreenId(): " + maxScreenId, true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001109 mMaxScreenId = maxScreenId;
1110 }
1111
1112 private long initializeMaxScreenId(SQLiteDatabase db) {
1113 Cursor c = db.rawQuery("SELECT MAX(" + LauncherSettings.WorkspaceScreens._ID + ") FROM " + TABLE_WORKSPACE_SCREENS, null);
1114
1115 // get the result
1116 final int maxIdIndex = 0;
1117 long id = -1;
1118 if (c != null && c.moveToNext()) {
1119 id = c.getLong(maxIdIndex);
1120 }
1121 if (c != null) {
1122 c.close();
1123 }
1124
1125 if (id == -1) {
1126 throw new RuntimeException("Error: could not query max screen id");
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001127 }
1128
Winson Chunga90303b2013-11-15 13:05:06 -08001129 // Log to disk
1130 Launcher.addDumpLog(TAG, "11683562 - initializeMaxScreenId(): " + id, true);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001131 return id;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001132 }
1133
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001135 * Upgrade existing clock and photo frame widgets into their new widget
Bjorn Bringert93c45762009-12-16 13:19:47 +00001136 * equivalents.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137 */
1138 private void convertWidgets(SQLiteDatabase db) {
Bjorn Bringert34251342009-12-15 13:33:11 +00001139 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 final int[] bindSources = new int[] {
1141 Favorites.ITEM_TYPE_WIDGET_CLOCK,
1142 Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME,
Bjorn Bringert7984c942009-12-09 15:38:25 +00001143 Favorites.ITEM_TYPE_WIDGET_SEARCH,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001144 };
Bjorn Bringert7984c942009-12-09 15:38:25 +00001145
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001146 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE, bindSources);
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001147
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001148 Cursor c = null;
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001149
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001150 db.beginTransaction();
1151 try {
1152 // Select and iterate through each matching widget
Bjorn Bringert7984c942009-12-09 15:38:25 +00001153 c = db.query(TABLE_FAVORITES, new String[] { Favorites._ID, Favorites.ITEM_TYPE },
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001154 selectWhere, null, null, null, null);
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001155
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001156 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001157
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001158 final ContentValues values = new ContentValues();
1159 while (c != null && c.moveToNext()) {
1160 long favoriteId = c.getLong(0);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001161 int favoriteType = c.getInt(1);
1162
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001163 // Allocate and update database with new appWidgetId
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001164 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001165 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001166
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001167 if (LOGD) {
1168 Log.d(TAG, "allocated appWidgetId=" + appWidgetId
1169 + " for favoriteId=" + favoriteId);
1170 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171 values.clear();
Bjorn Bringert7984c942009-12-09 15:38:25 +00001172 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
1173 values.put(Favorites.APPWIDGET_ID, appWidgetId);
1174
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175 // Original widgets might not have valid spans when upgrading
Bjorn Bringert7984c942009-12-09 15:38:25 +00001176 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
1177 values.put(LauncherSettings.Favorites.SPANX, 4);
1178 values.put(LauncherSettings.Favorites.SPANY, 1);
1179 } else {
1180 values.put(LauncherSettings.Favorites.SPANX, 2);
1181 values.put(LauncherSettings.Favorites.SPANY, 2);
1182 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183
1184 String updateWhere = Favorites._ID + "=" + favoriteId;
1185 db.update(TABLE_FAVORITES, values, updateWhere, null);
Bjorn Bringert34251342009-12-15 13:33:11 +00001186
Bjorn Bringert34251342009-12-15 13:33:11 +00001187 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_CLOCK) {
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.alarmclock",
1191 "com.android.alarmclock.AnalogAppWidgetProvider"));
1192 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001193 // TODO: check return value
1194 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
Bjorn Bringert34251342009-12-15 13:33:11 +00001195 new ComponentName("com.android.camera",
1196 "com.android.camera.PhotoAppWidgetProvider"));
1197 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001198 // TODO: check return value
1199 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
Bjorn Bringertcd8fec02010-01-14 13:26:43 +00001200 getSearchWidgetProvider());
Bjorn Bringert34251342009-12-15 13:33:11 +00001201 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001203 Log.e(TAG, "Problem allocating appWidgetId", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001204 }
1205 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001206
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207 db.setTransactionSuccessful();
1208 } catch (SQLException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001209 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001210 } finally {
1211 db.endTransaction();
1212 if (c != null) {
1213 c.close();
1214 }
1215 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001216
1217 // Update max item id
1218 mMaxItemId = initializeMaxItemId(db);
1219 if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 }
1221
Adam Cohena043fa82014-07-23 14:49:38 -07001222 private boolean initializeExternalAdd(ContentValues values) {
1223 // 1. Ensure that externally added items have a valid item id
1224 long id = generateNewItemId();
1225 values.put(LauncherSettings.Favorites._ID, id);
1226
1227 // 2. In the case of an app widget, and if no app widget id is specified, we
1228 // attempt allocate and bind the widget.
1229 Integer itemType = values.getAsInteger(LauncherSettings.Favorites.ITEM_TYPE);
1230 if (itemType != null &&
1231 itemType.intValue() == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET &&
1232 !values.containsKey(LauncherSettings.Favorites.APPWIDGET_ID)) {
1233
1234 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
1235 ComponentName cn = ComponentName.unflattenFromString(
1236 values.getAsString(Favorites.APPWIDGET_PROVIDER));
1237
1238 if (cn != null) {
1239 try {
1240 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
Adam Cohen3ed316a2014-07-23 18:21:20 -07001241 values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId);
Adam Coheneb1ac422014-10-14 08:55:28 -07001242 if (!appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,cn)) {
Adam Cohend61a6382014-10-14 15:04:34 -07001243 return false;
Adam Cohena043fa82014-07-23 14:49:38 -07001244 }
1245 } catch (RuntimeException e) {
1246 Log.e(TAG, "Failed to initialize external widget", e);
Adam Coheneb1ac422014-10-14 08:55:28 -07001247 return false;
Adam Cohena043fa82014-07-23 14:49:38 -07001248 }
Adam Coheneb1ac422014-10-14 08:55:28 -07001249 } else {
1250 return false;
Adam Cohena043fa82014-07-23 14:49:38 -07001251 }
1252 }
Adam Cohen7ec3bbf2014-07-31 00:09:45 -07001253
1254 // Add screen id if not present
1255 long screenId = values.getAsLong(LauncherSettings.Favorites.SCREEN);
1256 if (!addScreenIdIfNecessary(screenId)) {
1257 return false;
1258 }
Adam Cohena043fa82014-07-23 14:49:38 -07001259 return true;
1260 }
1261
Adam Cohen7ec3bbf2014-07-31 00:09:45 -07001262 // Returns true of screen id exists, or if successfully added
1263 private boolean addScreenIdIfNecessary(long screenId) {
1264 if (!hasScreenId(screenId)) {
1265 int rank = getMaxScreenRank() + 1;
1266
1267 ContentValues v = new ContentValues();
1268 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1269 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, rank);
1270 if (dbInsertAndCheck(this, getWritableDatabase(),
1271 TABLE_WORKSPACE_SCREENS, null, v) < 0) {
1272 return false;
1273 }
1274 }
1275 return true;
1276 }
1277
1278 private boolean hasScreenId(long screenId) {
1279 SQLiteDatabase db = getWritableDatabase();
1280 Cursor c = db.rawQuery("SELECT * FROM " + TABLE_WORKSPACE_SCREENS + " WHERE "
1281 + LauncherSettings.WorkspaceScreens._ID + " = " + screenId, null);
1282 if (c != null) {
1283 int count = c.getCount();
1284 c.close();
1285 return count > 0;
1286 } else {
1287 return false;
1288 }
1289 }
1290
1291 private int getMaxScreenRank() {
1292 SQLiteDatabase db = getWritableDatabase();
1293 Cursor c = db.rawQuery("SELECT MAX(" + LauncherSettings.WorkspaceScreens.SCREEN_RANK
1294 + ") FROM " + TABLE_WORKSPACE_SCREENS, null);
1295
1296 // get the result
1297 final int maxRankIndex = 0;
1298 int rank = -1;
1299 if (c != null && c.moveToNext()) {
1300 rank = c.getInt(maxRankIndex);
1301 }
1302 if (c != null) {
1303 c.close();
1304 }
1305
1306 return rank;
1307 }
1308
Sunny Goyal3a5a9d12014-10-01 15:33:41 -07001309 private int loadFavorites(SQLiteDatabase db, AutoInstallsLayout loader) {
Adam Cohen71483f42014-05-15 14:04:01 -07001310 ArrayList<Long> screenIds = new ArrayList<Long>();
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001311 // TODO: Use multiple loaders with fall-back and transaction.
1312 int count = loader.loadLayout(db, screenIds);
Adam Cohen71483f42014-05-15 14:04:01 -07001313
1314 // Add the screens specified by the items above
1315 Collections.sort(screenIds);
1316 int rank = 0;
1317 ContentValues values = new ContentValues();
1318 for (Long id : screenIds) {
1319 values.clear();
1320 values.put(LauncherSettings.WorkspaceScreens._ID, id);
1321 values.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, rank);
1322 if (dbInsertAndCheck(this, db, TABLE_WORKSPACE_SCREENS, null, values) < 0) {
1323 throw new RuntimeException("Failed initialize screen table"
1324 + "from default layout");
1325 }
1326 rank++;
1327 }
1328
1329 // Ensure that the max ids are initialized
1330 mMaxItemId = initializeMaxItemId(db);
1331 mMaxScreenId = initializeMaxScreenId(db);
Adam Cohen7ec3bbf2014-07-31 00:09:45 -07001332
Adam Cohen71483f42014-05-15 14:04:01 -07001333 return count;
1334 }
1335
Bjorn Bringertcd8fec02010-01-14 13:26:43 +00001336 private ComponentName getSearchWidgetProvider() {
1337 SearchManager searchManager =
1338 (SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE);
1339 ComponentName searchComponent = searchManager.getGlobalSearchActivity();
1340 if (searchComponent == null) return null;
1341 return getProviderInPackage(searchComponent.getPackageName());
1342 }
1343
1344 /**
1345 * Gets an appwidget provider from the given package. If the package contains more than
1346 * one appwidget provider, an arbitrary one is returned.
1347 */
1348 private ComponentName getProviderInPackage(String packageName) {
1349 AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
1350 List<AppWidgetProviderInfo> providers = appWidgetManager.getInstalledProviders();
1351 if (providers == null) return null;
1352 final int providerCount = providers.size();
1353 for (int i = 0; i < providerCount; i++) {
1354 ComponentName provider = providers.get(i).provider;
1355 if (provider != null && provider.getPackageName().equals(packageName)) {
1356 return provider;
1357 }
1358 }
1359 return null;
1360 }
1361
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001362 private void migrateLauncher2Shortcuts(SQLiteDatabase db, Uri uri) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001363 final ContentResolver resolver = mContext.getContentResolver();
1364 Cursor c = null;
1365 int count = 0;
1366 int curScreen = 0;
1367
1368 try {
1369 c = resolver.query(uri, null, null, null, "title ASC");
1370 } catch (Exception e) {
1371 // Ignore
1372 }
1373
Dan Sandlerd5024042014-01-09 15:01:33 -05001374 // We already have a favorites database in the old provider
1375 if (c != null) {
1376 try {
1377 if (c.getCount() > 0) {
1378 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1379 final int intentIndex
1380 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
1381 final int titleIndex
1382 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
1383 final int iconTypeIndex
1384 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
1385 final int iconIndex
1386 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1387 final int iconPackageIndex
1388 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
1389 final int iconResourceIndex
1390 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
1391 final int containerIndex
1392 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
1393 final int itemTypeIndex
1394 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
1395 final int screenIndex
1396 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
1397 final int cellXIndex
1398 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
1399 final int cellYIndex
1400 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
1401 final int uriIndex
1402 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1403 final int displayModeIndex
1404 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
Kenny Guyed131872014-04-30 03:02:21 +01001405 final int profileIndex
1406 = c.getColumnIndex(LauncherSettings.Favorites.PROFILE_ID);
Dan Sandlerd5024042014-01-09 15:01:33 -05001407
1408 int i = 0;
1409 int curX = 0;
1410 int curY = 0;
1411
1412 final LauncherAppState app = LauncherAppState.getInstance();
1413 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1414 final int width = (int) grid.numColumns;
1415 final int height = (int) grid.numRows;
1416 final int hotseatWidth = (int) grid.numHotseatIcons;
1417
1418 final HashSet<String> seenIntents = new HashSet<String>(c.getCount());
1419
Adam Cohen72960972014-01-15 18:13:55 -08001420 final ArrayList<ContentValues> shortcuts = new ArrayList<ContentValues>();
1421 final ArrayList<ContentValues> folders = new ArrayList<ContentValues>();
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001422 final SparseArray<ContentValues> hotseat = new SparseArray<ContentValues>();
Dan Sandlerd5024042014-01-09 15:01:33 -05001423
1424 while (c.moveToNext()) {
1425 final int itemType = c.getInt(itemTypeIndex);
1426 if (itemType != Favorites.ITEM_TYPE_APPLICATION
1427 && itemType != Favorites.ITEM_TYPE_SHORTCUT
1428 && itemType != Favorites.ITEM_TYPE_FOLDER) {
1429 continue;
1430 }
1431
1432 final int cellX = c.getInt(cellXIndex);
1433 final int cellY = c.getInt(cellYIndex);
1434 final int screen = c.getInt(screenIndex);
1435 int container = c.getInt(containerIndex);
1436 final String intentStr = c.getString(intentIndex);
Kenny Guyed131872014-04-30 03:02:21 +01001437
1438 UserManagerCompat userManager = UserManagerCompat.getInstance(mContext);
1439 UserHandleCompat userHandle;
1440 final long userSerialNumber;
1441 if (profileIndex != -1 && !c.isNull(profileIndex)) {
1442 userSerialNumber = c.getInt(profileIndex);
1443 userHandle = userManager.getUserForSerialNumber(userSerialNumber);
1444 } else {
1445 // Default to the serial number of this user, for older
1446 // shortcuts.
1447 userHandle = UserHandleCompat.myUserHandle();
1448 userSerialNumber = userManager.getSerialNumberForUser(userHandle);
1449 }
Dan Sandlerd5024042014-01-09 15:01:33 -05001450 Launcher.addDumpLog(TAG, "migrating \""
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001451 + c.getString(titleIndex) + "\" ("
1452 + cellX + "," + cellY + "@"
1453 + LauncherSettings.Favorites.containerToString(container)
1454 + "/" + screen
1455 + "): " + intentStr, true);
Dan Sandlerd5024042014-01-09 15:01:33 -05001456
1457 if (itemType != Favorites.ITEM_TYPE_FOLDER) {
Adam Cohen556f6132014-01-15 15:18:08 -08001458
1459 final Intent intent;
1460 final ComponentName cn;
1461 try {
1462 intent = Intent.parseUri(intentStr, 0);
1463 } catch (URISyntaxException e) {
1464 // bogus intent?
1465 Launcher.addDumpLog(TAG,
1466 "skipping invalid intent uri", true);
1467 continue;
1468 }
1469
1470 cn = intent.getComponent();
Dan Sandlerd5024042014-01-09 15:01:33 -05001471 if (TextUtils.isEmpty(intentStr)) {
1472 // no intent? no icon
1473 Launcher.addDumpLog(TAG, "skipping empty intent", true);
1474 continue;
Adam Cohen72960972014-01-15 18:13:55 -08001475 } else if (cn != null &&
Kenny Guyed131872014-04-30 03:02:21 +01001476 !LauncherModel.isValidPackageActivity(mContext, cn,
1477 userHandle)) {
Adam Cohen556f6132014-01-15 15:18:08 -08001478 // component no longer exists.
Adam Cohen72960972014-01-15 18:13:55 -08001479 Launcher.addDumpLog(TAG, "skipping item whose component " +
Adam Cohen556f6132014-01-15 15:18:08 -08001480 "no longer exists.", true);
1481 continue;
Adam Cohen72960972014-01-15 18:13:55 -08001482 } else if (container ==
1483 LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1484 // Dedupe icons directly on the workspace
1485
Adam Cohen556f6132014-01-15 15:18:08 -08001486 // Canonicalize
1487 // the Play Store sets the package parameter, but Launcher
Adam Cohena33f11e2014-10-24 12:20:20 -07001488 // does not, so we clear that out to keep them the same.
1489 // Also ignore intent flags for the purposes of deduping.
Adam Cohen556f6132014-01-15 15:18:08 -08001490 intent.setPackage(null);
Adam Cohena33f11e2014-10-24 12:20:20 -07001491 int flags = intent.getFlags();
1492 intent.setFlags(0);
Adam Cohen556f6132014-01-15 15:18:08 -08001493 final String key = intent.toUri(0);
Adam Cohena33f11e2014-10-24 12:20:20 -07001494 intent.setFlags(flags);
Adam Cohen556f6132014-01-15 15:18:08 -08001495 if (seenIntents.contains(key)) {
1496 Launcher.addDumpLog(TAG, "skipping duplicate", true);
Dan Sandlerd5024042014-01-09 15:01:33 -05001497 continue;
Adam Cohen556f6132014-01-15 15:18:08 -08001498 } else {
1499 seenIntents.add(key);
Dan Sandlerd5024042014-01-09 15:01:33 -05001500 }
1501 }
1502 }
1503
1504 ContentValues values = new ContentValues(c.getColumnCount());
1505 values.put(LauncherSettings.Favorites._ID, c.getInt(idIndex));
1506 values.put(LauncherSettings.Favorites.INTENT, intentStr);
1507 values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex));
1508 values.put(LauncherSettings.Favorites.ICON_TYPE,
1509 c.getInt(iconTypeIndex));
1510 values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex));
1511 values.put(LauncherSettings.Favorites.ICON_PACKAGE,
1512 c.getString(iconPackageIndex));
1513 values.put(LauncherSettings.Favorites.ICON_RESOURCE,
1514 c.getString(iconResourceIndex));
1515 values.put(LauncherSettings.Favorites.ITEM_TYPE, itemType);
1516 values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1);
1517 values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex));
1518 values.put(LauncherSettings.Favorites.DISPLAY_MODE,
1519 c.getInt(displayModeIndex));
Kenny Guyed131872014-04-30 03:02:21 +01001520 values.put(LauncherSettings.Favorites.PROFILE_ID, userSerialNumber);
Dan Sandlerd5024042014-01-09 15:01:33 -05001521
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001522 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1523 hotseat.put(screen, values);
Dan Sandlerd5024042014-01-09 15:01:33 -05001524 }
1525
1526 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1527 // In a folder or in the hotseat, preserve position
1528 values.put(LauncherSettings.Favorites.SCREEN, screen);
1529 values.put(LauncherSettings.Favorites.CELLX, cellX);
1530 values.put(LauncherSettings.Favorites.CELLY, cellY);
1531 } else {
Adam Cohen72960972014-01-15 18:13:55 -08001532 // For items contained directly on one of the workspace screen,
1533 // we'll determine their location (screen, x, y) in a second pass.
Dan Sandlerd5024042014-01-09 15:01:33 -05001534 }
1535
1536 values.put(LauncherSettings.Favorites.CONTAINER, container);
1537
Adam Cohen72960972014-01-15 18:13:55 -08001538 if (itemType != Favorites.ITEM_TYPE_FOLDER) {
1539 shortcuts.add(values);
1540 } else {
1541 folders.add(values);
1542 }
Dan Sandlerd5024042014-01-09 15:01:33 -05001543 }
1544
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001545 // Now that we have all the hotseat icons, let's go through them left-right
1546 // and assign valid locations for them in the new hotseat
1547 final int N = hotseat.size();
1548 for (int idx=0; idx<N; idx++) {
1549 int hotseatX = hotseat.keyAt(idx);
1550 ContentValues values = hotseat.valueAt(idx);
1551
1552 if (hotseatX == grid.hotseatAllAppsRank) {
1553 // let's drop this in the next available hole in the hotseat
1554 while (++hotseatX < hotseatWidth) {
1555 if (hotseat.get(hotseatX) == null) {
1556 // found a spot! move it here
1557 values.put(LauncherSettings.Favorites.SCREEN,
1558 hotseatX);
1559 break;
1560 }
1561 }
1562 }
1563 if (hotseatX >= hotseatWidth) {
1564 // no room for you in the hotseat? it's off to the desktop with you
1565 values.put(LauncherSettings.Favorites.CONTAINER,
1566 Favorites.CONTAINER_DESKTOP);
1567 }
1568 }
1569
Adam Cohen72960972014-01-15 18:13:55 -08001570 final ArrayList<ContentValues> allItems = new ArrayList<ContentValues>();
1571 // Folders first
1572 allItems.addAll(folders);
1573 // Then shortcuts
1574 allItems.addAll(shortcuts);
1575
1576 // Layout all the folders
1577 for (ContentValues values: allItems) {
1578 if (values.getAsInteger(LauncherSettings.Favorites.CONTAINER) !=
1579 LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1580 // Hotseat items and folder items have already had their
1581 // location information set. Nothing to be done here.
1582 continue;
1583 }
1584 values.put(LauncherSettings.Favorites.SCREEN, curScreen);
1585 values.put(LauncherSettings.Favorites.CELLX, curX);
1586 values.put(LauncherSettings.Favorites.CELLY, curY);
1587 curX = (curX + 1) % width;
1588 if (curX == 0) {
1589 curY = (curY + 1);
1590 }
1591 // Leave the last row of icons blank on every screen
1592 if (curY == height - 1) {
1593 curScreen = (int) generateNewScreenId();
1594 curY = 0;
1595 }
1596 }
1597
1598 if (allItems.size() > 0) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001599 db.beginTransaction();
1600 try {
Adam Cohen72960972014-01-15 18:13:55 -08001601 for (ContentValues row: allItems) {
1602 if (row == null) continue;
1603 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, row)
Dan Sandlerd5024042014-01-09 15:01:33 -05001604 < 0) {
1605 return;
1606 } else {
1607 count++;
1608 }
1609 }
1610 db.setTransactionSuccessful();
1611 } finally {
1612 db.endTransaction();
1613 }
1614 }
1615
1616 db.beginTransaction();
1617 try {
1618 for (i=0; i<=curScreen; i++) {
1619 final ContentValues values = new ContentValues();
1620 values.put(LauncherSettings.WorkspaceScreens._ID, i);
1621 values.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
1622 if (dbInsertAndCheck(this, db, TABLE_WORKSPACE_SCREENS, null, values)
1623 < 0) {
1624 return;
1625 }
1626 }
1627 db.setTransactionSuccessful();
1628 } finally {
1629 db.endTransaction();
1630 }
1631 }
1632 } finally {
1633 c.close();
1634 }
1635 }
1636
1637 Launcher.addDumpLog(TAG, "migrated " + count + " icons from Launcher2 into "
1638 + (curScreen+1) + " screens", true);
1639
1640 // ensure that new screens are created to hold these icons
1641 setFlagJustLoadedOldDb();
1642
1643 // Update max IDs; very important since we just grabbed IDs from another database
1644 mMaxItemId = initializeMaxItemId(db);
1645 mMaxScreenId = initializeMaxScreenId(db);
1646 if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId + " mMaxScreenId: " + mMaxScreenId);
1647 }
Mike Cleronb87bd162009-10-30 16:36:56 -07001648 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001649
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001650 /**
1651 * Build a query string that will match any row where the column matches
1652 * anything in the values list.
1653 */
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001654 private static String buildOrWhereString(String column, int[] values) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001655 StringBuilder selectWhere = new StringBuilder();
1656 for (int i = values.length - 1; i >= 0; i--) {
1657 selectWhere.append(column).append("=").append(values[i]);
1658 if (i > 0) {
1659 selectWhere.append(" OR ");
1660 }
1661 }
1662 return selectWhere.toString();
1663 }
1664
1665 static class SqlArguments {
1666 public final String table;
1667 public final String where;
1668 public final String[] args;
1669
1670 SqlArguments(Uri url, String where, String[] args) {
1671 if (url.getPathSegments().size() == 1) {
1672 this.table = url.getPathSegments().get(0);
1673 this.where = where;
1674 this.args = args;
1675 } else if (url.getPathSegments().size() != 2) {
1676 throw new IllegalArgumentException("Invalid URI: " + url);
1677 } else if (!TextUtils.isEmpty(where)) {
1678 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
1679 } else {
1680 this.table = url.getPathSegments().get(0);
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001681 this.where = "_id=" + ContentUris.parseId(url);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 this.args = null;
1683 }
1684 }
1685
1686 SqlArguments(Uri url) {
1687 if (url.getPathSegments().size() == 1) {
1688 table = url.getPathSegments().get(0);
1689 where = null;
1690 args = null;
1691 } else {
1692 throw new IllegalArgumentException("Invalid URI: " + url);
1693 }
1694 }
1695 }
Adam Cohen72960972014-01-15 18:13:55 -08001696}