blob: 942b4beac475d3fff4d4dda1fd77181af96a470b [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 {
56 private static final String LOG_TAG = "LauncherProvider";
57 private static final boolean LOGD = true;
58
59 private static final String DATABASE_NAME = "launcher.db";
60
Mike Cleron3a2b3f22009-11-05 17:17:42 -080061 private static final int DATABASE_VERSION = 5;
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";
Romain Guy73b979d2009-06-09 12:57:21 -070069 static final String TABLE_GESTURES = "gestures";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070 static final String PARAMETER_NOTIFY = "notify";
71
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -070072 /**
Romain Guy73b979d2009-06-09 12:57:21 -070073 * {@link Uri} triggered at any registered {@link android.database.ContentObserver} when
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -070074 * {@link AppWidgetHost#deleteHost()} is called during database creation.
75 * Use this to recall {@link AppWidgetHost#startListening()} if needed.
76 */
77 static final Uri CONTENT_APPWIDGET_RESET_URI =
78 Uri.parse("content://" + AUTHORITY + "/appWidgetReset");
79
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080 private SQLiteOpenHelper mOpenHelper;
81
82 @Override
83 public boolean onCreate() {
84 mOpenHelper = new DatabaseHelper(getContext());
85 return true;
86 }
87
88 @Override
89 public String getType(Uri uri) {
90 SqlArguments args = new SqlArguments(uri, null, null);
91 if (TextUtils.isEmpty(args.where)) {
92 return "vnd.android.cursor.dir/" + args.table;
93 } else {
94 return "vnd.android.cursor.item/" + args.table;
95 }
96 }
97
98 @Override
99 public Cursor query(Uri uri, String[] projection, String selection,
100 String[] selectionArgs, String sortOrder) {
101
102 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
103 SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
104 qb.setTables(args.table);
105
Romain Guy73b979d2009-06-09 12:57:21 -0700106 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107 Cursor result = qb.query(db, projection, args.where, args.args, null, null, sortOrder);
108 result.setNotificationUri(getContext().getContentResolver(), uri);
109
110 return result;
111 }
112
113 @Override
114 public Uri insert(Uri uri, ContentValues initialValues) {
115 SqlArguments args = new SqlArguments(uri);
116
117 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
118 final long rowId = db.insert(args.table, null, initialValues);
119 if (rowId <= 0) return null;
120
121 uri = ContentUris.withAppendedId(uri, rowId);
122 sendNotify(uri);
123
124 return uri;
125 }
126
127 @Override
128 public int bulkInsert(Uri uri, ContentValues[] values) {
129 SqlArguments args = new SqlArguments(uri);
130
131 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
132 db.beginTransaction();
133 try {
134 int numValues = values.length;
135 for (int i = 0; i < numValues; i++) {
136 if (db.insert(args.table, null, values[i]) < 0) return 0;
137 }
138 db.setTransactionSuccessful();
139 } finally {
140 db.endTransaction();
141 }
142
143 sendNotify(uri);
144 return values.length;
145 }
146
147 @Override
148 public int delete(Uri uri, String selection, String[] selectionArgs) {
149 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
150
151 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
152 int count = db.delete(args.table, args.where, args.args);
153 if (count > 0) sendNotify(uri);
154
155 return count;
156 }
157
158 @Override
159 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
160 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
161
162 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
163 int count = db.update(args.table, values, args.where, args.args);
164 if (count > 0) sendNotify(uri);
165
166 return count;
167 }
168
169 private void sendNotify(Uri uri) {
170 String notify = uri.getQueryParameter(PARAMETER_NOTIFY);
171 if (notify == null || "true".equals(notify)) {
172 getContext().getContentResolver().notifyChange(uri, null);
173 }
174 }
175
176 private static class DatabaseHelper extends SQLiteOpenHelper {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 private static final String TAG_FAVORITES = "favorites";
178 private static final String TAG_FAVORITE = "favorite";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700179 private static final String TAG_CLOCK = "clock";
180 private static final String TAG_SEARCH = "search";
Mike Cleronb87bd162009-10-30 16:36:56 -0700181 private static final String TAG_APPWIDGET = "appwidget";
182 private static final String TAG_SHORTCUT = "shortcut";
183
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 private final Context mContext;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700185 private final AppWidgetHost mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186
187 DatabaseHelper(Context context) {
188 super(context, DATABASE_NAME, null, DATABASE_VERSION);
189 mContext = context;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700190 mAppWidgetHost = new AppWidgetHost(context, Launcher.APPWIDGET_HOST_ID);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 }
192
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700193 /**
194 * Send notification that we've deleted the {@link AppWidgetHost},
195 * probably as part of the initial database creation. The receiver may
196 * want to re-call {@link AppWidgetHost#startListening()} to ensure
197 * callbacks are correctly set.
198 */
199 private void sendAppWidgetResetNotify() {
200 final ContentResolver resolver = mContext.getContentResolver();
201 resolver.notifyChange(CONTENT_APPWIDGET_RESET_URI, null);
202 }
203
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204 @Override
205 public void onCreate(SQLiteDatabase db) {
206 if (LOGD) Log.d(LOG_TAG, "creating new launcher database");
207
208 db.execSQL("CREATE TABLE favorites (" +
209 "_id INTEGER PRIMARY KEY," +
210 "title TEXT," +
211 "intent TEXT," +
212 "container INTEGER," +
213 "screen INTEGER," +
214 "cellX INTEGER," +
215 "cellY INTEGER," +
216 "spanX INTEGER," +
217 "spanY INTEGER," +
218 "itemType INTEGER," +
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700219 "appWidgetId INTEGER NOT NULL DEFAULT -1," +
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800220 "isShortcut INTEGER," +
221 "iconType INTEGER," +
222 "iconPackage TEXT," +
223 "iconResource TEXT," +
224 "icon BLOB," +
225 "uri TEXT," +
226 "displayMode INTEGER" +
227 ");");
228
Romain Guy73b979d2009-06-09 12:57:21 -0700229 db.execSQL("CREATE TABLE gestures (" +
230 "_id INTEGER PRIMARY KEY," +
231 "title TEXT," +
232 "intent TEXT," +
233 "itemType INTEGER," +
234 "iconType INTEGER," +
235 "iconPackage TEXT," +
236 "iconResource TEXT," +
237 "icon BLOB" +
238 ");");
239
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700240 // Database was just created, so wipe any previous widgets
241 if (mAppWidgetHost != null) {
242 mAppWidgetHost.deleteHost();
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700243 sendAppWidgetResetNotify();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 }
245
246 if (!convertDatabase(db)) {
247 // Populate favorites table with initial favorites
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700248 loadFavorites(db);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 }
250 }
251
252 private boolean convertDatabase(SQLiteDatabase db) {
253 if (LOGD) Log.d(LOG_TAG, "converting database from an older format, but not onUpgrade");
254 boolean converted = false;
255
256 final Uri uri = Uri.parse("content://" + Settings.AUTHORITY +
257 "/old_favorites?notify=true");
258 final ContentResolver resolver = mContext.getContentResolver();
259 Cursor cursor = null;
260
261 try {
262 cursor = resolver.query(uri, null, null, null, null);
263 } catch (Exception e) {
264 // Ignore
265 }
266
267 // We already have a favorites database in the old provider
268 if (cursor != null && cursor.getCount() > 0) {
269 try {
270 converted = copyFromCursor(db, cursor) > 0;
271 } finally {
272 cursor.close();
273 }
274
275 if (converted) {
276 resolver.delete(uri, null, null);
277 }
278 }
279
280 if (converted) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700281 // Convert widgets from this import into widgets
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282 if (LOGD) Log.d(LOG_TAG, "converted and now triggering widget upgrade");
283 convertWidgets(db);
284 }
285
286 return converted;
287 }
288
289 private int copyFromCursor(SQLiteDatabase db, Cursor c) {
Romain Guy73b979d2009-06-09 12:57:21 -0700290 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800291 final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
292 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
293 final int iconTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
294 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
295 final int iconPackageIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
296 final int iconResourceIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
297 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
298 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
299 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
300 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
301 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
302 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
303 final int displayModeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
304
305 ContentValues[] rows = new ContentValues[c.getCount()];
306 int i = 0;
307 while (c.moveToNext()) {
308 ContentValues values = new ContentValues(c.getColumnCount());
Romain Guy73b979d2009-06-09 12:57:21 -0700309 values.put(LauncherSettings.Favorites._ID, c.getLong(idIndex));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800310 values.put(LauncherSettings.Favorites.INTENT, c.getString(intentIndex));
311 values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex));
312 values.put(LauncherSettings.Favorites.ICON_TYPE, c.getInt(iconTypeIndex));
313 values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex));
314 values.put(LauncherSettings.Favorites.ICON_PACKAGE, c.getString(iconPackageIndex));
315 values.put(LauncherSettings.Favorites.ICON_RESOURCE, c.getString(iconResourceIndex));
316 values.put(LauncherSettings.Favorites.CONTAINER, c.getInt(containerIndex));
317 values.put(LauncherSettings.Favorites.ITEM_TYPE, c.getInt(itemTypeIndex));
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700318 values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800319 values.put(LauncherSettings.Favorites.SCREEN, c.getInt(screenIndex));
320 values.put(LauncherSettings.Favorites.CELLX, c.getInt(cellXIndex));
321 values.put(LauncherSettings.Favorites.CELLY, c.getInt(cellYIndex));
322 values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex));
323 values.put(LauncherSettings.Favorites.DISPLAY_MODE, c.getInt(displayModeIndex));
324 rows[i++] = values;
325 }
326
327 db.beginTransaction();
328 int total = 0;
329 try {
330 int numValues = rows.length;
331 for (i = 0; i < numValues; i++) {
332 if (db.insert(TABLE_FAVORITES, null, rows[i]) < 0) {
333 return 0;
334 } else {
335 total++;
336 }
337 }
338 db.setTransactionSuccessful();
339 } finally {
340 db.endTransaction();
341 }
342
343 return total;
344 }
345
346 @Override
347 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
348 if (LOGD) Log.d(LOG_TAG, "onUpgrade triggered");
349
350 int version = oldVersion;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700351 if (version < 3) {
352 // upgrade 1,2 -> 3 added appWidgetId column
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 db.beginTransaction();
354 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700355 // Insert new column for holding appWidgetIds
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 db.execSQL("ALTER TABLE favorites " +
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700357 "ADD COLUMN appWidgetId INTEGER NOT NULL DEFAULT -1;");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 db.setTransactionSuccessful();
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700359 version = 3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 } catch (SQLException ex) {
361 // Old version remains, which means we wipe old data
362 Log.e(LOG_TAG, ex.getMessage(), ex);
363 } finally {
364 db.endTransaction();
365 }
366
367 // Convert existing widgets only if table upgrade was successful
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700368 if (version == 3) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 convertWidgets(db);
370 }
371 }
Romain Guy73b979d2009-06-09 12:57:21 -0700372
373 if (version < 4) {
374 db.beginTransaction();
375 try {
376 db.execSQL("CREATE TABLE gestures (" +
377 "_id INTEGER PRIMARY KEY," +
378 "title TEXT," +
379 "intent TEXT," +
380 "itemType INTEGER," +
381 "iconType INTEGER," +
382 "iconPackage TEXT," +
383 "iconResource TEXT," +
384 "icon BLOB" +
385 ");");
386 db.setTransactionSuccessful();
387 version = 4;
388 } catch (SQLException ex) {
389 // Old version remains, which means we wipe old data
390 Log.e(LOG_TAG, ex.getMessage(), ex);
391 } finally {
392 db.endTransaction();
393 }
394 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800396 if (version < 5) {
397 // We went from 3 to 5 screens. Move everything 1 to the right
398 db.beginTransaction();
399 try {
400 db.execSQL("UPDATE favorites SET screen=(screen + 1);");
401 db.setTransactionSuccessful();
402 version = 5;
403 } catch (SQLException ex) {
404 // Old version remains, which means we wipe old data
405 Log.e(LOG_TAG, ex.getMessage(), ex);
406 } finally {
407 db.endTransaction();
408 }
409 }
410
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800411 if (version != DATABASE_VERSION) {
412 Log.w(LOG_TAG, "Destroying all old data.");
413 db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES);
Romain Guy417d2342009-06-19 14:28:51 -0700414 db.execSQL("DROP TABLE IF EXISTS " + TABLE_GESTURES);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800415 onCreate(db);
416 }
417 }
418
419 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700420 * Upgrade existing clock and photo frame widgets into their new widget
421 * equivalents. This method allocates appWidgetIds, and then hands off to
422 * LauncherAppWidgetBinder to finish the actual binding.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 */
424 private void convertWidgets(SQLiteDatabase db) {
425 final int[] bindSources = new int[] {
426 Favorites.ITEM_TYPE_WIDGET_CLOCK,
427 Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME,
428 };
429
430 final ArrayList<ComponentName> bindTargets = new ArrayList<ComponentName>();
431 bindTargets.add(new ComponentName("com.android.alarmclock",
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700432 "com.android.alarmclock.AnalogAppWidgetProvider"));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800433 bindTargets.add(new ComponentName("com.android.camera",
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700434 "com.android.camera.PhotoAppWidgetProvider"));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800435
436 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE, bindSources);
437
438 Cursor c = null;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700439 boolean allocatedAppWidgets = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440
441 db.beginTransaction();
442 try {
443 // Select and iterate through each matching widget
444 c = db.query(TABLE_FAVORITES, new String[] { Favorites._ID },
445 selectWhere, null, null, null, null);
446
447 if (LOGD) Log.d(LOG_TAG, "found upgrade cursor count="+c.getCount());
448
449 final ContentValues values = new ContentValues();
450 while (c != null && c.moveToNext()) {
451 long favoriteId = c.getLong(0);
452
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700453 // Allocate and update database with new appWidgetId
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700455 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700457 if (LOGD) Log.d(LOG_TAG, "allocated appWidgetId="+appWidgetId+" for favoriteId="+favoriteId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800458
459 values.clear();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700460 values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461
462 // Original widgets might not have valid spans when upgrading
463 values.put(LauncherSettings.Favorites.SPANX, 2);
464 values.put(LauncherSettings.Favorites.SPANY, 2);
465
466 String updateWhere = Favorites._ID + "=" + favoriteId;
467 db.update(TABLE_FAVORITES, values, updateWhere, null);
468
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700469 allocatedAppWidgets = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800470 } catch (RuntimeException ex) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700471 Log.e(LOG_TAG, "Problem allocating appWidgetId", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800472 }
473 }
474
475 db.setTransactionSuccessful();
476 } catch (SQLException ex) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700477 Log.w(LOG_TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800478 } finally {
479 db.endTransaction();
480 if (c != null) {
481 c.close();
482 }
483 }
484
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700485 // If any appWidgetIds allocated, then launch over to binder
486 if (allocatedAppWidgets) {
487 launchAppWidgetBinder(bindSources, bindTargets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800488 }
489 }
490
491 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700492 * Launch the widget binder that walks through the Launcher database,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800493 * binding any matching widgets to the corresponding targets. We can't
494 * bind ourselves because our parent process can't obtain the
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700495 * BIND_APPWIDGET permission.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800496 */
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700497 private void launchAppWidgetBinder(int[] bindSources, ArrayList<ComponentName> bindTargets) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800498 final Intent intent = new Intent();
499 intent.setComponent(new ComponentName("com.android.settings",
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700500 "com.android.settings.LauncherAppWidgetBinder"));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800501 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
502
503 final Bundle extras = new Bundle();
504 extras.putIntArray(EXTRA_BIND_SOURCES, bindSources);
505 extras.putParcelableArrayList(EXTRA_BIND_TARGETS, bindTargets);
506 intent.putExtras(extras);
507
508 mContext.startActivity(intent);
509 }
510
511 /**
512 * Loads the default set of favorite packages from an xml file.
513 *
514 * @param db The database to write the values into
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800515 */
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700516 private int loadFavorites(SQLiteDatabase db) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800517 Intent intent = new Intent(Intent.ACTION_MAIN, null);
518 intent.addCategory(Intent.CATEGORY_LAUNCHER);
519 ContentValues values = new ContentValues();
520
521 PackageManager packageManager = mContext.getPackageManager();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800522 int i = 0;
523 try {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700524 XmlResourceParser parser = mContext.getResources().getXml(R.xml.default_workspace);
525 AttributeSet attrs = Xml.asAttributeSet(parser);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800526 XmlUtils.beginDocument(parser, TAG_FAVORITES);
527
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700528 final int depth = parser.getDepth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800529
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700530 int type;
531 while (((type = parser.next()) != XmlPullParser.END_TAG ||
532 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
533
534 if (type != XmlPullParser.START_TAG) {
535 continue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800536 }
537
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700538 boolean added = false;
539 final String name = parser.getName();
540
541 TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.Favorite);
542
543 values.clear();
544 values.put(LauncherSettings.Favorites.CONTAINER,
545 LauncherSettings.Favorites.CONTAINER_DESKTOP);
546 values.put(LauncherSettings.Favorites.SCREEN,
547 a.getString(R.styleable.Favorite_screen));
548 values.put(LauncherSettings.Favorites.CELLX,
549 a.getString(R.styleable.Favorite_x));
550 values.put(LauncherSettings.Favorites.CELLY,
551 a.getString(R.styleable.Favorite_y));
552
553 if (TAG_FAVORITE.equals(name)) {
Mike Cleronb87bd162009-10-30 16:36:56 -0700554 added = addAppShortcut(db, values, a, packageManager, intent);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700555 } else if (TAG_SEARCH.equals(name)) {
556 added = addSearchWidget(db, values);
557 } else if (TAG_CLOCK.equals(name)) {
558 added = addClockWidget(db, values);
Mike Cleronb87bd162009-10-30 16:36:56 -0700559 } else if (TAG_APPWIDGET.equals(name)) {
560 added = addAppWidget(db, values, a);
561 } else if (TAG_SHORTCUT.equals(name)) {
562 added = addUriShortcut(db, values, a);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800563 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700564
565 if (added) i++;
566
567 a.recycle();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800568 }
569 } catch (XmlPullParserException e) {
570 Log.w(LOG_TAG, "Got exception parsing favorites.", e);
571 } catch (IOException e) {
572 Log.w(LOG_TAG, "Got exception parsing favorites.", e);
573 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700574
575 return i;
576 }
577
Mike Cleronb87bd162009-10-30 16:36:56 -0700578 private boolean addAppShortcut(SQLiteDatabase db, ContentValues values, TypedArray a,
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700579 PackageManager packageManager, Intent intent) {
580
581 ActivityInfo info;
582 String packageName = a.getString(R.styleable.Favorite_packageName);
583 String className = a.getString(R.styleable.Favorite_className);
584 try {
585 ComponentName cn = new ComponentName(packageName, className);
586 info = packageManager.getActivityInfo(cn, 0);
587 intent.setComponent(cn);
Dianne Hackbornbd2e54d2009-03-24 21:00:33 -0700588 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
589 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Romain Guy1ce1a242009-06-23 17:34:54 -0700590 values.put(Favorites.INTENT, intent.toUri(0));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700591 values.put(Favorites.TITLE, info.loadLabel(packageManager).toString());
592 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPLICATION);
593 values.put(Favorites.SPANX, 1);
594 values.put(Favorites.SPANY, 1);
595 db.insert(TABLE_FAVORITES, null, values);
596 } catch (PackageManager.NameNotFoundException e) {
597 Log.w(LOG_TAG, "Unable to add favorite: " + packageName +
598 "/" + className, e);
599 return false;
600 }
601 return true;
602 }
603
604 private boolean addSearchWidget(SQLiteDatabase db, ContentValues values) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605 // Add a search box
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700606 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_WIDGET_SEARCH);
607 values.put(Favorites.SPANX, 4);
608 values.put(Favorites.SPANY, 1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800609 db.insert(TABLE_FAVORITES, null, values);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700610
611 return true;
612 }
613
614 private boolean addClockWidget(SQLiteDatabase db, ContentValues values) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800615 final int[] bindSources = new int[] {
616 Favorites.ITEM_TYPE_WIDGET_CLOCK,
617 };
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700618
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800619 final ArrayList<ComponentName> bindTargets = new ArrayList<ComponentName>();
620 bindTargets.add(new ComponentName("com.android.alarmclock",
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700621 "com.android.alarmclock.AnalogAppWidgetProvider"));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700622
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700623 boolean allocatedAppWidgets = false;
Mike Cleronb87bd162009-10-30 16:36:56 -0700624
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700625 // Try binding to an analog clock widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700627 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700628
629 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_WIDGET_CLOCK);
630 values.put(Favorites.SPANX, 2);
631 values.put(Favorites.SPANY, 2);
632 values.put(Favorites.APPWIDGET_ID, appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633 db.insert(TABLE_FAVORITES, null, values);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700634
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700635 allocatedAppWidgets = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636 } catch (RuntimeException ex) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700637 Log.e(LOG_TAG, "Problem allocating appWidgetId", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638 }
639
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700640 // If any appWidgetIds allocated, then launch over to binder
641 if (allocatedAppWidgets) {
642 launchAppWidgetBinder(bindSources, bindTargets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800643 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700644
645 return allocatedAppWidgets;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646 }
Mike Cleronb87bd162009-10-30 16:36:56 -0700647
648 private boolean addAppWidget(SQLiteDatabase db, ContentValues values, TypedArray a) {
649 final int[] bindSources = new int[] {
650 Favorites.ITEM_TYPE_APPWIDGET,
651 };
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652
Mike Cleronb87bd162009-10-30 16:36:56 -0700653 String packageName = a.getString(R.styleable.Favorite_packageName);
654 String className = a.getString(R.styleable.Favorite_className);
655
656 if (packageName == null || className == null) {
657 return false;
658 }
659
660 ComponentName cn = new ComponentName(packageName, className);
661
662 final ArrayList<ComponentName> bindTargets = new ArrayList<ComponentName>();
663 bindTargets.add(cn);
664
665 boolean allocatedAppWidgets = false;
666 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
667
668 try {
669 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
670
671 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
672 values.put(Favorites.SPANX, a.getString(R.styleable.Favorite_spanX));
673 values.put(Favorites.SPANY, a.getString(R.styleable.Favorite_spanY));
674 values.put(Favorites.APPWIDGET_ID, appWidgetId);
675 db.insert(TABLE_FAVORITES, null, values);
676
677 allocatedAppWidgets = true;
678
679 appWidgetManager.bindAppWidgetId(appWidgetId, cn);
680 } catch (RuntimeException ex) {
681 Log.e(LOG_TAG, "Problem allocating appWidgetId", ex);
682 }
683
684 return allocatedAppWidgets;
685 }
686
687 private boolean addUriShortcut(SQLiteDatabase db, ContentValues values,
688 TypedArray a) {
689 Resources r = mContext.getResources();
690
691 final int iconResId = a.getResourceId(R.styleable.Favorite_icon, 0);
692 final int titleResId = a.getResourceId(R.styleable.Favorite_title, 0);
693
694 Intent intent = null;
695 String uri = null;
696 try {
697 uri = a.getString(R.styleable.Favorite_uri);
698 intent = Intent.parseUri(uri, 0);
699 } catch (URISyntaxException e) {
700 Log.w(LauncherModel.TAG, "Shortcut has malformed uri: " + uri);
701 return false; // Oh well
702 }
703
704 if (iconResId == 0 || titleResId == 0) {
705 Log.w(LauncherModel.TAG,
706 "Shortcut is missing title or icon resource ID");
707 return false;
708 }
709
710 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
711 values.put(Favorites.INTENT, intent.toUri(0));
712 values.put(Favorites.TITLE, r.getString(titleResId));
713 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_SHORTCUT);
714 values.put(Favorites.SPANX, 1);
715 values.put(Favorites.SPANY, 1);
716 values.put(Favorites.ICON_TYPE, Favorites.ICON_TYPE_RESOURCE);
717 values.put(Favorites.ICON_PACKAGE, mContext.getPackageName());
718 values.put(Favorites.ICON_RESOURCE, r.getResourceName(iconResId));
719
720 db.insert(TABLE_FAVORITES, null, values);
721
722 return true;
723 }
724 }
725
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726 /**
727 * Build a query string that will match any row where the column matches
728 * anything in the values list.
729 */
730 static String buildOrWhereString(String column, int[] values) {
731 StringBuilder selectWhere = new StringBuilder();
732 for (int i = values.length - 1; i >= 0; i--) {
733 selectWhere.append(column).append("=").append(values[i]);
734 if (i > 0) {
735 selectWhere.append(" OR ");
736 }
737 }
738 return selectWhere.toString();
739 }
740
741 static class SqlArguments {
742 public final String table;
743 public final String where;
744 public final String[] args;
745
746 SqlArguments(Uri url, String where, String[] args) {
747 if (url.getPathSegments().size() == 1) {
748 this.table = url.getPathSegments().get(0);
749 this.where = where;
750 this.args = args;
751 } else if (url.getPathSegments().size() != 2) {
752 throw new IllegalArgumentException("Invalid URI: " + url);
753 } else if (!TextUtils.isEmpty(where)) {
754 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
755 } else {
756 this.table = url.getPathSegments().get(0);
757 this.where = "_id=" + ContentUris.parseId(url);
758 this.args = null;
759 }
760 }
761
762 SqlArguments(Uri url) {
763 if (url.getPathSegments().size() == 1) {
764 table = url.getPathSegments().get(0);
765 where = null;
766 args = null;
767 } else {
768 throw new IllegalArgumentException("Invalid URI: " + url);
769 }
770 }
771 }
772}