blob: 977446b01e3cab87b474955a659ba903efadd62b [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();
Dan Sandler2b471742014-01-21 14:14:41 -0500324 final File dbFile = new File(db.getPath());
Dan Sandlerd5024042014-01-09 15:01:33 -0500325 mOpenHelper.close();
Dan Sandler2b471742014-01-21 14:14:41 -0500326 if (dbFile.exists()) {
327 SQLiteDatabase.deleteDatabase(dbFile);
328 }
Dan Sandlerd5024042014-01-09 15:01:33 -0500329 mOpenHelper = new DatabaseHelper(getContext());
330 }
331
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800332 private static class DatabaseHelper extends SQLiteOpenHelper {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800333 private static final String TAG_FAVORITES = "favorites";
334 private static final String TAG_FAVORITE = "favorite";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700335 private static final String TAG_CLOCK = "clock";
336 private static final String TAG_SEARCH = "search";
Mike Cleronb87bd162009-10-30 16:36:56 -0700337 private static final String TAG_APPWIDGET = "appwidget";
338 private static final String TAG_SHORTCUT = "shortcut";
Adam Cohen228da5a2011-07-27 22:23:47 -0700339 private static final String TAG_FOLDER = "folder";
Winson Chungb3302ae2012-05-01 10:19:14 -0700340 private static final String TAG_EXTRA = "extra";
Daniel Sandler57dac262013-10-03 13:28:36 -0400341 private static final String TAG_INCLUDE = "include";
Winson Chung3d503fb2011-07-13 17:25:49 -0700342
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800343 private final Context mContext;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700344 private final AppWidgetHost mAppWidgetHost;
Adam Cohendcd297f2013-06-18 13:13:40 -0700345 private long mMaxItemId = -1;
346 private long mMaxScreenId = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800347
348 DatabaseHelper(Context context) {
349 super(context, DATABASE_NAME, null, DATABASE_VERSION);
350 mContext = context;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700351 mAppWidgetHost = new AppWidgetHost(context, Launcher.APPWIDGET_HOST_ID);
Winson Chung3d503fb2011-07-13 17:25:49 -0700352
353 // In the case where neither onCreate nor onUpgrade gets called, we read the maxId from
354 // the DB here
Adam Cohendcd297f2013-06-18 13:13:40 -0700355 if (mMaxItemId == -1) {
356 mMaxItemId = initializeMaxItemId(getWritableDatabase());
357 }
358 if (mMaxScreenId == -1) {
359 mMaxScreenId = initializeMaxScreenId(getWritableDatabase());
Winson Chung3d503fb2011-07-13 17:25:49 -0700360 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361 }
362
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700363 /**
364 * Send notification that we've deleted the {@link AppWidgetHost},
365 * probably as part of the initial database creation. The receiver may
366 * want to re-call {@link AppWidgetHost#startListening()} to ensure
367 * callbacks are correctly set.
368 */
369 private void sendAppWidgetResetNotify() {
370 final ContentResolver resolver = mContext.getContentResolver();
371 resolver.notifyChange(CONTENT_APPWIDGET_RESET_URI, null);
372 }
373
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 @Override
375 public void onCreate(SQLiteDatabase db) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800376 if (LOGD) Log.d(TAG, "creating new launcher database");
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700377
Adam Cohendcd297f2013-06-18 13:13:40 -0700378 mMaxItemId = 1;
379 mMaxScreenId = 0;
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700380
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 db.execSQL("CREATE TABLE favorites (" +
382 "_id INTEGER PRIMARY KEY," +
383 "title TEXT," +
384 "intent TEXT," +
385 "container INTEGER," +
386 "screen INTEGER," +
387 "cellX INTEGER," +
388 "cellY INTEGER," +
389 "spanX INTEGER," +
390 "spanY INTEGER," +
391 "itemType INTEGER," +
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700392 "appWidgetId INTEGER NOT NULL DEFAULT -1," +
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 "isShortcut INTEGER," +
394 "iconType INTEGER," +
395 "iconPackage TEXT," +
396 "iconResource TEXT," +
397 "icon BLOB," +
398 "uri TEXT," +
Chris Wrend5e66bf2013-09-16 14:02:29 -0400399 "displayMode INTEGER," +
Chris Wren1ada10d2013-09-13 18:01:38 -0400400 "appWidgetProvider TEXT," +
401 "modified INTEGER NOT NULL DEFAULT 0" +
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800402 ");");
Adam Cohendcd297f2013-06-18 13:13:40 -0700403 addWorkspacesTable(db);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800404
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700405 // Database was just created, so wipe any previous widgets
406 if (mAppWidgetHost != null) {
407 mAppWidgetHost.deleteHost();
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700408 sendAppWidgetResetNotify();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700410
Adam Cohen6dbe0492013-12-02 17:00:14 -0800411 if (shouldImportLauncher2Database(mContext)) {
Dan Sandlerf0b8dac2013-11-19 12:21:25 -0500412 // Try converting the old database
413 ContentValuesCallback permuteScreensCb = new ContentValuesCallback() {
414 public void onRow(ContentValues values) {
415 int container = values.getAsInteger(LauncherSettings.Favorites.CONTAINER);
416 if (container == Favorites.CONTAINER_DESKTOP) {
417 int screen = values.getAsInteger(LauncherSettings.Favorites.SCREEN);
418 screen = (int) upgradeLauncherDb_permuteScreens(screen);
419 values.put(LauncherSettings.Favorites.SCREEN, screen);
420 }
421 }
422 };
423 Uri uri = Uri.parse("content://" + Settings.AUTHORITY +
424 "/old_favorites?notify=true");
425 if (!convertDatabase(db, uri, permuteScreensCb, true)) {
426 // Try and upgrade from the Launcher2 db
427 uri = LauncherSettings.Favorites.OLD_CONTENT_URI;
428 if (!convertDatabase(db, uri, permuteScreensCb, false)) {
429 // If we fail, then set a flag to load the default workspace
430 setFlagEmptyDbCreated();
431 return;
Winson Chungc763c4e2013-07-19 13:49:06 -0700432 }
433 }
Dan Sandlerf0b8dac2013-11-19 12:21:25 -0500434 // Right now, in non-default workspace cases, we want to run the final
435 // upgrade code (ie. to fix workspace screen indices -> ids, etc.), so
436 // set that flag too.
437 setFlagJustLoadedOldDb();
438 } else {
439 // Fresh and clean launcher DB.
440 mMaxItemId = initializeMaxItemId(db);
441 setFlagEmptyDbCreated();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800442 }
443 }
444
Adam Cohendcd297f2013-06-18 13:13:40 -0700445 private void addWorkspacesTable(SQLiteDatabase db) {
446 db.execSQL("CREATE TABLE " + TABLE_WORKSPACE_SCREENS + " (" +
447 LauncherSettings.WorkspaceScreens._ID + " INTEGER," +
Chris Wren1ada10d2013-09-13 18:01:38 -0400448 LauncherSettings.WorkspaceScreens.SCREEN_RANK + " INTEGER," +
449 LauncherSettings.ChangeLogColumns.MODIFIED + " INTEGER NOT NULL DEFAULT 0" +
Adam Cohendcd297f2013-06-18 13:13:40 -0700450 ");");
451 }
452
Winson Chungc763c4e2013-07-19 13:49:06 -0700453 private void setFlagJustLoadedOldDb() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400454 String spKey = LauncherAppState.getSharedPreferencesKey();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700455 SharedPreferences sp = mContext.getSharedPreferences(spKey, Context.MODE_PRIVATE);
456 SharedPreferences.Editor editor = sp.edit();
Winson Chungc763c4e2013-07-19 13:49:06 -0700457 editor.putBoolean(UPGRADED_FROM_OLD_DATABASE, true);
458 editor.putBoolean(EMPTY_DATABASE_CREATED, false);
Michael Jurkab85f8a42012-04-25 15:48:32 -0700459 editor.commit();
460 }
461
Winson Chungc763c4e2013-07-19 13:49:06 -0700462 private void setFlagEmptyDbCreated() {
463 String spKey = LauncherAppState.getSharedPreferencesKey();
464 SharedPreferences sp = mContext.getSharedPreferences(spKey, Context.MODE_PRIVATE);
465 SharedPreferences.Editor editor = sp.edit();
466 editor.putBoolean(EMPTY_DATABASE_CREATED, true);
467 editor.putBoolean(UPGRADED_FROM_OLD_DATABASE, false);
468 editor.commit();
469 }
470
471 // We rearrange the screens from the old launcher
472 // 12345 -> 34512
473 private long upgradeLauncherDb_permuteScreens(long screen) {
474 if (screen >= 2) {
475 return screen - 2;
476 } else {
477 return screen + 3;
478 }
479 }
480
481 private boolean convertDatabase(SQLiteDatabase db, Uri uri,
482 ContentValuesCallback cb, boolean deleteRows) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800483 if (LOGD) Log.d(TAG, "converting database from an older format, but not onUpgrade");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800484 boolean converted = false;
485
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800486 final ContentResolver resolver = mContext.getContentResolver();
487 Cursor cursor = null;
488
489 try {
490 cursor = resolver.query(uri, null, null, null, null);
491 } catch (Exception e) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700492 // Ignore
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800493 }
494
495 // We already have a favorites database in the old provider
Winson Chungc763c4e2013-07-19 13:49:06 -0700496 if (cursor != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800497 try {
Winson Chungc763c4e2013-07-19 13:49:06 -0700498 if (cursor.getCount() > 0) {
499 converted = copyFromCursor(db, cursor, cb) > 0;
500 if (converted && deleteRows) {
501 resolver.delete(uri, null, null);
502 }
503 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800504 } finally {
505 cursor.close();
506 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800507 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700508
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800509 if (converted) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700510 // Convert widgets from this import into widgets
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800511 if (LOGD) Log.d(TAG, "converted and now triggering widget upgrade");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800512 convertWidgets(db);
Winson Chungc763c4e2013-07-19 13:49:06 -0700513
514 // Update max item id
515 mMaxItemId = initializeMaxItemId(db);
516 if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800517 }
518
519 return converted;
520 }
521
Winson Chungc763c4e2013-07-19 13:49:06 -0700522 private int copyFromCursor(SQLiteDatabase db, Cursor c, ContentValuesCallback cb) {
Romain Guy73b979d2009-06-09 12:57:21 -0700523 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800524 final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
525 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
526 final int iconTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
527 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
528 final int iconPackageIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
529 final int iconResourceIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
530 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
531 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
532 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
533 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
534 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
535 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
536 final int displayModeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
537
538 ContentValues[] rows = new ContentValues[c.getCount()];
539 int i = 0;
540 while (c.moveToNext()) {
541 ContentValues values = new ContentValues(c.getColumnCount());
Romain Guy73b979d2009-06-09 12:57:21 -0700542 values.put(LauncherSettings.Favorites._ID, c.getLong(idIndex));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800543 values.put(LauncherSettings.Favorites.INTENT, c.getString(intentIndex));
544 values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex));
545 values.put(LauncherSettings.Favorites.ICON_TYPE, c.getInt(iconTypeIndex));
546 values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex));
547 values.put(LauncherSettings.Favorites.ICON_PACKAGE, c.getString(iconPackageIndex));
548 values.put(LauncherSettings.Favorites.ICON_RESOURCE, c.getString(iconResourceIndex));
549 values.put(LauncherSettings.Favorites.CONTAINER, c.getInt(containerIndex));
550 values.put(LauncherSettings.Favorites.ITEM_TYPE, c.getInt(itemTypeIndex));
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700551 values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800552 values.put(LauncherSettings.Favorites.SCREEN, c.getInt(screenIndex));
553 values.put(LauncherSettings.Favorites.CELLX, c.getInt(cellXIndex));
554 values.put(LauncherSettings.Favorites.CELLY, c.getInt(cellYIndex));
555 values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex));
556 values.put(LauncherSettings.Favorites.DISPLAY_MODE, c.getInt(displayModeIndex));
Winson Chungc763c4e2013-07-19 13:49:06 -0700557 if (cb != null) {
558 cb.onRow(values);
559 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560 rows[i++] = values;
561 }
562
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800563 int total = 0;
Winson Chungc763c4e2013-07-19 13:49:06 -0700564 if (i > 0) {
565 db.beginTransaction();
566 try {
567 int numValues = rows.length;
568 for (i = 0; i < numValues; i++) {
569 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, rows[i]) < 0) {
570 return 0;
571 } else {
572 total++;
573 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800574 }
Winson Chungc763c4e2013-07-19 13:49:06 -0700575 db.setTransactionSuccessful();
576 } finally {
577 db.endTransaction();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800578 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800579 }
580
581 return total;
582 }
583
584 @Override
585 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Winson Chungc763c4e2013-07-19 13:49:06 -0700586 if (LOGD) Log.d(TAG, "onUpgrade triggered: " + oldVersion);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700587
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800588 int version = oldVersion;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700589 if (version < 3) {
590 // upgrade 1,2 -> 3 added appWidgetId column
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800591 db.beginTransaction();
592 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700593 // Insert new column for holding appWidgetIds
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800594 db.execSQL("ALTER TABLE favorites " +
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700595 "ADD COLUMN appWidgetId INTEGER NOT NULL DEFAULT -1;");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800596 db.setTransactionSuccessful();
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700597 version = 3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800598 } catch (SQLException ex) {
599 // Old version remains, which means we wipe old data
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800600 Log.e(TAG, ex.getMessage(), ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800601 } finally {
602 db.endTransaction();
603 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700604
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605 // Convert existing widgets only if table upgrade was successful
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700606 if (version == 3) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800607 convertWidgets(db);
608 }
609 }
Romain Guy73b979d2009-06-09 12:57:21 -0700610
611 if (version < 4) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800612 version = 4;
Romain Guy73b979d2009-06-09 12:57:21 -0700613 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700614
Romain Guy509cd6a2010-03-23 15:10:56 -0700615 // Where's version 5?
616 // - Donut and sholes on 2.0 shipped with version 4 of launcher1.
Daniel Sandler325dc232013-06-05 22:57:57 -0400617 // - Passion shipped on 2.1 with version 6 of launcher3
Romain Guy509cd6a2010-03-23 15:10:56 -0700618 // - Sholes shipped on 2.1r1 (aka Mr. 3) with version 5 of launcher 1
619 // but version 5 on there was the updateContactsShortcuts change
620 // which was version 6 in launcher 2 (first shipped on passion 2.1r1).
621 // The updateContactsShortcuts change is idempotent, so running it twice
622 // is okay so we'll do that when upgrading the devices that shipped with it.
623 if (version < 6) {
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800624 // We went from 3 to 5 screens. Move everything 1 to the right
625 db.beginTransaction();
626 try {
627 db.execSQL("UPDATE favorites SET screen=(screen + 1);");
628 db.setTransactionSuccessful();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800629 } catch (SQLException ex) {
630 // Old version remains, which means we wipe old data
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800631 Log.e(TAG, ex.getMessage(), ex);
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800632 } finally {
633 db.endTransaction();
634 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700635
Romain Guy509cd6a2010-03-23 15:10:56 -0700636 // We added the fast track.
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800637 if (updateContactsShortcuts(db)) {
638 version = 6;
639 }
640 }
Bjorn Bringert7984c942009-12-09 15:38:25 +0000641
642 if (version < 7) {
643 // Version 7 gets rid of the special search widget.
644 convertWidgets(db);
645 version = 7;
646 }
647
Joe Onorato0589f0f2010-02-08 13:44:00 -0800648 if (version < 8) {
649 // Version 8 (froyo) has the icons all normalized. This should
650 // already be the case in practice, but we now rely on it and don't
651 // resample the images each time.
652 normalizeIcons(db);
653 version = 8;
654 }
655
Winson Chung3d503fb2011-07-13 17:25:49 -0700656 if (version < 9) {
657 // The max id is not yet set at this point (onUpgrade is triggered in the ctor
658 // 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 -0700659 if (mMaxItemId == -1) {
660 mMaxItemId = initializeMaxItemId(db);
Winson Chung3d503fb2011-07-13 17:25:49 -0700661 }
662
663 // Add default hotseat icons
Winson Chung6d092682011-11-16 18:43:26 -0800664 loadFavorites(db, R.xml.update_workspace);
Winson Chung3d503fb2011-07-13 17:25:49 -0700665 version = 9;
666 }
667
Daniel Lehmannd02402c2012-05-14 18:30:53 -0700668 // We bumped the version three time during JB, once to update the launch flags, once to
669 // update the override for the default launch animation and once to set the mimetype
670 // to improve startup performance
671 if (version < 12) {
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700672 // Contact shortcuts need a different set of flags to be launched now
673 // The updateContactsShortcuts change is idempotent, so we can keep using it like
674 // back in the Donut days
675 updateContactsShortcuts(db);
Daniel Lehmannd02402c2012-05-14 18:30:53 -0700676 version = 12;
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700677 }
678
Adam Cohendcd297f2013-06-18 13:13:40 -0700679 if (version < 13) {
680 // With the new shrink-wrapped and re-orderable workspaces, it makes sense
681 // to persist workspace screens and their relative order.
682 mMaxScreenId = 0;
683
684 // This will never happen in the wild, but when we switch to using workspace
685 // screen ids, redo the import from old launcher.
Winson Chungc763c4e2013-07-19 13:49:06 -0700686 sJustLoadedFromOldDb = true;
Adam Cohendcd297f2013-06-18 13:13:40 -0700687
688 addWorkspacesTable(db);
689 version = 13;
690 }
691
Chris Wrend5e66bf2013-09-16 14:02:29 -0400692 if (version < 14) {
693 db.beginTransaction();
694 try {
695 // Insert new column for holding widget provider name
696 db.execSQL("ALTER TABLE favorites " +
697 "ADD COLUMN appWidgetProvider TEXT;");
698 db.setTransactionSuccessful();
699 version = 14;
700 } catch (SQLException ex) {
701 // Old version remains, which means we wipe old data
702 Log.e(TAG, ex.getMessage(), ex);
703 } finally {
704 db.endTransaction();
705 }
706 }
707
Chris Wren1ada10d2013-09-13 18:01:38 -0400708
709 if (version < 15) {
710 db.beginTransaction();
711 try {
712 // Insert new column for holding update timestamp
713 db.execSQL("ALTER TABLE favorites " +
714 "ADD COLUMN modified INTEGER NOT NULL DEFAULT 0;");
715 db.execSQL("ALTER TABLE workspaceScreens " +
716 "ADD COLUMN modified INTEGER NOT NULL DEFAULT 0;");
717 db.setTransactionSuccessful();
718 version = 15;
719 } catch (SQLException ex) {
720 // Old version remains, which means we wipe old data
721 Log.e(TAG, ex.getMessage(), ex);
722 } finally {
723 db.endTransaction();
724 }
725 }
726
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 if (version != DATABASE_VERSION) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800728 Log.w(TAG, "Destroying all old data.");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800729 db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES);
Adam Cohendcd297f2013-06-18 13:13:40 -0700730 db.execSQL("DROP TABLE IF EXISTS " + TABLE_WORKSPACE_SCREENS);
731
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 onCreate(db);
733 }
734 }
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800735
736 private boolean updateContactsShortcuts(SQLiteDatabase db) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800737 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE,
738 new int[] { Favorites.ITEM_TYPE_SHORTCUT });
739
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700740 Cursor c = null;
741 final String actionQuickContact = "com.android.contacts.action.QUICK_CONTACT";
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800742 db.beginTransaction();
743 try {
744 // Select and iterate through each matching widget
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700745 c = db.query(TABLE_FAVORITES,
746 new String[] { Favorites._ID, Favorites.INTENT },
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800747 selectWhere, null, null, null, null);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700748 if (c == null) return false;
749
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800750 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700751
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800752 final int idIndex = c.getColumnIndex(Favorites._ID);
753 final int intentIndex = c.getColumnIndex(Favorites.INTENT);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700754
755 while (c.moveToNext()) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800756 long favoriteId = c.getLong(idIndex);
757 final String intentUri = c.getString(intentIndex);
758 if (intentUri != null) {
759 try {
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700760 final Intent intent = Intent.parseUri(intentUri, 0);
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800761 android.util.Log.d("Home", intent.toString());
762 final Uri uri = intent.getData();
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700763 if (uri != null) {
764 final String data = uri.toString();
765 if ((Intent.ACTION_VIEW.equals(intent.getAction()) ||
766 actionQuickContact.equals(intent.getAction())) &&
767 (data.startsWith("content://contacts/people/") ||
768 data.startsWith("content://com.android.contacts/" +
769 "contacts/lookup/"))) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800770
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700771 final Intent newIntent = new Intent(actionQuickContact);
772 // When starting from the launcher, start in a new, cleared task
773 // CLEAR_WHEN_TASK_RESET cannot reset the root of a task, so we
774 // clear the whole thing preemptively here since
775 // QuickContactActivity will finish itself when launching other
776 // detail activities.
777 newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
778 Intent.FLAG_ACTIVITY_CLEAR_TASK);
Winson Chung2672ff92012-05-04 16:22:30 -0700779 newIntent.putExtra(
780 Launcher.INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION, true);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700781 newIntent.setData(uri);
Daniel Lehmannd02402c2012-05-14 18:30:53 -0700782 // Determine the type and also put that in the shortcut
783 // (that can speed up launch a bit)
784 newIntent.setDataAndType(uri, newIntent.resolveType(mContext));
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800785
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700786 final ContentValues values = new ContentValues();
787 values.put(LauncherSettings.Favorites.INTENT,
788 newIntent.toUri(0));
789
790 String updateWhere = Favorites._ID + "=" + favoriteId;
791 db.update(TABLE_FAVORITES, values, updateWhere, null);
792 }
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800793 }
794 } catch (RuntimeException ex) {
795 Log.e(TAG, "Problem upgrading shortcut", ex);
796 } catch (URISyntaxException e) {
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700797 Log.e(TAG, "Problem upgrading shortcut", e);
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800798 }
799 }
800 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700801
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800802 db.setTransactionSuccessful();
803 } catch (SQLException ex) {
804 Log.w(TAG, "Problem while upgrading contacts", ex);
805 return false;
806 } finally {
807 db.endTransaction();
808 if (c != null) {
809 c.close();
810 }
811 }
812
813 return true;
814 }
815
Joe Onorato0589f0f2010-02-08 13:44:00 -0800816 private void normalizeIcons(SQLiteDatabase db) {
817 Log.d(TAG, "normalizing icons");
818
Joe Onorato346e1292010-02-18 10:34:24 -0500819 db.beginTransaction();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800820 Cursor c = null;
Joe Onorato9690b392010-03-23 17:34:37 -0400821 SQLiteStatement update = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800822 try {
823 boolean logged = false;
Joe Onorato9690b392010-03-23 17:34:37 -0400824 update = db.compileStatement("UPDATE favorites "
Jeff Hamiltoneaf77d62010-02-13 00:08:17 -0600825 + "SET icon=? WHERE _id=?");
Joe Onorato0589f0f2010-02-08 13:44:00 -0800826
827 c = db.rawQuery("SELECT _id, icon FROM favorites WHERE iconType=" +
828 Favorites.ICON_TYPE_BITMAP, null);
829
830 final int idIndex = c.getColumnIndexOrThrow(Favorites._ID);
831 final int iconIndex = c.getColumnIndexOrThrow(Favorites.ICON);
832
833 while (c.moveToNext()) {
834 long id = c.getLong(idIndex);
835 byte[] data = c.getBlob(iconIndex);
836 try {
837 Bitmap bitmap = Utilities.resampleIconBitmap(
838 BitmapFactory.decodeByteArray(data, 0, data.length),
839 mContext);
840 if (bitmap != null) {
841 update.bindLong(1, id);
842 data = ItemInfo.flattenBitmap(bitmap);
843 if (data != null) {
844 update.bindBlob(2, data);
845 update.execute();
846 }
847 bitmap.recycle();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800848 }
849 } catch (Exception e) {
850 if (!logged) {
851 Log.e(TAG, "Failed normalizing icon " + id, e);
852 } else {
853 Log.e(TAG, "Also failed normalizing icon " + id);
854 }
855 logged = true;
856 }
857 }
Bjorn Bringert3a928e42010-02-19 11:15:40 +0000858 db.setTransactionSuccessful();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800859 } catch (SQLException ex) {
860 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
861 } finally {
862 db.endTransaction();
Joe Onorato9690b392010-03-23 17:34:37 -0400863 if (update != null) {
864 update.close();
865 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800866 if (c != null) {
867 c.close();
868 }
869 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700870 }
871
872 // Generates a new ID to use for an object in your database. This method should be only
873 // called from the main UI thread. As an exception, we do call it when we call the
874 // constructor from the worker thread; however, this doesn't extend until after the
875 // constructor is called, and we only pass a reference to LauncherProvider to LauncherApp
876 // after that point
Adam Cohendcd297f2013-06-18 13:13:40 -0700877 public long generateNewItemId() {
878 if (mMaxItemId < 0) {
879 throw new RuntimeException("Error: max item id was not initialized");
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700880 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700881 mMaxItemId += 1;
882 return mMaxItemId;
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700883 }
884
Winson Chungc763c4e2013-07-19 13:49:06 -0700885 public void updateMaxItemId(long id) {
886 mMaxItemId = id + 1;
887 }
888
Chris Wren5dee7af2013-12-20 17:22:11 -0500889 public void checkId(String table, ContentValues values) {
890 long id = values.getAsLong(LauncherSettings.BaseLauncherColumns._ID);
891 if (table == LauncherProvider.TABLE_WORKSPACE_SCREENS) {
892 mMaxScreenId = Math.max(id, mMaxScreenId);
893 } else {
894 mMaxItemId = Math.max(id, mMaxItemId);
895 }
896 }
897
Adam Cohendcd297f2013-06-18 13:13:40 -0700898 private long initializeMaxItemId(SQLiteDatabase db) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700899 Cursor c = db.rawQuery("SELECT MAX(_id) FROM favorites", null);
900
901 // get the result
902 final int maxIdIndex = 0;
903 long id = -1;
904 if (c != null && c.moveToNext()) {
905 id = c.getLong(maxIdIndex);
906 }
Michael Jurka5130e402011-10-13 04:55:35 -0700907 if (c != null) {
908 c.close();
909 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700910
911 if (id == -1) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700912 throw new RuntimeException("Error: could not query max item id");
913 }
914
915 return id;
916 }
917
918 // Generates a new ID to use for an workspace screen in your database. This method
919 // should be only called from the main UI thread. As an exception, we do call it when we
920 // call the constructor from the worker thread; however, this doesn't extend until after the
921 // constructor is called, and we only pass a reference to LauncherProvider to LauncherApp
922 // after that point
923 public long generateNewScreenId() {
924 if (mMaxScreenId < 0) {
925 throw new RuntimeException("Error: max screen id was not initialized");
926 }
927 mMaxScreenId += 1;
Winson Chunga90303b2013-11-15 13:05:06 -0800928 // Log to disk
929 Launcher.addDumpLog(TAG, "11683562 - generateNewScreenId(): " + mMaxScreenId, true);
Adam Cohendcd297f2013-06-18 13:13:40 -0700930 return mMaxScreenId;
931 }
932
933 public void updateMaxScreenId(long maxScreenId) {
Winson Chunga90303b2013-11-15 13:05:06 -0800934 // Log to disk
935 Launcher.addDumpLog(TAG, "11683562 - updateMaxScreenId(): " + maxScreenId, true);
Adam Cohendcd297f2013-06-18 13:13:40 -0700936 mMaxScreenId = maxScreenId;
937 }
938
939 private long initializeMaxScreenId(SQLiteDatabase db) {
940 Cursor c = db.rawQuery("SELECT MAX(" + LauncherSettings.WorkspaceScreens._ID + ") FROM " + TABLE_WORKSPACE_SCREENS, null);
941
942 // get the result
943 final int maxIdIndex = 0;
944 long id = -1;
945 if (c != null && c.moveToNext()) {
946 id = c.getLong(maxIdIndex);
947 }
948 if (c != null) {
949 c.close();
950 }
951
952 if (id == -1) {
953 throw new RuntimeException("Error: could not query max screen id");
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700954 }
955
Winson Chunga90303b2013-11-15 13:05:06 -0800956 // Log to disk
957 Launcher.addDumpLog(TAG, "11683562 - initializeMaxScreenId(): " + id, true);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700958 return id;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800959 }
960
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800961 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700962 * Upgrade existing clock and photo frame widgets into their new widget
Bjorn Bringert93c45762009-12-16 13:19:47 +0000963 * equivalents.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964 */
965 private void convertWidgets(SQLiteDatabase db) {
Bjorn Bringert34251342009-12-15 13:33:11 +0000966 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967 final int[] bindSources = new int[] {
968 Favorites.ITEM_TYPE_WIDGET_CLOCK,
969 Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME,
Bjorn Bringert7984c942009-12-09 15:38:25 +0000970 Favorites.ITEM_TYPE_WIDGET_SEARCH,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 };
Bjorn Bringert7984c942009-12-09 15:38:25 +0000972
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800973 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE, bindSources);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700974
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 Cursor c = null;
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700976
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 db.beginTransaction();
978 try {
979 // Select and iterate through each matching widget
Bjorn Bringert7984c942009-12-09 15:38:25 +0000980 c = db.query(TABLE_FAVORITES, new String[] { Favorites._ID, Favorites.ITEM_TYPE },
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 selectWhere, null, null, null, null);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700982
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800983 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700984
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985 final ContentValues values = new ContentValues();
986 while (c != null && c.moveToNext()) {
987 long favoriteId = c.getLong(0);
Bjorn Bringert7984c942009-12-09 15:38:25 +0000988 int favoriteType = c.getInt(1);
989
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700990 // Allocate and update database with new appWidgetId
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700992 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700993
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800994 if (LOGD) {
995 Log.d(TAG, "allocated appWidgetId=" + appWidgetId
996 + " for favoriteId=" + favoriteId);
997 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800998 values.clear();
Bjorn Bringert7984c942009-12-09 15:38:25 +0000999 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
1000 values.put(Favorites.APPWIDGET_ID, appWidgetId);
1001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002 // Original widgets might not have valid spans when upgrading
Bjorn Bringert7984c942009-12-09 15:38:25 +00001003 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
1004 values.put(LauncherSettings.Favorites.SPANX, 4);
1005 values.put(LauncherSettings.Favorites.SPANY, 1);
1006 } else {
1007 values.put(LauncherSettings.Favorites.SPANX, 2);
1008 values.put(LauncherSettings.Favorites.SPANY, 2);
1009 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001010
1011 String updateWhere = Favorites._ID + "=" + favoriteId;
1012 db.update(TABLE_FAVORITES, values, updateWhere, null);
Bjorn Bringert34251342009-12-15 13:33:11 +00001013
Bjorn Bringert34251342009-12-15 13:33:11 +00001014 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_CLOCK) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001015 // TODO: check return value
1016 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
Bjorn Bringert34251342009-12-15 13:33:11 +00001017 new ComponentName("com.android.alarmclock",
1018 "com.android.alarmclock.AnalogAppWidgetProvider"));
1019 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001020 // TODO: check return value
1021 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
Bjorn Bringert34251342009-12-15 13:33:11 +00001022 new ComponentName("com.android.camera",
1023 "com.android.camera.PhotoAppWidgetProvider"));
1024 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001025 // TODO: check return value
1026 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
Bjorn Bringertcd8fec02010-01-14 13:26:43 +00001027 getSearchWidgetProvider());
Bjorn Bringert34251342009-12-15 13:33:11 +00001028 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001029 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001030 Log.e(TAG, "Problem allocating appWidgetId", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001031 }
1032 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001033
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001034 db.setTransactionSuccessful();
1035 } catch (SQLException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001036 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037 } finally {
1038 db.endTransaction();
1039 if (c != null) {
1040 c.close();
1041 }
1042 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001043
1044 // Update max item id
1045 mMaxItemId = initializeMaxItemId(db);
1046 if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047 }
1048
Michael Jurka8b805b12012-04-18 14:23:14 -07001049 private static final void beginDocument(XmlPullParser parser, String firstElementName)
1050 throws XmlPullParserException, IOException {
1051 int type;
Michael Jurka9bc8eba2012-05-21 20:36:44 -07001052 while ((type = parser.next()) != XmlPullParser.START_TAG
1053 && type != XmlPullParser.END_DOCUMENT) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001054 ;
1055 }
1056
Michael Jurka9bc8eba2012-05-21 20:36:44 -07001057 if (type != XmlPullParser.START_TAG) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001058 throw new XmlPullParserException("No start tag found");
1059 }
1060
1061 if (!parser.getName().equals(firstElementName)) {
1062 throw new XmlPullParserException("Unexpected start tag: found " + parser.getName() +
1063 ", expected " + firstElementName);
1064 }
1065 }
1066
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001067 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001068 * Loads the default set of favorite packages from an xml file.
1069 *
1070 * @param db The database to write the values into
Winson Chung3d503fb2011-07-13 17:25:49 -07001071 * @param filterContainerId The specific container id of items to load
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001072 */
Winson Chung6d092682011-11-16 18:43:26 -08001073 private int loadFavorites(SQLiteDatabase db, int workspaceResourceId) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074 Intent intent = new Intent(Intent.ACTION_MAIN, null);
1075 intent.addCategory(Intent.CATEGORY_LAUNCHER);
1076 ContentValues values = new ContentValues();
1077
Daniel Sandler57dac262013-10-03 13:28:36 -04001078 if (LOGD) Log.v(TAG, String.format("Loading favorites from resid=0x%08x", workspaceResourceId));
1079
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001080 PackageManager packageManager = mContext.getPackageManager();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081 int i = 0;
1082 try {
Winson Chung6d092682011-11-16 18:43:26 -08001083 XmlResourceParser parser = mContext.getResources().getXml(workspaceResourceId);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001084 AttributeSet attrs = Xml.asAttributeSet(parser);
Michael Jurka8b805b12012-04-18 14:23:14 -07001085 beginDocument(parser, TAG_FAVORITES);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001087 final int depth = parser.getDepth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001089 int type;
1090 while (((type = parser.next()) != XmlPullParser.END_TAG ||
1091 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
1092
1093 if (type != XmlPullParser.START_TAG) {
1094 continue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 }
1096
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001097 boolean added = false;
1098 final String name = parser.getName();
1099
Daniel Sandler57dac262013-10-03 13:28:36 -04001100 if (TAG_INCLUDE.equals(name)) {
1101 final TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.Include);
1102
1103 final int resId = a.getResourceId(R.styleable.Include_workspace, 0);
1104
1105 if (LOGD) Log.v(TAG, String.format(("%" + (2*(depth+1)) + "s<include workspace=%08x>"),
1106 "", resId));
1107
1108 if (resId != 0 && resId != workspaceResourceId) {
1109 // recursively load some more favorites, why not?
1110 i += loadFavorites(db, resId);
1111 added = false;
Daniel Sandler57dac262013-10-03 13:28:36 -04001112 } else {
1113 Log.w(TAG, String.format("Skipping <include workspace=0x%08x>", resId));
1114 }
1115
1116 a.recycle();
1117
1118 if (LOGD) Log.v(TAG, String.format(("%" + (2*(depth+1)) + "s</include>"), ""));
1119 continue;
1120 }
1121
1122 // Assuming it's a <favorite> at this point
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001123 TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.Favorite);
1124
Winson Chung3d503fb2011-07-13 17:25:49 -07001125 long container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
1126 if (a.hasValue(R.styleable.Favorite_container)) {
1127 container = Long.valueOf(a.getString(R.styleable.Favorite_container));
1128 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001129
Winson Chung6d092682011-11-16 18:43:26 -08001130 String screen = a.getString(R.styleable.Favorite_screen);
1131 String x = a.getString(R.styleable.Favorite_x);
1132 String y = a.getString(R.styleable.Favorite_y);
1133
Winson Chung6d092682011-11-16 18:43:26 -08001134 values.clear();
1135 values.put(LauncherSettings.Favorites.CONTAINER, container);
1136 values.put(LauncherSettings.Favorites.SCREEN, screen);
1137 values.put(LauncherSettings.Favorites.CELLX, x);
1138 values.put(LauncherSettings.Favorites.CELLY, y);
1139
Daniel Sandler57dac262013-10-03 13:28:36 -04001140 if (LOGD) {
1141 final String title = a.getString(R.styleable.Favorite_title);
1142 final String pkg = a.getString(R.styleable.Favorite_packageName);
1143 final String something = title != null ? title : pkg;
1144 Log.v(TAG, String.format(
1145 ("%" + (2*(depth+1)) + "s<%s%s c=%d s=%s x=%s y=%s>"),
1146 "", name,
1147 (something == null ? "" : (" \"" + something + "\"")),
1148 container, screen, x, y));
1149 }
1150
Winson Chung6d092682011-11-16 18:43:26 -08001151 if (TAG_FAVORITE.equals(name)) {
1152 long id = addAppShortcut(db, values, a, packageManager, intent);
1153 added = id >= 0;
1154 } else if (TAG_SEARCH.equals(name)) {
1155 added = addSearchWidget(db, values);
1156 } else if (TAG_CLOCK.equals(name)) {
1157 added = addClockWidget(db, values);
1158 } else if (TAG_APPWIDGET.equals(name)) {
Winson Chungb3302ae2012-05-01 10:19:14 -07001159 added = addAppWidget(parser, attrs, type, db, values, a, packageManager);
Winson Chung6d092682011-11-16 18:43:26 -08001160 } else if (TAG_SHORTCUT.equals(name)) {
1161 long id = addUriShortcut(db, values, a);
1162 added = id >= 0;
1163 } else if (TAG_FOLDER.equals(name)) {
1164 String title;
1165 int titleResId = a.getResourceId(R.styleable.Favorite_title, -1);
1166 if (titleResId != -1) {
1167 title = mContext.getResources().getString(titleResId);
1168 } else {
1169 title = mContext.getResources().getString(R.string.folder_name);
Winson Chung3d503fb2011-07-13 17:25:49 -07001170 }
Winson Chung6d092682011-11-16 18:43:26 -08001171 values.put(LauncherSettings.Favorites.TITLE, title);
1172 long folderId = addFolder(db, values);
1173 added = folderId >= 0;
Winson Chung3d503fb2011-07-13 17:25:49 -07001174
Winson Chung6d092682011-11-16 18:43:26 -08001175 ArrayList<Long> folderItems = new ArrayList<Long>();
Winson Chung3d503fb2011-07-13 17:25:49 -07001176
Winson Chung6d092682011-11-16 18:43:26 -08001177 int folderDepth = parser.getDepth();
1178 while ((type = parser.next()) != XmlPullParser.END_TAG ||
1179 parser.getDepth() > folderDepth) {
1180 if (type != XmlPullParser.START_TAG) {
1181 continue;
1182 }
1183 final String folder_item_name = parser.getName();
1184
1185 TypedArray ar = mContext.obtainStyledAttributes(attrs,
1186 R.styleable.Favorite);
1187 values.clear();
1188 values.put(LauncherSettings.Favorites.CONTAINER, folderId);
1189
Daniel Sandler57dac262013-10-03 13:28:36 -04001190 if (LOGD) {
1191 final String pkg = ar.getString(R.styleable.Favorite_packageName);
1192 final String uri = ar.getString(R.styleable.Favorite_uri);
1193 Log.v(TAG, String.format(("%" + (2*(folderDepth+1)) + "s<%s \"%s\">"), "",
1194 folder_item_name, uri != null ? uri : pkg));
1195 }
1196
Winson Chung6d092682011-11-16 18:43:26 -08001197 if (TAG_FAVORITE.equals(folder_item_name) && folderId >= 0) {
1198 long id =
1199 addAppShortcut(db, values, ar, packageManager, intent);
1200 if (id >= 0) {
1201 folderItems.add(id);
1202 }
1203 } else if (TAG_SHORTCUT.equals(folder_item_name) && folderId >= 0) {
1204 long id = addUriShortcut(db, values, ar);
1205 if (id >= 0) {
1206 folderItems.add(id);
1207 }
Adam Cohen228da5a2011-07-27 22:23:47 -07001208 } else {
Winson Chung6d092682011-11-16 18:43:26 -08001209 throw new RuntimeException("Folders can " +
1210 "contain only shortcuts");
Adam Cohen228da5a2011-07-27 22:23:47 -07001211 }
Winson Chung6d092682011-11-16 18:43:26 -08001212 ar.recycle();
1213 }
1214 // We can only have folders with >= 2 items, so we need to remove the
1215 // folder and clean up if less than 2 items were included, or some
1216 // failed to add, and less than 2 were actually added
1217 if (folderItems.size() < 2 && folderId >= 0) {
1218 // We just delete the folder and any items that made it
1219 deleteId(db, folderId);
1220 if (folderItems.size() > 0) {
1221 deleteId(db, folderItems.get(0));
Adam Cohen228da5a2011-07-27 22:23:47 -07001222 }
Winson Chung6d092682011-11-16 18:43:26 -08001223 added = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001224 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001226 if (added) i++;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001227 a.recycle();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228 }
1229 } catch (XmlPullParserException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001230 Log.w(TAG, "Got exception parsing favorites.", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 } catch (IOException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001232 Log.w(TAG, "Got exception parsing favorites.", e);
Winson Chung3d503fb2011-07-13 17:25:49 -07001233 } catch (RuntimeException e) {
1234 Log.w(TAG, "Got exception parsing favorites.", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001236
Winson Chungc763c4e2013-07-19 13:49:06 -07001237 // Update the max item id after we have loaded the database
1238 if (mMaxItemId == -1) {
1239 mMaxItemId = initializeMaxItemId(db);
1240 }
1241
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001242 return i;
1243 }
1244
Adam Cohen228da5a2011-07-27 22:23:47 -07001245 private long addAppShortcut(SQLiteDatabase db, ContentValues values, TypedArray a,
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001246 PackageManager packageManager, Intent intent) {
Adam Cohen228da5a2011-07-27 22:23:47 -07001247 long id = -1;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001248 ActivityInfo info;
1249 String packageName = a.getString(R.styleable.Favorite_packageName);
1250 String className = a.getString(R.styleable.Favorite_className);
1251 try {
Romain Guy693599f2010-03-23 10:58:18 -07001252 ComponentName cn;
1253 try {
1254 cn = new ComponentName(packageName, className);
1255 info = packageManager.getActivityInfo(cn, 0);
1256 } catch (PackageManager.NameNotFoundException nnfe) {
1257 String[] packages = packageManager.currentToCanonicalPackageNames(
1258 new String[] { packageName });
1259 cn = new ComponentName(packages[0], className);
1260 info = packageManager.getActivityInfo(cn, 0);
1261 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001262 id = generateNewItemId();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001263 intent.setComponent(cn);
Romain Guy693599f2010-03-23 10:58:18 -07001264 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1265 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Romain Guy1ce1a242009-06-23 17:34:54 -07001266 values.put(Favorites.INTENT, intent.toUri(0));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001267 values.put(Favorites.TITLE, info.loadLabel(packageManager).toString());
1268 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPLICATION);
1269 values.put(Favorites.SPANX, 1);
1270 values.put(Favorites.SPANY, 1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001271 values.put(Favorites._ID, generateNewItemId());
Adam Cohen228da5a2011-07-27 22:23:47 -07001272 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values) < 0) {
1273 return -1;
1274 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001275 } catch (PackageManager.NameNotFoundException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001276 Log.w(TAG, "Unable to add favorite: " + packageName +
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001277 "/" + className, e);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001278 }
Adam Cohen228da5a2011-07-27 22:23:47 -07001279 return id;
1280 }
1281
1282 private long addFolder(SQLiteDatabase db, ContentValues values) {
1283 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_FOLDER);
1284 values.put(Favorites.SPANX, 1);
1285 values.put(Favorites.SPANY, 1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001286 long id = generateNewItemId();
Adam Cohen228da5a2011-07-27 22:23:47 -07001287 values.put(Favorites._ID, id);
1288 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values) <= 0) {
1289 return -1;
1290 } else {
1291 return id;
1292 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001293 }
1294
Bjorn Bringertcd8fec02010-01-14 13:26:43 +00001295 private ComponentName getSearchWidgetProvider() {
1296 SearchManager searchManager =
1297 (SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE);
1298 ComponentName searchComponent = searchManager.getGlobalSearchActivity();
1299 if (searchComponent == null) return null;
1300 return getProviderInPackage(searchComponent.getPackageName());
1301 }
1302
1303 /**
1304 * Gets an appwidget provider from the given package. If the package contains more than
1305 * one appwidget provider, an arbitrary one is returned.
1306 */
1307 private ComponentName getProviderInPackage(String packageName) {
1308 AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
1309 List<AppWidgetProviderInfo> providers = appWidgetManager.getInstalledProviders();
1310 if (providers == null) return null;
1311 final int providerCount = providers.size();
1312 for (int i = 0; i < providerCount; i++) {
1313 ComponentName provider = providers.get(i).provider;
1314 if (provider != null && provider.getPackageName().equals(packageName)) {
1315 return provider;
1316 }
1317 }
1318 return null;
1319 }
1320
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001321 private boolean addSearchWidget(SQLiteDatabase db, ContentValues values) {
Bjorn Bringertcd8fec02010-01-14 13:26:43 +00001322 ComponentName cn = getSearchWidgetProvider();
Winson Chungb3302ae2012-05-01 10:19:14 -07001323 return addAppWidget(db, values, cn, 4, 1, null);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001324 }
1325
1326 private boolean addClockWidget(SQLiteDatabase db, ContentValues values) {
Bjorn Bringert34251342009-12-15 13:33:11 +00001327 ComponentName cn = new ComponentName("com.android.alarmclock",
1328 "com.android.alarmclock.AnalogAppWidgetProvider");
Winson Chungb3302ae2012-05-01 10:19:14 -07001329 return addAppWidget(db, values, cn, 2, 2, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 }
Adam Cohen228da5a2011-07-27 22:23:47 -07001331
Winson Chungb3302ae2012-05-01 10:19:14 -07001332 private boolean addAppWidget(XmlResourceParser parser, AttributeSet attrs, int type,
1333 SQLiteDatabase db, ContentValues values, TypedArray a,
1334 PackageManager packageManager) throws XmlPullParserException, IOException {
Romain Guy693599f2010-03-23 10:58:18 -07001335
Mike Cleronb87bd162009-10-30 16:36:56 -07001336 String packageName = a.getString(R.styleable.Favorite_packageName);
1337 String className = a.getString(R.styleable.Favorite_className);
1338
1339 if (packageName == null || className == null) {
1340 return false;
1341 }
Romain Guy693599f2010-03-23 10:58:18 -07001342
1343 boolean hasPackage = true;
Mike Cleronb87bd162009-10-30 16:36:56 -07001344 ComponentName cn = new ComponentName(packageName, className);
Romain Guy693599f2010-03-23 10:58:18 -07001345 try {
1346 packageManager.getReceiverInfo(cn, 0);
1347 } catch (Exception e) {
1348 String[] packages = packageManager.currentToCanonicalPackageNames(
1349 new String[] { packageName });
1350 cn = new ComponentName(packages[0], className);
1351 try {
1352 packageManager.getReceiverInfo(cn, 0);
1353 } catch (Exception e1) {
1354 hasPackage = false;
1355 }
1356 }
1357
1358 if (hasPackage) {
1359 int spanX = a.getInt(R.styleable.Favorite_spanX, 0);
1360 int spanY = a.getInt(R.styleable.Favorite_spanY, 0);
Winson Chungb3302ae2012-05-01 10:19:14 -07001361
1362 // Read the extras
1363 Bundle extras = new Bundle();
1364 int widgetDepth = parser.getDepth();
1365 while ((type = parser.next()) != XmlPullParser.END_TAG ||
1366 parser.getDepth() > widgetDepth) {
1367 if (type != XmlPullParser.START_TAG) {
1368 continue;
1369 }
1370
1371 TypedArray ar = mContext.obtainStyledAttributes(attrs, R.styleable.Extra);
1372 if (TAG_EXTRA.equals(parser.getName())) {
1373 String key = ar.getString(R.styleable.Extra_key);
1374 String value = ar.getString(R.styleable.Extra_value);
1375 if (key != null && value != null) {
1376 extras.putString(key, value);
1377 } else {
1378 throw new RuntimeException("Widget extras must have a key and value");
1379 }
1380 } else {
1381 throw new RuntimeException("Widgets can contain only extras");
1382 }
1383 ar.recycle();
1384 }
1385
1386 return addAppWidget(db, values, cn, spanX, spanY, extras);
Romain Guy693599f2010-03-23 10:58:18 -07001387 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001388
Romain Guy693599f2010-03-23 10:58:18 -07001389 return false;
Bjorn Bringert7984c942009-12-09 15:38:25 +00001390 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001391 private boolean addAppWidget(SQLiteDatabase db, ContentValues values, ComponentName cn,
Winson Chungb3302ae2012-05-01 10:19:14 -07001392 int spanX, int spanY, Bundle extras) {
Mike Cleronb87bd162009-10-30 16:36:56 -07001393 boolean allocatedAppWidgets = false;
1394 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
1395
1396 try {
1397 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001398
Mike Cleronb87bd162009-10-30 16:36:56 -07001399 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001400 values.put(Favorites.SPANX, spanX);
1401 values.put(Favorites.SPANY, spanY);
Mike Cleronb87bd162009-10-30 16:36:56 -07001402 values.put(Favorites.APPWIDGET_ID, appWidgetId);
Chris Wrend5e66bf2013-09-16 14:02:29 -04001403 values.put(Favorites.APPWIDGET_PROVIDER, cn.flattenToString());
Adam Cohendcd297f2013-06-18 13:13:40 -07001404 values.put(Favorites._ID, generateNewItemId());
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001405 dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values);
Mike Cleronb87bd162009-10-30 16:36:56 -07001406
1407 allocatedAppWidgets = true;
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001408
Michael Jurka8b805b12012-04-18 14:23:14 -07001409 // TODO: need to check return value
1410 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, cn);
Winson Chungb3302ae2012-05-01 10:19:14 -07001411
1412 // Send a broadcast to configure the widget
1413 if (extras != null && !extras.isEmpty()) {
1414 Intent intent = new Intent(ACTION_APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE);
1415 intent.setComponent(cn);
1416 intent.putExtras(extras);
1417 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1418 mContext.sendBroadcast(intent);
1419 }
Mike Cleronb87bd162009-10-30 16:36:56 -07001420 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001421 Log.e(TAG, "Problem allocating appWidgetId", ex);
Mike Cleronb87bd162009-10-30 16:36:56 -07001422 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001423
Mike Cleronb87bd162009-10-30 16:36:56 -07001424 return allocatedAppWidgets;
1425 }
Adam Cohen228da5a2011-07-27 22:23:47 -07001426
1427 private long addUriShortcut(SQLiteDatabase db, ContentValues values,
Mike Cleronb87bd162009-10-30 16:36:56 -07001428 TypedArray a) {
1429 Resources r = mContext.getResources();
1430
1431 final int iconResId = a.getResourceId(R.styleable.Favorite_icon, 0);
1432 final int titleResId = a.getResourceId(R.styleable.Favorite_title, 0);
1433
Romain Guy7eb9e5e2009-12-02 20:10:07 -08001434 Intent intent;
Mike Cleronb87bd162009-10-30 16:36:56 -07001435 String uri = null;
1436 try {
1437 uri = a.getString(R.styleable.Favorite_uri);
1438 intent = Intent.parseUri(uri, 0);
1439 } catch (URISyntaxException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001440 Log.w(TAG, "Shortcut has malformed uri: " + uri);
Adam Cohen228da5a2011-07-27 22:23:47 -07001441 return -1; // Oh well
Mike Cleronb87bd162009-10-30 16:36:56 -07001442 }
1443
1444 if (iconResId == 0 || titleResId == 0) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001445 Log.w(TAG, "Shortcut is missing title or icon resource ID");
Adam Cohen228da5a2011-07-27 22:23:47 -07001446 return -1;
Mike Cleronb87bd162009-10-30 16:36:56 -07001447 }
1448
Adam Cohendcd297f2013-06-18 13:13:40 -07001449 long id = generateNewItemId();
Mike Cleronb87bd162009-10-30 16:36:56 -07001450 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1451 values.put(Favorites.INTENT, intent.toUri(0));
1452 values.put(Favorites.TITLE, r.getString(titleResId));
1453 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_SHORTCUT);
1454 values.put(Favorites.SPANX, 1);
1455 values.put(Favorites.SPANY, 1);
1456 values.put(Favorites.ICON_TYPE, Favorites.ICON_TYPE_RESOURCE);
1457 values.put(Favorites.ICON_PACKAGE, mContext.getPackageName());
1458 values.put(Favorites.ICON_RESOURCE, r.getResourceName(iconResId));
Adam Cohen228da5a2011-07-27 22:23:47 -07001459 values.put(Favorites._ID, id);
Mike Cleronb87bd162009-10-30 16:36:56 -07001460
Adam Cohen228da5a2011-07-27 22:23:47 -07001461 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values) < 0) {
1462 return -1;
1463 }
1464 return id;
Mike Cleronb87bd162009-10-30 16:36:56 -07001465 }
Dan Sandlerd5024042014-01-09 15:01:33 -05001466
1467 public void migrateLauncher2Shortcuts(SQLiteDatabase db, Uri uri) {
1468 final ContentResolver resolver = mContext.getContentResolver();
1469 Cursor c = null;
1470 int count = 0;
1471 int curScreen = 0;
1472
1473 try {
1474 c = resolver.query(uri, null, null, null, "title ASC");
1475 } catch (Exception e) {
1476 // Ignore
1477 }
1478
Dan Sandlerd5024042014-01-09 15:01:33 -05001479 // We already have a favorites database in the old provider
1480 if (c != null) {
1481 try {
1482 if (c.getCount() > 0) {
1483 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1484 final int intentIndex
1485 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
1486 final int titleIndex
1487 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
1488 final int iconTypeIndex
1489 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
1490 final int iconIndex
1491 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1492 final int iconPackageIndex
1493 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
1494 final int iconResourceIndex
1495 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
1496 final int containerIndex
1497 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
1498 final int itemTypeIndex
1499 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
1500 final int screenIndex
1501 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
1502 final int cellXIndex
1503 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
1504 final int cellYIndex
1505 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
1506 final int uriIndex
1507 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1508 final int displayModeIndex
1509 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
1510
1511 int i = 0;
1512 int curX = 0;
1513 int curY = 0;
1514
1515 final LauncherAppState app = LauncherAppState.getInstance();
1516 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1517 final int width = (int) grid.numColumns;
1518 final int height = (int) grid.numRows;
1519 final int hotseatWidth = (int) grid.numHotseatIcons;
Adam Cohen556f6132014-01-15 15:18:08 -08001520 PackageManager pm = mContext.getPackageManager();
Dan Sandlerd5024042014-01-09 15:01:33 -05001521
1522 final HashSet<String> seenIntents = new HashSet<String>(c.getCount());
1523
Adam Cohen72960972014-01-15 18:13:55 -08001524 final ArrayList<ContentValues> shortcuts = new ArrayList<ContentValues>();
1525 final ArrayList<ContentValues> folders = new ArrayList<ContentValues>();
Dan Sandlerd5024042014-01-09 15:01:33 -05001526
1527 while (c.moveToNext()) {
1528 final int itemType = c.getInt(itemTypeIndex);
1529 if (itemType != Favorites.ITEM_TYPE_APPLICATION
1530 && itemType != Favorites.ITEM_TYPE_SHORTCUT
1531 && itemType != Favorites.ITEM_TYPE_FOLDER) {
1532 continue;
1533 }
1534
1535 final int cellX = c.getInt(cellXIndex);
1536 final int cellY = c.getInt(cellYIndex);
1537 final int screen = c.getInt(screenIndex);
1538 int container = c.getInt(containerIndex);
1539 final String intentStr = c.getString(intentIndex);
1540 Launcher.addDumpLog(TAG, "migrating \""
1541 + c.getString(titleIndex) + "\": " + intentStr, true);
1542
1543 if (itemType != Favorites.ITEM_TYPE_FOLDER) {
Adam Cohen556f6132014-01-15 15:18:08 -08001544
1545 final Intent intent;
1546 final ComponentName cn;
1547 try {
1548 intent = Intent.parseUri(intentStr, 0);
1549 } catch (URISyntaxException e) {
1550 // bogus intent?
1551 Launcher.addDumpLog(TAG,
1552 "skipping invalid intent uri", true);
1553 continue;
1554 }
1555
1556 cn = intent.getComponent();
Dan Sandlerd5024042014-01-09 15:01:33 -05001557 if (TextUtils.isEmpty(intentStr)) {
1558 // no intent? no icon
1559 Launcher.addDumpLog(TAG, "skipping empty intent", true);
1560 continue;
Adam Cohen72960972014-01-15 18:13:55 -08001561 } else if (cn != null &&
1562 !LauncherModel.isValidPackageComponent(pm, cn)) {
Adam Cohen556f6132014-01-15 15:18:08 -08001563 // component no longer exists.
Adam Cohen72960972014-01-15 18:13:55 -08001564 Launcher.addDumpLog(TAG, "skipping item whose component " +
Adam Cohen556f6132014-01-15 15:18:08 -08001565 "no longer exists.", true);
1566 continue;
Adam Cohen72960972014-01-15 18:13:55 -08001567 } else if (container ==
1568 LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1569 // Dedupe icons directly on the workspace
1570
Adam Cohen556f6132014-01-15 15:18:08 -08001571 // Canonicalize
1572 // the Play Store sets the package parameter, but Launcher
1573 // does not, so we clear that out to keep them the same
1574 intent.setPackage(null);
1575 final String key = intent.toUri(0);
1576 if (seenIntents.contains(key)) {
1577 Launcher.addDumpLog(TAG, "skipping duplicate", true);
Dan Sandlerd5024042014-01-09 15:01:33 -05001578 continue;
Adam Cohen556f6132014-01-15 15:18:08 -08001579 } else {
1580 seenIntents.add(key);
Dan Sandlerd5024042014-01-09 15:01:33 -05001581 }
1582 }
1583 }
1584
1585 ContentValues values = new ContentValues(c.getColumnCount());
1586 values.put(LauncherSettings.Favorites._ID, c.getInt(idIndex));
1587 values.put(LauncherSettings.Favorites.INTENT, intentStr);
1588 values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex));
1589 values.put(LauncherSettings.Favorites.ICON_TYPE,
1590 c.getInt(iconTypeIndex));
1591 values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex));
1592 values.put(LauncherSettings.Favorites.ICON_PACKAGE,
1593 c.getString(iconPackageIndex));
1594 values.put(LauncherSettings.Favorites.ICON_RESOURCE,
1595 c.getString(iconResourceIndex));
1596 values.put(LauncherSettings.Favorites.ITEM_TYPE, itemType);
1597 values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1);
1598 values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex));
1599 values.put(LauncherSettings.Favorites.DISPLAY_MODE,
1600 c.getInt(displayModeIndex));
1601
1602 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT
Adam Cohen72960972014-01-15 18:13:55 -08001603 && (screen >= hotseatWidth ||
1604 screen == grid.hotseatAllAppsRank)) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001605 // no room for you in the hotseat? it's off to the desktop with you
1606 container = Favorites.CONTAINER_DESKTOP;
1607 }
1608
1609 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1610 // In a folder or in the hotseat, preserve position
1611 values.put(LauncherSettings.Favorites.SCREEN, screen);
1612 values.put(LauncherSettings.Favorites.CELLX, cellX);
1613 values.put(LauncherSettings.Favorites.CELLY, cellY);
1614 } else {
Adam Cohen72960972014-01-15 18:13:55 -08001615 // For items contained directly on one of the workspace screen,
1616 // we'll determine their location (screen, x, y) in a second pass.
Dan Sandlerd5024042014-01-09 15:01:33 -05001617 }
1618
1619 values.put(LauncherSettings.Favorites.CONTAINER, container);
1620
Adam Cohen72960972014-01-15 18:13:55 -08001621 if (itemType != Favorites.ITEM_TYPE_FOLDER) {
1622 shortcuts.add(values);
1623 } else {
1624 folders.add(values);
1625 }
Dan Sandlerd5024042014-01-09 15:01:33 -05001626 }
1627
Adam Cohen72960972014-01-15 18:13:55 -08001628 final ArrayList<ContentValues> allItems = new ArrayList<ContentValues>();
1629 // Folders first
1630 allItems.addAll(folders);
1631 // Then shortcuts
1632 allItems.addAll(shortcuts);
1633
1634 // Layout all the folders
1635 for (ContentValues values: allItems) {
1636 if (values.getAsInteger(LauncherSettings.Favorites.CONTAINER) !=
1637 LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1638 // Hotseat items and folder items have already had their
1639 // location information set. Nothing to be done here.
1640 continue;
1641 }
1642 values.put(LauncherSettings.Favorites.SCREEN, curScreen);
1643 values.put(LauncherSettings.Favorites.CELLX, curX);
1644 values.put(LauncherSettings.Favorites.CELLY, curY);
1645 curX = (curX + 1) % width;
1646 if (curX == 0) {
1647 curY = (curY + 1);
1648 }
1649 // Leave the last row of icons blank on every screen
1650 if (curY == height - 1) {
1651 curScreen = (int) generateNewScreenId();
1652 curY = 0;
1653 }
1654 }
1655
1656 if (allItems.size() > 0) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001657 db.beginTransaction();
1658 try {
Adam Cohen72960972014-01-15 18:13:55 -08001659 for (ContentValues row: allItems) {
1660 if (row == null) continue;
1661 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, row)
Dan Sandlerd5024042014-01-09 15:01:33 -05001662 < 0) {
1663 return;
1664 } else {
1665 count++;
1666 }
1667 }
1668 db.setTransactionSuccessful();
1669 } finally {
1670 db.endTransaction();
1671 }
1672 }
1673
1674 db.beginTransaction();
1675 try {
1676 for (i=0; i<=curScreen; i++) {
1677 final ContentValues values = new ContentValues();
1678 values.put(LauncherSettings.WorkspaceScreens._ID, i);
1679 values.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
1680 if (dbInsertAndCheck(this, db, TABLE_WORKSPACE_SCREENS, null, values)
1681 < 0) {
1682 return;
1683 }
1684 }
1685 db.setTransactionSuccessful();
1686 } finally {
1687 db.endTransaction();
1688 }
1689 }
1690 } finally {
1691 c.close();
1692 }
1693 }
1694
1695 Launcher.addDumpLog(TAG, "migrated " + count + " icons from Launcher2 into "
1696 + (curScreen+1) + " screens", true);
1697
1698 // ensure that new screens are created to hold these icons
1699 setFlagJustLoadedOldDb();
1700
1701 // Update max IDs; very important since we just grabbed IDs from another database
1702 mMaxItemId = initializeMaxItemId(db);
1703 mMaxScreenId = initializeMaxScreenId(db);
1704 if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId + " mMaxScreenId: " + mMaxScreenId);
1705 }
Mike Cleronb87bd162009-10-30 16:36:56 -07001706 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001707
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001708 /**
1709 * Build a query string that will match any row where the column matches
1710 * anything in the values list.
1711 */
1712 static String buildOrWhereString(String column, int[] values) {
1713 StringBuilder selectWhere = new StringBuilder();
1714 for (int i = values.length - 1; i >= 0; i--) {
1715 selectWhere.append(column).append("=").append(values[i]);
1716 if (i > 0) {
1717 selectWhere.append(" OR ");
1718 }
1719 }
1720 return selectWhere.toString();
1721 }
1722
1723 static class SqlArguments {
1724 public final String table;
1725 public final String where;
1726 public final String[] args;
1727
1728 SqlArguments(Uri url, String where, String[] args) {
1729 if (url.getPathSegments().size() == 1) {
1730 this.table = url.getPathSegments().get(0);
1731 this.where = where;
1732 this.args = args;
1733 } else if (url.getPathSegments().size() != 2) {
1734 throw new IllegalArgumentException("Invalid URI: " + url);
1735 } else if (!TextUtils.isEmpty(where)) {
1736 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
1737 } else {
1738 this.table = url.getPathSegments().get(0);
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001739 this.where = "_id=" + ContentUris.parseId(url);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001740 this.args = null;
1741 }
1742 }
1743
1744 SqlArguments(Uri url) {
1745 if (url.getPathSegments().size() == 1) {
1746 table = url.getPathSegments().get(0);
1747 where = null;
1748 args = null;
1749 } else {
1750 throw new IllegalArgumentException("Invalid URI: " + url);
1751 }
1752 }
1753 }
Adam Cohen72960972014-01-15 18:13:55 -08001754}