blob: c3ceefdd32599e0344c5c83caf8b2743d39df1b2 [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
Romain Guy7eb9e5e2009-12-02 20:10:07 -080061 private static final int DATABASE_VERSION = 6;
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 }
385
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386 if (version != DATABASE_VERSION) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800387 Log.w(TAG, "Destroying all old data.");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES);
389 onCreate(db);
390 }
391 }
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800392
393 private boolean updateContactsShortcuts(SQLiteDatabase db) {
394 Cursor c = null;
395 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE,
396 new int[] { Favorites.ITEM_TYPE_SHORTCUT });
397
398 db.beginTransaction();
399 try {
400 // Select and iterate through each matching widget
401 c = db.query(TABLE_FAVORITES, new String[] { Favorites._ID, Favorites.INTENT },
402 selectWhere, null, null, null, null);
403
404 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
405
406 final ContentValues values = new ContentValues();
407 final int idIndex = c.getColumnIndex(Favorites._ID);
408 final int intentIndex = c.getColumnIndex(Favorites.INTENT);
409
410 while (c != null && c.moveToNext()) {
411 long favoriteId = c.getLong(idIndex);
412 final String intentUri = c.getString(intentIndex);
413 if (intentUri != null) {
414 try {
415 Intent intent = Intent.parseUri(intentUri, 0);
416 android.util.Log.d("Home", intent.toString());
417 final Uri uri = intent.getData();
418 final String data = uri.toString();
419 if (Intent.ACTION_VIEW.equals(intent.getAction()) &&
420 (data.startsWith("content://contacts/people/") ||
421 data.startsWith("content://com.android.contacts/contacts/lookup/"))) {
422
423 intent = new Intent("com.android.contacts.action.QUICK_CONTACT");
424 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
425 Intent.FLAG_ACTIVITY_CLEAR_TOP |
426 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
427
428 intent.setData(uri);
429 intent.putExtra("mode", 3);
430 intent.putExtra("exclude_mimes", (String[]) null);
431
432 values.clear();
433 values.put(LauncherSettings.Favorites.INTENT, intent.toUri(0));
434
435 String updateWhere = Favorites._ID + "=" + favoriteId;
436 db.update(TABLE_FAVORITES, values, updateWhere, null);
437 }
438 } catch (RuntimeException ex) {
439 Log.e(TAG, "Problem upgrading shortcut", ex);
440 } catch (URISyntaxException e) {
441 Log.e(TAG, "Problem upgrading shortcut", e);
442 }
443 }
444 }
445
446 db.setTransactionSuccessful();
447 } catch (SQLException ex) {
448 Log.w(TAG, "Problem while upgrading contacts", ex);
449 return false;
450 } finally {
451 db.endTransaction();
452 if (c != null) {
453 c.close();
454 }
455 }
456
457 return true;
458 }
459
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700461 * Upgrade existing clock and photo frame widgets into their new widget
462 * equivalents. This method allocates appWidgetIds, and then hands off to
463 * LauncherAppWidgetBinder to finish the actual binding.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800464 */
465 private void convertWidgets(SQLiteDatabase db) {
466 final int[] bindSources = new int[] {
467 Favorites.ITEM_TYPE_WIDGET_CLOCK,
468 Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME,
469 };
470
471 final ArrayList<ComponentName> bindTargets = new ArrayList<ComponentName>();
472 bindTargets.add(new ComponentName("com.android.alarmclock",
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700473 "com.android.alarmclock.AnalogAppWidgetProvider"));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800474 bindTargets.add(new ComponentName("com.android.camera",
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700475 "com.android.camera.PhotoAppWidgetProvider"));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800476
477 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE, bindSources);
478
479 Cursor c = null;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700480 boolean allocatedAppWidgets = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800481
482 db.beginTransaction();
483 try {
484 // Select and iterate through each matching widget
485 c = db.query(TABLE_FAVORITES, new String[] { Favorites._ID },
486 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);
493
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700494 // Allocate and update database with new appWidgetId
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800495 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700496 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800497
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800498 if (LOGD) {
499 Log.d(TAG, "allocated appWidgetId=" + appWidgetId
500 + " for favoriteId=" + favoriteId);
501 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800502
503 values.clear();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700504 values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800505
506 // Original widgets might not have valid spans when upgrading
507 values.put(LauncherSettings.Favorites.SPANX, 2);
508 values.put(LauncherSettings.Favorites.SPANY, 2);
509
510 String updateWhere = Favorites._ID + "=" + favoriteId;
511 db.update(TABLE_FAVORITES, values, updateWhere, null);
512
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700513 allocatedAppWidgets = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800514 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800515 Log.e(TAG, "Problem allocating appWidgetId", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800516 }
517 }
518
519 db.setTransactionSuccessful();
520 } catch (SQLException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800521 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800522 } finally {
523 db.endTransaction();
524 if (c != null) {
525 c.close();
526 }
527 }
528
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700529 // If any appWidgetIds allocated, then launch over to binder
530 if (allocatedAppWidgets) {
531 launchAppWidgetBinder(bindSources, bindTargets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800532 }
533 }
534
535 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700536 * Launch the widget binder that walks through the Launcher database,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800537 * binding any matching widgets to the corresponding targets. We can't
538 * bind ourselves because our parent process can't obtain the
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700539 * BIND_APPWIDGET permission.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800540 */
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700541 private void launchAppWidgetBinder(int[] bindSources, ArrayList<ComponentName> bindTargets) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800542 final Intent intent = new Intent();
543 intent.setComponent(new ComponentName("com.android.settings",
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700544 "com.android.settings.LauncherAppWidgetBinder"));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800545 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
546
547 final Bundle extras = new Bundle();
548 extras.putIntArray(EXTRA_BIND_SOURCES, bindSources);
549 extras.putParcelableArrayList(EXTRA_BIND_TARGETS, bindTargets);
550 intent.putExtras(extras);
551
552 mContext.startActivity(intent);
553 }
554
555 /**
556 * Loads the default set of favorite packages from an xml file.
557 *
558 * @param db The database to write the values into
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800559 */
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700560 private int loadFavorites(SQLiteDatabase db) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561 Intent intent = new Intent(Intent.ACTION_MAIN, null);
562 intent.addCategory(Intent.CATEGORY_LAUNCHER);
563 ContentValues values = new ContentValues();
564
565 PackageManager packageManager = mContext.getPackageManager();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800566 int i = 0;
567 try {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700568 XmlResourceParser parser = mContext.getResources().getXml(R.xml.default_workspace);
569 AttributeSet attrs = Xml.asAttributeSet(parser);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570 XmlUtils.beginDocument(parser, TAG_FAVORITES);
571
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700572 final int depth = parser.getDepth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800573
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700574 int type;
575 while (((type = parser.next()) != XmlPullParser.END_TAG ||
576 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
577
578 if (type != XmlPullParser.START_TAG) {
579 continue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800580 }
581
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700582 boolean added = false;
583 final String name = parser.getName();
584
585 TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.Favorite);
586
587 values.clear();
588 values.put(LauncherSettings.Favorites.CONTAINER,
589 LauncherSettings.Favorites.CONTAINER_DESKTOP);
590 values.put(LauncherSettings.Favorites.SCREEN,
591 a.getString(R.styleable.Favorite_screen));
592 values.put(LauncherSettings.Favorites.CELLX,
593 a.getString(R.styleable.Favorite_x));
594 values.put(LauncherSettings.Favorites.CELLY,
595 a.getString(R.styleable.Favorite_y));
596
597 if (TAG_FAVORITE.equals(name)) {
Mike Cleronb87bd162009-10-30 16:36:56 -0700598 added = addAppShortcut(db, values, a, packageManager, intent);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700599 } else if (TAG_SEARCH.equals(name)) {
600 added = addSearchWidget(db, values);
601 } else if (TAG_CLOCK.equals(name)) {
602 added = addClockWidget(db, values);
Mike Cleronb87bd162009-10-30 16:36:56 -0700603 } else if (TAG_APPWIDGET.equals(name)) {
604 added = addAppWidget(db, values, a);
605 } else if (TAG_SHORTCUT.equals(name)) {
606 added = addUriShortcut(db, values, a);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800607 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700608
609 if (added) i++;
610
611 a.recycle();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800612 }
613 } catch (XmlPullParserException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800614 Log.w(TAG, "Got exception parsing favorites.", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800615 } catch (IOException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800616 Log.w(TAG, "Got exception parsing favorites.", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800617 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700618
619 return i;
620 }
621
Mike Cleronb87bd162009-10-30 16:36:56 -0700622 private boolean addAppShortcut(SQLiteDatabase db, ContentValues values, TypedArray a,
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700623 PackageManager packageManager, Intent intent) {
624
625 ActivityInfo info;
626 String packageName = a.getString(R.styleable.Favorite_packageName);
627 String className = a.getString(R.styleable.Favorite_className);
628 try {
629 ComponentName cn = new ComponentName(packageName, className);
630 info = packageManager.getActivityInfo(cn, 0);
631 intent.setComponent(cn);
Dianne Hackbornbd2e54d2009-03-24 21:00:33 -0700632 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
633 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Romain Guy1ce1a242009-06-23 17:34:54 -0700634 values.put(Favorites.INTENT, intent.toUri(0));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700635 values.put(Favorites.TITLE, info.loadLabel(packageManager).toString());
636 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPLICATION);
637 values.put(Favorites.SPANX, 1);
638 values.put(Favorites.SPANY, 1);
639 db.insert(TABLE_FAVORITES, null, values);
640 } catch (PackageManager.NameNotFoundException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800641 Log.w(TAG, "Unable to add favorite: " + packageName +
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700642 "/" + className, e);
643 return false;
644 }
645 return true;
646 }
647
648 private boolean addSearchWidget(SQLiteDatabase db, ContentValues values) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 // Add a search box
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700650 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_WIDGET_SEARCH);
651 values.put(Favorites.SPANX, 4);
652 values.put(Favorites.SPANY, 1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 db.insert(TABLE_FAVORITES, null, values);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700654
655 return true;
656 }
657
658 private boolean addClockWidget(SQLiteDatabase db, ContentValues values) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 final int[] bindSources = new int[] {
660 Favorites.ITEM_TYPE_WIDGET_CLOCK,
661 };
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700662
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 final ArrayList<ComponentName> bindTargets = new ArrayList<ComponentName>();
664 bindTargets.add(new ComponentName("com.android.alarmclock",
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700665 "com.android.alarmclock.AnalogAppWidgetProvider"));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700666
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700667 boolean allocatedAppWidgets = false;
Mike Cleronb87bd162009-10-30 16:36:56 -0700668
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700669 // Try binding to an analog clock widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700671 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700672
673 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_WIDGET_CLOCK);
674 values.put(Favorites.SPANX, 2);
675 values.put(Favorites.SPANY, 2);
676 values.put(Favorites.APPWIDGET_ID, appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 db.insert(TABLE_FAVORITES, null, values);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700678
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700679 allocatedAppWidgets = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800681 Log.e(TAG, "Problem allocating appWidgetId", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682 }
683
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700684 // If any appWidgetIds allocated, then launch over to binder
685 if (allocatedAppWidgets) {
686 launchAppWidgetBinder(bindSources, bindTargets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700688
689 return allocatedAppWidgets;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 }
Mike Cleronb87bd162009-10-30 16:36:56 -0700691
692 private boolean addAppWidget(SQLiteDatabase db, ContentValues values, TypedArray a) {
Mike Cleronb87bd162009-10-30 16:36:56 -0700693 String packageName = a.getString(R.styleable.Favorite_packageName);
694 String className = a.getString(R.styleable.Favorite_className);
695
696 if (packageName == null || className == null) {
697 return false;
698 }
699
700 ComponentName cn = new ComponentName(packageName, className);
701
Mike Cleronb87bd162009-10-30 16:36:56 -0700702 boolean allocatedAppWidgets = false;
703 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
704
705 try {
706 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
707
708 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
709 values.put(Favorites.SPANX, a.getString(R.styleable.Favorite_spanX));
710 values.put(Favorites.SPANY, a.getString(R.styleable.Favorite_spanY));
711 values.put(Favorites.APPWIDGET_ID, appWidgetId);
712 db.insert(TABLE_FAVORITES, null, values);
713
714 allocatedAppWidgets = true;
715
716 appWidgetManager.bindAppWidgetId(appWidgetId, cn);
717 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800718 Log.e(TAG, "Problem allocating appWidgetId", ex);
Mike Cleronb87bd162009-10-30 16:36:56 -0700719 }
720
721 return allocatedAppWidgets;
722 }
723
724 private boolean addUriShortcut(SQLiteDatabase db, ContentValues values,
725 TypedArray a) {
726 Resources r = mContext.getResources();
727
728 final int iconResId = a.getResourceId(R.styleable.Favorite_icon, 0);
729 final int titleResId = a.getResourceId(R.styleable.Favorite_title, 0);
730
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800731 Intent intent;
Mike Cleronb87bd162009-10-30 16:36:56 -0700732 String uri = null;
733 try {
734 uri = a.getString(R.styleable.Favorite_uri);
735 intent = Intent.parseUri(uri, 0);
736 } catch (URISyntaxException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800737 Log.w(TAG, "Shortcut has malformed uri: " + uri);
Mike Cleronb87bd162009-10-30 16:36:56 -0700738 return false; // Oh well
739 }
740
741 if (iconResId == 0 || titleResId == 0) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800742 Log.w(TAG, "Shortcut is missing title or icon resource ID");
Mike Cleronb87bd162009-10-30 16:36:56 -0700743 return false;
744 }
745
746 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
747 values.put(Favorites.INTENT, intent.toUri(0));
748 values.put(Favorites.TITLE, r.getString(titleResId));
749 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_SHORTCUT);
750 values.put(Favorites.SPANX, 1);
751 values.put(Favorites.SPANY, 1);
752 values.put(Favorites.ICON_TYPE, Favorites.ICON_TYPE_RESOURCE);
753 values.put(Favorites.ICON_PACKAGE, mContext.getPackageName());
754 values.put(Favorites.ICON_RESOURCE, r.getResourceName(iconResId));
755
756 db.insert(TABLE_FAVORITES, null, values);
757
758 return true;
759 }
760 }
761
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762 /**
763 * Build a query string that will match any row where the column matches
764 * anything in the values list.
765 */
766 static String buildOrWhereString(String column, int[] values) {
767 StringBuilder selectWhere = new StringBuilder();
768 for (int i = values.length - 1; i >= 0; i--) {
769 selectWhere.append(column).append("=").append(values[i]);
770 if (i > 0) {
771 selectWhere.append(" OR ");
772 }
773 }
774 return selectWhere.toString();
775 }
776
777 static class SqlArguments {
778 public final String table;
779 public final String where;
780 public final String[] args;
781
782 SqlArguments(Uri url, String where, String[] args) {
783 if (url.getPathSegments().size() == 1) {
784 this.table = url.getPathSegments().get(0);
785 this.where = where;
786 this.args = args;
787 } else if (url.getPathSegments().size() != 2) {
788 throw new IllegalArgumentException("Invalid URI: " + url);
789 } else if (!TextUtils.isEmpty(where)) {
790 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
791 } else {
792 this.table = url.getPathSegments().get(0);
793 this.where = "_id=" + ContentUris.parseId(url);
794 this.args = null;
795 }
796 }
797
798 SqlArguments(Uri url) {
799 if (url.getPathSegments().size() == 1) {
800 table = url.getPathSegments().get(0);
801 where = null;
802 args = null;
803 } else {
804 throw new IllegalArgumentException("Invalid URI: " + url);
805 }
806 }
807 }
808}