blob: ae343fc9f66fef24b22df44d785a6b0232e4ee2d [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;
48import android.os.*;
49import android.provider.Settings;
50
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import java.io.IOException;
Mike Cleronb87bd162009-10-30 16:36:56 -070052import java.net.URISyntaxException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import java.util.ArrayList;
Bjorn Bringertcd8fec02010-01-14 13:26:43 +000054import java.util.List;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import org.xmlpull.v1.XmlPullParserException;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070057import org.xmlpull.v1.XmlPullParser;
Tom Taylorfd1e7572009-12-21 14:00:50 -080058import com.android.common.XmlUtils;
Joe Onoratoa5902522009-07-30 13:37:37 -070059import com.android.launcher2.LauncherSettings.Favorites;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060
61public class LauncherProvider extends ContentProvider {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080062 private static final String TAG = "Launcher.LauncherProvider";
63 private static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064
65 private static final String DATABASE_NAME = "launcher.db";
66
Joe Onorato0589f0f2010-02-08 13:44:00 -080067 private static final int DATABASE_VERSION = 8;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068
Joe Onoratoa5902522009-07-30 13:37:37 -070069 static final String AUTHORITY = "com.android.launcher2.settings";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070
Joe Onoratoa5902522009-07-30 13:37:37 -070071 static final String EXTRA_BIND_SOURCES = "com.android.launcher2.settings.bindsources";
72 static final String EXTRA_BIND_TARGETS = "com.android.launcher2.settings.bindtargets";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073
74 static final String TABLE_FAVORITES = "favorites";
75 static final String PARAMETER_NOTIFY = "notify";
76
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -070077 /**
Romain Guy73b979d2009-06-09 12:57:21 -070078 * {@link Uri} triggered at any registered {@link android.database.ContentObserver} when
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -070079 * {@link AppWidgetHost#deleteHost()} is called during database creation.
80 * Use this to recall {@link AppWidgetHost#startListening()} if needed.
81 */
82 static final Uri CONTENT_APPWIDGET_RESET_URI =
83 Uri.parse("content://" + AUTHORITY + "/appWidgetReset");
84
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085 private SQLiteOpenHelper mOpenHelper;
86
87 @Override
88 public boolean onCreate() {
89 mOpenHelper = new DatabaseHelper(getContext());
90 return true;
91 }
92
93 @Override
94 public String getType(Uri uri) {
95 SqlArguments args = new SqlArguments(uri, null, null);
96 if (TextUtils.isEmpty(args.where)) {
97 return "vnd.android.cursor.dir/" + args.table;
98 } else {
99 return "vnd.android.cursor.item/" + args.table;
100 }
101 }
102
103 @Override
104 public Cursor query(Uri uri, String[] projection, String selection,
105 String[] selectionArgs, String sortOrder) {
106
107 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
108 SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
109 qb.setTables(args.table);
110
Romain Guy73b979d2009-06-09 12:57:21 -0700111 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112 Cursor result = qb.query(db, projection, args.where, args.args, null, null, sortOrder);
113 result.setNotificationUri(getContext().getContentResolver(), uri);
114
115 return result;
116 }
117
118 @Override
119 public Uri insert(Uri uri, ContentValues initialValues) {
120 SqlArguments args = new SqlArguments(uri);
121
122 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
123 final long rowId = db.insert(args.table, null, initialValues);
124 if (rowId <= 0) return null;
125
126 uri = ContentUris.withAppendedId(uri, rowId);
127 sendNotify(uri);
128
129 return uri;
130 }
131
132 @Override
133 public int bulkInsert(Uri uri, ContentValues[] values) {
134 SqlArguments args = new SqlArguments(uri);
135
136 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
137 db.beginTransaction();
138 try {
139 int numValues = values.length;
140 for (int i = 0; i < numValues; i++) {
141 if (db.insert(args.table, null, values[i]) < 0) return 0;
142 }
143 db.setTransactionSuccessful();
144 } finally {
145 db.endTransaction();
146 }
147
148 sendNotify(uri);
149 return values.length;
150 }
151
152 @Override
153 public int delete(Uri uri, String selection, String[] selectionArgs) {
154 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
155
156 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
157 int count = db.delete(args.table, args.where, args.args);
158 if (count > 0) sendNotify(uri);
159
160 return count;
161 }
162
163 @Override
164 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
165 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
166
167 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
168 int count = db.update(args.table, values, args.where, args.args);
169 if (count > 0) sendNotify(uri);
170
171 return count;
172 }
173
174 private void sendNotify(Uri uri) {
175 String notify = uri.getQueryParameter(PARAMETER_NOTIFY);
176 if (notify == null || "true".equals(notify)) {
177 getContext().getContentResolver().notifyChange(uri, null);
178 }
179 }
180
181 private static class DatabaseHelper extends SQLiteOpenHelper {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 private static final String TAG_FAVORITES = "favorites";
183 private static final String TAG_FAVORITE = "favorite";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700184 private static final String TAG_CLOCK = "clock";
185 private static final String TAG_SEARCH = "search";
Mike Cleronb87bd162009-10-30 16:36:56 -0700186 private static final String TAG_APPWIDGET = "appwidget";
187 private static final String TAG_SHORTCUT = "shortcut";
188
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 private final Context mContext;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700190 private final AppWidgetHost mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191
192 DatabaseHelper(Context context) {
193 super(context, DATABASE_NAME, null, DATABASE_VERSION);
194 mContext = context;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700195 mAppWidgetHost = new AppWidgetHost(context, Launcher.APPWIDGET_HOST_ID);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196 }
197
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700198 /**
199 * Send notification that we've deleted the {@link AppWidgetHost},
200 * probably as part of the initial database creation. The receiver may
201 * want to re-call {@link AppWidgetHost#startListening()} to ensure
202 * callbacks are correctly set.
203 */
204 private void sendAppWidgetResetNotify() {
205 final ContentResolver resolver = mContext.getContentResolver();
206 resolver.notifyChange(CONTENT_APPWIDGET_RESET_URI, null);
207 }
208
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209 @Override
210 public void onCreate(SQLiteDatabase db) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800211 if (LOGD) Log.d(TAG, "creating new launcher database");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212
213 db.execSQL("CREATE TABLE favorites (" +
214 "_id INTEGER PRIMARY KEY," +
215 "title TEXT," +
216 "intent TEXT," +
217 "container INTEGER," +
218 "screen INTEGER," +
219 "cellX INTEGER," +
220 "cellY INTEGER," +
221 "spanX INTEGER," +
222 "spanY INTEGER," +
223 "itemType INTEGER," +
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700224 "appWidgetId INTEGER NOT NULL DEFAULT -1," +
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225 "isShortcut INTEGER," +
226 "iconType INTEGER," +
227 "iconPackage TEXT," +
228 "iconResource TEXT," +
229 "icon BLOB," +
230 "uri TEXT," +
231 "displayMode INTEGER" +
232 ");");
233
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700234 // Database was just created, so wipe any previous widgets
235 if (mAppWidgetHost != null) {
236 mAppWidgetHost.deleteHost();
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700237 sendAppWidgetResetNotify();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800238 }
239
240 if (!convertDatabase(db)) {
241 // Populate favorites table with initial favorites
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700242 loadFavorites(db);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243 }
244 }
245
246 private boolean convertDatabase(SQLiteDatabase db) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800247 if (LOGD) Log.d(TAG, "converting database from an older format, but not onUpgrade");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248 boolean converted = false;
249
250 final Uri uri = Uri.parse("content://" + Settings.AUTHORITY +
251 "/old_favorites?notify=true");
252 final ContentResolver resolver = mContext.getContentResolver();
253 Cursor cursor = null;
254
255 try {
256 cursor = resolver.query(uri, null, null, null, null);
257 } catch (Exception e) {
258 // Ignore
259 }
260
261 // We already have a favorites database in the old provider
262 if (cursor != null && cursor.getCount() > 0) {
263 try {
264 converted = copyFromCursor(db, cursor) > 0;
265 } finally {
266 cursor.close();
267 }
268
269 if (converted) {
270 resolver.delete(uri, null, null);
271 }
272 }
273
274 if (converted) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700275 // Convert widgets from this import into widgets
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800276 if (LOGD) Log.d(TAG, "converted and now triggering widget upgrade");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800277 convertWidgets(db);
278 }
279
280 return converted;
281 }
282
283 private int copyFromCursor(SQLiteDatabase db, Cursor c) {
Romain Guy73b979d2009-06-09 12:57:21 -0700284 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285 final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
286 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
287 final int iconTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
288 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
289 final int iconPackageIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
290 final int iconResourceIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
291 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
292 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
293 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
294 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
295 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
296 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
297 final int displayModeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
298
299 ContentValues[] rows = new ContentValues[c.getCount()];
300 int i = 0;
301 while (c.moveToNext()) {
302 ContentValues values = new ContentValues(c.getColumnCount());
Romain Guy73b979d2009-06-09 12:57:21 -0700303 values.put(LauncherSettings.Favorites._ID, c.getLong(idIndex));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800304 values.put(LauncherSettings.Favorites.INTENT, c.getString(intentIndex));
305 values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex));
306 values.put(LauncherSettings.Favorites.ICON_TYPE, c.getInt(iconTypeIndex));
307 values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex));
308 values.put(LauncherSettings.Favorites.ICON_PACKAGE, c.getString(iconPackageIndex));
309 values.put(LauncherSettings.Favorites.ICON_RESOURCE, c.getString(iconResourceIndex));
310 values.put(LauncherSettings.Favorites.CONTAINER, c.getInt(containerIndex));
311 values.put(LauncherSettings.Favorites.ITEM_TYPE, c.getInt(itemTypeIndex));
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700312 values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800313 values.put(LauncherSettings.Favorites.SCREEN, c.getInt(screenIndex));
314 values.put(LauncherSettings.Favorites.CELLX, c.getInt(cellXIndex));
315 values.put(LauncherSettings.Favorites.CELLY, c.getInt(cellYIndex));
316 values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex));
317 values.put(LauncherSettings.Favorites.DISPLAY_MODE, c.getInt(displayModeIndex));
318 rows[i++] = values;
319 }
320
321 db.beginTransaction();
322 int total = 0;
323 try {
324 int numValues = rows.length;
325 for (i = 0; i < numValues; i++) {
326 if (db.insert(TABLE_FAVORITES, null, rows[i]) < 0) {
327 return 0;
328 } else {
329 total++;
330 }
331 }
332 db.setTransactionSuccessful();
333 } finally {
334 db.endTransaction();
335 }
336
337 return total;
338 }
339
340 @Override
341 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800342 if (LOGD) Log.d(TAG, "onUpgrade triggered");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800343
344 int version = oldVersion;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700345 if (version < 3) {
346 // upgrade 1,2 -> 3 added appWidgetId column
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800347 db.beginTransaction();
348 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700349 // Insert new column for holding appWidgetIds
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800350 db.execSQL("ALTER TABLE favorites " +
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700351 "ADD COLUMN appWidgetId INTEGER NOT NULL DEFAULT -1;");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800352 db.setTransactionSuccessful();
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700353 version = 3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 } catch (SQLException ex) {
355 // Old version remains, which means we wipe old data
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800356 Log.e(TAG, ex.getMessage(), ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 } finally {
358 db.endTransaction();
359 }
360
361 // Convert existing widgets only if table upgrade was successful
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700362 if (version == 3) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363 convertWidgets(db);
364 }
365 }
Romain Guy73b979d2009-06-09 12:57:21 -0700366
367 if (version < 4) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800368 version = 4;
Romain Guy73b979d2009-06-09 12:57:21 -0700369 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800371 if (version < 5) {
372 // We went from 3 to 5 screens. Move everything 1 to the right
373 db.beginTransaction();
374 try {
375 db.execSQL("UPDATE favorites SET screen=(screen + 1);");
376 db.setTransactionSuccessful();
377 version = 5;
378 } catch (SQLException ex) {
379 // Old version remains, which means we wipe old data
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800380 Log.e(TAG, ex.getMessage(), ex);
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800381 } finally {
382 db.endTransaction();
383 }
384 }
385
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800386 if (version < 6) {
387 if (updateContactsShortcuts(db)) {
388 version = 6;
389 }
390 }
Bjorn Bringert7984c942009-12-09 15:38:25 +0000391
392 if (version < 7) {
393 // Version 7 gets rid of the special search widget.
394 convertWidgets(db);
395 version = 7;
396 }
397
Joe Onorato0589f0f2010-02-08 13:44:00 -0800398 if (version < 8) {
399 // Version 8 (froyo) has the icons all normalized. This should
400 // already be the case in practice, but we now rely on it and don't
401 // resample the images each time.
402 normalizeIcons(db);
403 version = 8;
404 }
405
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 if (version != DATABASE_VERSION) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800407 Log.w(TAG, "Destroying all old data.");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES);
409 onCreate(db);
410 }
411 }
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800412
413 private boolean updateContactsShortcuts(SQLiteDatabase db) {
414 Cursor c = null;
415 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE,
416 new int[] { Favorites.ITEM_TYPE_SHORTCUT });
417
418 db.beginTransaction();
419 try {
420 // Select and iterate through each matching widget
421 c = db.query(TABLE_FAVORITES, new String[] { Favorites._ID, Favorites.INTENT },
422 selectWhere, null, null, null, null);
423
424 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
425
426 final ContentValues values = new ContentValues();
427 final int idIndex = c.getColumnIndex(Favorites._ID);
428 final int intentIndex = c.getColumnIndex(Favorites.INTENT);
429
430 while (c != null && c.moveToNext()) {
431 long favoriteId = c.getLong(idIndex);
432 final String intentUri = c.getString(intentIndex);
433 if (intentUri != null) {
434 try {
435 Intent intent = Intent.parseUri(intentUri, 0);
436 android.util.Log.d("Home", intent.toString());
437 final Uri uri = intent.getData();
438 final String data = uri.toString();
439 if (Intent.ACTION_VIEW.equals(intent.getAction()) &&
440 (data.startsWith("content://contacts/people/") ||
441 data.startsWith("content://com.android.contacts/contacts/lookup/"))) {
442
443 intent = new Intent("com.android.contacts.action.QUICK_CONTACT");
444 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
445 Intent.FLAG_ACTIVITY_CLEAR_TOP |
446 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
447
448 intent.setData(uri);
449 intent.putExtra("mode", 3);
450 intent.putExtra("exclude_mimes", (String[]) null);
451
452 values.clear();
453 values.put(LauncherSettings.Favorites.INTENT, intent.toUri(0));
454
455 String updateWhere = Favorites._ID + "=" + favoriteId;
456 db.update(TABLE_FAVORITES, values, updateWhere, null);
457 }
458 } catch (RuntimeException ex) {
459 Log.e(TAG, "Problem upgrading shortcut", ex);
460 } catch (URISyntaxException e) {
461 Log.e(TAG, "Problem upgrading shortcut", e);
462 }
463 }
464 }
465
466 db.setTransactionSuccessful();
467 } catch (SQLException ex) {
468 Log.w(TAG, "Problem while upgrading contacts", ex);
469 return false;
470 } finally {
471 db.endTransaction();
472 if (c != null) {
473 c.close();
474 }
475 }
476
477 return true;
478 }
479
Joe Onorato0589f0f2010-02-08 13:44:00 -0800480 private void normalizeIcons(SQLiteDatabase db) {
481 Log.d(TAG, "normalizing icons");
482
483 Cursor c = null;
484 try {
485 boolean logged = false;
486 final ContentValues values = new ContentValues();
487 final ContentResolver cr = mContext.getContentResolver();
488 final SQLiteStatement update = db.compileStatement("UPDATE favorites "
Jeff Hamiltoneaf77d62010-02-13 00:08:17 -0600489 + "SET icon=? WHERE _id=?");
Joe Onorato0589f0f2010-02-08 13:44:00 -0800490
491 c = db.rawQuery("SELECT _id, icon FROM favorites WHERE iconType=" +
492 Favorites.ICON_TYPE_BITMAP, null);
493
494 final int idIndex = c.getColumnIndexOrThrow(Favorites._ID);
495 final int iconIndex = c.getColumnIndexOrThrow(Favorites.ICON);
496
497 while (c.moveToNext()) {
498 long id = c.getLong(idIndex);
499 byte[] data = c.getBlob(iconIndex);
500 try {
501 Bitmap bitmap = Utilities.resampleIconBitmap(
502 BitmapFactory.decodeByteArray(data, 0, data.length),
503 mContext);
504 if (bitmap != null) {
505 update.bindLong(1, id);
506 data = ItemInfo.flattenBitmap(bitmap);
507 if (data != null) {
508 update.bindBlob(2, data);
509 update.execute();
510 }
511 bitmap.recycle();
512 bitmap = null;
513 }
514 } catch (Exception e) {
515 if (!logged) {
516 Log.e(TAG, "Failed normalizing icon " + id, e);
517 } else {
518 Log.e(TAG, "Also failed normalizing icon " + id);
519 }
520 logged = true;
521 }
522 }
523 } catch (SQLException ex) {
524 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
525 } finally {
526 db.endTransaction();
527 if (c != null) {
528 c.close();
529 }
530 }
531
532 }
533
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800534 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700535 * Upgrade existing clock and photo frame widgets into their new widget
Bjorn Bringert93c45762009-12-16 13:19:47 +0000536 * equivalents.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800537 */
538 private void convertWidgets(SQLiteDatabase db) {
Bjorn Bringert34251342009-12-15 13:33:11 +0000539 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800540 final int[] bindSources = new int[] {
541 Favorites.ITEM_TYPE_WIDGET_CLOCK,
542 Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME,
Bjorn Bringert7984c942009-12-09 15:38:25 +0000543 Favorites.ITEM_TYPE_WIDGET_SEARCH,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800544 };
Bjorn Bringert7984c942009-12-09 15:38:25 +0000545
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800546 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE, bindSources);
547
548 Cursor c = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800549
550 db.beginTransaction();
551 try {
552 // Select and iterate through each matching widget
Bjorn Bringert7984c942009-12-09 15:38:25 +0000553 c = db.query(TABLE_FAVORITES, new String[] { Favorites._ID, Favorites.ITEM_TYPE },
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800554 selectWhere, null, null, null, null);
555
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800556 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800557
558 final ContentValues values = new ContentValues();
559 while (c != null && c.moveToNext()) {
560 long favoriteId = c.getLong(0);
Bjorn Bringert7984c942009-12-09 15:38:25 +0000561 int favoriteType = c.getInt(1);
562
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700563 // Allocate and update database with new appWidgetId
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800564 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700565 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800566
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800567 if (LOGD) {
568 Log.d(TAG, "allocated appWidgetId=" + appWidgetId
569 + " for favoriteId=" + favoriteId);
570 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800571 values.clear();
Bjorn Bringert7984c942009-12-09 15:38:25 +0000572 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
573 values.put(Favorites.APPWIDGET_ID, appWidgetId);
574
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800575 // Original widgets might not have valid spans when upgrading
Bjorn Bringert7984c942009-12-09 15:38:25 +0000576 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
577 values.put(LauncherSettings.Favorites.SPANX, 4);
578 values.put(LauncherSettings.Favorites.SPANY, 1);
579 } else {
580 values.put(LauncherSettings.Favorites.SPANX, 2);
581 values.put(LauncherSettings.Favorites.SPANY, 2);
582 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800583
584 String updateWhere = Favorites._ID + "=" + favoriteId;
585 db.update(TABLE_FAVORITES, values, updateWhere, null);
Bjorn Bringert34251342009-12-15 13:33:11 +0000586
587 ComponentName cn = null;
588 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_CLOCK) {
589 appWidgetManager.bindAppWidgetId(appWidgetId,
590 new ComponentName("com.android.alarmclock",
591 "com.android.alarmclock.AnalogAppWidgetProvider"));
592 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME) {
593 appWidgetManager.bindAppWidgetId(appWidgetId,
594 new ComponentName("com.android.camera",
595 "com.android.camera.PhotoAppWidgetProvider"));
596 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
597 appWidgetManager.bindAppWidgetId(appWidgetId,
Bjorn Bringertcd8fec02010-01-14 13:26:43 +0000598 getSearchWidgetProvider());
Bjorn Bringert34251342009-12-15 13:33:11 +0000599 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800600 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800601 Log.e(TAG, "Problem allocating appWidgetId", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800602 }
603 }
604
605 db.setTransactionSuccessful();
606 } catch (SQLException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800607 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800608 } finally {
609 db.endTransaction();
610 if (c != null) {
611 c.close();
612 }
613 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800614 }
615
616 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800617 * Loads the default set of favorite packages from an xml file.
618 *
619 * @param db The database to write the values into
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800620 */
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700621 private int loadFavorites(SQLiteDatabase db) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622 Intent intent = new Intent(Intent.ACTION_MAIN, null);
623 intent.addCategory(Intent.CATEGORY_LAUNCHER);
624 ContentValues values = new ContentValues();
625
626 PackageManager packageManager = mContext.getPackageManager();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800627 int i = 0;
628 try {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700629 XmlResourceParser parser = mContext.getResources().getXml(R.xml.default_workspace);
630 AttributeSet attrs = Xml.asAttributeSet(parser);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631 XmlUtils.beginDocument(parser, TAG_FAVORITES);
632
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700633 final int depth = parser.getDepth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700635 int type;
636 while (((type = parser.next()) != XmlPullParser.END_TAG ||
637 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
638
639 if (type != XmlPullParser.START_TAG) {
640 continue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641 }
642
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700643 boolean added = false;
644 final String name = parser.getName();
645
646 TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.Favorite);
647
648 values.clear();
649 values.put(LauncherSettings.Favorites.CONTAINER,
650 LauncherSettings.Favorites.CONTAINER_DESKTOP);
651 values.put(LauncherSettings.Favorites.SCREEN,
652 a.getString(R.styleable.Favorite_screen));
653 values.put(LauncherSettings.Favorites.CELLX,
654 a.getString(R.styleable.Favorite_x));
655 values.put(LauncherSettings.Favorites.CELLY,
656 a.getString(R.styleable.Favorite_y));
657
658 if (TAG_FAVORITE.equals(name)) {
Mike Cleronb87bd162009-10-30 16:36:56 -0700659 added = addAppShortcut(db, values, a, packageManager, intent);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700660 } else if (TAG_SEARCH.equals(name)) {
661 added = addSearchWidget(db, values);
662 } else if (TAG_CLOCK.equals(name)) {
663 added = addClockWidget(db, values);
Mike Cleronb87bd162009-10-30 16:36:56 -0700664 } else if (TAG_APPWIDGET.equals(name)) {
665 added = addAppWidget(db, values, a);
666 } else if (TAG_SHORTCUT.equals(name)) {
667 added = addUriShortcut(db, values, a);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700669
670 if (added) i++;
671
672 a.recycle();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 }
674 } catch (XmlPullParserException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800675 Log.w(TAG, "Got exception parsing favorites.", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 } catch (IOException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800677 Log.w(TAG, "Got exception parsing favorites.", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800678 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700679
680 return i;
681 }
682
Mike Cleronb87bd162009-10-30 16:36:56 -0700683 private boolean addAppShortcut(SQLiteDatabase db, ContentValues values, TypedArray a,
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700684 PackageManager packageManager, Intent intent) {
685
686 ActivityInfo info;
687 String packageName = a.getString(R.styleable.Favorite_packageName);
688 String className = a.getString(R.styleable.Favorite_className);
689 try {
690 ComponentName cn = new ComponentName(packageName, className);
691 info = packageManager.getActivityInfo(cn, 0);
692 intent.setComponent(cn);
Dianne Hackbornbd2e54d2009-03-24 21:00:33 -0700693 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
694 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Romain Guy1ce1a242009-06-23 17:34:54 -0700695 values.put(Favorites.INTENT, intent.toUri(0));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700696 values.put(Favorites.TITLE, info.loadLabel(packageManager).toString());
697 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPLICATION);
698 values.put(Favorites.SPANX, 1);
699 values.put(Favorites.SPANY, 1);
700 db.insert(TABLE_FAVORITES, null, values);
701 } catch (PackageManager.NameNotFoundException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800702 Log.w(TAG, "Unable to add favorite: " + packageName +
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700703 "/" + className, e);
704 return false;
705 }
706 return true;
707 }
708
Bjorn Bringertcd8fec02010-01-14 13:26:43 +0000709 private ComponentName getSearchWidgetProvider() {
710 SearchManager searchManager =
711 (SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE);
712 ComponentName searchComponent = searchManager.getGlobalSearchActivity();
713 if (searchComponent == null) return null;
714 return getProviderInPackage(searchComponent.getPackageName());
715 }
716
717 /**
718 * Gets an appwidget provider from the given package. If the package contains more than
719 * one appwidget provider, an arbitrary one is returned.
720 */
721 private ComponentName getProviderInPackage(String packageName) {
722 AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
723 List<AppWidgetProviderInfo> providers = appWidgetManager.getInstalledProviders();
724 if (providers == null) return null;
725 final int providerCount = providers.size();
726 for (int i = 0; i < providerCount; i++) {
727 ComponentName provider = providers.get(i).provider;
728 if (provider != null && provider.getPackageName().equals(packageName)) {
729 return provider;
730 }
731 }
732 return null;
733 }
734
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700735 private boolean addSearchWidget(SQLiteDatabase db, ContentValues values) {
Bjorn Bringertcd8fec02010-01-14 13:26:43 +0000736 ComponentName cn = getSearchWidgetProvider();
Bjorn Bringert7984c942009-12-09 15:38:25 +0000737 return addAppWidget(db, values, cn, 4, 1);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700738 }
739
740 private boolean addClockWidget(SQLiteDatabase db, ContentValues values) {
Bjorn Bringert34251342009-12-15 13:33:11 +0000741 ComponentName cn = new ComponentName("com.android.alarmclock",
742 "com.android.alarmclock.AnalogAppWidgetProvider");
743 return addAppWidget(db, values, cn, 2, 2);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744 }
Mike Cleronb87bd162009-10-30 16:36:56 -0700745
746 private boolean addAppWidget(SQLiteDatabase db, ContentValues values, TypedArray a) {
Mike Cleronb87bd162009-10-30 16:36:56 -0700747 String packageName = a.getString(R.styleable.Favorite_packageName);
748 String className = a.getString(R.styleable.Favorite_className);
749
750 if (packageName == null || className == null) {
751 return false;
752 }
753
754 ComponentName cn = new ComponentName(packageName, className);
Bjorn Bringert7984c942009-12-09 15:38:25 +0000755 int spanX = a.getInt(R.styleable.Favorite_spanX, 0);
756 int spanY = a.getInt(R.styleable.Favorite_spanY, 0);
757 return addAppWidget(db, values, cn, spanX, spanY);
758 }
759
760 private boolean addAppWidget(SQLiteDatabase db, ContentValues values, ComponentName cn,
761 int spanX, int spanY) {
Mike Cleronb87bd162009-10-30 16:36:56 -0700762 boolean allocatedAppWidgets = false;
763 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
764
765 try {
766 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
767
768 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
Bjorn Bringert7984c942009-12-09 15:38:25 +0000769 values.put(Favorites.SPANX, spanX);
770 values.put(Favorites.SPANY, spanY);
Mike Cleronb87bd162009-10-30 16:36:56 -0700771 values.put(Favorites.APPWIDGET_ID, appWidgetId);
772 db.insert(TABLE_FAVORITES, null, values);
773
774 allocatedAppWidgets = true;
775
776 appWidgetManager.bindAppWidgetId(appWidgetId, cn);
777 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800778 Log.e(TAG, "Problem allocating appWidgetId", ex);
Mike Cleronb87bd162009-10-30 16:36:56 -0700779 }
780
781 return allocatedAppWidgets;
782 }
783
784 private boolean addUriShortcut(SQLiteDatabase db, ContentValues values,
785 TypedArray a) {
786 Resources r = mContext.getResources();
787
788 final int iconResId = a.getResourceId(R.styleable.Favorite_icon, 0);
789 final int titleResId = a.getResourceId(R.styleable.Favorite_title, 0);
790
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800791 Intent intent;
Mike Cleronb87bd162009-10-30 16:36:56 -0700792 String uri = null;
793 try {
794 uri = a.getString(R.styleable.Favorite_uri);
795 intent = Intent.parseUri(uri, 0);
796 } catch (URISyntaxException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800797 Log.w(TAG, "Shortcut has malformed uri: " + uri);
Mike Cleronb87bd162009-10-30 16:36:56 -0700798 return false; // Oh well
799 }
800
801 if (iconResId == 0 || titleResId == 0) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800802 Log.w(TAG, "Shortcut is missing title or icon resource ID");
Mike Cleronb87bd162009-10-30 16:36:56 -0700803 return false;
804 }
805
806 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
807 values.put(Favorites.INTENT, intent.toUri(0));
808 values.put(Favorites.TITLE, r.getString(titleResId));
809 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_SHORTCUT);
810 values.put(Favorites.SPANX, 1);
811 values.put(Favorites.SPANY, 1);
812 values.put(Favorites.ICON_TYPE, Favorites.ICON_TYPE_RESOURCE);
813 values.put(Favorites.ICON_PACKAGE, mContext.getPackageName());
814 values.put(Favorites.ICON_RESOURCE, r.getResourceName(iconResId));
815
816 db.insert(TABLE_FAVORITES, null, values);
817
818 return true;
819 }
820 }
821
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822 /**
823 * Build a query string that will match any row where the column matches
824 * anything in the values list.
825 */
826 static String buildOrWhereString(String column, int[] values) {
827 StringBuilder selectWhere = new StringBuilder();
828 for (int i = values.length - 1; i >= 0; i--) {
829 selectWhere.append(column).append("=").append(values[i]);
830 if (i > 0) {
831 selectWhere.append(" OR ");
832 }
833 }
834 return selectWhere.toString();
835 }
836
837 static class SqlArguments {
838 public final String table;
839 public final String where;
840 public final String[] args;
841
842 SqlArguments(Uri url, String where, String[] args) {
843 if (url.getPathSegments().size() == 1) {
844 this.table = url.getPathSegments().get(0);
845 this.where = where;
846 this.args = args;
847 } else if (url.getPathSegments().size() != 2) {
848 throw new IllegalArgumentException("Invalid URI: " + url);
849 } else if (!TextUtils.isEmpty(where)) {
850 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
851 } else {
852 this.table = url.getPathSegments().get(0);
853 this.where = "_id=" + ContentUris.parseId(url);
854 this.args = null;
855 }
856 }
857
858 SqlArguments(Uri url) {
859 if (url.getPathSegments().size() == 1) {
860 table = url.getPathSegments().get(0);
861 where = null;
862 args = null;
863 } else {
864 throw new IllegalArgumentException("Invalid URI: " + url);
865 }
866 }
867 }
868}