blob: b952729bd0364beb702de2a28141625bd6290cdd [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
Adam Cohen71e03b92014-02-21 14:09:53 -080074 private static final int DATABASE_VERSION = 17;
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
Adam Cohen71e03b92014-02-21 14:09:53 -0800772 if (version < 17) {
773 // We use the db version upgrade here to identify users who may not have seen
774 // clings yet (because they weren't available), but for whom the clings are now
775 // available (tablet users). Because one of the possible cling flows (migration)
776 // is very destructive (wipes out workspaces), we want to prevent this from showing
777 // until clear data. We do so by marking that the clings have been shown.
778 LauncherClings.synchonouslyMarkFirstRunClingDismissed(mContext);
779 version = 17;
780 }
781
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800782 if (version != DATABASE_VERSION) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800783 Log.w(TAG, "Destroying all old data.");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800784 db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES);
Adam Cohendcd297f2013-06-18 13:13:40 -0700785 db.execSQL("DROP TABLE IF EXISTS " + TABLE_WORKSPACE_SCREENS);
786
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800787 onCreate(db);
788 }
789 }
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800790
791 private boolean updateContactsShortcuts(SQLiteDatabase db) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800792 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE,
793 new int[] { Favorites.ITEM_TYPE_SHORTCUT });
794
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700795 Cursor c = null;
796 final String actionQuickContact = "com.android.contacts.action.QUICK_CONTACT";
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800797 db.beginTransaction();
798 try {
799 // Select and iterate through each matching widget
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700800 c = db.query(TABLE_FAVORITES,
801 new String[] { Favorites._ID, Favorites.INTENT },
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800802 selectWhere, null, null, null, null);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700803 if (c == null) return false;
804
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800805 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700806
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800807 final int idIndex = c.getColumnIndex(Favorites._ID);
808 final int intentIndex = c.getColumnIndex(Favorites.INTENT);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700809
810 while (c.moveToNext()) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800811 long favoriteId = c.getLong(idIndex);
812 final String intentUri = c.getString(intentIndex);
813 if (intentUri != null) {
814 try {
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700815 final Intent intent = Intent.parseUri(intentUri, 0);
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800816 android.util.Log.d("Home", intent.toString());
817 final Uri uri = intent.getData();
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700818 if (uri != null) {
819 final String data = uri.toString();
820 if ((Intent.ACTION_VIEW.equals(intent.getAction()) ||
821 actionQuickContact.equals(intent.getAction())) &&
822 (data.startsWith("content://contacts/people/") ||
823 data.startsWith("content://com.android.contacts/" +
824 "contacts/lookup/"))) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800825
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700826 final Intent newIntent = new Intent(actionQuickContact);
827 // When starting from the launcher, start in a new, cleared task
828 // CLEAR_WHEN_TASK_RESET cannot reset the root of a task, so we
829 // clear the whole thing preemptively here since
830 // QuickContactActivity will finish itself when launching other
831 // detail activities.
832 newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
833 Intent.FLAG_ACTIVITY_CLEAR_TASK);
Winson Chung2672ff92012-05-04 16:22:30 -0700834 newIntent.putExtra(
835 Launcher.INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION, true);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700836 newIntent.setData(uri);
Daniel Lehmannd02402c2012-05-14 18:30:53 -0700837 // Determine the type and also put that in the shortcut
838 // (that can speed up launch a bit)
839 newIntent.setDataAndType(uri, newIntent.resolveType(mContext));
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800840
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700841 final ContentValues values = new ContentValues();
842 values.put(LauncherSettings.Favorites.INTENT,
843 newIntent.toUri(0));
844
845 String updateWhere = Favorites._ID + "=" + favoriteId;
846 db.update(TABLE_FAVORITES, values, updateWhere, null);
847 }
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800848 }
849 } catch (RuntimeException ex) {
850 Log.e(TAG, "Problem upgrading shortcut", ex);
851 } catch (URISyntaxException e) {
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700852 Log.e(TAG, "Problem upgrading shortcut", e);
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800853 }
854 }
855 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700856
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800857 db.setTransactionSuccessful();
858 } catch (SQLException ex) {
859 Log.w(TAG, "Problem while upgrading contacts", ex);
860 return false;
861 } finally {
862 db.endTransaction();
863 if (c != null) {
864 c.close();
865 }
866 }
867
868 return true;
869 }
870
Joe Onorato0589f0f2010-02-08 13:44:00 -0800871 private void normalizeIcons(SQLiteDatabase db) {
872 Log.d(TAG, "normalizing icons");
873
Joe Onorato346e1292010-02-18 10:34:24 -0500874 db.beginTransaction();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800875 Cursor c = null;
Joe Onorato9690b392010-03-23 17:34:37 -0400876 SQLiteStatement update = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800877 try {
878 boolean logged = false;
Joe Onorato9690b392010-03-23 17:34:37 -0400879 update = db.compileStatement("UPDATE favorites "
Jeff Hamiltoneaf77d62010-02-13 00:08:17 -0600880 + "SET icon=? WHERE _id=?");
Joe Onorato0589f0f2010-02-08 13:44:00 -0800881
882 c = db.rawQuery("SELECT _id, icon FROM favorites WHERE iconType=" +
883 Favorites.ICON_TYPE_BITMAP, null);
884
885 final int idIndex = c.getColumnIndexOrThrow(Favorites._ID);
886 final int iconIndex = c.getColumnIndexOrThrow(Favorites.ICON);
887
888 while (c.moveToNext()) {
889 long id = c.getLong(idIndex);
890 byte[] data = c.getBlob(iconIndex);
891 try {
892 Bitmap bitmap = Utilities.resampleIconBitmap(
893 BitmapFactory.decodeByteArray(data, 0, data.length),
894 mContext);
895 if (bitmap != null) {
896 update.bindLong(1, id);
897 data = ItemInfo.flattenBitmap(bitmap);
898 if (data != null) {
899 update.bindBlob(2, data);
900 update.execute();
901 }
902 bitmap.recycle();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800903 }
904 } catch (Exception e) {
905 if (!logged) {
906 Log.e(TAG, "Failed normalizing icon " + id, e);
907 } else {
908 Log.e(TAG, "Also failed normalizing icon " + id);
909 }
910 logged = true;
911 }
912 }
Bjorn Bringert3a928e42010-02-19 11:15:40 +0000913 db.setTransactionSuccessful();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800914 } catch (SQLException ex) {
915 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
916 } finally {
917 db.endTransaction();
Joe Onorato9690b392010-03-23 17:34:37 -0400918 if (update != null) {
919 update.close();
920 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800921 if (c != null) {
922 c.close();
923 }
924 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700925 }
926
927 // Generates a new ID to use for an object in your database. This method should be only
928 // called from the main UI thread. As an exception, we do call it when we call the
929 // constructor from the worker thread; however, this doesn't extend until after the
930 // constructor is called, and we only pass a reference to LauncherProvider to LauncherApp
931 // after that point
Adam Cohendcd297f2013-06-18 13:13:40 -0700932 public long generateNewItemId() {
933 if (mMaxItemId < 0) {
934 throw new RuntimeException("Error: max item id was not initialized");
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700935 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700936 mMaxItemId += 1;
937 return mMaxItemId;
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700938 }
939
Winson Chungc763c4e2013-07-19 13:49:06 -0700940 public void updateMaxItemId(long id) {
941 mMaxItemId = id + 1;
942 }
943
Chris Wren5dee7af2013-12-20 17:22:11 -0500944 public void checkId(String table, ContentValues values) {
945 long id = values.getAsLong(LauncherSettings.BaseLauncherColumns._ID);
946 if (table == LauncherProvider.TABLE_WORKSPACE_SCREENS) {
947 mMaxScreenId = Math.max(id, mMaxScreenId);
948 } else {
949 mMaxItemId = Math.max(id, mMaxItemId);
950 }
951 }
952
Adam Cohendcd297f2013-06-18 13:13:40 -0700953 private long initializeMaxItemId(SQLiteDatabase db) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700954 Cursor c = db.rawQuery("SELECT MAX(_id) FROM favorites", null);
955
956 // get the result
957 final int maxIdIndex = 0;
958 long id = -1;
959 if (c != null && c.moveToNext()) {
960 id = c.getLong(maxIdIndex);
961 }
Michael Jurka5130e402011-10-13 04:55:35 -0700962 if (c != null) {
963 c.close();
964 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700965
966 if (id == -1) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700967 throw new RuntimeException("Error: could not query max item id");
968 }
969
970 return id;
971 }
972
973 // Generates a new ID to use for an workspace screen in your database. This method
974 // should be only called from the main UI thread. As an exception, we do call it when we
975 // call the constructor from the worker thread; however, this doesn't extend until after the
976 // constructor is called, and we only pass a reference to LauncherProvider to LauncherApp
977 // after that point
978 public long generateNewScreenId() {
979 if (mMaxScreenId < 0) {
980 throw new RuntimeException("Error: max screen id was not initialized");
981 }
982 mMaxScreenId += 1;
Winson Chunga90303b2013-11-15 13:05:06 -0800983 // Log to disk
984 Launcher.addDumpLog(TAG, "11683562 - generateNewScreenId(): " + mMaxScreenId, true);
Adam Cohendcd297f2013-06-18 13:13:40 -0700985 return mMaxScreenId;
986 }
987
988 public void updateMaxScreenId(long maxScreenId) {
Winson Chunga90303b2013-11-15 13:05:06 -0800989 // Log to disk
990 Launcher.addDumpLog(TAG, "11683562 - updateMaxScreenId(): " + maxScreenId, true);
Adam Cohendcd297f2013-06-18 13:13:40 -0700991 mMaxScreenId = maxScreenId;
992 }
993
994 private long initializeMaxScreenId(SQLiteDatabase db) {
995 Cursor c = db.rawQuery("SELECT MAX(" + LauncherSettings.WorkspaceScreens._ID + ") FROM " + TABLE_WORKSPACE_SCREENS, null);
996
997 // get the result
998 final int maxIdIndex = 0;
999 long id = -1;
1000 if (c != null && c.moveToNext()) {
1001 id = c.getLong(maxIdIndex);
1002 }
1003 if (c != null) {
1004 c.close();
1005 }
1006
1007 if (id == -1) {
1008 throw new RuntimeException("Error: could not query max screen id");
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001009 }
1010
Winson Chunga90303b2013-11-15 13:05:06 -08001011 // Log to disk
1012 Launcher.addDumpLog(TAG, "11683562 - initializeMaxScreenId(): " + id, true);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001013 return id;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001014 }
1015
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001016 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001017 * Upgrade existing clock and photo frame widgets into their new widget
Bjorn Bringert93c45762009-12-16 13:19:47 +00001018 * equivalents.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001019 */
1020 private void convertWidgets(SQLiteDatabase db) {
Bjorn Bringert34251342009-12-15 13:33:11 +00001021 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 final int[] bindSources = new int[] {
1023 Favorites.ITEM_TYPE_WIDGET_CLOCK,
1024 Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME,
Bjorn Bringert7984c942009-12-09 15:38:25 +00001025 Favorites.ITEM_TYPE_WIDGET_SEARCH,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 };
Bjorn Bringert7984c942009-12-09 15:38:25 +00001027
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001028 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE, bindSources);
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001029
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001030 Cursor c = null;
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001031
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001032 db.beginTransaction();
1033 try {
1034 // Select and iterate through each matching widget
Bjorn Bringert7984c942009-12-09 15:38:25 +00001035 c = db.query(TABLE_FAVORITES, new String[] { Favorites._ID, Favorites.ITEM_TYPE },
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036 selectWhere, null, null, null, null);
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001037
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001038 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001039
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001040 final ContentValues values = new ContentValues();
1041 while (c != null && c.moveToNext()) {
1042 long favoriteId = c.getLong(0);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001043 int favoriteType = c.getInt(1);
1044
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001045 // Allocate and update database with new appWidgetId
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001046 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001047 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001048
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001049 if (LOGD) {
1050 Log.d(TAG, "allocated appWidgetId=" + appWidgetId
1051 + " for favoriteId=" + favoriteId);
1052 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053 values.clear();
Bjorn Bringert7984c942009-12-09 15:38:25 +00001054 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
1055 values.put(Favorites.APPWIDGET_ID, appWidgetId);
1056
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057 // Original widgets might not have valid spans when upgrading
Bjorn Bringert7984c942009-12-09 15:38:25 +00001058 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
1059 values.put(LauncherSettings.Favorites.SPANX, 4);
1060 values.put(LauncherSettings.Favorites.SPANY, 1);
1061 } else {
1062 values.put(LauncherSettings.Favorites.SPANX, 2);
1063 values.put(LauncherSettings.Favorites.SPANY, 2);
1064 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001065
1066 String updateWhere = Favorites._ID + "=" + favoriteId;
1067 db.update(TABLE_FAVORITES, values, updateWhere, null);
Bjorn Bringert34251342009-12-15 13:33:11 +00001068
Bjorn Bringert34251342009-12-15 13:33:11 +00001069 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_CLOCK) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001070 // TODO: check return value
1071 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
Bjorn Bringert34251342009-12-15 13:33:11 +00001072 new ComponentName("com.android.alarmclock",
1073 "com.android.alarmclock.AnalogAppWidgetProvider"));
1074 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001075 // TODO: check return value
1076 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
Bjorn Bringert34251342009-12-15 13:33:11 +00001077 new ComponentName("com.android.camera",
1078 "com.android.camera.PhotoAppWidgetProvider"));
1079 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001080 // TODO: check return value
1081 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
Bjorn Bringertcd8fec02010-01-14 13:26:43 +00001082 getSearchWidgetProvider());
Bjorn Bringert34251342009-12-15 13:33:11 +00001083 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001085 Log.e(TAG, "Problem allocating appWidgetId", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086 }
1087 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001088
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001089 db.setTransactionSuccessful();
1090 } catch (SQLException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001091 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 } finally {
1093 db.endTransaction();
1094 if (c != null) {
1095 c.close();
1096 }
1097 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001098
1099 // Update max item id
1100 mMaxItemId = initializeMaxItemId(db);
1101 if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001102 }
1103
Michael Jurka8b805b12012-04-18 14:23:14 -07001104 private static final void beginDocument(XmlPullParser parser, String firstElementName)
1105 throws XmlPullParserException, IOException {
1106 int type;
Michael Jurka9bc8eba2012-05-21 20:36:44 -07001107 while ((type = parser.next()) != XmlPullParser.START_TAG
1108 && type != XmlPullParser.END_DOCUMENT) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001109 ;
1110 }
1111
Michael Jurka9bc8eba2012-05-21 20:36:44 -07001112 if (type != XmlPullParser.START_TAG) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001113 throw new XmlPullParserException("No start tag found");
1114 }
1115
1116 if (!parser.getName().equals(firstElementName)) {
1117 throw new XmlPullParserException("Unexpected start tag: found " + parser.getName() +
1118 ", expected " + firstElementName);
1119 }
1120 }
1121
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001122 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001123 * Loads the default set of favorite packages from an xml file.
1124 *
1125 * @param db The database to write the values into
Winson Chung3d503fb2011-07-13 17:25:49 -07001126 * @param filterContainerId The specific container id of items to load
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001127 */
Winson Chung6d092682011-11-16 18:43:26 -08001128 private int loadFavorites(SQLiteDatabase db, int workspaceResourceId) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 Intent intent = new Intent(Intent.ACTION_MAIN, null);
1130 intent.addCategory(Intent.CATEGORY_LAUNCHER);
1131 ContentValues values = new ContentValues();
1132
Daniel Sandler57dac262013-10-03 13:28:36 -04001133 if (LOGD) Log.v(TAG, String.format("Loading favorites from resid=0x%08x", workspaceResourceId));
1134
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001135 PackageManager packageManager = mContext.getPackageManager();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001136 int i = 0;
1137 try {
Winson Chung6d092682011-11-16 18:43:26 -08001138 XmlResourceParser parser = mContext.getResources().getXml(workspaceResourceId);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001139 AttributeSet attrs = Xml.asAttributeSet(parser);
Michael Jurka8b805b12012-04-18 14:23:14 -07001140 beginDocument(parser, TAG_FAVORITES);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001142 final int depth = parser.getDepth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001144 int type;
1145 while (((type = parser.next()) != XmlPullParser.END_TAG ||
1146 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
1147
1148 if (type != XmlPullParser.START_TAG) {
1149 continue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001150 }
1151
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001152 boolean added = false;
1153 final String name = parser.getName();
1154
Daniel Sandler57dac262013-10-03 13:28:36 -04001155 if (TAG_INCLUDE.equals(name)) {
1156 final TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.Include);
1157
1158 final int resId = a.getResourceId(R.styleable.Include_workspace, 0);
1159
1160 if (LOGD) Log.v(TAG, String.format(("%" + (2*(depth+1)) + "s<include workspace=%08x>"),
1161 "", resId));
1162
1163 if (resId != 0 && resId != workspaceResourceId) {
1164 // recursively load some more favorites, why not?
1165 i += loadFavorites(db, resId);
1166 added = false;
Daniel Sandler57dac262013-10-03 13:28:36 -04001167 } else {
1168 Log.w(TAG, String.format("Skipping <include workspace=0x%08x>", resId));
1169 }
1170
1171 a.recycle();
1172
1173 if (LOGD) Log.v(TAG, String.format(("%" + (2*(depth+1)) + "s</include>"), ""));
1174 continue;
1175 }
1176
1177 // Assuming it's a <favorite> at this point
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001178 TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.Favorite);
1179
Winson Chung3d503fb2011-07-13 17:25:49 -07001180 long container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
1181 if (a.hasValue(R.styleable.Favorite_container)) {
1182 container = Long.valueOf(a.getString(R.styleable.Favorite_container));
1183 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001184
Winson Chung6d092682011-11-16 18:43:26 -08001185 String screen = a.getString(R.styleable.Favorite_screen);
1186 String x = a.getString(R.styleable.Favorite_x);
1187 String y = a.getString(R.styleable.Favorite_y);
1188
Winson Chung6d092682011-11-16 18:43:26 -08001189 values.clear();
1190 values.put(LauncherSettings.Favorites.CONTAINER, container);
1191 values.put(LauncherSettings.Favorites.SCREEN, screen);
1192 values.put(LauncherSettings.Favorites.CELLX, x);
1193 values.put(LauncherSettings.Favorites.CELLY, y);
1194
Daniel Sandler57dac262013-10-03 13:28:36 -04001195 if (LOGD) {
1196 final String title = a.getString(R.styleable.Favorite_title);
1197 final String pkg = a.getString(R.styleable.Favorite_packageName);
1198 final String something = title != null ? title : pkg;
1199 Log.v(TAG, String.format(
1200 ("%" + (2*(depth+1)) + "s<%s%s c=%d s=%s x=%s y=%s>"),
1201 "", name,
1202 (something == null ? "" : (" \"" + something + "\"")),
1203 container, screen, x, y));
1204 }
1205
Winson Chung6d092682011-11-16 18:43:26 -08001206 if (TAG_FAVORITE.equals(name)) {
1207 long id = addAppShortcut(db, values, a, packageManager, intent);
1208 added = id >= 0;
1209 } else if (TAG_SEARCH.equals(name)) {
1210 added = addSearchWidget(db, values);
1211 } else if (TAG_CLOCK.equals(name)) {
1212 added = addClockWidget(db, values);
1213 } else if (TAG_APPWIDGET.equals(name)) {
Winson Chungb3302ae2012-05-01 10:19:14 -07001214 added = addAppWidget(parser, attrs, type, db, values, a, packageManager);
Winson Chung6d092682011-11-16 18:43:26 -08001215 } else if (TAG_SHORTCUT.equals(name)) {
1216 long id = addUriShortcut(db, values, a);
1217 added = id >= 0;
1218 } else if (TAG_FOLDER.equals(name)) {
1219 String title;
1220 int titleResId = a.getResourceId(R.styleable.Favorite_title, -1);
1221 if (titleResId != -1) {
1222 title = mContext.getResources().getString(titleResId);
1223 } else {
1224 title = mContext.getResources().getString(R.string.folder_name);
Winson Chung3d503fb2011-07-13 17:25:49 -07001225 }
Winson Chung6d092682011-11-16 18:43:26 -08001226 values.put(LauncherSettings.Favorites.TITLE, title);
1227 long folderId = addFolder(db, values);
1228 added = folderId >= 0;
Winson Chung3d503fb2011-07-13 17:25:49 -07001229
Winson Chung6d092682011-11-16 18:43:26 -08001230 ArrayList<Long> folderItems = new ArrayList<Long>();
Winson Chung3d503fb2011-07-13 17:25:49 -07001231
Winson Chung6d092682011-11-16 18:43:26 -08001232 int folderDepth = parser.getDepth();
1233 while ((type = parser.next()) != XmlPullParser.END_TAG ||
1234 parser.getDepth() > folderDepth) {
1235 if (type != XmlPullParser.START_TAG) {
1236 continue;
1237 }
1238 final String folder_item_name = parser.getName();
1239
1240 TypedArray ar = mContext.obtainStyledAttributes(attrs,
1241 R.styleable.Favorite);
1242 values.clear();
1243 values.put(LauncherSettings.Favorites.CONTAINER, folderId);
1244
Daniel Sandler57dac262013-10-03 13:28:36 -04001245 if (LOGD) {
1246 final String pkg = ar.getString(R.styleable.Favorite_packageName);
1247 final String uri = ar.getString(R.styleable.Favorite_uri);
1248 Log.v(TAG, String.format(("%" + (2*(folderDepth+1)) + "s<%s \"%s\">"), "",
1249 folder_item_name, uri != null ? uri : pkg));
1250 }
1251
Winson Chung6d092682011-11-16 18:43:26 -08001252 if (TAG_FAVORITE.equals(folder_item_name) && folderId >= 0) {
1253 long id =
1254 addAppShortcut(db, values, ar, packageManager, intent);
1255 if (id >= 0) {
1256 folderItems.add(id);
1257 }
1258 } else if (TAG_SHORTCUT.equals(folder_item_name) && folderId >= 0) {
1259 long id = addUriShortcut(db, values, ar);
1260 if (id >= 0) {
1261 folderItems.add(id);
1262 }
Adam Cohen228da5a2011-07-27 22:23:47 -07001263 } else {
Winson Chung6d092682011-11-16 18:43:26 -08001264 throw new RuntimeException("Folders can " +
1265 "contain only shortcuts");
Adam Cohen228da5a2011-07-27 22:23:47 -07001266 }
Winson Chung6d092682011-11-16 18:43:26 -08001267 ar.recycle();
1268 }
1269 // We can only have folders with >= 2 items, so we need to remove the
1270 // folder and clean up if less than 2 items were included, or some
1271 // failed to add, and less than 2 were actually added
1272 if (folderItems.size() < 2 && folderId >= 0) {
1273 // We just delete the folder and any items that made it
1274 deleteId(db, folderId);
1275 if (folderItems.size() > 0) {
1276 deleteId(db, folderItems.get(0));
Adam Cohen228da5a2011-07-27 22:23:47 -07001277 }
Winson Chung6d092682011-11-16 18:43:26 -08001278 added = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001279 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001281 if (added) i++;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001282 a.recycle();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283 }
1284 } catch (XmlPullParserException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001285 Log.w(TAG, "Got exception parsing favorites.", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 } catch (IOException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001287 Log.w(TAG, "Got exception parsing favorites.", e);
Winson Chung3d503fb2011-07-13 17:25:49 -07001288 } catch (RuntimeException e) {
1289 Log.w(TAG, "Got exception parsing favorites.", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001290 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001291
Winson Chungc763c4e2013-07-19 13:49:06 -07001292 // Update the max item id after we have loaded the database
1293 if (mMaxItemId == -1) {
1294 mMaxItemId = initializeMaxItemId(db);
1295 }
1296
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001297 return i;
1298 }
1299
Adam Cohen228da5a2011-07-27 22:23:47 -07001300 private long addAppShortcut(SQLiteDatabase db, ContentValues values, TypedArray a,
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001301 PackageManager packageManager, Intent intent) {
Adam Cohen228da5a2011-07-27 22:23:47 -07001302 long id = -1;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001303 ActivityInfo info;
1304 String packageName = a.getString(R.styleable.Favorite_packageName);
1305 String className = a.getString(R.styleable.Favorite_className);
1306 try {
Romain Guy693599f2010-03-23 10:58:18 -07001307 ComponentName cn;
1308 try {
1309 cn = new ComponentName(packageName, className);
1310 info = packageManager.getActivityInfo(cn, 0);
1311 } catch (PackageManager.NameNotFoundException nnfe) {
1312 String[] packages = packageManager.currentToCanonicalPackageNames(
1313 new String[] { packageName });
1314 cn = new ComponentName(packages[0], className);
1315 info = packageManager.getActivityInfo(cn, 0);
1316 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001317 id = generateNewItemId();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001318 intent.setComponent(cn);
Romain Guy693599f2010-03-23 10:58:18 -07001319 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1320 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Romain Guy1ce1a242009-06-23 17:34:54 -07001321 values.put(Favorites.INTENT, intent.toUri(0));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001322 values.put(Favorites.TITLE, info.loadLabel(packageManager).toString());
1323 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPLICATION);
1324 values.put(Favorites.SPANX, 1);
1325 values.put(Favorites.SPANY, 1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001326 values.put(Favorites._ID, generateNewItemId());
Adam Cohen228da5a2011-07-27 22:23:47 -07001327 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values) < 0) {
1328 return -1;
1329 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001330 } catch (PackageManager.NameNotFoundException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001331 Log.w(TAG, "Unable to add favorite: " + packageName +
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001332 "/" + className, e);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001333 }
Adam Cohen228da5a2011-07-27 22:23:47 -07001334 return id;
1335 }
1336
1337 private long addFolder(SQLiteDatabase db, ContentValues values) {
1338 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_FOLDER);
1339 values.put(Favorites.SPANX, 1);
1340 values.put(Favorites.SPANY, 1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001341 long id = generateNewItemId();
Adam Cohen228da5a2011-07-27 22:23:47 -07001342 values.put(Favorites._ID, id);
1343 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values) <= 0) {
1344 return -1;
1345 } else {
1346 return id;
1347 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001348 }
1349
Bjorn Bringertcd8fec02010-01-14 13:26:43 +00001350 private ComponentName getSearchWidgetProvider() {
1351 SearchManager searchManager =
1352 (SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE);
1353 ComponentName searchComponent = searchManager.getGlobalSearchActivity();
1354 if (searchComponent == null) return null;
1355 return getProviderInPackage(searchComponent.getPackageName());
1356 }
1357
1358 /**
1359 * Gets an appwidget provider from the given package. If the package contains more than
1360 * one appwidget provider, an arbitrary one is returned.
1361 */
1362 private ComponentName getProviderInPackage(String packageName) {
1363 AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
1364 List<AppWidgetProviderInfo> providers = appWidgetManager.getInstalledProviders();
1365 if (providers == null) return null;
1366 final int providerCount = providers.size();
1367 for (int i = 0; i < providerCount; i++) {
1368 ComponentName provider = providers.get(i).provider;
1369 if (provider != null && provider.getPackageName().equals(packageName)) {
1370 return provider;
1371 }
1372 }
1373 return null;
1374 }
1375
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001376 private boolean addSearchWidget(SQLiteDatabase db, ContentValues values) {
Bjorn Bringertcd8fec02010-01-14 13:26:43 +00001377 ComponentName cn = getSearchWidgetProvider();
Winson Chungb3302ae2012-05-01 10:19:14 -07001378 return addAppWidget(db, values, cn, 4, 1, null);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001379 }
1380
1381 private boolean addClockWidget(SQLiteDatabase db, ContentValues values) {
Bjorn Bringert34251342009-12-15 13:33:11 +00001382 ComponentName cn = new ComponentName("com.android.alarmclock",
1383 "com.android.alarmclock.AnalogAppWidgetProvider");
Winson Chungb3302ae2012-05-01 10:19:14 -07001384 return addAppWidget(db, values, cn, 2, 2, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 }
Adam Cohen228da5a2011-07-27 22:23:47 -07001386
Winson Chungb3302ae2012-05-01 10:19:14 -07001387 private boolean addAppWidget(XmlResourceParser parser, AttributeSet attrs, int type,
1388 SQLiteDatabase db, ContentValues values, TypedArray a,
1389 PackageManager packageManager) throws XmlPullParserException, IOException {
Romain Guy693599f2010-03-23 10:58:18 -07001390
Mike Cleronb87bd162009-10-30 16:36:56 -07001391 String packageName = a.getString(R.styleable.Favorite_packageName);
1392 String className = a.getString(R.styleable.Favorite_className);
1393
1394 if (packageName == null || className == null) {
1395 return false;
1396 }
Romain Guy693599f2010-03-23 10:58:18 -07001397
1398 boolean hasPackage = true;
Mike Cleronb87bd162009-10-30 16:36:56 -07001399 ComponentName cn = new ComponentName(packageName, className);
Romain Guy693599f2010-03-23 10:58:18 -07001400 try {
1401 packageManager.getReceiverInfo(cn, 0);
1402 } catch (Exception e) {
1403 String[] packages = packageManager.currentToCanonicalPackageNames(
1404 new String[] { packageName });
1405 cn = new ComponentName(packages[0], className);
1406 try {
1407 packageManager.getReceiverInfo(cn, 0);
1408 } catch (Exception e1) {
1409 hasPackage = false;
1410 }
1411 }
1412
1413 if (hasPackage) {
1414 int spanX = a.getInt(R.styleable.Favorite_spanX, 0);
1415 int spanY = a.getInt(R.styleable.Favorite_spanY, 0);
Winson Chungb3302ae2012-05-01 10:19:14 -07001416
1417 // Read the extras
1418 Bundle extras = new Bundle();
1419 int widgetDepth = parser.getDepth();
1420 while ((type = parser.next()) != XmlPullParser.END_TAG ||
1421 parser.getDepth() > widgetDepth) {
1422 if (type != XmlPullParser.START_TAG) {
1423 continue;
1424 }
1425
1426 TypedArray ar = mContext.obtainStyledAttributes(attrs, R.styleable.Extra);
1427 if (TAG_EXTRA.equals(parser.getName())) {
1428 String key = ar.getString(R.styleable.Extra_key);
1429 String value = ar.getString(R.styleable.Extra_value);
1430 if (key != null && value != null) {
1431 extras.putString(key, value);
1432 } else {
1433 throw new RuntimeException("Widget extras must have a key and value");
1434 }
1435 } else {
1436 throw new RuntimeException("Widgets can contain only extras");
1437 }
1438 ar.recycle();
1439 }
1440
1441 return addAppWidget(db, values, cn, spanX, spanY, extras);
Romain Guy693599f2010-03-23 10:58:18 -07001442 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001443
Romain Guy693599f2010-03-23 10:58:18 -07001444 return false;
Bjorn Bringert7984c942009-12-09 15:38:25 +00001445 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001446 private boolean addAppWidget(SQLiteDatabase db, ContentValues values, ComponentName cn,
Winson Chungb3302ae2012-05-01 10:19:14 -07001447 int spanX, int spanY, Bundle extras) {
Mike Cleronb87bd162009-10-30 16:36:56 -07001448 boolean allocatedAppWidgets = false;
1449 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
1450
1451 try {
1452 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001453
Mike Cleronb87bd162009-10-30 16:36:56 -07001454 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001455 values.put(Favorites.SPANX, spanX);
1456 values.put(Favorites.SPANY, spanY);
Mike Cleronb87bd162009-10-30 16:36:56 -07001457 values.put(Favorites.APPWIDGET_ID, appWidgetId);
Chris Wrend5e66bf2013-09-16 14:02:29 -04001458 values.put(Favorites.APPWIDGET_PROVIDER, cn.flattenToString());
Adam Cohendcd297f2013-06-18 13:13:40 -07001459 values.put(Favorites._ID, generateNewItemId());
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001460 dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values);
Mike Cleronb87bd162009-10-30 16:36:56 -07001461
1462 allocatedAppWidgets = true;
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001463
Michael Jurka8b805b12012-04-18 14:23:14 -07001464 // TODO: need to check return value
1465 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, cn);
Winson Chungb3302ae2012-05-01 10:19:14 -07001466
1467 // Send a broadcast to configure the widget
1468 if (extras != null && !extras.isEmpty()) {
1469 Intent intent = new Intent(ACTION_APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE);
1470 intent.setComponent(cn);
1471 intent.putExtras(extras);
1472 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1473 mContext.sendBroadcast(intent);
1474 }
Mike Cleronb87bd162009-10-30 16:36:56 -07001475 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001476 Log.e(TAG, "Problem allocating appWidgetId", ex);
Mike Cleronb87bd162009-10-30 16:36:56 -07001477 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001478
Mike Cleronb87bd162009-10-30 16:36:56 -07001479 return allocatedAppWidgets;
1480 }
Adam Cohen228da5a2011-07-27 22:23:47 -07001481
1482 private long addUriShortcut(SQLiteDatabase db, ContentValues values,
Mike Cleronb87bd162009-10-30 16:36:56 -07001483 TypedArray a) {
1484 Resources r = mContext.getResources();
1485
1486 final int iconResId = a.getResourceId(R.styleable.Favorite_icon, 0);
1487 final int titleResId = a.getResourceId(R.styleable.Favorite_title, 0);
1488
Romain Guy7eb9e5e2009-12-02 20:10:07 -08001489 Intent intent;
Mike Cleronb87bd162009-10-30 16:36:56 -07001490 String uri = null;
1491 try {
1492 uri = a.getString(R.styleable.Favorite_uri);
1493 intent = Intent.parseUri(uri, 0);
1494 } catch (URISyntaxException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001495 Log.w(TAG, "Shortcut has malformed uri: " + uri);
Adam Cohen228da5a2011-07-27 22:23:47 -07001496 return -1; // Oh well
Mike Cleronb87bd162009-10-30 16:36:56 -07001497 }
1498
1499 if (iconResId == 0 || titleResId == 0) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001500 Log.w(TAG, "Shortcut is missing title or icon resource ID");
Adam Cohen228da5a2011-07-27 22:23:47 -07001501 return -1;
Mike Cleronb87bd162009-10-30 16:36:56 -07001502 }
1503
Adam Cohendcd297f2013-06-18 13:13:40 -07001504 long id = generateNewItemId();
Mike Cleronb87bd162009-10-30 16:36:56 -07001505 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1506 values.put(Favorites.INTENT, intent.toUri(0));
1507 values.put(Favorites.TITLE, r.getString(titleResId));
1508 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_SHORTCUT);
1509 values.put(Favorites.SPANX, 1);
1510 values.put(Favorites.SPANY, 1);
1511 values.put(Favorites.ICON_TYPE, Favorites.ICON_TYPE_RESOURCE);
1512 values.put(Favorites.ICON_PACKAGE, mContext.getPackageName());
1513 values.put(Favorites.ICON_RESOURCE, r.getResourceName(iconResId));
Adam Cohen228da5a2011-07-27 22:23:47 -07001514 values.put(Favorites._ID, id);
Mike Cleronb87bd162009-10-30 16:36:56 -07001515
Adam Cohen228da5a2011-07-27 22:23:47 -07001516 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values) < 0) {
1517 return -1;
1518 }
1519 return id;
Mike Cleronb87bd162009-10-30 16:36:56 -07001520 }
Dan Sandlerd5024042014-01-09 15:01:33 -05001521
1522 public void migrateLauncher2Shortcuts(SQLiteDatabase db, Uri uri) {
1523 final ContentResolver resolver = mContext.getContentResolver();
1524 Cursor c = null;
1525 int count = 0;
1526 int curScreen = 0;
1527
1528 try {
1529 c = resolver.query(uri, null, null, null, "title ASC");
1530 } catch (Exception e) {
1531 // Ignore
1532 }
1533
Dan Sandlerd5024042014-01-09 15:01:33 -05001534 // We already have a favorites database in the old provider
1535 if (c != null) {
1536 try {
1537 if (c.getCount() > 0) {
1538 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1539 final int intentIndex
1540 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
1541 final int titleIndex
1542 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
1543 final int iconTypeIndex
1544 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
1545 final int iconIndex
1546 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1547 final int iconPackageIndex
1548 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
1549 final int iconResourceIndex
1550 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
1551 final int containerIndex
1552 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
1553 final int itemTypeIndex
1554 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
1555 final int screenIndex
1556 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
1557 final int cellXIndex
1558 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
1559 final int cellYIndex
1560 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
1561 final int uriIndex
1562 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1563 final int displayModeIndex
1564 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
1565
1566 int i = 0;
1567 int curX = 0;
1568 int curY = 0;
1569
1570 final LauncherAppState app = LauncherAppState.getInstance();
1571 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1572 final int width = (int) grid.numColumns;
1573 final int height = (int) grid.numRows;
1574 final int hotseatWidth = (int) grid.numHotseatIcons;
Adam Cohen556f6132014-01-15 15:18:08 -08001575 PackageManager pm = mContext.getPackageManager();
Dan Sandlerd5024042014-01-09 15:01:33 -05001576
1577 final HashSet<String> seenIntents = new HashSet<String>(c.getCount());
1578
Adam Cohen72960972014-01-15 18:13:55 -08001579 final ArrayList<ContentValues> shortcuts = new ArrayList<ContentValues>();
1580 final ArrayList<ContentValues> folders = new ArrayList<ContentValues>();
Dan Sandlerd5024042014-01-09 15:01:33 -05001581
1582 while (c.moveToNext()) {
1583 final int itemType = c.getInt(itemTypeIndex);
1584 if (itemType != Favorites.ITEM_TYPE_APPLICATION
1585 && itemType != Favorites.ITEM_TYPE_SHORTCUT
1586 && itemType != Favorites.ITEM_TYPE_FOLDER) {
1587 continue;
1588 }
1589
1590 final int cellX = c.getInt(cellXIndex);
1591 final int cellY = c.getInt(cellYIndex);
1592 final int screen = c.getInt(screenIndex);
1593 int container = c.getInt(containerIndex);
1594 final String intentStr = c.getString(intentIndex);
1595 Launcher.addDumpLog(TAG, "migrating \""
1596 + c.getString(titleIndex) + "\": " + intentStr, true);
1597
1598 if (itemType != Favorites.ITEM_TYPE_FOLDER) {
Adam Cohen556f6132014-01-15 15:18:08 -08001599
1600 final Intent intent;
1601 final ComponentName cn;
1602 try {
1603 intent = Intent.parseUri(intentStr, 0);
1604 } catch (URISyntaxException e) {
1605 // bogus intent?
1606 Launcher.addDumpLog(TAG,
1607 "skipping invalid intent uri", true);
1608 continue;
1609 }
1610
1611 cn = intent.getComponent();
Dan Sandlerd5024042014-01-09 15:01:33 -05001612 if (TextUtils.isEmpty(intentStr)) {
1613 // no intent? no icon
1614 Launcher.addDumpLog(TAG, "skipping empty intent", true);
1615 continue;
Adam Cohen72960972014-01-15 18:13:55 -08001616 } else if (cn != null &&
1617 !LauncherModel.isValidPackageComponent(pm, cn)) {
Adam Cohen556f6132014-01-15 15:18:08 -08001618 // component no longer exists.
Adam Cohen72960972014-01-15 18:13:55 -08001619 Launcher.addDumpLog(TAG, "skipping item whose component " +
Adam Cohen556f6132014-01-15 15:18:08 -08001620 "no longer exists.", true);
1621 continue;
Adam Cohen72960972014-01-15 18:13:55 -08001622 } else if (container ==
1623 LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1624 // Dedupe icons directly on the workspace
1625
Adam Cohen556f6132014-01-15 15:18:08 -08001626 // Canonicalize
1627 // the Play Store sets the package parameter, but Launcher
1628 // does not, so we clear that out to keep them the same
1629 intent.setPackage(null);
1630 final String key = intent.toUri(0);
1631 if (seenIntents.contains(key)) {
1632 Launcher.addDumpLog(TAG, "skipping duplicate", true);
Dan Sandlerd5024042014-01-09 15:01:33 -05001633 continue;
Adam Cohen556f6132014-01-15 15:18:08 -08001634 } else {
1635 seenIntents.add(key);
Dan Sandlerd5024042014-01-09 15:01:33 -05001636 }
1637 }
1638 }
1639
1640 ContentValues values = new ContentValues(c.getColumnCount());
1641 values.put(LauncherSettings.Favorites._ID, c.getInt(idIndex));
1642 values.put(LauncherSettings.Favorites.INTENT, intentStr);
1643 values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex));
1644 values.put(LauncherSettings.Favorites.ICON_TYPE,
1645 c.getInt(iconTypeIndex));
1646 values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex));
1647 values.put(LauncherSettings.Favorites.ICON_PACKAGE,
1648 c.getString(iconPackageIndex));
1649 values.put(LauncherSettings.Favorites.ICON_RESOURCE,
1650 c.getString(iconResourceIndex));
1651 values.put(LauncherSettings.Favorites.ITEM_TYPE, itemType);
1652 values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1);
1653 values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex));
1654 values.put(LauncherSettings.Favorites.DISPLAY_MODE,
1655 c.getInt(displayModeIndex));
1656
1657 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT
Adam Cohen72960972014-01-15 18:13:55 -08001658 && (screen >= hotseatWidth ||
1659 screen == grid.hotseatAllAppsRank)) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001660 // no room for you in the hotseat? it's off to the desktop with you
1661 container = Favorites.CONTAINER_DESKTOP;
1662 }
1663
1664 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1665 // In a folder or in the hotseat, preserve position
1666 values.put(LauncherSettings.Favorites.SCREEN, screen);
1667 values.put(LauncherSettings.Favorites.CELLX, cellX);
1668 values.put(LauncherSettings.Favorites.CELLY, cellY);
1669 } else {
Adam Cohen72960972014-01-15 18:13:55 -08001670 // For items contained directly on one of the workspace screen,
1671 // we'll determine their location (screen, x, y) in a second pass.
Dan Sandlerd5024042014-01-09 15:01:33 -05001672 }
1673
1674 values.put(LauncherSettings.Favorites.CONTAINER, container);
1675
Adam Cohen72960972014-01-15 18:13:55 -08001676 if (itemType != Favorites.ITEM_TYPE_FOLDER) {
1677 shortcuts.add(values);
1678 } else {
1679 folders.add(values);
1680 }
Dan Sandlerd5024042014-01-09 15:01:33 -05001681 }
1682
Adam Cohen72960972014-01-15 18:13:55 -08001683 final ArrayList<ContentValues> allItems = new ArrayList<ContentValues>();
1684 // Folders first
1685 allItems.addAll(folders);
1686 // Then shortcuts
1687 allItems.addAll(shortcuts);
1688
1689 // Layout all the folders
1690 for (ContentValues values: allItems) {
1691 if (values.getAsInteger(LauncherSettings.Favorites.CONTAINER) !=
1692 LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1693 // Hotseat items and folder items have already had their
1694 // location information set. Nothing to be done here.
1695 continue;
1696 }
1697 values.put(LauncherSettings.Favorites.SCREEN, curScreen);
1698 values.put(LauncherSettings.Favorites.CELLX, curX);
1699 values.put(LauncherSettings.Favorites.CELLY, curY);
1700 curX = (curX + 1) % width;
1701 if (curX == 0) {
1702 curY = (curY + 1);
1703 }
1704 // Leave the last row of icons blank on every screen
1705 if (curY == height - 1) {
1706 curScreen = (int) generateNewScreenId();
1707 curY = 0;
1708 }
1709 }
1710
1711 if (allItems.size() > 0) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001712 db.beginTransaction();
1713 try {
Adam Cohen72960972014-01-15 18:13:55 -08001714 for (ContentValues row: allItems) {
1715 if (row == null) continue;
1716 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, row)
Dan Sandlerd5024042014-01-09 15:01:33 -05001717 < 0) {
1718 return;
1719 } else {
1720 count++;
1721 }
1722 }
1723 db.setTransactionSuccessful();
1724 } finally {
1725 db.endTransaction();
1726 }
1727 }
1728
1729 db.beginTransaction();
1730 try {
1731 for (i=0; i<=curScreen; i++) {
1732 final ContentValues values = new ContentValues();
1733 values.put(LauncherSettings.WorkspaceScreens._ID, i);
1734 values.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
1735 if (dbInsertAndCheck(this, db, TABLE_WORKSPACE_SCREENS, null, values)
1736 < 0) {
1737 return;
1738 }
1739 }
1740 db.setTransactionSuccessful();
1741 } finally {
1742 db.endTransaction();
1743 }
1744 }
1745 } finally {
1746 c.close();
1747 }
1748 }
1749
1750 Launcher.addDumpLog(TAG, "migrated " + count + " icons from Launcher2 into "
1751 + (curScreen+1) + " screens", true);
1752
1753 // ensure that new screens are created to hold these icons
1754 setFlagJustLoadedOldDb();
1755
1756 // Update max IDs; very important since we just grabbed IDs from another database
1757 mMaxItemId = initializeMaxItemId(db);
1758 mMaxScreenId = initializeMaxScreenId(db);
1759 if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId + " mMaxScreenId: " + mMaxScreenId);
1760 }
Mike Cleronb87bd162009-10-30 16:36:56 -07001761 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001762
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001763 /**
1764 * Build a query string that will match any row where the column matches
1765 * anything in the values list.
1766 */
1767 static String buildOrWhereString(String column, int[] values) {
1768 StringBuilder selectWhere = new StringBuilder();
1769 for (int i = values.length - 1; i >= 0; i--) {
1770 selectWhere.append(column).append("=").append(values[i]);
1771 if (i > 0) {
1772 selectWhere.append(" OR ");
1773 }
1774 }
1775 return selectWhere.toString();
1776 }
1777
1778 static class SqlArguments {
1779 public final String table;
1780 public final String where;
1781 public final String[] args;
1782
1783 SqlArguments(Uri url, String where, String[] args) {
1784 if (url.getPathSegments().size() == 1) {
1785 this.table = url.getPathSegments().get(0);
1786 this.where = where;
1787 this.args = args;
1788 } else if (url.getPathSegments().size() != 2) {
1789 throw new IllegalArgumentException("Invalid URI: " + url);
1790 } else if (!TextUtils.isEmpty(where)) {
1791 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
1792 } else {
1793 this.table = url.getPathSegments().get(0);
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001794 this.where = "_id=" + ContentUris.parseId(url);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001795 this.args = null;
1796 }
1797 }
1798
1799 SqlArguments(Uri url) {
1800 if (url.getPathSegments().size() == 1) {
1801 table = url.getPathSegments().get(0);
1802 where = null;
1803 args = null;
1804 } else {
1805 throw new IllegalArgumentException("Invalid URI: " + url);
1806 }
1807 }
1808 }
Adam Cohen72960972014-01-15 18:13:55 -08001809}