blob: 24b877b8b5b11bcb37addfb6b05bea5f4f9475c5 [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
Joe Onorato346e1292010-02-18 10:34:24 -0500483 db.beginTransaction();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800484 Cursor c = null;
485 try {
486 boolean logged = false;
487 final ContentValues values = new ContentValues();
488 final ContentResolver cr = mContext.getContentResolver();
489 final SQLiteStatement update = db.compileStatement("UPDATE favorites "
Jeff Hamiltoneaf77d62010-02-13 00:08:17 -0600490 + "SET icon=? WHERE _id=?");
Joe Onorato0589f0f2010-02-08 13:44:00 -0800491
492 c = db.rawQuery("SELECT _id, icon FROM favorites WHERE iconType=" +
493 Favorites.ICON_TYPE_BITMAP, null);
494
495 final int idIndex = c.getColumnIndexOrThrow(Favorites._ID);
496 final int iconIndex = c.getColumnIndexOrThrow(Favorites.ICON);
497
498 while (c.moveToNext()) {
499 long id = c.getLong(idIndex);
500 byte[] data = c.getBlob(iconIndex);
501 try {
502 Bitmap bitmap = Utilities.resampleIconBitmap(
503 BitmapFactory.decodeByteArray(data, 0, data.length),
504 mContext);
505 if (bitmap != null) {
506 update.bindLong(1, id);
507 data = ItemInfo.flattenBitmap(bitmap);
508 if (data != null) {
509 update.bindBlob(2, data);
510 update.execute();
511 }
512 bitmap.recycle();
513 bitmap = null;
514 }
515 } catch (Exception e) {
516 if (!logged) {
517 Log.e(TAG, "Failed normalizing icon " + id, e);
518 } else {
519 Log.e(TAG, "Also failed normalizing icon " + id);
520 }
521 logged = true;
522 }
523 }
Bjorn Bringert3a928e42010-02-19 11:15:40 +0000524 db.setTransactionSuccessful();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800525 } catch (SQLException ex) {
526 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
527 } finally {
528 db.endTransaction();
529 if (c != null) {
530 c.close();
531 }
532 }
533
534 }
535
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800536 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700537 * Upgrade existing clock and photo frame widgets into their new widget
Bjorn Bringert93c45762009-12-16 13:19:47 +0000538 * equivalents.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800539 */
540 private void convertWidgets(SQLiteDatabase db) {
Bjorn Bringert34251342009-12-15 13:33:11 +0000541 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800542 final int[] bindSources = new int[] {
543 Favorites.ITEM_TYPE_WIDGET_CLOCK,
544 Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME,
Bjorn Bringert7984c942009-12-09 15:38:25 +0000545 Favorites.ITEM_TYPE_WIDGET_SEARCH,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800546 };
Bjorn Bringert7984c942009-12-09 15:38:25 +0000547
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800548 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE, bindSources);
549
550 Cursor c = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800551
552 db.beginTransaction();
553 try {
554 // Select and iterate through each matching widget
Bjorn Bringert7984c942009-12-09 15:38:25 +0000555 c = db.query(TABLE_FAVORITES, new String[] { Favorites._ID, Favorites.ITEM_TYPE },
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800556 selectWhere, null, null, null, null);
557
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800558 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800559
560 final ContentValues values = new ContentValues();
561 while (c != null && c.moveToNext()) {
562 long favoriteId = c.getLong(0);
Bjorn Bringert7984c942009-12-09 15:38:25 +0000563 int favoriteType = c.getInt(1);
564
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700565 // Allocate and update database with new appWidgetId
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800566 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700567 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800568
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800569 if (LOGD) {
570 Log.d(TAG, "allocated appWidgetId=" + appWidgetId
571 + " for favoriteId=" + favoriteId);
572 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800573 values.clear();
Bjorn Bringert7984c942009-12-09 15:38:25 +0000574 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
575 values.put(Favorites.APPWIDGET_ID, appWidgetId);
576
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800577 // Original widgets might not have valid spans when upgrading
Bjorn Bringert7984c942009-12-09 15:38:25 +0000578 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
579 values.put(LauncherSettings.Favorites.SPANX, 4);
580 values.put(LauncherSettings.Favorites.SPANY, 1);
581 } else {
582 values.put(LauncherSettings.Favorites.SPANX, 2);
583 values.put(LauncherSettings.Favorites.SPANY, 2);
584 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800585
586 String updateWhere = Favorites._ID + "=" + favoriteId;
587 db.update(TABLE_FAVORITES, values, updateWhere, null);
Bjorn Bringert34251342009-12-15 13:33:11 +0000588
589 ComponentName cn = null;
590 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_CLOCK) {
591 appWidgetManager.bindAppWidgetId(appWidgetId,
592 new ComponentName("com.android.alarmclock",
593 "com.android.alarmclock.AnalogAppWidgetProvider"));
594 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME) {
595 appWidgetManager.bindAppWidgetId(appWidgetId,
596 new ComponentName("com.android.camera",
597 "com.android.camera.PhotoAppWidgetProvider"));
598 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
599 appWidgetManager.bindAppWidgetId(appWidgetId,
Bjorn Bringertcd8fec02010-01-14 13:26:43 +0000600 getSearchWidgetProvider());
Bjorn Bringert34251342009-12-15 13:33:11 +0000601 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800602 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800603 Log.e(TAG, "Problem allocating appWidgetId", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800604 }
605 }
606
607 db.setTransactionSuccessful();
608 } catch (SQLException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800609 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800610 } finally {
611 db.endTransaction();
612 if (c != null) {
613 c.close();
614 }
615 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800616 }
617
618 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800619 * Loads the default set of favorite packages from an xml file.
620 *
621 * @param db The database to write the values into
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622 */
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700623 private int loadFavorites(SQLiteDatabase db) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800624 Intent intent = new Intent(Intent.ACTION_MAIN, null);
625 intent.addCategory(Intent.CATEGORY_LAUNCHER);
626 ContentValues values = new ContentValues();
627
628 PackageManager packageManager = mContext.getPackageManager();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800629 int i = 0;
630 try {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700631 XmlResourceParser parser = mContext.getResources().getXml(R.xml.default_workspace);
632 AttributeSet attrs = Xml.asAttributeSet(parser);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633 XmlUtils.beginDocument(parser, TAG_FAVORITES);
634
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700635 final int depth = parser.getDepth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700637 int type;
638 while (((type = parser.next()) != XmlPullParser.END_TAG ||
639 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
640
641 if (type != XmlPullParser.START_TAG) {
642 continue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800643 }
644
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700645 boolean added = false;
646 final String name = parser.getName();
647
648 TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.Favorite);
649
650 values.clear();
651 values.put(LauncherSettings.Favorites.CONTAINER,
652 LauncherSettings.Favorites.CONTAINER_DESKTOP);
653 values.put(LauncherSettings.Favorites.SCREEN,
654 a.getString(R.styleable.Favorite_screen));
655 values.put(LauncherSettings.Favorites.CELLX,
656 a.getString(R.styleable.Favorite_x));
657 values.put(LauncherSettings.Favorites.CELLY,
658 a.getString(R.styleable.Favorite_y));
659
660 if (TAG_FAVORITE.equals(name)) {
Mike Cleronb87bd162009-10-30 16:36:56 -0700661 added = addAppShortcut(db, values, a, packageManager, intent);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700662 } else if (TAG_SEARCH.equals(name)) {
663 added = addSearchWidget(db, values);
664 } else if (TAG_CLOCK.equals(name)) {
665 added = addClockWidget(db, values);
Mike Cleronb87bd162009-10-30 16:36:56 -0700666 } else if (TAG_APPWIDGET.equals(name)) {
667 added = addAppWidget(db, values, a);
668 } else if (TAG_SHORTCUT.equals(name)) {
669 added = addUriShortcut(db, values, a);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700671
672 if (added) i++;
673
674 a.recycle();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 }
676 } catch (XmlPullParserException 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 } catch (IOException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800679 Log.w(TAG, "Got exception parsing favorites.", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700681
682 return i;
683 }
684
Mike Cleronb87bd162009-10-30 16:36:56 -0700685 private boolean addAppShortcut(SQLiteDatabase db, ContentValues values, TypedArray a,
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700686 PackageManager packageManager, Intent intent) {
687
688 ActivityInfo info;
689 String packageName = a.getString(R.styleable.Favorite_packageName);
690 String className = a.getString(R.styleable.Favorite_className);
691 try {
692 ComponentName cn = new ComponentName(packageName, className);
693 info = packageManager.getActivityInfo(cn, 0);
694 intent.setComponent(cn);
Dianne Hackbornbd2e54d2009-03-24 21:00:33 -0700695 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
696 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Romain Guy1ce1a242009-06-23 17:34:54 -0700697 values.put(Favorites.INTENT, intent.toUri(0));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700698 values.put(Favorites.TITLE, info.loadLabel(packageManager).toString());
699 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPLICATION);
700 values.put(Favorites.SPANX, 1);
701 values.put(Favorites.SPANY, 1);
702 db.insert(TABLE_FAVORITES, null, values);
703 } catch (PackageManager.NameNotFoundException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800704 Log.w(TAG, "Unable to add favorite: " + packageName +
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700705 "/" + className, e);
706 return false;
707 }
708 return true;
709 }
710
Bjorn Bringertcd8fec02010-01-14 13:26:43 +0000711 private ComponentName getSearchWidgetProvider() {
712 SearchManager searchManager =
713 (SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE);
714 ComponentName searchComponent = searchManager.getGlobalSearchActivity();
715 if (searchComponent == null) return null;
716 return getProviderInPackage(searchComponent.getPackageName());
717 }
718
719 /**
720 * Gets an appwidget provider from the given package. If the package contains more than
721 * one appwidget provider, an arbitrary one is returned.
722 */
723 private ComponentName getProviderInPackage(String packageName) {
724 AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
725 List<AppWidgetProviderInfo> providers = appWidgetManager.getInstalledProviders();
726 if (providers == null) return null;
727 final int providerCount = providers.size();
728 for (int i = 0; i < providerCount; i++) {
729 ComponentName provider = providers.get(i).provider;
730 if (provider != null && provider.getPackageName().equals(packageName)) {
731 return provider;
732 }
733 }
734 return null;
735 }
736
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700737 private boolean addSearchWidget(SQLiteDatabase db, ContentValues values) {
Bjorn Bringertcd8fec02010-01-14 13:26:43 +0000738 ComponentName cn = getSearchWidgetProvider();
Bjorn Bringert7984c942009-12-09 15:38:25 +0000739 return addAppWidget(db, values, cn, 4, 1);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700740 }
741
742 private boolean addClockWidget(SQLiteDatabase db, ContentValues values) {
Bjorn Bringert34251342009-12-15 13:33:11 +0000743 ComponentName cn = new ComponentName("com.android.alarmclock",
744 "com.android.alarmclock.AnalogAppWidgetProvider");
745 return addAppWidget(db, values, cn, 2, 2);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800746 }
Mike Cleronb87bd162009-10-30 16:36:56 -0700747
748 private boolean addAppWidget(SQLiteDatabase db, ContentValues values, TypedArray a) {
Mike Cleronb87bd162009-10-30 16:36:56 -0700749 String packageName = a.getString(R.styleable.Favorite_packageName);
750 String className = a.getString(R.styleable.Favorite_className);
751
752 if (packageName == null || className == null) {
753 return false;
754 }
755
756 ComponentName cn = new ComponentName(packageName, className);
Bjorn Bringert7984c942009-12-09 15:38:25 +0000757 int spanX = a.getInt(R.styleable.Favorite_spanX, 0);
758 int spanY = a.getInt(R.styleable.Favorite_spanY, 0);
759 return addAppWidget(db, values, cn, spanX, spanY);
760 }
761
762 private boolean addAppWidget(SQLiteDatabase db, ContentValues values, ComponentName cn,
763 int spanX, int spanY) {
Mike Cleronb87bd162009-10-30 16:36:56 -0700764 boolean allocatedAppWidgets = false;
765 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
766
767 try {
768 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
769
770 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
Bjorn Bringert7984c942009-12-09 15:38:25 +0000771 values.put(Favorites.SPANX, spanX);
772 values.put(Favorites.SPANY, spanY);
Mike Cleronb87bd162009-10-30 16:36:56 -0700773 values.put(Favorites.APPWIDGET_ID, appWidgetId);
774 db.insert(TABLE_FAVORITES, null, values);
775
776 allocatedAppWidgets = true;
777
778 appWidgetManager.bindAppWidgetId(appWidgetId, cn);
779 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800780 Log.e(TAG, "Problem allocating appWidgetId", ex);
Mike Cleronb87bd162009-10-30 16:36:56 -0700781 }
782
783 return allocatedAppWidgets;
784 }
785
786 private boolean addUriShortcut(SQLiteDatabase db, ContentValues values,
787 TypedArray a) {
788 Resources r = mContext.getResources();
789
790 final int iconResId = a.getResourceId(R.styleable.Favorite_icon, 0);
791 final int titleResId = a.getResourceId(R.styleable.Favorite_title, 0);
792
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800793 Intent intent;
Mike Cleronb87bd162009-10-30 16:36:56 -0700794 String uri = null;
795 try {
796 uri = a.getString(R.styleable.Favorite_uri);
797 intent = Intent.parseUri(uri, 0);
798 } catch (URISyntaxException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800799 Log.w(TAG, "Shortcut has malformed uri: " + uri);
Mike Cleronb87bd162009-10-30 16:36:56 -0700800 return false; // Oh well
801 }
802
803 if (iconResId == 0 || titleResId == 0) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800804 Log.w(TAG, "Shortcut is missing title or icon resource ID");
Mike Cleronb87bd162009-10-30 16:36:56 -0700805 return false;
806 }
807
808 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
809 values.put(Favorites.INTENT, intent.toUri(0));
810 values.put(Favorites.TITLE, r.getString(titleResId));
811 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_SHORTCUT);
812 values.put(Favorites.SPANX, 1);
813 values.put(Favorites.SPANY, 1);
814 values.put(Favorites.ICON_TYPE, Favorites.ICON_TYPE_RESOURCE);
815 values.put(Favorites.ICON_PACKAGE, mContext.getPackageName());
816 values.put(Favorites.ICON_RESOURCE, r.getResourceName(iconResId));
817
818 db.insert(TABLE_FAVORITES, null, values);
819
820 return true;
821 }
822 }
823
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800824 /**
825 * Build a query string that will match any row where the column matches
826 * anything in the values list.
827 */
828 static String buildOrWhereString(String column, int[] values) {
829 StringBuilder selectWhere = new StringBuilder();
830 for (int i = values.length - 1; i >= 0; i--) {
831 selectWhere.append(column).append("=").append(values[i]);
832 if (i > 0) {
833 selectWhere.append(" OR ");
834 }
835 }
836 return selectWhere.toString();
837 }
838
839 static class SqlArguments {
840 public final String table;
841 public final String where;
842 public final String[] args;
843
844 SqlArguments(Uri url, String where, String[] args) {
845 if (url.getPathSegments().size() == 1) {
846 this.table = url.getPathSegments().get(0);
847 this.where = where;
848 this.args = args;
849 } else if (url.getPathSegments().size() != 2) {
850 throw new IllegalArgumentException("Invalid URI: " + url);
851 } else if (!TextUtils.isEmpty(where)) {
852 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
853 } else {
854 this.table = url.getPathSegments().get(0);
855 this.where = "_id=" + ContentUris.parseId(url);
856 this.args = null;
857 }
858 }
859
860 SqlArguments(Uri url) {
861 if (url.getPathSegments().size() == 1) {
862 table = url.getPathSegments().get(0);
863 where = null;
864 args = null;
865 } else {
866 throw new IllegalArgumentException("Invalid URI: " + url);
867 }
868 }
869 }
870}