blob: 365d989bf006f7a5e713c6a876114e0032e2a7c5 [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
Sunny Goyal33d44382014-10-16 09:24:19 -0700296 public void clearFlagEmptyDbCreated() {
297 String spKey = LauncherAppState.getSharedPreferencesKey();
298 getContext().getSharedPreferences(spKey, Context.MODE_PRIVATE)
299 .edit()
300 .remove(EMPTY_DATABASE_CREATED)
301 .commit();
302 }
303
Sunny Goyal42de82f2014-09-26 22:09:29 -0700304 /**
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700305 * Loads the default workspace based on the following priority scheme:
306 * 1) From a package provided by play store
307 * 2) From a partner configuration APK, already in the system image
308 * 3) The default configuration for the particular device
Brian Muramatsu5524b492012-10-02 16:55:54 -0700309 */
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700310 synchronized public void loadDefaultFavoritesIfNecessary() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400311 String spKey = LauncherAppState.getSharedPreferencesKey();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700312 SharedPreferences sp = getContext().getSharedPreferences(spKey, Context.MODE_PRIVATE);
Adam Cohene25af792013-06-06 23:08:25 -0700313
Winson Chungc763c4e2013-07-19 13:49:06 -0700314 if (sp.getBoolean(EMPTY_DATABASE_CREATED, false)) {
Chris Wren5dee7af2013-12-20 17:22:11 -0500315 Log.d(TAG, "loading default workspace");
Michael Jurka45355c42012-10-08 13:21:35 +0200316
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700317 AutoInstallsLayout loader = AutoInstallsLayout.get(getContext(),
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700318 mOpenHelper.mAppWidgetHost, mOpenHelper);
319
320 if (loader == null) {
Adam Cohen9b8f51f2014-05-30 15:34:09 -0700321 final Partner partner = Partner.get(getContext().getPackageManager());
322 if (partner != null && partner.hasDefaultLayout()) {
323 final Resources partnerRes = partner.getResources();
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700324 int workspaceResId = partnerRes.getIdentifier(Partner.RES_DEFAULT_LAYOUT,
Adam Cohen9b8f51f2014-05-30 15:34:09 -0700325 "xml", partner.getPackageName());
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700326 if (workspaceResId != 0) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700327 loader = new DefaultLayoutParser(getContext(), mOpenHelper.mAppWidgetHost,
328 mOpenHelper, partnerRes, workspaceResId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700329 }
Adam Cohen9b8f51f2014-05-30 15:34:09 -0700330 }
331 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700332
Sunny Goyal9d219682014-10-23 14:21:02 -0700333 final boolean usingExternallyProvidedLayout = loader != null;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700334 if (loader == null) {
Sunny Goyal9d219682014-10-23 14:21:02 -0700335 loader = getDefaultLayoutParser();
Brian Muramatsu5524b492012-10-02 16:55:54 -0700336 }
Michael Jurkab85f8a42012-04-25 15:48:32 -0700337 // Populate favorites table with initial favorites
Sunny Goyal9d219682014-10-23 14:21:02 -0700338 if ((mOpenHelper.loadFavorites(mOpenHelper.getWritableDatabase(), loader) <= 0)
339 && usingExternallyProvidedLayout) {
340 // Unable to load external layout. Cleanup and load the internal layout.
341 createEmptyDB();
342 mOpenHelper.loadFavorites(mOpenHelper.getWritableDatabase(),
343 getDefaultLayoutParser());
344 }
Sunny Goyal33d44382014-10-16 09:24:19 -0700345 clearFlagEmptyDbCreated();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700346 }
347 }
348
Sunny Goyal9d219682014-10-23 14:21:02 -0700349 private DefaultLayoutParser getDefaultLayoutParser() {
350 int defaultLayout = LauncherAppState.getInstance()
351 .getDynamicGrid().getDeviceProfile().defaultLayoutId;
352 return new DefaultLayoutParser(getContext(), mOpenHelper.mAppWidgetHost,
353 mOpenHelper, getContext().getResources(), defaultLayout);
354 }
355
Dan Sandlerd5024042014-01-09 15:01:33 -0500356 public void migrateLauncher2Shortcuts() {
357 mOpenHelper.migrateLauncher2Shortcuts(mOpenHelper.getWritableDatabase(),
Jason Monk0bfcceb2014-03-21 15:42:06 -0400358 Uri.parse(getContext().getString(R.string.old_launcher_provider_uri)));
Dan Sandlerd5024042014-01-09 15:01:33 -0500359 }
360
Winson Chungc763c4e2013-07-19 13:49:06 -0700361 private static interface ContentValuesCallback {
362 public void onRow(ContentValues values);
363 }
364
Adam Cohen6dbe0492013-12-02 17:00:14 -0800365 private static boolean shouldImportLauncher2Database(Context context) {
366 boolean isTablet = context.getResources().getBoolean(R.bool.is_tablet);
367
368 // We don't import the old databse for tablets, as the grid size has changed.
369 return !isTablet && IMPORT_LAUNCHER2_DATABASE;
370 }
371
Dan Sandlerd5024042014-01-09 15:01:33 -0500372 public void deleteDatabase() {
373 // Are you sure? (y/n)
374 final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
Dan Sandler2b471742014-01-21 14:14:41 -0500375 final File dbFile = new File(db.getPath());
Dan Sandlerd5024042014-01-09 15:01:33 -0500376 mOpenHelper.close();
Dan Sandler2b471742014-01-21 14:14:41 -0500377 if (dbFile.exists()) {
378 SQLiteDatabase.deleteDatabase(dbFile);
379 }
Dan Sandlerd5024042014-01-09 15:01:33 -0500380 mOpenHelper = new DatabaseHelper(getContext());
381 }
382
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700383 private static class DatabaseHelper extends SQLiteOpenHelper implements LayoutParserCallback {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 private final Context mContext;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700385 private final AppWidgetHost mAppWidgetHost;
Adam Cohendcd297f2013-06-18 13:13:40 -0700386 private long mMaxItemId = -1;
387 private long mMaxScreenId = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388
Winson Chung0b560dd2014-01-21 13:00:26 -0800389 private boolean mNewDbCreated = false;
390
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391 DatabaseHelper(Context context) {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100392 super(context, LauncherFiles.LAUNCHER_DB, null, DATABASE_VERSION);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 mContext = context;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700394 mAppWidgetHost = new AppWidgetHost(context, Launcher.APPWIDGET_HOST_ID);
Winson Chung3d503fb2011-07-13 17:25:49 -0700395
396 // In the case where neither onCreate nor onUpgrade gets called, we read the maxId from
397 // the DB here
Adam Cohendcd297f2013-06-18 13:13:40 -0700398 if (mMaxItemId == -1) {
399 mMaxItemId = initializeMaxItemId(getWritableDatabase());
400 }
401 if (mMaxScreenId == -1) {
402 mMaxScreenId = initializeMaxScreenId(getWritableDatabase());
Winson Chung3d503fb2011-07-13 17:25:49 -0700403 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800404 }
405
Winson Chung0b560dd2014-01-21 13:00:26 -0800406 public boolean wasNewDbCreated() {
407 return mNewDbCreated;
408 }
409
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700410 /**
411 * Send notification that we've deleted the {@link AppWidgetHost},
412 * probably as part of the initial database creation. The receiver may
413 * want to re-call {@link AppWidgetHost#startListening()} to ensure
414 * callbacks are correctly set.
415 */
416 private void sendAppWidgetResetNotify() {
417 final ContentResolver resolver = mContext.getContentResolver();
418 resolver.notifyChange(CONTENT_APPWIDGET_RESET_URI, null);
419 }
420
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800421 @Override
422 public void onCreate(SQLiteDatabase db) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800423 if (LOGD) Log.d(TAG, "creating new launcher database");
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700424
Adam Cohendcd297f2013-06-18 13:13:40 -0700425 mMaxItemId = 1;
426 mMaxScreenId = 0;
Winson Chung0b560dd2014-01-21 13:00:26 -0800427 mNewDbCreated = true;
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700428
Kenny Guyed131872014-04-30 03:02:21 +0100429 UserManagerCompat userManager = UserManagerCompat.getInstance(mContext);
430 long userSerialNumber = userManager.getSerialNumberForUser(
431 UserHandleCompat.myUserHandle());
432
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800433 db.execSQL("CREATE TABLE favorites (" +
434 "_id INTEGER PRIMARY KEY," +
435 "title TEXT," +
436 "intent TEXT," +
437 "container INTEGER," +
438 "screen INTEGER," +
439 "cellX INTEGER," +
440 "cellY INTEGER," +
441 "spanX INTEGER," +
442 "spanY INTEGER," +
443 "itemType INTEGER," +
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700444 "appWidgetId INTEGER NOT NULL DEFAULT -1," +
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445 "isShortcut INTEGER," +
446 "iconType INTEGER," +
447 "iconPackage TEXT," +
448 "iconResource TEXT," +
449 "icon BLOB," +
450 "uri TEXT," +
Chris Wrend5e66bf2013-09-16 14:02:29 -0400451 "displayMode INTEGER," +
Chris Wren1ada10d2013-09-13 18:01:38 -0400452 "appWidgetProvider TEXT," +
Chris Wrenf4d08112014-01-16 18:13:56 -0500453 "modified INTEGER NOT NULL DEFAULT 0," +
Kenny Guyed131872014-04-30 03:02:21 +0100454 "restored INTEGER NOT NULL DEFAULT 0," +
455 "profileId INTEGER DEFAULT " + userSerialNumber +
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456 ");");
Adam Cohendcd297f2013-06-18 13:13:40 -0700457 addWorkspacesTable(db);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800458
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700459 // Database was just created, so wipe any previous widgets
460 if (mAppWidgetHost != null) {
461 mAppWidgetHost.deleteHost();
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700462 sendAppWidgetResetNotify();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800463 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700464
Adam Cohen6dbe0492013-12-02 17:00:14 -0800465 if (shouldImportLauncher2Database(mContext)) {
Dan Sandlerf0b8dac2013-11-19 12:21:25 -0500466 // Try converting the old database
467 ContentValuesCallback permuteScreensCb = new ContentValuesCallback() {
468 public void onRow(ContentValues values) {
469 int container = values.getAsInteger(LauncherSettings.Favorites.CONTAINER);
470 if (container == Favorites.CONTAINER_DESKTOP) {
471 int screen = values.getAsInteger(LauncherSettings.Favorites.SCREEN);
472 screen = (int) upgradeLauncherDb_permuteScreens(screen);
473 values.put(LauncherSettings.Favorites.SCREEN, screen);
474 }
475 }
476 };
477 Uri uri = Uri.parse("content://" + Settings.AUTHORITY +
478 "/old_favorites?notify=true");
479 if (!convertDatabase(db, uri, permuteScreensCb, true)) {
480 // Try and upgrade from the Launcher2 db
Jason Monk0bfcceb2014-03-21 15:42:06 -0400481 uri = Uri.parse(mContext.getString(R.string.old_launcher_provider_uri));
Dan Sandlerf0b8dac2013-11-19 12:21:25 -0500482 if (!convertDatabase(db, uri, permuteScreensCb, false)) {
483 // If we fail, then set a flag to load the default workspace
484 setFlagEmptyDbCreated();
485 return;
Winson Chungc763c4e2013-07-19 13:49:06 -0700486 }
487 }
Dan Sandlerf0b8dac2013-11-19 12:21:25 -0500488 // Right now, in non-default workspace cases, we want to run the final
489 // upgrade code (ie. to fix workspace screen indices -> ids, etc.), so
490 // set that flag too.
491 setFlagJustLoadedOldDb();
492 } else {
493 // Fresh and clean launcher DB.
494 mMaxItemId = initializeMaxItemId(db);
495 setFlagEmptyDbCreated();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800496 }
497 }
498
Adam Cohendcd297f2013-06-18 13:13:40 -0700499 private void addWorkspacesTable(SQLiteDatabase db) {
500 db.execSQL("CREATE TABLE " + TABLE_WORKSPACE_SCREENS + " (" +
501 LauncherSettings.WorkspaceScreens._ID + " INTEGER," +
Chris Wren1ada10d2013-09-13 18:01:38 -0400502 LauncherSettings.WorkspaceScreens.SCREEN_RANK + " INTEGER," +
503 LauncherSettings.ChangeLogColumns.MODIFIED + " INTEGER NOT NULL DEFAULT 0" +
Adam Cohendcd297f2013-06-18 13:13:40 -0700504 ");");
505 }
506
Adam Cohen119285e2014-04-02 16:59:08 -0700507 private void removeOrphanedItems(SQLiteDatabase db) {
Adam Cohenf9c14de2014-04-17 18:20:45 -0700508 // Delete items directly on the workspace who's screen id doesn't exist
509 // "DELETE FROM favorites WHERE screen NOT IN (SELECT _id FROM workspaceScreens)
510 // AND container = -100"
511 String removeOrphanedDesktopItems = "DELETE FROM " + TABLE_FAVORITES +
512 " WHERE " +
Adam Cohen119285e2014-04-02 16:59:08 -0700513 LauncherSettings.Favorites.SCREEN + " NOT IN (SELECT " +
Adam Cohenf9c14de2014-04-17 18:20:45 -0700514 LauncherSettings.WorkspaceScreens._ID + " FROM " + TABLE_WORKSPACE_SCREENS + ")" +
515 " AND " +
516 LauncherSettings.Favorites.CONTAINER + " = " +
517 LauncherSettings.Favorites.CONTAINER_DESKTOP;
518 db.execSQL(removeOrphanedDesktopItems);
519
520 // Delete items contained in folders which no longer exist (after above statement)
521 // "DELETE FROM favorites WHERE container <> -100 AND container <> -101 AND container
522 // NOT IN (SELECT _id FROM favorites WHERE itemType = 2)"
523 String removeOrphanedFolderItems = "DELETE FROM " + TABLE_FAVORITES +
524 " WHERE " +
525 LauncherSettings.Favorites.CONTAINER + " <> " +
526 LauncherSettings.Favorites.CONTAINER_DESKTOP +
527 " AND "
528 + LauncherSettings.Favorites.CONTAINER + " <> " +
529 LauncherSettings.Favorites.CONTAINER_HOTSEAT +
530 " AND "
531 + LauncherSettings.Favorites.CONTAINER + " NOT IN (SELECT " +
532 LauncherSettings.Favorites._ID + " FROM " + TABLE_FAVORITES +
533 " WHERE " + LauncherSettings.Favorites.ITEM_TYPE + " = " +
534 LauncherSettings.Favorites.ITEM_TYPE_FOLDER + ")";
535 db.execSQL(removeOrphanedFolderItems);
Adam Cohen119285e2014-04-02 16:59:08 -0700536 }
537
Winson Chungc763c4e2013-07-19 13:49:06 -0700538 private void setFlagJustLoadedOldDb() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400539 String spKey = LauncherAppState.getSharedPreferencesKey();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700540 SharedPreferences sp = mContext.getSharedPreferences(spKey, Context.MODE_PRIVATE);
541 SharedPreferences.Editor editor = sp.edit();
Winson Chungc763c4e2013-07-19 13:49:06 -0700542 editor.putBoolean(UPGRADED_FROM_OLD_DATABASE, true);
543 editor.putBoolean(EMPTY_DATABASE_CREATED, false);
Michael Jurkab85f8a42012-04-25 15:48:32 -0700544 editor.commit();
545 }
546
Winson Chungc763c4e2013-07-19 13:49:06 -0700547 private void setFlagEmptyDbCreated() {
548 String spKey = LauncherAppState.getSharedPreferencesKey();
549 SharedPreferences sp = mContext.getSharedPreferences(spKey, Context.MODE_PRIVATE);
550 SharedPreferences.Editor editor = sp.edit();
551 editor.putBoolean(EMPTY_DATABASE_CREATED, true);
552 editor.putBoolean(UPGRADED_FROM_OLD_DATABASE, false);
553 editor.commit();
554 }
555
556 // We rearrange the screens from the old launcher
557 // 12345 -> 34512
558 private long upgradeLauncherDb_permuteScreens(long screen) {
559 if (screen >= 2) {
560 return screen - 2;
561 } else {
562 return screen + 3;
563 }
564 }
565
566 private boolean convertDatabase(SQLiteDatabase db, Uri uri,
567 ContentValuesCallback cb, boolean deleteRows) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800568 if (LOGD) Log.d(TAG, "converting database from an older format, but not onUpgrade");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800569 boolean converted = false;
570
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800571 final ContentResolver resolver = mContext.getContentResolver();
572 Cursor cursor = null;
573
574 try {
575 cursor = resolver.query(uri, null, null, null, null);
576 } catch (Exception e) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700577 // Ignore
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800578 }
579
580 // We already have a favorites database in the old provider
Winson Chungc763c4e2013-07-19 13:49:06 -0700581 if (cursor != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800582 try {
Winson Chungc763c4e2013-07-19 13:49:06 -0700583 if (cursor.getCount() > 0) {
584 converted = copyFromCursor(db, cursor, cb) > 0;
585 if (converted && deleteRows) {
586 resolver.delete(uri, null, null);
587 }
588 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800589 } finally {
590 cursor.close();
591 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800592 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700593
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800594 if (converted) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700595 // Convert widgets from this import into widgets
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800596 if (LOGD) Log.d(TAG, "converted and now triggering widget upgrade");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800597 convertWidgets(db);
Winson Chungc763c4e2013-07-19 13:49:06 -0700598
599 // Update max item id
600 mMaxItemId = initializeMaxItemId(db);
601 if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800602 }
603
604 return converted;
605 }
606
Winson Chungc763c4e2013-07-19 13:49:06 -0700607 private int copyFromCursor(SQLiteDatabase db, Cursor c, ContentValuesCallback cb) {
Romain Guy73b979d2009-06-09 12:57:21 -0700608 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800609 final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
610 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
611 final int iconTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
612 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
613 final int iconPackageIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
614 final int iconResourceIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
615 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
616 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
617 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
618 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
619 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
620 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
621 final int displayModeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
622
623 ContentValues[] rows = new ContentValues[c.getCount()];
624 int i = 0;
625 while (c.moveToNext()) {
626 ContentValues values = new ContentValues(c.getColumnCount());
Romain Guy73b979d2009-06-09 12:57:21 -0700627 values.put(LauncherSettings.Favorites._ID, c.getLong(idIndex));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800628 values.put(LauncherSettings.Favorites.INTENT, c.getString(intentIndex));
629 values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex));
630 values.put(LauncherSettings.Favorites.ICON_TYPE, c.getInt(iconTypeIndex));
631 values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex));
632 values.put(LauncherSettings.Favorites.ICON_PACKAGE, c.getString(iconPackageIndex));
633 values.put(LauncherSettings.Favorites.ICON_RESOURCE, c.getString(iconResourceIndex));
634 values.put(LauncherSettings.Favorites.CONTAINER, c.getInt(containerIndex));
635 values.put(LauncherSettings.Favorites.ITEM_TYPE, c.getInt(itemTypeIndex));
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700636 values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 values.put(LauncherSettings.Favorites.SCREEN, c.getInt(screenIndex));
638 values.put(LauncherSettings.Favorites.CELLX, c.getInt(cellXIndex));
639 values.put(LauncherSettings.Favorites.CELLY, c.getInt(cellYIndex));
640 values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex));
641 values.put(LauncherSettings.Favorites.DISPLAY_MODE, c.getInt(displayModeIndex));
Winson Chungc763c4e2013-07-19 13:49:06 -0700642 if (cb != null) {
643 cb.onRow(values);
644 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800645 rows[i++] = values;
646 }
647
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648 int total = 0;
Winson Chungc763c4e2013-07-19 13:49:06 -0700649 if (i > 0) {
650 db.beginTransaction();
651 try {
652 int numValues = rows.length;
653 for (i = 0; i < numValues; i++) {
654 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, rows[i]) < 0) {
655 return 0;
656 } else {
657 total++;
658 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 }
Winson Chungc763c4e2013-07-19 13:49:06 -0700660 db.setTransactionSuccessful();
661 } finally {
662 db.endTransaction();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 }
665
666 return total;
667 }
668
669 @Override
670 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Winson Chungc763c4e2013-07-19 13:49:06 -0700671 if (LOGD) Log.d(TAG, "onUpgrade triggered: " + oldVersion);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700672
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 int version = oldVersion;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700674 if (version < 3) {
675 // upgrade 1,2 -> 3 added appWidgetId column
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 db.beginTransaction();
677 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700678 // Insert new column for holding appWidgetIds
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 db.execSQL("ALTER TABLE favorites " +
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700680 "ADD COLUMN appWidgetId INTEGER NOT NULL DEFAULT -1;");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 db.setTransactionSuccessful();
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700682 version = 3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 } catch (SQLException ex) {
684 // Old version remains, which means we wipe old data
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800685 Log.e(TAG, ex.getMessage(), ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800686 } finally {
687 db.endTransaction();
688 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700689
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 // Convert existing widgets only if table upgrade was successful
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700691 if (version == 3) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692 convertWidgets(db);
693 }
694 }
Romain Guy73b979d2009-06-09 12:57:21 -0700695
696 if (version < 4) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800697 version = 4;
Romain Guy73b979d2009-06-09 12:57:21 -0700698 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700699
Romain Guy509cd6a2010-03-23 15:10:56 -0700700 // Where's version 5?
701 // - Donut and sholes on 2.0 shipped with version 4 of launcher1.
Daniel Sandler325dc232013-06-05 22:57:57 -0400702 // - Passion shipped on 2.1 with version 6 of launcher3
Romain Guy509cd6a2010-03-23 15:10:56 -0700703 // - Sholes shipped on 2.1r1 (aka Mr. 3) with version 5 of launcher 1
704 // but version 5 on there was the updateContactsShortcuts change
705 // which was version 6 in launcher 2 (first shipped on passion 2.1r1).
706 // The updateContactsShortcuts change is idempotent, so running it twice
707 // is okay so we'll do that when upgrading the devices that shipped with it.
708 if (version < 6) {
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800709 // We went from 3 to 5 screens. Move everything 1 to the right
710 db.beginTransaction();
711 try {
712 db.execSQL("UPDATE favorites SET screen=(screen + 1);");
713 db.setTransactionSuccessful();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800714 } catch (SQLException ex) {
715 // Old version remains, which means we wipe old data
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800716 Log.e(TAG, ex.getMessage(), ex);
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800717 } finally {
718 db.endTransaction();
719 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700720
Romain Guy509cd6a2010-03-23 15:10:56 -0700721 // We added the fast track.
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800722 if (updateContactsShortcuts(db)) {
723 version = 6;
724 }
725 }
Bjorn Bringert7984c942009-12-09 15:38:25 +0000726
727 if (version < 7) {
728 // Version 7 gets rid of the special search widget.
729 convertWidgets(db);
730 version = 7;
731 }
732
Joe Onorato0589f0f2010-02-08 13:44:00 -0800733 if (version < 8) {
734 // Version 8 (froyo) has the icons all normalized. This should
735 // already be the case in practice, but we now rely on it and don't
736 // resample the images each time.
737 normalizeIcons(db);
738 version = 8;
739 }
740
Winson Chung3d503fb2011-07-13 17:25:49 -0700741 if (version < 9) {
742 // The max id is not yet set at this point (onUpgrade is triggered in the ctor
743 // 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 -0700744 if (mMaxItemId == -1) {
745 mMaxItemId = initializeMaxItemId(db);
Winson Chung3d503fb2011-07-13 17:25:49 -0700746 }
747
748 // Add default hotseat icons
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700749 loadFavorites(db, new DefaultLayoutParser(mContext, mAppWidgetHost, this,
750 mContext.getResources(), R.xml.update_workspace));
Winson Chung3d503fb2011-07-13 17:25:49 -0700751 version = 9;
752 }
753
Daniel Lehmannd02402c2012-05-14 18:30:53 -0700754 // We bumped the version three time during JB, once to update the launch flags, once to
755 // update the override for the default launch animation and once to set the mimetype
756 // to improve startup performance
757 if (version < 12) {
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700758 // Contact shortcuts need a different set of flags to be launched now
759 // The updateContactsShortcuts change is idempotent, so we can keep using it like
760 // back in the Donut days
761 updateContactsShortcuts(db);
Daniel Lehmannd02402c2012-05-14 18:30:53 -0700762 version = 12;
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700763 }
764
Adam Cohendcd297f2013-06-18 13:13:40 -0700765 if (version < 13) {
766 // With the new shrink-wrapped and re-orderable workspaces, it makes sense
767 // to persist workspace screens and their relative order.
768 mMaxScreenId = 0;
769
770 // This will never happen in the wild, but when we switch to using workspace
771 // screen ids, redo the import from old launcher.
Winson Chungc763c4e2013-07-19 13:49:06 -0700772 sJustLoadedFromOldDb = true;
Adam Cohendcd297f2013-06-18 13:13:40 -0700773
774 addWorkspacesTable(db);
775 version = 13;
776 }
777
Chris Wrend5e66bf2013-09-16 14:02:29 -0400778 if (version < 14) {
779 db.beginTransaction();
780 try {
781 // Insert new column for holding widget provider name
782 db.execSQL("ALTER TABLE favorites " +
783 "ADD COLUMN appWidgetProvider TEXT;");
784 db.setTransactionSuccessful();
785 version = 14;
786 } catch (SQLException ex) {
787 // Old version remains, which means we wipe old data
788 Log.e(TAG, ex.getMessage(), ex);
789 } finally {
790 db.endTransaction();
791 }
792 }
793
Chris Wren1ada10d2013-09-13 18:01:38 -0400794 if (version < 15) {
795 db.beginTransaction();
796 try {
797 // Insert new column for holding update timestamp
798 db.execSQL("ALTER TABLE favorites " +
799 "ADD COLUMN modified INTEGER NOT NULL DEFAULT 0;");
800 db.execSQL("ALTER TABLE workspaceScreens " +
801 "ADD COLUMN modified INTEGER NOT NULL DEFAULT 0;");
802 db.setTransactionSuccessful();
803 version = 15;
804 } catch (SQLException ex) {
805 // Old version remains, which means we wipe old data
806 Log.e(TAG, ex.getMessage(), ex);
807 } finally {
808 db.endTransaction();
809 }
810 }
811
Chris Wrenf4d08112014-01-16 18:13:56 -0500812
813 if (version < 16) {
814 db.beginTransaction();
815 try {
816 // Insert new column for holding restore status
817 db.execSQL("ALTER TABLE favorites " +
818 "ADD COLUMN restored INTEGER NOT NULL DEFAULT 0;");
819 db.setTransactionSuccessful();
820 version = 16;
821 } catch (SQLException ex) {
822 // Old version remains, which means we wipe old data
823 Log.e(TAG, ex.getMessage(), ex);
824 } finally {
825 db.endTransaction();
826 }
827 }
828
Adam Cohen71e03b92014-02-21 14:09:53 -0800829 if (version < 17) {
830 // We use the db version upgrade here to identify users who may not have seen
831 // clings yet (because they weren't available), but for whom the clings are now
832 // available (tablet users). Because one of the possible cling flows (migration)
833 // is very destructive (wipes out workspaces), we want to prevent this from showing
834 // until clear data. We do so by marking that the clings have been shown.
835 LauncherClings.synchonouslyMarkFirstRunClingDismissed(mContext);
836 version = 17;
837 }
838
Adam Cohen119285e2014-04-02 16:59:08 -0700839 if (version < 18) {
Adam Cohenf9c14de2014-04-17 18:20:45 -0700840 // No-op
841 version = 18;
842 }
843
844 if (version < 19) {
Adam Cohen119285e2014-04-02 16:59:08 -0700845 // Due to a data loss bug, some users may have items associated with screen ids
846 // which no longer exist. Since this can cause other problems, and since the user
847 // will never see these items anyway, we use database upgrade as an opportunity to
848 // clean things up.
Adam Cohenf9c14de2014-04-17 18:20:45 -0700849 removeOrphanedItems(db);
850 version = 19;
Adam Cohen119285e2014-04-02 16:59:08 -0700851 }
852
Kenny Guyed131872014-04-30 03:02:21 +0100853 if (version < 20) {
854 // Add userId column
855 if (addProfileColumn(db)) {
856 version = 20;
857 }
858 // else old version remains, which means we wipe old data
859 }
860
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861 if (version != DATABASE_VERSION) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800862 Log.w(TAG, "Destroying all old data.");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800863 db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES);
Adam Cohendcd297f2013-06-18 13:13:40 -0700864 db.execSQL("DROP TABLE IF EXISTS " + TABLE_WORKSPACE_SCREENS);
865
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800866 onCreate(db);
867 }
868 }
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800869
Adam Cohen9b1d0622014-05-21 19:01:57 -0700870 @Override
871 public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
872 // This shouldn't happen -- throw our hands up in the air and start over.
873 Log.w(TAG, "Database version downgrade from: " + oldVersion + " to " + newVersion +
874 ". Wiping databse.");
Sunny Goyal42de82f2014-09-26 22:09:29 -0700875 createEmptyDB(db);
876 }
Adam Cohen9b1d0622014-05-21 19:01:57 -0700877
Sunny Goyal42de82f2014-09-26 22:09:29 -0700878
879 /**
880 * Clears all the data for a fresh start.
881 */
882 public void createEmptyDB(SQLiteDatabase db) {
Adam Cohen9b1d0622014-05-21 19:01:57 -0700883 db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES);
884 db.execSQL("DROP TABLE IF EXISTS " + TABLE_WORKSPACE_SCREENS);
885 onCreate(db);
886 }
887
Kenny Guyed131872014-04-30 03:02:21 +0100888 private boolean addProfileColumn(SQLiteDatabase db) {
889 db.beginTransaction();
890 try {
891 UserManagerCompat userManager = UserManagerCompat.getInstance(mContext);
892 // Default to the serial number of this user, for older
893 // shortcuts.
894 long userSerialNumber = userManager.getSerialNumberForUser(
895 UserHandleCompat.myUserHandle());
896 // Insert new column for holding user serial number
897 db.execSQL("ALTER TABLE favorites " +
898 "ADD COLUMN profileId INTEGER DEFAULT "
899 + userSerialNumber + ";");
900 db.setTransactionSuccessful();
901 } catch (SQLException ex) {
902 // Old version remains, which means we wipe old data
903 Log.e(TAG, ex.getMessage(), ex);
904 return false;
905 } finally {
906 db.endTransaction();
907 }
908 return true;
909 }
910
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800911 private boolean updateContactsShortcuts(SQLiteDatabase db) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800912 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE,
913 new int[] { Favorites.ITEM_TYPE_SHORTCUT });
914
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700915 Cursor c = null;
916 final String actionQuickContact = "com.android.contacts.action.QUICK_CONTACT";
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800917 db.beginTransaction();
918 try {
919 // Select and iterate through each matching widget
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700920 c = db.query(TABLE_FAVORITES,
921 new String[] { Favorites._ID, Favorites.INTENT },
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800922 selectWhere, null, null, null, null);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700923 if (c == null) return false;
924
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800925 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700926
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800927 final int idIndex = c.getColumnIndex(Favorites._ID);
928 final int intentIndex = c.getColumnIndex(Favorites.INTENT);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700929
930 while (c.moveToNext()) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800931 long favoriteId = c.getLong(idIndex);
932 final String intentUri = c.getString(intentIndex);
933 if (intentUri != null) {
934 try {
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700935 final Intent intent = Intent.parseUri(intentUri, 0);
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800936 android.util.Log.d("Home", intent.toString());
937 final Uri uri = intent.getData();
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700938 if (uri != null) {
939 final String data = uri.toString();
940 if ((Intent.ACTION_VIEW.equals(intent.getAction()) ||
941 actionQuickContact.equals(intent.getAction())) &&
942 (data.startsWith("content://contacts/people/") ||
943 data.startsWith("content://com.android.contacts/" +
944 "contacts/lookup/"))) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800945
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700946 final Intent newIntent = new Intent(actionQuickContact);
947 // When starting from the launcher, start in a new, cleared task
948 // CLEAR_WHEN_TASK_RESET cannot reset the root of a task, so we
949 // clear the whole thing preemptively here since
950 // QuickContactActivity will finish itself when launching other
951 // detail activities.
952 newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
953 Intent.FLAG_ACTIVITY_CLEAR_TASK);
Winson Chung2672ff92012-05-04 16:22:30 -0700954 newIntent.putExtra(
955 Launcher.INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION, true);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700956 newIntent.setData(uri);
Daniel Lehmannd02402c2012-05-14 18:30:53 -0700957 // Determine the type and also put that in the shortcut
958 // (that can speed up launch a bit)
959 newIntent.setDataAndType(uri, newIntent.resolveType(mContext));
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800960
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700961 final ContentValues values = new ContentValues();
962 values.put(LauncherSettings.Favorites.INTENT,
963 newIntent.toUri(0));
964
965 String updateWhere = Favorites._ID + "=" + favoriteId;
966 db.update(TABLE_FAVORITES, values, updateWhere, null);
967 }
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800968 }
969 } catch (RuntimeException ex) {
970 Log.e(TAG, "Problem upgrading shortcut", ex);
971 } catch (URISyntaxException e) {
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700972 Log.e(TAG, "Problem upgrading shortcut", e);
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800973 }
974 }
975 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700976
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800977 db.setTransactionSuccessful();
978 } catch (SQLException ex) {
979 Log.w(TAG, "Problem while upgrading contacts", ex);
980 return false;
981 } finally {
982 db.endTransaction();
983 if (c != null) {
984 c.close();
985 }
986 }
987
988 return true;
989 }
990
Joe Onorato0589f0f2010-02-08 13:44:00 -0800991 private void normalizeIcons(SQLiteDatabase db) {
992 Log.d(TAG, "normalizing icons");
993
Joe Onorato346e1292010-02-18 10:34:24 -0500994 db.beginTransaction();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800995 Cursor c = null;
Joe Onorato9690b392010-03-23 17:34:37 -0400996 SQLiteStatement update = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800997 try {
998 boolean logged = false;
Joe Onorato9690b392010-03-23 17:34:37 -0400999 update = db.compileStatement("UPDATE favorites "
Jeff Hamiltoneaf77d62010-02-13 00:08:17 -06001000 + "SET icon=? WHERE _id=?");
Joe Onorato0589f0f2010-02-08 13:44:00 -08001001
1002 c = db.rawQuery("SELECT _id, icon FROM favorites WHERE iconType=" +
1003 Favorites.ICON_TYPE_BITMAP, null);
1004
1005 final int idIndex = c.getColumnIndexOrThrow(Favorites._ID);
1006 final int iconIndex = c.getColumnIndexOrThrow(Favorites.ICON);
1007
1008 while (c.moveToNext()) {
1009 long id = c.getLong(idIndex);
1010 byte[] data = c.getBlob(iconIndex);
1011 try {
Sunny Goyal2fce90c2014-10-07 12:01:58 -07001012 Bitmap bitmap = Utilities.createIconBitmap(
Joe Onorato0589f0f2010-02-08 13:44:00 -08001013 BitmapFactory.decodeByteArray(data, 0, data.length),
1014 mContext);
1015 if (bitmap != null) {
1016 update.bindLong(1, id);
1017 data = ItemInfo.flattenBitmap(bitmap);
1018 if (data != null) {
1019 update.bindBlob(2, data);
1020 update.execute();
1021 }
1022 bitmap.recycle();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001023 }
1024 } catch (Exception e) {
1025 if (!logged) {
1026 Log.e(TAG, "Failed normalizing icon " + id, e);
1027 } else {
1028 Log.e(TAG, "Also failed normalizing icon " + id);
1029 }
1030 logged = true;
1031 }
1032 }
Bjorn Bringert3a928e42010-02-19 11:15:40 +00001033 db.setTransactionSuccessful();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001034 } catch (SQLException ex) {
1035 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
1036 } finally {
1037 db.endTransaction();
Joe Onorato9690b392010-03-23 17:34:37 -04001038 if (update != null) {
1039 update.close();
1040 }
Joe Onorato0589f0f2010-02-08 13:44:00 -08001041 if (c != null) {
1042 c.close();
1043 }
1044 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001045 }
1046
1047 // Generates a new ID to use for an object in your database. This method should be only
1048 // called from the main UI thread. As an exception, we do call it when we call the
1049 // constructor from the worker thread; however, this doesn't extend until after the
1050 // constructor is called, and we only pass a reference to LauncherProvider to LauncherApp
1051 // after that point
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001052 @Override
Adam Cohendcd297f2013-06-18 13:13:40 -07001053 public long generateNewItemId() {
1054 if (mMaxItemId < 0) {
1055 throw new RuntimeException("Error: max item id was not initialized");
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001056 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001057 mMaxItemId += 1;
1058 return mMaxItemId;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001059 }
1060
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001061 @Override
1062 public long insertAndCheck(SQLiteDatabase db, ContentValues values) {
1063 return dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values);
1064 }
1065
Winson Chungc763c4e2013-07-19 13:49:06 -07001066 public void updateMaxItemId(long id) {
1067 mMaxItemId = id + 1;
1068 }
1069
Chris Wren5dee7af2013-12-20 17:22:11 -05001070 public void checkId(String table, ContentValues values) {
1071 long id = values.getAsLong(LauncherSettings.BaseLauncherColumns._ID);
1072 if (table == LauncherProvider.TABLE_WORKSPACE_SCREENS) {
1073 mMaxScreenId = Math.max(id, mMaxScreenId);
1074 } else {
1075 mMaxItemId = Math.max(id, mMaxItemId);
1076 }
1077 }
1078
Adam Cohendcd297f2013-06-18 13:13:40 -07001079 private long initializeMaxItemId(SQLiteDatabase db) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001080 Cursor c = db.rawQuery("SELECT MAX(_id) FROM favorites", null);
1081
1082 // get the result
1083 final int maxIdIndex = 0;
1084 long id = -1;
1085 if (c != null && c.moveToNext()) {
1086 id = c.getLong(maxIdIndex);
1087 }
Michael Jurka5130e402011-10-13 04:55:35 -07001088 if (c != null) {
1089 c.close();
1090 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001091
1092 if (id == -1) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001093 throw new RuntimeException("Error: could not query max item id");
1094 }
1095
1096 return id;
1097 }
1098
1099 // Generates a new ID to use for an workspace screen in your database. This method
1100 // should be only called from the main UI thread. As an exception, we do call it when we
1101 // call the constructor from the worker thread; however, this doesn't extend until after the
1102 // constructor is called, and we only pass a reference to LauncherProvider to LauncherApp
1103 // after that point
1104 public long generateNewScreenId() {
1105 if (mMaxScreenId < 0) {
1106 throw new RuntimeException("Error: max screen id was not initialized");
1107 }
1108 mMaxScreenId += 1;
Winson Chunga90303b2013-11-15 13:05:06 -08001109 // Log to disk
1110 Launcher.addDumpLog(TAG, "11683562 - generateNewScreenId(): " + mMaxScreenId, true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001111 return mMaxScreenId;
1112 }
1113
1114 public void updateMaxScreenId(long maxScreenId) {
Winson Chunga90303b2013-11-15 13:05:06 -08001115 // Log to disk
1116 Launcher.addDumpLog(TAG, "11683562 - updateMaxScreenId(): " + maxScreenId, true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001117 mMaxScreenId = maxScreenId;
1118 }
1119
1120 private long initializeMaxScreenId(SQLiteDatabase db) {
1121 Cursor c = db.rawQuery("SELECT MAX(" + LauncherSettings.WorkspaceScreens._ID + ") FROM " + TABLE_WORKSPACE_SCREENS, null);
1122
1123 // get the result
1124 final int maxIdIndex = 0;
1125 long id = -1;
1126 if (c != null && c.moveToNext()) {
1127 id = c.getLong(maxIdIndex);
1128 }
1129 if (c != null) {
1130 c.close();
1131 }
1132
1133 if (id == -1) {
1134 throw new RuntimeException("Error: could not query max screen id");
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001135 }
1136
Winson Chunga90303b2013-11-15 13:05:06 -08001137 // Log to disk
1138 Launcher.addDumpLog(TAG, "11683562 - initializeMaxScreenId(): " + id, true);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001139 return id;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001140 }
1141
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001142 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001143 * Upgrade existing clock and photo frame widgets into their new widget
Bjorn Bringert93c45762009-12-16 13:19:47 +00001144 * equivalents.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001145 */
1146 private void convertWidgets(SQLiteDatabase db) {
Bjorn Bringert34251342009-12-15 13:33:11 +00001147 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001148 final int[] bindSources = new int[] {
1149 Favorites.ITEM_TYPE_WIDGET_CLOCK,
1150 Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME,
Bjorn Bringert7984c942009-12-09 15:38:25 +00001151 Favorites.ITEM_TYPE_WIDGET_SEARCH,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152 };
Bjorn Bringert7984c942009-12-09 15:38:25 +00001153
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001154 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE, bindSources);
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001155
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001156 Cursor c = null;
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001157
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001158 db.beginTransaction();
1159 try {
1160 // Select and iterate through each matching widget
Bjorn Bringert7984c942009-12-09 15:38:25 +00001161 c = db.query(TABLE_FAVORITES, new String[] { Favorites._ID, Favorites.ITEM_TYPE },
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001162 selectWhere, null, null, null, null);
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001163
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001164 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001165
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001166 final ContentValues values = new ContentValues();
1167 while (c != null && c.moveToNext()) {
1168 long favoriteId = c.getLong(0);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001169 int favoriteType = c.getInt(1);
1170
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001171 // Allocate and update database with new appWidgetId
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001173 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001174
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001175 if (LOGD) {
1176 Log.d(TAG, "allocated appWidgetId=" + appWidgetId
1177 + " for favoriteId=" + favoriteId);
1178 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001179 values.clear();
Bjorn Bringert7984c942009-12-09 15:38:25 +00001180 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
1181 values.put(Favorites.APPWIDGET_ID, appWidgetId);
1182
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183 // Original widgets might not have valid spans when upgrading
Bjorn Bringert7984c942009-12-09 15:38:25 +00001184 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
1185 values.put(LauncherSettings.Favorites.SPANX, 4);
1186 values.put(LauncherSettings.Favorites.SPANY, 1);
1187 } else {
1188 values.put(LauncherSettings.Favorites.SPANX, 2);
1189 values.put(LauncherSettings.Favorites.SPANY, 2);
1190 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191
1192 String updateWhere = Favorites._ID + "=" + favoriteId;
1193 db.update(TABLE_FAVORITES, values, updateWhere, null);
Bjorn Bringert34251342009-12-15 13:33:11 +00001194
Bjorn Bringert34251342009-12-15 13:33:11 +00001195 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_CLOCK) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001196 // TODO: check return value
1197 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
Bjorn Bringert34251342009-12-15 13:33:11 +00001198 new ComponentName("com.android.alarmclock",
1199 "com.android.alarmclock.AnalogAppWidgetProvider"));
1200 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001201 // TODO: check return value
1202 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
Bjorn Bringert34251342009-12-15 13:33:11 +00001203 new ComponentName("com.android.camera",
1204 "com.android.camera.PhotoAppWidgetProvider"));
1205 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001206 // TODO: check return value
1207 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
Bjorn Bringertcd8fec02010-01-14 13:26:43 +00001208 getSearchWidgetProvider());
Bjorn Bringert34251342009-12-15 13:33:11 +00001209 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001210 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001211 Log.e(TAG, "Problem allocating appWidgetId", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001212 }
1213 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001214
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215 db.setTransactionSuccessful();
1216 } catch (SQLException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001217 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218 } finally {
1219 db.endTransaction();
1220 if (c != null) {
1221 c.close();
1222 }
1223 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001224
1225 // Update max item id
1226 mMaxItemId = initializeMaxItemId(db);
1227 if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228 }
1229
Adam Cohena043fa82014-07-23 14:49:38 -07001230 private boolean initializeExternalAdd(ContentValues values) {
1231 // 1. Ensure that externally added items have a valid item id
1232 long id = generateNewItemId();
1233 values.put(LauncherSettings.Favorites._ID, id);
1234
1235 // 2. In the case of an app widget, and if no app widget id is specified, we
1236 // attempt allocate and bind the widget.
1237 Integer itemType = values.getAsInteger(LauncherSettings.Favorites.ITEM_TYPE);
1238 if (itemType != null &&
1239 itemType.intValue() == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET &&
1240 !values.containsKey(LauncherSettings.Favorites.APPWIDGET_ID)) {
1241
1242 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
1243 ComponentName cn = ComponentName.unflattenFromString(
1244 values.getAsString(Favorites.APPWIDGET_PROVIDER));
1245
1246 if (cn != null) {
1247 try {
1248 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
Adam Cohen3ed316a2014-07-23 18:21:20 -07001249 values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId);
Adam Coheneb1ac422014-10-14 08:55:28 -07001250 if (!appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,cn)) {
Adam Cohend61a6382014-10-14 15:04:34 -07001251 return false;
Adam Cohena043fa82014-07-23 14:49:38 -07001252 }
1253 } catch (RuntimeException e) {
1254 Log.e(TAG, "Failed to initialize external widget", e);
Adam Coheneb1ac422014-10-14 08:55:28 -07001255 return false;
Adam Cohena043fa82014-07-23 14:49:38 -07001256 }
Adam Coheneb1ac422014-10-14 08:55:28 -07001257 } else {
1258 return false;
Adam Cohena043fa82014-07-23 14:49:38 -07001259 }
1260 }
Adam Cohen7ec3bbf2014-07-31 00:09:45 -07001261
1262 // Add screen id if not present
1263 long screenId = values.getAsLong(LauncherSettings.Favorites.SCREEN);
1264 if (!addScreenIdIfNecessary(screenId)) {
1265 return false;
1266 }
Adam Cohena043fa82014-07-23 14:49:38 -07001267 return true;
1268 }
1269
Adam Cohen7ec3bbf2014-07-31 00:09:45 -07001270 // Returns true of screen id exists, or if successfully added
1271 private boolean addScreenIdIfNecessary(long screenId) {
1272 if (!hasScreenId(screenId)) {
1273 int rank = getMaxScreenRank() + 1;
1274
1275 ContentValues v = new ContentValues();
1276 v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
1277 v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, rank);
1278 if (dbInsertAndCheck(this, getWritableDatabase(),
1279 TABLE_WORKSPACE_SCREENS, null, v) < 0) {
1280 return false;
1281 }
1282 }
1283 return true;
1284 }
1285
1286 private boolean hasScreenId(long screenId) {
1287 SQLiteDatabase db = getWritableDatabase();
1288 Cursor c = db.rawQuery("SELECT * FROM " + TABLE_WORKSPACE_SCREENS + " WHERE "
1289 + LauncherSettings.WorkspaceScreens._ID + " = " + screenId, null);
1290 if (c != null) {
1291 int count = c.getCount();
1292 c.close();
1293 return count > 0;
1294 } else {
1295 return false;
1296 }
1297 }
1298
1299 private int getMaxScreenRank() {
1300 SQLiteDatabase db = getWritableDatabase();
1301 Cursor c = db.rawQuery("SELECT MAX(" + LauncherSettings.WorkspaceScreens.SCREEN_RANK
1302 + ") FROM " + TABLE_WORKSPACE_SCREENS, null);
1303
1304 // get the result
1305 final int maxRankIndex = 0;
1306 int rank = -1;
1307 if (c != null && c.moveToNext()) {
1308 rank = c.getInt(maxRankIndex);
1309 }
1310 if (c != null) {
1311 c.close();
1312 }
1313
1314 return rank;
1315 }
1316
Sunny Goyal3a5a9d12014-10-01 15:33:41 -07001317 private int loadFavorites(SQLiteDatabase db, AutoInstallsLayout loader) {
Adam Cohen71483f42014-05-15 14:04:01 -07001318 ArrayList<Long> screenIds = new ArrayList<Long>();
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001319 // TODO: Use multiple loaders with fall-back and transaction.
1320 int count = loader.loadLayout(db, screenIds);
Adam Cohen71483f42014-05-15 14:04:01 -07001321
1322 // Add the screens specified by the items above
1323 Collections.sort(screenIds);
1324 int rank = 0;
1325 ContentValues values = new ContentValues();
1326 for (Long id : screenIds) {
1327 values.clear();
1328 values.put(LauncherSettings.WorkspaceScreens._ID, id);
1329 values.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, rank);
1330 if (dbInsertAndCheck(this, db, TABLE_WORKSPACE_SCREENS, null, values) < 0) {
1331 throw new RuntimeException("Failed initialize screen table"
1332 + "from default layout");
1333 }
1334 rank++;
1335 }
1336
1337 // Ensure that the max ids are initialized
1338 mMaxItemId = initializeMaxItemId(db);
1339 mMaxScreenId = initializeMaxScreenId(db);
Adam Cohen7ec3bbf2014-07-31 00:09:45 -07001340
Adam Cohen71483f42014-05-15 14:04:01 -07001341 return count;
1342 }
1343
Bjorn Bringertcd8fec02010-01-14 13:26:43 +00001344 private ComponentName getSearchWidgetProvider() {
1345 SearchManager searchManager =
1346 (SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE);
1347 ComponentName searchComponent = searchManager.getGlobalSearchActivity();
1348 if (searchComponent == null) return null;
1349 return getProviderInPackage(searchComponent.getPackageName());
1350 }
1351
1352 /**
1353 * Gets an appwidget provider from the given package. If the package contains more than
1354 * one appwidget provider, an arbitrary one is returned.
1355 */
1356 private ComponentName getProviderInPackage(String packageName) {
1357 AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
1358 List<AppWidgetProviderInfo> providers = appWidgetManager.getInstalledProviders();
1359 if (providers == null) return null;
1360 final int providerCount = providers.size();
1361 for (int i = 0; i < providerCount; i++) {
1362 ComponentName provider = providers.get(i).provider;
1363 if (provider != null && provider.getPackageName().equals(packageName)) {
1364 return provider;
1365 }
1366 }
1367 return null;
1368 }
1369
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001370 private void migrateLauncher2Shortcuts(SQLiteDatabase db, Uri uri) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001371 final ContentResolver resolver = mContext.getContentResolver();
1372 Cursor c = null;
1373 int count = 0;
1374 int curScreen = 0;
1375
1376 try {
1377 c = resolver.query(uri, null, null, null, "title ASC");
1378 } catch (Exception e) {
1379 // Ignore
1380 }
1381
Dan Sandlerd5024042014-01-09 15:01:33 -05001382 // We already have a favorites database in the old provider
1383 if (c != null) {
1384 try {
1385 if (c.getCount() > 0) {
1386 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1387 final int intentIndex
1388 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
1389 final int titleIndex
1390 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
1391 final int iconTypeIndex
1392 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
1393 final int iconIndex
1394 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1395 final int iconPackageIndex
1396 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
1397 final int iconResourceIndex
1398 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
1399 final int containerIndex
1400 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
1401 final int itemTypeIndex
1402 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
1403 final int screenIndex
1404 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
1405 final int cellXIndex
1406 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
1407 final int cellYIndex
1408 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
1409 final int uriIndex
1410 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1411 final int displayModeIndex
1412 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
Kenny Guyed131872014-04-30 03:02:21 +01001413 final int profileIndex
1414 = c.getColumnIndex(LauncherSettings.Favorites.PROFILE_ID);
Dan Sandlerd5024042014-01-09 15:01:33 -05001415
1416 int i = 0;
1417 int curX = 0;
1418 int curY = 0;
1419
1420 final LauncherAppState app = LauncherAppState.getInstance();
1421 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1422 final int width = (int) grid.numColumns;
1423 final int height = (int) grid.numRows;
1424 final int hotseatWidth = (int) grid.numHotseatIcons;
1425
1426 final HashSet<String> seenIntents = new HashSet<String>(c.getCount());
1427
Adam Cohen72960972014-01-15 18:13:55 -08001428 final ArrayList<ContentValues> shortcuts = new ArrayList<ContentValues>();
1429 final ArrayList<ContentValues> folders = new ArrayList<ContentValues>();
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001430 final SparseArray<ContentValues> hotseat = new SparseArray<ContentValues>();
Dan Sandlerd5024042014-01-09 15:01:33 -05001431
1432 while (c.moveToNext()) {
1433 final int itemType = c.getInt(itemTypeIndex);
1434 if (itemType != Favorites.ITEM_TYPE_APPLICATION
1435 && itemType != Favorites.ITEM_TYPE_SHORTCUT
1436 && itemType != Favorites.ITEM_TYPE_FOLDER) {
1437 continue;
1438 }
1439
1440 final int cellX = c.getInt(cellXIndex);
1441 final int cellY = c.getInt(cellYIndex);
1442 final int screen = c.getInt(screenIndex);
1443 int container = c.getInt(containerIndex);
1444 final String intentStr = c.getString(intentIndex);
Kenny Guyed131872014-04-30 03:02:21 +01001445
1446 UserManagerCompat userManager = UserManagerCompat.getInstance(mContext);
1447 UserHandleCompat userHandle;
1448 final long userSerialNumber;
1449 if (profileIndex != -1 && !c.isNull(profileIndex)) {
1450 userSerialNumber = c.getInt(profileIndex);
1451 userHandle = userManager.getUserForSerialNumber(userSerialNumber);
1452 } else {
1453 // Default to the serial number of this user, for older
1454 // shortcuts.
1455 userHandle = UserHandleCompat.myUserHandle();
1456 userSerialNumber = userManager.getSerialNumberForUser(userHandle);
1457 }
Dan Sandlerd5024042014-01-09 15:01:33 -05001458 Launcher.addDumpLog(TAG, "migrating \""
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001459 + c.getString(titleIndex) + "\" ("
1460 + cellX + "," + cellY + "@"
1461 + LauncherSettings.Favorites.containerToString(container)
1462 + "/" + screen
1463 + "): " + intentStr, true);
Dan Sandlerd5024042014-01-09 15:01:33 -05001464
1465 if (itemType != Favorites.ITEM_TYPE_FOLDER) {
Adam Cohen556f6132014-01-15 15:18:08 -08001466
1467 final Intent intent;
1468 final ComponentName cn;
1469 try {
1470 intent = Intent.parseUri(intentStr, 0);
1471 } catch (URISyntaxException e) {
1472 // bogus intent?
1473 Launcher.addDumpLog(TAG,
1474 "skipping invalid intent uri", true);
1475 continue;
1476 }
1477
1478 cn = intent.getComponent();
Dan Sandlerd5024042014-01-09 15:01:33 -05001479 if (TextUtils.isEmpty(intentStr)) {
1480 // no intent? no icon
1481 Launcher.addDumpLog(TAG, "skipping empty intent", true);
1482 continue;
Adam Cohen72960972014-01-15 18:13:55 -08001483 } else if (cn != null &&
Kenny Guyed131872014-04-30 03:02:21 +01001484 !LauncherModel.isValidPackageActivity(mContext, cn,
1485 userHandle)) {
Adam Cohen556f6132014-01-15 15:18:08 -08001486 // component no longer exists.
Adam Cohen72960972014-01-15 18:13:55 -08001487 Launcher.addDumpLog(TAG, "skipping item whose component " +
Adam Cohen556f6132014-01-15 15:18:08 -08001488 "no longer exists.", true);
1489 continue;
Adam Cohen72960972014-01-15 18:13:55 -08001490 } else if (container ==
1491 LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1492 // Dedupe icons directly on the workspace
1493
Adam Cohen556f6132014-01-15 15:18:08 -08001494 // Canonicalize
1495 // the Play Store sets the package parameter, but Launcher
Adam Cohena33f11e2014-10-24 12:20:20 -07001496 // does not, so we clear that out to keep them the same.
1497 // Also ignore intent flags for the purposes of deduping.
Adam Cohen556f6132014-01-15 15:18:08 -08001498 intent.setPackage(null);
Adam Cohena33f11e2014-10-24 12:20:20 -07001499 int flags = intent.getFlags();
1500 intent.setFlags(0);
Adam Cohen556f6132014-01-15 15:18:08 -08001501 final String key = intent.toUri(0);
Adam Cohena33f11e2014-10-24 12:20:20 -07001502 intent.setFlags(flags);
Adam Cohen556f6132014-01-15 15:18:08 -08001503 if (seenIntents.contains(key)) {
1504 Launcher.addDumpLog(TAG, "skipping duplicate", true);
Dan Sandlerd5024042014-01-09 15:01:33 -05001505 continue;
Adam Cohen556f6132014-01-15 15:18:08 -08001506 } else {
1507 seenIntents.add(key);
Dan Sandlerd5024042014-01-09 15:01:33 -05001508 }
1509 }
1510 }
1511
1512 ContentValues values = new ContentValues(c.getColumnCount());
1513 values.put(LauncherSettings.Favorites._ID, c.getInt(idIndex));
1514 values.put(LauncherSettings.Favorites.INTENT, intentStr);
1515 values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex));
1516 values.put(LauncherSettings.Favorites.ICON_TYPE,
1517 c.getInt(iconTypeIndex));
1518 values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex));
1519 values.put(LauncherSettings.Favorites.ICON_PACKAGE,
1520 c.getString(iconPackageIndex));
1521 values.put(LauncherSettings.Favorites.ICON_RESOURCE,
1522 c.getString(iconResourceIndex));
1523 values.put(LauncherSettings.Favorites.ITEM_TYPE, itemType);
1524 values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1);
1525 values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex));
1526 values.put(LauncherSettings.Favorites.DISPLAY_MODE,
1527 c.getInt(displayModeIndex));
Kenny Guyed131872014-04-30 03:02:21 +01001528 values.put(LauncherSettings.Favorites.PROFILE_ID, userSerialNumber);
Dan Sandlerd5024042014-01-09 15:01:33 -05001529
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001530 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1531 hotseat.put(screen, values);
Dan Sandlerd5024042014-01-09 15:01:33 -05001532 }
1533
1534 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1535 // In a folder or in the hotseat, preserve position
1536 values.put(LauncherSettings.Favorites.SCREEN, screen);
1537 values.put(LauncherSettings.Favorites.CELLX, cellX);
1538 values.put(LauncherSettings.Favorites.CELLY, cellY);
1539 } else {
Adam Cohen72960972014-01-15 18:13:55 -08001540 // For items contained directly on one of the workspace screen,
1541 // we'll determine their location (screen, x, y) in a second pass.
Dan Sandlerd5024042014-01-09 15:01:33 -05001542 }
1543
1544 values.put(LauncherSettings.Favorites.CONTAINER, container);
1545
Adam Cohen72960972014-01-15 18:13:55 -08001546 if (itemType != Favorites.ITEM_TYPE_FOLDER) {
1547 shortcuts.add(values);
1548 } else {
1549 folders.add(values);
1550 }
Dan Sandlerd5024042014-01-09 15:01:33 -05001551 }
1552
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001553 // Now that we have all the hotseat icons, let's go through them left-right
1554 // and assign valid locations for them in the new hotseat
1555 final int N = hotseat.size();
1556 for (int idx=0; idx<N; idx++) {
1557 int hotseatX = hotseat.keyAt(idx);
1558 ContentValues values = hotseat.valueAt(idx);
1559
1560 if (hotseatX == grid.hotseatAllAppsRank) {
1561 // let's drop this in the next available hole in the hotseat
1562 while (++hotseatX < hotseatWidth) {
1563 if (hotseat.get(hotseatX) == null) {
1564 // found a spot! move it here
1565 values.put(LauncherSettings.Favorites.SCREEN,
1566 hotseatX);
1567 break;
1568 }
1569 }
1570 }
1571 if (hotseatX >= hotseatWidth) {
1572 // no room for you in the hotseat? it's off to the desktop with you
1573 values.put(LauncherSettings.Favorites.CONTAINER,
1574 Favorites.CONTAINER_DESKTOP);
1575 }
1576 }
1577
Adam Cohen72960972014-01-15 18:13:55 -08001578 final ArrayList<ContentValues> allItems = new ArrayList<ContentValues>();
1579 // Folders first
1580 allItems.addAll(folders);
1581 // Then shortcuts
1582 allItems.addAll(shortcuts);
1583
1584 // Layout all the folders
1585 for (ContentValues values: allItems) {
1586 if (values.getAsInteger(LauncherSettings.Favorites.CONTAINER) !=
1587 LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1588 // Hotseat items and folder items have already had their
1589 // location information set. Nothing to be done here.
1590 continue;
1591 }
1592 values.put(LauncherSettings.Favorites.SCREEN, curScreen);
1593 values.put(LauncherSettings.Favorites.CELLX, curX);
1594 values.put(LauncherSettings.Favorites.CELLY, curY);
1595 curX = (curX + 1) % width;
1596 if (curX == 0) {
1597 curY = (curY + 1);
1598 }
1599 // Leave the last row of icons blank on every screen
1600 if (curY == height - 1) {
1601 curScreen = (int) generateNewScreenId();
1602 curY = 0;
1603 }
1604 }
1605
1606 if (allItems.size() > 0) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001607 db.beginTransaction();
1608 try {
Adam Cohen72960972014-01-15 18:13:55 -08001609 for (ContentValues row: allItems) {
1610 if (row == null) continue;
1611 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, row)
Dan Sandlerd5024042014-01-09 15:01:33 -05001612 < 0) {
1613 return;
1614 } else {
1615 count++;
1616 }
1617 }
1618 db.setTransactionSuccessful();
1619 } finally {
1620 db.endTransaction();
1621 }
1622 }
1623
1624 db.beginTransaction();
1625 try {
1626 for (i=0; i<=curScreen; i++) {
1627 final ContentValues values = new ContentValues();
1628 values.put(LauncherSettings.WorkspaceScreens._ID, i);
1629 values.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
1630 if (dbInsertAndCheck(this, db, TABLE_WORKSPACE_SCREENS, null, values)
1631 < 0) {
1632 return;
1633 }
1634 }
1635 db.setTransactionSuccessful();
1636 } finally {
1637 db.endTransaction();
1638 }
1639 }
1640 } finally {
1641 c.close();
1642 }
1643 }
1644
1645 Launcher.addDumpLog(TAG, "migrated " + count + " icons from Launcher2 into "
1646 + (curScreen+1) + " screens", true);
1647
1648 // ensure that new screens are created to hold these icons
1649 setFlagJustLoadedOldDb();
1650
1651 // Update max IDs; very important since we just grabbed IDs from another database
1652 mMaxItemId = initializeMaxItemId(db);
1653 mMaxScreenId = initializeMaxScreenId(db);
1654 if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId + " mMaxScreenId: " + mMaxScreenId);
1655 }
Mike Cleronb87bd162009-10-30 16:36:56 -07001656 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001657
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658 /**
1659 * Build a query string that will match any row where the column matches
1660 * anything in the values list.
1661 */
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001662 private static String buildOrWhereString(String column, int[] values) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663 StringBuilder selectWhere = new StringBuilder();
1664 for (int i = values.length - 1; i >= 0; i--) {
1665 selectWhere.append(column).append("=").append(values[i]);
1666 if (i > 0) {
1667 selectWhere.append(" OR ");
1668 }
1669 }
1670 return selectWhere.toString();
1671 }
1672
1673 static class SqlArguments {
1674 public final String table;
1675 public final String where;
1676 public final String[] args;
1677
1678 SqlArguments(Uri url, String where, String[] args) {
1679 if (url.getPathSegments().size() == 1) {
1680 this.table = url.getPathSegments().get(0);
1681 this.where = where;
1682 this.args = args;
1683 } else if (url.getPathSegments().size() != 2) {
1684 throw new IllegalArgumentException("Invalid URI: " + url);
1685 } else if (!TextUtils.isEmpty(where)) {
1686 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
1687 } else {
1688 this.table = url.getPathSegments().get(0);
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001689 this.where = "_id=" + ContentUris.parseId(url);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001690 this.args = null;
1691 }
1692 }
1693
1694 SqlArguments(Uri url) {
1695 if (url.getPathSegments().size() == 1) {
1696 table = url.getPathSegments().get(0);
1697 where = null;
1698 args = null;
1699 } else {
1700 throw new IllegalArgumentException("Invalid URI: " + url);
1701 }
1702 }
1703 }
Adam Cohen72960972014-01-15 18:13:55 -08001704}