blob: a69f4235d86b29e72b3c7aef1606f98377a69b89 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Bjorn Bringertcd8fec02010-01-14 13:26:43 +000019import android.app.SearchManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070020import android.appwidget.AppWidgetHost;
Mike Cleronb87bd162009-10-30 16:36:56 -070021import android.appwidget.AppWidgetManager;
Bjorn Bringertcd8fec02010-01-14 13:26:43 +000022import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.content.ComponentName;
Adam Cohen228da5a2011-07-27 22:23:47 -070024import android.content.ContentProvider;
Yura085c8532014-02-11 15:15:29 +000025import android.content.ContentProviderOperation;
26import android.content.ContentProviderResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.content.ContentResolver;
Adam Cohen228da5a2011-07-27 22:23:47 -070028import android.content.ContentUris;
29import android.content.ContentValues;
30import android.content.Context;
31import android.content.Intent;
Yura085c8532014-02-11 15:15:29 +000032import android.content.OperationApplicationException;
Michael Jurkab85f8a42012-04-25 15:48:32 -070033import android.content.SharedPreferences;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.pm.ActivityInfo;
Adam Cohen228da5a2011-07-27 22:23:47 -070035import android.content.pm.PackageManager;
36import android.content.res.Resources;
37import android.content.res.TypedArray;
38import android.content.res.XmlResourceParser;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.database.Cursor;
40import android.database.SQLException;
Adam Cohen228da5a2011-07-27 22:23:47 -070041import android.database.sqlite.SQLiteDatabase;
42import android.database.sqlite.SQLiteOpenHelper;
43import android.database.sqlite.SQLiteQueryBuilder;
44import android.database.sqlite.SQLiteStatement;
Joe Onorato0589f0f2010-02-08 13:44:00 -080045import android.graphics.Bitmap;
46import android.graphics.BitmapFactory;
Adam Cohen228da5a2011-07-27 22:23:47 -070047import android.net.Uri;
Winson Chungb3302ae2012-05-01 10:19:14 -070048import android.os.Bundle;
Adam Cohen228da5a2011-07-27 22:23:47 -070049import android.provider.Settings;
50import android.text.TextUtils;
51import android.util.AttributeSet;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.util.Log;
53import android.util.Xml;
Adam Cohen228da5a2011-07-27 22:23:47 -070054
Daniel Sandler325dc232013-06-05 22:57:57 -040055import com.android.launcher3.LauncherSettings.Favorites;
Chris Wrene523e702013-10-09 10:36:55 -040056import com.android.launcher3.config.ProviderConfig;
Michael Jurka8b805b12012-04-18 14:23:14 -070057
58import org.xmlpull.v1.XmlPullParser;
59import org.xmlpull.v1.XmlPullParserException;
60
Dan Sandlerd5024042014-01-09 15:01:33 -050061import java.io.File;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import java.io.IOException;
Mike Cleronb87bd162009-10-30 16:36:56 -070063import java.net.URISyntaxException;
Adam Cohen228da5a2011-07-27 22:23:47 -070064import java.util.ArrayList;
Dan Sandlerd5024042014-01-09 15:01:33 -050065import java.util.HashSet;
Bjorn Bringertcd8fec02010-01-14 13:26:43 +000066import java.util.List;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068public class LauncherProvider extends ContentProvider {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080069 private static final String TAG = "Launcher.LauncherProvider";
70 private static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071
72 private static final String DATABASE_NAME = "launcher.db";
Winson Chung3d503fb2011-07-13 17:25:49 -070073
Chris Wrenf4d08112014-01-16 18:13:56 -050074 private static final int DATABASE_VERSION = 16;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075
Adam Cohene25af792013-06-06 23:08:25 -070076 static final String OLD_AUTHORITY = "com.android.launcher2.settings";
Chris Wrene523e702013-10-09 10:36:55 -040077 static final String AUTHORITY = ProviderConfig.AUTHORITY;
Winson Chung3d503fb2011-07-13 17:25:49 -070078
Dan Sandlerf0b8dac2013-11-19 12:21:25 -050079 // Should we attempt to load anything from the com.android.launcher2 provider?
Dan Sandlerd5024042014-01-09 15:01:33 -050080 static final boolean IMPORT_LAUNCHER2_DATABASE = false;
Dan Sandlerf0b8dac2013-11-19 12:21:25 -050081
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082 static final String TABLE_FAVORITES = "favorites";
Adam Cohendcd297f2013-06-18 13:13:40 -070083 static final String TABLE_WORKSPACE_SCREENS = "workspaceScreens";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084 static final String PARAMETER_NOTIFY = "notify";
Winson Chungc763c4e2013-07-19 13:49:06 -070085 static final String UPGRADED_FROM_OLD_DATABASE =
86 "UPGRADED_FROM_OLD_DATABASE";
87 static final String EMPTY_DATABASE_CREATED =
88 "EMPTY_DATABASE_CREATED";
Michael Jurka45355c42012-10-08 13:21:35 +020089 static final String DEFAULT_WORKSPACE_RESOURCE_ID =
90 "DEFAULT_WORKSPACE_RESOURCE_ID";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091
Winson Chungb3302ae2012-05-01 10:19:14 -070092 private static final String ACTION_APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE =
Adam Cohene25af792013-06-06 23:08:25 -070093 "com.android.launcher.action.APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE";
Winson Chungb3302ae2012-05-01 10:19:14 -070094
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -070095 /**
Romain Guy73b979d2009-06-09 12:57:21 -070096 * {@link Uri} triggered at any registered {@link android.database.ContentObserver} when
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -070097 * {@link AppWidgetHost#deleteHost()} is called during database creation.
98 * Use this to recall {@link AppWidgetHost#startListening()} if needed.
99 */
100 static final Uri CONTENT_APPWIDGET_RESET_URI =
101 Uri.parse("content://" + AUTHORITY + "/appWidgetReset");
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700102
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700103 private DatabaseHelper mOpenHelper;
Winson Chungc763c4e2013-07-19 13:49:06 -0700104 private static boolean sJustLoadedFromOldDb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105
106 @Override
107 public boolean onCreate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400108 final Context context = getContext();
109 mOpenHelper = new DatabaseHelper(context);
110 LauncherAppState.setLauncherProvider(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111 return true;
112 }
113
Winson Chung0b560dd2014-01-21 13:00:26 -0800114 public boolean wasNewDbCreated() {
115 return mOpenHelper.wasNewDbCreated();
116 }
117
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118 @Override
119 public String getType(Uri uri) {
120 SqlArguments args = new SqlArguments(uri, null, null);
121 if (TextUtils.isEmpty(args.where)) {
122 return "vnd.android.cursor.dir/" + args.table;
123 } else {
124 return "vnd.android.cursor.item/" + args.table;
125 }
126 }
127
128 @Override
129 public Cursor query(Uri uri, String[] projection, String selection,
130 String[] selectionArgs, String sortOrder) {
131
132 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
133 SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
134 qb.setTables(args.table);
135
Romain Guy73b979d2009-06-09 12:57:21 -0700136 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 Cursor result = qb.query(db, projection, args.where, args.args, null, null, sortOrder);
138 result.setNotificationUri(getContext().getContentResolver(), uri);
139
140 return result;
141 }
142
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700143 private static long dbInsertAndCheck(DatabaseHelper helper,
144 SQLiteDatabase db, String table, String nullColumnHack, ContentValues values) {
Dan Sandlerd5024042014-01-09 15:01:33 -0500145 if (values == null) {
146 throw new RuntimeException("Error: attempting to insert null values");
147 }
Chris Wren5dee7af2013-12-20 17:22:11 -0500148 if (!values.containsKey(LauncherSettings.BaseLauncherColumns._ID)) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700149 throw new RuntimeException("Error: attempting to add item without specifying an id");
150 }
Chris Wren5dee7af2013-12-20 17:22:11 -0500151 helper.checkId(table, values);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700152 return db.insert(table, nullColumnHack, values);
153 }
154
Adam Cohen228da5a2011-07-27 22:23:47 -0700155 private static void deleteId(SQLiteDatabase db, long id) {
156 Uri uri = LauncherSettings.Favorites.getContentUri(id, false);
157 SqlArguments args = new SqlArguments(uri, null, null);
158 db.delete(args.table, args.where, args.args);
159 }
160
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 @Override
162 public Uri insert(Uri uri, ContentValues initialValues) {
163 SqlArguments args = new SqlArguments(uri);
164
165 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
Chris Wren1ada10d2013-09-13 18:01:38 -0400166 addModifiedTime(initialValues);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700167 final long rowId = dbInsertAndCheck(mOpenHelper, db, args.table, null, initialValues);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 if (rowId <= 0) return null;
169
170 uri = ContentUris.withAppendedId(uri, rowId);
171 sendNotify(uri);
172
173 return uri;
174 }
175
176 @Override
177 public int bulkInsert(Uri uri, ContentValues[] values) {
178 SqlArguments args = new SqlArguments(uri);
179
180 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
181 db.beginTransaction();
182 try {
183 int numValues = values.length;
184 for (int i = 0; i < numValues; i++) {
Chris Wren1ada10d2013-09-13 18:01:38 -0400185 addModifiedTime(values[i]);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700186 if (dbInsertAndCheck(mOpenHelper, db, args.table, null, values[i]) < 0) {
187 return 0;
188 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 }
190 db.setTransactionSuccessful();
191 } finally {
192 db.endTransaction();
193 }
194
195 sendNotify(uri);
196 return values.length;
197 }
198
199 @Override
Yura085c8532014-02-11 15:15:29 +0000200 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
201 throws OperationApplicationException {
202 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
203 db.beginTransaction();
204 try {
205 ContentProviderResult[] result = super.applyBatch(operations);
206 db.setTransactionSuccessful();
207 return result;
208 } finally {
209 db.endTransaction();
210 }
211 }
212
213 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214 public int delete(Uri uri, String selection, String[] selectionArgs) {
215 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
216
217 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
218 int count = db.delete(args.table, args.where, args.args);
219 if (count > 0) sendNotify(uri);
220
221 return count;
222 }
223
224 @Override
225 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
226 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
227
Chris Wren1ada10d2013-09-13 18:01:38 -0400228 addModifiedTime(values);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
230 int count = db.update(args.table, values, args.where, args.args);
231 if (count > 0) sendNotify(uri);
232
233 return count;
234 }
235
236 private void sendNotify(Uri uri) {
237 String notify = uri.getQueryParameter(PARAMETER_NOTIFY);
238 if (notify == null || "true".equals(notify)) {
239 getContext().getContentResolver().notifyChange(uri, null);
240 }
Chris Wren1ada10d2013-09-13 18:01:38 -0400241
242 // always notify the backup agent
Chris Wren92aa4232013-10-04 11:29:36 -0400243 LauncherBackupAgentHelper.dataChanged(getContext());
Chris Wren1ada10d2013-09-13 18:01:38 -0400244 }
245
246 private void addModifiedTime(ContentValues values) {
247 values.put(LauncherSettings.ChangeLogColumns.MODIFIED, System.currentTimeMillis());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248 }
249
Adam Cohendcd297f2013-06-18 13:13:40 -0700250 public long generateNewItemId() {
251 return mOpenHelper.generateNewItemId();
252 }
253
Winson Chungc763c4e2013-07-19 13:49:06 -0700254 public void updateMaxItemId(long id) {
255 mOpenHelper.updateMaxItemId(id);
256 }
257
Adam Cohendcd297f2013-06-18 13:13:40 -0700258 public long generateNewScreenId() {
259 return mOpenHelper.generateNewScreenId();
260 }
261
262 // This is only required one time while loading the workspace during the
263 // upgrade path, and should never be called from anywhere else.
264 public void updateMaxScreenId(long maxScreenId) {
265 mOpenHelper.updateMaxScreenId(maxScreenId);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700266 }
267
Brian Muramatsu5524b492012-10-02 16:55:54 -0700268 /**
Adam Cohene25af792013-06-06 23:08:25 -0700269 * @param Should we load the old db for upgrade? first run only.
270 */
Winson Chungc763c4e2013-07-19 13:49:06 -0700271 synchronized public boolean justLoadedOldDb() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400272 String spKey = LauncherAppState.getSharedPreferencesKey();
Adam Cohene25af792013-06-06 23:08:25 -0700273 SharedPreferences sp = getContext().getSharedPreferences(spKey, Context.MODE_PRIVATE);
274
Winson Chungc763c4e2013-07-19 13:49:06 -0700275 boolean loadedOldDb = false || sJustLoadedFromOldDb;
Adam Cohendcd297f2013-06-18 13:13:40 -0700276
Winson Chungc763c4e2013-07-19 13:49:06 -0700277 sJustLoadedFromOldDb = false;
278 if (sp.getBoolean(UPGRADED_FROM_OLD_DATABASE, false)) {
Adam Cohene25af792013-06-06 23:08:25 -0700279
280 SharedPreferences.Editor editor = sp.edit();
Winson Chungc763c4e2013-07-19 13:49:06 -0700281 editor.remove(UPGRADED_FROM_OLD_DATABASE);
Adam Cohene25af792013-06-06 23:08:25 -0700282 editor.commit();
Winson Chungc763c4e2013-07-19 13:49:06 -0700283 loadedOldDb = true;
Adam Cohene25af792013-06-06 23:08:25 -0700284 }
Winson Chungc763c4e2013-07-19 13:49:06 -0700285 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -0700286 }
287
288 /**
Brian Muramatsu5524b492012-10-02 16:55:54 -0700289 * @param workspaceResId that can be 0 to use default or non-zero for specific resource
290 */
Michael Jurka45355c42012-10-08 13:21:35 +0200291 synchronized public void loadDefaultFavoritesIfNecessary(int origWorkspaceResId) {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400292 String spKey = LauncherAppState.getSharedPreferencesKey();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700293 SharedPreferences sp = getContext().getSharedPreferences(spKey, Context.MODE_PRIVATE);
Adam Cohene25af792013-06-06 23:08:25 -0700294
Winson Chungc763c4e2013-07-19 13:49:06 -0700295 if (sp.getBoolean(EMPTY_DATABASE_CREATED, false)) {
Chris Wren5dee7af2013-12-20 17:22:11 -0500296 Log.d(TAG, "loading default workspace");
Michael Jurka45355c42012-10-08 13:21:35 +0200297 int workspaceResId = origWorkspaceResId;
298
Brian Muramatsu5524b492012-10-02 16:55:54 -0700299 // Use default workspace resource if none provided
300 if (workspaceResId == 0) {
Nilesh Agrawalda41ea62013-12-09 14:17:49 -0800301 workspaceResId =
302 sp.getInt(DEFAULT_WORKSPACE_RESOURCE_ID, getDefaultWorkspaceResourceId());
Brian Muramatsu5524b492012-10-02 16:55:54 -0700303 }
304
Michael Jurkab85f8a42012-04-25 15:48:32 -0700305 // Populate favorites table with initial favorites
306 SharedPreferences.Editor editor = sp.edit();
Winson Chungc763c4e2013-07-19 13:49:06 -0700307 editor.remove(EMPTY_DATABASE_CREATED);
Michael Jurka45355c42012-10-08 13:21:35 +0200308 if (origWorkspaceResId != 0) {
309 editor.putInt(DEFAULT_WORKSPACE_RESOURCE_ID, origWorkspaceResId);
310 }
Adam Cohene25af792013-06-06 23:08:25 -0700311
Brian Muramatsu5524b492012-10-02 16:55:54 -0700312 mOpenHelper.loadFavorites(mOpenHelper.getWritableDatabase(), workspaceResId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700313 mOpenHelper.setFlagJustLoadedOldDb();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700314 editor.commit();
315 }
316 }
317
Dan Sandlerd5024042014-01-09 15:01:33 -0500318 public void migrateLauncher2Shortcuts() {
319 mOpenHelper.migrateLauncher2Shortcuts(mOpenHelper.getWritableDatabase(),
320 LauncherSettings.Favorites.OLD_CONTENT_URI);
321 }
322
Nilesh Agrawalda41ea62013-12-09 14:17:49 -0800323 private static int getDefaultWorkspaceResourceId() {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800324 if (LauncherAppState.isDisableAllApps()) {
Nilesh Agrawalda41ea62013-12-09 14:17:49 -0800325 return R.xml.default_workspace_no_all_apps;
326 } else {
327 return R.xml.default_workspace;
328 }
329 }
330
Winson Chungc763c4e2013-07-19 13:49:06 -0700331 private static interface ContentValuesCallback {
332 public void onRow(ContentValues values);
333 }
334
Adam Cohen6dbe0492013-12-02 17:00:14 -0800335 private static boolean shouldImportLauncher2Database(Context context) {
336 boolean isTablet = context.getResources().getBoolean(R.bool.is_tablet);
337
338 // We don't import the old databse for tablets, as the grid size has changed.
339 return !isTablet && IMPORT_LAUNCHER2_DATABASE;
340 }
341
Dan Sandlerd5024042014-01-09 15:01:33 -0500342 public void deleteDatabase() {
343 // Are you sure? (y/n)
344 final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
Dan Sandler2b471742014-01-21 14:14:41 -0500345 final File dbFile = new File(db.getPath());
Dan Sandlerd5024042014-01-09 15:01:33 -0500346 mOpenHelper.close();
Dan Sandler2b471742014-01-21 14:14:41 -0500347 if (dbFile.exists()) {
348 SQLiteDatabase.deleteDatabase(dbFile);
349 }
Dan Sandlerd5024042014-01-09 15:01:33 -0500350 mOpenHelper = new DatabaseHelper(getContext());
351 }
352
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 private static class DatabaseHelper extends SQLiteOpenHelper {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 private static final String TAG_FAVORITES = "favorites";
355 private static final String TAG_FAVORITE = "favorite";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700356 private static final String TAG_CLOCK = "clock";
357 private static final String TAG_SEARCH = "search";
Mike Cleronb87bd162009-10-30 16:36:56 -0700358 private static final String TAG_APPWIDGET = "appwidget";
359 private static final String TAG_SHORTCUT = "shortcut";
Adam Cohen228da5a2011-07-27 22:23:47 -0700360 private static final String TAG_FOLDER = "folder";
Winson Chungb3302ae2012-05-01 10:19:14 -0700361 private static final String TAG_EXTRA = "extra";
Daniel Sandler57dac262013-10-03 13:28:36 -0400362 private static final String TAG_INCLUDE = "include";
Winson Chung3d503fb2011-07-13 17:25:49 -0700363
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364 private final Context mContext;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700365 private final AppWidgetHost mAppWidgetHost;
Adam Cohendcd297f2013-06-18 13:13:40 -0700366 private long mMaxItemId = -1;
367 private long mMaxScreenId = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368
Winson Chung0b560dd2014-01-21 13:00:26 -0800369 private boolean mNewDbCreated = false;
370
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800371 DatabaseHelper(Context context) {
372 super(context, DATABASE_NAME, null, DATABASE_VERSION);
373 mContext = context;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700374 mAppWidgetHost = new AppWidgetHost(context, Launcher.APPWIDGET_HOST_ID);
Winson Chung3d503fb2011-07-13 17:25:49 -0700375
376 // In the case where neither onCreate nor onUpgrade gets called, we read the maxId from
377 // the DB here
Adam Cohendcd297f2013-06-18 13:13:40 -0700378 if (mMaxItemId == -1) {
379 mMaxItemId = initializeMaxItemId(getWritableDatabase());
380 }
381 if (mMaxScreenId == -1) {
382 mMaxScreenId = initializeMaxScreenId(getWritableDatabase());
Winson Chung3d503fb2011-07-13 17:25:49 -0700383 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 }
385
Winson Chung0b560dd2014-01-21 13:00:26 -0800386 public boolean wasNewDbCreated() {
387 return mNewDbCreated;
388 }
389
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700390 /**
391 * Send notification that we've deleted the {@link AppWidgetHost},
392 * probably as part of the initial database creation. The receiver may
393 * want to re-call {@link AppWidgetHost#startListening()} to ensure
394 * callbacks are correctly set.
395 */
396 private void sendAppWidgetResetNotify() {
397 final ContentResolver resolver = mContext.getContentResolver();
398 resolver.notifyChange(CONTENT_APPWIDGET_RESET_URI, null);
399 }
400
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 @Override
402 public void onCreate(SQLiteDatabase db) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800403 if (LOGD) Log.d(TAG, "creating new launcher database");
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700404
Adam Cohendcd297f2013-06-18 13:13:40 -0700405 mMaxItemId = 1;
406 mMaxScreenId = 0;
Winson Chung0b560dd2014-01-21 13:00:26 -0800407 mNewDbCreated = true;
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700408
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 db.execSQL("CREATE TABLE favorites (" +
410 "_id INTEGER PRIMARY KEY," +
411 "title TEXT," +
412 "intent TEXT," +
413 "container INTEGER," +
414 "screen INTEGER," +
415 "cellX INTEGER," +
416 "cellY INTEGER," +
417 "spanX INTEGER," +
418 "spanY INTEGER," +
419 "itemType INTEGER," +
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700420 "appWidgetId INTEGER NOT NULL DEFAULT -1," +
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800421 "isShortcut INTEGER," +
422 "iconType INTEGER," +
423 "iconPackage TEXT," +
424 "iconResource TEXT," +
425 "icon BLOB," +
426 "uri TEXT," +
Chris Wrend5e66bf2013-09-16 14:02:29 -0400427 "displayMode INTEGER," +
Chris Wren1ada10d2013-09-13 18:01:38 -0400428 "appWidgetProvider TEXT," +
Chris Wrenf4d08112014-01-16 18:13:56 -0500429 "modified INTEGER NOT NULL DEFAULT 0," +
430 "restored INTEGER NOT NULL DEFAULT 0" +
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 ");");
Adam Cohendcd297f2013-06-18 13:13:40 -0700432 addWorkspacesTable(db);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800433
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700434 // Database was just created, so wipe any previous widgets
435 if (mAppWidgetHost != null) {
436 mAppWidgetHost.deleteHost();
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700437 sendAppWidgetResetNotify();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700439
Adam Cohen6dbe0492013-12-02 17:00:14 -0800440 if (shouldImportLauncher2Database(mContext)) {
Dan Sandlerf0b8dac2013-11-19 12:21:25 -0500441 // Try converting the old database
442 ContentValuesCallback permuteScreensCb = new ContentValuesCallback() {
443 public void onRow(ContentValues values) {
444 int container = values.getAsInteger(LauncherSettings.Favorites.CONTAINER);
445 if (container == Favorites.CONTAINER_DESKTOP) {
446 int screen = values.getAsInteger(LauncherSettings.Favorites.SCREEN);
447 screen = (int) upgradeLauncherDb_permuteScreens(screen);
448 values.put(LauncherSettings.Favorites.SCREEN, screen);
449 }
450 }
451 };
452 Uri uri = Uri.parse("content://" + Settings.AUTHORITY +
453 "/old_favorites?notify=true");
454 if (!convertDatabase(db, uri, permuteScreensCb, true)) {
455 // Try and upgrade from the Launcher2 db
456 uri = LauncherSettings.Favorites.OLD_CONTENT_URI;
457 if (!convertDatabase(db, uri, permuteScreensCb, false)) {
458 // If we fail, then set a flag to load the default workspace
459 setFlagEmptyDbCreated();
460 return;
Winson Chungc763c4e2013-07-19 13:49:06 -0700461 }
462 }
Dan Sandlerf0b8dac2013-11-19 12:21:25 -0500463 // Right now, in non-default workspace cases, we want to run the final
464 // upgrade code (ie. to fix workspace screen indices -> ids, etc.), so
465 // set that flag too.
466 setFlagJustLoadedOldDb();
467 } else {
468 // Fresh and clean launcher DB.
469 mMaxItemId = initializeMaxItemId(db);
470 setFlagEmptyDbCreated();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800471 }
472 }
473
Adam Cohendcd297f2013-06-18 13:13:40 -0700474 private void addWorkspacesTable(SQLiteDatabase db) {
475 db.execSQL("CREATE TABLE " + TABLE_WORKSPACE_SCREENS + " (" +
476 LauncherSettings.WorkspaceScreens._ID + " INTEGER," +
Chris Wren1ada10d2013-09-13 18:01:38 -0400477 LauncherSettings.WorkspaceScreens.SCREEN_RANK + " INTEGER," +
478 LauncherSettings.ChangeLogColumns.MODIFIED + " INTEGER NOT NULL DEFAULT 0" +
Adam Cohendcd297f2013-06-18 13:13:40 -0700479 ");");
480 }
481
Winson Chungc763c4e2013-07-19 13:49:06 -0700482 private void setFlagJustLoadedOldDb() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400483 String spKey = LauncherAppState.getSharedPreferencesKey();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700484 SharedPreferences sp = mContext.getSharedPreferences(spKey, Context.MODE_PRIVATE);
485 SharedPreferences.Editor editor = sp.edit();
Winson Chungc763c4e2013-07-19 13:49:06 -0700486 editor.putBoolean(UPGRADED_FROM_OLD_DATABASE, true);
487 editor.putBoolean(EMPTY_DATABASE_CREATED, false);
Michael Jurkab85f8a42012-04-25 15:48:32 -0700488 editor.commit();
489 }
490
Winson Chungc763c4e2013-07-19 13:49:06 -0700491 private void setFlagEmptyDbCreated() {
492 String spKey = LauncherAppState.getSharedPreferencesKey();
493 SharedPreferences sp = mContext.getSharedPreferences(spKey, Context.MODE_PRIVATE);
494 SharedPreferences.Editor editor = sp.edit();
495 editor.putBoolean(EMPTY_DATABASE_CREATED, true);
496 editor.putBoolean(UPGRADED_FROM_OLD_DATABASE, false);
497 editor.commit();
498 }
499
500 // We rearrange the screens from the old launcher
501 // 12345 -> 34512
502 private long upgradeLauncherDb_permuteScreens(long screen) {
503 if (screen >= 2) {
504 return screen - 2;
505 } else {
506 return screen + 3;
507 }
508 }
509
510 private boolean convertDatabase(SQLiteDatabase db, Uri uri,
511 ContentValuesCallback cb, boolean deleteRows) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800512 if (LOGD) Log.d(TAG, "converting database from an older format, but not onUpgrade");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800513 boolean converted = false;
514
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800515 final ContentResolver resolver = mContext.getContentResolver();
516 Cursor cursor = null;
517
518 try {
519 cursor = resolver.query(uri, null, null, null, null);
520 } catch (Exception e) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700521 // Ignore
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800522 }
523
524 // We already have a favorites database in the old provider
Winson Chungc763c4e2013-07-19 13:49:06 -0700525 if (cursor != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800526 try {
Winson Chungc763c4e2013-07-19 13:49:06 -0700527 if (cursor.getCount() > 0) {
528 converted = copyFromCursor(db, cursor, cb) > 0;
529 if (converted && deleteRows) {
530 resolver.delete(uri, null, null);
531 }
532 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800533 } finally {
534 cursor.close();
535 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800536 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700537
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800538 if (converted) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700539 // Convert widgets from this import into widgets
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800540 if (LOGD) Log.d(TAG, "converted and now triggering widget upgrade");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800541 convertWidgets(db);
Winson Chungc763c4e2013-07-19 13:49:06 -0700542
543 // Update max item id
544 mMaxItemId = initializeMaxItemId(db);
545 if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800546 }
547
548 return converted;
549 }
550
Winson Chungc763c4e2013-07-19 13:49:06 -0700551 private int copyFromCursor(SQLiteDatabase db, Cursor c, ContentValuesCallback cb) {
Romain Guy73b979d2009-06-09 12:57:21 -0700552 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800553 final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
554 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
555 final int iconTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
556 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
557 final int iconPackageIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
558 final int iconResourceIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
559 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
560 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
561 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
562 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
563 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
564 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
565 final int displayModeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
566
567 ContentValues[] rows = new ContentValues[c.getCount()];
568 int i = 0;
569 while (c.moveToNext()) {
570 ContentValues values = new ContentValues(c.getColumnCount());
Romain Guy73b979d2009-06-09 12:57:21 -0700571 values.put(LauncherSettings.Favorites._ID, c.getLong(idIndex));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800572 values.put(LauncherSettings.Favorites.INTENT, c.getString(intentIndex));
573 values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex));
574 values.put(LauncherSettings.Favorites.ICON_TYPE, c.getInt(iconTypeIndex));
575 values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex));
576 values.put(LauncherSettings.Favorites.ICON_PACKAGE, c.getString(iconPackageIndex));
577 values.put(LauncherSettings.Favorites.ICON_RESOURCE, c.getString(iconResourceIndex));
578 values.put(LauncherSettings.Favorites.CONTAINER, c.getInt(containerIndex));
579 values.put(LauncherSettings.Favorites.ITEM_TYPE, c.getInt(itemTypeIndex));
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700580 values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800581 values.put(LauncherSettings.Favorites.SCREEN, c.getInt(screenIndex));
582 values.put(LauncherSettings.Favorites.CELLX, c.getInt(cellXIndex));
583 values.put(LauncherSettings.Favorites.CELLY, c.getInt(cellYIndex));
584 values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex));
585 values.put(LauncherSettings.Favorites.DISPLAY_MODE, c.getInt(displayModeIndex));
Winson Chungc763c4e2013-07-19 13:49:06 -0700586 if (cb != null) {
587 cb.onRow(values);
588 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800589 rows[i++] = values;
590 }
591
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800592 int total = 0;
Winson Chungc763c4e2013-07-19 13:49:06 -0700593 if (i > 0) {
594 db.beginTransaction();
595 try {
596 int numValues = rows.length;
597 for (i = 0; i < numValues; i++) {
598 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, rows[i]) < 0) {
599 return 0;
600 } else {
601 total++;
602 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800603 }
Winson Chungc763c4e2013-07-19 13:49:06 -0700604 db.setTransactionSuccessful();
605 } finally {
606 db.endTransaction();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800607 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800608 }
609
610 return total;
611 }
612
613 @Override
614 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Winson Chungc763c4e2013-07-19 13:49:06 -0700615 if (LOGD) Log.d(TAG, "onUpgrade triggered: " + oldVersion);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700616
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800617 int version = oldVersion;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700618 if (version < 3) {
619 // upgrade 1,2 -> 3 added appWidgetId column
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800620 db.beginTransaction();
621 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700622 // Insert new column for holding appWidgetIds
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800623 db.execSQL("ALTER TABLE favorites " +
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700624 "ADD COLUMN appWidgetId INTEGER NOT NULL DEFAULT -1;");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800625 db.setTransactionSuccessful();
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700626 version = 3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -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);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800630 } finally {
631 db.endTransaction();
632 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700633
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634 // Convert existing widgets only if table upgrade was successful
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700635 if (version == 3) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636 convertWidgets(db);
637 }
638 }
Romain Guy73b979d2009-06-09 12:57:21 -0700639
640 if (version < 4) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800641 version = 4;
Romain Guy73b979d2009-06-09 12:57:21 -0700642 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700643
Romain Guy509cd6a2010-03-23 15:10:56 -0700644 // Where's version 5?
645 // - Donut and sholes on 2.0 shipped with version 4 of launcher1.
Daniel Sandler325dc232013-06-05 22:57:57 -0400646 // - Passion shipped on 2.1 with version 6 of launcher3
Romain Guy509cd6a2010-03-23 15:10:56 -0700647 // - Sholes shipped on 2.1r1 (aka Mr. 3) with version 5 of launcher 1
648 // but version 5 on there was the updateContactsShortcuts change
649 // which was version 6 in launcher 2 (first shipped on passion 2.1r1).
650 // The updateContactsShortcuts change is idempotent, so running it twice
651 // is okay so we'll do that when upgrading the devices that shipped with it.
652 if (version < 6) {
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800653 // We went from 3 to 5 screens. Move everything 1 to the right
654 db.beginTransaction();
655 try {
656 db.execSQL("UPDATE favorites SET screen=(screen + 1);");
657 db.setTransactionSuccessful();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800658 } catch (SQLException ex) {
659 // Old version remains, which means we wipe old data
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800660 Log.e(TAG, ex.getMessage(), ex);
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800661 } finally {
662 db.endTransaction();
663 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700664
Romain Guy509cd6a2010-03-23 15:10:56 -0700665 // We added the fast track.
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800666 if (updateContactsShortcuts(db)) {
667 version = 6;
668 }
669 }
Bjorn Bringert7984c942009-12-09 15:38:25 +0000670
671 if (version < 7) {
672 // Version 7 gets rid of the special search widget.
673 convertWidgets(db);
674 version = 7;
675 }
676
Joe Onorato0589f0f2010-02-08 13:44:00 -0800677 if (version < 8) {
678 // Version 8 (froyo) has the icons all normalized. This should
679 // already be the case in practice, but we now rely on it and don't
680 // resample the images each time.
681 normalizeIcons(db);
682 version = 8;
683 }
684
Winson Chung3d503fb2011-07-13 17:25:49 -0700685 if (version < 9) {
686 // The max id is not yet set at this point (onUpgrade is triggered in the ctor
687 // 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 -0700688 if (mMaxItemId == -1) {
689 mMaxItemId = initializeMaxItemId(db);
Winson Chung3d503fb2011-07-13 17:25:49 -0700690 }
691
692 // Add default hotseat icons
Winson Chung6d092682011-11-16 18:43:26 -0800693 loadFavorites(db, R.xml.update_workspace);
Winson Chung3d503fb2011-07-13 17:25:49 -0700694 version = 9;
695 }
696
Daniel Lehmannd02402c2012-05-14 18:30:53 -0700697 // We bumped the version three time during JB, once to update the launch flags, once to
698 // update the override for the default launch animation and once to set the mimetype
699 // to improve startup performance
700 if (version < 12) {
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700701 // Contact shortcuts need a different set of flags to be launched now
702 // The updateContactsShortcuts change is idempotent, so we can keep using it like
703 // back in the Donut days
704 updateContactsShortcuts(db);
Daniel Lehmannd02402c2012-05-14 18:30:53 -0700705 version = 12;
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700706 }
707
Adam Cohendcd297f2013-06-18 13:13:40 -0700708 if (version < 13) {
709 // With the new shrink-wrapped and re-orderable workspaces, it makes sense
710 // to persist workspace screens and their relative order.
711 mMaxScreenId = 0;
712
713 // This will never happen in the wild, but when we switch to using workspace
714 // screen ids, redo the import from old launcher.
Winson Chungc763c4e2013-07-19 13:49:06 -0700715 sJustLoadedFromOldDb = true;
Adam Cohendcd297f2013-06-18 13:13:40 -0700716
717 addWorkspacesTable(db);
718 version = 13;
719 }
720
Chris Wrend5e66bf2013-09-16 14:02:29 -0400721 if (version < 14) {
722 db.beginTransaction();
723 try {
724 // Insert new column for holding widget provider name
725 db.execSQL("ALTER TABLE favorites " +
726 "ADD COLUMN appWidgetProvider TEXT;");
727 db.setTransactionSuccessful();
728 version = 14;
729 } catch (SQLException ex) {
730 // Old version remains, which means we wipe old data
731 Log.e(TAG, ex.getMessage(), ex);
732 } finally {
733 db.endTransaction();
734 }
735 }
736
Chris Wren1ada10d2013-09-13 18:01:38 -0400737 if (version < 15) {
738 db.beginTransaction();
739 try {
740 // Insert new column for holding update timestamp
741 db.execSQL("ALTER TABLE favorites " +
742 "ADD COLUMN modified INTEGER NOT NULL DEFAULT 0;");
743 db.execSQL("ALTER TABLE workspaceScreens " +
744 "ADD COLUMN modified INTEGER NOT NULL DEFAULT 0;");
745 db.setTransactionSuccessful();
746 version = 15;
747 } catch (SQLException ex) {
748 // Old version remains, which means we wipe old data
749 Log.e(TAG, ex.getMessage(), ex);
750 } finally {
751 db.endTransaction();
752 }
753 }
754
Chris Wrenf4d08112014-01-16 18:13:56 -0500755
756 if (version < 16) {
757 db.beginTransaction();
758 try {
759 // Insert new column for holding restore status
760 db.execSQL("ALTER TABLE favorites " +
761 "ADD COLUMN restored INTEGER NOT NULL DEFAULT 0;");
762 db.setTransactionSuccessful();
763 version = 16;
764 } catch (SQLException ex) {
765 // Old version remains, which means we wipe old data
766 Log.e(TAG, ex.getMessage(), ex);
767 } finally {
768 db.endTransaction();
769 }
770 }
771
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800772 if (version != DATABASE_VERSION) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800773 Log.w(TAG, "Destroying all old data.");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800774 db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES);
Adam Cohendcd297f2013-06-18 13:13:40 -0700775 db.execSQL("DROP TABLE IF EXISTS " + TABLE_WORKSPACE_SCREENS);
776
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800777 onCreate(db);
778 }
779 }
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800780
781 private boolean updateContactsShortcuts(SQLiteDatabase db) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800782 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE,
783 new int[] { Favorites.ITEM_TYPE_SHORTCUT });
784
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700785 Cursor c = null;
786 final String actionQuickContact = "com.android.contacts.action.QUICK_CONTACT";
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800787 db.beginTransaction();
788 try {
789 // Select and iterate through each matching widget
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700790 c = db.query(TABLE_FAVORITES,
791 new String[] { Favorites._ID, Favorites.INTENT },
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800792 selectWhere, null, null, null, null);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700793 if (c == null) return false;
794
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800795 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700796
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800797 final int idIndex = c.getColumnIndex(Favorites._ID);
798 final int intentIndex = c.getColumnIndex(Favorites.INTENT);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700799
800 while (c.moveToNext()) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800801 long favoriteId = c.getLong(idIndex);
802 final String intentUri = c.getString(intentIndex);
803 if (intentUri != null) {
804 try {
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700805 final Intent intent = Intent.parseUri(intentUri, 0);
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800806 android.util.Log.d("Home", intent.toString());
807 final Uri uri = intent.getData();
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700808 if (uri != null) {
809 final String data = uri.toString();
810 if ((Intent.ACTION_VIEW.equals(intent.getAction()) ||
811 actionQuickContact.equals(intent.getAction())) &&
812 (data.startsWith("content://contacts/people/") ||
813 data.startsWith("content://com.android.contacts/" +
814 "contacts/lookup/"))) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800815
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700816 final Intent newIntent = new Intent(actionQuickContact);
817 // When starting from the launcher, start in a new, cleared task
818 // CLEAR_WHEN_TASK_RESET cannot reset the root of a task, so we
819 // clear the whole thing preemptively here since
820 // QuickContactActivity will finish itself when launching other
821 // detail activities.
822 newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
823 Intent.FLAG_ACTIVITY_CLEAR_TASK);
Winson Chung2672ff92012-05-04 16:22:30 -0700824 newIntent.putExtra(
825 Launcher.INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION, true);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700826 newIntent.setData(uri);
Daniel Lehmannd02402c2012-05-14 18:30:53 -0700827 // Determine the type and also put that in the shortcut
828 // (that can speed up launch a bit)
829 newIntent.setDataAndType(uri, newIntent.resolveType(mContext));
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800830
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700831 final ContentValues values = new ContentValues();
832 values.put(LauncherSettings.Favorites.INTENT,
833 newIntent.toUri(0));
834
835 String updateWhere = Favorites._ID + "=" + favoriteId;
836 db.update(TABLE_FAVORITES, values, updateWhere, null);
837 }
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800838 }
839 } catch (RuntimeException ex) {
840 Log.e(TAG, "Problem upgrading shortcut", ex);
841 } catch (URISyntaxException e) {
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700842 Log.e(TAG, "Problem upgrading shortcut", e);
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800843 }
844 }
845 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700846
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800847 db.setTransactionSuccessful();
848 } catch (SQLException ex) {
849 Log.w(TAG, "Problem while upgrading contacts", ex);
850 return false;
851 } finally {
852 db.endTransaction();
853 if (c != null) {
854 c.close();
855 }
856 }
857
858 return true;
859 }
860
Joe Onorato0589f0f2010-02-08 13:44:00 -0800861 private void normalizeIcons(SQLiteDatabase db) {
862 Log.d(TAG, "normalizing icons");
863
Joe Onorato346e1292010-02-18 10:34:24 -0500864 db.beginTransaction();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800865 Cursor c = null;
Joe Onorato9690b392010-03-23 17:34:37 -0400866 SQLiteStatement update = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800867 try {
868 boolean logged = false;
Joe Onorato9690b392010-03-23 17:34:37 -0400869 update = db.compileStatement("UPDATE favorites "
Jeff Hamiltoneaf77d62010-02-13 00:08:17 -0600870 + "SET icon=? WHERE _id=?");
Joe Onorato0589f0f2010-02-08 13:44:00 -0800871
872 c = db.rawQuery("SELECT _id, icon FROM favorites WHERE iconType=" +
873 Favorites.ICON_TYPE_BITMAP, null);
874
875 final int idIndex = c.getColumnIndexOrThrow(Favorites._ID);
876 final int iconIndex = c.getColumnIndexOrThrow(Favorites.ICON);
877
878 while (c.moveToNext()) {
879 long id = c.getLong(idIndex);
880 byte[] data = c.getBlob(iconIndex);
881 try {
882 Bitmap bitmap = Utilities.resampleIconBitmap(
883 BitmapFactory.decodeByteArray(data, 0, data.length),
884 mContext);
885 if (bitmap != null) {
886 update.bindLong(1, id);
887 data = ItemInfo.flattenBitmap(bitmap);
888 if (data != null) {
889 update.bindBlob(2, data);
890 update.execute();
891 }
892 bitmap.recycle();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800893 }
894 } catch (Exception e) {
895 if (!logged) {
896 Log.e(TAG, "Failed normalizing icon " + id, e);
897 } else {
898 Log.e(TAG, "Also failed normalizing icon " + id);
899 }
900 logged = true;
901 }
902 }
Bjorn Bringert3a928e42010-02-19 11:15:40 +0000903 db.setTransactionSuccessful();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800904 } catch (SQLException ex) {
905 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
906 } finally {
907 db.endTransaction();
Joe Onorato9690b392010-03-23 17:34:37 -0400908 if (update != null) {
909 update.close();
910 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800911 if (c != null) {
912 c.close();
913 }
914 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700915 }
916
917 // Generates a new ID to use for an object in your database. This method should be only
918 // called from the main UI thread. As an exception, we do call it when we call the
919 // constructor from the worker thread; however, this doesn't extend until after the
920 // constructor is called, and we only pass a reference to LauncherProvider to LauncherApp
921 // after that point
Adam Cohendcd297f2013-06-18 13:13:40 -0700922 public long generateNewItemId() {
923 if (mMaxItemId < 0) {
924 throw new RuntimeException("Error: max item id was not initialized");
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700925 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700926 mMaxItemId += 1;
927 return mMaxItemId;
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700928 }
929
Winson Chungc763c4e2013-07-19 13:49:06 -0700930 public void updateMaxItemId(long id) {
931 mMaxItemId = id + 1;
932 }
933
Chris Wren5dee7af2013-12-20 17:22:11 -0500934 public void checkId(String table, ContentValues values) {
935 long id = values.getAsLong(LauncherSettings.BaseLauncherColumns._ID);
936 if (table == LauncherProvider.TABLE_WORKSPACE_SCREENS) {
937 mMaxScreenId = Math.max(id, mMaxScreenId);
938 } else {
939 mMaxItemId = Math.max(id, mMaxItemId);
940 }
941 }
942
Adam Cohendcd297f2013-06-18 13:13:40 -0700943 private long initializeMaxItemId(SQLiteDatabase db) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700944 Cursor c = db.rawQuery("SELECT MAX(_id) FROM favorites", null);
945
946 // get the result
947 final int maxIdIndex = 0;
948 long id = -1;
949 if (c != null && c.moveToNext()) {
950 id = c.getLong(maxIdIndex);
951 }
Michael Jurka5130e402011-10-13 04:55:35 -0700952 if (c != null) {
953 c.close();
954 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700955
956 if (id == -1) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700957 throw new RuntimeException("Error: could not query max item id");
958 }
959
960 return id;
961 }
962
963 // Generates a new ID to use for an workspace screen in your database. This method
964 // should be only called from the main UI thread. As an exception, we do call it when we
965 // call the constructor from the worker thread; however, this doesn't extend until after the
966 // constructor is called, and we only pass a reference to LauncherProvider to LauncherApp
967 // after that point
968 public long generateNewScreenId() {
969 if (mMaxScreenId < 0) {
970 throw new RuntimeException("Error: max screen id was not initialized");
971 }
972 mMaxScreenId += 1;
Winson Chunga90303b2013-11-15 13:05:06 -0800973 // Log to disk
974 Launcher.addDumpLog(TAG, "11683562 - generateNewScreenId(): " + mMaxScreenId, true);
Adam Cohendcd297f2013-06-18 13:13:40 -0700975 return mMaxScreenId;
976 }
977
978 public void updateMaxScreenId(long maxScreenId) {
Winson Chunga90303b2013-11-15 13:05:06 -0800979 // Log to disk
980 Launcher.addDumpLog(TAG, "11683562 - updateMaxScreenId(): " + maxScreenId, true);
Adam Cohendcd297f2013-06-18 13:13:40 -0700981 mMaxScreenId = maxScreenId;
982 }
983
984 private long initializeMaxScreenId(SQLiteDatabase db) {
985 Cursor c = db.rawQuery("SELECT MAX(" + LauncherSettings.WorkspaceScreens._ID + ") FROM " + TABLE_WORKSPACE_SCREENS, null);
986
987 // get the result
988 final int maxIdIndex = 0;
989 long id = -1;
990 if (c != null && c.moveToNext()) {
991 id = c.getLong(maxIdIndex);
992 }
993 if (c != null) {
994 c.close();
995 }
996
997 if (id == -1) {
998 throw new RuntimeException("Error: could not query max screen id");
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700999 }
1000
Winson Chunga90303b2013-11-15 13:05:06 -08001001 // Log to disk
1002 Launcher.addDumpLog(TAG, "11683562 - initializeMaxScreenId(): " + id, true);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001003 return id;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001004 }
1005
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001006 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001007 * Upgrade existing clock and photo frame widgets into their new widget
Bjorn Bringert93c45762009-12-16 13:19:47 +00001008 * equivalents.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001009 */
1010 private void convertWidgets(SQLiteDatabase db) {
Bjorn Bringert34251342009-12-15 13:33:11 +00001011 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001012 final int[] bindSources = new int[] {
1013 Favorites.ITEM_TYPE_WIDGET_CLOCK,
1014 Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME,
Bjorn Bringert7984c942009-12-09 15:38:25 +00001015 Favorites.ITEM_TYPE_WIDGET_SEARCH,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001016 };
Bjorn Bringert7984c942009-12-09 15:38:25 +00001017
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001018 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE, bindSources);
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001019
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001020 Cursor c = null;
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001021
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 db.beginTransaction();
1023 try {
1024 // Select and iterate through each matching widget
Bjorn Bringert7984c942009-12-09 15:38:25 +00001025 c = db.query(TABLE_FAVORITES, new String[] { Favorites._ID, Favorites.ITEM_TYPE },
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 selectWhere, null, null, null, null);
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001027
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001028 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001029
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001030 final ContentValues values = new ContentValues();
1031 while (c != null && c.moveToNext()) {
1032 long favoriteId = c.getLong(0);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001033 int favoriteType = c.getInt(1);
1034
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001035 // Allocate and update database with new appWidgetId
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001037 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001038
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001039 if (LOGD) {
1040 Log.d(TAG, "allocated appWidgetId=" + appWidgetId
1041 + " for favoriteId=" + favoriteId);
1042 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001043 values.clear();
Bjorn Bringert7984c942009-12-09 15:38:25 +00001044 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
1045 values.put(Favorites.APPWIDGET_ID, appWidgetId);
1046
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047 // Original widgets might not have valid spans when upgrading
Bjorn Bringert7984c942009-12-09 15:38:25 +00001048 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
1049 values.put(LauncherSettings.Favorites.SPANX, 4);
1050 values.put(LauncherSettings.Favorites.SPANY, 1);
1051 } else {
1052 values.put(LauncherSettings.Favorites.SPANX, 2);
1053 values.put(LauncherSettings.Favorites.SPANY, 2);
1054 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001055
1056 String updateWhere = Favorites._ID + "=" + favoriteId;
1057 db.update(TABLE_FAVORITES, values, updateWhere, null);
Bjorn Bringert34251342009-12-15 13:33:11 +00001058
Bjorn Bringert34251342009-12-15 13:33:11 +00001059 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_CLOCK) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001060 // TODO: check return value
1061 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
Bjorn Bringert34251342009-12-15 13:33:11 +00001062 new ComponentName("com.android.alarmclock",
1063 "com.android.alarmclock.AnalogAppWidgetProvider"));
1064 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001065 // TODO: check return value
1066 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
Bjorn Bringert34251342009-12-15 13:33:11 +00001067 new ComponentName("com.android.camera",
1068 "com.android.camera.PhotoAppWidgetProvider"));
1069 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001070 // TODO: check return value
1071 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
Bjorn Bringertcd8fec02010-01-14 13:26:43 +00001072 getSearchWidgetProvider());
Bjorn Bringert34251342009-12-15 13:33:11 +00001073 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001075 Log.e(TAG, "Problem allocating appWidgetId", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001076 }
1077 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001078
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079 db.setTransactionSuccessful();
1080 } catch (SQLException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001081 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082 } finally {
1083 db.endTransaction();
1084 if (c != null) {
1085 c.close();
1086 }
1087 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001088
1089 // Update max item id
1090 mMaxItemId = initializeMaxItemId(db);
1091 if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 }
1093
Michael Jurka8b805b12012-04-18 14:23:14 -07001094 private static final void beginDocument(XmlPullParser parser, String firstElementName)
1095 throws XmlPullParserException, IOException {
1096 int type;
Michael Jurka9bc8eba2012-05-21 20:36:44 -07001097 while ((type = parser.next()) != XmlPullParser.START_TAG
1098 && type != XmlPullParser.END_DOCUMENT) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001099 ;
1100 }
1101
Michael Jurka9bc8eba2012-05-21 20:36:44 -07001102 if (type != XmlPullParser.START_TAG) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001103 throw new XmlPullParserException("No start tag found");
1104 }
1105
1106 if (!parser.getName().equals(firstElementName)) {
1107 throw new XmlPullParserException("Unexpected start tag: found " + parser.getName() +
1108 ", expected " + firstElementName);
1109 }
1110 }
1111
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001112 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113 * Loads the default set of favorite packages from an xml file.
1114 *
1115 * @param db The database to write the values into
Winson Chung3d503fb2011-07-13 17:25:49 -07001116 * @param filterContainerId The specific container id of items to load
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 */
Winson Chung6d092682011-11-16 18:43:26 -08001118 private int loadFavorites(SQLiteDatabase db, int workspaceResourceId) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001119 Intent intent = new Intent(Intent.ACTION_MAIN, null);
1120 intent.addCategory(Intent.CATEGORY_LAUNCHER);
1121 ContentValues values = new ContentValues();
1122
Daniel Sandler57dac262013-10-03 13:28:36 -04001123 if (LOGD) Log.v(TAG, String.format("Loading favorites from resid=0x%08x", workspaceResourceId));
1124
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 PackageManager packageManager = mContext.getPackageManager();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001126 int i = 0;
1127 try {
Winson Chung6d092682011-11-16 18:43:26 -08001128 XmlResourceParser parser = mContext.getResources().getXml(workspaceResourceId);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001129 AttributeSet attrs = Xml.asAttributeSet(parser);
Michael Jurka8b805b12012-04-18 14:23:14 -07001130 beginDocument(parser, TAG_FAVORITES);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001131
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001132 final int depth = parser.getDepth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001134 int type;
1135 while (((type = parser.next()) != XmlPullParser.END_TAG ||
1136 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
1137
1138 if (type != XmlPullParser.START_TAG) {
1139 continue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 }
1141
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001142 boolean added = false;
1143 final String name = parser.getName();
1144
Daniel Sandler57dac262013-10-03 13:28:36 -04001145 if (TAG_INCLUDE.equals(name)) {
1146 final TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.Include);
1147
1148 final int resId = a.getResourceId(R.styleable.Include_workspace, 0);
1149
1150 if (LOGD) Log.v(TAG, String.format(("%" + (2*(depth+1)) + "s<include workspace=%08x>"),
1151 "", resId));
1152
1153 if (resId != 0 && resId != workspaceResourceId) {
1154 // recursively load some more favorites, why not?
1155 i += loadFavorites(db, resId);
1156 added = false;
Daniel Sandler57dac262013-10-03 13:28:36 -04001157 } else {
1158 Log.w(TAG, String.format("Skipping <include workspace=0x%08x>", resId));
1159 }
1160
1161 a.recycle();
1162
1163 if (LOGD) Log.v(TAG, String.format(("%" + (2*(depth+1)) + "s</include>"), ""));
1164 continue;
1165 }
1166
1167 // Assuming it's a <favorite> at this point
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001168 TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.Favorite);
1169
Winson Chung3d503fb2011-07-13 17:25:49 -07001170 long container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
1171 if (a.hasValue(R.styleable.Favorite_container)) {
1172 container = Long.valueOf(a.getString(R.styleable.Favorite_container));
1173 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001174
Winson Chung6d092682011-11-16 18:43:26 -08001175 String screen = a.getString(R.styleable.Favorite_screen);
1176 String x = a.getString(R.styleable.Favorite_x);
1177 String y = a.getString(R.styleable.Favorite_y);
1178
Winson Chung6d092682011-11-16 18:43:26 -08001179 values.clear();
1180 values.put(LauncherSettings.Favorites.CONTAINER, container);
1181 values.put(LauncherSettings.Favorites.SCREEN, screen);
1182 values.put(LauncherSettings.Favorites.CELLX, x);
1183 values.put(LauncherSettings.Favorites.CELLY, y);
1184
Daniel Sandler57dac262013-10-03 13:28:36 -04001185 if (LOGD) {
1186 final String title = a.getString(R.styleable.Favorite_title);
1187 final String pkg = a.getString(R.styleable.Favorite_packageName);
1188 final String something = title != null ? title : pkg;
1189 Log.v(TAG, String.format(
1190 ("%" + (2*(depth+1)) + "s<%s%s c=%d s=%s x=%s y=%s>"),
1191 "", name,
1192 (something == null ? "" : (" \"" + something + "\"")),
1193 container, screen, x, y));
1194 }
1195
Winson Chung6d092682011-11-16 18:43:26 -08001196 if (TAG_FAVORITE.equals(name)) {
1197 long id = addAppShortcut(db, values, a, packageManager, intent);
1198 added = id >= 0;
1199 } else if (TAG_SEARCH.equals(name)) {
1200 added = addSearchWidget(db, values);
1201 } else if (TAG_CLOCK.equals(name)) {
1202 added = addClockWidget(db, values);
1203 } else if (TAG_APPWIDGET.equals(name)) {
Winson Chungb3302ae2012-05-01 10:19:14 -07001204 added = addAppWidget(parser, attrs, type, db, values, a, packageManager);
Winson Chung6d092682011-11-16 18:43:26 -08001205 } else if (TAG_SHORTCUT.equals(name)) {
1206 long id = addUriShortcut(db, values, a);
1207 added = id >= 0;
1208 } else if (TAG_FOLDER.equals(name)) {
1209 String title;
1210 int titleResId = a.getResourceId(R.styleable.Favorite_title, -1);
1211 if (titleResId != -1) {
1212 title = mContext.getResources().getString(titleResId);
1213 } else {
1214 title = mContext.getResources().getString(R.string.folder_name);
Winson Chung3d503fb2011-07-13 17:25:49 -07001215 }
Winson Chung6d092682011-11-16 18:43:26 -08001216 values.put(LauncherSettings.Favorites.TITLE, title);
1217 long folderId = addFolder(db, values);
1218 added = folderId >= 0;
Winson Chung3d503fb2011-07-13 17:25:49 -07001219
Winson Chung6d092682011-11-16 18:43:26 -08001220 ArrayList<Long> folderItems = new ArrayList<Long>();
Winson Chung3d503fb2011-07-13 17:25:49 -07001221
Winson Chung6d092682011-11-16 18:43:26 -08001222 int folderDepth = parser.getDepth();
1223 while ((type = parser.next()) != XmlPullParser.END_TAG ||
1224 parser.getDepth() > folderDepth) {
1225 if (type != XmlPullParser.START_TAG) {
1226 continue;
1227 }
1228 final String folder_item_name = parser.getName();
1229
1230 TypedArray ar = mContext.obtainStyledAttributes(attrs,
1231 R.styleable.Favorite);
1232 values.clear();
1233 values.put(LauncherSettings.Favorites.CONTAINER, folderId);
1234
Daniel Sandler57dac262013-10-03 13:28:36 -04001235 if (LOGD) {
1236 final String pkg = ar.getString(R.styleable.Favorite_packageName);
1237 final String uri = ar.getString(R.styleable.Favorite_uri);
1238 Log.v(TAG, String.format(("%" + (2*(folderDepth+1)) + "s<%s \"%s\">"), "",
1239 folder_item_name, uri != null ? uri : pkg));
1240 }
1241
Winson Chung6d092682011-11-16 18:43:26 -08001242 if (TAG_FAVORITE.equals(folder_item_name) && folderId >= 0) {
1243 long id =
1244 addAppShortcut(db, values, ar, packageManager, intent);
1245 if (id >= 0) {
1246 folderItems.add(id);
1247 }
1248 } else if (TAG_SHORTCUT.equals(folder_item_name) && folderId >= 0) {
1249 long id = addUriShortcut(db, values, ar);
1250 if (id >= 0) {
1251 folderItems.add(id);
1252 }
Adam Cohen228da5a2011-07-27 22:23:47 -07001253 } else {
Winson Chung6d092682011-11-16 18:43:26 -08001254 throw new RuntimeException("Folders can " +
1255 "contain only shortcuts");
Adam Cohen228da5a2011-07-27 22:23:47 -07001256 }
Winson Chung6d092682011-11-16 18:43:26 -08001257 ar.recycle();
1258 }
1259 // We can only have folders with >= 2 items, so we need to remove the
1260 // folder and clean up if less than 2 items were included, or some
1261 // failed to add, and less than 2 were actually added
1262 if (folderItems.size() < 2 && folderId >= 0) {
1263 // We just delete the folder and any items that made it
1264 deleteId(db, folderId);
1265 if (folderItems.size() > 0) {
1266 deleteId(db, folderItems.get(0));
Adam Cohen228da5a2011-07-27 22:23:47 -07001267 }
Winson Chung6d092682011-11-16 18:43:26 -08001268 added = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001269 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001271 if (added) i++;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001272 a.recycle();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273 }
1274 } catch (XmlPullParserException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001275 Log.w(TAG, "Got exception parsing favorites.", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001276 } catch (IOException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001277 Log.w(TAG, "Got exception parsing favorites.", e);
Winson Chung3d503fb2011-07-13 17:25:49 -07001278 } catch (RuntimeException e) {
1279 Log.w(TAG, "Got exception parsing favorites.", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001281
Winson Chungc763c4e2013-07-19 13:49:06 -07001282 // Update the max item id after we have loaded the database
1283 if (mMaxItemId == -1) {
1284 mMaxItemId = initializeMaxItemId(db);
1285 }
1286
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001287 return i;
1288 }
1289
Adam Cohen228da5a2011-07-27 22:23:47 -07001290 private long addAppShortcut(SQLiteDatabase db, ContentValues values, TypedArray a,
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001291 PackageManager packageManager, Intent intent) {
Adam Cohen228da5a2011-07-27 22:23:47 -07001292 long id = -1;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001293 ActivityInfo info;
1294 String packageName = a.getString(R.styleable.Favorite_packageName);
1295 String className = a.getString(R.styleable.Favorite_className);
1296 try {
Romain Guy693599f2010-03-23 10:58:18 -07001297 ComponentName cn;
1298 try {
1299 cn = new ComponentName(packageName, className);
1300 info = packageManager.getActivityInfo(cn, 0);
1301 } catch (PackageManager.NameNotFoundException nnfe) {
1302 String[] packages = packageManager.currentToCanonicalPackageNames(
1303 new String[] { packageName });
1304 cn = new ComponentName(packages[0], className);
1305 info = packageManager.getActivityInfo(cn, 0);
1306 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001307 id = generateNewItemId();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001308 intent.setComponent(cn);
Romain Guy693599f2010-03-23 10:58:18 -07001309 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1310 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Romain Guy1ce1a242009-06-23 17:34:54 -07001311 values.put(Favorites.INTENT, intent.toUri(0));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001312 values.put(Favorites.TITLE, info.loadLabel(packageManager).toString());
1313 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPLICATION);
1314 values.put(Favorites.SPANX, 1);
1315 values.put(Favorites.SPANY, 1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001316 values.put(Favorites._ID, generateNewItemId());
Adam Cohen228da5a2011-07-27 22:23:47 -07001317 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values) < 0) {
1318 return -1;
1319 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001320 } catch (PackageManager.NameNotFoundException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001321 Log.w(TAG, "Unable to add favorite: " + packageName +
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001322 "/" + className, e);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001323 }
Adam Cohen228da5a2011-07-27 22:23:47 -07001324 return id;
1325 }
1326
1327 private long addFolder(SQLiteDatabase db, ContentValues values) {
1328 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_FOLDER);
1329 values.put(Favorites.SPANX, 1);
1330 values.put(Favorites.SPANY, 1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001331 long id = generateNewItemId();
Adam Cohen228da5a2011-07-27 22:23:47 -07001332 values.put(Favorites._ID, id);
1333 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values) <= 0) {
1334 return -1;
1335 } else {
1336 return id;
1337 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001338 }
1339
Bjorn Bringertcd8fec02010-01-14 13:26:43 +00001340 private ComponentName getSearchWidgetProvider() {
1341 SearchManager searchManager =
1342 (SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE);
1343 ComponentName searchComponent = searchManager.getGlobalSearchActivity();
1344 if (searchComponent == null) return null;
1345 return getProviderInPackage(searchComponent.getPackageName());
1346 }
1347
1348 /**
1349 * Gets an appwidget provider from the given package. If the package contains more than
1350 * one appwidget provider, an arbitrary one is returned.
1351 */
1352 private ComponentName getProviderInPackage(String packageName) {
1353 AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
1354 List<AppWidgetProviderInfo> providers = appWidgetManager.getInstalledProviders();
1355 if (providers == null) return null;
1356 final int providerCount = providers.size();
1357 for (int i = 0; i < providerCount; i++) {
1358 ComponentName provider = providers.get(i).provider;
1359 if (provider != null && provider.getPackageName().equals(packageName)) {
1360 return provider;
1361 }
1362 }
1363 return null;
1364 }
1365
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001366 private boolean addSearchWidget(SQLiteDatabase db, ContentValues values) {
Bjorn Bringertcd8fec02010-01-14 13:26:43 +00001367 ComponentName cn = getSearchWidgetProvider();
Winson Chungb3302ae2012-05-01 10:19:14 -07001368 return addAppWidget(db, values, cn, 4, 1, null);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001369 }
1370
1371 private boolean addClockWidget(SQLiteDatabase db, ContentValues values) {
Bjorn Bringert34251342009-12-15 13:33:11 +00001372 ComponentName cn = new ComponentName("com.android.alarmclock",
1373 "com.android.alarmclock.AnalogAppWidgetProvider");
Winson Chungb3302ae2012-05-01 10:19:14 -07001374 return addAppWidget(db, values, cn, 2, 2, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001375 }
Adam Cohen228da5a2011-07-27 22:23:47 -07001376
Winson Chungb3302ae2012-05-01 10:19:14 -07001377 private boolean addAppWidget(XmlResourceParser parser, AttributeSet attrs, int type,
1378 SQLiteDatabase db, ContentValues values, TypedArray a,
1379 PackageManager packageManager) throws XmlPullParserException, IOException {
Romain Guy693599f2010-03-23 10:58:18 -07001380
Mike Cleronb87bd162009-10-30 16:36:56 -07001381 String packageName = a.getString(R.styleable.Favorite_packageName);
1382 String className = a.getString(R.styleable.Favorite_className);
1383
1384 if (packageName == null || className == null) {
1385 return false;
1386 }
Romain Guy693599f2010-03-23 10:58:18 -07001387
1388 boolean hasPackage = true;
Mike Cleronb87bd162009-10-30 16:36:56 -07001389 ComponentName cn = new ComponentName(packageName, className);
Romain Guy693599f2010-03-23 10:58:18 -07001390 try {
1391 packageManager.getReceiverInfo(cn, 0);
1392 } catch (Exception e) {
1393 String[] packages = packageManager.currentToCanonicalPackageNames(
1394 new String[] { packageName });
1395 cn = new ComponentName(packages[0], className);
1396 try {
1397 packageManager.getReceiverInfo(cn, 0);
1398 } catch (Exception e1) {
1399 hasPackage = false;
1400 }
1401 }
1402
1403 if (hasPackage) {
1404 int spanX = a.getInt(R.styleable.Favorite_spanX, 0);
1405 int spanY = a.getInt(R.styleable.Favorite_spanY, 0);
Winson Chungb3302ae2012-05-01 10:19:14 -07001406
1407 // Read the extras
1408 Bundle extras = new Bundle();
1409 int widgetDepth = parser.getDepth();
1410 while ((type = parser.next()) != XmlPullParser.END_TAG ||
1411 parser.getDepth() > widgetDepth) {
1412 if (type != XmlPullParser.START_TAG) {
1413 continue;
1414 }
1415
1416 TypedArray ar = mContext.obtainStyledAttributes(attrs, R.styleable.Extra);
1417 if (TAG_EXTRA.equals(parser.getName())) {
1418 String key = ar.getString(R.styleable.Extra_key);
1419 String value = ar.getString(R.styleable.Extra_value);
1420 if (key != null && value != null) {
1421 extras.putString(key, value);
1422 } else {
1423 throw new RuntimeException("Widget extras must have a key and value");
1424 }
1425 } else {
1426 throw new RuntimeException("Widgets can contain only extras");
1427 }
1428 ar.recycle();
1429 }
1430
1431 return addAppWidget(db, values, cn, spanX, spanY, extras);
Romain Guy693599f2010-03-23 10:58:18 -07001432 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001433
Romain Guy693599f2010-03-23 10:58:18 -07001434 return false;
Bjorn Bringert7984c942009-12-09 15:38:25 +00001435 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001436 private boolean addAppWidget(SQLiteDatabase db, ContentValues values, ComponentName cn,
Winson Chungb3302ae2012-05-01 10:19:14 -07001437 int spanX, int spanY, Bundle extras) {
Mike Cleronb87bd162009-10-30 16:36:56 -07001438 boolean allocatedAppWidgets = false;
1439 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
1440
1441 try {
1442 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001443
Mike Cleronb87bd162009-10-30 16:36:56 -07001444 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001445 values.put(Favorites.SPANX, spanX);
1446 values.put(Favorites.SPANY, spanY);
Mike Cleronb87bd162009-10-30 16:36:56 -07001447 values.put(Favorites.APPWIDGET_ID, appWidgetId);
Chris Wrend5e66bf2013-09-16 14:02:29 -04001448 values.put(Favorites.APPWIDGET_PROVIDER, cn.flattenToString());
Adam Cohendcd297f2013-06-18 13:13:40 -07001449 values.put(Favorites._ID, generateNewItemId());
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001450 dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values);
Mike Cleronb87bd162009-10-30 16:36:56 -07001451
1452 allocatedAppWidgets = true;
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001453
Michael Jurka8b805b12012-04-18 14:23:14 -07001454 // TODO: need to check return value
1455 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, cn);
Winson Chungb3302ae2012-05-01 10:19:14 -07001456
1457 // Send a broadcast to configure the widget
1458 if (extras != null && !extras.isEmpty()) {
1459 Intent intent = new Intent(ACTION_APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE);
1460 intent.setComponent(cn);
1461 intent.putExtras(extras);
1462 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1463 mContext.sendBroadcast(intent);
1464 }
Mike Cleronb87bd162009-10-30 16:36:56 -07001465 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001466 Log.e(TAG, "Problem allocating appWidgetId", ex);
Mike Cleronb87bd162009-10-30 16:36:56 -07001467 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001468
Mike Cleronb87bd162009-10-30 16:36:56 -07001469 return allocatedAppWidgets;
1470 }
Adam Cohen228da5a2011-07-27 22:23:47 -07001471
1472 private long addUriShortcut(SQLiteDatabase db, ContentValues values,
Mike Cleronb87bd162009-10-30 16:36:56 -07001473 TypedArray a) {
1474 Resources r = mContext.getResources();
1475
1476 final int iconResId = a.getResourceId(R.styleable.Favorite_icon, 0);
1477 final int titleResId = a.getResourceId(R.styleable.Favorite_title, 0);
1478
Romain Guy7eb9e5e2009-12-02 20:10:07 -08001479 Intent intent;
Mike Cleronb87bd162009-10-30 16:36:56 -07001480 String uri = null;
1481 try {
1482 uri = a.getString(R.styleable.Favorite_uri);
1483 intent = Intent.parseUri(uri, 0);
1484 } catch (URISyntaxException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001485 Log.w(TAG, "Shortcut has malformed uri: " + uri);
Adam Cohen228da5a2011-07-27 22:23:47 -07001486 return -1; // Oh well
Mike Cleronb87bd162009-10-30 16:36:56 -07001487 }
1488
1489 if (iconResId == 0 || titleResId == 0) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001490 Log.w(TAG, "Shortcut is missing title or icon resource ID");
Adam Cohen228da5a2011-07-27 22:23:47 -07001491 return -1;
Mike Cleronb87bd162009-10-30 16:36:56 -07001492 }
1493
Adam Cohendcd297f2013-06-18 13:13:40 -07001494 long id = generateNewItemId();
Mike Cleronb87bd162009-10-30 16:36:56 -07001495 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1496 values.put(Favorites.INTENT, intent.toUri(0));
1497 values.put(Favorites.TITLE, r.getString(titleResId));
1498 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_SHORTCUT);
1499 values.put(Favorites.SPANX, 1);
1500 values.put(Favorites.SPANY, 1);
1501 values.put(Favorites.ICON_TYPE, Favorites.ICON_TYPE_RESOURCE);
1502 values.put(Favorites.ICON_PACKAGE, mContext.getPackageName());
1503 values.put(Favorites.ICON_RESOURCE, r.getResourceName(iconResId));
Adam Cohen228da5a2011-07-27 22:23:47 -07001504 values.put(Favorites._ID, id);
Mike Cleronb87bd162009-10-30 16:36:56 -07001505
Adam Cohen228da5a2011-07-27 22:23:47 -07001506 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values) < 0) {
1507 return -1;
1508 }
1509 return id;
Mike Cleronb87bd162009-10-30 16:36:56 -07001510 }
Dan Sandlerd5024042014-01-09 15:01:33 -05001511
1512 public void migrateLauncher2Shortcuts(SQLiteDatabase db, Uri uri) {
1513 final ContentResolver resolver = mContext.getContentResolver();
1514 Cursor c = null;
1515 int count = 0;
1516 int curScreen = 0;
1517
1518 try {
1519 c = resolver.query(uri, null, null, null, "title ASC");
1520 } catch (Exception e) {
1521 // Ignore
1522 }
1523
Dan Sandlerd5024042014-01-09 15:01:33 -05001524 // We already have a favorites database in the old provider
1525 if (c != null) {
1526 try {
1527 if (c.getCount() > 0) {
1528 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1529 final int intentIndex
1530 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
1531 final int titleIndex
1532 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
1533 final int iconTypeIndex
1534 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
1535 final int iconIndex
1536 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1537 final int iconPackageIndex
1538 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
1539 final int iconResourceIndex
1540 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
1541 final int containerIndex
1542 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
1543 final int itemTypeIndex
1544 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
1545 final int screenIndex
1546 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
1547 final int cellXIndex
1548 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
1549 final int cellYIndex
1550 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
1551 final int uriIndex
1552 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1553 final int displayModeIndex
1554 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
1555
1556 int i = 0;
1557 int curX = 0;
1558 int curY = 0;
1559
1560 final LauncherAppState app = LauncherAppState.getInstance();
1561 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1562 final int width = (int) grid.numColumns;
1563 final int height = (int) grid.numRows;
1564 final int hotseatWidth = (int) grid.numHotseatIcons;
Adam Cohen556f6132014-01-15 15:18:08 -08001565 PackageManager pm = mContext.getPackageManager();
Dan Sandlerd5024042014-01-09 15:01:33 -05001566
1567 final HashSet<String> seenIntents = new HashSet<String>(c.getCount());
1568
Adam Cohen72960972014-01-15 18:13:55 -08001569 final ArrayList<ContentValues> shortcuts = new ArrayList<ContentValues>();
1570 final ArrayList<ContentValues> folders = new ArrayList<ContentValues>();
Dan Sandlerd5024042014-01-09 15:01:33 -05001571
1572 while (c.moveToNext()) {
1573 final int itemType = c.getInt(itemTypeIndex);
1574 if (itemType != Favorites.ITEM_TYPE_APPLICATION
1575 && itemType != Favorites.ITEM_TYPE_SHORTCUT
1576 && itemType != Favorites.ITEM_TYPE_FOLDER) {
1577 continue;
1578 }
1579
1580 final int cellX = c.getInt(cellXIndex);
1581 final int cellY = c.getInt(cellYIndex);
1582 final int screen = c.getInt(screenIndex);
1583 int container = c.getInt(containerIndex);
1584 final String intentStr = c.getString(intentIndex);
1585 Launcher.addDumpLog(TAG, "migrating \""
1586 + c.getString(titleIndex) + "\": " + intentStr, true);
1587
1588 if (itemType != Favorites.ITEM_TYPE_FOLDER) {
Adam Cohen556f6132014-01-15 15:18:08 -08001589
1590 final Intent intent;
1591 final ComponentName cn;
1592 try {
1593 intent = Intent.parseUri(intentStr, 0);
1594 } catch (URISyntaxException e) {
1595 // bogus intent?
1596 Launcher.addDumpLog(TAG,
1597 "skipping invalid intent uri", true);
1598 continue;
1599 }
1600
1601 cn = intent.getComponent();
Dan Sandlerd5024042014-01-09 15:01:33 -05001602 if (TextUtils.isEmpty(intentStr)) {
1603 // no intent? no icon
1604 Launcher.addDumpLog(TAG, "skipping empty intent", true);
1605 continue;
Adam Cohen72960972014-01-15 18:13:55 -08001606 } else if (cn != null &&
1607 !LauncherModel.isValidPackageComponent(pm, cn)) {
Adam Cohen556f6132014-01-15 15:18:08 -08001608 // component no longer exists.
Adam Cohen72960972014-01-15 18:13:55 -08001609 Launcher.addDumpLog(TAG, "skipping item whose component " +
Adam Cohen556f6132014-01-15 15:18:08 -08001610 "no longer exists.", true);
1611 continue;
Adam Cohen72960972014-01-15 18:13:55 -08001612 } else if (container ==
1613 LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1614 // Dedupe icons directly on the workspace
1615
Adam Cohen556f6132014-01-15 15:18:08 -08001616 // Canonicalize
1617 // the Play Store sets the package parameter, but Launcher
1618 // does not, so we clear that out to keep them the same
1619 intent.setPackage(null);
1620 final String key = intent.toUri(0);
1621 if (seenIntents.contains(key)) {
1622 Launcher.addDumpLog(TAG, "skipping duplicate", true);
Dan Sandlerd5024042014-01-09 15:01:33 -05001623 continue;
Adam Cohen556f6132014-01-15 15:18:08 -08001624 } else {
1625 seenIntents.add(key);
Dan Sandlerd5024042014-01-09 15:01:33 -05001626 }
1627 }
1628 }
1629
1630 ContentValues values = new ContentValues(c.getColumnCount());
1631 values.put(LauncherSettings.Favorites._ID, c.getInt(idIndex));
1632 values.put(LauncherSettings.Favorites.INTENT, intentStr);
1633 values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex));
1634 values.put(LauncherSettings.Favorites.ICON_TYPE,
1635 c.getInt(iconTypeIndex));
1636 values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex));
1637 values.put(LauncherSettings.Favorites.ICON_PACKAGE,
1638 c.getString(iconPackageIndex));
1639 values.put(LauncherSettings.Favorites.ICON_RESOURCE,
1640 c.getString(iconResourceIndex));
1641 values.put(LauncherSettings.Favorites.ITEM_TYPE, itemType);
1642 values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1);
1643 values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex));
1644 values.put(LauncherSettings.Favorites.DISPLAY_MODE,
1645 c.getInt(displayModeIndex));
1646
1647 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT
Adam Cohen72960972014-01-15 18:13:55 -08001648 && (screen >= hotseatWidth ||
1649 screen == grid.hotseatAllAppsRank)) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001650 // no room for you in the hotseat? it's off to the desktop with you
1651 container = Favorites.CONTAINER_DESKTOP;
1652 }
1653
1654 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1655 // In a folder or in the hotseat, preserve position
1656 values.put(LauncherSettings.Favorites.SCREEN, screen);
1657 values.put(LauncherSettings.Favorites.CELLX, cellX);
1658 values.put(LauncherSettings.Favorites.CELLY, cellY);
1659 } else {
Adam Cohen72960972014-01-15 18:13:55 -08001660 // For items contained directly on one of the workspace screen,
1661 // we'll determine their location (screen, x, y) in a second pass.
Dan Sandlerd5024042014-01-09 15:01:33 -05001662 }
1663
1664 values.put(LauncherSettings.Favorites.CONTAINER, container);
1665
Adam Cohen72960972014-01-15 18:13:55 -08001666 if (itemType != Favorites.ITEM_TYPE_FOLDER) {
1667 shortcuts.add(values);
1668 } else {
1669 folders.add(values);
1670 }
Dan Sandlerd5024042014-01-09 15:01:33 -05001671 }
1672
Adam Cohen72960972014-01-15 18:13:55 -08001673 final ArrayList<ContentValues> allItems = new ArrayList<ContentValues>();
1674 // Folders first
1675 allItems.addAll(folders);
1676 // Then shortcuts
1677 allItems.addAll(shortcuts);
1678
1679 // Layout all the folders
1680 for (ContentValues values: allItems) {
1681 if (values.getAsInteger(LauncherSettings.Favorites.CONTAINER) !=
1682 LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1683 // Hotseat items and folder items have already had their
1684 // location information set. Nothing to be done here.
1685 continue;
1686 }
1687 values.put(LauncherSettings.Favorites.SCREEN, curScreen);
1688 values.put(LauncherSettings.Favorites.CELLX, curX);
1689 values.put(LauncherSettings.Favorites.CELLY, curY);
1690 curX = (curX + 1) % width;
1691 if (curX == 0) {
1692 curY = (curY + 1);
1693 }
1694 // Leave the last row of icons blank on every screen
1695 if (curY == height - 1) {
1696 curScreen = (int) generateNewScreenId();
1697 curY = 0;
1698 }
1699 }
1700
1701 if (allItems.size() > 0) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001702 db.beginTransaction();
1703 try {
Adam Cohen72960972014-01-15 18:13:55 -08001704 for (ContentValues row: allItems) {
1705 if (row == null) continue;
1706 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, row)
Dan Sandlerd5024042014-01-09 15:01:33 -05001707 < 0) {
1708 return;
1709 } else {
1710 count++;
1711 }
1712 }
1713 db.setTransactionSuccessful();
1714 } finally {
1715 db.endTransaction();
1716 }
1717 }
1718
1719 db.beginTransaction();
1720 try {
1721 for (i=0; i<=curScreen; i++) {
1722 final ContentValues values = new ContentValues();
1723 values.put(LauncherSettings.WorkspaceScreens._ID, i);
1724 values.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
1725 if (dbInsertAndCheck(this, db, TABLE_WORKSPACE_SCREENS, null, values)
1726 < 0) {
1727 return;
1728 }
1729 }
1730 db.setTransactionSuccessful();
1731 } finally {
1732 db.endTransaction();
1733 }
1734 }
1735 } finally {
1736 c.close();
1737 }
1738 }
1739
1740 Launcher.addDumpLog(TAG, "migrated " + count + " icons from Launcher2 into "
1741 + (curScreen+1) + " screens", true);
1742
1743 // ensure that new screens are created to hold these icons
1744 setFlagJustLoadedOldDb();
1745
1746 // Update max IDs; very important since we just grabbed IDs from another database
1747 mMaxItemId = initializeMaxItemId(db);
1748 mMaxScreenId = initializeMaxScreenId(db);
1749 if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId + " mMaxScreenId: " + mMaxScreenId);
1750 }
Mike Cleronb87bd162009-10-30 16:36:56 -07001751 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001752
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001753 /**
1754 * Build a query string that will match any row where the column matches
1755 * anything in the values list.
1756 */
1757 static String buildOrWhereString(String column, int[] values) {
1758 StringBuilder selectWhere = new StringBuilder();
1759 for (int i = values.length - 1; i >= 0; i--) {
1760 selectWhere.append(column).append("=").append(values[i]);
1761 if (i > 0) {
1762 selectWhere.append(" OR ");
1763 }
1764 }
1765 return selectWhere.toString();
1766 }
1767
1768 static class SqlArguments {
1769 public final String table;
1770 public final String where;
1771 public final String[] args;
1772
1773 SqlArguments(Uri url, String where, String[] args) {
1774 if (url.getPathSegments().size() == 1) {
1775 this.table = url.getPathSegments().get(0);
1776 this.where = where;
1777 this.args = args;
1778 } else if (url.getPathSegments().size() != 2) {
1779 throw new IllegalArgumentException("Invalid URI: " + url);
1780 } else if (!TextUtils.isEmpty(where)) {
1781 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
1782 } else {
1783 this.table = url.getPathSegments().get(0);
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001784 this.where = "_id=" + ContentUris.parseId(url);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001785 this.args = null;
1786 }
1787 }
1788
1789 SqlArguments(Uri url) {
1790 if (url.getPathSegments().size() == 1) {
1791 table = url.getPathSegments().get(0);
1792 where = null;
1793 args = null;
1794 } else {
1795 throw new IllegalArgumentException("Invalid URI: " + url);
1796 }
1797 }
1798 }
Adam Cohen72960972014-01-15 18:13:55 -08001799}