blob: e262be385e318836663c362f73b2a8bf752faea0 [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
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
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.ContentProvider;
24import android.content.Context;
25import android.content.ContentValues;
26import android.content.Intent;
27import android.content.ComponentName;
28import android.content.ContentUris;
29import android.content.ContentResolver;
Mike Cleronb87bd162009-10-30 16:36:56 -070030import android.content.res.Resources;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070031import android.content.res.XmlResourceParser;
32import android.content.res.TypedArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.pm.PackageManager;
34import android.content.pm.ActivityInfo;
35import android.database.sqlite.SQLiteOpenHelper;
36import android.database.sqlite.SQLiteDatabase;
Joe Onorato0589f0f2010-02-08 13:44:00 -080037import android.database.sqlite.SQLiteStatement;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.database.sqlite.SQLiteQueryBuilder;
39import android.database.Cursor;
40import android.database.SQLException;
Joe Onorato0589f0f2010-02-08 13:44:00 -080041import android.graphics.Bitmap;
42import android.graphics.BitmapFactory;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.util.Log;
44import android.util.Xml;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070045import android.util.AttributeSet;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.net.Uri;
47import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.provider.Settings;
49
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import java.io.IOException;
Mike Cleronb87bd162009-10-30 16:36:56 -070051import java.net.URISyntaxException;
Bjorn Bringertcd8fec02010-01-14 13:26:43 +000052import java.util.List;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import org.xmlpull.v1.XmlPullParserException;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070055import org.xmlpull.v1.XmlPullParser;
Dianne Hackborn094fc7a2010-02-24 20:01:46 -080056
57import com.android.internal.util.XmlUtils;
Joe Onoratoa5902522009-07-30 13:37:37 -070058import com.android.launcher2.LauncherSettings.Favorites;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059
60public class LauncherProvider extends ContentProvider {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080061 private static final String TAG = "Launcher.LauncherProvider";
62 private static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063
64 private static final String DATABASE_NAME = "launcher.db";
65
Joe Onorato0589f0f2010-02-08 13:44:00 -080066 private static final int DATABASE_VERSION = 8;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067
Joe Onoratoa5902522009-07-30 13:37:37 -070068 static final String AUTHORITY = "com.android.launcher2.settings";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070 static final String TABLE_FAVORITES = "favorites";
71 static final String PARAMETER_NOTIFY = "notify";
72
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -070073 /**
Romain Guy73b979d2009-06-09 12:57:21 -070074 * {@link Uri} triggered at any registered {@link android.database.ContentObserver} when
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -070075 * {@link AppWidgetHost#deleteHost()} is called during database creation.
76 * Use this to recall {@link AppWidgetHost#startListening()} if needed.
77 */
78 static final Uri CONTENT_APPWIDGET_RESET_URI =
79 Uri.parse("content://" + AUTHORITY + "/appWidgetReset");
80
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081 private SQLiteOpenHelper mOpenHelper;
82
83 @Override
84 public boolean onCreate() {
85 mOpenHelper = new DatabaseHelper(getContext());
86 return true;
87 }
88
89 @Override
90 public String getType(Uri uri) {
91 SqlArguments args = new SqlArguments(uri, null, null);
92 if (TextUtils.isEmpty(args.where)) {
93 return "vnd.android.cursor.dir/" + args.table;
94 } else {
95 return "vnd.android.cursor.item/" + args.table;
96 }
97 }
98
99 @Override
100 public Cursor query(Uri uri, String[] projection, String selection,
101 String[] selectionArgs, String sortOrder) {
102
103 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
104 SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
105 qb.setTables(args.table);
106
Romain Guy73b979d2009-06-09 12:57:21 -0700107 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108 Cursor result = qb.query(db, projection, args.where, args.args, null, null, sortOrder);
109 result.setNotificationUri(getContext().getContentResolver(), uri);
110
111 return result;
112 }
113
114 @Override
115 public Uri insert(Uri uri, ContentValues initialValues) {
116 SqlArguments args = new SqlArguments(uri);
117
118 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
119 final long rowId = db.insert(args.table, null, initialValues);
120 if (rowId <= 0) return null;
121
122 uri = ContentUris.withAppendedId(uri, rowId);
123 sendNotify(uri);
124
125 return uri;
126 }
127
128 @Override
129 public int bulkInsert(Uri uri, ContentValues[] values) {
130 SqlArguments args = new SqlArguments(uri);
131
132 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
133 db.beginTransaction();
134 try {
135 int numValues = values.length;
136 for (int i = 0; i < numValues; i++) {
137 if (db.insert(args.table, null, values[i]) < 0) return 0;
138 }
139 db.setTransactionSuccessful();
140 } finally {
141 db.endTransaction();
142 }
143
144 sendNotify(uri);
145 return values.length;
146 }
147
148 @Override
149 public int delete(Uri uri, String selection, String[] selectionArgs) {
150 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
151
152 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
153 int count = db.delete(args.table, args.where, args.args);
154 if (count > 0) sendNotify(uri);
155
156 return count;
157 }
158
159 @Override
160 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
161 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
162
163 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
164 int count = db.update(args.table, values, args.where, args.args);
165 if (count > 0) sendNotify(uri);
166
167 return count;
168 }
169
170 private void sendNotify(Uri uri) {
171 String notify = uri.getQueryParameter(PARAMETER_NOTIFY);
172 if (notify == null || "true".equals(notify)) {
173 getContext().getContentResolver().notifyChange(uri, null);
174 }
175 }
176
177 private static class DatabaseHelper extends SQLiteOpenHelper {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 private static final String TAG_FAVORITES = "favorites";
179 private static final String TAG_FAVORITE = "favorite";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700180 private static final String TAG_CLOCK = "clock";
181 private static final String TAG_SEARCH = "search";
Mike Cleronb87bd162009-10-30 16:36:56 -0700182 private static final String TAG_APPWIDGET = "appwidget";
183 private static final String TAG_SHORTCUT = "shortcut";
184
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 private final Context mContext;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700186 private final AppWidgetHost mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187
188 DatabaseHelper(Context context) {
189 super(context, DATABASE_NAME, null, DATABASE_VERSION);
190 mContext = context;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700191 mAppWidgetHost = new AppWidgetHost(context, Launcher.APPWIDGET_HOST_ID);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 }
193
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700194 /**
195 * Send notification that we've deleted the {@link AppWidgetHost},
196 * probably as part of the initial database creation. The receiver may
197 * want to re-call {@link AppWidgetHost#startListening()} to ensure
198 * callbacks are correctly set.
199 */
200 private void sendAppWidgetResetNotify() {
201 final ContentResolver resolver = mContext.getContentResolver();
202 resolver.notifyChange(CONTENT_APPWIDGET_RESET_URI, null);
203 }
204
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205 @Override
206 public void onCreate(SQLiteDatabase db) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800207 if (LOGD) Log.d(TAG, "creating new launcher database");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208
209 db.execSQL("CREATE TABLE favorites (" +
210 "_id INTEGER PRIMARY KEY," +
211 "title TEXT," +
212 "intent TEXT," +
213 "container INTEGER," +
214 "screen INTEGER," +
215 "cellX INTEGER," +
216 "cellY INTEGER," +
217 "spanX INTEGER," +
218 "spanY INTEGER," +
219 "itemType INTEGER," +
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700220 "appWidgetId INTEGER NOT NULL DEFAULT -1," +
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221 "isShortcut INTEGER," +
222 "iconType INTEGER," +
223 "iconPackage TEXT," +
224 "iconResource TEXT," +
225 "icon BLOB," +
226 "uri TEXT," +
227 "displayMode INTEGER" +
228 ");");
229
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700230 // Database was just created, so wipe any previous widgets
231 if (mAppWidgetHost != null) {
232 mAppWidgetHost.deleteHost();
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700233 sendAppWidgetResetNotify();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800234 }
235
236 if (!convertDatabase(db)) {
237 // Populate favorites table with initial favorites
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700238 loadFavorites(db);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239 }
240 }
241
242 private boolean convertDatabase(SQLiteDatabase db) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800243 if (LOGD) Log.d(TAG, "converting database from an older format, but not onUpgrade");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 boolean converted = false;
245
246 final Uri uri = Uri.parse("content://" + Settings.AUTHORITY +
247 "/old_favorites?notify=true");
248 final ContentResolver resolver = mContext.getContentResolver();
249 Cursor cursor = null;
250
251 try {
252 cursor = resolver.query(uri, null, null, null, null);
253 } catch (Exception e) {
254 // Ignore
255 }
256
257 // We already have a favorites database in the old provider
258 if (cursor != null && cursor.getCount() > 0) {
259 try {
260 converted = copyFromCursor(db, cursor) > 0;
261 } finally {
262 cursor.close();
263 }
264
265 if (converted) {
266 resolver.delete(uri, null, null);
267 }
268 }
269
270 if (converted) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700271 // Convert widgets from this import into widgets
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800272 if (LOGD) Log.d(TAG, "converted and now triggering widget upgrade");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273 convertWidgets(db);
274 }
275
276 return converted;
277 }
278
279 private int copyFromCursor(SQLiteDatabase db, Cursor c) {
Romain Guy73b979d2009-06-09 12:57:21 -0700280 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281 final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
282 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
283 final int iconTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
284 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
285 final int iconPackageIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
286 final int iconResourceIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
287 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
288 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
289 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
290 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
291 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
292 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
293 final int displayModeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
294
295 ContentValues[] rows = new ContentValues[c.getCount()];
296 int i = 0;
297 while (c.moveToNext()) {
298 ContentValues values = new ContentValues(c.getColumnCount());
Romain Guy73b979d2009-06-09 12:57:21 -0700299 values.put(LauncherSettings.Favorites._ID, c.getLong(idIndex));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800300 values.put(LauncherSettings.Favorites.INTENT, c.getString(intentIndex));
301 values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex));
302 values.put(LauncherSettings.Favorites.ICON_TYPE, c.getInt(iconTypeIndex));
303 values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex));
304 values.put(LauncherSettings.Favorites.ICON_PACKAGE, c.getString(iconPackageIndex));
305 values.put(LauncherSettings.Favorites.ICON_RESOURCE, c.getString(iconResourceIndex));
306 values.put(LauncherSettings.Favorites.CONTAINER, c.getInt(containerIndex));
307 values.put(LauncherSettings.Favorites.ITEM_TYPE, c.getInt(itemTypeIndex));
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700308 values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800309 values.put(LauncherSettings.Favorites.SCREEN, c.getInt(screenIndex));
310 values.put(LauncherSettings.Favorites.CELLX, c.getInt(cellXIndex));
311 values.put(LauncherSettings.Favorites.CELLY, c.getInt(cellYIndex));
312 values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex));
313 values.put(LauncherSettings.Favorites.DISPLAY_MODE, c.getInt(displayModeIndex));
314 rows[i++] = values;
315 }
316
317 db.beginTransaction();
318 int total = 0;
319 try {
320 int numValues = rows.length;
321 for (i = 0; i < numValues; i++) {
322 if (db.insert(TABLE_FAVORITES, null, rows[i]) < 0) {
323 return 0;
324 } else {
325 total++;
326 }
327 }
328 db.setTransactionSuccessful();
329 } finally {
330 db.endTransaction();
331 }
332
333 return total;
334 }
335
336 @Override
337 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800338 if (LOGD) Log.d(TAG, "onUpgrade triggered");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800339
340 int version = oldVersion;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700341 if (version < 3) {
342 // upgrade 1,2 -> 3 added appWidgetId column
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800343 db.beginTransaction();
344 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700345 // Insert new column for holding appWidgetIds
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800346 db.execSQL("ALTER TABLE favorites " +
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700347 "ADD COLUMN appWidgetId INTEGER NOT NULL DEFAULT -1;");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800348 db.setTransactionSuccessful();
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700349 version = 3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800350 } catch (SQLException ex) {
351 // Old version remains, which means we wipe old data
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800352 Log.e(TAG, ex.getMessage(), ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 } finally {
354 db.endTransaction();
355 }
356
357 // Convert existing widgets only if table upgrade was successful
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700358 if (version == 3) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 convertWidgets(db);
360 }
361 }
Romain Guy73b979d2009-06-09 12:57:21 -0700362
363 if (version < 4) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800364 version = 4;
Romain Guy73b979d2009-06-09 12:57:21 -0700365 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800367 if (version < 5) {
368 // We went from 3 to 5 screens. Move everything 1 to the right
369 db.beginTransaction();
370 try {
371 db.execSQL("UPDATE favorites SET screen=(screen + 1);");
372 db.setTransactionSuccessful();
373 version = 5;
374 } catch (SQLException ex) {
375 // Old version remains, which means we wipe old data
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800376 Log.e(TAG, ex.getMessage(), ex);
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800377 } finally {
378 db.endTransaction();
379 }
380 }
381
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800382 if (version < 6) {
383 if (updateContactsShortcuts(db)) {
384 version = 6;
385 }
386 }
Bjorn Bringert7984c942009-12-09 15:38:25 +0000387
388 if (version < 7) {
389 // Version 7 gets rid of the special search widget.
390 convertWidgets(db);
391 version = 7;
392 }
393
Joe Onorato0589f0f2010-02-08 13:44:00 -0800394 if (version < 8) {
395 // Version 8 (froyo) has the icons all normalized. This should
396 // already be the case in practice, but we now rely on it and don't
397 // resample the images each time.
398 normalizeIcons(db);
399 version = 8;
400 }
401
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800402 if (version != DATABASE_VERSION) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800403 Log.w(TAG, "Destroying all old data.");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800404 db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES);
405 onCreate(db);
406 }
407 }
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800408
409 private boolean updateContactsShortcuts(SQLiteDatabase db) {
410 Cursor c = null;
411 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE,
412 new int[] { Favorites.ITEM_TYPE_SHORTCUT });
413
414 db.beginTransaction();
415 try {
416 // Select and iterate through each matching widget
417 c = db.query(TABLE_FAVORITES, new String[] { Favorites._ID, Favorites.INTENT },
418 selectWhere, null, null, null, null);
419
420 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
421
422 final ContentValues values = new ContentValues();
423 final int idIndex = c.getColumnIndex(Favorites._ID);
424 final int intentIndex = c.getColumnIndex(Favorites.INTENT);
425
426 while (c != null && c.moveToNext()) {
427 long favoriteId = c.getLong(idIndex);
428 final String intentUri = c.getString(intentIndex);
429 if (intentUri != null) {
430 try {
431 Intent intent = Intent.parseUri(intentUri, 0);
432 android.util.Log.d("Home", intent.toString());
433 final Uri uri = intent.getData();
434 final String data = uri.toString();
435 if (Intent.ACTION_VIEW.equals(intent.getAction()) &&
436 (data.startsWith("content://contacts/people/") ||
437 data.startsWith("content://com.android.contacts/contacts/lookup/"))) {
438
439 intent = new Intent("com.android.contacts.action.QUICK_CONTACT");
440 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
441 Intent.FLAG_ACTIVITY_CLEAR_TOP |
442 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
443
444 intent.setData(uri);
445 intent.putExtra("mode", 3);
446 intent.putExtra("exclude_mimes", (String[]) null);
447
448 values.clear();
449 values.put(LauncherSettings.Favorites.INTENT, intent.toUri(0));
450
451 String updateWhere = Favorites._ID + "=" + favoriteId;
452 db.update(TABLE_FAVORITES, values, updateWhere, null);
453 }
454 } catch (RuntimeException ex) {
455 Log.e(TAG, "Problem upgrading shortcut", ex);
456 } catch (URISyntaxException e) {
457 Log.e(TAG, "Problem upgrading shortcut", e);
458 }
459 }
460 }
461
462 db.setTransactionSuccessful();
463 } catch (SQLException ex) {
464 Log.w(TAG, "Problem while upgrading contacts", ex);
465 return false;
466 } finally {
467 db.endTransaction();
468 if (c != null) {
469 c.close();
470 }
471 }
472
473 return true;
474 }
475
Joe Onorato0589f0f2010-02-08 13:44:00 -0800476 private void normalizeIcons(SQLiteDatabase db) {
477 Log.d(TAG, "normalizing icons");
478
Joe Onorato346e1292010-02-18 10:34:24 -0500479 db.beginTransaction();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800480 Cursor c = null;
481 try {
482 boolean logged = false;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800483 final SQLiteStatement update = db.compileStatement("UPDATE favorites "
Jeff Hamiltoneaf77d62010-02-13 00:08:17 -0600484 + "SET icon=? WHERE _id=?");
Joe Onorato0589f0f2010-02-08 13:44:00 -0800485
486 c = db.rawQuery("SELECT _id, icon FROM favorites WHERE iconType=" +
487 Favorites.ICON_TYPE_BITMAP, null);
488
489 final int idIndex = c.getColumnIndexOrThrow(Favorites._ID);
490 final int iconIndex = c.getColumnIndexOrThrow(Favorites.ICON);
491
492 while (c.moveToNext()) {
493 long id = c.getLong(idIndex);
494 byte[] data = c.getBlob(iconIndex);
495 try {
496 Bitmap bitmap = Utilities.resampleIconBitmap(
497 BitmapFactory.decodeByteArray(data, 0, data.length),
498 mContext);
499 if (bitmap != null) {
500 update.bindLong(1, id);
501 data = ItemInfo.flattenBitmap(bitmap);
502 if (data != null) {
503 update.bindBlob(2, data);
504 update.execute();
505 }
506 bitmap.recycle();
507 bitmap = null;
508 }
509 } catch (Exception e) {
510 if (!logged) {
511 Log.e(TAG, "Failed normalizing icon " + id, e);
512 } else {
513 Log.e(TAG, "Also failed normalizing icon " + id);
514 }
515 logged = true;
516 }
517 }
Bjorn Bringert3a928e42010-02-19 11:15:40 +0000518 db.setTransactionSuccessful();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800519 } catch (SQLException ex) {
520 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
521 } finally {
522 db.endTransaction();
523 if (c != null) {
524 c.close();
525 }
526 }
527
528 }
529
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800530 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700531 * Upgrade existing clock and photo frame widgets into their new widget
Bjorn Bringert93c45762009-12-16 13:19:47 +0000532 * equivalents.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800533 */
534 private void convertWidgets(SQLiteDatabase db) {
Bjorn Bringert34251342009-12-15 13:33:11 +0000535 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800536 final int[] bindSources = new int[] {
537 Favorites.ITEM_TYPE_WIDGET_CLOCK,
538 Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME,
Bjorn Bringert7984c942009-12-09 15:38:25 +0000539 Favorites.ITEM_TYPE_WIDGET_SEARCH,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800540 };
Bjorn Bringert7984c942009-12-09 15:38:25 +0000541
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800542 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE, bindSources);
543
544 Cursor c = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800545
546 db.beginTransaction();
547 try {
548 // Select and iterate through each matching widget
Bjorn Bringert7984c942009-12-09 15:38:25 +0000549 c = db.query(TABLE_FAVORITES, new String[] { Favorites._ID, Favorites.ITEM_TYPE },
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800550 selectWhere, null, null, null, null);
551
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800552 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800553
554 final ContentValues values = new ContentValues();
555 while (c != null && c.moveToNext()) {
556 long favoriteId = c.getLong(0);
Bjorn Bringert7984c942009-12-09 15:38:25 +0000557 int favoriteType = c.getInt(1);
558
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700559 // Allocate and update database with new appWidgetId
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700561 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800562
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800563 if (LOGD) {
564 Log.d(TAG, "allocated appWidgetId=" + appWidgetId
565 + " for favoriteId=" + favoriteId);
566 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800567 values.clear();
Bjorn Bringert7984c942009-12-09 15:38:25 +0000568 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
569 values.put(Favorites.APPWIDGET_ID, appWidgetId);
570
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800571 // Original widgets might not have valid spans when upgrading
Bjorn Bringert7984c942009-12-09 15:38:25 +0000572 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
573 values.put(LauncherSettings.Favorites.SPANX, 4);
574 values.put(LauncherSettings.Favorites.SPANY, 1);
575 } else {
576 values.put(LauncherSettings.Favorites.SPANX, 2);
577 values.put(LauncherSettings.Favorites.SPANY, 2);
578 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800579
580 String updateWhere = Favorites._ID + "=" + favoriteId;
581 db.update(TABLE_FAVORITES, values, updateWhere, null);
Bjorn Bringert34251342009-12-15 13:33:11 +0000582
Bjorn Bringert34251342009-12-15 13:33:11 +0000583 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_CLOCK) {
584 appWidgetManager.bindAppWidgetId(appWidgetId,
585 new ComponentName("com.android.alarmclock",
586 "com.android.alarmclock.AnalogAppWidgetProvider"));
587 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME) {
588 appWidgetManager.bindAppWidgetId(appWidgetId,
589 new ComponentName("com.android.camera",
590 "com.android.camera.PhotoAppWidgetProvider"));
591 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
592 appWidgetManager.bindAppWidgetId(appWidgetId,
Bjorn Bringertcd8fec02010-01-14 13:26:43 +0000593 getSearchWidgetProvider());
Bjorn Bringert34251342009-12-15 13:33:11 +0000594 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800595 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800596 Log.e(TAG, "Problem allocating appWidgetId", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800597 }
598 }
599
600 db.setTransactionSuccessful();
601 } catch (SQLException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800602 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800603 } finally {
604 db.endTransaction();
605 if (c != null) {
606 c.close();
607 }
608 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800609 }
610
611 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800612 * Loads the default set of favorite packages from an xml file.
613 *
614 * @param db The database to write the values into
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800615 */
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700616 private int loadFavorites(SQLiteDatabase db) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800617 Intent intent = new Intent(Intent.ACTION_MAIN, null);
618 intent.addCategory(Intent.CATEGORY_LAUNCHER);
619 ContentValues values = new ContentValues();
620
621 PackageManager packageManager = mContext.getPackageManager();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622 int i = 0;
623 try {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700624 XmlResourceParser parser = mContext.getResources().getXml(R.xml.default_workspace);
625 AttributeSet attrs = Xml.asAttributeSet(parser);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 XmlUtils.beginDocument(parser, TAG_FAVORITES);
627
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700628 final int depth = parser.getDepth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800629
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700630 int type;
631 while (((type = parser.next()) != XmlPullParser.END_TAG ||
632 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
633
634 if (type != XmlPullParser.START_TAG) {
635 continue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636 }
637
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700638 boolean added = false;
639 final String name = parser.getName();
640
641 TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.Favorite);
642
643 values.clear();
644 values.put(LauncherSettings.Favorites.CONTAINER,
645 LauncherSettings.Favorites.CONTAINER_DESKTOP);
646 values.put(LauncherSettings.Favorites.SCREEN,
647 a.getString(R.styleable.Favorite_screen));
648 values.put(LauncherSettings.Favorites.CELLX,
649 a.getString(R.styleable.Favorite_x));
650 values.put(LauncherSettings.Favorites.CELLY,
651 a.getString(R.styleable.Favorite_y));
652
653 if (TAG_FAVORITE.equals(name)) {
Mike Cleronb87bd162009-10-30 16:36:56 -0700654 added = addAppShortcut(db, values, a, packageManager, intent);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700655 } else if (TAG_SEARCH.equals(name)) {
656 added = addSearchWidget(db, values);
657 } else if (TAG_CLOCK.equals(name)) {
658 added = addClockWidget(db, values);
Mike Cleronb87bd162009-10-30 16:36:56 -0700659 } else if (TAG_APPWIDGET.equals(name)) {
660 added = addAppWidget(db, values, a);
661 } else if (TAG_SHORTCUT.equals(name)) {
662 added = addUriShortcut(db, values, a);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700664
665 if (added) i++;
666
667 a.recycle();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 }
669 } catch (XmlPullParserException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800670 Log.w(TAG, "Got exception parsing favorites.", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 } catch (IOException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800672 Log.w(TAG, "Got exception parsing favorites.", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700674
675 return i;
676 }
677
Mike Cleronb87bd162009-10-30 16:36:56 -0700678 private boolean addAppShortcut(SQLiteDatabase db, ContentValues values, TypedArray a,
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700679 PackageManager packageManager, Intent intent) {
680
681 ActivityInfo info;
682 String packageName = a.getString(R.styleable.Favorite_packageName);
683 String className = a.getString(R.styleable.Favorite_className);
684 try {
685 ComponentName cn = new ComponentName(packageName, className);
686 info = packageManager.getActivityInfo(cn, 0);
687 intent.setComponent(cn);
Dianne Hackbornbd2e54d2009-03-24 21:00:33 -0700688 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
689 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Romain Guy1ce1a242009-06-23 17:34:54 -0700690 values.put(Favorites.INTENT, intent.toUri(0));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700691 values.put(Favorites.TITLE, info.loadLabel(packageManager).toString());
692 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPLICATION);
693 values.put(Favorites.SPANX, 1);
694 values.put(Favorites.SPANY, 1);
695 db.insert(TABLE_FAVORITES, null, values);
696 } catch (PackageManager.NameNotFoundException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800697 Log.w(TAG, "Unable to add favorite: " + packageName +
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700698 "/" + className, e);
699 return false;
700 }
701 return true;
702 }
703
Bjorn Bringertcd8fec02010-01-14 13:26:43 +0000704 private ComponentName getSearchWidgetProvider() {
705 SearchManager searchManager =
706 (SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE);
707 ComponentName searchComponent = searchManager.getGlobalSearchActivity();
708 if (searchComponent == null) return null;
709 return getProviderInPackage(searchComponent.getPackageName());
710 }
711
712 /**
713 * Gets an appwidget provider from the given package. If the package contains more than
714 * one appwidget provider, an arbitrary one is returned.
715 */
716 private ComponentName getProviderInPackage(String packageName) {
717 AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
718 List<AppWidgetProviderInfo> providers = appWidgetManager.getInstalledProviders();
719 if (providers == null) return null;
720 final int providerCount = providers.size();
721 for (int i = 0; i < providerCount; i++) {
722 ComponentName provider = providers.get(i).provider;
723 if (provider != null && provider.getPackageName().equals(packageName)) {
724 return provider;
725 }
726 }
727 return null;
728 }
729
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700730 private boolean addSearchWidget(SQLiteDatabase db, ContentValues values) {
Bjorn Bringertcd8fec02010-01-14 13:26:43 +0000731 ComponentName cn = getSearchWidgetProvider();
Bjorn Bringert7984c942009-12-09 15:38:25 +0000732 return addAppWidget(db, values, cn, 4, 1);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700733 }
734
735 private boolean addClockWidget(SQLiteDatabase db, ContentValues values) {
Bjorn Bringert34251342009-12-15 13:33:11 +0000736 ComponentName cn = new ComponentName("com.android.alarmclock",
737 "com.android.alarmclock.AnalogAppWidgetProvider");
738 return addAppWidget(db, values, cn, 2, 2);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739 }
Mike Cleronb87bd162009-10-30 16:36:56 -0700740
741 private boolean addAppWidget(SQLiteDatabase db, ContentValues values, TypedArray a) {
Mike Cleronb87bd162009-10-30 16:36:56 -0700742 String packageName = a.getString(R.styleable.Favorite_packageName);
743 String className = a.getString(R.styleable.Favorite_className);
744
745 if (packageName == null || className == null) {
746 return false;
747 }
748
749 ComponentName cn = new ComponentName(packageName, className);
Bjorn Bringert7984c942009-12-09 15:38:25 +0000750 int spanX = a.getInt(R.styleable.Favorite_spanX, 0);
751 int spanY = a.getInt(R.styleable.Favorite_spanY, 0);
752 return addAppWidget(db, values, cn, spanX, spanY);
753 }
754
755 private boolean addAppWidget(SQLiteDatabase db, ContentValues values, ComponentName cn,
756 int spanX, int spanY) {
Mike Cleronb87bd162009-10-30 16:36:56 -0700757 boolean allocatedAppWidgets = false;
758 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
759
760 try {
761 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
762
763 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
Bjorn Bringert7984c942009-12-09 15:38:25 +0000764 values.put(Favorites.SPANX, spanX);
765 values.put(Favorites.SPANY, spanY);
Mike Cleronb87bd162009-10-30 16:36:56 -0700766 values.put(Favorites.APPWIDGET_ID, appWidgetId);
767 db.insert(TABLE_FAVORITES, null, values);
768
769 allocatedAppWidgets = true;
770
771 appWidgetManager.bindAppWidgetId(appWidgetId, cn);
772 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800773 Log.e(TAG, "Problem allocating appWidgetId", ex);
Mike Cleronb87bd162009-10-30 16:36:56 -0700774 }
775
776 return allocatedAppWidgets;
777 }
778
779 private boolean addUriShortcut(SQLiteDatabase db, ContentValues values,
780 TypedArray a) {
781 Resources r = mContext.getResources();
782
783 final int iconResId = a.getResourceId(R.styleable.Favorite_icon, 0);
784 final int titleResId = a.getResourceId(R.styleable.Favorite_title, 0);
785
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800786 Intent intent;
Mike Cleronb87bd162009-10-30 16:36:56 -0700787 String uri = null;
788 try {
789 uri = a.getString(R.styleable.Favorite_uri);
790 intent = Intent.parseUri(uri, 0);
791 } catch (URISyntaxException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800792 Log.w(TAG, "Shortcut has malformed uri: " + uri);
Mike Cleronb87bd162009-10-30 16:36:56 -0700793 return false; // Oh well
794 }
795
796 if (iconResId == 0 || titleResId == 0) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800797 Log.w(TAG, "Shortcut is missing title or icon resource ID");
Mike Cleronb87bd162009-10-30 16:36:56 -0700798 return false;
799 }
800
801 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
802 values.put(Favorites.INTENT, intent.toUri(0));
803 values.put(Favorites.TITLE, r.getString(titleResId));
804 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_SHORTCUT);
805 values.put(Favorites.SPANX, 1);
806 values.put(Favorites.SPANY, 1);
807 values.put(Favorites.ICON_TYPE, Favorites.ICON_TYPE_RESOURCE);
808 values.put(Favorites.ICON_PACKAGE, mContext.getPackageName());
809 values.put(Favorites.ICON_RESOURCE, r.getResourceName(iconResId));
810
811 db.insert(TABLE_FAVORITES, null, values);
812
813 return true;
814 }
815 }
816
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817 /**
818 * Build a query string that will match any row where the column matches
819 * anything in the values list.
820 */
821 static String buildOrWhereString(String column, int[] values) {
822 StringBuilder selectWhere = new StringBuilder();
823 for (int i = values.length - 1; i >= 0; i--) {
824 selectWhere.append(column).append("=").append(values[i]);
825 if (i > 0) {
826 selectWhere.append(" OR ");
827 }
828 }
829 return selectWhere.toString();
830 }
831
832 static class SqlArguments {
833 public final String table;
834 public final String where;
835 public final String[] args;
836
837 SqlArguments(Uri url, String where, String[] args) {
838 if (url.getPathSegments().size() == 1) {
839 this.table = url.getPathSegments().get(0);
840 this.where = where;
841 this.args = args;
842 } else if (url.getPathSegments().size() != 2) {
843 throw new IllegalArgumentException("Invalid URI: " + url);
844 } else if (!TextUtils.isEmpty(where)) {
845 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
846 } else {
847 this.table = url.getPathSegments().get(0);
848 this.where = "_id=" + ContentUris.parseId(url);
849 this.args = null;
850 }
851 }
852
853 SqlArguments(Uri url) {
854 if (url.getPathSegments().size() == 1) {
855 table = url.getPathSegments().get(0);
856 where = null;
857 args = null;
858 } else {
859 throw new IllegalArgumentException("Invalid URI: " + url);
860 }
861 }
862 }
863}