blob: d40e1ecf9c7164a1a745b93c63f659a913e8e3b3 [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
The Android Open Source Project7376fae2009-03-11 12:11:58 -070019import android.appwidget.AppWidgetHost;
Mike Cleronb87bd162009-10-30 16:36:56 -070020import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.content.ContentProvider;
22import android.content.Context;
23import android.content.ContentValues;
24import android.content.Intent;
25import android.content.ComponentName;
26import android.content.ContentUris;
27import android.content.ContentResolver;
Mike Cleronb87bd162009-10-30 16:36:56 -070028import android.content.res.Resources;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070029import android.content.res.XmlResourceParser;
30import android.content.res.TypedArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.content.pm.PackageManager;
32import android.content.pm.ActivityInfo;
33import android.database.sqlite.SQLiteOpenHelper;
34import android.database.sqlite.SQLiteDatabase;
35import android.database.sqlite.SQLiteQueryBuilder;
36import android.database.Cursor;
37import android.database.SQLException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.util.Log;
39import android.util.Xml;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070040import android.util.AttributeSet;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.net.Uri;
42import android.text.TextUtils;
43import android.os.*;
44import android.provider.Settings;
45
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import java.io.IOException;
Mike Cleronb87bd162009-10-30 16:36:56 -070047import java.net.URISyntaxException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import java.util.ArrayList;
49
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import org.xmlpull.v1.XmlPullParserException;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070051import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import com.android.internal.util.XmlUtils;
Joe Onoratoa5902522009-07-30 13:37:37 -070053import com.android.launcher2.LauncherSettings.Favorites;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054
55public class LauncherProvider extends ContentProvider {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080056 private static final String TAG = "Launcher.LauncherProvider";
57 private static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058
59 private static final String DATABASE_NAME = "launcher.db";
60
Bjorn Bringert7984c942009-12-09 15:38:25 +000061 private static final int DATABASE_VERSION = 7;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062
Joe Onoratoa5902522009-07-30 13:37:37 -070063 static final String AUTHORITY = "com.android.launcher2.settings";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064
Joe Onoratoa5902522009-07-30 13:37:37 -070065 static final String EXTRA_BIND_SOURCES = "com.android.launcher2.settings.bindsources";
66 static final String EXTRA_BIND_TARGETS = "com.android.launcher2.settings.bindtargets";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067
68 static final String TABLE_FAVORITES = "favorites";
69 static final String PARAMETER_NOTIFY = "notify";
70
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -070071 /**
Romain Guy73b979d2009-06-09 12:57:21 -070072 * {@link Uri} triggered at any registered {@link android.database.ContentObserver} when
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -070073 * {@link AppWidgetHost#deleteHost()} is called during database creation.
74 * Use this to recall {@link AppWidgetHost#startListening()} if needed.
75 */
76 static final Uri CONTENT_APPWIDGET_RESET_URI =
77 Uri.parse("content://" + AUTHORITY + "/appWidgetReset");
78
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079 private SQLiteOpenHelper mOpenHelper;
80
81 @Override
82 public boolean onCreate() {
83 mOpenHelper = new DatabaseHelper(getContext());
84 return true;
85 }
86
87 @Override
88 public String getType(Uri uri) {
89 SqlArguments args = new SqlArguments(uri, null, null);
90 if (TextUtils.isEmpty(args.where)) {
91 return "vnd.android.cursor.dir/" + args.table;
92 } else {
93 return "vnd.android.cursor.item/" + args.table;
94 }
95 }
96
97 @Override
98 public Cursor query(Uri uri, String[] projection, String selection,
99 String[] selectionArgs, String sortOrder) {
100
101 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
102 SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
103 qb.setTables(args.table);
104
Romain Guy73b979d2009-06-09 12:57:21 -0700105 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106 Cursor result = qb.query(db, projection, args.where, args.args, null, null, sortOrder);
107 result.setNotificationUri(getContext().getContentResolver(), uri);
108
109 return result;
110 }
111
112 @Override
113 public Uri insert(Uri uri, ContentValues initialValues) {
114 SqlArguments args = new SqlArguments(uri);
115
116 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
117 final long rowId = db.insert(args.table, null, initialValues);
118 if (rowId <= 0) return null;
119
120 uri = ContentUris.withAppendedId(uri, rowId);
121 sendNotify(uri);
122
123 return uri;
124 }
125
126 @Override
127 public int bulkInsert(Uri uri, ContentValues[] values) {
128 SqlArguments args = new SqlArguments(uri);
129
130 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
131 db.beginTransaction();
132 try {
133 int numValues = values.length;
134 for (int i = 0; i < numValues; i++) {
135 if (db.insert(args.table, null, values[i]) < 0) return 0;
136 }
137 db.setTransactionSuccessful();
138 } finally {
139 db.endTransaction();
140 }
141
142 sendNotify(uri);
143 return values.length;
144 }
145
146 @Override
147 public int delete(Uri uri, String selection, String[] selectionArgs) {
148 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
149
150 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
151 int count = db.delete(args.table, args.where, args.args);
152 if (count > 0) sendNotify(uri);
153
154 return count;
155 }
156
157 @Override
158 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
159 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
160
161 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
162 int count = db.update(args.table, values, args.where, args.args);
163 if (count > 0) sendNotify(uri);
164
165 return count;
166 }
167
168 private void sendNotify(Uri uri) {
169 String notify = uri.getQueryParameter(PARAMETER_NOTIFY);
170 if (notify == null || "true".equals(notify)) {
171 getContext().getContentResolver().notifyChange(uri, null);
172 }
173 }
174
175 private static class DatabaseHelper extends SQLiteOpenHelper {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 private static final String TAG_FAVORITES = "favorites";
177 private static final String TAG_FAVORITE = "favorite";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700178 private static final String TAG_CLOCK = "clock";
179 private static final String TAG_SEARCH = "search";
Mike Cleronb87bd162009-10-30 16:36:56 -0700180 private static final String TAG_APPWIDGET = "appwidget";
181 private static final String TAG_SHORTCUT = "shortcut";
182
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 private final Context mContext;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700184 private final AppWidgetHost mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185
186 DatabaseHelper(Context context) {
187 super(context, DATABASE_NAME, null, DATABASE_VERSION);
188 mContext = context;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700189 mAppWidgetHost = new AppWidgetHost(context, Launcher.APPWIDGET_HOST_ID);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 }
191
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700192 /**
193 * Send notification that we've deleted the {@link AppWidgetHost},
194 * probably as part of the initial database creation. The receiver may
195 * want to re-call {@link AppWidgetHost#startListening()} to ensure
196 * callbacks are correctly set.
197 */
198 private void sendAppWidgetResetNotify() {
199 final ContentResolver resolver = mContext.getContentResolver();
200 resolver.notifyChange(CONTENT_APPWIDGET_RESET_URI, null);
201 }
202
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203 @Override
204 public void onCreate(SQLiteDatabase db) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800205 if (LOGD) Log.d(TAG, "creating new launcher database");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206
207 db.execSQL("CREATE TABLE favorites (" +
208 "_id INTEGER PRIMARY KEY," +
209 "title TEXT," +
210 "intent TEXT," +
211 "container INTEGER," +
212 "screen INTEGER," +
213 "cellX INTEGER," +
214 "cellY INTEGER," +
215 "spanX INTEGER," +
216 "spanY INTEGER," +
217 "itemType INTEGER," +
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700218 "appWidgetId INTEGER NOT NULL DEFAULT -1," +
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 "isShortcut INTEGER," +
220 "iconType INTEGER," +
221 "iconPackage TEXT," +
222 "iconResource TEXT," +
223 "icon BLOB," +
224 "uri TEXT," +
225 "displayMode INTEGER" +
226 ");");
227
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700228 // Database was just created, so wipe any previous widgets
229 if (mAppWidgetHost != null) {
230 mAppWidgetHost.deleteHost();
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700231 sendAppWidgetResetNotify();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232 }
233
234 if (!convertDatabase(db)) {
235 // Populate favorites table with initial favorites
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700236 loadFavorites(db);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237 }
238 }
239
240 private boolean convertDatabase(SQLiteDatabase db) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800241 if (LOGD) Log.d(TAG, "converting database from an older format, but not onUpgrade");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242 boolean converted = false;
243
244 final Uri uri = Uri.parse("content://" + Settings.AUTHORITY +
245 "/old_favorites?notify=true");
246 final ContentResolver resolver = mContext.getContentResolver();
247 Cursor cursor = null;
248
249 try {
250 cursor = resolver.query(uri, null, null, null, null);
251 } catch (Exception e) {
252 // Ignore
253 }
254
255 // We already have a favorites database in the old provider
256 if (cursor != null && cursor.getCount() > 0) {
257 try {
258 converted = copyFromCursor(db, cursor) > 0;
259 } finally {
260 cursor.close();
261 }
262
263 if (converted) {
264 resolver.delete(uri, null, null);
265 }
266 }
267
268 if (converted) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700269 // Convert widgets from this import into widgets
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800270 if (LOGD) Log.d(TAG, "converted and now triggering widget upgrade");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271 convertWidgets(db);
272 }
273
274 return converted;
275 }
276
277 private int copyFromCursor(SQLiteDatabase db, Cursor c) {
Romain Guy73b979d2009-06-09 12:57:21 -0700278 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279 final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
280 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
281 final int iconTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
282 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
283 final int iconPackageIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
284 final int iconResourceIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
285 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
286 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
287 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
288 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
289 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
290 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
291 final int displayModeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
292
293 ContentValues[] rows = new ContentValues[c.getCount()];
294 int i = 0;
295 while (c.moveToNext()) {
296 ContentValues values = new ContentValues(c.getColumnCount());
Romain Guy73b979d2009-06-09 12:57:21 -0700297 values.put(LauncherSettings.Favorites._ID, c.getLong(idIndex));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800298 values.put(LauncherSettings.Favorites.INTENT, c.getString(intentIndex));
299 values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex));
300 values.put(LauncherSettings.Favorites.ICON_TYPE, c.getInt(iconTypeIndex));
301 values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex));
302 values.put(LauncherSettings.Favorites.ICON_PACKAGE, c.getString(iconPackageIndex));
303 values.put(LauncherSettings.Favorites.ICON_RESOURCE, c.getString(iconResourceIndex));
304 values.put(LauncherSettings.Favorites.CONTAINER, c.getInt(containerIndex));
305 values.put(LauncherSettings.Favorites.ITEM_TYPE, c.getInt(itemTypeIndex));
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700306 values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800307 values.put(LauncherSettings.Favorites.SCREEN, c.getInt(screenIndex));
308 values.put(LauncherSettings.Favorites.CELLX, c.getInt(cellXIndex));
309 values.put(LauncherSettings.Favorites.CELLY, c.getInt(cellYIndex));
310 values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex));
311 values.put(LauncherSettings.Favorites.DISPLAY_MODE, c.getInt(displayModeIndex));
312 rows[i++] = values;
313 }
314
315 db.beginTransaction();
316 int total = 0;
317 try {
318 int numValues = rows.length;
319 for (i = 0; i < numValues; i++) {
320 if (db.insert(TABLE_FAVORITES, null, rows[i]) < 0) {
321 return 0;
322 } else {
323 total++;
324 }
325 }
326 db.setTransactionSuccessful();
327 } finally {
328 db.endTransaction();
329 }
330
331 return total;
332 }
333
334 @Override
335 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800336 if (LOGD) Log.d(TAG, "onUpgrade triggered");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800337
338 int version = oldVersion;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700339 if (version < 3) {
340 // upgrade 1,2 -> 3 added appWidgetId column
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800341 db.beginTransaction();
342 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700343 // Insert new column for holding appWidgetIds
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800344 db.execSQL("ALTER TABLE favorites " +
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700345 "ADD COLUMN appWidgetId INTEGER NOT NULL DEFAULT -1;");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800346 db.setTransactionSuccessful();
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700347 version = 3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800348 } catch (SQLException ex) {
349 // Old version remains, which means we wipe old data
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800350 Log.e(TAG, ex.getMessage(), ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 } finally {
352 db.endTransaction();
353 }
354
355 // Convert existing widgets only if table upgrade was successful
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700356 if (version == 3) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 convertWidgets(db);
358 }
359 }
Romain Guy73b979d2009-06-09 12:57:21 -0700360
361 if (version < 4) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800362 version = 4;
Romain Guy73b979d2009-06-09 12:57:21 -0700363 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800365 if (version < 5) {
366 // We went from 3 to 5 screens. Move everything 1 to the right
367 db.beginTransaction();
368 try {
369 db.execSQL("UPDATE favorites SET screen=(screen + 1);");
370 db.setTransactionSuccessful();
371 version = 5;
372 } catch (SQLException ex) {
373 // Old version remains, which means we wipe old data
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800374 Log.e(TAG, ex.getMessage(), ex);
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800375 } finally {
376 db.endTransaction();
377 }
378 }
379
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800380 if (version < 6) {
381 if (updateContactsShortcuts(db)) {
382 version = 6;
383 }
384 }
Bjorn Bringert7984c942009-12-09 15:38:25 +0000385
386 if (version < 7) {
387 // Version 7 gets rid of the special search widget.
388 convertWidgets(db);
389 version = 7;
390 }
391
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 if (version != DATABASE_VERSION) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800393 Log.w(TAG, "Destroying all old data.");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES);
395 onCreate(db);
396 }
397 }
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800398
399 private boolean updateContactsShortcuts(SQLiteDatabase db) {
400 Cursor c = null;
401 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE,
402 new int[] { Favorites.ITEM_TYPE_SHORTCUT });
403
404 db.beginTransaction();
405 try {
406 // Select and iterate through each matching widget
407 c = db.query(TABLE_FAVORITES, new String[] { Favorites._ID, Favorites.INTENT },
408 selectWhere, null, null, null, null);
409
410 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
411
412 final ContentValues values = new ContentValues();
413 final int idIndex = c.getColumnIndex(Favorites._ID);
414 final int intentIndex = c.getColumnIndex(Favorites.INTENT);
415
416 while (c != null && c.moveToNext()) {
417 long favoriteId = c.getLong(idIndex);
418 final String intentUri = c.getString(intentIndex);
419 if (intentUri != null) {
420 try {
421 Intent intent = Intent.parseUri(intentUri, 0);
422 android.util.Log.d("Home", intent.toString());
423 final Uri uri = intent.getData();
424 final String data = uri.toString();
425 if (Intent.ACTION_VIEW.equals(intent.getAction()) &&
426 (data.startsWith("content://contacts/people/") ||
427 data.startsWith("content://com.android.contacts/contacts/lookup/"))) {
428
429 intent = new Intent("com.android.contacts.action.QUICK_CONTACT");
430 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
431 Intent.FLAG_ACTIVITY_CLEAR_TOP |
432 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
433
434 intent.setData(uri);
435 intent.putExtra("mode", 3);
436 intent.putExtra("exclude_mimes", (String[]) null);
437
438 values.clear();
439 values.put(LauncherSettings.Favorites.INTENT, intent.toUri(0));
440
441 String updateWhere = Favorites._ID + "=" + favoriteId;
442 db.update(TABLE_FAVORITES, values, updateWhere, null);
443 }
444 } catch (RuntimeException ex) {
445 Log.e(TAG, "Problem upgrading shortcut", ex);
446 } catch (URISyntaxException e) {
447 Log.e(TAG, "Problem upgrading shortcut", e);
448 }
449 }
450 }
451
452 db.setTransactionSuccessful();
453 } catch (SQLException ex) {
454 Log.w(TAG, "Problem while upgrading contacts", ex);
455 return false;
456 } finally {
457 db.endTransaction();
458 if (c != null) {
459 c.close();
460 }
461 }
462
463 return true;
464 }
465
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700467 * Upgrade existing clock and photo frame widgets into their new widget
468 * equivalents. This method allocates appWidgetIds, and then hands off to
469 * LauncherAppWidgetBinder to finish the actual binding.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800470 */
471 private void convertWidgets(SQLiteDatabase db) {
Bjorn Bringert34251342009-12-15 13:33:11 +0000472 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800473 final int[] bindSources = new int[] {
474 Favorites.ITEM_TYPE_WIDGET_CLOCK,
475 Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME,
Bjorn Bringert7984c942009-12-09 15:38:25 +0000476 Favorites.ITEM_TYPE_WIDGET_SEARCH,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800477 };
Bjorn Bringert7984c942009-12-09 15:38:25 +0000478
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE, bindSources);
480
481 Cursor c = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800482
483 db.beginTransaction();
484 try {
485 // Select and iterate through each matching widget
Bjorn Bringert7984c942009-12-09 15:38:25 +0000486 c = db.query(TABLE_FAVORITES, new String[] { Favorites._ID, Favorites.ITEM_TYPE },
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800487 selectWhere, null, null, null, null);
488
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800489 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800490
491 final ContentValues values = new ContentValues();
492 while (c != null && c.moveToNext()) {
493 long favoriteId = c.getLong(0);
Bjorn Bringert7984c942009-12-09 15:38:25 +0000494 int favoriteType = c.getInt(1);
495
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700496 // Allocate and update database with new appWidgetId
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800497 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700498 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800499
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800500 if (LOGD) {
501 Log.d(TAG, "allocated appWidgetId=" + appWidgetId
502 + " for favoriteId=" + favoriteId);
503 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800504 values.clear();
Bjorn Bringert7984c942009-12-09 15:38:25 +0000505 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
506 values.put(Favorites.APPWIDGET_ID, appWidgetId);
507
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800508 // Original widgets might not have valid spans when upgrading
Bjorn Bringert7984c942009-12-09 15:38:25 +0000509 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
510 values.put(LauncherSettings.Favorites.SPANX, 4);
511 values.put(LauncherSettings.Favorites.SPANY, 1);
512 } else {
513 values.put(LauncherSettings.Favorites.SPANX, 2);
514 values.put(LauncherSettings.Favorites.SPANY, 2);
515 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800516
517 String updateWhere = Favorites._ID + "=" + favoriteId;
518 db.update(TABLE_FAVORITES, values, updateWhere, null);
Bjorn Bringert34251342009-12-15 13:33:11 +0000519
520 ComponentName cn = null;
521 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_CLOCK) {
522 appWidgetManager.bindAppWidgetId(appWidgetId,
523 new ComponentName("com.android.alarmclock",
524 "com.android.alarmclock.AnalogAppWidgetProvider"));
525 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME) {
526 appWidgetManager.bindAppWidgetId(appWidgetId,
527 new ComponentName("com.android.camera",
528 "com.android.camera.PhotoAppWidgetProvider"));
529 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
530 appWidgetManager.bindAppWidgetId(appWidgetId,
531 new ComponentName("com.android.quicksearchbox",
532 "com.android.quicksearchbox.SearchWidgetProvider"));
533 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800534 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800535 Log.e(TAG, "Problem allocating appWidgetId", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800536 }
537 }
538
539 db.setTransactionSuccessful();
540 } catch (SQLException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800541 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800542 } finally {
543 db.endTransaction();
544 if (c != null) {
545 c.close();
546 }
547 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800548 }
549
550 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800551 * Loads the default set of favorite packages from an xml file.
552 *
553 * @param db The database to write the values into
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800554 */
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700555 private int loadFavorites(SQLiteDatabase db) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800556 Intent intent = new Intent(Intent.ACTION_MAIN, null);
557 intent.addCategory(Intent.CATEGORY_LAUNCHER);
558 ContentValues values = new ContentValues();
559
560 PackageManager packageManager = mContext.getPackageManager();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561 int i = 0;
562 try {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700563 XmlResourceParser parser = mContext.getResources().getXml(R.xml.default_workspace);
564 AttributeSet attrs = Xml.asAttributeSet(parser);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800565 XmlUtils.beginDocument(parser, TAG_FAVORITES);
566
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700567 final int depth = parser.getDepth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800568
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700569 int type;
570 while (((type = parser.next()) != XmlPullParser.END_TAG ||
571 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
572
573 if (type != XmlPullParser.START_TAG) {
574 continue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800575 }
576
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700577 boolean added = false;
578 final String name = parser.getName();
579
580 TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.Favorite);
581
582 values.clear();
583 values.put(LauncherSettings.Favorites.CONTAINER,
584 LauncherSettings.Favorites.CONTAINER_DESKTOP);
585 values.put(LauncherSettings.Favorites.SCREEN,
586 a.getString(R.styleable.Favorite_screen));
587 values.put(LauncherSettings.Favorites.CELLX,
588 a.getString(R.styleable.Favorite_x));
589 values.put(LauncherSettings.Favorites.CELLY,
590 a.getString(R.styleable.Favorite_y));
591
592 if (TAG_FAVORITE.equals(name)) {
Mike Cleronb87bd162009-10-30 16:36:56 -0700593 added = addAppShortcut(db, values, a, packageManager, intent);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700594 } else if (TAG_SEARCH.equals(name)) {
595 added = addSearchWidget(db, values);
596 } else if (TAG_CLOCK.equals(name)) {
597 added = addClockWidget(db, values);
Mike Cleronb87bd162009-10-30 16:36:56 -0700598 } else if (TAG_APPWIDGET.equals(name)) {
599 added = addAppWidget(db, values, a);
600 } else if (TAG_SHORTCUT.equals(name)) {
601 added = addUriShortcut(db, values, a);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800602 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700603
604 if (added) i++;
605
606 a.recycle();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800607 }
608 } catch (XmlPullParserException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800609 Log.w(TAG, "Got exception parsing favorites.", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800610 } catch (IOException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800611 Log.w(TAG, "Got exception parsing favorites.", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800612 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700613
614 return i;
615 }
616
Mike Cleronb87bd162009-10-30 16:36:56 -0700617 private boolean addAppShortcut(SQLiteDatabase db, ContentValues values, TypedArray a,
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700618 PackageManager packageManager, Intent intent) {
619
620 ActivityInfo info;
621 String packageName = a.getString(R.styleable.Favorite_packageName);
622 String className = a.getString(R.styleable.Favorite_className);
623 try {
624 ComponentName cn = new ComponentName(packageName, className);
625 info = packageManager.getActivityInfo(cn, 0);
626 intent.setComponent(cn);
Dianne Hackbornbd2e54d2009-03-24 21:00:33 -0700627 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
628 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Romain Guy1ce1a242009-06-23 17:34:54 -0700629 values.put(Favorites.INTENT, intent.toUri(0));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700630 values.put(Favorites.TITLE, info.loadLabel(packageManager).toString());
631 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPLICATION);
632 values.put(Favorites.SPANX, 1);
633 values.put(Favorites.SPANY, 1);
634 db.insert(TABLE_FAVORITES, null, values);
635 } catch (PackageManager.NameNotFoundException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800636 Log.w(TAG, "Unable to add favorite: " + packageName +
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700637 "/" + className, e);
638 return false;
639 }
640 return true;
641 }
642
643 private boolean addSearchWidget(SQLiteDatabase db, ContentValues values) {
Bjorn Bringert7984c942009-12-09 15:38:25 +0000644 ComponentName cn = new ComponentName("com.android.quicksearchbox",
645 "com.android.quicksearchbox.SearchWidgetProvider");
646 return addAppWidget(db, values, cn, 4, 1);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700647 }
648
649 private boolean addClockWidget(SQLiteDatabase db, ContentValues values) {
Bjorn Bringert34251342009-12-15 13:33:11 +0000650 ComponentName cn = new ComponentName("com.android.alarmclock",
651 "com.android.alarmclock.AnalogAppWidgetProvider");
652 return addAppWidget(db, values, cn, 2, 2);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 }
Mike Cleronb87bd162009-10-30 16:36:56 -0700654
655 private boolean addAppWidget(SQLiteDatabase db, ContentValues values, TypedArray a) {
Mike Cleronb87bd162009-10-30 16:36:56 -0700656 String packageName = a.getString(R.styleable.Favorite_packageName);
657 String className = a.getString(R.styleable.Favorite_className);
658
659 if (packageName == null || className == null) {
660 return false;
661 }
662
663 ComponentName cn = new ComponentName(packageName, className);
Bjorn Bringert7984c942009-12-09 15:38:25 +0000664 int spanX = a.getInt(R.styleable.Favorite_spanX, 0);
665 int spanY = a.getInt(R.styleable.Favorite_spanY, 0);
666 return addAppWidget(db, values, cn, spanX, spanY);
667 }
668
669 private boolean addAppWidget(SQLiteDatabase db, ContentValues values, ComponentName cn,
670 int spanX, int spanY) {
Mike Cleronb87bd162009-10-30 16:36:56 -0700671 boolean allocatedAppWidgets = false;
672 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
673
674 try {
675 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
676
677 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
Bjorn Bringert7984c942009-12-09 15:38:25 +0000678 values.put(Favorites.SPANX, spanX);
679 values.put(Favorites.SPANY, spanY);
Mike Cleronb87bd162009-10-30 16:36:56 -0700680 values.put(Favorites.APPWIDGET_ID, appWidgetId);
681 db.insert(TABLE_FAVORITES, null, values);
682
683 allocatedAppWidgets = true;
684
685 appWidgetManager.bindAppWidgetId(appWidgetId, cn);
686 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800687 Log.e(TAG, "Problem allocating appWidgetId", ex);
Mike Cleronb87bd162009-10-30 16:36:56 -0700688 }
689
690 return allocatedAppWidgets;
691 }
692
693 private boolean addUriShortcut(SQLiteDatabase db, ContentValues values,
694 TypedArray a) {
695 Resources r = mContext.getResources();
696
697 final int iconResId = a.getResourceId(R.styleable.Favorite_icon, 0);
698 final int titleResId = a.getResourceId(R.styleable.Favorite_title, 0);
699
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800700 Intent intent;
Mike Cleronb87bd162009-10-30 16:36:56 -0700701 String uri = null;
702 try {
703 uri = a.getString(R.styleable.Favorite_uri);
704 intent = Intent.parseUri(uri, 0);
705 } catch (URISyntaxException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800706 Log.w(TAG, "Shortcut has malformed uri: " + uri);
Mike Cleronb87bd162009-10-30 16:36:56 -0700707 return false; // Oh well
708 }
709
710 if (iconResId == 0 || titleResId == 0) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800711 Log.w(TAG, "Shortcut is missing title or icon resource ID");
Mike Cleronb87bd162009-10-30 16:36:56 -0700712 return false;
713 }
714
715 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
716 values.put(Favorites.INTENT, intent.toUri(0));
717 values.put(Favorites.TITLE, r.getString(titleResId));
718 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_SHORTCUT);
719 values.put(Favorites.SPANX, 1);
720 values.put(Favorites.SPANY, 1);
721 values.put(Favorites.ICON_TYPE, Favorites.ICON_TYPE_RESOURCE);
722 values.put(Favorites.ICON_PACKAGE, mContext.getPackageName());
723 values.put(Favorites.ICON_RESOURCE, r.getResourceName(iconResId));
724
725 db.insert(TABLE_FAVORITES, null, values);
726
727 return true;
728 }
729 }
730
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 /**
732 * Build a query string that will match any row where the column matches
733 * anything in the values list.
734 */
735 static String buildOrWhereString(String column, int[] values) {
736 StringBuilder selectWhere = new StringBuilder();
737 for (int i = values.length - 1; i >= 0; i--) {
738 selectWhere.append(column).append("=").append(values[i]);
739 if (i > 0) {
740 selectWhere.append(" OR ");
741 }
742 }
743 return selectWhere.toString();
744 }
745
746 static class SqlArguments {
747 public final String table;
748 public final String where;
749 public final String[] args;
750
751 SqlArguments(Uri url, String where, String[] args) {
752 if (url.getPathSegments().size() == 1) {
753 this.table = url.getPathSegments().get(0);
754 this.where = where;
755 this.args = args;
756 } else if (url.getPathSegments().size() != 2) {
757 throw new IllegalArgumentException("Invalid URI: " + url);
758 } else if (!TextUtils.isEmpty(where)) {
759 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
760 } else {
761 this.table = url.getPathSegments().get(0);
762 this.where = "_id=" + ContentUris.parseId(url);
763 this.args = null;
764 }
765 }
766
767 SqlArguments(Uri url) {
768 if (url.getPathSegments().size() == 1) {
769 table = url.getPathSegments().get(0);
770 where = null;
771 args = null;
772 } else {
773 throw new IllegalArgumentException("Invalid URI: " + url);
774 }
775 }
776 }
777}