blob: f03e50c99f50195c6a39c0de5f8e7928068857d9 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Bjorn Bringertcd8fec02010-01-14 13:26:43 +000019import android.app.SearchManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070020import android.appwidget.AppWidgetHost;
Mike Cleronb87bd162009-10-30 16:36:56 -070021import android.appwidget.AppWidgetManager;
Bjorn Bringertcd8fec02010-01-14 13:26:43 +000022import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.content.ContentProvider;
24import android.content.Context;
25import android.content.ContentValues;
26import android.content.Intent;
27import android.content.ComponentName;
28import android.content.ContentUris;
29import android.content.ContentResolver;
Mike Cleronb87bd162009-10-30 16:36:56 -070030import android.content.res.Resources;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070031import android.content.res.XmlResourceParser;
32import android.content.res.TypedArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.pm.PackageManager;
34import android.content.pm.ActivityInfo;
35import android.database.sqlite.SQLiteOpenHelper;
36import android.database.sqlite.SQLiteDatabase;
37import android.database.sqlite.SQLiteQueryBuilder;
38import android.database.Cursor;
39import android.database.SQLException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.util.Log;
41import android.util.Xml;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070042import android.util.AttributeSet;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.net.Uri;
44import android.text.TextUtils;
45import android.os.*;
46import android.provider.Settings;
47
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import java.io.IOException;
Mike Cleronb87bd162009-10-30 16:36:56 -070049import java.net.URISyntaxException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import java.util.ArrayList;
Bjorn Bringertcd8fec02010-01-14 13:26:43 +000051import java.util.List;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import org.xmlpull.v1.XmlPullParserException;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070054import org.xmlpull.v1.XmlPullParser;
Tom Taylorfd1e7572009-12-21 14:00:50 -080055import com.android.common.XmlUtils;
Joe Onoratoa5902522009-07-30 13:37:37 -070056import com.android.launcher2.LauncherSettings.Favorites;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057
58public class LauncherProvider extends ContentProvider {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080059 private static final String TAG = "Launcher.LauncherProvider";
60 private static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061
62 private static final String DATABASE_NAME = "launcher.db";
63
Bjorn Bringert7984c942009-12-09 15:38:25 +000064 private static final int DATABASE_VERSION = 7;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065
Joe Onoratoa5902522009-07-30 13:37:37 -070066 static final String AUTHORITY = "com.android.launcher2.settings";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067
Joe Onoratoa5902522009-07-30 13:37:37 -070068 static final String EXTRA_BIND_SOURCES = "com.android.launcher2.settings.bindsources";
69 static final String EXTRA_BIND_TARGETS = "com.android.launcher2.settings.bindtargets";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070
71 static final String TABLE_FAVORITES = "favorites";
72 static final String PARAMETER_NOTIFY = "notify";
73
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -070074 /**
Romain Guy73b979d2009-06-09 12:57:21 -070075 * {@link Uri} triggered at any registered {@link android.database.ContentObserver} when
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -070076 * {@link AppWidgetHost#deleteHost()} is called during database creation.
77 * Use this to recall {@link AppWidgetHost#startListening()} if needed.
78 */
79 static final Uri CONTENT_APPWIDGET_RESET_URI =
80 Uri.parse("content://" + AUTHORITY + "/appWidgetReset");
81
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082 private SQLiteOpenHelper mOpenHelper;
83
84 @Override
85 public boolean onCreate() {
86 mOpenHelper = new DatabaseHelper(getContext());
87 return true;
88 }
89
90 @Override
91 public String getType(Uri uri) {
92 SqlArguments args = new SqlArguments(uri, null, null);
93 if (TextUtils.isEmpty(args.where)) {
94 return "vnd.android.cursor.dir/" + args.table;
95 } else {
96 return "vnd.android.cursor.item/" + args.table;
97 }
98 }
99
100 @Override
101 public Cursor query(Uri uri, String[] projection, String selection,
102 String[] selectionArgs, String sortOrder) {
103
104 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
105 SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
106 qb.setTables(args.table);
107
Romain Guy73b979d2009-06-09 12:57:21 -0700108 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109 Cursor result = qb.query(db, projection, args.where, args.args, null, null, sortOrder);
110 result.setNotificationUri(getContext().getContentResolver(), uri);
111
112 return result;
113 }
114
115 @Override
116 public Uri insert(Uri uri, ContentValues initialValues) {
117 SqlArguments args = new SqlArguments(uri);
118
119 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
120 final long rowId = db.insert(args.table, null, initialValues);
121 if (rowId <= 0) return null;
122
123 uri = ContentUris.withAppendedId(uri, rowId);
124 sendNotify(uri);
125
126 return uri;
127 }
128
129 @Override
130 public int bulkInsert(Uri uri, ContentValues[] values) {
131 SqlArguments args = new SqlArguments(uri);
132
133 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
134 db.beginTransaction();
135 try {
136 int numValues = values.length;
137 for (int i = 0; i < numValues; i++) {
138 if (db.insert(args.table, null, values[i]) < 0) return 0;
139 }
140 db.setTransactionSuccessful();
141 } finally {
142 db.endTransaction();
143 }
144
145 sendNotify(uri);
146 return values.length;
147 }
148
149 @Override
150 public int delete(Uri uri, String selection, String[] selectionArgs) {
151 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
152
153 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
154 int count = db.delete(args.table, args.where, args.args);
155 if (count > 0) sendNotify(uri);
156
157 return count;
158 }
159
160 @Override
161 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
162 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
163
164 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
165 int count = db.update(args.table, values, args.where, args.args);
166 if (count > 0) sendNotify(uri);
167
168 return count;
169 }
170
171 private void sendNotify(Uri uri) {
172 String notify = uri.getQueryParameter(PARAMETER_NOTIFY);
173 if (notify == null || "true".equals(notify)) {
174 getContext().getContentResolver().notifyChange(uri, null);
175 }
176 }
177
178 private static class DatabaseHelper extends SQLiteOpenHelper {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 private static final String TAG_FAVORITES = "favorites";
180 private static final String TAG_FAVORITE = "favorite";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700181 private static final String TAG_CLOCK = "clock";
182 private static final String TAG_SEARCH = "search";
Mike Cleronb87bd162009-10-30 16:36:56 -0700183 private static final String TAG_APPWIDGET = "appwidget";
184 private static final String TAG_SHORTCUT = "shortcut";
185
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 private final Context mContext;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700187 private final AppWidgetHost mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188
189 DatabaseHelper(Context context) {
190 super(context, DATABASE_NAME, null, DATABASE_VERSION);
191 mContext = context;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700192 mAppWidgetHost = new AppWidgetHost(context, Launcher.APPWIDGET_HOST_ID);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 }
194
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700195 /**
196 * Send notification that we've deleted the {@link AppWidgetHost},
197 * probably as part of the initial database creation. The receiver may
198 * want to re-call {@link AppWidgetHost#startListening()} to ensure
199 * callbacks are correctly set.
200 */
201 private void sendAppWidgetResetNotify() {
202 final ContentResolver resolver = mContext.getContentResolver();
203 resolver.notifyChange(CONTENT_APPWIDGET_RESET_URI, null);
204 }
205
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 @Override
207 public void onCreate(SQLiteDatabase db) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800208 if (LOGD) Log.d(TAG, "creating new launcher database");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209
210 db.execSQL("CREATE TABLE favorites (" +
211 "_id INTEGER PRIMARY KEY," +
212 "title TEXT," +
213 "intent TEXT," +
214 "container INTEGER," +
215 "screen INTEGER," +
216 "cellX INTEGER," +
217 "cellY INTEGER," +
218 "spanX INTEGER," +
219 "spanY INTEGER," +
220 "itemType INTEGER," +
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700221 "appWidgetId INTEGER NOT NULL DEFAULT -1," +
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222 "isShortcut INTEGER," +
223 "iconType INTEGER," +
224 "iconPackage TEXT," +
225 "iconResource TEXT," +
226 "icon BLOB," +
227 "uri TEXT," +
228 "displayMode INTEGER" +
229 ");");
230
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700231 // Database was just created, so wipe any previous widgets
232 if (mAppWidgetHost != null) {
233 mAppWidgetHost.deleteHost();
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700234 sendAppWidgetResetNotify();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235 }
236
237 if (!convertDatabase(db)) {
238 // Populate favorites table with initial favorites
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700239 loadFavorites(db);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800240 }
241 }
242
243 private boolean convertDatabase(SQLiteDatabase db) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800244 if (LOGD) Log.d(TAG, "converting database from an older format, but not onUpgrade");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245 boolean converted = false;
246
247 final Uri uri = Uri.parse("content://" + Settings.AUTHORITY +
248 "/old_favorites?notify=true");
249 final ContentResolver resolver = mContext.getContentResolver();
250 Cursor cursor = null;
251
252 try {
253 cursor = resolver.query(uri, null, null, null, null);
254 } catch (Exception e) {
255 // Ignore
256 }
257
258 // We already have a favorites database in the old provider
259 if (cursor != null && cursor.getCount() > 0) {
260 try {
261 converted = copyFromCursor(db, cursor) > 0;
262 } finally {
263 cursor.close();
264 }
265
266 if (converted) {
267 resolver.delete(uri, null, null);
268 }
269 }
270
271 if (converted) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700272 // Convert widgets from this import into widgets
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800273 if (LOGD) Log.d(TAG, "converted and now triggering widget upgrade");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274 convertWidgets(db);
275 }
276
277 return converted;
278 }
279
280 private int copyFromCursor(SQLiteDatabase db, Cursor c) {
Romain Guy73b979d2009-06-09 12:57:21 -0700281 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282 final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
283 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
284 final int iconTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
285 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
286 final int iconPackageIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
287 final int iconResourceIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
288 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
289 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
290 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
291 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
292 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
293 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
294 final int displayModeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
295
296 ContentValues[] rows = new ContentValues[c.getCount()];
297 int i = 0;
298 while (c.moveToNext()) {
299 ContentValues values = new ContentValues(c.getColumnCount());
Romain Guy73b979d2009-06-09 12:57:21 -0700300 values.put(LauncherSettings.Favorites._ID, c.getLong(idIndex));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800301 values.put(LauncherSettings.Favorites.INTENT, c.getString(intentIndex));
302 values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex));
303 values.put(LauncherSettings.Favorites.ICON_TYPE, c.getInt(iconTypeIndex));
304 values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex));
305 values.put(LauncherSettings.Favorites.ICON_PACKAGE, c.getString(iconPackageIndex));
306 values.put(LauncherSettings.Favorites.ICON_RESOURCE, c.getString(iconResourceIndex));
307 values.put(LauncherSettings.Favorites.CONTAINER, c.getInt(containerIndex));
308 values.put(LauncherSettings.Favorites.ITEM_TYPE, c.getInt(itemTypeIndex));
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700309 values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800310 values.put(LauncherSettings.Favorites.SCREEN, c.getInt(screenIndex));
311 values.put(LauncherSettings.Favorites.CELLX, c.getInt(cellXIndex));
312 values.put(LauncherSettings.Favorites.CELLY, c.getInt(cellYIndex));
313 values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex));
314 values.put(LauncherSettings.Favorites.DISPLAY_MODE, c.getInt(displayModeIndex));
315 rows[i++] = values;
316 }
317
318 db.beginTransaction();
319 int total = 0;
320 try {
321 int numValues = rows.length;
322 for (i = 0; i < numValues; i++) {
323 if (db.insert(TABLE_FAVORITES, null, rows[i]) < 0) {
324 return 0;
325 } else {
326 total++;
327 }
328 }
329 db.setTransactionSuccessful();
330 } finally {
331 db.endTransaction();
332 }
333
334 return total;
335 }
336
337 @Override
338 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800339 if (LOGD) Log.d(TAG, "onUpgrade triggered");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800340
341 int version = oldVersion;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700342 if (version < 3) {
343 // upgrade 1,2 -> 3 added appWidgetId column
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800344 db.beginTransaction();
345 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700346 // Insert new column for holding appWidgetIds
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800347 db.execSQL("ALTER TABLE favorites " +
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700348 "ADD COLUMN appWidgetId INTEGER NOT NULL DEFAULT -1;");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800349 db.setTransactionSuccessful();
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700350 version = 3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 } catch (SQLException ex) {
352 // Old version remains, which means we wipe old data
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800353 Log.e(TAG, ex.getMessage(), ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 } finally {
355 db.endTransaction();
356 }
357
358 // Convert existing widgets only if table upgrade was successful
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700359 if (version == 3) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 convertWidgets(db);
361 }
362 }
Romain Guy73b979d2009-06-09 12:57:21 -0700363
364 if (version < 4) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800365 version = 4;
Romain Guy73b979d2009-06-09 12:57:21 -0700366 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800367
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800368 if (version < 5) {
369 // We went from 3 to 5 screens. Move everything 1 to the right
370 db.beginTransaction();
371 try {
372 db.execSQL("UPDATE favorites SET screen=(screen + 1);");
373 db.setTransactionSuccessful();
374 version = 5;
375 } catch (SQLException ex) {
376 // Old version remains, which means we wipe old data
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800377 Log.e(TAG, ex.getMessage(), ex);
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800378 } finally {
379 db.endTransaction();
380 }
381 }
382
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800383 if (version < 6) {
384 if (updateContactsShortcuts(db)) {
385 version = 6;
386 }
387 }
Bjorn Bringert7984c942009-12-09 15:38:25 +0000388
389 if (version < 7) {
390 // Version 7 gets rid of the special search widget.
391 convertWidgets(db);
392 version = 7;
393 }
394
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 if (version != DATABASE_VERSION) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800396 Log.w(TAG, "Destroying all old data.");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397 db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES);
398 onCreate(db);
399 }
400 }
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800401
402 private boolean updateContactsShortcuts(SQLiteDatabase db) {
403 Cursor c = null;
404 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE,
405 new int[] { Favorites.ITEM_TYPE_SHORTCUT });
406
407 db.beginTransaction();
408 try {
409 // Select and iterate through each matching widget
410 c = db.query(TABLE_FAVORITES, new String[] { Favorites._ID, Favorites.INTENT },
411 selectWhere, null, null, null, null);
412
413 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
414
415 final ContentValues values = new ContentValues();
416 final int idIndex = c.getColumnIndex(Favorites._ID);
417 final int intentIndex = c.getColumnIndex(Favorites.INTENT);
418
419 while (c != null && c.moveToNext()) {
420 long favoriteId = c.getLong(idIndex);
421 final String intentUri = c.getString(intentIndex);
422 if (intentUri != null) {
423 try {
424 Intent intent = Intent.parseUri(intentUri, 0);
425 android.util.Log.d("Home", intent.toString());
426 final Uri uri = intent.getData();
427 final String data = uri.toString();
428 if (Intent.ACTION_VIEW.equals(intent.getAction()) &&
429 (data.startsWith("content://contacts/people/") ||
430 data.startsWith("content://com.android.contacts/contacts/lookup/"))) {
431
432 intent = new Intent("com.android.contacts.action.QUICK_CONTACT");
433 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
434 Intent.FLAG_ACTIVITY_CLEAR_TOP |
435 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
436
437 intent.setData(uri);
438 intent.putExtra("mode", 3);
439 intent.putExtra("exclude_mimes", (String[]) null);
440
441 values.clear();
442 values.put(LauncherSettings.Favorites.INTENT, intent.toUri(0));
443
444 String updateWhere = Favorites._ID + "=" + favoriteId;
445 db.update(TABLE_FAVORITES, values, updateWhere, null);
446 }
447 } catch (RuntimeException ex) {
448 Log.e(TAG, "Problem upgrading shortcut", ex);
449 } catch (URISyntaxException e) {
450 Log.e(TAG, "Problem upgrading shortcut", e);
451 }
452 }
453 }
454
455 db.setTransactionSuccessful();
456 } catch (SQLException ex) {
457 Log.w(TAG, "Problem while upgrading contacts", ex);
458 return false;
459 } finally {
460 db.endTransaction();
461 if (c != null) {
462 c.close();
463 }
464 }
465
466 return true;
467 }
468
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700470 * Upgrade existing clock and photo frame widgets into their new widget
Bjorn Bringert93c45762009-12-16 13:19:47 +0000471 * equivalents.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800472 */
473 private void convertWidgets(SQLiteDatabase db) {
Bjorn Bringert34251342009-12-15 13:33:11 +0000474 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800475 final int[] bindSources = new int[] {
476 Favorites.ITEM_TYPE_WIDGET_CLOCK,
477 Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME,
Bjorn Bringert7984c942009-12-09 15:38:25 +0000478 Favorites.ITEM_TYPE_WIDGET_SEARCH,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 };
Bjorn Bringert7984c942009-12-09 15:38:25 +0000480
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800481 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE, bindSources);
482
483 Cursor c = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800484
485 db.beginTransaction();
486 try {
487 // Select and iterate through each matching widget
Bjorn Bringert7984c942009-12-09 15:38:25 +0000488 c = db.query(TABLE_FAVORITES, new String[] { Favorites._ID, Favorites.ITEM_TYPE },
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800489 selectWhere, null, null, null, null);
490
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800491 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800492
493 final ContentValues values = new ContentValues();
494 while (c != null && c.moveToNext()) {
495 long favoriteId = c.getLong(0);
Bjorn Bringert7984c942009-12-09 15:38:25 +0000496 int favoriteType = c.getInt(1);
497
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700498 // Allocate and update database with new appWidgetId
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800499 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700500 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800501
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800502 if (LOGD) {
503 Log.d(TAG, "allocated appWidgetId=" + appWidgetId
504 + " for favoriteId=" + favoriteId);
505 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800506 values.clear();
Bjorn Bringert7984c942009-12-09 15:38:25 +0000507 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
508 values.put(Favorites.APPWIDGET_ID, appWidgetId);
509
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800510 // Original widgets might not have valid spans when upgrading
Bjorn Bringert7984c942009-12-09 15:38:25 +0000511 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
512 values.put(LauncherSettings.Favorites.SPANX, 4);
513 values.put(LauncherSettings.Favorites.SPANY, 1);
514 } else {
515 values.put(LauncherSettings.Favorites.SPANX, 2);
516 values.put(LauncherSettings.Favorites.SPANY, 2);
517 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800518
519 String updateWhere = Favorites._ID + "=" + favoriteId;
520 db.update(TABLE_FAVORITES, values, updateWhere, null);
Bjorn Bringert34251342009-12-15 13:33:11 +0000521
522 ComponentName cn = null;
523 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_CLOCK) {
524 appWidgetManager.bindAppWidgetId(appWidgetId,
525 new ComponentName("com.android.alarmclock",
526 "com.android.alarmclock.AnalogAppWidgetProvider"));
527 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME) {
528 appWidgetManager.bindAppWidgetId(appWidgetId,
529 new ComponentName("com.android.camera",
530 "com.android.camera.PhotoAppWidgetProvider"));
531 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
532 appWidgetManager.bindAppWidgetId(appWidgetId,
Bjorn Bringertcd8fec02010-01-14 13:26:43 +0000533 getSearchWidgetProvider());
Bjorn Bringert34251342009-12-15 13:33:11 +0000534 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800535 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800536 Log.e(TAG, "Problem allocating appWidgetId", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800537 }
538 }
539
540 db.setTransactionSuccessful();
541 } catch (SQLException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800542 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800543 } finally {
544 db.endTransaction();
545 if (c != null) {
546 c.close();
547 }
548 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800549 }
550
551 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800552 * Loads the default set of favorite packages from an xml file.
553 *
554 * @param db The database to write the values into
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800555 */
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700556 private int loadFavorites(SQLiteDatabase db) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800557 Intent intent = new Intent(Intent.ACTION_MAIN, null);
558 intent.addCategory(Intent.CATEGORY_LAUNCHER);
559 ContentValues values = new ContentValues();
560
561 PackageManager packageManager = mContext.getPackageManager();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800562 int i = 0;
563 try {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700564 XmlResourceParser parser = mContext.getResources().getXml(R.xml.default_workspace);
565 AttributeSet attrs = Xml.asAttributeSet(parser);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800566 XmlUtils.beginDocument(parser, TAG_FAVORITES);
567
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700568 final int depth = parser.getDepth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800569
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700570 int type;
571 while (((type = parser.next()) != XmlPullParser.END_TAG ||
572 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
573
574 if (type != XmlPullParser.START_TAG) {
575 continue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800576 }
577
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700578 boolean added = false;
579 final String name = parser.getName();
580
581 TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.Favorite);
582
583 values.clear();
584 values.put(LauncherSettings.Favorites.CONTAINER,
585 LauncherSettings.Favorites.CONTAINER_DESKTOP);
586 values.put(LauncherSettings.Favorites.SCREEN,
587 a.getString(R.styleable.Favorite_screen));
588 values.put(LauncherSettings.Favorites.CELLX,
589 a.getString(R.styleable.Favorite_x));
590 values.put(LauncherSettings.Favorites.CELLY,
591 a.getString(R.styleable.Favorite_y));
592
593 if (TAG_FAVORITE.equals(name)) {
Mike Cleronb87bd162009-10-30 16:36:56 -0700594 added = addAppShortcut(db, values, a, packageManager, intent);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700595 } else if (TAG_SEARCH.equals(name)) {
596 added = addSearchWidget(db, values);
597 } else if (TAG_CLOCK.equals(name)) {
598 added = addClockWidget(db, values);
Mike Cleronb87bd162009-10-30 16:36:56 -0700599 } else if (TAG_APPWIDGET.equals(name)) {
600 added = addAppWidget(db, values, a);
601 } else if (TAG_SHORTCUT.equals(name)) {
602 added = addUriShortcut(db, values, a);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800603 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700604
605 if (added) i++;
606
607 a.recycle();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800608 }
609 } catch (XmlPullParserException 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 } catch (IOException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800612 Log.w(TAG, "Got exception parsing favorites.", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800613 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700614
615 return i;
616 }
617
Mike Cleronb87bd162009-10-30 16:36:56 -0700618 private boolean addAppShortcut(SQLiteDatabase db, ContentValues values, TypedArray a,
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700619 PackageManager packageManager, Intent intent) {
620
621 ActivityInfo info;
622 String packageName = a.getString(R.styleable.Favorite_packageName);
623 String className = a.getString(R.styleable.Favorite_className);
624 try {
625 ComponentName cn = new ComponentName(packageName, className);
626 info = packageManager.getActivityInfo(cn, 0);
627 intent.setComponent(cn);
Dianne Hackbornbd2e54d2009-03-24 21:00:33 -0700628 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
629 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Romain Guy1ce1a242009-06-23 17:34:54 -0700630 values.put(Favorites.INTENT, intent.toUri(0));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700631 values.put(Favorites.TITLE, info.loadLabel(packageManager).toString());
632 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPLICATION);
633 values.put(Favorites.SPANX, 1);
634 values.put(Favorites.SPANY, 1);
635 db.insert(TABLE_FAVORITES, null, values);
636 } catch (PackageManager.NameNotFoundException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800637 Log.w(TAG, "Unable to add favorite: " + packageName +
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700638 "/" + className, e);
639 return false;
640 }
641 return true;
642 }
643
Bjorn Bringertcd8fec02010-01-14 13:26:43 +0000644 private ComponentName getSearchWidgetProvider() {
645 SearchManager searchManager =
646 (SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE);
647 ComponentName searchComponent = searchManager.getGlobalSearchActivity();
648 if (searchComponent == null) return null;
649 return getProviderInPackage(searchComponent.getPackageName());
650 }
651
652 /**
653 * Gets an appwidget provider from the given package. If the package contains more than
654 * one appwidget provider, an arbitrary one is returned.
655 */
656 private ComponentName getProviderInPackage(String packageName) {
657 AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
658 List<AppWidgetProviderInfo> providers = appWidgetManager.getInstalledProviders();
659 if (providers == null) return null;
660 final int providerCount = providers.size();
661 for (int i = 0; i < providerCount; i++) {
662 ComponentName provider = providers.get(i).provider;
663 if (provider != null && provider.getPackageName().equals(packageName)) {
664 return provider;
665 }
666 }
667 return null;
668 }
669
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700670 private boolean addSearchWidget(SQLiteDatabase db, ContentValues values) {
Bjorn Bringertcd8fec02010-01-14 13:26:43 +0000671 ComponentName cn = getSearchWidgetProvider();
Bjorn Bringert7984c942009-12-09 15:38:25 +0000672 return addAppWidget(db, values, cn, 4, 1);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700673 }
674
675 private boolean addClockWidget(SQLiteDatabase db, ContentValues values) {
Bjorn Bringert34251342009-12-15 13:33:11 +0000676 ComponentName cn = new ComponentName("com.android.alarmclock",
677 "com.android.alarmclock.AnalogAppWidgetProvider");
678 return addAppWidget(db, values, cn, 2, 2);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 }
Mike Cleronb87bd162009-10-30 16:36:56 -0700680
681 private boolean addAppWidget(SQLiteDatabase db, ContentValues values, TypedArray a) {
Mike Cleronb87bd162009-10-30 16:36:56 -0700682 String packageName = a.getString(R.styleable.Favorite_packageName);
683 String className = a.getString(R.styleable.Favorite_className);
684
685 if (packageName == null || className == null) {
686 return false;
687 }
688
689 ComponentName cn = new ComponentName(packageName, className);
Bjorn Bringert7984c942009-12-09 15:38:25 +0000690 int spanX = a.getInt(R.styleable.Favorite_spanX, 0);
691 int spanY = a.getInt(R.styleable.Favorite_spanY, 0);
692 return addAppWidget(db, values, cn, spanX, spanY);
693 }
694
695 private boolean addAppWidget(SQLiteDatabase db, ContentValues values, ComponentName cn,
696 int spanX, int spanY) {
Mike Cleronb87bd162009-10-30 16:36:56 -0700697 boolean allocatedAppWidgets = false;
698 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
699
700 try {
701 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
702
703 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
Bjorn Bringert7984c942009-12-09 15:38:25 +0000704 values.put(Favorites.SPANX, spanX);
705 values.put(Favorites.SPANY, spanY);
Mike Cleronb87bd162009-10-30 16:36:56 -0700706 values.put(Favorites.APPWIDGET_ID, appWidgetId);
707 db.insert(TABLE_FAVORITES, null, values);
708
709 allocatedAppWidgets = true;
710
711 appWidgetManager.bindAppWidgetId(appWidgetId, cn);
712 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800713 Log.e(TAG, "Problem allocating appWidgetId", ex);
Mike Cleronb87bd162009-10-30 16:36:56 -0700714 }
715
716 return allocatedAppWidgets;
717 }
718
719 private boolean addUriShortcut(SQLiteDatabase db, ContentValues values,
720 TypedArray a) {
721 Resources r = mContext.getResources();
722
723 final int iconResId = a.getResourceId(R.styleable.Favorite_icon, 0);
724 final int titleResId = a.getResourceId(R.styleable.Favorite_title, 0);
725
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800726 Intent intent;
Mike Cleronb87bd162009-10-30 16:36:56 -0700727 String uri = null;
728 try {
729 uri = a.getString(R.styleable.Favorite_uri);
730 intent = Intent.parseUri(uri, 0);
731 } catch (URISyntaxException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800732 Log.w(TAG, "Shortcut has malformed uri: " + uri);
Mike Cleronb87bd162009-10-30 16:36:56 -0700733 return false; // Oh well
734 }
735
736 if (iconResId == 0 || titleResId == 0) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800737 Log.w(TAG, "Shortcut is missing title or icon resource ID");
Mike Cleronb87bd162009-10-30 16:36:56 -0700738 return false;
739 }
740
741 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
742 values.put(Favorites.INTENT, intent.toUri(0));
743 values.put(Favorites.TITLE, r.getString(titleResId));
744 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_SHORTCUT);
745 values.put(Favorites.SPANX, 1);
746 values.put(Favorites.SPANY, 1);
747 values.put(Favorites.ICON_TYPE, Favorites.ICON_TYPE_RESOURCE);
748 values.put(Favorites.ICON_PACKAGE, mContext.getPackageName());
749 values.put(Favorites.ICON_RESOURCE, r.getResourceName(iconResId));
750
751 db.insert(TABLE_FAVORITES, null, values);
752
753 return true;
754 }
755 }
756
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757 /**
758 * Build a query string that will match any row where the column matches
759 * anything in the values list.
760 */
761 static String buildOrWhereString(String column, int[] values) {
762 StringBuilder selectWhere = new StringBuilder();
763 for (int i = values.length - 1; i >= 0; i--) {
764 selectWhere.append(column).append("=").append(values[i]);
765 if (i > 0) {
766 selectWhere.append(" OR ");
767 }
768 }
769 return selectWhere.toString();
770 }
771
772 static class SqlArguments {
773 public final String table;
774 public final String where;
775 public final String[] args;
776
777 SqlArguments(Uri url, String where, String[] args) {
778 if (url.getPathSegments().size() == 1) {
779 this.table = url.getPathSegments().get(0);
780 this.where = where;
781 this.args = args;
782 } else if (url.getPathSegments().size() != 2) {
783 throw new IllegalArgumentException("Invalid URI: " + url);
784 } else if (!TextUtils.isEmpty(where)) {
785 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
786 } else {
787 this.table = url.getPathSegments().get(0);
788 this.where = "_id=" + ContentUris.parseId(url);
789 this.args = null;
790 }
791 }
792
793 SqlArguments(Uri url) {
794 if (url.getPathSegments().size() == 1) {
795 table = url.getPathSegments().get(0);
796 where = null;
797 args = null;
798 } else {
799 throw new IllegalArgumentException("Invalid URI: " + url);
800 }
801 }
802 }
803}