blob: 9226c22538915c8c4159c4d7db45d936129c721e [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
Romain Guy73b979d2009-06-09 12:57:21 -070061 private static final int DATABASE_VERSION = 4;
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
396 if (version != DATABASE_VERSION) {
397 Log.w(LOG_TAG, "Destroying all old data.");
398 db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES);
Romain Guy417d2342009-06-19 14:28:51 -0700399 db.execSQL("DROP TABLE IF EXISTS " + TABLE_GESTURES);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 onCreate(db);
401 }
402 }
403
404 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700405 * Upgrade existing clock and photo frame widgets into their new widget
406 * equivalents. This method allocates appWidgetIds, and then hands off to
407 * LauncherAppWidgetBinder to finish the actual binding.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 */
409 private void convertWidgets(SQLiteDatabase db) {
410 final int[] bindSources = new int[] {
411 Favorites.ITEM_TYPE_WIDGET_CLOCK,
412 Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME,
413 };
414
415 final ArrayList<ComponentName> bindTargets = new ArrayList<ComponentName>();
416 bindTargets.add(new ComponentName("com.android.alarmclock",
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700417 "com.android.alarmclock.AnalogAppWidgetProvider"));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800418 bindTargets.add(new ComponentName("com.android.camera",
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700419 "com.android.camera.PhotoAppWidgetProvider"));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800420
421 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE, bindSources);
422
423 Cursor c = null;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700424 boolean allocatedAppWidgets = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800425
426 db.beginTransaction();
427 try {
428 // Select and iterate through each matching widget
429 c = db.query(TABLE_FAVORITES, new String[] { Favorites._ID },
430 selectWhere, null, null, null, null);
431
432 if (LOGD) Log.d(LOG_TAG, "found upgrade cursor count="+c.getCount());
433
434 final ContentValues values = new ContentValues();
435 while (c != null && c.moveToNext()) {
436 long favoriteId = c.getLong(0);
437
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700438 // Allocate and update database with new appWidgetId
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800439 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700440 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700442 if (LOGD) Log.d(LOG_TAG, "allocated appWidgetId="+appWidgetId+" for favoriteId="+favoriteId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443
444 values.clear();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700445 values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446
447 // Original widgets might not have valid spans when upgrading
448 values.put(LauncherSettings.Favorites.SPANX, 2);
449 values.put(LauncherSettings.Favorites.SPANY, 2);
450
451 String updateWhere = Favorites._ID + "=" + favoriteId;
452 db.update(TABLE_FAVORITES, values, updateWhere, null);
453
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700454 allocatedAppWidgets = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 } catch (RuntimeException ex) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700456 Log.e(LOG_TAG, "Problem allocating appWidgetId", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 }
458 }
459
460 db.setTransactionSuccessful();
461 } catch (SQLException ex) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700462 Log.w(LOG_TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800463 } finally {
464 db.endTransaction();
465 if (c != null) {
466 c.close();
467 }
468 }
469
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700470 // If any appWidgetIds allocated, then launch over to binder
471 if (allocatedAppWidgets) {
472 launchAppWidgetBinder(bindSources, bindTargets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800473 }
474 }
475
476 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700477 * Launch the widget binder that walks through the Launcher database,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800478 * binding any matching widgets to the corresponding targets. We can't
479 * bind ourselves because our parent process can't obtain the
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700480 * BIND_APPWIDGET permission.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800481 */
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700482 private void launchAppWidgetBinder(int[] bindSources, ArrayList<ComponentName> bindTargets) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800483 final Intent intent = new Intent();
484 intent.setComponent(new ComponentName("com.android.settings",
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700485 "com.android.settings.LauncherAppWidgetBinder"));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800486 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
487
488 final Bundle extras = new Bundle();
489 extras.putIntArray(EXTRA_BIND_SOURCES, bindSources);
490 extras.putParcelableArrayList(EXTRA_BIND_TARGETS, bindTargets);
491 intent.putExtras(extras);
492
493 mContext.startActivity(intent);
494 }
495
496 /**
497 * Loads the default set of favorite packages from an xml file.
498 *
499 * @param db The database to write the values into
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800500 */
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700501 private int loadFavorites(SQLiteDatabase db) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800502 Intent intent = new Intent(Intent.ACTION_MAIN, null);
503 intent.addCategory(Intent.CATEGORY_LAUNCHER);
504 ContentValues values = new ContentValues();
505
506 PackageManager packageManager = mContext.getPackageManager();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800507 int i = 0;
508 try {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700509 XmlResourceParser parser = mContext.getResources().getXml(R.xml.default_workspace);
510 AttributeSet attrs = Xml.asAttributeSet(parser);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800511 XmlUtils.beginDocument(parser, TAG_FAVORITES);
512
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700513 final int depth = parser.getDepth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800514
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700515 int type;
516 while (((type = parser.next()) != XmlPullParser.END_TAG ||
517 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
518
519 if (type != XmlPullParser.START_TAG) {
520 continue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800521 }
522
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700523 boolean added = false;
524 final String name = parser.getName();
525
526 TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.Favorite);
527
528 values.clear();
529 values.put(LauncherSettings.Favorites.CONTAINER,
530 LauncherSettings.Favorites.CONTAINER_DESKTOP);
531 values.put(LauncherSettings.Favorites.SCREEN,
532 a.getString(R.styleable.Favorite_screen));
533 values.put(LauncherSettings.Favorites.CELLX,
534 a.getString(R.styleable.Favorite_x));
535 values.put(LauncherSettings.Favorites.CELLY,
536 a.getString(R.styleable.Favorite_y));
537
538 if (TAG_FAVORITE.equals(name)) {
Mike Cleronb87bd162009-10-30 16:36:56 -0700539 added = addAppShortcut(db, values, a, packageManager, intent);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700540 } else if (TAG_SEARCH.equals(name)) {
541 added = addSearchWidget(db, values);
542 } else if (TAG_CLOCK.equals(name)) {
543 added = addClockWidget(db, values);
Mike Cleronb87bd162009-10-30 16:36:56 -0700544 } else if (TAG_APPWIDGET.equals(name)) {
545 added = addAppWidget(db, values, a);
546 } else if (TAG_SHORTCUT.equals(name)) {
547 added = addUriShortcut(db, values, a);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800548 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700549
550 if (added) i++;
551
552 a.recycle();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800553 }
554 } catch (XmlPullParserException e) {
555 Log.w(LOG_TAG, "Got exception parsing favorites.", e);
556 } catch (IOException e) {
557 Log.w(LOG_TAG, "Got exception parsing favorites.", e);
558 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700559
560 return i;
561 }
562
Mike Cleronb87bd162009-10-30 16:36:56 -0700563 private boolean addAppShortcut(SQLiteDatabase db, ContentValues values, TypedArray a,
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700564 PackageManager packageManager, Intent intent) {
565
566 ActivityInfo info;
567 String packageName = a.getString(R.styleable.Favorite_packageName);
568 String className = a.getString(R.styleable.Favorite_className);
569 try {
570 ComponentName cn = new ComponentName(packageName, className);
571 info = packageManager.getActivityInfo(cn, 0);
572 intent.setComponent(cn);
Dianne Hackbornbd2e54d2009-03-24 21:00:33 -0700573 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
574 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Romain Guy1ce1a242009-06-23 17:34:54 -0700575 values.put(Favorites.INTENT, intent.toUri(0));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700576 values.put(Favorites.TITLE, info.loadLabel(packageManager).toString());
577 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPLICATION);
578 values.put(Favorites.SPANX, 1);
579 values.put(Favorites.SPANY, 1);
580 db.insert(TABLE_FAVORITES, null, values);
581 } catch (PackageManager.NameNotFoundException e) {
582 Log.w(LOG_TAG, "Unable to add favorite: " + packageName +
583 "/" + className, e);
584 return false;
585 }
586 return true;
587 }
588
589 private boolean addSearchWidget(SQLiteDatabase db, ContentValues values) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800590 // Add a search box
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700591 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_WIDGET_SEARCH);
592 values.put(Favorites.SPANX, 4);
593 values.put(Favorites.SPANY, 1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800594 db.insert(TABLE_FAVORITES, null, values);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700595
596 return true;
597 }
598
599 private boolean addClockWidget(SQLiteDatabase db, ContentValues values) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800600 final int[] bindSources = new int[] {
601 Favorites.ITEM_TYPE_WIDGET_CLOCK,
602 };
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700603
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800604 final ArrayList<ComponentName> bindTargets = new ArrayList<ComponentName>();
605 bindTargets.add(new ComponentName("com.android.alarmclock",
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700606 "com.android.alarmclock.AnalogAppWidgetProvider"));
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700607
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700608 boolean allocatedAppWidgets = false;
Mike Cleronb87bd162009-10-30 16:36:56 -0700609
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700610 // Try binding to an analog clock widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700612 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700613
614 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_WIDGET_CLOCK);
615 values.put(Favorites.SPANX, 2);
616 values.put(Favorites.SPANY, 2);
617 values.put(Favorites.APPWIDGET_ID, appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800618 db.insert(TABLE_FAVORITES, null, values);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700619
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700620 allocatedAppWidgets = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800621 } catch (RuntimeException ex) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700622 Log.e(LOG_TAG, "Problem allocating appWidgetId", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800623 }
624
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700625 // If any appWidgetIds allocated, then launch over to binder
626 if (allocatedAppWidgets) {
627 launchAppWidgetBinder(bindSources, bindTargets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800628 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700629
630 return allocatedAppWidgets;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631 }
Mike Cleronb87bd162009-10-30 16:36:56 -0700632
633 private boolean addAppWidget(SQLiteDatabase db, ContentValues values, TypedArray a) {
634 final int[] bindSources = new int[] {
635 Favorites.ITEM_TYPE_APPWIDGET,
636 };
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637
Mike Cleronb87bd162009-10-30 16:36:56 -0700638 String packageName = a.getString(R.styleable.Favorite_packageName);
639 String className = a.getString(R.styleable.Favorite_className);
640
641 if (packageName == null || className == null) {
642 return false;
643 }
644
645 ComponentName cn = new ComponentName(packageName, className);
646
647 final ArrayList<ComponentName> bindTargets = new ArrayList<ComponentName>();
648 bindTargets.add(cn);
649
650 boolean allocatedAppWidgets = false;
651 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
652
653 try {
654 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
655
656 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
657 values.put(Favorites.SPANX, a.getString(R.styleable.Favorite_spanX));
658 values.put(Favorites.SPANY, a.getString(R.styleable.Favorite_spanY));
659 values.put(Favorites.APPWIDGET_ID, appWidgetId);
660 db.insert(TABLE_FAVORITES, null, values);
661
662 allocatedAppWidgets = true;
663
664 appWidgetManager.bindAppWidgetId(appWidgetId, cn);
665 } catch (RuntimeException ex) {
666 Log.e(LOG_TAG, "Problem allocating appWidgetId", ex);
667 }
668
669 return allocatedAppWidgets;
670 }
671
672 private boolean addUriShortcut(SQLiteDatabase db, ContentValues values,
673 TypedArray a) {
674 Resources r = mContext.getResources();
675
676 final int iconResId = a.getResourceId(R.styleable.Favorite_icon, 0);
677 final int titleResId = a.getResourceId(R.styleable.Favorite_title, 0);
678
679 Intent intent = null;
680 String uri = null;
681 try {
682 uri = a.getString(R.styleable.Favorite_uri);
683 intent = Intent.parseUri(uri, 0);
684 } catch (URISyntaxException e) {
685 Log.w(LauncherModel.TAG, "Shortcut has malformed uri: " + uri);
686 return false; // Oh well
687 }
688
689 if (iconResId == 0 || titleResId == 0) {
690 Log.w(LauncherModel.TAG,
691 "Shortcut is missing title or icon resource ID");
692 return false;
693 }
694
695 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
696 values.put(Favorites.INTENT, intent.toUri(0));
697 values.put(Favorites.TITLE, r.getString(titleResId));
698 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_SHORTCUT);
699 values.put(Favorites.SPANX, 1);
700 values.put(Favorites.SPANY, 1);
701 values.put(Favorites.ICON_TYPE, Favorites.ICON_TYPE_RESOURCE);
702 values.put(Favorites.ICON_PACKAGE, mContext.getPackageName());
703 values.put(Favorites.ICON_RESOURCE, r.getResourceName(iconResId));
704
705 db.insert(TABLE_FAVORITES, null, values);
706
707 return true;
708 }
709 }
710
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 /**
712 * Build a query string that will match any row where the column matches
713 * anything in the values list.
714 */
715 static String buildOrWhereString(String column, int[] values) {
716 StringBuilder selectWhere = new StringBuilder();
717 for (int i = values.length - 1; i >= 0; i--) {
718 selectWhere.append(column).append("=").append(values[i]);
719 if (i > 0) {
720 selectWhere.append(" OR ");
721 }
722 }
723 return selectWhere.toString();
724 }
725
726 static class SqlArguments {
727 public final String table;
728 public final String where;
729 public final String[] args;
730
731 SqlArguments(Uri url, String where, String[] args) {
732 if (url.getPathSegments().size() == 1) {
733 this.table = url.getPathSegments().get(0);
734 this.where = where;
735 this.args = args;
736 } else if (url.getPathSegments().size() != 2) {
737 throw new IllegalArgumentException("Invalid URI: " + url);
738 } else if (!TextUtils.isEmpty(where)) {
739 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
740 } else {
741 this.table = url.getPathSegments().get(0);
742 this.where = "_id=" + ContentUris.parseId(url);
743 this.args = null;
744 }
745 }
746
747 SqlArguments(Uri url) {
748 if (url.getPathSegments().size() == 1) {
749 table = url.getPathSegments().get(0);
750 where = null;
751 args = null;
752 } else {
753 throw new IllegalArgumentException("Invalid URI: " + url);
754 }
755 }
756 }
757}