blob: a080dd8ca94139dbf411c13bfee733d40125a7ab [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;
Dan Sandlerab5fa3a2014-03-06 23:48:04 -050053import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.util.Xml;
Adam Cohen228da5a2011-07-27 22:23:47 -070055
Daniel Sandler325dc232013-06-05 22:57:57 -040056import com.android.launcher3.LauncherSettings.Favorites;
Chris Wrene523e702013-10-09 10:36:55 -040057import com.android.launcher3.config.ProviderConfig;
Michael Jurka8b805b12012-04-18 14:23:14 -070058
59import org.xmlpull.v1.XmlPullParser;
60import org.xmlpull.v1.XmlPullParserException;
61
Dan Sandlerd5024042014-01-09 15:01:33 -050062import java.io.File;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import java.io.IOException;
Mike Cleronb87bd162009-10-30 16:36:56 -070064import java.net.URISyntaxException;
Adam Cohen228da5a2011-07-27 22:23:47 -070065import java.util.ArrayList;
Dan Sandlerd5024042014-01-09 15:01:33 -050066import java.util.HashSet;
Bjorn Bringertcd8fec02010-01-14 13:26:43 +000067import java.util.List;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069public class LauncherProvider extends ContentProvider {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080070 private static final String TAG = "Launcher.LauncherProvider";
71 private static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072
73 private static final String DATABASE_NAME = "launcher.db";
Winson Chung3d503fb2011-07-13 17:25:49 -070074
Adam Cohen71e03b92014-02-21 14:09:53 -080075 private static final int DATABASE_VERSION = 17;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076
Adam Cohene25af792013-06-06 23:08:25 -070077 static final String OLD_AUTHORITY = "com.android.launcher2.settings";
Chris Wrene523e702013-10-09 10:36:55 -040078 static final String AUTHORITY = ProviderConfig.AUTHORITY;
Winson Chung3d503fb2011-07-13 17:25:49 -070079
Dan Sandlerf0b8dac2013-11-19 12:21:25 -050080 // Should we attempt to load anything from the com.android.launcher2 provider?
Dan Sandlerd5024042014-01-09 15:01:33 -050081 static final boolean IMPORT_LAUNCHER2_DATABASE = false;
Dan Sandlerf0b8dac2013-11-19 12:21:25 -050082
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083 static final String TABLE_FAVORITES = "favorites";
Adam Cohendcd297f2013-06-18 13:13:40 -070084 static final String TABLE_WORKSPACE_SCREENS = "workspaceScreens";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085 static final String PARAMETER_NOTIFY = "notify";
Winson Chungc763c4e2013-07-19 13:49:06 -070086 static final String UPGRADED_FROM_OLD_DATABASE =
87 "UPGRADED_FROM_OLD_DATABASE";
88 static final String EMPTY_DATABASE_CREATED =
89 "EMPTY_DATABASE_CREATED";
Michael Jurka45355c42012-10-08 13:21:35 +020090 static final String DEFAULT_WORKSPACE_RESOURCE_ID =
91 "DEFAULT_WORKSPACE_RESOURCE_ID";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092
Winson Chungb3302ae2012-05-01 10:19:14 -070093 private static final String ACTION_APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE =
Adam Cohene25af792013-06-06 23:08:25 -070094 "com.android.launcher.action.APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE";
Winson Chungb3302ae2012-05-01 10:19:14 -070095
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -070096 /**
Romain Guy73b979d2009-06-09 12:57:21 -070097 * {@link Uri} triggered at any registered {@link android.database.ContentObserver} when
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -070098 * {@link AppWidgetHost#deleteHost()} is called during database creation.
99 * Use this to recall {@link AppWidgetHost#startListening()} if needed.
100 */
101 static final Uri CONTENT_APPWIDGET_RESET_URI =
102 Uri.parse("content://" + AUTHORITY + "/appWidgetReset");
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700103
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700104 private DatabaseHelper mOpenHelper;
Winson Chungc763c4e2013-07-19 13:49:06 -0700105 private static boolean sJustLoadedFromOldDb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106
107 @Override
108 public boolean onCreate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400109 final Context context = getContext();
110 mOpenHelper = new DatabaseHelper(context);
111 LauncherAppState.setLauncherProvider(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112 return true;
113 }
114
Winson Chung0b560dd2014-01-21 13:00:26 -0800115 public boolean wasNewDbCreated() {
116 return mOpenHelper.wasNewDbCreated();
117 }
118
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119 @Override
120 public String getType(Uri uri) {
121 SqlArguments args = new SqlArguments(uri, null, null);
122 if (TextUtils.isEmpty(args.where)) {
123 return "vnd.android.cursor.dir/" + args.table;
124 } else {
125 return "vnd.android.cursor.item/" + args.table;
126 }
127 }
128
129 @Override
130 public Cursor query(Uri uri, String[] projection, String selection,
131 String[] selectionArgs, String sortOrder) {
132
133 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
134 SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
135 qb.setTables(args.table);
136
Romain Guy73b979d2009-06-09 12:57:21 -0700137 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138 Cursor result = qb.query(db, projection, args.where, args.args, null, null, sortOrder);
139 result.setNotificationUri(getContext().getContentResolver(), uri);
140
141 return result;
142 }
143
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700144 private static long dbInsertAndCheck(DatabaseHelper helper,
145 SQLiteDatabase db, String table, String nullColumnHack, ContentValues values) {
Dan Sandlerd5024042014-01-09 15:01:33 -0500146 if (values == null) {
147 throw new RuntimeException("Error: attempting to insert null values");
148 }
Chris Wren5dee7af2013-12-20 17:22:11 -0500149 if (!values.containsKey(LauncherSettings.BaseLauncherColumns._ID)) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700150 throw new RuntimeException("Error: attempting to add item without specifying an id");
151 }
Chris Wren5dee7af2013-12-20 17:22:11 -0500152 helper.checkId(table, values);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700153 return db.insert(table, nullColumnHack, values);
154 }
155
Adam Cohen228da5a2011-07-27 22:23:47 -0700156 private static void deleteId(SQLiteDatabase db, long id) {
157 Uri uri = LauncherSettings.Favorites.getContentUri(id, false);
158 SqlArguments args = new SqlArguments(uri, null, null);
159 db.delete(args.table, args.where, args.args);
160 }
161
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 @Override
163 public Uri insert(Uri uri, ContentValues initialValues) {
164 SqlArguments args = new SqlArguments(uri);
165
166 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
Chris Wren1ada10d2013-09-13 18:01:38 -0400167 addModifiedTime(initialValues);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700168 final long rowId = dbInsertAndCheck(mOpenHelper, db, args.table, null, initialValues);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 if (rowId <= 0) return null;
170
171 uri = ContentUris.withAppendedId(uri, rowId);
172 sendNotify(uri);
173
174 return uri;
175 }
176
177 @Override
178 public int bulkInsert(Uri uri, ContentValues[] values) {
179 SqlArguments args = new SqlArguments(uri);
180
181 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
182 db.beginTransaction();
183 try {
184 int numValues = values.length;
185 for (int i = 0; i < numValues; i++) {
Chris Wren1ada10d2013-09-13 18:01:38 -0400186 addModifiedTime(values[i]);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700187 if (dbInsertAndCheck(mOpenHelper, db, args.table, null, values[i]) < 0) {
188 return 0;
189 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 }
191 db.setTransactionSuccessful();
192 } finally {
193 db.endTransaction();
194 }
195
196 sendNotify(uri);
197 return values.length;
198 }
199
200 @Override
Yura085c8532014-02-11 15:15:29 +0000201 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
202 throws OperationApplicationException {
203 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
204 db.beginTransaction();
205 try {
206 ContentProviderResult[] result = super.applyBatch(operations);
207 db.setTransactionSuccessful();
208 return result;
209 } finally {
210 db.endTransaction();
211 }
212 }
213
214 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215 public int delete(Uri uri, String selection, String[] selectionArgs) {
216 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
217
218 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
219 int count = db.delete(args.table, args.where, args.args);
220 if (count > 0) sendNotify(uri);
221
222 return count;
223 }
224
225 @Override
226 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
227 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
228
Chris Wren1ada10d2013-09-13 18:01:38 -0400229 addModifiedTime(values);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800230 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
231 int count = db.update(args.table, values, args.where, args.args);
232 if (count > 0) sendNotify(uri);
233
234 return count;
235 }
236
237 private void sendNotify(Uri uri) {
238 String notify = uri.getQueryParameter(PARAMETER_NOTIFY);
239 if (notify == null || "true".equals(notify)) {
240 getContext().getContentResolver().notifyChange(uri, null);
241 }
Chris Wren1ada10d2013-09-13 18:01:38 -0400242
243 // always notify the backup agent
Chris Wren92aa4232013-10-04 11:29:36 -0400244 LauncherBackupAgentHelper.dataChanged(getContext());
Chris Wren1ada10d2013-09-13 18:01:38 -0400245 }
246
247 private void addModifiedTime(ContentValues values) {
248 values.put(LauncherSettings.ChangeLogColumns.MODIFIED, System.currentTimeMillis());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 }
250
Adam Cohendcd297f2013-06-18 13:13:40 -0700251 public long generateNewItemId() {
252 return mOpenHelper.generateNewItemId();
253 }
254
Winson Chungc763c4e2013-07-19 13:49:06 -0700255 public void updateMaxItemId(long id) {
256 mOpenHelper.updateMaxItemId(id);
257 }
258
Adam Cohendcd297f2013-06-18 13:13:40 -0700259 public long generateNewScreenId() {
260 return mOpenHelper.generateNewScreenId();
261 }
262
263 // This is only required one time while loading the workspace during the
264 // upgrade path, and should never be called from anywhere else.
265 public void updateMaxScreenId(long maxScreenId) {
266 mOpenHelper.updateMaxScreenId(maxScreenId);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700267 }
268
Brian Muramatsu5524b492012-10-02 16:55:54 -0700269 /**
Adam Cohene25af792013-06-06 23:08:25 -0700270 * @param Should we load the old db for upgrade? first run only.
271 */
Winson Chungc763c4e2013-07-19 13:49:06 -0700272 synchronized public boolean justLoadedOldDb() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400273 String spKey = LauncherAppState.getSharedPreferencesKey();
Adam Cohene25af792013-06-06 23:08:25 -0700274 SharedPreferences sp = getContext().getSharedPreferences(spKey, Context.MODE_PRIVATE);
275
Winson Chungc763c4e2013-07-19 13:49:06 -0700276 boolean loadedOldDb = false || sJustLoadedFromOldDb;
Adam Cohendcd297f2013-06-18 13:13:40 -0700277
Winson Chungc763c4e2013-07-19 13:49:06 -0700278 sJustLoadedFromOldDb = false;
279 if (sp.getBoolean(UPGRADED_FROM_OLD_DATABASE, false)) {
Adam Cohene25af792013-06-06 23:08:25 -0700280
281 SharedPreferences.Editor editor = sp.edit();
Winson Chungc763c4e2013-07-19 13:49:06 -0700282 editor.remove(UPGRADED_FROM_OLD_DATABASE);
Adam Cohene25af792013-06-06 23:08:25 -0700283 editor.commit();
Winson Chungc763c4e2013-07-19 13:49:06 -0700284 loadedOldDb = true;
Adam Cohene25af792013-06-06 23:08:25 -0700285 }
Winson Chungc763c4e2013-07-19 13:49:06 -0700286 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -0700287 }
288
289 /**
Brian Muramatsu5524b492012-10-02 16:55:54 -0700290 * @param workspaceResId that can be 0 to use default or non-zero for specific resource
291 */
Michael Jurka45355c42012-10-08 13:21:35 +0200292 synchronized public void loadDefaultFavoritesIfNecessary(int origWorkspaceResId) {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400293 String spKey = LauncherAppState.getSharedPreferencesKey();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700294 SharedPreferences sp = getContext().getSharedPreferences(spKey, Context.MODE_PRIVATE);
Adam Cohene25af792013-06-06 23:08:25 -0700295
Winson Chungc763c4e2013-07-19 13:49:06 -0700296 if (sp.getBoolean(EMPTY_DATABASE_CREATED, false)) {
Chris Wren5dee7af2013-12-20 17:22:11 -0500297 Log.d(TAG, "loading default workspace");
Michael Jurka45355c42012-10-08 13:21:35 +0200298 int workspaceResId = origWorkspaceResId;
299
Brian Muramatsu5524b492012-10-02 16:55:54 -0700300 // Use default workspace resource if none provided
301 if (workspaceResId == 0) {
Nilesh Agrawalda41ea62013-12-09 14:17:49 -0800302 workspaceResId =
303 sp.getInt(DEFAULT_WORKSPACE_RESOURCE_ID, getDefaultWorkspaceResourceId());
Brian Muramatsu5524b492012-10-02 16:55:54 -0700304 }
305
Michael Jurkab85f8a42012-04-25 15:48:32 -0700306 // Populate favorites table with initial favorites
307 SharedPreferences.Editor editor = sp.edit();
Winson Chungc763c4e2013-07-19 13:49:06 -0700308 editor.remove(EMPTY_DATABASE_CREATED);
Michael Jurka45355c42012-10-08 13:21:35 +0200309 if (origWorkspaceResId != 0) {
310 editor.putInt(DEFAULT_WORKSPACE_RESOURCE_ID, origWorkspaceResId);
311 }
Adam Cohene25af792013-06-06 23:08:25 -0700312
Brian Muramatsu5524b492012-10-02 16:55:54 -0700313 mOpenHelper.loadFavorites(mOpenHelper.getWritableDatabase(), workspaceResId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700314 mOpenHelper.setFlagJustLoadedOldDb();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700315 editor.commit();
316 }
317 }
318
Dan Sandlerd5024042014-01-09 15:01:33 -0500319 public void migrateLauncher2Shortcuts() {
320 mOpenHelper.migrateLauncher2Shortcuts(mOpenHelper.getWritableDatabase(),
321 LauncherSettings.Favorites.OLD_CONTENT_URI);
322 }
323
Nilesh Agrawalda41ea62013-12-09 14:17:49 -0800324 private static int getDefaultWorkspaceResourceId() {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800325 if (LauncherAppState.isDisableAllApps()) {
Nilesh Agrawalda41ea62013-12-09 14:17:49 -0800326 return R.xml.default_workspace_no_all_apps;
327 } else {
328 return R.xml.default_workspace;
329 }
330 }
331
Winson Chungc763c4e2013-07-19 13:49:06 -0700332 private static interface ContentValuesCallback {
333 public void onRow(ContentValues values);
334 }
335
Adam Cohen6dbe0492013-12-02 17:00:14 -0800336 private static boolean shouldImportLauncher2Database(Context context) {
337 boolean isTablet = context.getResources().getBoolean(R.bool.is_tablet);
338
339 // We don't import the old databse for tablets, as the grid size has changed.
340 return !isTablet && IMPORT_LAUNCHER2_DATABASE;
341 }
342
Dan Sandlerd5024042014-01-09 15:01:33 -0500343 public void deleteDatabase() {
344 // Are you sure? (y/n)
345 final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
Dan Sandler2b471742014-01-21 14:14:41 -0500346 final File dbFile = new File(db.getPath());
Dan Sandlerd5024042014-01-09 15:01:33 -0500347 mOpenHelper.close();
Dan Sandler2b471742014-01-21 14:14:41 -0500348 if (dbFile.exists()) {
349 SQLiteDatabase.deleteDatabase(dbFile);
350 }
Dan Sandlerd5024042014-01-09 15:01:33 -0500351 mOpenHelper = new DatabaseHelper(getContext());
352 }
353
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 private static class DatabaseHelper extends SQLiteOpenHelper {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800355 private static final String TAG_FAVORITES = "favorites";
356 private static final String TAG_FAVORITE = "favorite";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700357 private static final String TAG_CLOCK = "clock";
358 private static final String TAG_SEARCH = "search";
Mike Cleronb87bd162009-10-30 16:36:56 -0700359 private static final String TAG_APPWIDGET = "appwidget";
360 private static final String TAG_SHORTCUT = "shortcut";
Adam Cohen228da5a2011-07-27 22:23:47 -0700361 private static final String TAG_FOLDER = "folder";
Winson Chungb3302ae2012-05-01 10:19:14 -0700362 private static final String TAG_EXTRA = "extra";
Daniel Sandler57dac262013-10-03 13:28:36 -0400363 private static final String TAG_INCLUDE = "include";
Winson Chung3d503fb2011-07-13 17:25:49 -0700364
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800365 private final Context mContext;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700366 private final AppWidgetHost mAppWidgetHost;
Adam Cohendcd297f2013-06-18 13:13:40 -0700367 private long mMaxItemId = -1;
368 private long mMaxScreenId = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369
Winson Chung0b560dd2014-01-21 13:00:26 -0800370 private boolean mNewDbCreated = false;
371
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800372 DatabaseHelper(Context context) {
373 super(context, DATABASE_NAME, null, DATABASE_VERSION);
374 mContext = context;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700375 mAppWidgetHost = new AppWidgetHost(context, Launcher.APPWIDGET_HOST_ID);
Winson Chung3d503fb2011-07-13 17:25:49 -0700376
377 // In the case where neither onCreate nor onUpgrade gets called, we read the maxId from
378 // the DB here
Adam Cohendcd297f2013-06-18 13:13:40 -0700379 if (mMaxItemId == -1) {
380 mMaxItemId = initializeMaxItemId(getWritableDatabase());
381 }
382 if (mMaxScreenId == -1) {
383 mMaxScreenId = initializeMaxScreenId(getWritableDatabase());
Winson Chung3d503fb2011-07-13 17:25:49 -0700384 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 }
386
Winson Chung0b560dd2014-01-21 13:00:26 -0800387 public boolean wasNewDbCreated() {
388 return mNewDbCreated;
389 }
390
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700391 /**
392 * Send notification that we've deleted the {@link AppWidgetHost},
393 * probably as part of the initial database creation. The receiver may
394 * want to re-call {@link AppWidgetHost#startListening()} to ensure
395 * callbacks are correctly set.
396 */
397 private void sendAppWidgetResetNotify() {
398 final ContentResolver resolver = mContext.getContentResolver();
399 resolver.notifyChange(CONTENT_APPWIDGET_RESET_URI, null);
400 }
401
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800402 @Override
403 public void onCreate(SQLiteDatabase db) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800404 if (LOGD) Log.d(TAG, "creating new launcher database");
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700405
Adam Cohendcd297f2013-06-18 13:13:40 -0700406 mMaxItemId = 1;
407 mMaxScreenId = 0;
Winson Chung0b560dd2014-01-21 13:00:26 -0800408 mNewDbCreated = true;
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700409
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410 db.execSQL("CREATE TABLE favorites (" +
411 "_id INTEGER PRIMARY KEY," +
412 "title TEXT," +
413 "intent TEXT," +
414 "container INTEGER," +
415 "screen INTEGER," +
416 "cellX INTEGER," +
417 "cellY INTEGER," +
418 "spanX INTEGER," +
419 "spanY INTEGER," +
420 "itemType INTEGER," +
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700421 "appWidgetId INTEGER NOT NULL DEFAULT -1," +
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800422 "isShortcut INTEGER," +
423 "iconType INTEGER," +
424 "iconPackage TEXT," +
425 "iconResource TEXT," +
426 "icon BLOB," +
427 "uri TEXT," +
Chris Wrend5e66bf2013-09-16 14:02:29 -0400428 "displayMode INTEGER," +
Chris Wren1ada10d2013-09-13 18:01:38 -0400429 "appWidgetProvider TEXT," +
Chris Wrenf4d08112014-01-16 18:13:56 -0500430 "modified INTEGER NOT NULL DEFAULT 0," +
431 "restored INTEGER NOT NULL DEFAULT 0" +
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800432 ");");
Adam Cohendcd297f2013-06-18 13:13:40 -0700433 addWorkspacesTable(db);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800434
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700435 // Database was just created, so wipe any previous widgets
436 if (mAppWidgetHost != null) {
437 mAppWidgetHost.deleteHost();
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700438 sendAppWidgetResetNotify();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800439 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700440
Adam Cohen6dbe0492013-12-02 17:00:14 -0800441 if (shouldImportLauncher2Database(mContext)) {
Dan Sandlerf0b8dac2013-11-19 12:21:25 -0500442 // Try converting the old database
443 ContentValuesCallback permuteScreensCb = new ContentValuesCallback() {
444 public void onRow(ContentValues values) {
445 int container = values.getAsInteger(LauncherSettings.Favorites.CONTAINER);
446 if (container == Favorites.CONTAINER_DESKTOP) {
447 int screen = values.getAsInteger(LauncherSettings.Favorites.SCREEN);
448 screen = (int) upgradeLauncherDb_permuteScreens(screen);
449 values.put(LauncherSettings.Favorites.SCREEN, screen);
450 }
451 }
452 };
453 Uri uri = Uri.parse("content://" + Settings.AUTHORITY +
454 "/old_favorites?notify=true");
455 if (!convertDatabase(db, uri, permuteScreensCb, true)) {
456 // Try and upgrade from the Launcher2 db
457 uri = LauncherSettings.Favorites.OLD_CONTENT_URI;
458 if (!convertDatabase(db, uri, permuteScreensCb, false)) {
459 // If we fail, then set a flag to load the default workspace
460 setFlagEmptyDbCreated();
461 return;
Winson Chungc763c4e2013-07-19 13:49:06 -0700462 }
463 }
Dan Sandlerf0b8dac2013-11-19 12:21:25 -0500464 // Right now, in non-default workspace cases, we want to run the final
465 // upgrade code (ie. to fix workspace screen indices -> ids, etc.), so
466 // set that flag too.
467 setFlagJustLoadedOldDb();
468 } else {
469 // Fresh and clean launcher DB.
470 mMaxItemId = initializeMaxItemId(db);
471 setFlagEmptyDbCreated();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800472 }
473 }
474
Adam Cohendcd297f2013-06-18 13:13:40 -0700475 private void addWorkspacesTable(SQLiteDatabase db) {
476 db.execSQL("CREATE TABLE " + TABLE_WORKSPACE_SCREENS + " (" +
477 LauncherSettings.WorkspaceScreens._ID + " INTEGER," +
Chris Wren1ada10d2013-09-13 18:01:38 -0400478 LauncherSettings.WorkspaceScreens.SCREEN_RANK + " INTEGER," +
479 LauncherSettings.ChangeLogColumns.MODIFIED + " INTEGER NOT NULL DEFAULT 0" +
Adam Cohendcd297f2013-06-18 13:13:40 -0700480 ");");
481 }
482
Winson Chungc763c4e2013-07-19 13:49:06 -0700483 private void setFlagJustLoadedOldDb() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400484 String spKey = LauncherAppState.getSharedPreferencesKey();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700485 SharedPreferences sp = mContext.getSharedPreferences(spKey, Context.MODE_PRIVATE);
486 SharedPreferences.Editor editor = sp.edit();
Winson Chungc763c4e2013-07-19 13:49:06 -0700487 editor.putBoolean(UPGRADED_FROM_OLD_DATABASE, true);
488 editor.putBoolean(EMPTY_DATABASE_CREATED, false);
Michael Jurkab85f8a42012-04-25 15:48:32 -0700489 editor.commit();
490 }
491
Winson Chungc763c4e2013-07-19 13:49:06 -0700492 private void setFlagEmptyDbCreated() {
493 String spKey = LauncherAppState.getSharedPreferencesKey();
494 SharedPreferences sp = mContext.getSharedPreferences(spKey, Context.MODE_PRIVATE);
495 SharedPreferences.Editor editor = sp.edit();
496 editor.putBoolean(EMPTY_DATABASE_CREATED, true);
497 editor.putBoolean(UPGRADED_FROM_OLD_DATABASE, false);
498 editor.commit();
499 }
500
501 // We rearrange the screens from the old launcher
502 // 12345 -> 34512
503 private long upgradeLauncherDb_permuteScreens(long screen) {
504 if (screen >= 2) {
505 return screen - 2;
506 } else {
507 return screen + 3;
508 }
509 }
510
511 private boolean convertDatabase(SQLiteDatabase db, Uri uri,
512 ContentValuesCallback cb, boolean deleteRows) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800513 if (LOGD) Log.d(TAG, "converting database from an older format, but not onUpgrade");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800514 boolean converted = false;
515
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800516 final ContentResolver resolver = mContext.getContentResolver();
517 Cursor cursor = null;
518
519 try {
520 cursor = resolver.query(uri, null, null, null, null);
521 } catch (Exception e) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700522 // Ignore
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800523 }
524
525 // We already have a favorites database in the old provider
Winson Chungc763c4e2013-07-19 13:49:06 -0700526 if (cursor != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800527 try {
Winson Chungc763c4e2013-07-19 13:49:06 -0700528 if (cursor.getCount() > 0) {
529 converted = copyFromCursor(db, cursor, cb) > 0;
530 if (converted && deleteRows) {
531 resolver.delete(uri, null, null);
532 }
533 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800534 } finally {
535 cursor.close();
536 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800537 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700538
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800539 if (converted) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700540 // Convert widgets from this import into widgets
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800541 if (LOGD) Log.d(TAG, "converted and now triggering widget upgrade");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800542 convertWidgets(db);
Winson Chungc763c4e2013-07-19 13:49:06 -0700543
544 // Update max item id
545 mMaxItemId = initializeMaxItemId(db);
546 if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800547 }
548
549 return converted;
550 }
551
Winson Chungc763c4e2013-07-19 13:49:06 -0700552 private int copyFromCursor(SQLiteDatabase db, Cursor c, ContentValuesCallback cb) {
Romain Guy73b979d2009-06-09 12:57:21 -0700553 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800554 final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
555 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
556 final int iconTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
557 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
558 final int iconPackageIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
559 final int iconResourceIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
560 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
561 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
562 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
563 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
564 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
565 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
566 final int displayModeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
567
568 ContentValues[] rows = new ContentValues[c.getCount()];
569 int i = 0;
570 while (c.moveToNext()) {
571 ContentValues values = new ContentValues(c.getColumnCount());
Romain Guy73b979d2009-06-09 12:57:21 -0700572 values.put(LauncherSettings.Favorites._ID, c.getLong(idIndex));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800573 values.put(LauncherSettings.Favorites.INTENT, c.getString(intentIndex));
574 values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex));
575 values.put(LauncherSettings.Favorites.ICON_TYPE, c.getInt(iconTypeIndex));
576 values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex));
577 values.put(LauncherSettings.Favorites.ICON_PACKAGE, c.getString(iconPackageIndex));
578 values.put(LauncherSettings.Favorites.ICON_RESOURCE, c.getString(iconResourceIndex));
579 values.put(LauncherSettings.Favorites.CONTAINER, c.getInt(containerIndex));
580 values.put(LauncherSettings.Favorites.ITEM_TYPE, c.getInt(itemTypeIndex));
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700581 values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800582 values.put(LauncherSettings.Favorites.SCREEN, c.getInt(screenIndex));
583 values.put(LauncherSettings.Favorites.CELLX, c.getInt(cellXIndex));
584 values.put(LauncherSettings.Favorites.CELLY, c.getInt(cellYIndex));
585 values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex));
586 values.put(LauncherSettings.Favorites.DISPLAY_MODE, c.getInt(displayModeIndex));
Winson Chungc763c4e2013-07-19 13:49:06 -0700587 if (cb != null) {
588 cb.onRow(values);
589 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800590 rows[i++] = values;
591 }
592
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800593 int total = 0;
Winson Chungc763c4e2013-07-19 13:49:06 -0700594 if (i > 0) {
595 db.beginTransaction();
596 try {
597 int numValues = rows.length;
598 for (i = 0; i < numValues; i++) {
599 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, rows[i]) < 0) {
600 return 0;
601 } else {
602 total++;
603 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800604 }
Winson Chungc763c4e2013-07-19 13:49:06 -0700605 db.setTransactionSuccessful();
606 } finally {
607 db.endTransaction();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800608 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800609 }
610
611 return total;
612 }
613
614 @Override
615 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Winson Chungc763c4e2013-07-19 13:49:06 -0700616 if (LOGD) Log.d(TAG, "onUpgrade triggered: " + oldVersion);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700617
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800618 int version = oldVersion;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700619 if (version < 3) {
620 // upgrade 1,2 -> 3 added appWidgetId column
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800621 db.beginTransaction();
622 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700623 // Insert new column for holding appWidgetIds
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800624 db.execSQL("ALTER TABLE favorites " +
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700625 "ADD COLUMN appWidgetId INTEGER NOT NULL DEFAULT -1;");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 db.setTransactionSuccessful();
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700627 version = 3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800628 } catch (SQLException ex) {
629 // Old version remains, which means we wipe old data
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800630 Log.e(TAG, ex.getMessage(), ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631 } finally {
632 db.endTransaction();
633 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700634
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800635 // Convert existing widgets only if table upgrade was successful
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700636 if (version == 3) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 convertWidgets(db);
638 }
639 }
Romain Guy73b979d2009-06-09 12:57:21 -0700640
641 if (version < 4) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800642 version = 4;
Romain Guy73b979d2009-06-09 12:57:21 -0700643 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700644
Romain Guy509cd6a2010-03-23 15:10:56 -0700645 // Where's version 5?
646 // - Donut and sholes on 2.0 shipped with version 4 of launcher1.
Daniel Sandler325dc232013-06-05 22:57:57 -0400647 // - Passion shipped on 2.1 with version 6 of launcher3
Romain Guy509cd6a2010-03-23 15:10:56 -0700648 // - Sholes shipped on 2.1r1 (aka Mr. 3) with version 5 of launcher 1
649 // but version 5 on there was the updateContactsShortcuts change
650 // which was version 6 in launcher 2 (first shipped on passion 2.1r1).
651 // The updateContactsShortcuts change is idempotent, so running it twice
652 // is okay so we'll do that when upgrading the devices that shipped with it.
653 if (version < 6) {
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800654 // We went from 3 to 5 screens. Move everything 1 to the right
655 db.beginTransaction();
656 try {
657 db.execSQL("UPDATE favorites SET screen=(screen + 1);");
658 db.setTransactionSuccessful();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800659 } catch (SQLException ex) {
660 // Old version remains, which means we wipe old data
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800661 Log.e(TAG, ex.getMessage(), ex);
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800662 } finally {
663 db.endTransaction();
664 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700665
Romain Guy509cd6a2010-03-23 15:10:56 -0700666 // We added the fast track.
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800667 if (updateContactsShortcuts(db)) {
668 version = 6;
669 }
670 }
Bjorn Bringert7984c942009-12-09 15:38:25 +0000671
672 if (version < 7) {
673 // Version 7 gets rid of the special search widget.
674 convertWidgets(db);
675 version = 7;
676 }
677
Joe Onorato0589f0f2010-02-08 13:44:00 -0800678 if (version < 8) {
679 // Version 8 (froyo) has the icons all normalized. This should
680 // already be the case in practice, but we now rely on it and don't
681 // resample the images each time.
682 normalizeIcons(db);
683 version = 8;
684 }
685
Winson Chung3d503fb2011-07-13 17:25:49 -0700686 if (version < 9) {
687 // The max id is not yet set at this point (onUpgrade is triggered in the ctor
688 // 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 -0700689 if (mMaxItemId == -1) {
690 mMaxItemId = initializeMaxItemId(db);
Winson Chung3d503fb2011-07-13 17:25:49 -0700691 }
692
693 // Add default hotseat icons
Winson Chung6d092682011-11-16 18:43:26 -0800694 loadFavorites(db, R.xml.update_workspace);
Winson Chung3d503fb2011-07-13 17:25:49 -0700695 version = 9;
696 }
697
Daniel Lehmannd02402c2012-05-14 18:30:53 -0700698 // We bumped the version three time during JB, once to update the launch flags, once to
699 // update the override for the default launch animation and once to set the mimetype
700 // to improve startup performance
701 if (version < 12) {
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700702 // Contact shortcuts need a different set of flags to be launched now
703 // The updateContactsShortcuts change is idempotent, so we can keep using it like
704 // back in the Donut days
705 updateContactsShortcuts(db);
Daniel Lehmannd02402c2012-05-14 18:30:53 -0700706 version = 12;
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700707 }
708
Adam Cohendcd297f2013-06-18 13:13:40 -0700709 if (version < 13) {
710 // With the new shrink-wrapped and re-orderable workspaces, it makes sense
711 // to persist workspace screens and their relative order.
712 mMaxScreenId = 0;
713
714 // This will never happen in the wild, but when we switch to using workspace
715 // screen ids, redo the import from old launcher.
Winson Chungc763c4e2013-07-19 13:49:06 -0700716 sJustLoadedFromOldDb = true;
Adam Cohendcd297f2013-06-18 13:13:40 -0700717
718 addWorkspacesTable(db);
719 version = 13;
720 }
721
Chris Wrend5e66bf2013-09-16 14:02:29 -0400722 if (version < 14) {
723 db.beginTransaction();
724 try {
725 // Insert new column for holding widget provider name
726 db.execSQL("ALTER TABLE favorites " +
727 "ADD COLUMN appWidgetProvider TEXT;");
728 db.setTransactionSuccessful();
729 version = 14;
730 } catch (SQLException ex) {
731 // Old version remains, which means we wipe old data
732 Log.e(TAG, ex.getMessage(), ex);
733 } finally {
734 db.endTransaction();
735 }
736 }
737
Chris Wren1ada10d2013-09-13 18:01:38 -0400738 if (version < 15) {
739 db.beginTransaction();
740 try {
741 // Insert new column for holding update timestamp
742 db.execSQL("ALTER TABLE favorites " +
743 "ADD COLUMN modified INTEGER NOT NULL DEFAULT 0;");
744 db.execSQL("ALTER TABLE workspaceScreens " +
745 "ADD COLUMN modified INTEGER NOT NULL DEFAULT 0;");
746 db.setTransactionSuccessful();
747 version = 15;
748 } catch (SQLException ex) {
749 // Old version remains, which means we wipe old data
750 Log.e(TAG, ex.getMessage(), ex);
751 } finally {
752 db.endTransaction();
753 }
754 }
755
Chris Wrenf4d08112014-01-16 18:13:56 -0500756
757 if (version < 16) {
758 db.beginTransaction();
759 try {
760 // Insert new column for holding restore status
761 db.execSQL("ALTER TABLE favorites " +
762 "ADD COLUMN restored INTEGER NOT NULL DEFAULT 0;");
763 db.setTransactionSuccessful();
764 version = 16;
765 } catch (SQLException ex) {
766 // Old version remains, which means we wipe old data
767 Log.e(TAG, ex.getMessage(), ex);
768 } finally {
769 db.endTransaction();
770 }
771 }
772
Adam Cohen71e03b92014-02-21 14:09:53 -0800773 if (version < 17) {
774 // We use the db version upgrade here to identify users who may not have seen
775 // clings yet (because they weren't available), but for whom the clings are now
776 // available (tablet users). Because one of the possible cling flows (migration)
777 // is very destructive (wipes out workspaces), we want to prevent this from showing
778 // until clear data. We do so by marking that the clings have been shown.
779 LauncherClings.synchonouslyMarkFirstRunClingDismissed(mContext);
780 version = 17;
781 }
782
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783 if (version != DATABASE_VERSION) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800784 Log.w(TAG, "Destroying all old data.");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800785 db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES);
Adam Cohendcd297f2013-06-18 13:13:40 -0700786 db.execSQL("DROP TABLE IF EXISTS " + TABLE_WORKSPACE_SCREENS);
787
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788 onCreate(db);
789 }
790 }
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800791
792 private boolean updateContactsShortcuts(SQLiteDatabase db) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800793 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE,
794 new int[] { Favorites.ITEM_TYPE_SHORTCUT });
795
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700796 Cursor c = null;
797 final String actionQuickContact = "com.android.contacts.action.QUICK_CONTACT";
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800798 db.beginTransaction();
799 try {
800 // Select and iterate through each matching widget
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700801 c = db.query(TABLE_FAVORITES,
802 new String[] { Favorites._ID, Favorites.INTENT },
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800803 selectWhere, null, null, null, null);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700804 if (c == null) return false;
805
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800806 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700807
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800808 final int idIndex = c.getColumnIndex(Favorites._ID);
809 final int intentIndex = c.getColumnIndex(Favorites.INTENT);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700810
811 while (c.moveToNext()) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800812 long favoriteId = c.getLong(idIndex);
813 final String intentUri = c.getString(intentIndex);
814 if (intentUri != null) {
815 try {
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700816 final Intent intent = Intent.parseUri(intentUri, 0);
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800817 android.util.Log.d("Home", intent.toString());
818 final Uri uri = intent.getData();
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700819 if (uri != null) {
820 final String data = uri.toString();
821 if ((Intent.ACTION_VIEW.equals(intent.getAction()) ||
822 actionQuickContact.equals(intent.getAction())) &&
823 (data.startsWith("content://contacts/people/") ||
824 data.startsWith("content://com.android.contacts/" +
825 "contacts/lookup/"))) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800826
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700827 final Intent newIntent = new Intent(actionQuickContact);
828 // When starting from the launcher, start in a new, cleared task
829 // CLEAR_WHEN_TASK_RESET cannot reset the root of a task, so we
830 // clear the whole thing preemptively here since
831 // QuickContactActivity will finish itself when launching other
832 // detail activities.
833 newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
834 Intent.FLAG_ACTIVITY_CLEAR_TASK);
Winson Chung2672ff92012-05-04 16:22:30 -0700835 newIntent.putExtra(
836 Launcher.INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION, true);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700837 newIntent.setData(uri);
Daniel Lehmannd02402c2012-05-14 18:30:53 -0700838 // Determine the type and also put that in the shortcut
839 // (that can speed up launch a bit)
840 newIntent.setDataAndType(uri, newIntent.resolveType(mContext));
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800841
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700842 final ContentValues values = new ContentValues();
843 values.put(LauncherSettings.Favorites.INTENT,
844 newIntent.toUri(0));
845
846 String updateWhere = Favorites._ID + "=" + favoriteId;
847 db.update(TABLE_FAVORITES, values, updateWhere, null);
848 }
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800849 }
850 } catch (RuntimeException ex) {
851 Log.e(TAG, "Problem upgrading shortcut", ex);
852 } catch (URISyntaxException e) {
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700853 Log.e(TAG, "Problem upgrading shortcut", e);
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800854 }
855 }
856 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700857
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800858 db.setTransactionSuccessful();
859 } catch (SQLException ex) {
860 Log.w(TAG, "Problem while upgrading contacts", ex);
861 return false;
862 } finally {
863 db.endTransaction();
864 if (c != null) {
865 c.close();
866 }
867 }
868
869 return true;
870 }
871
Joe Onorato0589f0f2010-02-08 13:44:00 -0800872 private void normalizeIcons(SQLiteDatabase db) {
873 Log.d(TAG, "normalizing icons");
874
Joe Onorato346e1292010-02-18 10:34:24 -0500875 db.beginTransaction();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800876 Cursor c = null;
Joe Onorato9690b392010-03-23 17:34:37 -0400877 SQLiteStatement update = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800878 try {
879 boolean logged = false;
Joe Onorato9690b392010-03-23 17:34:37 -0400880 update = db.compileStatement("UPDATE favorites "
Jeff Hamiltoneaf77d62010-02-13 00:08:17 -0600881 + "SET icon=? WHERE _id=?");
Joe Onorato0589f0f2010-02-08 13:44:00 -0800882
883 c = db.rawQuery("SELECT _id, icon FROM favorites WHERE iconType=" +
884 Favorites.ICON_TYPE_BITMAP, null);
885
886 final int idIndex = c.getColumnIndexOrThrow(Favorites._ID);
887 final int iconIndex = c.getColumnIndexOrThrow(Favorites.ICON);
888
889 while (c.moveToNext()) {
890 long id = c.getLong(idIndex);
891 byte[] data = c.getBlob(iconIndex);
892 try {
893 Bitmap bitmap = Utilities.resampleIconBitmap(
894 BitmapFactory.decodeByteArray(data, 0, data.length),
895 mContext);
896 if (bitmap != null) {
897 update.bindLong(1, id);
898 data = ItemInfo.flattenBitmap(bitmap);
899 if (data != null) {
900 update.bindBlob(2, data);
901 update.execute();
902 }
903 bitmap.recycle();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800904 }
905 } catch (Exception e) {
906 if (!logged) {
907 Log.e(TAG, "Failed normalizing icon " + id, e);
908 } else {
909 Log.e(TAG, "Also failed normalizing icon " + id);
910 }
911 logged = true;
912 }
913 }
Bjorn Bringert3a928e42010-02-19 11:15:40 +0000914 db.setTransactionSuccessful();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800915 } catch (SQLException ex) {
916 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
917 } finally {
918 db.endTransaction();
Joe Onorato9690b392010-03-23 17:34:37 -0400919 if (update != null) {
920 update.close();
921 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800922 if (c != null) {
923 c.close();
924 }
925 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700926 }
927
928 // Generates a new ID to use for an object in your database. This method should be only
929 // called from the main UI thread. As an exception, we do call it when we call the
930 // constructor from the worker thread; however, this doesn't extend until after the
931 // constructor is called, and we only pass a reference to LauncherProvider to LauncherApp
932 // after that point
Adam Cohendcd297f2013-06-18 13:13:40 -0700933 public long generateNewItemId() {
934 if (mMaxItemId < 0) {
935 throw new RuntimeException("Error: max item id was not initialized");
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700936 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700937 mMaxItemId += 1;
938 return mMaxItemId;
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700939 }
940
Winson Chungc763c4e2013-07-19 13:49:06 -0700941 public void updateMaxItemId(long id) {
942 mMaxItemId = id + 1;
943 }
944
Chris Wren5dee7af2013-12-20 17:22:11 -0500945 public void checkId(String table, ContentValues values) {
946 long id = values.getAsLong(LauncherSettings.BaseLauncherColumns._ID);
947 if (table == LauncherProvider.TABLE_WORKSPACE_SCREENS) {
948 mMaxScreenId = Math.max(id, mMaxScreenId);
949 } else {
950 mMaxItemId = Math.max(id, mMaxItemId);
951 }
952 }
953
Adam Cohendcd297f2013-06-18 13:13:40 -0700954 private long initializeMaxItemId(SQLiteDatabase db) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700955 Cursor c = db.rawQuery("SELECT MAX(_id) FROM favorites", null);
956
957 // get the result
958 final int maxIdIndex = 0;
959 long id = -1;
960 if (c != null && c.moveToNext()) {
961 id = c.getLong(maxIdIndex);
962 }
Michael Jurka5130e402011-10-13 04:55:35 -0700963 if (c != null) {
964 c.close();
965 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700966
967 if (id == -1) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700968 throw new RuntimeException("Error: could not query max item id");
969 }
970
971 return id;
972 }
973
974 // Generates a new ID to use for an workspace screen in your database. This method
975 // should be only called from the main UI thread. As an exception, we do call it when we
976 // call the constructor from the worker thread; however, this doesn't extend until after the
977 // constructor is called, and we only pass a reference to LauncherProvider to LauncherApp
978 // after that point
979 public long generateNewScreenId() {
980 if (mMaxScreenId < 0) {
981 throw new RuntimeException("Error: max screen id was not initialized");
982 }
983 mMaxScreenId += 1;
Winson Chunga90303b2013-11-15 13:05:06 -0800984 // Log to disk
985 Launcher.addDumpLog(TAG, "11683562 - generateNewScreenId(): " + mMaxScreenId, true);
Adam Cohendcd297f2013-06-18 13:13:40 -0700986 return mMaxScreenId;
987 }
988
989 public void updateMaxScreenId(long maxScreenId) {
Winson Chunga90303b2013-11-15 13:05:06 -0800990 // Log to disk
991 Launcher.addDumpLog(TAG, "11683562 - updateMaxScreenId(): " + maxScreenId, true);
Adam Cohendcd297f2013-06-18 13:13:40 -0700992 mMaxScreenId = maxScreenId;
993 }
994
995 private long initializeMaxScreenId(SQLiteDatabase db) {
996 Cursor c = db.rawQuery("SELECT MAX(" + LauncherSettings.WorkspaceScreens._ID + ") FROM " + TABLE_WORKSPACE_SCREENS, null);
997
998 // get the result
999 final int maxIdIndex = 0;
1000 long id = -1;
1001 if (c != null && c.moveToNext()) {
1002 id = c.getLong(maxIdIndex);
1003 }
1004 if (c != null) {
1005 c.close();
1006 }
1007
1008 if (id == -1) {
1009 throw new RuntimeException("Error: could not query max screen id");
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001010 }
1011
Winson Chunga90303b2013-11-15 13:05:06 -08001012 // Log to disk
1013 Launcher.addDumpLog(TAG, "11683562 - initializeMaxScreenId(): " + id, true);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001014 return id;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001015 }
1016
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001017 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001018 * Upgrade existing clock and photo frame widgets into their new widget
Bjorn Bringert93c45762009-12-16 13:19:47 +00001019 * equivalents.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001020 */
1021 private void convertWidgets(SQLiteDatabase db) {
Bjorn Bringert34251342009-12-15 13:33:11 +00001022 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023 final int[] bindSources = new int[] {
1024 Favorites.ITEM_TYPE_WIDGET_CLOCK,
1025 Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME,
Bjorn Bringert7984c942009-12-09 15:38:25 +00001026 Favorites.ITEM_TYPE_WIDGET_SEARCH,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001027 };
Bjorn Bringert7984c942009-12-09 15:38:25 +00001028
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001029 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE, bindSources);
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001030
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001031 Cursor c = null;
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001032
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001033 db.beginTransaction();
1034 try {
1035 // Select and iterate through each matching widget
Bjorn Bringert7984c942009-12-09 15:38:25 +00001036 c = db.query(TABLE_FAVORITES, new String[] { Favorites._ID, Favorites.ITEM_TYPE },
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037 selectWhere, null, null, null, null);
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001038
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001039 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001040
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041 final ContentValues values = new ContentValues();
1042 while (c != null && c.moveToNext()) {
1043 long favoriteId = c.getLong(0);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001044 int favoriteType = c.getInt(1);
1045
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001046 // Allocate and update database with new appWidgetId
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001048 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001049
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001050 if (LOGD) {
1051 Log.d(TAG, "allocated appWidgetId=" + appWidgetId
1052 + " for favoriteId=" + favoriteId);
1053 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001054 values.clear();
Bjorn Bringert7984c942009-12-09 15:38:25 +00001055 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
1056 values.put(Favorites.APPWIDGET_ID, appWidgetId);
1057
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001058 // Original widgets might not have valid spans when upgrading
Bjorn Bringert7984c942009-12-09 15:38:25 +00001059 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
1060 values.put(LauncherSettings.Favorites.SPANX, 4);
1061 values.put(LauncherSettings.Favorites.SPANY, 1);
1062 } else {
1063 values.put(LauncherSettings.Favorites.SPANX, 2);
1064 values.put(LauncherSettings.Favorites.SPANY, 2);
1065 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066
1067 String updateWhere = Favorites._ID + "=" + favoriteId;
1068 db.update(TABLE_FAVORITES, values, updateWhere, null);
Bjorn Bringert34251342009-12-15 13:33:11 +00001069
Bjorn Bringert34251342009-12-15 13:33:11 +00001070 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_CLOCK) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001071 // TODO: check return value
1072 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
Bjorn Bringert34251342009-12-15 13:33:11 +00001073 new ComponentName("com.android.alarmclock",
1074 "com.android.alarmclock.AnalogAppWidgetProvider"));
1075 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001076 // TODO: check return value
1077 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
Bjorn Bringert34251342009-12-15 13:33:11 +00001078 new ComponentName("com.android.camera",
1079 "com.android.camera.PhotoAppWidgetProvider"));
1080 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001081 // TODO: check return value
1082 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
Bjorn Bringertcd8fec02010-01-14 13:26:43 +00001083 getSearchWidgetProvider());
Bjorn Bringert34251342009-12-15 13:33:11 +00001084 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001086 Log.e(TAG, "Problem allocating appWidgetId", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087 }
1088 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001089
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 db.setTransactionSuccessful();
1091 } catch (SQLException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001092 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 } finally {
1094 db.endTransaction();
1095 if (c != null) {
1096 c.close();
1097 }
1098 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001099
1100 // Update max item id
1101 mMaxItemId = initializeMaxItemId(db);
1102 if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 }
1104
Michael Jurka8b805b12012-04-18 14:23:14 -07001105 private static final void beginDocument(XmlPullParser parser, String firstElementName)
1106 throws XmlPullParserException, IOException {
1107 int type;
Michael Jurka9bc8eba2012-05-21 20:36:44 -07001108 while ((type = parser.next()) != XmlPullParser.START_TAG
1109 && type != XmlPullParser.END_DOCUMENT) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001110 ;
1111 }
1112
Michael Jurka9bc8eba2012-05-21 20:36:44 -07001113 if (type != XmlPullParser.START_TAG) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001114 throw new XmlPullParserException("No start tag found");
1115 }
1116
1117 if (!parser.getName().equals(firstElementName)) {
1118 throw new XmlPullParserException("Unexpected start tag: found " + parser.getName() +
1119 ", expected " + firstElementName);
1120 }
1121 }
1122
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001123 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001124 * Loads the default set of favorite packages from an xml file.
1125 *
1126 * @param db The database to write the values into
Winson Chung3d503fb2011-07-13 17:25:49 -07001127 * @param filterContainerId The specific container id of items to load
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001128 */
Winson Chung6d092682011-11-16 18:43:26 -08001129 private int loadFavorites(SQLiteDatabase db, int workspaceResourceId) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001130 Intent intent = new Intent(Intent.ACTION_MAIN, null);
1131 intent.addCategory(Intent.CATEGORY_LAUNCHER);
1132 ContentValues values = new ContentValues();
1133
Daniel Sandler57dac262013-10-03 13:28:36 -04001134 if (LOGD) Log.v(TAG, String.format("Loading favorites from resid=0x%08x", workspaceResourceId));
1135
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001136 PackageManager packageManager = mContext.getPackageManager();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137 int i = 0;
1138 try {
Winson Chung6d092682011-11-16 18:43:26 -08001139 XmlResourceParser parser = mContext.getResources().getXml(workspaceResourceId);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001140 AttributeSet attrs = Xml.asAttributeSet(parser);
Michael Jurka8b805b12012-04-18 14:23:14 -07001141 beginDocument(parser, TAG_FAVORITES);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001142
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001143 final int depth = parser.getDepth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001144
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001145 int type;
1146 while (((type = parser.next()) != XmlPullParser.END_TAG ||
1147 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
1148
1149 if (type != XmlPullParser.START_TAG) {
1150 continue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151 }
1152
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001153 boolean added = false;
1154 final String name = parser.getName();
1155
Daniel Sandler57dac262013-10-03 13:28:36 -04001156 if (TAG_INCLUDE.equals(name)) {
1157 final TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.Include);
1158
1159 final int resId = a.getResourceId(R.styleable.Include_workspace, 0);
1160
1161 if (LOGD) Log.v(TAG, String.format(("%" + (2*(depth+1)) + "s<include workspace=%08x>"),
1162 "", resId));
1163
1164 if (resId != 0 && resId != workspaceResourceId) {
1165 // recursively load some more favorites, why not?
1166 i += loadFavorites(db, resId);
1167 added = false;
Daniel Sandler57dac262013-10-03 13:28:36 -04001168 } else {
1169 Log.w(TAG, String.format("Skipping <include workspace=0x%08x>", resId));
1170 }
1171
1172 a.recycle();
1173
1174 if (LOGD) Log.v(TAG, String.format(("%" + (2*(depth+1)) + "s</include>"), ""));
1175 continue;
1176 }
1177
1178 // Assuming it's a <favorite> at this point
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001179 TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.Favorite);
1180
Winson Chung3d503fb2011-07-13 17:25:49 -07001181 long container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
1182 if (a.hasValue(R.styleable.Favorite_container)) {
1183 container = Long.valueOf(a.getString(R.styleable.Favorite_container));
1184 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001185
Winson Chung6d092682011-11-16 18:43:26 -08001186 String screen = a.getString(R.styleable.Favorite_screen);
1187 String x = a.getString(R.styleable.Favorite_x);
1188 String y = a.getString(R.styleable.Favorite_y);
1189
Winson Chung6d092682011-11-16 18:43:26 -08001190 values.clear();
1191 values.put(LauncherSettings.Favorites.CONTAINER, container);
1192 values.put(LauncherSettings.Favorites.SCREEN, screen);
1193 values.put(LauncherSettings.Favorites.CELLX, x);
1194 values.put(LauncherSettings.Favorites.CELLY, y);
1195
Daniel Sandler57dac262013-10-03 13:28:36 -04001196 if (LOGD) {
1197 final String title = a.getString(R.styleable.Favorite_title);
1198 final String pkg = a.getString(R.styleable.Favorite_packageName);
1199 final String something = title != null ? title : pkg;
1200 Log.v(TAG, String.format(
1201 ("%" + (2*(depth+1)) + "s<%s%s c=%d s=%s x=%s y=%s>"),
1202 "", name,
1203 (something == null ? "" : (" \"" + something + "\"")),
1204 container, screen, x, y));
1205 }
1206
Winson Chung6d092682011-11-16 18:43:26 -08001207 if (TAG_FAVORITE.equals(name)) {
1208 long id = addAppShortcut(db, values, a, packageManager, intent);
1209 added = id >= 0;
1210 } else if (TAG_SEARCH.equals(name)) {
1211 added = addSearchWidget(db, values);
1212 } else if (TAG_CLOCK.equals(name)) {
1213 added = addClockWidget(db, values);
1214 } else if (TAG_APPWIDGET.equals(name)) {
Winson Chungb3302ae2012-05-01 10:19:14 -07001215 added = addAppWidget(parser, attrs, type, db, values, a, packageManager);
Winson Chung6d092682011-11-16 18:43:26 -08001216 } else if (TAG_SHORTCUT.equals(name)) {
1217 long id = addUriShortcut(db, values, a);
1218 added = id >= 0;
1219 } else if (TAG_FOLDER.equals(name)) {
1220 String title;
1221 int titleResId = a.getResourceId(R.styleable.Favorite_title, -1);
1222 if (titleResId != -1) {
1223 title = mContext.getResources().getString(titleResId);
1224 } else {
1225 title = mContext.getResources().getString(R.string.folder_name);
Winson Chung3d503fb2011-07-13 17:25:49 -07001226 }
Winson Chung6d092682011-11-16 18:43:26 -08001227 values.put(LauncherSettings.Favorites.TITLE, title);
1228 long folderId = addFolder(db, values);
1229 added = folderId >= 0;
Winson Chung3d503fb2011-07-13 17:25:49 -07001230
Winson Chung6d092682011-11-16 18:43:26 -08001231 ArrayList<Long> folderItems = new ArrayList<Long>();
Winson Chung3d503fb2011-07-13 17:25:49 -07001232
Winson Chung6d092682011-11-16 18:43:26 -08001233 int folderDepth = parser.getDepth();
1234 while ((type = parser.next()) != XmlPullParser.END_TAG ||
1235 parser.getDepth() > folderDepth) {
1236 if (type != XmlPullParser.START_TAG) {
1237 continue;
1238 }
1239 final String folder_item_name = parser.getName();
1240
1241 TypedArray ar = mContext.obtainStyledAttributes(attrs,
1242 R.styleable.Favorite);
1243 values.clear();
1244 values.put(LauncherSettings.Favorites.CONTAINER, folderId);
1245
Daniel Sandler57dac262013-10-03 13:28:36 -04001246 if (LOGD) {
1247 final String pkg = ar.getString(R.styleable.Favorite_packageName);
1248 final String uri = ar.getString(R.styleable.Favorite_uri);
1249 Log.v(TAG, String.format(("%" + (2*(folderDepth+1)) + "s<%s \"%s\">"), "",
1250 folder_item_name, uri != null ? uri : pkg));
1251 }
1252
Winson Chung6d092682011-11-16 18:43:26 -08001253 if (TAG_FAVORITE.equals(folder_item_name) && folderId >= 0) {
1254 long id =
1255 addAppShortcut(db, values, ar, packageManager, intent);
1256 if (id >= 0) {
1257 folderItems.add(id);
1258 }
1259 } else if (TAG_SHORTCUT.equals(folder_item_name) && folderId >= 0) {
1260 long id = addUriShortcut(db, values, ar);
1261 if (id >= 0) {
1262 folderItems.add(id);
1263 }
Adam Cohen228da5a2011-07-27 22:23:47 -07001264 } else {
Winson Chung6d092682011-11-16 18:43:26 -08001265 throw new RuntimeException("Folders can " +
1266 "contain only shortcuts");
Adam Cohen228da5a2011-07-27 22:23:47 -07001267 }
Winson Chung6d092682011-11-16 18:43:26 -08001268 ar.recycle();
1269 }
1270 // We can only have folders with >= 2 items, so we need to remove the
1271 // folder and clean up if less than 2 items were included, or some
1272 // failed to add, and less than 2 were actually added
1273 if (folderItems.size() < 2 && folderId >= 0) {
1274 // We just delete the folder and any items that made it
1275 deleteId(db, folderId);
1276 if (folderItems.size() > 0) {
1277 deleteId(db, folderItems.get(0));
Adam Cohen228da5a2011-07-27 22:23:47 -07001278 }
Winson Chung6d092682011-11-16 18:43:26 -08001279 added = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001280 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001281 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001282 if (added) i++;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001283 a.recycle();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284 }
1285 } catch (XmlPullParserException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001286 Log.w(TAG, "Got exception parsing favorites.", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 } catch (IOException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001288 Log.w(TAG, "Got exception parsing favorites.", e);
Winson Chung3d503fb2011-07-13 17:25:49 -07001289 } catch (RuntimeException e) {
1290 Log.w(TAG, "Got exception parsing favorites.", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001292
Winson Chungc763c4e2013-07-19 13:49:06 -07001293 // Update the max item id after we have loaded the database
1294 if (mMaxItemId == -1) {
1295 mMaxItemId = initializeMaxItemId(db);
1296 }
1297
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001298 return i;
1299 }
1300
Adam Cohen228da5a2011-07-27 22:23:47 -07001301 private long addAppShortcut(SQLiteDatabase db, ContentValues values, TypedArray a,
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001302 PackageManager packageManager, Intent intent) {
Adam Cohen228da5a2011-07-27 22:23:47 -07001303 long id = -1;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001304 ActivityInfo info;
1305 String packageName = a.getString(R.styleable.Favorite_packageName);
1306 String className = a.getString(R.styleable.Favorite_className);
1307 try {
Romain Guy693599f2010-03-23 10:58:18 -07001308 ComponentName cn;
1309 try {
1310 cn = new ComponentName(packageName, className);
1311 info = packageManager.getActivityInfo(cn, 0);
1312 } catch (PackageManager.NameNotFoundException nnfe) {
1313 String[] packages = packageManager.currentToCanonicalPackageNames(
1314 new String[] { packageName });
1315 cn = new ComponentName(packages[0], className);
1316 info = packageManager.getActivityInfo(cn, 0);
1317 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001318 id = generateNewItemId();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001319 intent.setComponent(cn);
Romain Guy693599f2010-03-23 10:58:18 -07001320 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1321 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Romain Guy1ce1a242009-06-23 17:34:54 -07001322 values.put(Favorites.INTENT, intent.toUri(0));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001323 values.put(Favorites.TITLE, info.loadLabel(packageManager).toString());
1324 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPLICATION);
1325 values.put(Favorites.SPANX, 1);
1326 values.put(Favorites.SPANY, 1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001327 values.put(Favorites._ID, generateNewItemId());
Adam Cohen228da5a2011-07-27 22:23:47 -07001328 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values) < 0) {
1329 return -1;
1330 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001331 } catch (PackageManager.NameNotFoundException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001332 Log.w(TAG, "Unable to add favorite: " + packageName +
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001333 "/" + className, e);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001334 }
Adam Cohen228da5a2011-07-27 22:23:47 -07001335 return id;
1336 }
1337
1338 private long addFolder(SQLiteDatabase db, ContentValues values) {
1339 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_FOLDER);
1340 values.put(Favorites.SPANX, 1);
1341 values.put(Favorites.SPANY, 1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001342 long id = generateNewItemId();
Adam Cohen228da5a2011-07-27 22:23:47 -07001343 values.put(Favorites._ID, id);
1344 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values) <= 0) {
1345 return -1;
1346 } else {
1347 return id;
1348 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001349 }
1350
Bjorn Bringertcd8fec02010-01-14 13:26:43 +00001351 private ComponentName getSearchWidgetProvider() {
1352 SearchManager searchManager =
1353 (SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE);
1354 ComponentName searchComponent = searchManager.getGlobalSearchActivity();
1355 if (searchComponent == null) return null;
1356 return getProviderInPackage(searchComponent.getPackageName());
1357 }
1358
1359 /**
1360 * Gets an appwidget provider from the given package. If the package contains more than
1361 * one appwidget provider, an arbitrary one is returned.
1362 */
1363 private ComponentName getProviderInPackage(String packageName) {
1364 AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
1365 List<AppWidgetProviderInfo> providers = appWidgetManager.getInstalledProviders();
1366 if (providers == null) return null;
1367 final int providerCount = providers.size();
1368 for (int i = 0; i < providerCount; i++) {
1369 ComponentName provider = providers.get(i).provider;
1370 if (provider != null && provider.getPackageName().equals(packageName)) {
1371 return provider;
1372 }
1373 }
1374 return null;
1375 }
1376
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001377 private boolean addSearchWidget(SQLiteDatabase db, ContentValues values) {
Bjorn Bringertcd8fec02010-01-14 13:26:43 +00001378 ComponentName cn = getSearchWidgetProvider();
Winson Chungb3302ae2012-05-01 10:19:14 -07001379 return addAppWidget(db, values, cn, 4, 1, null);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001380 }
1381
1382 private boolean addClockWidget(SQLiteDatabase db, ContentValues values) {
Bjorn Bringert34251342009-12-15 13:33:11 +00001383 ComponentName cn = new ComponentName("com.android.alarmclock",
1384 "com.android.alarmclock.AnalogAppWidgetProvider");
Winson Chungb3302ae2012-05-01 10:19:14 -07001385 return addAppWidget(db, values, cn, 2, 2, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 }
Adam Cohen228da5a2011-07-27 22:23:47 -07001387
Winson Chungb3302ae2012-05-01 10:19:14 -07001388 private boolean addAppWidget(XmlResourceParser parser, AttributeSet attrs, int type,
1389 SQLiteDatabase db, ContentValues values, TypedArray a,
1390 PackageManager packageManager) throws XmlPullParserException, IOException {
Romain Guy693599f2010-03-23 10:58:18 -07001391
Mike Cleronb87bd162009-10-30 16:36:56 -07001392 String packageName = a.getString(R.styleable.Favorite_packageName);
1393 String className = a.getString(R.styleable.Favorite_className);
1394
1395 if (packageName == null || className == null) {
1396 return false;
1397 }
Romain Guy693599f2010-03-23 10:58:18 -07001398
1399 boolean hasPackage = true;
Mike Cleronb87bd162009-10-30 16:36:56 -07001400 ComponentName cn = new ComponentName(packageName, className);
Romain Guy693599f2010-03-23 10:58:18 -07001401 try {
1402 packageManager.getReceiverInfo(cn, 0);
1403 } catch (Exception e) {
1404 String[] packages = packageManager.currentToCanonicalPackageNames(
1405 new String[] { packageName });
1406 cn = new ComponentName(packages[0], className);
1407 try {
1408 packageManager.getReceiverInfo(cn, 0);
1409 } catch (Exception e1) {
1410 hasPackage = false;
1411 }
1412 }
1413
1414 if (hasPackage) {
1415 int spanX = a.getInt(R.styleable.Favorite_spanX, 0);
1416 int spanY = a.getInt(R.styleable.Favorite_spanY, 0);
Winson Chungb3302ae2012-05-01 10:19:14 -07001417
1418 // Read the extras
1419 Bundle extras = new Bundle();
1420 int widgetDepth = parser.getDepth();
1421 while ((type = parser.next()) != XmlPullParser.END_TAG ||
1422 parser.getDepth() > widgetDepth) {
1423 if (type != XmlPullParser.START_TAG) {
1424 continue;
1425 }
1426
1427 TypedArray ar = mContext.obtainStyledAttributes(attrs, R.styleable.Extra);
1428 if (TAG_EXTRA.equals(parser.getName())) {
1429 String key = ar.getString(R.styleable.Extra_key);
1430 String value = ar.getString(R.styleable.Extra_value);
1431 if (key != null && value != null) {
1432 extras.putString(key, value);
1433 } else {
1434 throw new RuntimeException("Widget extras must have a key and value");
1435 }
1436 } else {
1437 throw new RuntimeException("Widgets can contain only extras");
1438 }
1439 ar.recycle();
1440 }
1441
1442 return addAppWidget(db, values, cn, spanX, spanY, extras);
Romain Guy693599f2010-03-23 10:58:18 -07001443 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001444
Romain Guy693599f2010-03-23 10:58:18 -07001445 return false;
Bjorn Bringert7984c942009-12-09 15:38:25 +00001446 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001447 private boolean addAppWidget(SQLiteDatabase db, ContentValues values, ComponentName cn,
Winson Chungb3302ae2012-05-01 10:19:14 -07001448 int spanX, int spanY, Bundle extras) {
Mike Cleronb87bd162009-10-30 16:36:56 -07001449 boolean allocatedAppWidgets = false;
1450 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
1451
1452 try {
1453 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001454
Mike Cleronb87bd162009-10-30 16:36:56 -07001455 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001456 values.put(Favorites.SPANX, spanX);
1457 values.put(Favorites.SPANY, spanY);
Mike Cleronb87bd162009-10-30 16:36:56 -07001458 values.put(Favorites.APPWIDGET_ID, appWidgetId);
Chris Wrend5e66bf2013-09-16 14:02:29 -04001459 values.put(Favorites.APPWIDGET_PROVIDER, cn.flattenToString());
Adam Cohendcd297f2013-06-18 13:13:40 -07001460 values.put(Favorites._ID, generateNewItemId());
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001461 dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values);
Mike Cleronb87bd162009-10-30 16:36:56 -07001462
1463 allocatedAppWidgets = true;
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001464
Michael Jurka8b805b12012-04-18 14:23:14 -07001465 // TODO: need to check return value
1466 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, cn);
Winson Chungb3302ae2012-05-01 10:19:14 -07001467
1468 // Send a broadcast to configure the widget
1469 if (extras != null && !extras.isEmpty()) {
1470 Intent intent = new Intent(ACTION_APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE);
1471 intent.setComponent(cn);
1472 intent.putExtras(extras);
1473 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1474 mContext.sendBroadcast(intent);
1475 }
Mike Cleronb87bd162009-10-30 16:36:56 -07001476 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001477 Log.e(TAG, "Problem allocating appWidgetId", ex);
Mike Cleronb87bd162009-10-30 16:36:56 -07001478 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001479
Mike Cleronb87bd162009-10-30 16:36:56 -07001480 return allocatedAppWidgets;
1481 }
Adam Cohen228da5a2011-07-27 22:23:47 -07001482
1483 private long addUriShortcut(SQLiteDatabase db, ContentValues values,
Mike Cleronb87bd162009-10-30 16:36:56 -07001484 TypedArray a) {
1485 Resources r = mContext.getResources();
1486
1487 final int iconResId = a.getResourceId(R.styleable.Favorite_icon, 0);
1488 final int titleResId = a.getResourceId(R.styleable.Favorite_title, 0);
1489
Romain Guy7eb9e5e2009-12-02 20:10:07 -08001490 Intent intent;
Mike Cleronb87bd162009-10-30 16:36:56 -07001491 String uri = null;
1492 try {
1493 uri = a.getString(R.styleable.Favorite_uri);
1494 intent = Intent.parseUri(uri, 0);
1495 } catch (URISyntaxException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001496 Log.w(TAG, "Shortcut has malformed uri: " + uri);
Adam Cohen228da5a2011-07-27 22:23:47 -07001497 return -1; // Oh well
Mike Cleronb87bd162009-10-30 16:36:56 -07001498 }
1499
1500 if (iconResId == 0 || titleResId == 0) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001501 Log.w(TAG, "Shortcut is missing title or icon resource ID");
Adam Cohen228da5a2011-07-27 22:23:47 -07001502 return -1;
Mike Cleronb87bd162009-10-30 16:36:56 -07001503 }
1504
Adam Cohendcd297f2013-06-18 13:13:40 -07001505 long id = generateNewItemId();
Mike Cleronb87bd162009-10-30 16:36:56 -07001506 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1507 values.put(Favorites.INTENT, intent.toUri(0));
1508 values.put(Favorites.TITLE, r.getString(titleResId));
1509 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_SHORTCUT);
1510 values.put(Favorites.SPANX, 1);
1511 values.put(Favorites.SPANY, 1);
1512 values.put(Favorites.ICON_TYPE, Favorites.ICON_TYPE_RESOURCE);
1513 values.put(Favorites.ICON_PACKAGE, mContext.getPackageName());
1514 values.put(Favorites.ICON_RESOURCE, r.getResourceName(iconResId));
Adam Cohen228da5a2011-07-27 22:23:47 -07001515 values.put(Favorites._ID, id);
Mike Cleronb87bd162009-10-30 16:36:56 -07001516
Adam Cohen228da5a2011-07-27 22:23:47 -07001517 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values) < 0) {
1518 return -1;
1519 }
1520 return id;
Mike Cleronb87bd162009-10-30 16:36:56 -07001521 }
Dan Sandlerd5024042014-01-09 15:01:33 -05001522
1523 public void migrateLauncher2Shortcuts(SQLiteDatabase db, Uri uri) {
1524 final ContentResolver resolver = mContext.getContentResolver();
1525 Cursor c = null;
1526 int count = 0;
1527 int curScreen = 0;
1528
1529 try {
1530 c = resolver.query(uri, null, null, null, "title ASC");
1531 } catch (Exception e) {
1532 // Ignore
1533 }
1534
Dan Sandlerd5024042014-01-09 15:01:33 -05001535 // We already have a favorites database in the old provider
1536 if (c != null) {
1537 try {
1538 if (c.getCount() > 0) {
1539 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1540 final int intentIndex
1541 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
1542 final int titleIndex
1543 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
1544 final int iconTypeIndex
1545 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
1546 final int iconIndex
1547 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1548 final int iconPackageIndex
1549 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
1550 final int iconResourceIndex
1551 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
1552 final int containerIndex
1553 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
1554 final int itemTypeIndex
1555 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
1556 final int screenIndex
1557 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
1558 final int cellXIndex
1559 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
1560 final int cellYIndex
1561 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
1562 final int uriIndex
1563 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1564 final int displayModeIndex
1565 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
1566
1567 int i = 0;
1568 int curX = 0;
1569 int curY = 0;
1570
1571 final LauncherAppState app = LauncherAppState.getInstance();
1572 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1573 final int width = (int) grid.numColumns;
1574 final int height = (int) grid.numRows;
1575 final int hotseatWidth = (int) grid.numHotseatIcons;
Adam Cohen556f6132014-01-15 15:18:08 -08001576 PackageManager pm = mContext.getPackageManager();
Dan Sandlerd5024042014-01-09 15:01:33 -05001577
1578 final HashSet<String> seenIntents = new HashSet<String>(c.getCount());
1579
Adam Cohen72960972014-01-15 18:13:55 -08001580 final ArrayList<ContentValues> shortcuts = new ArrayList<ContentValues>();
1581 final ArrayList<ContentValues> folders = new ArrayList<ContentValues>();
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001582 final SparseArray<ContentValues> hotseat = new SparseArray<ContentValues>();
Dan Sandlerd5024042014-01-09 15:01:33 -05001583
1584 while (c.moveToNext()) {
1585 final int itemType = c.getInt(itemTypeIndex);
1586 if (itemType != Favorites.ITEM_TYPE_APPLICATION
1587 && itemType != Favorites.ITEM_TYPE_SHORTCUT
1588 && itemType != Favorites.ITEM_TYPE_FOLDER) {
1589 continue;
1590 }
1591
1592 final int cellX = c.getInt(cellXIndex);
1593 final int cellY = c.getInt(cellYIndex);
1594 final int screen = c.getInt(screenIndex);
1595 int container = c.getInt(containerIndex);
1596 final String intentStr = c.getString(intentIndex);
1597 Launcher.addDumpLog(TAG, "migrating \""
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001598 + c.getString(titleIndex) + "\" ("
1599 + cellX + "," + cellY + "@"
1600 + LauncherSettings.Favorites.containerToString(container)
1601 + "/" + screen
1602 + "): " + intentStr, true);
Dan Sandlerd5024042014-01-09 15:01:33 -05001603
1604 if (itemType != Favorites.ITEM_TYPE_FOLDER) {
Adam Cohen556f6132014-01-15 15:18:08 -08001605
1606 final Intent intent;
1607 final ComponentName cn;
1608 try {
1609 intent = Intent.parseUri(intentStr, 0);
1610 } catch (URISyntaxException e) {
1611 // bogus intent?
1612 Launcher.addDumpLog(TAG,
1613 "skipping invalid intent uri", true);
1614 continue;
1615 }
1616
1617 cn = intent.getComponent();
Dan Sandlerd5024042014-01-09 15:01:33 -05001618 if (TextUtils.isEmpty(intentStr)) {
1619 // no intent? no icon
1620 Launcher.addDumpLog(TAG, "skipping empty intent", true);
1621 continue;
Adam Cohen72960972014-01-15 18:13:55 -08001622 } else if (cn != null &&
1623 !LauncherModel.isValidPackageComponent(pm, cn)) {
Adam Cohen556f6132014-01-15 15:18:08 -08001624 // component no longer exists.
Adam Cohen72960972014-01-15 18:13:55 -08001625 Launcher.addDumpLog(TAG, "skipping item whose component " +
Adam Cohen556f6132014-01-15 15:18:08 -08001626 "no longer exists.", true);
1627 continue;
Adam Cohen72960972014-01-15 18:13:55 -08001628 } else if (container ==
1629 LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1630 // Dedupe icons directly on the workspace
1631
Adam Cohen556f6132014-01-15 15:18:08 -08001632 // Canonicalize
1633 // the Play Store sets the package parameter, but Launcher
1634 // does not, so we clear that out to keep them the same
1635 intent.setPackage(null);
1636 final String key = intent.toUri(0);
1637 if (seenIntents.contains(key)) {
1638 Launcher.addDumpLog(TAG, "skipping duplicate", true);
Dan Sandlerd5024042014-01-09 15:01:33 -05001639 continue;
Adam Cohen556f6132014-01-15 15:18:08 -08001640 } else {
1641 seenIntents.add(key);
Dan Sandlerd5024042014-01-09 15:01:33 -05001642 }
1643 }
1644 }
1645
1646 ContentValues values = new ContentValues(c.getColumnCount());
1647 values.put(LauncherSettings.Favorites._ID, c.getInt(idIndex));
1648 values.put(LauncherSettings.Favorites.INTENT, intentStr);
1649 values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex));
1650 values.put(LauncherSettings.Favorites.ICON_TYPE,
1651 c.getInt(iconTypeIndex));
1652 values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex));
1653 values.put(LauncherSettings.Favorites.ICON_PACKAGE,
1654 c.getString(iconPackageIndex));
1655 values.put(LauncherSettings.Favorites.ICON_RESOURCE,
1656 c.getString(iconResourceIndex));
1657 values.put(LauncherSettings.Favorites.ITEM_TYPE, itemType);
1658 values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1);
1659 values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex));
1660 values.put(LauncherSettings.Favorites.DISPLAY_MODE,
1661 c.getInt(displayModeIndex));
1662
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001663 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1664 hotseat.put(screen, values);
Dan Sandlerd5024042014-01-09 15:01:33 -05001665 }
1666
1667 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1668 // In a folder or in the hotseat, preserve position
1669 values.put(LauncherSettings.Favorites.SCREEN, screen);
1670 values.put(LauncherSettings.Favorites.CELLX, cellX);
1671 values.put(LauncherSettings.Favorites.CELLY, cellY);
1672 } else {
Adam Cohen72960972014-01-15 18:13:55 -08001673 // For items contained directly on one of the workspace screen,
1674 // we'll determine their location (screen, x, y) in a second pass.
Dan Sandlerd5024042014-01-09 15:01:33 -05001675 }
1676
1677 values.put(LauncherSettings.Favorites.CONTAINER, container);
1678
Adam Cohen72960972014-01-15 18:13:55 -08001679 if (itemType != Favorites.ITEM_TYPE_FOLDER) {
1680 shortcuts.add(values);
1681 } else {
1682 folders.add(values);
1683 }
Dan Sandlerd5024042014-01-09 15:01:33 -05001684 }
1685
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001686 // Now that we have all the hotseat icons, let's go through them left-right
1687 // and assign valid locations for them in the new hotseat
1688 final int N = hotseat.size();
1689 for (int idx=0; idx<N; idx++) {
1690 int hotseatX = hotseat.keyAt(idx);
1691 ContentValues values = hotseat.valueAt(idx);
1692
1693 if (hotseatX == grid.hotseatAllAppsRank) {
1694 // let's drop this in the next available hole in the hotseat
1695 while (++hotseatX < hotseatWidth) {
1696 if (hotseat.get(hotseatX) == null) {
1697 // found a spot! move it here
1698 values.put(LauncherSettings.Favorites.SCREEN,
1699 hotseatX);
1700 break;
1701 }
1702 }
1703 }
1704 if (hotseatX >= hotseatWidth) {
1705 // no room for you in the hotseat? it's off to the desktop with you
1706 values.put(LauncherSettings.Favorites.CONTAINER,
1707 Favorites.CONTAINER_DESKTOP);
1708 }
1709 }
1710
Adam Cohen72960972014-01-15 18:13:55 -08001711 final ArrayList<ContentValues> allItems = new ArrayList<ContentValues>();
1712 // Folders first
1713 allItems.addAll(folders);
1714 // Then shortcuts
1715 allItems.addAll(shortcuts);
1716
1717 // Layout all the folders
1718 for (ContentValues values: allItems) {
1719 if (values.getAsInteger(LauncherSettings.Favorites.CONTAINER) !=
1720 LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1721 // Hotseat items and folder items have already had their
1722 // location information set. Nothing to be done here.
1723 continue;
1724 }
1725 values.put(LauncherSettings.Favorites.SCREEN, curScreen);
1726 values.put(LauncherSettings.Favorites.CELLX, curX);
1727 values.put(LauncherSettings.Favorites.CELLY, curY);
1728 curX = (curX + 1) % width;
1729 if (curX == 0) {
1730 curY = (curY + 1);
1731 }
1732 // Leave the last row of icons blank on every screen
1733 if (curY == height - 1) {
1734 curScreen = (int) generateNewScreenId();
1735 curY = 0;
1736 }
1737 }
1738
1739 if (allItems.size() > 0) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001740 db.beginTransaction();
1741 try {
Adam Cohen72960972014-01-15 18:13:55 -08001742 for (ContentValues row: allItems) {
1743 if (row == null) continue;
1744 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, row)
Dan Sandlerd5024042014-01-09 15:01:33 -05001745 < 0) {
1746 return;
1747 } else {
1748 count++;
1749 }
1750 }
1751 db.setTransactionSuccessful();
1752 } finally {
1753 db.endTransaction();
1754 }
1755 }
1756
1757 db.beginTransaction();
1758 try {
1759 for (i=0; i<=curScreen; i++) {
1760 final ContentValues values = new ContentValues();
1761 values.put(LauncherSettings.WorkspaceScreens._ID, i);
1762 values.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
1763 if (dbInsertAndCheck(this, db, TABLE_WORKSPACE_SCREENS, null, values)
1764 < 0) {
1765 return;
1766 }
1767 }
1768 db.setTransactionSuccessful();
1769 } finally {
1770 db.endTransaction();
1771 }
1772 }
1773 } finally {
1774 c.close();
1775 }
1776 }
1777
1778 Launcher.addDumpLog(TAG, "migrated " + count + " icons from Launcher2 into "
1779 + (curScreen+1) + " screens", true);
1780
1781 // ensure that new screens are created to hold these icons
1782 setFlagJustLoadedOldDb();
1783
1784 // Update max IDs; very important since we just grabbed IDs from another database
1785 mMaxItemId = initializeMaxItemId(db);
1786 mMaxScreenId = initializeMaxScreenId(db);
1787 if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId + " mMaxScreenId: " + mMaxScreenId);
1788 }
Mike Cleronb87bd162009-10-30 16:36:56 -07001789 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001790
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001791 /**
1792 * Build a query string that will match any row where the column matches
1793 * anything in the values list.
1794 */
1795 static String buildOrWhereString(String column, int[] values) {
1796 StringBuilder selectWhere = new StringBuilder();
1797 for (int i = values.length - 1; i >= 0; i--) {
1798 selectWhere.append(column).append("=").append(values[i]);
1799 if (i > 0) {
1800 selectWhere.append(" OR ");
1801 }
1802 }
1803 return selectWhere.toString();
1804 }
1805
1806 static class SqlArguments {
1807 public final String table;
1808 public final String where;
1809 public final String[] args;
1810
1811 SqlArguments(Uri url, String where, String[] args) {
1812 if (url.getPathSegments().size() == 1) {
1813 this.table = url.getPathSegments().get(0);
1814 this.where = where;
1815 this.args = args;
1816 } else if (url.getPathSegments().size() != 2) {
1817 throw new IllegalArgumentException("Invalid URI: " + url);
1818 } else if (!TextUtils.isEmpty(where)) {
1819 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
1820 } else {
1821 this.table = url.getPathSegments().get(0);
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001822 this.where = "_id=" + ContentUris.parseId(url);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001823 this.args = null;
1824 }
1825 }
1826
1827 SqlArguments(Uri url) {
1828 if (url.getPathSegments().size() == 1) {
1829 table = url.getPathSegments().get(0);
1830 where = null;
1831 args = null;
1832 } else {
1833 throw new IllegalArgumentException("Invalid URI: " + url);
1834 }
1835 }
1836 }
Adam Cohen72960972014-01-15 18:13:55 -08001837}