blob: d50b19cad1802b3406fe3ccca118ddf3c888052a [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Bjorn Bringertcd8fec02010-01-14 13:26:43 +000019import android.app.SearchManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070020import android.appwidget.AppWidgetHost;
Mike Cleronb87bd162009-10-30 16:36:56 -070021import android.appwidget.AppWidgetManager;
Bjorn Bringertcd8fec02010-01-14 13:26:43 +000022import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.content.ContentProvider;
24import android.content.Context;
25import android.content.ContentValues;
26import android.content.Intent;
27import android.content.ComponentName;
28import android.content.ContentUris;
29import android.content.ContentResolver;
Mike Cleronb87bd162009-10-30 16:36:56 -070030import android.content.res.Resources;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070031import android.content.res.XmlResourceParser;
32import android.content.res.TypedArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.pm.PackageManager;
34import android.content.pm.ActivityInfo;
35import android.database.sqlite.SQLiteOpenHelper;
36import android.database.sqlite.SQLiteDatabase;
Joe Onorato0589f0f2010-02-08 13:44:00 -080037import android.database.sqlite.SQLiteStatement;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.database.sqlite.SQLiteQueryBuilder;
39import android.database.Cursor;
40import android.database.SQLException;
Joe Onorato0589f0f2010-02-08 13:44:00 -080041import android.graphics.Bitmap;
42import android.graphics.BitmapFactory;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.util.Log;
44import android.util.Xml;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070045import android.util.AttributeSet;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.net.Uri;
47import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.provider.Settings;
49
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import java.io.IOException;
Mike Cleronb87bd162009-10-30 16:36:56 -070051import java.net.URISyntaxException;
Bjorn Bringertcd8fec02010-01-14 13:26:43 +000052import java.util.List;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import org.xmlpull.v1.XmlPullParserException;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070055import org.xmlpull.v1.XmlPullParser;
Dianne Hackborn094fc7a2010-02-24 20:01:46 -080056
57import com.android.internal.util.XmlUtils;
Joe Onoratoa5902522009-07-30 13:37:37 -070058import com.android.launcher2.LauncherSettings.Favorites;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059
Romain Guyedcce092010-03-04 13:03:17 -080060import com.android.launcher.R;
61
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062public 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
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072 static final String TABLE_FAVORITES = "favorites";
73 static final String PARAMETER_NOTIFY = "notify";
74
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -070075 /**
Romain Guy73b979d2009-06-09 12:57:21 -070076 * {@link Uri} triggered at any registered {@link android.database.ContentObserver} when
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -070077 * {@link AppWidgetHost#deleteHost()} is called during database creation.
78 * Use this to recall {@link AppWidgetHost#startListening()} if needed.
79 */
80 static final Uri CONTENT_APPWIDGET_RESET_URI =
81 Uri.parse("content://" + AUTHORITY + "/appWidgetReset");
82
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083 private SQLiteOpenHelper mOpenHelper;
84
85 @Override
86 public boolean onCreate() {
87 mOpenHelper = new DatabaseHelper(getContext());
88 return true;
89 }
90
91 @Override
92 public String getType(Uri uri) {
93 SqlArguments args = new SqlArguments(uri, null, null);
94 if (TextUtils.isEmpty(args.where)) {
95 return "vnd.android.cursor.dir/" + args.table;
96 } else {
97 return "vnd.android.cursor.item/" + args.table;
98 }
99 }
100
101 @Override
102 public Cursor query(Uri uri, String[] projection, String selection,
103 String[] selectionArgs, String sortOrder) {
104
105 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
106 SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
107 qb.setTables(args.table);
108
Romain Guy73b979d2009-06-09 12:57:21 -0700109 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110 Cursor result = qb.query(db, projection, args.where, args.args, null, null, sortOrder);
111 result.setNotificationUri(getContext().getContentResolver(), uri);
112
113 return result;
114 }
115
116 @Override
117 public Uri insert(Uri uri, ContentValues initialValues) {
118 SqlArguments args = new SqlArguments(uri);
119
120 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
121 final long rowId = db.insert(args.table, null, initialValues);
122 if (rowId <= 0) return null;
123
124 uri = ContentUris.withAppendedId(uri, rowId);
125 sendNotify(uri);
126
127 return uri;
128 }
129
130 @Override
131 public int bulkInsert(Uri uri, ContentValues[] values) {
132 SqlArguments args = new SqlArguments(uri);
133
134 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
135 db.beginTransaction();
136 try {
137 int numValues = values.length;
138 for (int i = 0; i < numValues; i++) {
139 if (db.insert(args.table, null, values[i]) < 0) return 0;
140 }
141 db.setTransactionSuccessful();
142 } finally {
143 db.endTransaction();
144 }
145
146 sendNotify(uri);
147 return values.length;
148 }
149
150 @Override
151 public int delete(Uri uri, String selection, String[] selectionArgs) {
152 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
153
154 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
155 int count = db.delete(args.table, args.where, args.args);
156 if (count > 0) sendNotify(uri);
157
158 return count;
159 }
160
161 @Override
162 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
163 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
164
165 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
166 int count = db.update(args.table, values, args.where, args.args);
167 if (count > 0) sendNotify(uri);
168
169 return count;
170 }
171
172 private void sendNotify(Uri uri) {
173 String notify = uri.getQueryParameter(PARAMETER_NOTIFY);
174 if (notify == null || "true".equals(notify)) {
175 getContext().getContentResolver().notifyChange(uri, null);
176 }
177 }
178
179 private static class DatabaseHelper extends SQLiteOpenHelper {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 private static final String TAG_FAVORITES = "favorites";
181 private static final String TAG_FAVORITE = "favorite";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700182 private static final String TAG_CLOCK = "clock";
183 private static final String TAG_SEARCH = "search";
Mike Cleronb87bd162009-10-30 16:36:56 -0700184 private static final String TAG_APPWIDGET = "appwidget";
185 private static final String TAG_SHORTCUT = "shortcut";
186
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 private final Context mContext;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700188 private final AppWidgetHost mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189
190 DatabaseHelper(Context context) {
191 super(context, DATABASE_NAME, null, DATABASE_VERSION);
192 mContext = context;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700193 mAppWidgetHost = new AppWidgetHost(context, Launcher.APPWIDGET_HOST_ID);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 }
195
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700196 /**
197 * Send notification that we've deleted the {@link AppWidgetHost},
198 * probably as part of the initial database creation. The receiver may
199 * want to re-call {@link AppWidgetHost#startListening()} to ensure
200 * callbacks are correctly set.
201 */
202 private void sendAppWidgetResetNotify() {
203 final ContentResolver resolver = mContext.getContentResolver();
204 resolver.notifyChange(CONTENT_APPWIDGET_RESET_URI, null);
205 }
206
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207 @Override
208 public void onCreate(SQLiteDatabase db) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800209 if (LOGD) Log.d(TAG, "creating new launcher database");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210
211 db.execSQL("CREATE TABLE favorites (" +
212 "_id INTEGER PRIMARY KEY," +
213 "title TEXT," +
214 "intent TEXT," +
215 "container INTEGER," +
216 "screen INTEGER," +
217 "cellX INTEGER," +
218 "cellY INTEGER," +
219 "spanX INTEGER," +
220 "spanY INTEGER," +
221 "itemType INTEGER," +
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700222 "appWidgetId INTEGER NOT NULL DEFAULT -1," +
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 "isShortcut INTEGER," +
224 "iconType INTEGER," +
225 "iconPackage TEXT," +
226 "iconResource TEXT," +
227 "icon BLOB," +
228 "uri TEXT," +
229 "displayMode INTEGER" +
230 ");");
231
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700232 // Database was just created, so wipe any previous widgets
233 if (mAppWidgetHost != null) {
234 mAppWidgetHost.deleteHost();
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700235 sendAppWidgetResetNotify();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236 }
237
238 if (!convertDatabase(db)) {
239 // Populate favorites table with initial favorites
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700240 loadFavorites(db);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800241 }
242 }
243
244 private boolean convertDatabase(SQLiteDatabase db) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800245 if (LOGD) Log.d(TAG, "converting database from an older format, but not onUpgrade");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 boolean converted = false;
247
248 final Uri uri = Uri.parse("content://" + Settings.AUTHORITY +
249 "/old_favorites?notify=true");
250 final ContentResolver resolver = mContext.getContentResolver();
251 Cursor cursor = null;
252
253 try {
254 cursor = resolver.query(uri, null, null, null, null);
255 } catch (Exception e) {
256 // Ignore
257 }
258
259 // We already have a favorites database in the old provider
260 if (cursor != null && cursor.getCount() > 0) {
261 try {
262 converted = copyFromCursor(db, cursor) > 0;
263 } finally {
264 cursor.close();
265 }
266
267 if (converted) {
268 resolver.delete(uri, null, null);
269 }
270 }
271
272 if (converted) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700273 // Convert widgets from this import into widgets
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800274 if (LOGD) Log.d(TAG, "converted and now triggering widget upgrade");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275 convertWidgets(db);
276 }
277
278 return converted;
279 }
280
281 private int copyFromCursor(SQLiteDatabase db, Cursor c) {
Romain Guy73b979d2009-06-09 12:57:21 -0700282 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283 final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
284 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
285 final int iconTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
286 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
287 final int iconPackageIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
288 final int iconResourceIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
289 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
290 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
291 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
292 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
293 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
294 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
295 final int displayModeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
296
297 ContentValues[] rows = new ContentValues[c.getCount()];
298 int i = 0;
299 while (c.moveToNext()) {
300 ContentValues values = new ContentValues(c.getColumnCount());
Romain Guy73b979d2009-06-09 12:57:21 -0700301 values.put(LauncherSettings.Favorites._ID, c.getLong(idIndex));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800302 values.put(LauncherSettings.Favorites.INTENT, c.getString(intentIndex));
303 values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex));
304 values.put(LauncherSettings.Favorites.ICON_TYPE, c.getInt(iconTypeIndex));
305 values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex));
306 values.put(LauncherSettings.Favorites.ICON_PACKAGE, c.getString(iconPackageIndex));
307 values.put(LauncherSettings.Favorites.ICON_RESOURCE, c.getString(iconResourceIndex));
308 values.put(LauncherSettings.Favorites.CONTAINER, c.getInt(containerIndex));
309 values.put(LauncherSettings.Favorites.ITEM_TYPE, c.getInt(itemTypeIndex));
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700310 values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800311 values.put(LauncherSettings.Favorites.SCREEN, c.getInt(screenIndex));
312 values.put(LauncherSettings.Favorites.CELLX, c.getInt(cellXIndex));
313 values.put(LauncherSettings.Favorites.CELLY, c.getInt(cellYIndex));
314 values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex));
315 values.put(LauncherSettings.Favorites.DISPLAY_MODE, c.getInt(displayModeIndex));
316 rows[i++] = values;
317 }
318
319 db.beginTransaction();
320 int total = 0;
321 try {
322 int numValues = rows.length;
323 for (i = 0; i < numValues; i++) {
324 if (db.insert(TABLE_FAVORITES, null, rows[i]) < 0) {
325 return 0;
326 } else {
327 total++;
328 }
329 }
330 db.setTransactionSuccessful();
331 } finally {
332 db.endTransaction();
333 }
334
335 return total;
336 }
337
338 @Override
339 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800340 if (LOGD) Log.d(TAG, "onUpgrade triggered");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800341
342 int version = oldVersion;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700343 if (version < 3) {
344 // upgrade 1,2 -> 3 added appWidgetId column
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800345 db.beginTransaction();
346 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700347 // Insert new column for holding appWidgetIds
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800348 db.execSQL("ALTER TABLE favorites " +
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700349 "ADD COLUMN appWidgetId INTEGER NOT NULL DEFAULT -1;");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800350 db.setTransactionSuccessful();
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700351 version = 3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800352 } catch (SQLException ex) {
353 // Old version remains, which means we wipe old data
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800354 Log.e(TAG, ex.getMessage(), ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800355 } finally {
356 db.endTransaction();
357 }
358
359 // Convert existing widgets only if table upgrade was successful
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700360 if (version == 3) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361 convertWidgets(db);
362 }
363 }
Romain Guy73b979d2009-06-09 12:57:21 -0700364
365 if (version < 4) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800366 version = 4;
Romain Guy73b979d2009-06-09 12:57:21 -0700367 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800369 if (version < 5) {
370 // We went from 3 to 5 screens. Move everything 1 to the right
371 db.beginTransaction();
372 try {
373 db.execSQL("UPDATE favorites SET screen=(screen + 1);");
374 db.setTransactionSuccessful();
375 version = 5;
376 } catch (SQLException ex) {
377 // Old version remains, which means we wipe old data
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800378 Log.e(TAG, ex.getMessage(), ex);
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800379 } finally {
380 db.endTransaction();
381 }
382 }
383
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800384 if (version < 6) {
385 if (updateContactsShortcuts(db)) {
386 version = 6;
387 }
388 }
Bjorn Bringert7984c942009-12-09 15:38:25 +0000389
390 if (version < 7) {
391 // Version 7 gets rid of the special search widget.
392 convertWidgets(db);
393 version = 7;
394 }
395
Joe Onorato0589f0f2010-02-08 13:44:00 -0800396 if (version < 8) {
397 // Version 8 (froyo) has the icons all normalized. This should
398 // already be the case in practice, but we now rely on it and don't
399 // resample the images each time.
400 normalizeIcons(db);
401 version = 8;
402 }
403
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800404 if (version != DATABASE_VERSION) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800405 Log.w(TAG, "Destroying all old data.");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES);
407 onCreate(db);
408 }
409 }
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800410
411 private boolean updateContactsShortcuts(SQLiteDatabase db) {
412 Cursor c = null;
413 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE,
414 new int[] { Favorites.ITEM_TYPE_SHORTCUT });
415
416 db.beginTransaction();
417 try {
418 // Select and iterate through each matching widget
419 c = db.query(TABLE_FAVORITES, new String[] { Favorites._ID, Favorites.INTENT },
420 selectWhere, null, null, null, null);
421
422 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
423
424 final ContentValues values = new ContentValues();
425 final int idIndex = c.getColumnIndex(Favorites._ID);
426 final int intentIndex = c.getColumnIndex(Favorites.INTENT);
427
428 while (c != null && c.moveToNext()) {
429 long favoriteId = c.getLong(idIndex);
430 final String intentUri = c.getString(intentIndex);
431 if (intentUri != null) {
432 try {
433 Intent intent = Intent.parseUri(intentUri, 0);
434 android.util.Log.d("Home", intent.toString());
435 final Uri uri = intent.getData();
436 final String data = uri.toString();
437 if (Intent.ACTION_VIEW.equals(intent.getAction()) &&
438 (data.startsWith("content://contacts/people/") ||
439 data.startsWith("content://com.android.contacts/contacts/lookup/"))) {
440
441 intent = new Intent("com.android.contacts.action.QUICK_CONTACT");
442 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
443 Intent.FLAG_ACTIVITY_CLEAR_TOP |
444 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
445
446 intent.setData(uri);
447 intent.putExtra("mode", 3);
448 intent.putExtra("exclude_mimes", (String[]) null);
449
450 values.clear();
451 values.put(LauncherSettings.Favorites.INTENT, intent.toUri(0));
452
453 String updateWhere = Favorites._ID + "=" + favoriteId;
454 db.update(TABLE_FAVORITES, values, updateWhere, null);
455 }
456 } catch (RuntimeException ex) {
457 Log.e(TAG, "Problem upgrading shortcut", ex);
458 } catch (URISyntaxException e) {
459 Log.e(TAG, "Problem upgrading shortcut", e);
460 }
461 }
462 }
463
464 db.setTransactionSuccessful();
465 } catch (SQLException ex) {
466 Log.w(TAG, "Problem while upgrading contacts", ex);
467 return false;
468 } finally {
469 db.endTransaction();
470 if (c != null) {
471 c.close();
472 }
473 }
474
475 return true;
476 }
477
Joe Onorato0589f0f2010-02-08 13:44:00 -0800478 private void normalizeIcons(SQLiteDatabase db) {
479 Log.d(TAG, "normalizing icons");
480
Joe Onorato346e1292010-02-18 10:34:24 -0500481 db.beginTransaction();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800482 Cursor c = null;
Joe Onorato9690b392010-03-23 17:34:37 -0400483 SQLiteStatement update = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800484 try {
485 boolean logged = false;
Joe Onorato9690b392010-03-23 17:34:37 -0400486 update = db.compileStatement("UPDATE favorites "
Jeff Hamiltoneaf77d62010-02-13 00:08:17 -0600487 + "SET icon=? WHERE _id=?");
Joe Onorato0589f0f2010-02-08 13:44:00 -0800488
489 c = db.rawQuery("SELECT _id, icon FROM favorites WHERE iconType=" +
490 Favorites.ICON_TYPE_BITMAP, null);
491
492 final int idIndex = c.getColumnIndexOrThrow(Favorites._ID);
493 final int iconIndex = c.getColumnIndexOrThrow(Favorites.ICON);
494
495 while (c.moveToNext()) {
496 long id = c.getLong(idIndex);
497 byte[] data = c.getBlob(iconIndex);
498 try {
499 Bitmap bitmap = Utilities.resampleIconBitmap(
500 BitmapFactory.decodeByteArray(data, 0, data.length),
501 mContext);
502 if (bitmap != null) {
503 update.bindLong(1, id);
504 data = ItemInfo.flattenBitmap(bitmap);
505 if (data != null) {
506 update.bindBlob(2, data);
507 update.execute();
508 }
509 bitmap.recycle();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800510 }
511 } catch (Exception e) {
512 if (!logged) {
513 Log.e(TAG, "Failed normalizing icon " + id, e);
514 } else {
515 Log.e(TAG, "Also failed normalizing icon " + id);
516 }
517 logged = true;
518 }
519 }
Bjorn Bringert3a928e42010-02-19 11:15:40 +0000520 db.setTransactionSuccessful();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800521 } catch (SQLException ex) {
522 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
523 } finally {
524 db.endTransaction();
Joe Onorato9690b392010-03-23 17:34:37 -0400525 if (update != null) {
526 update.close();
527 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800528 if (c != null) {
529 c.close();
530 }
531 }
532
533 }
534
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800535 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700536 * Upgrade existing clock and photo frame widgets into their new widget
Bjorn Bringert93c45762009-12-16 13:19:47 +0000537 * equivalents.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800538 */
539 private void convertWidgets(SQLiteDatabase db) {
Bjorn Bringert34251342009-12-15 13:33:11 +0000540 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800541 final int[] bindSources = new int[] {
542 Favorites.ITEM_TYPE_WIDGET_CLOCK,
543 Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME,
Bjorn Bringert7984c942009-12-09 15:38:25 +0000544 Favorites.ITEM_TYPE_WIDGET_SEARCH,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800545 };
Bjorn Bringert7984c942009-12-09 15:38:25 +0000546
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800547 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE, bindSources);
548
549 Cursor c = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800550
551 db.beginTransaction();
552 try {
553 // Select and iterate through each matching widget
Bjorn Bringert7984c942009-12-09 15:38:25 +0000554 c = db.query(TABLE_FAVORITES, new String[] { Favorites._ID, Favorites.ITEM_TYPE },
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800555 selectWhere, null, null, null, null);
556
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800557 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800558
559 final ContentValues values = new ContentValues();
560 while (c != null && c.moveToNext()) {
561 long favoriteId = c.getLong(0);
Bjorn Bringert7984c942009-12-09 15:38:25 +0000562 int favoriteType = c.getInt(1);
563
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700564 // Allocate and update database with new appWidgetId
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800565 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700566 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800567
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800568 if (LOGD) {
569 Log.d(TAG, "allocated appWidgetId=" + appWidgetId
570 + " for favoriteId=" + favoriteId);
571 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800572 values.clear();
Bjorn Bringert7984c942009-12-09 15:38:25 +0000573 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
574 values.put(Favorites.APPWIDGET_ID, appWidgetId);
575
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800576 // Original widgets might not have valid spans when upgrading
Bjorn Bringert7984c942009-12-09 15:38:25 +0000577 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
578 values.put(LauncherSettings.Favorites.SPANX, 4);
579 values.put(LauncherSettings.Favorites.SPANY, 1);
580 } else {
581 values.put(LauncherSettings.Favorites.SPANX, 2);
582 values.put(LauncherSettings.Favorites.SPANY, 2);
583 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800584
585 String updateWhere = Favorites._ID + "=" + favoriteId;
586 db.update(TABLE_FAVORITES, values, updateWhere, null);
Bjorn Bringert34251342009-12-15 13:33:11 +0000587
Bjorn Bringert34251342009-12-15 13:33:11 +0000588 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)) {
Romain Guy693599f2010-03-23 10:58:18 -0700665 added = addAppWidget(db, values, a, packageManager);
Mike Cleronb87bd162009-10-30 16:36:56 -0700666 } 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 {
Romain Guy693599f2010-03-23 10:58:18 -0700690 ComponentName cn;
691 try {
692 cn = new ComponentName(packageName, className);
693 info = packageManager.getActivityInfo(cn, 0);
694 } catch (PackageManager.NameNotFoundException nnfe) {
695 String[] packages = packageManager.currentToCanonicalPackageNames(
696 new String[] { packageName });
697 cn = new ComponentName(packages[0], className);
698 info = packageManager.getActivityInfo(cn, 0);
699 }
700
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700701 intent.setComponent(cn);
Romain Guy693599f2010-03-23 10:58:18 -0700702 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
703 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Romain Guy1ce1a242009-06-23 17:34:54 -0700704 values.put(Favorites.INTENT, intent.toUri(0));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700705 values.put(Favorites.TITLE, info.loadLabel(packageManager).toString());
706 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPLICATION);
707 values.put(Favorites.SPANX, 1);
708 values.put(Favorites.SPANY, 1);
709 db.insert(TABLE_FAVORITES, null, values);
710 } catch (PackageManager.NameNotFoundException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800711 Log.w(TAG, "Unable to add favorite: " + packageName +
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700712 "/" + className, e);
713 return false;
714 }
715 return true;
716 }
717
Bjorn Bringertcd8fec02010-01-14 13:26:43 +0000718 private ComponentName getSearchWidgetProvider() {
719 SearchManager searchManager =
720 (SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE);
721 ComponentName searchComponent = searchManager.getGlobalSearchActivity();
722 if (searchComponent == null) return null;
723 return getProviderInPackage(searchComponent.getPackageName());
724 }
725
726 /**
727 * Gets an appwidget provider from the given package. If the package contains more than
728 * one appwidget provider, an arbitrary one is returned.
729 */
730 private ComponentName getProviderInPackage(String packageName) {
731 AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
732 List<AppWidgetProviderInfo> providers = appWidgetManager.getInstalledProviders();
733 if (providers == null) return null;
734 final int providerCount = providers.size();
735 for (int i = 0; i < providerCount; i++) {
736 ComponentName provider = providers.get(i).provider;
737 if (provider != null && provider.getPackageName().equals(packageName)) {
738 return provider;
739 }
740 }
741 return null;
742 }
743
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700744 private boolean addSearchWidget(SQLiteDatabase db, ContentValues values) {
Bjorn Bringertcd8fec02010-01-14 13:26:43 +0000745 ComponentName cn = getSearchWidgetProvider();
Bjorn Bringert7984c942009-12-09 15:38:25 +0000746 return addAppWidget(db, values, cn, 4, 1);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700747 }
748
749 private boolean addClockWidget(SQLiteDatabase db, ContentValues values) {
Bjorn Bringert34251342009-12-15 13:33:11 +0000750 ComponentName cn = new ComponentName("com.android.alarmclock",
751 "com.android.alarmclock.AnalogAppWidgetProvider");
752 return addAppWidget(db, values, cn, 2, 2);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753 }
Mike Cleronb87bd162009-10-30 16:36:56 -0700754
Romain Guy693599f2010-03-23 10:58:18 -0700755 private boolean addAppWidget(SQLiteDatabase db, ContentValues values, TypedArray a,
756 PackageManager packageManager) {
757
Mike Cleronb87bd162009-10-30 16:36:56 -0700758 String packageName = a.getString(R.styleable.Favorite_packageName);
759 String className = a.getString(R.styleable.Favorite_className);
760
761 if (packageName == null || className == null) {
762 return false;
763 }
Romain Guy693599f2010-03-23 10:58:18 -0700764
765 boolean hasPackage = true;
Mike Cleronb87bd162009-10-30 16:36:56 -0700766 ComponentName cn = new ComponentName(packageName, className);
Romain Guy693599f2010-03-23 10:58:18 -0700767 try {
768 packageManager.getReceiverInfo(cn, 0);
769 } catch (Exception e) {
770 String[] packages = packageManager.currentToCanonicalPackageNames(
771 new String[] { packageName });
772 cn = new ComponentName(packages[0], className);
773 try {
774 packageManager.getReceiverInfo(cn, 0);
775 } catch (Exception e1) {
776 hasPackage = false;
777 }
778 }
779
780 if (hasPackage) {
781 int spanX = a.getInt(R.styleable.Favorite_spanX, 0);
782 int spanY = a.getInt(R.styleable.Favorite_spanY, 0);
783 return addAppWidget(db, values, cn, spanX, spanY);
784 }
785
786 return false;
Bjorn Bringert7984c942009-12-09 15:38:25 +0000787 }
788
789 private boolean addAppWidget(SQLiteDatabase db, ContentValues values, ComponentName cn,
790 int spanX, int spanY) {
Mike Cleronb87bd162009-10-30 16:36:56 -0700791 boolean allocatedAppWidgets = false;
792 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
793
794 try {
795 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
796
797 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
Bjorn Bringert7984c942009-12-09 15:38:25 +0000798 values.put(Favorites.SPANX, spanX);
799 values.put(Favorites.SPANY, spanY);
Mike Cleronb87bd162009-10-30 16:36:56 -0700800 values.put(Favorites.APPWIDGET_ID, appWidgetId);
801 db.insert(TABLE_FAVORITES, null, values);
802
803 allocatedAppWidgets = true;
804
805 appWidgetManager.bindAppWidgetId(appWidgetId, cn);
806 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800807 Log.e(TAG, "Problem allocating appWidgetId", ex);
Mike Cleronb87bd162009-10-30 16:36:56 -0700808 }
809
810 return allocatedAppWidgets;
811 }
812
813 private boolean addUriShortcut(SQLiteDatabase db, ContentValues values,
814 TypedArray a) {
815 Resources r = mContext.getResources();
816
817 final int iconResId = a.getResourceId(R.styleable.Favorite_icon, 0);
818 final int titleResId = a.getResourceId(R.styleable.Favorite_title, 0);
819
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800820 Intent intent;
Mike Cleronb87bd162009-10-30 16:36:56 -0700821 String uri = null;
822 try {
823 uri = a.getString(R.styleable.Favorite_uri);
824 intent = Intent.parseUri(uri, 0);
825 } catch (URISyntaxException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800826 Log.w(TAG, "Shortcut has malformed uri: " + uri);
Mike Cleronb87bd162009-10-30 16:36:56 -0700827 return false; // Oh well
828 }
829
830 if (iconResId == 0 || titleResId == 0) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800831 Log.w(TAG, "Shortcut is missing title or icon resource ID");
Mike Cleronb87bd162009-10-30 16:36:56 -0700832 return false;
833 }
834
835 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
836 values.put(Favorites.INTENT, intent.toUri(0));
837 values.put(Favorites.TITLE, r.getString(titleResId));
838 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_SHORTCUT);
839 values.put(Favorites.SPANX, 1);
840 values.put(Favorites.SPANY, 1);
841 values.put(Favorites.ICON_TYPE, Favorites.ICON_TYPE_RESOURCE);
842 values.put(Favorites.ICON_PACKAGE, mContext.getPackageName());
843 values.put(Favorites.ICON_RESOURCE, r.getResourceName(iconResId));
844
845 db.insert(TABLE_FAVORITES, null, values);
846
847 return true;
848 }
849 }
850
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800851 /**
852 * Build a query string that will match any row where the column matches
853 * anything in the values list.
854 */
855 static String buildOrWhereString(String column, int[] values) {
856 StringBuilder selectWhere = new StringBuilder();
857 for (int i = values.length - 1; i >= 0; i--) {
858 selectWhere.append(column).append("=").append(values[i]);
859 if (i > 0) {
860 selectWhere.append(" OR ");
861 }
862 }
863 return selectWhere.toString();
864 }
865
866 static class SqlArguments {
867 public final String table;
868 public final String where;
869 public final String[] args;
870
871 SqlArguments(Uri url, String where, String[] args) {
872 if (url.getPathSegments().size() == 1) {
873 this.table = url.getPathSegments().get(0);
874 this.where = where;
875 this.args = args;
876 } else if (url.getPathSegments().size() != 2) {
877 throw new IllegalArgumentException("Invalid URI: " + url);
878 } else if (!TextUtils.isEmpty(where)) {
879 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
880 } else {
881 this.table = url.getPathSegments().get(0);
882 this.where = "_id=" + ContentUris.parseId(url);
883 this.args = null;
884 }
885 }
886
887 SqlArguments(Uri url) {
888 if (url.getPathSegments().size() == 1) {
889 table = url.getPathSegments().get(0);
890 where = null;
891 args = null;
892 } else {
893 throw new IllegalArgumentException("Invalid URI: " + url);
894 }
895 }
896 }
897}