blob: 47d89912595b4d508181fe858a42543817fe77bf [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) {
472 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 };
477
478 final ArrayList<ComponentName> bindTargets = new ArrayList<ComponentName>();
479 bindTargets.add(new ComponentName("com.android.alarmclock",
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700480 "com.android.alarmclock.AnalogAppWidgetProvider"));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800481 bindTargets.add(new ComponentName("com.android.camera",
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700482 "com.android.camera.PhotoAppWidgetProvider"));
Bjorn Bringert7984c942009-12-09 15:38:25 +0000483 bindTargets.add(new ComponentName("com.android.quicksearchbox",
484 "com.android.quicksearchbox.SearchWidgetProvider"));
485
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800486 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE, bindSources);
487
488 Cursor c = null;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700489 boolean allocatedAppWidgets = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800490
491 db.beginTransaction();
492 try {
493 // Select and iterate through each matching widget
Bjorn Bringert7984c942009-12-09 15:38:25 +0000494 c = db.query(TABLE_FAVORITES, new String[] { Favorites._ID, Favorites.ITEM_TYPE },
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800495 selectWhere, null, null, null, null);
496
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800497 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800498
499 final ContentValues values = new ContentValues();
500 while (c != null && c.moveToNext()) {
501 long favoriteId = c.getLong(0);
Bjorn Bringert7984c942009-12-09 15:38:25 +0000502 int favoriteType = c.getInt(1);
503
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700504 // Allocate and update database with new appWidgetId
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800505 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700506 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800507
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800508 if (LOGD) {
509 Log.d(TAG, "allocated appWidgetId=" + appWidgetId
510 + " for favoriteId=" + favoriteId);
511 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800512 values.clear();
Bjorn Bringert7984c942009-12-09 15:38:25 +0000513 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
514 values.put(Favorites.APPWIDGET_ID, appWidgetId);
515
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800516 // Original widgets might not have valid spans when upgrading
Bjorn Bringert7984c942009-12-09 15:38:25 +0000517 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
518 values.put(LauncherSettings.Favorites.SPANX, 4);
519 values.put(LauncherSettings.Favorites.SPANY, 1);
520 } else {
521 values.put(LauncherSettings.Favorites.SPANX, 2);
522 values.put(LauncherSettings.Favorites.SPANY, 2);
523 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800524
525 String updateWhere = Favorites._ID + "=" + favoriteId;
526 db.update(TABLE_FAVORITES, values, updateWhere, null);
527
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700528 allocatedAppWidgets = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800529 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800530 Log.e(TAG, "Problem allocating appWidgetId", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800531 }
532 }
533
534 db.setTransactionSuccessful();
535 } catch (SQLException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800536 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800537 } finally {
538 db.endTransaction();
539 if (c != null) {
540 c.close();
541 }
542 }
543
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700544 // If any appWidgetIds allocated, then launch over to binder
545 if (allocatedAppWidgets) {
546 launchAppWidgetBinder(bindSources, bindTargets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800547 }
548 }
549
550 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700551 * Launch the widget binder that walks through the Launcher database,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800552 * binding any matching widgets to the corresponding targets. We can't
553 * bind ourselves because our parent process can't obtain the
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700554 * BIND_APPWIDGET permission.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800555 */
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700556 private void launchAppWidgetBinder(int[] bindSources, ArrayList<ComponentName> bindTargets) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800557 final Intent intent = new Intent();
558 intent.setComponent(new ComponentName("com.android.settings",
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700559 "com.android.settings.LauncherAppWidgetBinder"));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
561
562 final Bundle extras = new Bundle();
563 extras.putIntArray(EXTRA_BIND_SOURCES, bindSources);
564 extras.putParcelableArrayList(EXTRA_BIND_TARGETS, bindTargets);
565 intent.putExtras(extras);
566
567 mContext.startActivity(intent);
568 }
569
570 /**
571 * Loads the default set of favorite packages from an xml file.
572 *
573 * @param db The database to write the values into
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800574 */
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700575 private int loadFavorites(SQLiteDatabase db) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800576 Intent intent = new Intent(Intent.ACTION_MAIN, null);
577 intent.addCategory(Intent.CATEGORY_LAUNCHER);
578 ContentValues values = new ContentValues();
579
580 PackageManager packageManager = mContext.getPackageManager();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800581 int i = 0;
582 try {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700583 XmlResourceParser parser = mContext.getResources().getXml(R.xml.default_workspace);
584 AttributeSet attrs = Xml.asAttributeSet(parser);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800585 XmlUtils.beginDocument(parser, TAG_FAVORITES);
586
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700587 final int depth = parser.getDepth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800588
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700589 int type;
590 while (((type = parser.next()) != XmlPullParser.END_TAG ||
591 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
592
593 if (type != XmlPullParser.START_TAG) {
594 continue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800595 }
596
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700597 boolean added = false;
598 final String name = parser.getName();
599
600 TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.Favorite);
601
602 values.clear();
603 values.put(LauncherSettings.Favorites.CONTAINER,
604 LauncherSettings.Favorites.CONTAINER_DESKTOP);
605 values.put(LauncherSettings.Favorites.SCREEN,
606 a.getString(R.styleable.Favorite_screen));
607 values.put(LauncherSettings.Favorites.CELLX,
608 a.getString(R.styleable.Favorite_x));
609 values.put(LauncherSettings.Favorites.CELLY,
610 a.getString(R.styleable.Favorite_y));
611
612 if (TAG_FAVORITE.equals(name)) {
Mike Cleronb87bd162009-10-30 16:36:56 -0700613 added = addAppShortcut(db, values, a, packageManager, intent);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700614 } else if (TAG_SEARCH.equals(name)) {
615 added = addSearchWidget(db, values);
616 } else if (TAG_CLOCK.equals(name)) {
617 added = addClockWidget(db, values);
Mike Cleronb87bd162009-10-30 16:36:56 -0700618 } else if (TAG_APPWIDGET.equals(name)) {
619 added = addAppWidget(db, values, a);
620 } else if (TAG_SHORTCUT.equals(name)) {
621 added = addUriShortcut(db, values, a);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700623
624 if (added) i++;
625
626 a.recycle();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800627 }
628 } catch (XmlPullParserException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800629 Log.w(TAG, "Got exception parsing favorites.", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800630 } catch (IOException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800631 Log.w(TAG, "Got exception parsing favorites.", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700633
634 return i;
635 }
636
Mike Cleronb87bd162009-10-30 16:36:56 -0700637 private boolean addAppShortcut(SQLiteDatabase db, ContentValues values, TypedArray a,
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700638 PackageManager packageManager, Intent intent) {
639
640 ActivityInfo info;
641 String packageName = a.getString(R.styleable.Favorite_packageName);
642 String className = a.getString(R.styleable.Favorite_className);
643 try {
644 ComponentName cn = new ComponentName(packageName, className);
645 info = packageManager.getActivityInfo(cn, 0);
646 intent.setComponent(cn);
Dianne Hackbornbd2e54d2009-03-24 21:00:33 -0700647 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
648 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Romain Guy1ce1a242009-06-23 17:34:54 -0700649 values.put(Favorites.INTENT, intent.toUri(0));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700650 values.put(Favorites.TITLE, info.loadLabel(packageManager).toString());
651 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPLICATION);
652 values.put(Favorites.SPANX, 1);
653 values.put(Favorites.SPANY, 1);
654 db.insert(TABLE_FAVORITES, null, values);
655 } catch (PackageManager.NameNotFoundException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800656 Log.w(TAG, "Unable to add favorite: " + packageName +
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700657 "/" + className, e);
658 return false;
659 }
660 return true;
661 }
662
663 private boolean addSearchWidget(SQLiteDatabase db, ContentValues values) {
Bjorn Bringert7984c942009-12-09 15:38:25 +0000664 ComponentName cn = new ComponentName("com.android.quicksearchbox",
665 "com.android.quicksearchbox.SearchWidgetProvider");
666 return addAppWidget(db, values, cn, 4, 1);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700667 }
668
669 private boolean addClockWidget(SQLiteDatabase db, ContentValues values) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 final int[] bindSources = new int[] {
671 Favorites.ITEM_TYPE_WIDGET_CLOCK,
672 };
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700673
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800674 final ArrayList<ComponentName> bindTargets = new ArrayList<ComponentName>();
675 bindTargets.add(new ComponentName("com.android.alarmclock",
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700676 "com.android.alarmclock.AnalogAppWidgetProvider"));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700677
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700678 boolean allocatedAppWidgets = false;
Mike Cleronb87bd162009-10-30 16:36:56 -0700679
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700680 // Try binding to an analog clock widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700682 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700683
684 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_WIDGET_CLOCK);
685 values.put(Favorites.SPANX, 2);
686 values.put(Favorites.SPANY, 2);
687 values.put(Favorites.APPWIDGET_ID, appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800688 db.insert(TABLE_FAVORITES, null, values);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700689
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700690 allocatedAppWidgets = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800691 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800692 Log.e(TAG, "Problem allocating appWidgetId", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 }
694
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700695 // If any appWidgetIds allocated, then launch over to binder
696 if (allocatedAppWidgets) {
697 launchAppWidgetBinder(bindSources, bindTargets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700699
700 return allocatedAppWidgets;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701 }
Mike Cleronb87bd162009-10-30 16:36:56 -0700702
703 private boolean addAppWidget(SQLiteDatabase db, ContentValues values, TypedArray a) {
Mike Cleronb87bd162009-10-30 16:36:56 -0700704 String packageName = a.getString(R.styleable.Favorite_packageName);
705 String className = a.getString(R.styleable.Favorite_className);
706
707 if (packageName == null || className == null) {
708 return false;
709 }
710
711 ComponentName cn = new ComponentName(packageName, className);
Bjorn Bringert7984c942009-12-09 15:38:25 +0000712 int spanX = a.getInt(R.styleable.Favorite_spanX, 0);
713 int spanY = a.getInt(R.styleable.Favorite_spanY, 0);
714 return addAppWidget(db, values, cn, spanX, spanY);
715 }
716
717 private boolean addAppWidget(SQLiteDatabase db, ContentValues values, ComponentName cn,
718 int spanX, int spanY) {
Mike Cleronb87bd162009-10-30 16:36:56 -0700719 boolean allocatedAppWidgets = false;
720 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
721
722 try {
723 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
724
725 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
Bjorn Bringert7984c942009-12-09 15:38:25 +0000726 values.put(Favorites.SPANX, spanX);
727 values.put(Favorites.SPANY, spanY);
Mike Cleronb87bd162009-10-30 16:36:56 -0700728 values.put(Favorites.APPWIDGET_ID, appWidgetId);
729 db.insert(TABLE_FAVORITES, null, values);
730
731 allocatedAppWidgets = true;
732
733 appWidgetManager.bindAppWidgetId(appWidgetId, cn);
734 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800735 Log.e(TAG, "Problem allocating appWidgetId", ex);
Mike Cleronb87bd162009-10-30 16:36:56 -0700736 }
737
738 return allocatedAppWidgets;
739 }
740
741 private boolean addUriShortcut(SQLiteDatabase db, ContentValues values,
742 TypedArray a) {
743 Resources r = mContext.getResources();
744
745 final int iconResId = a.getResourceId(R.styleable.Favorite_icon, 0);
746 final int titleResId = a.getResourceId(R.styleable.Favorite_title, 0);
747
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800748 Intent intent;
Mike Cleronb87bd162009-10-30 16:36:56 -0700749 String uri = null;
750 try {
751 uri = a.getString(R.styleable.Favorite_uri);
752 intent = Intent.parseUri(uri, 0);
753 } catch (URISyntaxException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800754 Log.w(TAG, "Shortcut has malformed uri: " + uri);
Mike Cleronb87bd162009-10-30 16:36:56 -0700755 return false; // Oh well
756 }
757
758 if (iconResId == 0 || titleResId == 0) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800759 Log.w(TAG, "Shortcut is missing title or icon resource ID");
Mike Cleronb87bd162009-10-30 16:36:56 -0700760 return false;
761 }
762
763 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
764 values.put(Favorites.INTENT, intent.toUri(0));
765 values.put(Favorites.TITLE, r.getString(titleResId));
766 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_SHORTCUT);
767 values.put(Favorites.SPANX, 1);
768 values.put(Favorites.SPANY, 1);
769 values.put(Favorites.ICON_TYPE, Favorites.ICON_TYPE_RESOURCE);
770 values.put(Favorites.ICON_PACKAGE, mContext.getPackageName());
771 values.put(Favorites.ICON_RESOURCE, r.getResourceName(iconResId));
772
773 db.insert(TABLE_FAVORITES, null, values);
774
775 return true;
776 }
777 }
778
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800779 /**
780 * Build a query string that will match any row where the column matches
781 * anything in the values list.
782 */
783 static String buildOrWhereString(String column, int[] values) {
784 StringBuilder selectWhere = new StringBuilder();
785 for (int i = values.length - 1; i >= 0; i--) {
786 selectWhere.append(column).append("=").append(values[i]);
787 if (i > 0) {
788 selectWhere.append(" OR ");
789 }
790 }
791 return selectWhere.toString();
792 }
793
794 static class SqlArguments {
795 public final String table;
796 public final String where;
797 public final String[] args;
798
799 SqlArguments(Uri url, String where, String[] args) {
800 if (url.getPathSegments().size() == 1) {
801 this.table = url.getPathSegments().get(0);
802 this.where = where;
803 this.args = args;
804 } else if (url.getPathSegments().size() != 2) {
805 throw new IllegalArgumentException("Invalid URI: " + url);
806 } else if (!TextUtils.isEmpty(where)) {
807 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
808 } else {
809 this.table = url.getPathSegments().get(0);
810 this.where = "_id=" + ContentUris.parseId(url);
811 this.args = null;
812 }
813 }
814
815 SqlArguments(Uri url) {
816 if (url.getPathSegments().size() == 1) {
817 table = url.getPathSegments().get(0);
818 where = null;
819 args = null;
820 } else {
821 throw new IllegalArgumentException("Invalid URI: " + url);
822 }
823 }
824 }
825}