blob: abd5e5da662c193c0c905fea98b21ecc3f4c2f44 [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
Bjorn Bringert93c45762009-12-16 13:19:47 +0000468 * equivalents.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469 */
470 private void convertWidgets(SQLiteDatabase db) {
Bjorn Bringert34251342009-12-15 13:33:11 +0000471 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800472 final int[] bindSources = new int[] {
473 Favorites.ITEM_TYPE_WIDGET_CLOCK,
474 Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME,
Bjorn Bringert7984c942009-12-09 15:38:25 +0000475 Favorites.ITEM_TYPE_WIDGET_SEARCH,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800476 };
Bjorn Bringert7984c942009-12-09 15:38:25 +0000477
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800478 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE, bindSources);
479
480 Cursor c = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800481
482 db.beginTransaction();
483 try {
484 // Select and iterate through each matching widget
Bjorn Bringert7984c942009-12-09 15:38:25 +0000485 c = db.query(TABLE_FAVORITES, new String[] { Favorites._ID, Favorites.ITEM_TYPE },
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800486 selectWhere, null, null, null, null);
487
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800488 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800489
490 final ContentValues values = new ContentValues();
491 while (c != null && c.moveToNext()) {
492 long favoriteId = c.getLong(0);
Bjorn Bringert7984c942009-12-09 15:38:25 +0000493 int favoriteType = c.getInt(1);
494
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700495 // Allocate and update database with new appWidgetId
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800496 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700497 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800498
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800499 if (LOGD) {
500 Log.d(TAG, "allocated appWidgetId=" + appWidgetId
501 + " for favoriteId=" + favoriteId);
502 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800503 values.clear();
Bjorn Bringert7984c942009-12-09 15:38:25 +0000504 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
505 values.put(Favorites.APPWIDGET_ID, appWidgetId);
506
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800507 // Original widgets might not have valid spans when upgrading
Bjorn Bringert7984c942009-12-09 15:38:25 +0000508 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
509 values.put(LauncherSettings.Favorites.SPANX, 4);
510 values.put(LauncherSettings.Favorites.SPANY, 1);
511 } else {
512 values.put(LauncherSettings.Favorites.SPANX, 2);
513 values.put(LauncherSettings.Favorites.SPANY, 2);
514 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800515
516 String updateWhere = Favorites._ID + "=" + favoriteId;
517 db.update(TABLE_FAVORITES, values, updateWhere, null);
Bjorn Bringert34251342009-12-15 13:33:11 +0000518
519 ComponentName cn = null;
520 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_CLOCK) {
521 appWidgetManager.bindAppWidgetId(appWidgetId,
522 new ComponentName("com.android.alarmclock",
523 "com.android.alarmclock.AnalogAppWidgetProvider"));
524 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME) {
525 appWidgetManager.bindAppWidgetId(appWidgetId,
526 new ComponentName("com.android.camera",
527 "com.android.camera.PhotoAppWidgetProvider"));
528 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
529 appWidgetManager.bindAppWidgetId(appWidgetId,
530 new ComponentName("com.android.quicksearchbox",
531 "com.android.quicksearchbox.SearchWidgetProvider"));
532 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800533 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800534 Log.e(TAG, "Problem allocating appWidgetId", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800535 }
536 }
537
538 db.setTransactionSuccessful();
539 } catch (SQLException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800540 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800541 } finally {
542 db.endTransaction();
543 if (c != null) {
544 c.close();
545 }
546 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800547 }
548
549 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800550 * Loads the default set of favorite packages from an xml file.
551 *
552 * @param db The database to write the values into
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800553 */
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700554 private int loadFavorites(SQLiteDatabase db) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800555 Intent intent = new Intent(Intent.ACTION_MAIN, null);
556 intent.addCategory(Intent.CATEGORY_LAUNCHER);
557 ContentValues values = new ContentValues();
558
559 PackageManager packageManager = mContext.getPackageManager();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560 int i = 0;
561 try {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700562 XmlResourceParser parser = mContext.getResources().getXml(R.xml.default_workspace);
563 AttributeSet attrs = Xml.asAttributeSet(parser);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800564 XmlUtils.beginDocument(parser, TAG_FAVORITES);
565
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700566 final int depth = parser.getDepth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800567
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700568 int type;
569 while (((type = parser.next()) != XmlPullParser.END_TAG ||
570 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
571
572 if (type != XmlPullParser.START_TAG) {
573 continue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800574 }
575
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700576 boolean added = false;
577 final String name = parser.getName();
578
579 TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.Favorite);
580
581 values.clear();
582 values.put(LauncherSettings.Favorites.CONTAINER,
583 LauncherSettings.Favorites.CONTAINER_DESKTOP);
584 values.put(LauncherSettings.Favorites.SCREEN,
585 a.getString(R.styleable.Favorite_screen));
586 values.put(LauncherSettings.Favorites.CELLX,
587 a.getString(R.styleable.Favorite_x));
588 values.put(LauncherSettings.Favorites.CELLY,
589 a.getString(R.styleable.Favorite_y));
590
591 if (TAG_FAVORITE.equals(name)) {
Mike Cleronb87bd162009-10-30 16:36:56 -0700592 added = addAppShortcut(db, values, a, packageManager, intent);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700593 } else if (TAG_SEARCH.equals(name)) {
594 added = addSearchWidget(db, values);
595 } else if (TAG_CLOCK.equals(name)) {
596 added = addClockWidget(db, values);
Mike Cleronb87bd162009-10-30 16:36:56 -0700597 } else if (TAG_APPWIDGET.equals(name)) {
598 added = addAppWidget(db, values, a);
599 } else if (TAG_SHORTCUT.equals(name)) {
600 added = addUriShortcut(db, values, a);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800601 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700602
603 if (added) i++;
604
605 a.recycle();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800606 }
607 } catch (XmlPullParserException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800608 Log.w(TAG, "Got exception parsing favorites.", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800609 } catch (IOException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800610 Log.w(TAG, "Got exception parsing favorites.", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700612
613 return i;
614 }
615
Mike Cleronb87bd162009-10-30 16:36:56 -0700616 private boolean addAppShortcut(SQLiteDatabase db, ContentValues values, TypedArray a,
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700617 PackageManager packageManager, Intent intent) {
618
619 ActivityInfo info;
620 String packageName = a.getString(R.styleable.Favorite_packageName);
621 String className = a.getString(R.styleable.Favorite_className);
622 try {
623 ComponentName cn = new ComponentName(packageName, className);
624 info = packageManager.getActivityInfo(cn, 0);
625 intent.setComponent(cn);
Dianne Hackbornbd2e54d2009-03-24 21:00:33 -0700626 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
627 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Romain Guy1ce1a242009-06-23 17:34:54 -0700628 values.put(Favorites.INTENT, intent.toUri(0));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700629 values.put(Favorites.TITLE, info.loadLabel(packageManager).toString());
630 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPLICATION);
631 values.put(Favorites.SPANX, 1);
632 values.put(Favorites.SPANY, 1);
633 db.insert(TABLE_FAVORITES, null, values);
634 } catch (PackageManager.NameNotFoundException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800635 Log.w(TAG, "Unable to add favorite: " + packageName +
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700636 "/" + className, e);
637 return false;
638 }
639 return true;
640 }
641
642 private boolean addSearchWidget(SQLiteDatabase db, ContentValues values) {
Bjorn Bringert7984c942009-12-09 15:38:25 +0000643 ComponentName cn = new ComponentName("com.android.quicksearchbox",
644 "com.android.quicksearchbox.SearchWidgetProvider");
645 return addAppWidget(db, values, cn, 4, 1);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700646 }
647
648 private boolean addClockWidget(SQLiteDatabase db, ContentValues values) {
Bjorn Bringert34251342009-12-15 13:33:11 +0000649 ComponentName cn = new ComponentName("com.android.alarmclock",
650 "com.android.alarmclock.AnalogAppWidgetProvider");
651 return addAppWidget(db, values, cn, 2, 2);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 }
Mike Cleronb87bd162009-10-30 16:36:56 -0700653
654 private boolean addAppWidget(SQLiteDatabase db, ContentValues values, TypedArray a) {
Mike Cleronb87bd162009-10-30 16:36:56 -0700655 String packageName = a.getString(R.styleable.Favorite_packageName);
656 String className = a.getString(R.styleable.Favorite_className);
657
658 if (packageName == null || className == null) {
659 return false;
660 }
661
662 ComponentName cn = new ComponentName(packageName, className);
Bjorn Bringert7984c942009-12-09 15:38:25 +0000663 int spanX = a.getInt(R.styleable.Favorite_spanX, 0);
664 int spanY = a.getInt(R.styleable.Favorite_spanY, 0);
665 return addAppWidget(db, values, cn, spanX, spanY);
666 }
667
668 private boolean addAppWidget(SQLiteDatabase db, ContentValues values, ComponentName cn,
669 int spanX, int spanY) {
Mike Cleronb87bd162009-10-30 16:36:56 -0700670 boolean allocatedAppWidgets = false;
671 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
672
673 try {
674 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
675
676 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
Bjorn Bringert7984c942009-12-09 15:38:25 +0000677 values.put(Favorites.SPANX, spanX);
678 values.put(Favorites.SPANY, spanY);
Mike Cleronb87bd162009-10-30 16:36:56 -0700679 values.put(Favorites.APPWIDGET_ID, appWidgetId);
680 db.insert(TABLE_FAVORITES, null, values);
681
682 allocatedAppWidgets = true;
683
684 appWidgetManager.bindAppWidgetId(appWidgetId, cn);
685 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800686 Log.e(TAG, "Problem allocating appWidgetId", ex);
Mike Cleronb87bd162009-10-30 16:36:56 -0700687 }
688
689 return allocatedAppWidgets;
690 }
691
692 private boolean addUriShortcut(SQLiteDatabase db, ContentValues values,
693 TypedArray a) {
694 Resources r = mContext.getResources();
695
696 final int iconResId = a.getResourceId(R.styleable.Favorite_icon, 0);
697 final int titleResId = a.getResourceId(R.styleable.Favorite_title, 0);
698
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800699 Intent intent;
Mike Cleronb87bd162009-10-30 16:36:56 -0700700 String uri = null;
701 try {
702 uri = a.getString(R.styleable.Favorite_uri);
703 intent = Intent.parseUri(uri, 0);
704 } catch (URISyntaxException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800705 Log.w(TAG, "Shortcut has malformed uri: " + uri);
Mike Cleronb87bd162009-10-30 16:36:56 -0700706 return false; // Oh well
707 }
708
709 if (iconResId == 0 || titleResId == 0) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800710 Log.w(TAG, "Shortcut is missing title or icon resource ID");
Mike Cleronb87bd162009-10-30 16:36:56 -0700711 return false;
712 }
713
714 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
715 values.put(Favorites.INTENT, intent.toUri(0));
716 values.put(Favorites.TITLE, r.getString(titleResId));
717 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_SHORTCUT);
718 values.put(Favorites.SPANX, 1);
719 values.put(Favorites.SPANY, 1);
720 values.put(Favorites.ICON_TYPE, Favorites.ICON_TYPE_RESOURCE);
721 values.put(Favorites.ICON_PACKAGE, mContext.getPackageName());
722 values.put(Favorites.ICON_RESOURCE, r.getResourceName(iconResId));
723
724 db.insert(TABLE_FAVORITES, null, values);
725
726 return true;
727 }
728 }
729
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730 /**
731 * Build a query string that will match any row where the column matches
732 * anything in the values list.
733 */
734 static String buildOrWhereString(String column, int[] values) {
735 StringBuilder selectWhere = new StringBuilder();
736 for (int i = values.length - 1; i >= 0; i--) {
737 selectWhere.append(column).append("=").append(values[i]);
738 if (i > 0) {
739 selectWhere.append(" OR ");
740 }
741 }
742 return selectWhere.toString();
743 }
744
745 static class SqlArguments {
746 public final String table;
747 public final String where;
748 public final String[] args;
749
750 SqlArguments(Uri url, String where, String[] args) {
751 if (url.getPathSegments().size() == 1) {
752 this.table = url.getPathSegments().get(0);
753 this.where = where;
754 this.args = args;
755 } else if (url.getPathSegments().size() != 2) {
756 throw new IllegalArgumentException("Invalid URI: " + url);
757 } else if (!TextUtils.isEmpty(where)) {
758 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
759 } else {
760 this.table = url.getPathSegments().get(0);
761 this.where = "_id=" + ContentUris.parseId(url);
762 this.args = null;
763 }
764 }
765
766 SqlArguments(Uri url) {
767 if (url.getPathSegments().size() == 1) {
768 table = url.getPathSegments().get(0);
769 where = null;
770 args = null;
771 } else {
772 throw new IllegalArgumentException("Invalid URI: " + url);
773 }
774 }
775 }
776}