blob: d4ce322c8e6461def7533b8b5eb477024fd0e05e [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.ContentResolver;
Adam Cohen228da5a2011-07-27 22:23:47 -070026import android.content.ContentUris;
27import android.content.ContentValues;
28import android.content.Context;
29import android.content.Intent;
Michael Jurkab85f8a42012-04-25 15:48:32 -070030import android.content.SharedPreferences;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.content.pm.ActivityInfo;
Adam Cohen228da5a2011-07-27 22:23:47 -070032import android.content.pm.PackageManager;
33import android.content.res.Resources;
34import android.content.res.TypedArray;
35import android.content.res.XmlResourceParser;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.database.Cursor;
37import android.database.SQLException;
Adam Cohen228da5a2011-07-27 22:23:47 -070038import android.database.sqlite.SQLiteDatabase;
39import android.database.sqlite.SQLiteOpenHelper;
40import android.database.sqlite.SQLiteQueryBuilder;
41import android.database.sqlite.SQLiteStatement;
Joe Onorato0589f0f2010-02-08 13:44:00 -080042import android.graphics.Bitmap;
43import android.graphics.BitmapFactory;
Adam Cohen228da5a2011-07-27 22:23:47 -070044import android.net.Uri;
Winson Chungb3302ae2012-05-01 10:19:14 -070045import android.os.Bundle;
Adam Cohen228da5a2011-07-27 22:23:47 -070046import android.provider.Settings;
47import android.text.TextUtils;
48import android.util.AttributeSet;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.util.Log;
50import android.util.Xml;
Adam Cohen228da5a2011-07-27 22:23:47 -070051
Daniel Sandler325dc232013-06-05 22:57:57 -040052import com.android.launcher3.LauncherSettings.Favorites;
Chris Wrene523e702013-10-09 10:36:55 -040053import com.android.launcher3.config.ProviderConfig;
Michael Jurka8b805b12012-04-18 14:23:14 -070054
55import org.xmlpull.v1.XmlPullParser;
56import org.xmlpull.v1.XmlPullParserException;
57
Dan Sandlerd5024042014-01-09 15:01:33 -050058import java.io.File;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import java.io.IOException;
Mike Cleronb87bd162009-10-30 16:36:56 -070060import java.net.URISyntaxException;
Adam Cohen228da5a2011-07-27 22:23:47 -070061import java.util.ArrayList;
Dan Sandlerd5024042014-01-09 15:01:33 -050062import java.util.HashSet;
Bjorn Bringertcd8fec02010-01-14 13:26:43 +000063import java.util.List;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065public class LauncherProvider extends ContentProvider {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080066 private static final String TAG = "Launcher.LauncherProvider";
67 private static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068
69 private static final String DATABASE_NAME = "launcher.db";
Winson Chung3d503fb2011-07-13 17:25:49 -070070
Chris Wren1ada10d2013-09-13 18:01:38 -040071 private static final int DATABASE_VERSION = 15;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072
Adam Cohene25af792013-06-06 23:08:25 -070073 static final String OLD_AUTHORITY = "com.android.launcher2.settings";
Chris Wrene523e702013-10-09 10:36:55 -040074 static final String AUTHORITY = ProviderConfig.AUTHORITY;
Winson Chung3d503fb2011-07-13 17:25:49 -070075
Dan Sandlerf0b8dac2013-11-19 12:21:25 -050076 // Should we attempt to load anything from the com.android.launcher2 provider?
Dan Sandlerd5024042014-01-09 15:01:33 -050077 static final boolean IMPORT_LAUNCHER2_DATABASE = false;
Dan Sandlerf0b8dac2013-11-19 12:21:25 -050078
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079 static final String TABLE_FAVORITES = "favorites";
Adam Cohendcd297f2013-06-18 13:13:40 -070080 static final String TABLE_WORKSPACE_SCREENS = "workspaceScreens";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081 static final String PARAMETER_NOTIFY = "notify";
Winson Chungc763c4e2013-07-19 13:49:06 -070082 static final String UPGRADED_FROM_OLD_DATABASE =
83 "UPGRADED_FROM_OLD_DATABASE";
84 static final String EMPTY_DATABASE_CREATED =
85 "EMPTY_DATABASE_CREATED";
Michael Jurka45355c42012-10-08 13:21:35 +020086 static final String DEFAULT_WORKSPACE_RESOURCE_ID =
87 "DEFAULT_WORKSPACE_RESOURCE_ID";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088
Winson Chungb3302ae2012-05-01 10:19:14 -070089 private static final String ACTION_APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE =
Adam Cohene25af792013-06-06 23:08:25 -070090 "com.android.launcher.action.APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE";
Winson Chungb3302ae2012-05-01 10:19:14 -070091
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -070092 /**
Romain Guy73b979d2009-06-09 12:57:21 -070093 * {@link Uri} triggered at any registered {@link android.database.ContentObserver} when
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -070094 * {@link AppWidgetHost#deleteHost()} is called during database creation.
95 * Use this to recall {@link AppWidgetHost#startListening()} if needed.
96 */
97 static final Uri CONTENT_APPWIDGET_RESET_URI =
98 Uri.parse("content://" + AUTHORITY + "/appWidgetReset");
Daniel Lehmannc3a80402012-04-23 21:35:11 -070099
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700100 private DatabaseHelper mOpenHelper;
Winson Chungc763c4e2013-07-19 13:49:06 -0700101 private static boolean sJustLoadedFromOldDb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102
103 @Override
104 public boolean onCreate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400105 final Context context = getContext();
106 mOpenHelper = new DatabaseHelper(context);
107 LauncherAppState.setLauncherProvider(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108 return true;
109 }
110
111 @Override
112 public String getType(Uri uri) {
113 SqlArguments args = new SqlArguments(uri, null, null);
114 if (TextUtils.isEmpty(args.where)) {
115 return "vnd.android.cursor.dir/" + args.table;
116 } else {
117 return "vnd.android.cursor.item/" + args.table;
118 }
119 }
120
121 @Override
122 public Cursor query(Uri uri, String[] projection, String selection,
123 String[] selectionArgs, String sortOrder) {
124
125 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
126 SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
127 qb.setTables(args.table);
128
Romain Guy73b979d2009-06-09 12:57:21 -0700129 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 Cursor result = qb.query(db, projection, args.where, args.args, null, null, sortOrder);
131 result.setNotificationUri(getContext().getContentResolver(), uri);
132
133 return result;
134 }
135
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700136 private static long dbInsertAndCheck(DatabaseHelper helper,
137 SQLiteDatabase db, String table, String nullColumnHack, ContentValues values) {
Dan Sandlerd5024042014-01-09 15:01:33 -0500138 if (values == null) {
139 throw new RuntimeException("Error: attempting to insert null values");
140 }
Chris Wren5dee7af2013-12-20 17:22:11 -0500141 if (!values.containsKey(LauncherSettings.BaseLauncherColumns._ID)) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700142 throw new RuntimeException("Error: attempting to add item without specifying an id");
143 }
Chris Wren5dee7af2013-12-20 17:22:11 -0500144 helper.checkId(table, values);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700145 return db.insert(table, nullColumnHack, values);
146 }
147
Adam Cohen228da5a2011-07-27 22:23:47 -0700148 private static void deleteId(SQLiteDatabase db, long id) {
149 Uri uri = LauncherSettings.Favorites.getContentUri(id, false);
150 SqlArguments args = new SqlArguments(uri, null, null);
151 db.delete(args.table, args.where, args.args);
152 }
153
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 @Override
155 public Uri insert(Uri uri, ContentValues initialValues) {
156 SqlArguments args = new SqlArguments(uri);
157
158 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
Chris Wren1ada10d2013-09-13 18:01:38 -0400159 addModifiedTime(initialValues);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700160 final long rowId = dbInsertAndCheck(mOpenHelper, db, args.table, null, initialValues);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 if (rowId <= 0) return null;
162
163 uri = ContentUris.withAppendedId(uri, rowId);
164 sendNotify(uri);
165
166 return uri;
167 }
168
169 @Override
170 public int bulkInsert(Uri uri, ContentValues[] values) {
171 SqlArguments args = new SqlArguments(uri);
172
173 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
174 db.beginTransaction();
175 try {
176 int numValues = values.length;
177 for (int i = 0; i < numValues; i++) {
Chris Wren1ada10d2013-09-13 18:01:38 -0400178 addModifiedTime(values[i]);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700179 if (dbInsertAndCheck(mOpenHelper, db, args.table, null, values[i]) < 0) {
180 return 0;
181 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 }
183 db.setTransactionSuccessful();
184 } finally {
185 db.endTransaction();
186 }
187
188 sendNotify(uri);
189 return values.length;
190 }
191
192 @Override
193 public int delete(Uri uri, String selection, String[] selectionArgs) {
194 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
195
196 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
197 int count = db.delete(args.table, args.where, args.args);
198 if (count > 0) sendNotify(uri);
199
200 return count;
201 }
202
203 @Override
204 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
205 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
206
Chris Wren1ada10d2013-09-13 18:01:38 -0400207 addModifiedTime(values);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
209 int count = db.update(args.table, values, args.where, args.args);
210 if (count > 0) sendNotify(uri);
211
212 return count;
213 }
214
215 private void sendNotify(Uri uri) {
216 String notify = uri.getQueryParameter(PARAMETER_NOTIFY);
217 if (notify == null || "true".equals(notify)) {
218 getContext().getContentResolver().notifyChange(uri, null);
219 }
Chris Wren1ada10d2013-09-13 18:01:38 -0400220
221 // always notify the backup agent
Chris Wren92aa4232013-10-04 11:29:36 -0400222 LauncherBackupAgentHelper.dataChanged(getContext());
Chris Wren1ada10d2013-09-13 18:01:38 -0400223 }
224
225 private void addModifiedTime(ContentValues values) {
226 values.put(LauncherSettings.ChangeLogColumns.MODIFIED, System.currentTimeMillis());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227 }
228
Adam Cohendcd297f2013-06-18 13:13:40 -0700229 public long generateNewItemId() {
230 return mOpenHelper.generateNewItemId();
231 }
232
Winson Chungc763c4e2013-07-19 13:49:06 -0700233 public void updateMaxItemId(long id) {
234 mOpenHelper.updateMaxItemId(id);
235 }
236
Adam Cohendcd297f2013-06-18 13:13:40 -0700237 public long generateNewScreenId() {
238 return mOpenHelper.generateNewScreenId();
239 }
240
241 // This is only required one time while loading the workspace during the
242 // upgrade path, and should never be called from anywhere else.
243 public void updateMaxScreenId(long maxScreenId) {
244 mOpenHelper.updateMaxScreenId(maxScreenId);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700245 }
246
Brian Muramatsu5524b492012-10-02 16:55:54 -0700247 /**
Adam Cohene25af792013-06-06 23:08:25 -0700248 * @param Should we load the old db for upgrade? first run only.
249 */
Winson Chungc763c4e2013-07-19 13:49:06 -0700250 synchronized public boolean justLoadedOldDb() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400251 String spKey = LauncherAppState.getSharedPreferencesKey();
Adam Cohene25af792013-06-06 23:08:25 -0700252 SharedPreferences sp = getContext().getSharedPreferences(spKey, Context.MODE_PRIVATE);
253
Winson Chungc763c4e2013-07-19 13:49:06 -0700254 boolean loadedOldDb = false || sJustLoadedFromOldDb;
Adam Cohendcd297f2013-06-18 13:13:40 -0700255
Winson Chungc763c4e2013-07-19 13:49:06 -0700256 sJustLoadedFromOldDb = false;
257 if (sp.getBoolean(UPGRADED_FROM_OLD_DATABASE, false)) {
Adam Cohene25af792013-06-06 23:08:25 -0700258
259 SharedPreferences.Editor editor = sp.edit();
Winson Chungc763c4e2013-07-19 13:49:06 -0700260 editor.remove(UPGRADED_FROM_OLD_DATABASE);
Adam Cohene25af792013-06-06 23:08:25 -0700261 editor.commit();
Winson Chungc763c4e2013-07-19 13:49:06 -0700262 loadedOldDb = true;
Adam Cohene25af792013-06-06 23:08:25 -0700263 }
Winson Chungc763c4e2013-07-19 13:49:06 -0700264 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -0700265 }
266
267 /**
Brian Muramatsu5524b492012-10-02 16:55:54 -0700268 * @param workspaceResId that can be 0 to use default or non-zero for specific resource
269 */
Michael Jurka45355c42012-10-08 13:21:35 +0200270 synchronized public void loadDefaultFavoritesIfNecessary(int origWorkspaceResId) {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400271 String spKey = LauncherAppState.getSharedPreferencesKey();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700272 SharedPreferences sp = getContext().getSharedPreferences(spKey, Context.MODE_PRIVATE);
Adam Cohene25af792013-06-06 23:08:25 -0700273
Winson Chungc763c4e2013-07-19 13:49:06 -0700274 if (sp.getBoolean(EMPTY_DATABASE_CREATED, false)) {
Chris Wren5dee7af2013-12-20 17:22:11 -0500275 Log.d(TAG, "loading default workspace");
Michael Jurka45355c42012-10-08 13:21:35 +0200276 int workspaceResId = origWorkspaceResId;
277
Brian Muramatsu5524b492012-10-02 16:55:54 -0700278 // Use default workspace resource if none provided
279 if (workspaceResId == 0) {
Nilesh Agrawalda41ea62013-12-09 14:17:49 -0800280 workspaceResId =
281 sp.getInt(DEFAULT_WORKSPACE_RESOURCE_ID, getDefaultWorkspaceResourceId());
Brian Muramatsu5524b492012-10-02 16:55:54 -0700282 }
283
Michael Jurkab85f8a42012-04-25 15:48:32 -0700284 // Populate favorites table with initial favorites
285 SharedPreferences.Editor editor = sp.edit();
Winson Chungc763c4e2013-07-19 13:49:06 -0700286 editor.remove(EMPTY_DATABASE_CREATED);
Michael Jurka45355c42012-10-08 13:21:35 +0200287 if (origWorkspaceResId != 0) {
288 editor.putInt(DEFAULT_WORKSPACE_RESOURCE_ID, origWorkspaceResId);
289 }
Adam Cohene25af792013-06-06 23:08:25 -0700290
Brian Muramatsu5524b492012-10-02 16:55:54 -0700291 mOpenHelper.loadFavorites(mOpenHelper.getWritableDatabase(), workspaceResId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700292 mOpenHelper.setFlagJustLoadedOldDb();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700293 editor.commit();
294 }
295 }
296
Dan Sandlerd5024042014-01-09 15:01:33 -0500297 public void migrateLauncher2Shortcuts() {
298 mOpenHelper.migrateLauncher2Shortcuts(mOpenHelper.getWritableDatabase(),
299 LauncherSettings.Favorites.OLD_CONTENT_URI);
300 }
301
Nilesh Agrawalda41ea62013-12-09 14:17:49 -0800302 private static int getDefaultWorkspaceResourceId() {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800303 if (LauncherAppState.isDisableAllApps()) {
Nilesh Agrawalda41ea62013-12-09 14:17:49 -0800304 return R.xml.default_workspace_no_all_apps;
305 } else {
306 return R.xml.default_workspace;
307 }
308 }
309
Winson Chungc763c4e2013-07-19 13:49:06 -0700310 private static interface ContentValuesCallback {
311 public void onRow(ContentValues values);
312 }
313
Adam Cohen6dbe0492013-12-02 17:00:14 -0800314 private static boolean shouldImportLauncher2Database(Context context) {
315 boolean isTablet = context.getResources().getBoolean(R.bool.is_tablet);
316
317 // We don't import the old databse for tablets, as the grid size has changed.
318 return !isTablet && IMPORT_LAUNCHER2_DATABASE;
319 }
320
Dan Sandlerd5024042014-01-09 15:01:33 -0500321 public void deleteDatabase() {
322 // Are you sure? (y/n)
323 final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
324 final String dbFile = db.getPath();
325 mOpenHelper.close();
326 SQLiteDatabase.deleteDatabase(new File(dbFile));
327 mOpenHelper = new DatabaseHelper(getContext());
328 }
329
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800330 private static class DatabaseHelper extends SQLiteOpenHelper {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800331 private static final String TAG_FAVORITES = "favorites";
332 private static final String TAG_FAVORITE = "favorite";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700333 private static final String TAG_CLOCK = "clock";
334 private static final String TAG_SEARCH = "search";
Mike Cleronb87bd162009-10-30 16:36:56 -0700335 private static final String TAG_APPWIDGET = "appwidget";
336 private static final String TAG_SHORTCUT = "shortcut";
Adam Cohen228da5a2011-07-27 22:23:47 -0700337 private static final String TAG_FOLDER = "folder";
Winson Chungb3302ae2012-05-01 10:19:14 -0700338 private static final String TAG_EXTRA = "extra";
Daniel Sandler57dac262013-10-03 13:28:36 -0400339 private static final String TAG_INCLUDE = "include";
Winson Chung3d503fb2011-07-13 17:25:49 -0700340
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800341 private final Context mContext;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700342 private final AppWidgetHost mAppWidgetHost;
Adam Cohendcd297f2013-06-18 13:13:40 -0700343 private long mMaxItemId = -1;
344 private long mMaxScreenId = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800345
346 DatabaseHelper(Context context) {
347 super(context, DATABASE_NAME, null, DATABASE_VERSION);
348 mContext = context;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700349 mAppWidgetHost = new AppWidgetHost(context, Launcher.APPWIDGET_HOST_ID);
Winson Chung3d503fb2011-07-13 17:25:49 -0700350
351 // In the case where neither onCreate nor onUpgrade gets called, we read the maxId from
352 // the DB here
Adam Cohendcd297f2013-06-18 13:13:40 -0700353 if (mMaxItemId == -1) {
354 mMaxItemId = initializeMaxItemId(getWritableDatabase());
355 }
356 if (mMaxScreenId == -1) {
357 mMaxScreenId = initializeMaxScreenId(getWritableDatabase());
Winson Chung3d503fb2011-07-13 17:25:49 -0700358 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 }
360
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700361 /**
362 * Send notification that we've deleted the {@link AppWidgetHost},
363 * probably as part of the initial database creation. The receiver may
364 * want to re-call {@link AppWidgetHost#startListening()} to ensure
365 * callbacks are correctly set.
366 */
367 private void sendAppWidgetResetNotify() {
368 final ContentResolver resolver = mContext.getContentResolver();
369 resolver.notifyChange(CONTENT_APPWIDGET_RESET_URI, null);
370 }
371
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800372 @Override
373 public void onCreate(SQLiteDatabase db) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800374 if (LOGD) Log.d(TAG, "creating new launcher database");
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700375
Adam Cohendcd297f2013-06-18 13:13:40 -0700376 mMaxItemId = 1;
377 mMaxScreenId = 0;
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700378
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 db.execSQL("CREATE TABLE favorites (" +
380 "_id INTEGER PRIMARY KEY," +
381 "title TEXT," +
382 "intent TEXT," +
383 "container INTEGER," +
384 "screen INTEGER," +
385 "cellX INTEGER," +
386 "cellY INTEGER," +
387 "spanX INTEGER," +
388 "spanY INTEGER," +
389 "itemType INTEGER," +
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700390 "appWidgetId INTEGER NOT NULL DEFAULT -1," +
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391 "isShortcut INTEGER," +
392 "iconType INTEGER," +
393 "iconPackage TEXT," +
394 "iconResource TEXT," +
395 "icon BLOB," +
396 "uri TEXT," +
Chris Wrend5e66bf2013-09-16 14:02:29 -0400397 "displayMode INTEGER," +
Chris Wren1ada10d2013-09-13 18:01:38 -0400398 "appWidgetProvider TEXT," +
399 "modified INTEGER NOT NULL DEFAULT 0" +
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 ");");
Adam Cohendcd297f2013-06-18 13:13:40 -0700401 addWorkspacesTable(db);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800402
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700403 // Database was just created, so wipe any previous widgets
404 if (mAppWidgetHost != null) {
405 mAppWidgetHost.deleteHost();
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700406 sendAppWidgetResetNotify();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700408
Adam Cohen6dbe0492013-12-02 17:00:14 -0800409 if (shouldImportLauncher2Database(mContext)) {
Dan Sandlerf0b8dac2013-11-19 12:21:25 -0500410 // Try converting the old database
411 ContentValuesCallback permuteScreensCb = new ContentValuesCallback() {
412 public void onRow(ContentValues values) {
413 int container = values.getAsInteger(LauncherSettings.Favorites.CONTAINER);
414 if (container == Favorites.CONTAINER_DESKTOP) {
415 int screen = values.getAsInteger(LauncherSettings.Favorites.SCREEN);
416 screen = (int) upgradeLauncherDb_permuteScreens(screen);
417 values.put(LauncherSettings.Favorites.SCREEN, screen);
418 }
419 }
420 };
421 Uri uri = Uri.parse("content://" + Settings.AUTHORITY +
422 "/old_favorites?notify=true");
423 if (!convertDatabase(db, uri, permuteScreensCb, true)) {
424 // Try and upgrade from the Launcher2 db
425 uri = LauncherSettings.Favorites.OLD_CONTENT_URI;
426 if (!convertDatabase(db, uri, permuteScreensCb, false)) {
427 // If we fail, then set a flag to load the default workspace
428 setFlagEmptyDbCreated();
429 return;
Winson Chungc763c4e2013-07-19 13:49:06 -0700430 }
431 }
Dan Sandlerf0b8dac2013-11-19 12:21:25 -0500432 // Right now, in non-default workspace cases, we want to run the final
433 // upgrade code (ie. to fix workspace screen indices -> ids, etc.), so
434 // set that flag too.
435 setFlagJustLoadedOldDb();
436 } else {
437 // Fresh and clean launcher DB.
438 mMaxItemId = initializeMaxItemId(db);
439 setFlagEmptyDbCreated();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 }
441 }
442
Adam Cohendcd297f2013-06-18 13:13:40 -0700443 private void addWorkspacesTable(SQLiteDatabase db) {
444 db.execSQL("CREATE TABLE " + TABLE_WORKSPACE_SCREENS + " (" +
445 LauncherSettings.WorkspaceScreens._ID + " INTEGER," +
Chris Wren1ada10d2013-09-13 18:01:38 -0400446 LauncherSettings.WorkspaceScreens.SCREEN_RANK + " INTEGER," +
447 LauncherSettings.ChangeLogColumns.MODIFIED + " INTEGER NOT NULL DEFAULT 0" +
Adam Cohendcd297f2013-06-18 13:13:40 -0700448 ");");
449 }
450
Winson Chungc763c4e2013-07-19 13:49:06 -0700451 private void setFlagJustLoadedOldDb() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400452 String spKey = LauncherAppState.getSharedPreferencesKey();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700453 SharedPreferences sp = mContext.getSharedPreferences(spKey, Context.MODE_PRIVATE);
454 SharedPreferences.Editor editor = sp.edit();
Winson Chungc763c4e2013-07-19 13:49:06 -0700455 editor.putBoolean(UPGRADED_FROM_OLD_DATABASE, true);
456 editor.putBoolean(EMPTY_DATABASE_CREATED, false);
Michael Jurkab85f8a42012-04-25 15:48:32 -0700457 editor.commit();
458 }
459
Winson Chungc763c4e2013-07-19 13:49:06 -0700460 private void setFlagEmptyDbCreated() {
461 String spKey = LauncherAppState.getSharedPreferencesKey();
462 SharedPreferences sp = mContext.getSharedPreferences(spKey, Context.MODE_PRIVATE);
463 SharedPreferences.Editor editor = sp.edit();
464 editor.putBoolean(EMPTY_DATABASE_CREATED, true);
465 editor.putBoolean(UPGRADED_FROM_OLD_DATABASE, false);
466 editor.commit();
467 }
468
469 // We rearrange the screens from the old launcher
470 // 12345 -> 34512
471 private long upgradeLauncherDb_permuteScreens(long screen) {
472 if (screen >= 2) {
473 return screen - 2;
474 } else {
475 return screen + 3;
476 }
477 }
478
479 private boolean convertDatabase(SQLiteDatabase db, Uri uri,
480 ContentValuesCallback cb, boolean deleteRows) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800481 if (LOGD) Log.d(TAG, "converting database from an older format, but not onUpgrade");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800482 boolean converted = false;
483
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800484 final ContentResolver resolver = mContext.getContentResolver();
485 Cursor cursor = null;
486
487 try {
488 cursor = resolver.query(uri, null, null, null, null);
489 } catch (Exception e) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700490 // Ignore
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800491 }
492
493 // We already have a favorites database in the old provider
Winson Chungc763c4e2013-07-19 13:49:06 -0700494 if (cursor != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800495 try {
Winson Chungc763c4e2013-07-19 13:49:06 -0700496 if (cursor.getCount() > 0) {
497 converted = copyFromCursor(db, cursor, cb) > 0;
498 if (converted && deleteRows) {
499 resolver.delete(uri, null, null);
500 }
501 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800502 } finally {
503 cursor.close();
504 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800505 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700506
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800507 if (converted) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700508 // Convert widgets from this import into widgets
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800509 if (LOGD) Log.d(TAG, "converted and now triggering widget upgrade");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800510 convertWidgets(db);
Winson Chungc763c4e2013-07-19 13:49:06 -0700511
512 // Update max item id
513 mMaxItemId = initializeMaxItemId(db);
514 if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800515 }
516
517 return converted;
518 }
519
Winson Chungc763c4e2013-07-19 13:49:06 -0700520 private int copyFromCursor(SQLiteDatabase db, Cursor c, ContentValuesCallback cb) {
Romain Guy73b979d2009-06-09 12:57:21 -0700521 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800522 final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
523 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
524 final int iconTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
525 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
526 final int iconPackageIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
527 final int iconResourceIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
528 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
529 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
530 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
531 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
532 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
533 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
534 final int displayModeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
535
536 ContentValues[] rows = new ContentValues[c.getCount()];
537 int i = 0;
538 while (c.moveToNext()) {
539 ContentValues values = new ContentValues(c.getColumnCount());
Romain Guy73b979d2009-06-09 12:57:21 -0700540 values.put(LauncherSettings.Favorites._ID, c.getLong(idIndex));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800541 values.put(LauncherSettings.Favorites.INTENT, c.getString(intentIndex));
542 values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex));
543 values.put(LauncherSettings.Favorites.ICON_TYPE, c.getInt(iconTypeIndex));
544 values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex));
545 values.put(LauncherSettings.Favorites.ICON_PACKAGE, c.getString(iconPackageIndex));
546 values.put(LauncherSettings.Favorites.ICON_RESOURCE, c.getString(iconResourceIndex));
547 values.put(LauncherSettings.Favorites.CONTAINER, c.getInt(containerIndex));
548 values.put(LauncherSettings.Favorites.ITEM_TYPE, c.getInt(itemTypeIndex));
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700549 values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800550 values.put(LauncherSettings.Favorites.SCREEN, c.getInt(screenIndex));
551 values.put(LauncherSettings.Favorites.CELLX, c.getInt(cellXIndex));
552 values.put(LauncherSettings.Favorites.CELLY, c.getInt(cellYIndex));
553 values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex));
554 values.put(LauncherSettings.Favorites.DISPLAY_MODE, c.getInt(displayModeIndex));
Winson Chungc763c4e2013-07-19 13:49:06 -0700555 if (cb != null) {
556 cb.onRow(values);
557 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800558 rows[i++] = values;
559 }
560
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561 int total = 0;
Winson Chungc763c4e2013-07-19 13:49:06 -0700562 if (i > 0) {
563 db.beginTransaction();
564 try {
565 int numValues = rows.length;
566 for (i = 0; i < numValues; i++) {
567 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, rows[i]) < 0) {
568 return 0;
569 } else {
570 total++;
571 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800572 }
Winson Chungc763c4e2013-07-19 13:49:06 -0700573 db.setTransactionSuccessful();
574 } finally {
575 db.endTransaction();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800576 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800577 }
578
579 return total;
580 }
581
582 @Override
583 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Winson Chungc763c4e2013-07-19 13:49:06 -0700584 if (LOGD) Log.d(TAG, "onUpgrade triggered: " + oldVersion);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700585
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800586 int version = oldVersion;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700587 if (version < 3) {
588 // upgrade 1,2 -> 3 added appWidgetId column
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800589 db.beginTransaction();
590 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700591 // Insert new column for holding appWidgetIds
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800592 db.execSQL("ALTER TABLE favorites " +
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700593 "ADD COLUMN appWidgetId INTEGER NOT NULL DEFAULT -1;");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800594 db.setTransactionSuccessful();
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700595 version = 3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800596 } catch (SQLException ex) {
597 // Old version remains, which means we wipe old data
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800598 Log.e(TAG, ex.getMessage(), ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800599 } finally {
600 db.endTransaction();
601 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700602
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800603 // Convert existing widgets only if table upgrade was successful
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700604 if (version == 3) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605 convertWidgets(db);
606 }
607 }
Romain Guy73b979d2009-06-09 12:57:21 -0700608
609 if (version < 4) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800610 version = 4;
Romain Guy73b979d2009-06-09 12:57:21 -0700611 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700612
Romain Guy509cd6a2010-03-23 15:10:56 -0700613 // Where's version 5?
614 // - Donut and sholes on 2.0 shipped with version 4 of launcher1.
Daniel Sandler325dc232013-06-05 22:57:57 -0400615 // - Passion shipped on 2.1 with version 6 of launcher3
Romain Guy509cd6a2010-03-23 15:10:56 -0700616 // - Sholes shipped on 2.1r1 (aka Mr. 3) with version 5 of launcher 1
617 // but version 5 on there was the updateContactsShortcuts change
618 // which was version 6 in launcher 2 (first shipped on passion 2.1r1).
619 // The updateContactsShortcuts change is idempotent, so running it twice
620 // is okay so we'll do that when upgrading the devices that shipped with it.
621 if (version < 6) {
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800622 // We went from 3 to 5 screens. Move everything 1 to the right
623 db.beginTransaction();
624 try {
625 db.execSQL("UPDATE favorites SET screen=(screen + 1);");
626 db.setTransactionSuccessful();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800627 } catch (SQLException ex) {
628 // Old version remains, which means we wipe old data
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800629 Log.e(TAG, ex.getMessage(), ex);
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800630 } finally {
631 db.endTransaction();
632 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700633
Romain Guy509cd6a2010-03-23 15:10:56 -0700634 // We added the fast track.
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800635 if (updateContactsShortcuts(db)) {
636 version = 6;
637 }
638 }
Bjorn Bringert7984c942009-12-09 15:38:25 +0000639
640 if (version < 7) {
641 // Version 7 gets rid of the special search widget.
642 convertWidgets(db);
643 version = 7;
644 }
645
Joe Onorato0589f0f2010-02-08 13:44:00 -0800646 if (version < 8) {
647 // Version 8 (froyo) has the icons all normalized. This should
648 // already be the case in practice, but we now rely on it and don't
649 // resample the images each time.
650 normalizeIcons(db);
651 version = 8;
652 }
653
Winson Chung3d503fb2011-07-13 17:25:49 -0700654 if (version < 9) {
655 // The max id is not yet set at this point (onUpgrade is triggered in the ctor
656 // 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 -0700657 if (mMaxItemId == -1) {
658 mMaxItemId = initializeMaxItemId(db);
Winson Chung3d503fb2011-07-13 17:25:49 -0700659 }
660
661 // Add default hotseat icons
Winson Chung6d092682011-11-16 18:43:26 -0800662 loadFavorites(db, R.xml.update_workspace);
Winson Chung3d503fb2011-07-13 17:25:49 -0700663 version = 9;
664 }
665
Daniel Lehmannd02402c2012-05-14 18:30:53 -0700666 // We bumped the version three time during JB, once to update the launch flags, once to
667 // update the override for the default launch animation and once to set the mimetype
668 // to improve startup performance
669 if (version < 12) {
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700670 // Contact shortcuts need a different set of flags to be launched now
671 // The updateContactsShortcuts change is idempotent, so we can keep using it like
672 // back in the Donut days
673 updateContactsShortcuts(db);
Daniel Lehmannd02402c2012-05-14 18:30:53 -0700674 version = 12;
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700675 }
676
Adam Cohendcd297f2013-06-18 13:13:40 -0700677 if (version < 13) {
678 // With the new shrink-wrapped and re-orderable workspaces, it makes sense
679 // to persist workspace screens and their relative order.
680 mMaxScreenId = 0;
681
682 // This will never happen in the wild, but when we switch to using workspace
683 // screen ids, redo the import from old launcher.
Winson Chungc763c4e2013-07-19 13:49:06 -0700684 sJustLoadedFromOldDb = true;
Adam Cohendcd297f2013-06-18 13:13:40 -0700685
686 addWorkspacesTable(db);
687 version = 13;
688 }
689
Chris Wrend5e66bf2013-09-16 14:02:29 -0400690 if (version < 14) {
691 db.beginTransaction();
692 try {
693 // Insert new column for holding widget provider name
694 db.execSQL("ALTER TABLE favorites " +
695 "ADD COLUMN appWidgetProvider TEXT;");
696 db.setTransactionSuccessful();
697 version = 14;
698 } catch (SQLException ex) {
699 // Old version remains, which means we wipe old data
700 Log.e(TAG, ex.getMessage(), ex);
701 } finally {
702 db.endTransaction();
703 }
704 }
705
Chris Wren1ada10d2013-09-13 18:01:38 -0400706
707 if (version < 15) {
708 db.beginTransaction();
709 try {
710 // Insert new column for holding update timestamp
711 db.execSQL("ALTER TABLE favorites " +
712 "ADD COLUMN modified INTEGER NOT NULL DEFAULT 0;");
713 db.execSQL("ALTER TABLE workspaceScreens " +
714 "ADD COLUMN modified INTEGER NOT NULL DEFAULT 0;");
715 db.setTransactionSuccessful();
716 version = 15;
717 } catch (SQLException ex) {
718 // Old version remains, which means we wipe old data
719 Log.e(TAG, ex.getMessage(), ex);
720 } finally {
721 db.endTransaction();
722 }
723 }
724
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 if (version != DATABASE_VERSION) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800726 Log.w(TAG, "Destroying all old data.");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES);
Adam Cohendcd297f2013-06-18 13:13:40 -0700728 db.execSQL("DROP TABLE IF EXISTS " + TABLE_WORKSPACE_SCREENS);
729
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730 onCreate(db);
731 }
732 }
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800733
734 private boolean updateContactsShortcuts(SQLiteDatabase db) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800735 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE,
736 new int[] { Favorites.ITEM_TYPE_SHORTCUT });
737
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700738 Cursor c = null;
739 final String actionQuickContact = "com.android.contacts.action.QUICK_CONTACT";
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800740 db.beginTransaction();
741 try {
742 // Select and iterate through each matching widget
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700743 c = db.query(TABLE_FAVORITES,
744 new String[] { Favorites._ID, Favorites.INTENT },
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800745 selectWhere, null, null, null, null);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700746 if (c == null) return false;
747
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800748 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700749
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800750 final int idIndex = c.getColumnIndex(Favorites._ID);
751 final int intentIndex = c.getColumnIndex(Favorites.INTENT);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700752
753 while (c.moveToNext()) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800754 long favoriteId = c.getLong(idIndex);
755 final String intentUri = c.getString(intentIndex);
756 if (intentUri != null) {
757 try {
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700758 final Intent intent = Intent.parseUri(intentUri, 0);
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800759 android.util.Log.d("Home", intent.toString());
760 final Uri uri = intent.getData();
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700761 if (uri != null) {
762 final String data = uri.toString();
763 if ((Intent.ACTION_VIEW.equals(intent.getAction()) ||
764 actionQuickContact.equals(intent.getAction())) &&
765 (data.startsWith("content://contacts/people/") ||
766 data.startsWith("content://com.android.contacts/" +
767 "contacts/lookup/"))) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800768
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700769 final Intent newIntent = new Intent(actionQuickContact);
770 // When starting from the launcher, start in a new, cleared task
771 // CLEAR_WHEN_TASK_RESET cannot reset the root of a task, so we
772 // clear the whole thing preemptively here since
773 // QuickContactActivity will finish itself when launching other
774 // detail activities.
775 newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
776 Intent.FLAG_ACTIVITY_CLEAR_TASK);
Winson Chung2672ff92012-05-04 16:22:30 -0700777 newIntent.putExtra(
778 Launcher.INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION, true);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700779 newIntent.setData(uri);
Daniel Lehmannd02402c2012-05-14 18:30:53 -0700780 // Determine the type and also put that in the shortcut
781 // (that can speed up launch a bit)
782 newIntent.setDataAndType(uri, newIntent.resolveType(mContext));
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800783
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700784 final ContentValues values = new ContentValues();
785 values.put(LauncherSettings.Favorites.INTENT,
786 newIntent.toUri(0));
787
788 String updateWhere = Favorites._ID + "=" + favoriteId;
789 db.update(TABLE_FAVORITES, values, updateWhere, null);
790 }
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800791 }
792 } catch (RuntimeException ex) {
793 Log.e(TAG, "Problem upgrading shortcut", ex);
794 } catch (URISyntaxException e) {
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700795 Log.e(TAG, "Problem upgrading shortcut", e);
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800796 }
797 }
798 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700799
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800800 db.setTransactionSuccessful();
801 } catch (SQLException ex) {
802 Log.w(TAG, "Problem while upgrading contacts", ex);
803 return false;
804 } finally {
805 db.endTransaction();
806 if (c != null) {
807 c.close();
808 }
809 }
810
811 return true;
812 }
813
Joe Onorato0589f0f2010-02-08 13:44:00 -0800814 private void normalizeIcons(SQLiteDatabase db) {
815 Log.d(TAG, "normalizing icons");
816
Joe Onorato346e1292010-02-18 10:34:24 -0500817 db.beginTransaction();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800818 Cursor c = null;
Joe Onorato9690b392010-03-23 17:34:37 -0400819 SQLiteStatement update = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800820 try {
821 boolean logged = false;
Joe Onorato9690b392010-03-23 17:34:37 -0400822 update = db.compileStatement("UPDATE favorites "
Jeff Hamiltoneaf77d62010-02-13 00:08:17 -0600823 + "SET icon=? WHERE _id=?");
Joe Onorato0589f0f2010-02-08 13:44:00 -0800824
825 c = db.rawQuery("SELECT _id, icon FROM favorites WHERE iconType=" +
826 Favorites.ICON_TYPE_BITMAP, null);
827
828 final int idIndex = c.getColumnIndexOrThrow(Favorites._ID);
829 final int iconIndex = c.getColumnIndexOrThrow(Favorites.ICON);
830
831 while (c.moveToNext()) {
832 long id = c.getLong(idIndex);
833 byte[] data = c.getBlob(iconIndex);
834 try {
835 Bitmap bitmap = Utilities.resampleIconBitmap(
836 BitmapFactory.decodeByteArray(data, 0, data.length),
837 mContext);
838 if (bitmap != null) {
839 update.bindLong(1, id);
840 data = ItemInfo.flattenBitmap(bitmap);
841 if (data != null) {
842 update.bindBlob(2, data);
843 update.execute();
844 }
845 bitmap.recycle();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800846 }
847 } catch (Exception e) {
848 if (!logged) {
849 Log.e(TAG, "Failed normalizing icon " + id, e);
850 } else {
851 Log.e(TAG, "Also failed normalizing icon " + id);
852 }
853 logged = true;
854 }
855 }
Bjorn Bringert3a928e42010-02-19 11:15:40 +0000856 db.setTransactionSuccessful();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800857 } catch (SQLException ex) {
858 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
859 } finally {
860 db.endTransaction();
Joe Onorato9690b392010-03-23 17:34:37 -0400861 if (update != null) {
862 update.close();
863 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800864 if (c != null) {
865 c.close();
866 }
867 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700868 }
869
870 // Generates a new ID to use for an object in your database. This method should be only
871 // called from the main UI thread. As an exception, we do call it when we call the
872 // constructor from the worker thread; however, this doesn't extend until after the
873 // constructor is called, and we only pass a reference to LauncherProvider to LauncherApp
874 // after that point
Adam Cohendcd297f2013-06-18 13:13:40 -0700875 public long generateNewItemId() {
876 if (mMaxItemId < 0) {
877 throw new RuntimeException("Error: max item id was not initialized");
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700878 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700879 mMaxItemId += 1;
880 return mMaxItemId;
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700881 }
882
Winson Chungc763c4e2013-07-19 13:49:06 -0700883 public void updateMaxItemId(long id) {
884 mMaxItemId = id + 1;
885 }
886
Chris Wren5dee7af2013-12-20 17:22:11 -0500887 public void checkId(String table, ContentValues values) {
888 long id = values.getAsLong(LauncherSettings.BaseLauncherColumns._ID);
889 if (table == LauncherProvider.TABLE_WORKSPACE_SCREENS) {
890 mMaxScreenId = Math.max(id, mMaxScreenId);
891 } else {
892 mMaxItemId = Math.max(id, mMaxItemId);
893 }
894 }
895
Adam Cohendcd297f2013-06-18 13:13:40 -0700896 private long initializeMaxItemId(SQLiteDatabase db) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700897 Cursor c = db.rawQuery("SELECT MAX(_id) FROM favorites", null);
898
899 // get the result
900 final int maxIdIndex = 0;
901 long id = -1;
902 if (c != null && c.moveToNext()) {
903 id = c.getLong(maxIdIndex);
904 }
Michael Jurka5130e402011-10-13 04:55:35 -0700905 if (c != null) {
906 c.close();
907 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700908
909 if (id == -1) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700910 throw new RuntimeException("Error: could not query max item id");
911 }
912
913 return id;
914 }
915
916 // Generates a new ID to use for an workspace screen in your database. This method
917 // should be only called from the main UI thread. As an exception, we do call it when we
918 // call the constructor from the worker thread; however, this doesn't extend until after the
919 // constructor is called, and we only pass a reference to LauncherProvider to LauncherApp
920 // after that point
921 public long generateNewScreenId() {
922 if (mMaxScreenId < 0) {
923 throw new RuntimeException("Error: max screen id was not initialized");
924 }
925 mMaxScreenId += 1;
Winson Chunga90303b2013-11-15 13:05:06 -0800926 // Log to disk
927 Launcher.addDumpLog(TAG, "11683562 - generateNewScreenId(): " + mMaxScreenId, true);
Adam Cohendcd297f2013-06-18 13:13:40 -0700928 return mMaxScreenId;
929 }
930
931 public void updateMaxScreenId(long maxScreenId) {
Winson Chunga90303b2013-11-15 13:05:06 -0800932 // Log to disk
933 Launcher.addDumpLog(TAG, "11683562 - updateMaxScreenId(): " + maxScreenId, true);
Adam Cohendcd297f2013-06-18 13:13:40 -0700934 mMaxScreenId = maxScreenId;
935 }
936
937 private long initializeMaxScreenId(SQLiteDatabase db) {
938 Cursor c = db.rawQuery("SELECT MAX(" + LauncherSettings.WorkspaceScreens._ID + ") FROM " + TABLE_WORKSPACE_SCREENS, null);
939
940 // get the result
941 final int maxIdIndex = 0;
942 long id = -1;
943 if (c != null && c.moveToNext()) {
944 id = c.getLong(maxIdIndex);
945 }
946 if (c != null) {
947 c.close();
948 }
949
950 if (id == -1) {
951 throw new RuntimeException("Error: could not query max screen id");
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700952 }
953
Winson Chunga90303b2013-11-15 13:05:06 -0800954 // Log to disk
955 Launcher.addDumpLog(TAG, "11683562 - initializeMaxScreenId(): " + id, true);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700956 return id;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800957 }
958
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700960 * Upgrade existing clock and photo frame widgets into their new widget
Bjorn Bringert93c45762009-12-16 13:19:47 +0000961 * equivalents.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800962 */
963 private void convertWidgets(SQLiteDatabase db) {
Bjorn Bringert34251342009-12-15 13:33:11 +0000964 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 final int[] bindSources = new int[] {
966 Favorites.ITEM_TYPE_WIDGET_CLOCK,
967 Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME,
Bjorn Bringert7984c942009-12-09 15:38:25 +0000968 Favorites.ITEM_TYPE_WIDGET_SEARCH,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969 };
Bjorn Bringert7984c942009-12-09 15:38:25 +0000970
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE, bindSources);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700972
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800973 Cursor c = null;
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700974
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 db.beginTransaction();
976 try {
977 // Select and iterate through each matching widget
Bjorn Bringert7984c942009-12-09 15:38:25 +0000978 c = db.query(TABLE_FAVORITES, new String[] { Favorites._ID, Favorites.ITEM_TYPE },
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800979 selectWhere, null, null, null, null);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700980
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800981 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700982
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800983 final ContentValues values = new ContentValues();
984 while (c != null && c.moveToNext()) {
985 long favoriteId = c.getLong(0);
Bjorn Bringert7984c942009-12-09 15:38:25 +0000986 int favoriteType = c.getInt(1);
987
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700988 // Allocate and update database with new appWidgetId
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700990 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700991
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800992 if (LOGD) {
993 Log.d(TAG, "allocated appWidgetId=" + appWidgetId
994 + " for favoriteId=" + favoriteId);
995 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 values.clear();
Bjorn Bringert7984c942009-12-09 15:38:25 +0000997 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
998 values.put(Favorites.APPWIDGET_ID, appWidgetId);
999
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001000 // Original widgets might not have valid spans when upgrading
Bjorn Bringert7984c942009-12-09 15:38:25 +00001001 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
1002 values.put(LauncherSettings.Favorites.SPANX, 4);
1003 values.put(LauncherSettings.Favorites.SPANY, 1);
1004 } else {
1005 values.put(LauncherSettings.Favorites.SPANX, 2);
1006 values.put(LauncherSettings.Favorites.SPANY, 2);
1007 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001008
1009 String updateWhere = Favorites._ID + "=" + favoriteId;
1010 db.update(TABLE_FAVORITES, values, updateWhere, null);
Bjorn Bringert34251342009-12-15 13:33:11 +00001011
Bjorn Bringert34251342009-12-15 13:33:11 +00001012 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_CLOCK) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001013 // TODO: check return value
1014 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
Bjorn Bringert34251342009-12-15 13:33:11 +00001015 new ComponentName("com.android.alarmclock",
1016 "com.android.alarmclock.AnalogAppWidgetProvider"));
1017 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001018 // TODO: check return value
1019 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
Bjorn Bringert34251342009-12-15 13:33:11 +00001020 new ComponentName("com.android.camera",
1021 "com.android.camera.PhotoAppWidgetProvider"));
1022 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001023 // TODO: check return value
1024 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
Bjorn Bringertcd8fec02010-01-14 13:26:43 +00001025 getSearchWidgetProvider());
Bjorn Bringert34251342009-12-15 13:33:11 +00001026 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001027 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001028 Log.e(TAG, "Problem allocating appWidgetId", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001029 }
1030 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001031
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001032 db.setTransactionSuccessful();
1033 } catch (SQLException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001034 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035 } finally {
1036 db.endTransaction();
1037 if (c != null) {
1038 c.close();
1039 }
1040 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001041
1042 // Update max item id
1043 mMaxItemId = initializeMaxItemId(db);
1044 if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001045 }
1046
Michael Jurka8b805b12012-04-18 14:23:14 -07001047 private static final void beginDocument(XmlPullParser parser, String firstElementName)
1048 throws XmlPullParserException, IOException {
1049 int type;
Michael Jurka9bc8eba2012-05-21 20:36:44 -07001050 while ((type = parser.next()) != XmlPullParser.START_TAG
1051 && type != XmlPullParser.END_DOCUMENT) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001052 ;
1053 }
1054
Michael Jurka9bc8eba2012-05-21 20:36:44 -07001055 if (type != XmlPullParser.START_TAG) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001056 throw new XmlPullParserException("No start tag found");
1057 }
1058
1059 if (!parser.getName().equals(firstElementName)) {
1060 throw new XmlPullParserException("Unexpected start tag: found " + parser.getName() +
1061 ", expected " + firstElementName);
1062 }
1063 }
1064
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001065 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066 * Loads the default set of favorite packages from an xml file.
1067 *
1068 * @param db The database to write the values into
Winson Chung3d503fb2011-07-13 17:25:49 -07001069 * @param filterContainerId The specific container id of items to load
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001070 */
Winson Chung6d092682011-11-16 18:43:26 -08001071 private int loadFavorites(SQLiteDatabase db, int workspaceResourceId) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001072 Intent intent = new Intent(Intent.ACTION_MAIN, null);
1073 intent.addCategory(Intent.CATEGORY_LAUNCHER);
1074 ContentValues values = new ContentValues();
1075
Daniel Sandler57dac262013-10-03 13:28:36 -04001076 if (LOGD) Log.v(TAG, String.format("Loading favorites from resid=0x%08x", workspaceResourceId));
1077
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001078 PackageManager packageManager = mContext.getPackageManager();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079 int i = 0;
1080 try {
Winson Chung6d092682011-11-16 18:43:26 -08001081 XmlResourceParser parser = mContext.getResources().getXml(workspaceResourceId);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001082 AttributeSet attrs = Xml.asAttributeSet(parser);
Michael Jurka8b805b12012-04-18 14:23:14 -07001083 beginDocument(parser, TAG_FAVORITES);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001085 final int depth = parser.getDepth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001087 int type;
1088 while (((type = parser.next()) != XmlPullParser.END_TAG ||
1089 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
1090
1091 if (type != XmlPullParser.START_TAG) {
1092 continue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 }
1094
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001095 boolean added = false;
1096 final String name = parser.getName();
1097
Daniel Sandler57dac262013-10-03 13:28:36 -04001098 if (TAG_INCLUDE.equals(name)) {
1099 final TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.Include);
1100
1101 final int resId = a.getResourceId(R.styleable.Include_workspace, 0);
1102
1103 if (LOGD) Log.v(TAG, String.format(("%" + (2*(depth+1)) + "s<include workspace=%08x>"),
1104 "", resId));
1105
1106 if (resId != 0 && resId != workspaceResourceId) {
1107 // recursively load some more favorites, why not?
1108 i += loadFavorites(db, resId);
1109 added = false;
Daniel Sandler57dac262013-10-03 13:28:36 -04001110 } else {
1111 Log.w(TAG, String.format("Skipping <include workspace=0x%08x>", resId));
1112 }
1113
1114 a.recycle();
1115
1116 if (LOGD) Log.v(TAG, String.format(("%" + (2*(depth+1)) + "s</include>"), ""));
1117 continue;
1118 }
1119
1120 // Assuming it's a <favorite> at this point
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001121 TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.Favorite);
1122
Winson Chung3d503fb2011-07-13 17:25:49 -07001123 long container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
1124 if (a.hasValue(R.styleable.Favorite_container)) {
1125 container = Long.valueOf(a.getString(R.styleable.Favorite_container));
1126 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001127
Winson Chung6d092682011-11-16 18:43:26 -08001128 String screen = a.getString(R.styleable.Favorite_screen);
1129 String x = a.getString(R.styleable.Favorite_x);
1130 String y = a.getString(R.styleable.Favorite_y);
1131
Winson Chung6d092682011-11-16 18:43:26 -08001132 values.clear();
1133 values.put(LauncherSettings.Favorites.CONTAINER, container);
1134 values.put(LauncherSettings.Favorites.SCREEN, screen);
1135 values.put(LauncherSettings.Favorites.CELLX, x);
1136 values.put(LauncherSettings.Favorites.CELLY, y);
1137
Daniel Sandler57dac262013-10-03 13:28:36 -04001138 if (LOGD) {
1139 final String title = a.getString(R.styleable.Favorite_title);
1140 final String pkg = a.getString(R.styleable.Favorite_packageName);
1141 final String something = title != null ? title : pkg;
1142 Log.v(TAG, String.format(
1143 ("%" + (2*(depth+1)) + "s<%s%s c=%d s=%s x=%s y=%s>"),
1144 "", name,
1145 (something == null ? "" : (" \"" + something + "\"")),
1146 container, screen, x, y));
1147 }
1148
Winson Chung6d092682011-11-16 18:43:26 -08001149 if (TAG_FAVORITE.equals(name)) {
1150 long id = addAppShortcut(db, values, a, packageManager, intent);
1151 added = id >= 0;
1152 } else if (TAG_SEARCH.equals(name)) {
1153 added = addSearchWidget(db, values);
1154 } else if (TAG_CLOCK.equals(name)) {
1155 added = addClockWidget(db, values);
1156 } else if (TAG_APPWIDGET.equals(name)) {
Winson Chungb3302ae2012-05-01 10:19:14 -07001157 added = addAppWidget(parser, attrs, type, db, values, a, packageManager);
Winson Chung6d092682011-11-16 18:43:26 -08001158 } else if (TAG_SHORTCUT.equals(name)) {
1159 long id = addUriShortcut(db, values, a);
1160 added = id >= 0;
1161 } else if (TAG_FOLDER.equals(name)) {
1162 String title;
1163 int titleResId = a.getResourceId(R.styleable.Favorite_title, -1);
1164 if (titleResId != -1) {
1165 title = mContext.getResources().getString(titleResId);
1166 } else {
1167 title = mContext.getResources().getString(R.string.folder_name);
Winson Chung3d503fb2011-07-13 17:25:49 -07001168 }
Winson Chung6d092682011-11-16 18:43:26 -08001169 values.put(LauncherSettings.Favorites.TITLE, title);
1170 long folderId = addFolder(db, values);
1171 added = folderId >= 0;
Winson Chung3d503fb2011-07-13 17:25:49 -07001172
Winson Chung6d092682011-11-16 18:43:26 -08001173 ArrayList<Long> folderItems = new ArrayList<Long>();
Winson Chung3d503fb2011-07-13 17:25:49 -07001174
Winson Chung6d092682011-11-16 18:43:26 -08001175 int folderDepth = parser.getDepth();
1176 while ((type = parser.next()) != XmlPullParser.END_TAG ||
1177 parser.getDepth() > folderDepth) {
1178 if (type != XmlPullParser.START_TAG) {
1179 continue;
1180 }
1181 final String folder_item_name = parser.getName();
1182
1183 TypedArray ar = mContext.obtainStyledAttributes(attrs,
1184 R.styleable.Favorite);
1185 values.clear();
1186 values.put(LauncherSettings.Favorites.CONTAINER, folderId);
1187
Daniel Sandler57dac262013-10-03 13:28:36 -04001188 if (LOGD) {
1189 final String pkg = ar.getString(R.styleable.Favorite_packageName);
1190 final String uri = ar.getString(R.styleable.Favorite_uri);
1191 Log.v(TAG, String.format(("%" + (2*(folderDepth+1)) + "s<%s \"%s\">"), "",
1192 folder_item_name, uri != null ? uri : pkg));
1193 }
1194
Winson Chung6d092682011-11-16 18:43:26 -08001195 if (TAG_FAVORITE.equals(folder_item_name) && folderId >= 0) {
1196 long id =
1197 addAppShortcut(db, values, ar, packageManager, intent);
1198 if (id >= 0) {
1199 folderItems.add(id);
1200 }
1201 } else if (TAG_SHORTCUT.equals(folder_item_name) && folderId >= 0) {
1202 long id = addUriShortcut(db, values, ar);
1203 if (id >= 0) {
1204 folderItems.add(id);
1205 }
Adam Cohen228da5a2011-07-27 22:23:47 -07001206 } else {
Winson Chung6d092682011-11-16 18:43:26 -08001207 throw new RuntimeException("Folders can " +
1208 "contain only shortcuts");
Adam Cohen228da5a2011-07-27 22:23:47 -07001209 }
Winson Chung6d092682011-11-16 18:43:26 -08001210 ar.recycle();
1211 }
1212 // We can only have folders with >= 2 items, so we need to remove the
1213 // folder and clean up if less than 2 items were included, or some
1214 // failed to add, and less than 2 were actually added
1215 if (folderItems.size() < 2 && folderId >= 0) {
1216 // We just delete the folder and any items that made it
1217 deleteId(db, folderId);
1218 if (folderItems.size() > 0) {
1219 deleteId(db, folderItems.get(0));
Adam Cohen228da5a2011-07-27 22:23:47 -07001220 }
Winson Chung6d092682011-11-16 18:43:26 -08001221 added = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001222 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001223 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001224 if (added) i++;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001225 a.recycle();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001226 }
1227 } catch (XmlPullParserException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001228 Log.w(TAG, "Got exception parsing favorites.", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001229 } catch (IOException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001230 Log.w(TAG, "Got exception parsing favorites.", e);
Winson Chung3d503fb2011-07-13 17:25:49 -07001231 } catch (RuntimeException e) {
1232 Log.w(TAG, "Got exception parsing favorites.", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001234
Winson Chungc763c4e2013-07-19 13:49:06 -07001235 // Update the max item id after we have loaded the database
1236 if (mMaxItemId == -1) {
1237 mMaxItemId = initializeMaxItemId(db);
1238 }
1239
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001240 return i;
1241 }
1242
Adam Cohen228da5a2011-07-27 22:23:47 -07001243 private long addAppShortcut(SQLiteDatabase db, ContentValues values, TypedArray a,
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001244 PackageManager packageManager, Intent intent) {
Adam Cohen228da5a2011-07-27 22:23:47 -07001245 long id = -1;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001246 ActivityInfo info;
1247 String packageName = a.getString(R.styleable.Favorite_packageName);
1248 String className = a.getString(R.styleable.Favorite_className);
1249 try {
Romain Guy693599f2010-03-23 10:58:18 -07001250 ComponentName cn;
1251 try {
1252 cn = new ComponentName(packageName, className);
1253 info = packageManager.getActivityInfo(cn, 0);
1254 } catch (PackageManager.NameNotFoundException nnfe) {
1255 String[] packages = packageManager.currentToCanonicalPackageNames(
1256 new String[] { packageName });
1257 cn = new ComponentName(packages[0], className);
1258 info = packageManager.getActivityInfo(cn, 0);
1259 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001260 id = generateNewItemId();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001261 intent.setComponent(cn);
Romain Guy693599f2010-03-23 10:58:18 -07001262 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1263 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Romain Guy1ce1a242009-06-23 17:34:54 -07001264 values.put(Favorites.INTENT, intent.toUri(0));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001265 values.put(Favorites.TITLE, info.loadLabel(packageManager).toString());
1266 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPLICATION);
1267 values.put(Favorites.SPANX, 1);
1268 values.put(Favorites.SPANY, 1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001269 values.put(Favorites._ID, generateNewItemId());
Adam Cohen228da5a2011-07-27 22:23:47 -07001270 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values) < 0) {
1271 return -1;
1272 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001273 } catch (PackageManager.NameNotFoundException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001274 Log.w(TAG, "Unable to add favorite: " + packageName +
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001275 "/" + className, e);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001276 }
Adam Cohen228da5a2011-07-27 22:23:47 -07001277 return id;
1278 }
1279
1280 private long addFolder(SQLiteDatabase db, ContentValues values) {
1281 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_FOLDER);
1282 values.put(Favorites.SPANX, 1);
1283 values.put(Favorites.SPANY, 1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001284 long id = generateNewItemId();
Adam Cohen228da5a2011-07-27 22:23:47 -07001285 values.put(Favorites._ID, id);
1286 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values) <= 0) {
1287 return -1;
1288 } else {
1289 return id;
1290 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001291 }
1292
Bjorn Bringertcd8fec02010-01-14 13:26:43 +00001293 private ComponentName getSearchWidgetProvider() {
1294 SearchManager searchManager =
1295 (SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE);
1296 ComponentName searchComponent = searchManager.getGlobalSearchActivity();
1297 if (searchComponent == null) return null;
1298 return getProviderInPackage(searchComponent.getPackageName());
1299 }
1300
1301 /**
1302 * Gets an appwidget provider from the given package. If the package contains more than
1303 * one appwidget provider, an arbitrary one is returned.
1304 */
1305 private ComponentName getProviderInPackage(String packageName) {
1306 AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
1307 List<AppWidgetProviderInfo> providers = appWidgetManager.getInstalledProviders();
1308 if (providers == null) return null;
1309 final int providerCount = providers.size();
1310 for (int i = 0; i < providerCount; i++) {
1311 ComponentName provider = providers.get(i).provider;
1312 if (provider != null && provider.getPackageName().equals(packageName)) {
1313 return provider;
1314 }
1315 }
1316 return null;
1317 }
1318
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001319 private boolean addSearchWidget(SQLiteDatabase db, ContentValues values) {
Bjorn Bringertcd8fec02010-01-14 13:26:43 +00001320 ComponentName cn = getSearchWidgetProvider();
Winson Chungb3302ae2012-05-01 10:19:14 -07001321 return addAppWidget(db, values, cn, 4, 1, null);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001322 }
1323
1324 private boolean addClockWidget(SQLiteDatabase db, ContentValues values) {
Bjorn Bringert34251342009-12-15 13:33:11 +00001325 ComponentName cn = new ComponentName("com.android.alarmclock",
1326 "com.android.alarmclock.AnalogAppWidgetProvider");
Winson Chungb3302ae2012-05-01 10:19:14 -07001327 return addAppWidget(db, values, cn, 2, 2, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001328 }
Adam Cohen228da5a2011-07-27 22:23:47 -07001329
Winson Chungb3302ae2012-05-01 10:19:14 -07001330 private boolean addAppWidget(XmlResourceParser parser, AttributeSet attrs, int type,
1331 SQLiteDatabase db, ContentValues values, TypedArray a,
1332 PackageManager packageManager) throws XmlPullParserException, IOException {
Romain Guy693599f2010-03-23 10:58:18 -07001333
Mike Cleronb87bd162009-10-30 16:36:56 -07001334 String packageName = a.getString(R.styleable.Favorite_packageName);
1335 String className = a.getString(R.styleable.Favorite_className);
1336
1337 if (packageName == null || className == null) {
1338 return false;
1339 }
Romain Guy693599f2010-03-23 10:58:18 -07001340
1341 boolean hasPackage = true;
Mike Cleronb87bd162009-10-30 16:36:56 -07001342 ComponentName cn = new ComponentName(packageName, className);
Romain Guy693599f2010-03-23 10:58:18 -07001343 try {
1344 packageManager.getReceiverInfo(cn, 0);
1345 } catch (Exception e) {
1346 String[] packages = packageManager.currentToCanonicalPackageNames(
1347 new String[] { packageName });
1348 cn = new ComponentName(packages[0], className);
1349 try {
1350 packageManager.getReceiverInfo(cn, 0);
1351 } catch (Exception e1) {
1352 hasPackage = false;
1353 }
1354 }
1355
1356 if (hasPackage) {
1357 int spanX = a.getInt(R.styleable.Favorite_spanX, 0);
1358 int spanY = a.getInt(R.styleable.Favorite_spanY, 0);
Winson Chungb3302ae2012-05-01 10:19:14 -07001359
1360 // Read the extras
1361 Bundle extras = new Bundle();
1362 int widgetDepth = parser.getDepth();
1363 while ((type = parser.next()) != XmlPullParser.END_TAG ||
1364 parser.getDepth() > widgetDepth) {
1365 if (type != XmlPullParser.START_TAG) {
1366 continue;
1367 }
1368
1369 TypedArray ar = mContext.obtainStyledAttributes(attrs, R.styleable.Extra);
1370 if (TAG_EXTRA.equals(parser.getName())) {
1371 String key = ar.getString(R.styleable.Extra_key);
1372 String value = ar.getString(R.styleable.Extra_value);
1373 if (key != null && value != null) {
1374 extras.putString(key, value);
1375 } else {
1376 throw new RuntimeException("Widget extras must have a key and value");
1377 }
1378 } else {
1379 throw new RuntimeException("Widgets can contain only extras");
1380 }
1381 ar.recycle();
1382 }
1383
1384 return addAppWidget(db, values, cn, spanX, spanY, extras);
Romain Guy693599f2010-03-23 10:58:18 -07001385 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001386
Romain Guy693599f2010-03-23 10:58:18 -07001387 return false;
Bjorn Bringert7984c942009-12-09 15:38:25 +00001388 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001389 private boolean addAppWidget(SQLiteDatabase db, ContentValues values, ComponentName cn,
Winson Chungb3302ae2012-05-01 10:19:14 -07001390 int spanX, int spanY, Bundle extras) {
Mike Cleronb87bd162009-10-30 16:36:56 -07001391 boolean allocatedAppWidgets = false;
1392 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
1393
1394 try {
1395 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001396
Mike Cleronb87bd162009-10-30 16:36:56 -07001397 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001398 values.put(Favorites.SPANX, spanX);
1399 values.put(Favorites.SPANY, spanY);
Mike Cleronb87bd162009-10-30 16:36:56 -07001400 values.put(Favorites.APPWIDGET_ID, appWidgetId);
Chris Wrend5e66bf2013-09-16 14:02:29 -04001401 values.put(Favorites.APPWIDGET_PROVIDER, cn.flattenToString());
Adam Cohendcd297f2013-06-18 13:13:40 -07001402 values.put(Favorites._ID, generateNewItemId());
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001403 dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values);
Mike Cleronb87bd162009-10-30 16:36:56 -07001404
1405 allocatedAppWidgets = true;
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001406
Michael Jurka8b805b12012-04-18 14:23:14 -07001407 // TODO: need to check return value
1408 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, cn);
Winson Chungb3302ae2012-05-01 10:19:14 -07001409
1410 // Send a broadcast to configure the widget
1411 if (extras != null && !extras.isEmpty()) {
1412 Intent intent = new Intent(ACTION_APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE);
1413 intent.setComponent(cn);
1414 intent.putExtras(extras);
1415 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1416 mContext.sendBroadcast(intent);
1417 }
Mike Cleronb87bd162009-10-30 16:36:56 -07001418 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001419 Log.e(TAG, "Problem allocating appWidgetId", ex);
Mike Cleronb87bd162009-10-30 16:36:56 -07001420 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001421
Mike Cleronb87bd162009-10-30 16:36:56 -07001422 return allocatedAppWidgets;
1423 }
Adam Cohen228da5a2011-07-27 22:23:47 -07001424
1425 private long addUriShortcut(SQLiteDatabase db, ContentValues values,
Mike Cleronb87bd162009-10-30 16:36:56 -07001426 TypedArray a) {
1427 Resources r = mContext.getResources();
1428
1429 final int iconResId = a.getResourceId(R.styleable.Favorite_icon, 0);
1430 final int titleResId = a.getResourceId(R.styleable.Favorite_title, 0);
1431
Romain Guy7eb9e5e2009-12-02 20:10:07 -08001432 Intent intent;
Mike Cleronb87bd162009-10-30 16:36:56 -07001433 String uri = null;
1434 try {
1435 uri = a.getString(R.styleable.Favorite_uri);
1436 intent = Intent.parseUri(uri, 0);
1437 } catch (URISyntaxException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001438 Log.w(TAG, "Shortcut has malformed uri: " + uri);
Adam Cohen228da5a2011-07-27 22:23:47 -07001439 return -1; // Oh well
Mike Cleronb87bd162009-10-30 16:36:56 -07001440 }
1441
1442 if (iconResId == 0 || titleResId == 0) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001443 Log.w(TAG, "Shortcut is missing title or icon resource ID");
Adam Cohen228da5a2011-07-27 22:23:47 -07001444 return -1;
Mike Cleronb87bd162009-10-30 16:36:56 -07001445 }
1446
Adam Cohendcd297f2013-06-18 13:13:40 -07001447 long id = generateNewItemId();
Mike Cleronb87bd162009-10-30 16:36:56 -07001448 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1449 values.put(Favorites.INTENT, intent.toUri(0));
1450 values.put(Favorites.TITLE, r.getString(titleResId));
1451 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_SHORTCUT);
1452 values.put(Favorites.SPANX, 1);
1453 values.put(Favorites.SPANY, 1);
1454 values.put(Favorites.ICON_TYPE, Favorites.ICON_TYPE_RESOURCE);
1455 values.put(Favorites.ICON_PACKAGE, mContext.getPackageName());
1456 values.put(Favorites.ICON_RESOURCE, r.getResourceName(iconResId));
Adam Cohen228da5a2011-07-27 22:23:47 -07001457 values.put(Favorites._ID, id);
Mike Cleronb87bd162009-10-30 16:36:56 -07001458
Adam Cohen228da5a2011-07-27 22:23:47 -07001459 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values) < 0) {
1460 return -1;
1461 }
1462 return id;
Mike Cleronb87bd162009-10-30 16:36:56 -07001463 }
Dan Sandlerd5024042014-01-09 15:01:33 -05001464
1465 public void migrateLauncher2Shortcuts(SQLiteDatabase db, Uri uri) {
1466 final ContentResolver resolver = mContext.getContentResolver();
1467 Cursor c = null;
1468 int count = 0;
1469 int curScreen = 0;
1470
1471 try {
1472 c = resolver.query(uri, null, null, null, "title ASC");
1473 } catch (Exception e) {
1474 // Ignore
1475 }
1476
1477
1478 // We already have a favorites database in the old provider
1479 if (c != null) {
1480 try {
1481 if (c.getCount() > 0) {
1482 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1483 final int intentIndex
1484 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
1485 final int titleIndex
1486 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
1487 final int iconTypeIndex
1488 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
1489 final int iconIndex
1490 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1491 final int iconPackageIndex
1492 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
1493 final int iconResourceIndex
1494 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
1495 final int containerIndex
1496 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
1497 final int itemTypeIndex
1498 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
1499 final int screenIndex
1500 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
1501 final int cellXIndex
1502 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
1503 final int cellYIndex
1504 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
1505 final int uriIndex
1506 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1507 final int displayModeIndex
1508 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
1509
1510 int i = 0;
1511 int curX = 0;
1512 int curY = 0;
1513
1514 final LauncherAppState app = LauncherAppState.getInstance();
1515 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1516 final int width = (int) grid.numColumns;
1517 final int height = (int) grid.numRows;
1518 final int hotseatWidth = (int) grid.numHotseatIcons;
Adam Cohen556f6132014-01-15 15:18:08 -08001519 PackageManager pm = mContext.getPackageManager();
Dan Sandlerd5024042014-01-09 15:01:33 -05001520
1521 final HashSet<String> seenIntents = new HashSet<String>(c.getCount());
1522
1523 final ContentValues[] rows = new ContentValues[c.getCount()];
1524
1525 while (c.moveToNext()) {
1526 final int itemType = c.getInt(itemTypeIndex);
1527 if (itemType != Favorites.ITEM_TYPE_APPLICATION
1528 && itemType != Favorites.ITEM_TYPE_SHORTCUT
1529 && itemType != Favorites.ITEM_TYPE_FOLDER) {
1530 continue;
1531 }
1532
1533 final int cellX = c.getInt(cellXIndex);
1534 final int cellY = c.getInt(cellYIndex);
1535 final int screen = c.getInt(screenIndex);
1536 int container = c.getInt(containerIndex);
1537 final String intentStr = c.getString(intentIndex);
1538 Launcher.addDumpLog(TAG, "migrating \""
1539 + c.getString(titleIndex) + "\": " + intentStr, true);
1540
1541 if (itemType != Favorites.ITEM_TYPE_FOLDER) {
Adam Cohen556f6132014-01-15 15:18:08 -08001542
1543 final Intent intent;
1544 final ComponentName cn;
1545 try {
1546 intent = Intent.parseUri(intentStr, 0);
1547 } catch (URISyntaxException e) {
1548 // bogus intent?
1549 Launcher.addDumpLog(TAG,
1550 "skipping invalid intent uri", true);
1551 continue;
1552 }
1553
1554 cn = intent.getComponent();
1555
Dan Sandlerd5024042014-01-09 15:01:33 -05001556 if (TextUtils.isEmpty(intentStr)) {
1557 // no intent? no icon
1558 Launcher.addDumpLog(TAG, "skipping empty intent", true);
1559 continue;
Adam Cohen556f6132014-01-15 15:18:08 -08001560 } else if (!LauncherModel.isValidPackageComponent(pm, cn)) {
1561 // component no longer exists.
1562 Launcher.addDumpLog(TAG, "skipping item whose component" +
1563 "no longer exists.", true);
1564 continue;
Dan Sandlerd5024042014-01-09 15:01:33 -05001565 } else {
Adam Cohen556f6132014-01-15 15:18:08 -08001566 // Canonicalize
1567 // the Play Store sets the package parameter, but Launcher
1568 // does not, so we clear that out to keep them the same
1569 intent.setPackage(null);
1570 final String key = intent.toUri(0);
1571 if (seenIntents.contains(key)) {
1572 Launcher.addDumpLog(TAG, "skipping duplicate", true);
Dan Sandlerd5024042014-01-09 15:01:33 -05001573 continue;
Adam Cohen556f6132014-01-15 15:18:08 -08001574 } else {
1575 seenIntents.add(key);
Dan Sandlerd5024042014-01-09 15:01:33 -05001576 }
1577 }
1578 }
1579
1580 ContentValues values = new ContentValues(c.getColumnCount());
1581 values.put(LauncherSettings.Favorites._ID, c.getInt(idIndex));
1582 values.put(LauncherSettings.Favorites.INTENT, intentStr);
1583 values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex));
1584 values.put(LauncherSettings.Favorites.ICON_TYPE,
1585 c.getInt(iconTypeIndex));
1586 values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex));
1587 values.put(LauncherSettings.Favorites.ICON_PACKAGE,
1588 c.getString(iconPackageIndex));
1589 values.put(LauncherSettings.Favorites.ICON_RESOURCE,
1590 c.getString(iconResourceIndex));
1591 values.put(LauncherSettings.Favorites.ITEM_TYPE, itemType);
1592 values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1);
1593 values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex));
1594 values.put(LauncherSettings.Favorites.DISPLAY_MODE,
1595 c.getInt(displayModeIndex));
1596
1597 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT
1598 && screen >= hotseatWidth) {
1599 // no room for you in the hotseat? it's off to the desktop with you
1600 container = Favorites.CONTAINER_DESKTOP;
1601 }
1602
1603 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1604 // In a folder or in the hotseat, preserve position
1605 values.put(LauncherSettings.Favorites.SCREEN, screen);
1606 values.put(LauncherSettings.Favorites.CELLX, cellX);
1607 values.put(LauncherSettings.Favorites.CELLY, cellY);
1608 } else {
1609 values.put(LauncherSettings.Favorites.SCREEN, curScreen);
1610 values.put(LauncherSettings.Favorites.CELLX, curX);
1611 values.put(LauncherSettings.Favorites.CELLY, curY);
1612 curX = (curX + 1) % width;
1613 if (curX == 0) {
1614 curY = (curY + 1);
1615 }
1616 // Leave the last row of icons blank on screen 0
1617 if (curScreen == 0 && curY == height - 1 || curY == height) {
1618 curScreen = (int) generateNewScreenId();
1619 curY = 0;
1620 }
1621 }
1622
1623 values.put(LauncherSettings.Favorites.CONTAINER, container);
1624
1625 rows[i++] = values;
1626 }
1627
1628 if (i > 0) {
1629 db.beginTransaction();
1630 try {
1631 final int N = rows.length;
1632 for (i = 0; i < N; i++) {
1633 if (rows[i] == null) continue;
1634 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, rows[i])
1635 < 0) {
1636 return;
1637 } else {
1638 count++;
1639 }
1640 }
1641 db.setTransactionSuccessful();
1642 } finally {
1643 db.endTransaction();
1644 }
1645 }
1646
1647 db.beginTransaction();
1648 try {
1649 for (i=0; i<=curScreen; i++) {
1650 final ContentValues values = new ContentValues();
1651 values.put(LauncherSettings.WorkspaceScreens._ID, i);
1652 values.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
1653 if (dbInsertAndCheck(this, db, TABLE_WORKSPACE_SCREENS, null, values)
1654 < 0) {
1655 return;
1656 }
1657 }
1658 db.setTransactionSuccessful();
1659 } finally {
1660 db.endTransaction();
1661 }
1662 }
1663 } finally {
1664 c.close();
1665 }
1666 }
1667
1668 Launcher.addDumpLog(TAG, "migrated " + count + " icons from Launcher2 into "
1669 + (curScreen+1) + " screens", true);
1670
1671 // ensure that new screens are created to hold these icons
1672 setFlagJustLoadedOldDb();
1673
1674 // Update max IDs; very important since we just grabbed IDs from another database
1675 mMaxItemId = initializeMaxItemId(db);
1676 mMaxScreenId = initializeMaxScreenId(db);
1677 if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId + " mMaxScreenId: " + mMaxScreenId);
1678 }
Mike Cleronb87bd162009-10-30 16:36:56 -07001679 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001680
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001681 /**
1682 * Build a query string that will match any row where the column matches
1683 * anything in the values list.
1684 */
1685 static String buildOrWhereString(String column, int[] values) {
1686 StringBuilder selectWhere = new StringBuilder();
1687 for (int i = values.length - 1; i >= 0; i--) {
1688 selectWhere.append(column).append("=").append(values[i]);
1689 if (i > 0) {
1690 selectWhere.append(" OR ");
1691 }
1692 }
1693 return selectWhere.toString();
1694 }
1695
1696 static class SqlArguments {
1697 public final String table;
1698 public final String where;
1699 public final String[] args;
1700
1701 SqlArguments(Uri url, String where, String[] args) {
1702 if (url.getPathSegments().size() == 1) {
1703 this.table = url.getPathSegments().get(0);
1704 this.where = where;
1705 this.args = args;
1706 } else if (url.getPathSegments().size() != 2) {
1707 throw new IllegalArgumentException("Invalid URI: " + url);
1708 } else if (!TextUtils.isEmpty(where)) {
1709 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
1710 } else {
1711 this.table = url.getPathSegments().get(0);
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001712 this.where = "_id=" + ContentUris.parseId(url);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001713 this.args = null;
1714 }
1715 }
1716
1717 SqlArguments(Uri url) {
1718 if (url.getPathSegments().size() == 1) {
1719 table = url.getPathSegments().get(0);
1720 where = null;
1721 args = null;
1722 } else {
1723 throw new IllegalArgumentException("Invalid URI: " + url);
1724 }
1725 }
1726 }
Adam Cohen556f6132014-01-15 15:18:08 -08001727}