blob: 14c83b3e3bf1f1e155ca45610fb088e1c153447b [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;
Dianne Hackborn094fc7a2010-02-24 20:01:46 -080058
59import com.android.internal.util.XmlUtils;
Joe Onoratoa5902522009-07-30 13:37:37 -070060import com.android.launcher2.LauncherSettings.Favorites;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061
62public class LauncherProvider extends ContentProvider {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080063 private static final String TAG = "Launcher.LauncherProvider";
64 private static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065
66 private static final String DATABASE_NAME = "launcher.db";
67
Joe Onorato0589f0f2010-02-08 13:44:00 -080068 private static final int DATABASE_VERSION = 8;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069
Joe Onoratoa5902522009-07-30 13:37:37 -070070 static final String AUTHORITY = "com.android.launcher2.settings";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071
Joe Onoratoa5902522009-07-30 13:37:37 -070072 static final String EXTRA_BIND_SOURCES = "com.android.launcher2.settings.bindsources";
73 static final String EXTRA_BIND_TARGETS = "com.android.launcher2.settings.bindtargets";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074
75 static final String TABLE_FAVORITES = "favorites";
76 static final String PARAMETER_NOTIFY = "notify";
77
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -070078 /**
Romain Guy73b979d2009-06-09 12:57:21 -070079 * {@link Uri} triggered at any registered {@link android.database.ContentObserver} when
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -070080 * {@link AppWidgetHost#deleteHost()} is called during database creation.
81 * Use this to recall {@link AppWidgetHost#startListening()} if needed.
82 */
83 static final Uri CONTENT_APPWIDGET_RESET_URI =
84 Uri.parse("content://" + AUTHORITY + "/appWidgetReset");
85
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 private SQLiteOpenHelper mOpenHelper;
87
88 @Override
89 public boolean onCreate() {
90 mOpenHelper = new DatabaseHelper(getContext());
91 return true;
92 }
93
94 @Override
95 public String getType(Uri uri) {
96 SqlArguments args = new SqlArguments(uri, null, null);
97 if (TextUtils.isEmpty(args.where)) {
98 return "vnd.android.cursor.dir/" + args.table;
99 } else {
100 return "vnd.android.cursor.item/" + args.table;
101 }
102 }
103
104 @Override
105 public Cursor query(Uri uri, String[] projection, String selection,
106 String[] selectionArgs, String sortOrder) {
107
108 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
109 SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
110 qb.setTables(args.table);
111
Romain Guy73b979d2009-06-09 12:57:21 -0700112 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113 Cursor result = qb.query(db, projection, args.where, args.args, null, null, sortOrder);
114 result.setNotificationUri(getContext().getContentResolver(), uri);
115
116 return result;
117 }
118
119 @Override
120 public Uri insert(Uri uri, ContentValues initialValues) {
121 SqlArguments args = new SqlArguments(uri);
122
123 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
124 final long rowId = db.insert(args.table, null, initialValues);
125 if (rowId <= 0) return null;
126
127 uri = ContentUris.withAppendedId(uri, rowId);
128 sendNotify(uri);
129
130 return uri;
131 }
132
133 @Override
134 public int bulkInsert(Uri uri, ContentValues[] values) {
135 SqlArguments args = new SqlArguments(uri);
136
137 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
138 db.beginTransaction();
139 try {
140 int numValues = values.length;
141 for (int i = 0; i < numValues; i++) {
142 if (db.insert(args.table, null, values[i]) < 0) return 0;
143 }
144 db.setTransactionSuccessful();
145 } finally {
146 db.endTransaction();
147 }
148
149 sendNotify(uri);
150 return values.length;
151 }
152
153 @Override
154 public int delete(Uri uri, String selection, String[] selectionArgs) {
155 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
156
157 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
158 int count = db.delete(args.table, args.where, args.args);
159 if (count > 0) sendNotify(uri);
160
161 return count;
162 }
163
164 @Override
165 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
166 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
167
168 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
169 int count = db.update(args.table, values, args.where, args.args);
170 if (count > 0) sendNotify(uri);
171
172 return count;
173 }
174
175 private void sendNotify(Uri uri) {
176 String notify = uri.getQueryParameter(PARAMETER_NOTIFY);
177 if (notify == null || "true".equals(notify)) {
178 getContext().getContentResolver().notifyChange(uri, null);
179 }
180 }
181
182 private static class DatabaseHelper extends SQLiteOpenHelper {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 private static final String TAG_FAVORITES = "favorites";
184 private static final String TAG_FAVORITE = "favorite";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700185 private static final String TAG_CLOCK = "clock";
186 private static final String TAG_SEARCH = "search";
Mike Cleronb87bd162009-10-30 16:36:56 -0700187 private static final String TAG_APPWIDGET = "appwidget";
188 private static final String TAG_SHORTCUT = "shortcut";
189
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 private final Context mContext;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700191 private final AppWidgetHost mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192
193 DatabaseHelper(Context context) {
194 super(context, DATABASE_NAME, null, DATABASE_VERSION);
195 mContext = context;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700196 mAppWidgetHost = new AppWidgetHost(context, Launcher.APPWIDGET_HOST_ID);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 }
198
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700199 /**
200 * Send notification that we've deleted the {@link AppWidgetHost},
201 * probably as part of the initial database creation. The receiver may
202 * want to re-call {@link AppWidgetHost#startListening()} to ensure
203 * callbacks are correctly set.
204 */
205 private void sendAppWidgetResetNotify() {
206 final ContentResolver resolver = mContext.getContentResolver();
207 resolver.notifyChange(CONTENT_APPWIDGET_RESET_URI, null);
208 }
209
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210 @Override
211 public void onCreate(SQLiteDatabase db) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800212 if (LOGD) Log.d(TAG, "creating new launcher database");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213
214 db.execSQL("CREATE TABLE favorites (" +
215 "_id INTEGER PRIMARY KEY," +
216 "title TEXT," +
217 "intent TEXT," +
218 "container INTEGER," +
219 "screen INTEGER," +
220 "cellX INTEGER," +
221 "cellY INTEGER," +
222 "spanX INTEGER," +
223 "spanY INTEGER," +
224 "itemType INTEGER," +
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700225 "appWidgetId INTEGER NOT NULL DEFAULT -1," +
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800226 "isShortcut INTEGER," +
227 "iconType INTEGER," +
228 "iconPackage TEXT," +
229 "iconResource TEXT," +
230 "icon BLOB," +
231 "uri TEXT," +
232 "displayMode INTEGER" +
233 ");");
234
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700235 // Database was just created, so wipe any previous widgets
236 if (mAppWidgetHost != null) {
237 mAppWidgetHost.deleteHost();
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700238 sendAppWidgetResetNotify();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239 }
240
241 if (!convertDatabase(db)) {
242 // Populate favorites table with initial favorites
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700243 loadFavorites(db);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 }
245 }
246
247 private boolean convertDatabase(SQLiteDatabase db) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800248 if (LOGD) Log.d(TAG, "converting database from an older format, but not onUpgrade");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 boolean converted = false;
250
251 final Uri uri = Uri.parse("content://" + Settings.AUTHORITY +
252 "/old_favorites?notify=true");
253 final ContentResolver resolver = mContext.getContentResolver();
254 Cursor cursor = null;
255
256 try {
257 cursor = resolver.query(uri, null, null, null, null);
258 } catch (Exception e) {
259 // Ignore
260 }
261
262 // We already have a favorites database in the old provider
263 if (cursor != null && cursor.getCount() > 0) {
264 try {
265 converted = copyFromCursor(db, cursor) > 0;
266 } finally {
267 cursor.close();
268 }
269
270 if (converted) {
271 resolver.delete(uri, null, null);
272 }
273 }
274
275 if (converted) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700276 // Convert widgets from this import into widgets
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800277 if (LOGD) Log.d(TAG, "converted and now triggering widget upgrade");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278 convertWidgets(db);
279 }
280
281 return converted;
282 }
283
284 private int copyFromCursor(SQLiteDatabase db, Cursor c) {
Romain Guy73b979d2009-06-09 12:57:21 -0700285 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
287 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
288 final int iconTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
289 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
290 final int iconPackageIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
291 final int iconResourceIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
292 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
293 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
294 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
295 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
296 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
297 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
298 final int displayModeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
299
300 ContentValues[] rows = new ContentValues[c.getCount()];
301 int i = 0;
302 while (c.moveToNext()) {
303 ContentValues values = new ContentValues(c.getColumnCount());
Romain Guy73b979d2009-06-09 12:57:21 -0700304 values.put(LauncherSettings.Favorites._ID, c.getLong(idIndex));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800305 values.put(LauncherSettings.Favorites.INTENT, c.getString(intentIndex));
306 values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex));
307 values.put(LauncherSettings.Favorites.ICON_TYPE, c.getInt(iconTypeIndex));
308 values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex));
309 values.put(LauncherSettings.Favorites.ICON_PACKAGE, c.getString(iconPackageIndex));
310 values.put(LauncherSettings.Favorites.ICON_RESOURCE, c.getString(iconResourceIndex));
311 values.put(LauncherSettings.Favorites.CONTAINER, c.getInt(containerIndex));
312 values.put(LauncherSettings.Favorites.ITEM_TYPE, c.getInt(itemTypeIndex));
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700313 values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800314 values.put(LauncherSettings.Favorites.SCREEN, c.getInt(screenIndex));
315 values.put(LauncherSettings.Favorites.CELLX, c.getInt(cellXIndex));
316 values.put(LauncherSettings.Favorites.CELLY, c.getInt(cellYIndex));
317 values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex));
318 values.put(LauncherSettings.Favorites.DISPLAY_MODE, c.getInt(displayModeIndex));
319 rows[i++] = values;
320 }
321
322 db.beginTransaction();
323 int total = 0;
324 try {
325 int numValues = rows.length;
326 for (i = 0; i < numValues; i++) {
327 if (db.insert(TABLE_FAVORITES, null, rows[i]) < 0) {
328 return 0;
329 } else {
330 total++;
331 }
332 }
333 db.setTransactionSuccessful();
334 } finally {
335 db.endTransaction();
336 }
337
338 return total;
339 }
340
341 @Override
342 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800343 if (LOGD) Log.d(TAG, "onUpgrade triggered");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800344
345 int version = oldVersion;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700346 if (version < 3) {
347 // upgrade 1,2 -> 3 added appWidgetId column
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800348 db.beginTransaction();
349 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700350 // Insert new column for holding appWidgetIds
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 db.execSQL("ALTER TABLE favorites " +
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700352 "ADD COLUMN appWidgetId INTEGER NOT NULL DEFAULT -1;");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 db.setTransactionSuccessful();
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700354 version = 3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800355 } catch (SQLException ex) {
356 // Old version remains, which means we wipe old data
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800357 Log.e(TAG, ex.getMessage(), ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 } finally {
359 db.endTransaction();
360 }
361
362 // Convert existing widgets only if table upgrade was successful
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700363 if (version == 3) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364 convertWidgets(db);
365 }
366 }
Romain Guy73b979d2009-06-09 12:57:21 -0700367
368 if (version < 4) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800369 version = 4;
Romain Guy73b979d2009-06-09 12:57:21 -0700370 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800371
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800372 if (version < 5) {
373 // We went from 3 to 5 screens. Move everything 1 to the right
374 db.beginTransaction();
375 try {
376 db.execSQL("UPDATE favorites SET screen=(screen + 1);");
377 db.setTransactionSuccessful();
378 version = 5;
379 } catch (SQLException ex) {
380 // Old version remains, which means we wipe old data
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800381 Log.e(TAG, ex.getMessage(), ex);
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800382 } finally {
383 db.endTransaction();
384 }
385 }
386
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800387 if (version < 6) {
388 if (updateContactsShortcuts(db)) {
389 version = 6;
390 }
391 }
Bjorn Bringert7984c942009-12-09 15:38:25 +0000392
393 if (version < 7) {
394 // Version 7 gets rid of the special search widget.
395 convertWidgets(db);
396 version = 7;
397 }
398
Joe Onorato0589f0f2010-02-08 13:44:00 -0800399 if (version < 8) {
400 // Version 8 (froyo) has the icons all normalized. This should
401 // already be the case in practice, but we now rely on it and don't
402 // resample the images each time.
403 normalizeIcons(db);
404 version = 8;
405 }
406
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 if (version != DATABASE_VERSION) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800408 Log.w(TAG, "Destroying all old data.");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES);
410 onCreate(db);
411 }
412 }
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800413
414 private boolean updateContactsShortcuts(SQLiteDatabase db) {
415 Cursor c = null;
416 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE,
417 new int[] { Favorites.ITEM_TYPE_SHORTCUT });
418
419 db.beginTransaction();
420 try {
421 // Select and iterate through each matching widget
422 c = db.query(TABLE_FAVORITES, new String[] { Favorites._ID, Favorites.INTENT },
423 selectWhere, null, null, null, null);
424
425 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
426
427 final ContentValues values = new ContentValues();
428 final int idIndex = c.getColumnIndex(Favorites._ID);
429 final int intentIndex = c.getColumnIndex(Favorites.INTENT);
430
431 while (c != null && c.moveToNext()) {
432 long favoriteId = c.getLong(idIndex);
433 final String intentUri = c.getString(intentIndex);
434 if (intentUri != null) {
435 try {
436 Intent intent = Intent.parseUri(intentUri, 0);
437 android.util.Log.d("Home", intent.toString());
438 final Uri uri = intent.getData();
439 final String data = uri.toString();
440 if (Intent.ACTION_VIEW.equals(intent.getAction()) &&
441 (data.startsWith("content://contacts/people/") ||
442 data.startsWith("content://com.android.contacts/contacts/lookup/"))) {
443
444 intent = new Intent("com.android.contacts.action.QUICK_CONTACT");
445 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
446 Intent.FLAG_ACTIVITY_CLEAR_TOP |
447 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
448
449 intent.setData(uri);
450 intent.putExtra("mode", 3);
451 intent.putExtra("exclude_mimes", (String[]) null);
452
453 values.clear();
454 values.put(LauncherSettings.Favorites.INTENT, intent.toUri(0));
455
456 String updateWhere = Favorites._ID + "=" + favoriteId;
457 db.update(TABLE_FAVORITES, values, updateWhere, null);
458 }
459 } catch (RuntimeException ex) {
460 Log.e(TAG, "Problem upgrading shortcut", ex);
461 } catch (URISyntaxException e) {
462 Log.e(TAG, "Problem upgrading shortcut", e);
463 }
464 }
465 }
466
467 db.setTransactionSuccessful();
468 } catch (SQLException ex) {
469 Log.w(TAG, "Problem while upgrading contacts", ex);
470 return false;
471 } finally {
472 db.endTransaction();
473 if (c != null) {
474 c.close();
475 }
476 }
477
478 return true;
479 }
480
Joe Onorato0589f0f2010-02-08 13:44:00 -0800481 private void normalizeIcons(SQLiteDatabase db) {
482 Log.d(TAG, "normalizing icons");
483
Joe Onorato346e1292010-02-18 10:34:24 -0500484 db.beginTransaction();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800485 Cursor c = null;
486 try {
487 boolean logged = false;
488 final ContentValues values = new ContentValues();
489 final ContentResolver cr = mContext.getContentResolver();
490 final SQLiteStatement update = db.compileStatement("UPDATE favorites "
Jeff Hamiltoneaf77d62010-02-13 00:08:17 -0600491 + "SET icon=? WHERE _id=?");
Joe Onorato0589f0f2010-02-08 13:44:00 -0800492
493 c = db.rawQuery("SELECT _id, icon FROM favorites WHERE iconType=" +
494 Favorites.ICON_TYPE_BITMAP, null);
495
496 final int idIndex = c.getColumnIndexOrThrow(Favorites._ID);
497 final int iconIndex = c.getColumnIndexOrThrow(Favorites.ICON);
498
499 while (c.moveToNext()) {
500 long id = c.getLong(idIndex);
501 byte[] data = c.getBlob(iconIndex);
502 try {
503 Bitmap bitmap = Utilities.resampleIconBitmap(
504 BitmapFactory.decodeByteArray(data, 0, data.length),
505 mContext);
506 if (bitmap != null) {
507 update.bindLong(1, id);
508 data = ItemInfo.flattenBitmap(bitmap);
509 if (data != null) {
510 update.bindBlob(2, data);
511 update.execute();
512 }
513 bitmap.recycle();
514 bitmap = null;
515 }
516 } catch (Exception e) {
517 if (!logged) {
518 Log.e(TAG, "Failed normalizing icon " + id, e);
519 } else {
520 Log.e(TAG, "Also failed normalizing icon " + id);
521 }
522 logged = true;
523 }
524 }
Bjorn Bringert3a928e42010-02-19 11:15:40 +0000525 db.setTransactionSuccessful();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800526 } catch (SQLException ex) {
527 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
528 } finally {
529 db.endTransaction();
530 if (c != null) {
531 c.close();
532 }
533 }
534
535 }
536
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800537 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700538 * Upgrade existing clock and photo frame widgets into their new widget
Bjorn Bringert93c45762009-12-16 13:19:47 +0000539 * equivalents.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800540 */
541 private void convertWidgets(SQLiteDatabase db) {
Bjorn Bringert34251342009-12-15 13:33:11 +0000542 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800543 final int[] bindSources = new int[] {
544 Favorites.ITEM_TYPE_WIDGET_CLOCK,
545 Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME,
Bjorn Bringert7984c942009-12-09 15:38:25 +0000546 Favorites.ITEM_TYPE_WIDGET_SEARCH,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800547 };
Bjorn Bringert7984c942009-12-09 15:38:25 +0000548
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800549 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE, bindSources);
550
551 Cursor c = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800552
553 db.beginTransaction();
554 try {
555 // Select and iterate through each matching widget
Bjorn Bringert7984c942009-12-09 15:38:25 +0000556 c = db.query(TABLE_FAVORITES, new String[] { Favorites._ID, Favorites.ITEM_TYPE },
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800557 selectWhere, null, null, null, null);
558
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800559 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560
561 final ContentValues values = new ContentValues();
562 while (c != null && c.moveToNext()) {
563 long favoriteId = c.getLong(0);
Bjorn Bringert7984c942009-12-09 15:38:25 +0000564 int favoriteType = c.getInt(1);
565
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700566 // Allocate and update database with new appWidgetId
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800567 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700568 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800569
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800570 if (LOGD) {
571 Log.d(TAG, "allocated appWidgetId=" + appWidgetId
572 + " for favoriteId=" + favoriteId);
573 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800574 values.clear();
Bjorn Bringert7984c942009-12-09 15:38:25 +0000575 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
576 values.put(Favorites.APPWIDGET_ID, appWidgetId);
577
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800578 // Original widgets might not have valid spans when upgrading
Bjorn Bringert7984c942009-12-09 15:38:25 +0000579 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
580 values.put(LauncherSettings.Favorites.SPANX, 4);
581 values.put(LauncherSettings.Favorites.SPANY, 1);
582 } else {
583 values.put(LauncherSettings.Favorites.SPANX, 2);
584 values.put(LauncherSettings.Favorites.SPANY, 2);
585 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800586
587 String updateWhere = Favorites._ID + "=" + favoriteId;
588 db.update(TABLE_FAVORITES, values, updateWhere, null);
Bjorn Bringert34251342009-12-15 13:33:11 +0000589
590 ComponentName cn = null;
591 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_CLOCK) {
592 appWidgetManager.bindAppWidgetId(appWidgetId,
593 new ComponentName("com.android.alarmclock",
594 "com.android.alarmclock.AnalogAppWidgetProvider"));
595 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME) {
596 appWidgetManager.bindAppWidgetId(appWidgetId,
597 new ComponentName("com.android.camera",
598 "com.android.camera.PhotoAppWidgetProvider"));
599 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
600 appWidgetManager.bindAppWidgetId(appWidgetId,
Bjorn Bringertcd8fec02010-01-14 13:26:43 +0000601 getSearchWidgetProvider());
Bjorn Bringert34251342009-12-15 13:33:11 +0000602 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800603 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800604 Log.e(TAG, "Problem allocating appWidgetId", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605 }
606 }
607
608 db.setTransactionSuccessful();
609 } catch (SQLException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800610 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611 } finally {
612 db.endTransaction();
613 if (c != null) {
614 c.close();
615 }
616 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800617 }
618
619 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800620 * Loads the default set of favorite packages from an xml file.
621 *
622 * @param db The database to write the values into
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800623 */
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700624 private int loadFavorites(SQLiteDatabase db) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800625 Intent intent = new Intent(Intent.ACTION_MAIN, null);
626 intent.addCategory(Intent.CATEGORY_LAUNCHER);
627 ContentValues values = new ContentValues();
628
629 PackageManager packageManager = mContext.getPackageManager();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800630 int i = 0;
631 try {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700632 XmlResourceParser parser = mContext.getResources().getXml(R.xml.default_workspace);
633 AttributeSet attrs = Xml.asAttributeSet(parser);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634 XmlUtils.beginDocument(parser, TAG_FAVORITES);
635
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700636 final int depth = parser.getDepth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700638 int type;
639 while (((type = parser.next()) != XmlPullParser.END_TAG ||
640 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
641
642 if (type != XmlPullParser.START_TAG) {
643 continue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644 }
645
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700646 boolean added = false;
647 final String name = parser.getName();
648
649 TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.Favorite);
650
651 values.clear();
652 values.put(LauncherSettings.Favorites.CONTAINER,
653 LauncherSettings.Favorites.CONTAINER_DESKTOP);
654 values.put(LauncherSettings.Favorites.SCREEN,
655 a.getString(R.styleable.Favorite_screen));
656 values.put(LauncherSettings.Favorites.CELLX,
657 a.getString(R.styleable.Favorite_x));
658 values.put(LauncherSettings.Favorites.CELLY,
659 a.getString(R.styleable.Favorite_y));
660
661 if (TAG_FAVORITE.equals(name)) {
Mike Cleronb87bd162009-10-30 16:36:56 -0700662 added = addAppShortcut(db, values, a, packageManager, intent);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700663 } else if (TAG_SEARCH.equals(name)) {
664 added = addSearchWidget(db, values);
665 } else if (TAG_CLOCK.equals(name)) {
666 added = addClockWidget(db, values);
Mike Cleronb87bd162009-10-30 16:36:56 -0700667 } else if (TAG_APPWIDGET.equals(name)) {
668 added = addAppWidget(db, values, a);
669 } else if (TAG_SHORTCUT.equals(name)) {
670 added = addUriShortcut(db, values, a);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700672
673 if (added) i++;
674
675 a.recycle();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 }
677 } catch (XmlPullParserException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800678 Log.w(TAG, "Got exception parsing favorites.", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 } catch (IOException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800680 Log.w(TAG, "Got exception parsing favorites.", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700682
683 return i;
684 }
685
Mike Cleronb87bd162009-10-30 16:36:56 -0700686 private boolean addAppShortcut(SQLiteDatabase db, ContentValues values, TypedArray a,
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700687 PackageManager packageManager, Intent intent) {
688
689 ActivityInfo info;
690 String packageName = a.getString(R.styleable.Favorite_packageName);
691 String className = a.getString(R.styleable.Favorite_className);
692 try {
693 ComponentName cn = new ComponentName(packageName, className);
694 info = packageManager.getActivityInfo(cn, 0);
695 intent.setComponent(cn);
Dianne Hackbornbd2e54d2009-03-24 21:00:33 -0700696 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
697 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Romain Guy1ce1a242009-06-23 17:34:54 -0700698 values.put(Favorites.INTENT, intent.toUri(0));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700699 values.put(Favorites.TITLE, info.loadLabel(packageManager).toString());
700 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPLICATION);
701 values.put(Favorites.SPANX, 1);
702 values.put(Favorites.SPANY, 1);
703 db.insert(TABLE_FAVORITES, null, values);
704 } catch (PackageManager.NameNotFoundException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800705 Log.w(TAG, "Unable to add favorite: " + packageName +
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700706 "/" + className, e);
707 return false;
708 }
709 return true;
710 }
711
Bjorn Bringertcd8fec02010-01-14 13:26:43 +0000712 private ComponentName getSearchWidgetProvider() {
713 SearchManager searchManager =
714 (SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE);
715 ComponentName searchComponent = searchManager.getGlobalSearchActivity();
716 if (searchComponent == null) return null;
717 return getProviderInPackage(searchComponent.getPackageName());
718 }
719
720 /**
721 * Gets an appwidget provider from the given package. If the package contains more than
722 * one appwidget provider, an arbitrary one is returned.
723 */
724 private ComponentName getProviderInPackage(String packageName) {
725 AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
726 List<AppWidgetProviderInfo> providers = appWidgetManager.getInstalledProviders();
727 if (providers == null) return null;
728 final int providerCount = providers.size();
729 for (int i = 0; i < providerCount; i++) {
730 ComponentName provider = providers.get(i).provider;
731 if (provider != null && provider.getPackageName().equals(packageName)) {
732 return provider;
733 }
734 }
735 return null;
736 }
737
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700738 private boolean addSearchWidget(SQLiteDatabase db, ContentValues values) {
Bjorn Bringertcd8fec02010-01-14 13:26:43 +0000739 ComponentName cn = getSearchWidgetProvider();
Bjorn Bringert7984c942009-12-09 15:38:25 +0000740 return addAppWidget(db, values, cn, 4, 1);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700741 }
742
743 private boolean addClockWidget(SQLiteDatabase db, ContentValues values) {
Bjorn Bringert34251342009-12-15 13:33:11 +0000744 ComponentName cn = new ComponentName("com.android.alarmclock",
745 "com.android.alarmclock.AnalogAppWidgetProvider");
746 return addAppWidget(db, values, cn, 2, 2);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747 }
Mike Cleronb87bd162009-10-30 16:36:56 -0700748
749 private boolean addAppWidget(SQLiteDatabase db, ContentValues values, TypedArray a) {
Mike Cleronb87bd162009-10-30 16:36:56 -0700750 String packageName = a.getString(R.styleable.Favorite_packageName);
751 String className = a.getString(R.styleable.Favorite_className);
752
753 if (packageName == null || className == null) {
754 return false;
755 }
756
757 ComponentName cn = new ComponentName(packageName, className);
Bjorn Bringert7984c942009-12-09 15:38:25 +0000758 int spanX = a.getInt(R.styleable.Favorite_spanX, 0);
759 int spanY = a.getInt(R.styleable.Favorite_spanY, 0);
760 return addAppWidget(db, values, cn, spanX, spanY);
761 }
762
763 private boolean addAppWidget(SQLiteDatabase db, ContentValues values, ComponentName cn,
764 int spanX, int spanY) {
Mike Cleronb87bd162009-10-30 16:36:56 -0700765 boolean allocatedAppWidgets = false;
766 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
767
768 try {
769 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
770
771 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
Bjorn Bringert7984c942009-12-09 15:38:25 +0000772 values.put(Favorites.SPANX, spanX);
773 values.put(Favorites.SPANY, spanY);
Mike Cleronb87bd162009-10-30 16:36:56 -0700774 values.put(Favorites.APPWIDGET_ID, appWidgetId);
775 db.insert(TABLE_FAVORITES, null, values);
776
777 allocatedAppWidgets = true;
778
779 appWidgetManager.bindAppWidgetId(appWidgetId, cn);
780 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800781 Log.e(TAG, "Problem allocating appWidgetId", ex);
Mike Cleronb87bd162009-10-30 16:36:56 -0700782 }
783
784 return allocatedAppWidgets;
785 }
786
787 private boolean addUriShortcut(SQLiteDatabase db, ContentValues values,
788 TypedArray a) {
789 Resources r = mContext.getResources();
790
791 final int iconResId = a.getResourceId(R.styleable.Favorite_icon, 0);
792 final int titleResId = a.getResourceId(R.styleable.Favorite_title, 0);
793
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800794 Intent intent;
Mike Cleronb87bd162009-10-30 16:36:56 -0700795 String uri = null;
796 try {
797 uri = a.getString(R.styleable.Favorite_uri);
798 intent = Intent.parseUri(uri, 0);
799 } catch (URISyntaxException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800800 Log.w(TAG, "Shortcut has malformed uri: " + uri);
Mike Cleronb87bd162009-10-30 16:36:56 -0700801 return false; // Oh well
802 }
803
804 if (iconResId == 0 || titleResId == 0) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800805 Log.w(TAG, "Shortcut is missing title or icon resource ID");
Mike Cleronb87bd162009-10-30 16:36:56 -0700806 return false;
807 }
808
809 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
810 values.put(Favorites.INTENT, intent.toUri(0));
811 values.put(Favorites.TITLE, r.getString(titleResId));
812 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_SHORTCUT);
813 values.put(Favorites.SPANX, 1);
814 values.put(Favorites.SPANY, 1);
815 values.put(Favorites.ICON_TYPE, Favorites.ICON_TYPE_RESOURCE);
816 values.put(Favorites.ICON_PACKAGE, mContext.getPackageName());
817 values.put(Favorites.ICON_RESOURCE, r.getResourceName(iconResId));
818
819 db.insert(TABLE_FAVORITES, null, values);
820
821 return true;
822 }
823 }
824
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825 /**
826 * Build a query string that will match any row where the column matches
827 * anything in the values list.
828 */
829 static String buildOrWhereString(String column, int[] values) {
830 StringBuilder selectWhere = new StringBuilder();
831 for (int i = values.length - 1; i >= 0; i--) {
832 selectWhere.append(column).append("=").append(values[i]);
833 if (i > 0) {
834 selectWhere.append(" OR ");
835 }
836 }
837 return selectWhere.toString();
838 }
839
840 static class SqlArguments {
841 public final String table;
842 public final String where;
843 public final String[] args;
844
845 SqlArguments(Uri url, String where, String[] args) {
846 if (url.getPathSegments().size() == 1) {
847 this.table = url.getPathSegments().get(0);
848 this.where = where;
849 this.args = args;
850 } else if (url.getPathSegments().size() != 2) {
851 throw new IllegalArgumentException("Invalid URI: " + url);
852 } else if (!TextUtils.isEmpty(where)) {
853 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
854 } else {
855 this.table = url.getPathSegments().get(0);
856 this.where = "_id=" + ContentUris.parseId(url);
857 this.args = null;
858 }
859 }
860
861 SqlArguments(Uri url) {
862 if (url.getPathSegments().size() == 1) {
863 table = url.getPathSegments().get(0);
864 where = null;
865 args = null;
866 } else {
867 throw new IllegalArgumentException("Invalid URI: " + url);
868 }
869 }
870 }
871}