blob: 83f185ea4dc0dbc2dfd7c6ae577543b752473497 [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
17package com.android.launcher;
18
The Android Open Source Project7376fae2009-03-11 12:11:58 -070019import android.appwidget.AppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080020import android.content.ContentProvider;
21import android.content.Context;
22import android.content.ContentValues;
23import android.content.Intent;
24import android.content.ComponentName;
25import android.content.ContentUris;
26import android.content.ContentResolver;
27import android.content.pm.PackageManager;
28import android.content.pm.ActivityInfo;
29import android.database.sqlite.SQLiteOpenHelper;
30import android.database.sqlite.SQLiteDatabase;
31import android.database.sqlite.SQLiteQueryBuilder;
32import android.database.Cursor;
33import android.database.SQLException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.util.Log;
35import android.util.Xml;
36import android.net.Uri;
37import android.text.TextUtils;
38import android.os.*;
39import android.provider.Settings;
40
41import java.io.FileReader;
42import java.io.File;
43import java.io.FileNotFoundException;
44import java.io.IOException;
45import java.util.ArrayList;
46
47import org.xmlpull.v1.XmlPullParser;
48import org.xmlpull.v1.XmlPullParserException;
49import com.android.internal.util.XmlUtils;
50import com.android.launcher.LauncherSettings.Favorites;
51
52public class LauncherProvider extends ContentProvider {
53 private static final String LOG_TAG = "LauncherProvider";
54 private static final boolean LOGD = true;
55
56 private static final String DATABASE_NAME = "launcher.db";
57
58 private static final int DATABASE_VERSION = 2;
59
60 static final String AUTHORITY = "com.android.launcher.settings";
61
62 static final String EXTRA_BIND_SOURCES = "com.android.launcher.settings.bindsources";
63 static final String EXTRA_BIND_TARGETS = "com.android.launcher.settings.bindtargets";
64
65 static final String TABLE_FAVORITES = "favorites";
66 static final String PARAMETER_NOTIFY = "notify";
67
68 private SQLiteOpenHelper mOpenHelper;
69
70 @Override
71 public boolean onCreate() {
72 mOpenHelper = new DatabaseHelper(getContext());
73 return true;
74 }
75
76 @Override
77 public String getType(Uri uri) {
78 SqlArguments args = new SqlArguments(uri, null, null);
79 if (TextUtils.isEmpty(args.where)) {
80 return "vnd.android.cursor.dir/" + args.table;
81 } else {
82 return "vnd.android.cursor.item/" + args.table;
83 }
84 }
85
86 @Override
87 public Cursor query(Uri uri, String[] projection, String selection,
88 String[] selectionArgs, String sortOrder) {
89
90 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
91 SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
92 qb.setTables(args.table);
93
94 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
95 Cursor result = qb.query(db, projection, args.where, args.args, null, null, sortOrder);
96 result.setNotificationUri(getContext().getContentResolver(), uri);
97
98 return result;
99 }
100
101 @Override
102 public Uri insert(Uri uri, ContentValues initialValues) {
103 SqlArguments args = new SqlArguments(uri);
104
105 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
106 final long rowId = db.insert(args.table, null, initialValues);
107 if (rowId <= 0) return null;
108
109 uri = ContentUris.withAppendedId(uri, rowId);
110 sendNotify(uri);
111
112 return uri;
113 }
114
115 @Override
116 public int bulkInsert(Uri uri, ContentValues[] values) {
117 SqlArguments args = new SqlArguments(uri);
118
119 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
120 db.beginTransaction();
121 try {
122 int numValues = values.length;
123 for (int i = 0; i < numValues; i++) {
124 if (db.insert(args.table, null, values[i]) < 0) return 0;
125 }
126 db.setTransactionSuccessful();
127 } finally {
128 db.endTransaction();
129 }
130
131 sendNotify(uri);
132 return values.length;
133 }
134
135 @Override
136 public int delete(Uri uri, String selection, String[] selectionArgs) {
137 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
138
139 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
140 int count = db.delete(args.table, args.where, args.args);
141 if (count > 0) sendNotify(uri);
142
143 return count;
144 }
145
146 @Override
147 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
148 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
149
150 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
151 int count = db.update(args.table, values, args.where, args.args);
152 if (count > 0) sendNotify(uri);
153
154 return count;
155 }
156
157 private void sendNotify(Uri uri) {
158 String notify = uri.getQueryParameter(PARAMETER_NOTIFY);
159 if (notify == null || "true".equals(notify)) {
160 getContext().getContentResolver().notifyChange(uri, null);
161 }
162 }
163
164 private static class DatabaseHelper extends SQLiteOpenHelper {
165 /**
166 * Path to file containing default favorite packages, relative to ANDROID_ROOT.
167 */
168 private static final String DEFAULT_FAVORITES_PATH = "etc/favorites.xml";
169
170 private static final String TAG_FAVORITES = "favorites";
171 private static final String TAG_FAVORITE = "favorite";
172 private static final String TAG_PACKAGE = "package";
173 private static final String TAG_CLASS = "class";
174
175 private static final String ATTRIBUTE_SCREEN = "screen";
176 private static final String ATTRIBUTE_X = "x";
177 private static final String ATTRIBUTE_Y = "y";
178
179 private final Context mContext;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700180 private final AppWidgetHost mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181
182 DatabaseHelper(Context context) {
183 super(context, DATABASE_NAME, null, DATABASE_VERSION);
184 mContext = context;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700185 mAppWidgetHost = new AppWidgetHost(context, Launcher.APPWIDGET_HOST_ID);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 }
187
188 @Override
189 public void onCreate(SQLiteDatabase db) {
190 if (LOGD) Log.d(LOG_TAG, "creating new launcher database");
191
192 db.execSQL("CREATE TABLE favorites (" +
193 "_id INTEGER PRIMARY KEY," +
194 "title TEXT," +
195 "intent TEXT," +
196 "container INTEGER," +
197 "screen INTEGER," +
198 "cellX INTEGER," +
199 "cellY INTEGER," +
200 "spanX INTEGER," +
201 "spanY INTEGER," +
202 "itemType INTEGER," +
203 "gadgetId INTEGER NOT NULL DEFAULT -1," +
204 "isShortcut INTEGER," +
205 "iconType INTEGER," +
206 "iconPackage TEXT," +
207 "iconResource TEXT," +
208 "icon BLOB," +
209 "uri TEXT," +
210 "displayMode INTEGER" +
211 ");");
212
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700213 // Database was just created, so wipe any previous widgets
214 if (mAppWidgetHost != null) {
215 mAppWidgetHost.deleteHost();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216 }
217
218 if (!convertDatabase(db)) {
219 // Populate favorites table with initial favorites
220 loadFavorites(db, DEFAULT_FAVORITES_PATH);
221 }
222 }
223
224 private boolean convertDatabase(SQLiteDatabase db) {
225 if (LOGD) Log.d(LOG_TAG, "converting database from an older format, but not onUpgrade");
226 boolean converted = false;
227
228 final Uri uri = Uri.parse("content://" + Settings.AUTHORITY +
229 "/old_favorites?notify=true");
230 final ContentResolver resolver = mContext.getContentResolver();
231 Cursor cursor = null;
232
233 try {
234 cursor = resolver.query(uri, null, null, null, null);
235 } catch (Exception e) {
236 // Ignore
237 }
238
239 // We already have a favorites database in the old provider
240 if (cursor != null && cursor.getCount() > 0) {
241 try {
242 converted = copyFromCursor(db, cursor) > 0;
243 } finally {
244 cursor.close();
245 }
246
247 if (converted) {
248 resolver.delete(uri, null, null);
249 }
250 }
251
252 if (converted) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700253 // Convert widgets from this import into widgets
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254 if (LOGD) Log.d(LOG_TAG, "converted and now triggering widget upgrade");
255 convertWidgets(db);
256 }
257
258 return converted;
259 }
260
261 private int copyFromCursor(SQLiteDatabase db, Cursor c) {
262 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ID);
263 final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
264 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
265 final int iconTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
266 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
267 final int iconPackageIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
268 final int iconResourceIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
269 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
270 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
271 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
272 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
273 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
274 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
275 final int displayModeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
276
277 ContentValues[] rows = new ContentValues[c.getCount()];
278 int i = 0;
279 while (c.moveToNext()) {
280 ContentValues values = new ContentValues(c.getColumnCount());
281 values.put(LauncherSettings.Favorites.ID, c.getLong(idIndex));
282 values.put(LauncherSettings.Favorites.INTENT, c.getString(intentIndex));
283 values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex));
284 values.put(LauncherSettings.Favorites.ICON_TYPE, c.getInt(iconTypeIndex));
285 values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex));
286 values.put(LauncherSettings.Favorites.ICON_PACKAGE, c.getString(iconPackageIndex));
287 values.put(LauncherSettings.Favorites.ICON_RESOURCE, c.getString(iconResourceIndex));
288 values.put(LauncherSettings.Favorites.CONTAINER, c.getInt(containerIndex));
289 values.put(LauncherSettings.Favorites.ITEM_TYPE, c.getInt(itemTypeIndex));
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700290 values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800291 values.put(LauncherSettings.Favorites.SCREEN, c.getInt(screenIndex));
292 values.put(LauncherSettings.Favorites.CELLX, c.getInt(cellXIndex));
293 values.put(LauncherSettings.Favorites.CELLY, c.getInt(cellYIndex));
294 values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex));
295 values.put(LauncherSettings.Favorites.DISPLAY_MODE, c.getInt(displayModeIndex));
296 rows[i++] = values;
297 }
298
299 db.beginTransaction();
300 int total = 0;
301 try {
302 int numValues = rows.length;
303 for (i = 0; i < numValues; i++) {
304 if (db.insert(TABLE_FAVORITES, null, rows[i]) < 0) {
305 return 0;
306 } else {
307 total++;
308 }
309 }
310 db.setTransactionSuccessful();
311 } finally {
312 db.endTransaction();
313 }
314
315 return total;
316 }
317
318 @Override
319 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
320 if (LOGD) Log.d(LOG_TAG, "onUpgrade triggered");
321
322 int version = oldVersion;
323 if (version == 1) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700324 // upgrade 1 -> 2 added appWidgetId column
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800325 db.beginTransaction();
326 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700327 // Insert new column for holding appWidgetIds
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800328 db.execSQL("ALTER TABLE favorites " +
329 "ADD COLUMN gadgetId INTEGER NOT NULL DEFAULT -1;");
330 db.setTransactionSuccessful();
331 version = 2;
332 } catch (SQLException ex) {
333 // Old version remains, which means we wipe old data
334 Log.e(LOG_TAG, ex.getMessage(), ex);
335 } finally {
336 db.endTransaction();
337 }
338
339 // Convert existing widgets only if table upgrade was successful
340 if (version == 2) {
341 convertWidgets(db);
342 }
343 }
344
345 if (version != DATABASE_VERSION) {
346 Log.w(LOG_TAG, "Destroying all old data.");
347 db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES);
348 onCreate(db);
349 }
350 }
351
352 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700353 * Upgrade existing clock and photo frame widgets into their new widget
354 * equivalents. This method allocates appWidgetIds, and then hands off to
355 * LauncherAppWidgetBinder to finish the actual binding.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 */
357 private void convertWidgets(SQLiteDatabase db) {
358 final int[] bindSources = new int[] {
359 Favorites.ITEM_TYPE_WIDGET_CLOCK,
360 Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME,
361 };
362
363 final ArrayList<ComponentName> bindTargets = new ArrayList<ComponentName>();
364 bindTargets.add(new ComponentName("com.android.alarmclock",
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700365 "com.android.alarmclock.AnalogAppWidgetProvider"));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366 bindTargets.add(new ComponentName("com.android.camera",
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700367 "com.android.camera.PhotoAppWidgetProvider"));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368
369 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE, bindSources);
370
371 Cursor c = null;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700372 boolean allocatedAppWidgets = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373
374 db.beginTransaction();
375 try {
376 // Select and iterate through each matching widget
377 c = db.query(TABLE_FAVORITES, new String[] { Favorites._ID },
378 selectWhere, null, null, null, null);
379
380 if (LOGD) Log.d(LOG_TAG, "found upgrade cursor count="+c.getCount());
381
382 final ContentValues values = new ContentValues();
383 while (c != null && c.moveToNext()) {
384 long favoriteId = c.getLong(0);
385
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700386 // Allocate and update database with new appWidgetId
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700388 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700390 if (LOGD) Log.d(LOG_TAG, "allocated appWidgetId="+appWidgetId+" for favoriteId="+favoriteId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391
392 values.clear();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700393 values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394
395 // Original widgets might not have valid spans when upgrading
396 values.put(LauncherSettings.Favorites.SPANX, 2);
397 values.put(LauncherSettings.Favorites.SPANY, 2);
398
399 String updateWhere = Favorites._ID + "=" + favoriteId;
400 db.update(TABLE_FAVORITES, values, updateWhere, null);
401
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700402 allocatedAppWidgets = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800403 } catch (RuntimeException ex) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700404 Log.e(LOG_TAG, "Problem allocating appWidgetId", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800405 }
406 }
407
408 db.setTransactionSuccessful();
409 } catch (SQLException ex) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700410 Log.w(LOG_TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800411 } finally {
412 db.endTransaction();
413 if (c != null) {
414 c.close();
415 }
416 }
417
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700418 // If any appWidgetIds allocated, then launch over to binder
419 if (allocatedAppWidgets) {
420 launchAppWidgetBinder(bindSources, bindTargets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800421 }
422 }
423
424 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700425 * Launch the widget binder that walks through the Launcher database,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800426 * binding any matching widgets to the corresponding targets. We can't
427 * bind ourselves because our parent process can't obtain the
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700428 * BIND_APPWIDGET permission.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800429 */
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700430 private void launchAppWidgetBinder(int[] bindSources, ArrayList<ComponentName> bindTargets) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 final Intent intent = new Intent();
432 intent.setComponent(new ComponentName("com.android.settings",
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700433 "com.android.settings.LauncherAppWidgetBinder"));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800434 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
435
436 final Bundle extras = new Bundle();
437 extras.putIntArray(EXTRA_BIND_SOURCES, bindSources);
438 extras.putParcelableArrayList(EXTRA_BIND_TARGETS, bindTargets);
439 intent.putExtras(extras);
440
441 mContext.startActivity(intent);
442 }
443
444 /**
445 * Loads the default set of favorite packages from an xml file.
446 *
447 * @param db The database to write the values into
448 * @param subPath The relative path from ANDROID_ROOT to the file to read
449 */
450 private int loadFavorites(SQLiteDatabase db, String subPath) {
451 FileReader favReader;
452
453 // Environment.getRootDirectory() is a fancy way of saying ANDROID_ROOT or "/system".
454 final File favFile = new File(Environment.getRootDirectory(), subPath);
455 try {
456 favReader = new FileReader(favFile);
457 } catch (FileNotFoundException e) {
458 Log.e(LOG_TAG, "Couldn't find or open favorites file " + favFile);
459 return 0;
460 }
461
462 Intent intent = new Intent(Intent.ACTION_MAIN, null);
463 intent.addCategory(Intent.CATEGORY_LAUNCHER);
464 ContentValues values = new ContentValues();
465
466 PackageManager packageManager = mContext.getPackageManager();
467 ActivityInfo info;
468 int i = 0;
469 try {
470 XmlPullParser parser = Xml.newPullParser();
471 parser.setInput(favReader);
472
473 XmlUtils.beginDocument(parser, TAG_FAVORITES);
474
475 while (true) {
476 XmlUtils.nextElement(parser);
477
478 String name = parser.getName();
479 if (!TAG_FAVORITE.equals(name)) {
480 break;
481 }
482
483 String pkg = parser.getAttributeValue(null, TAG_PACKAGE);
484 String cls = parser.getAttributeValue(null, TAG_CLASS);
485 try {
486 ComponentName cn = new ComponentName(pkg, cls);
487 info = packageManager.getActivityInfo(cn, 0);
488 intent.setComponent(cn);
489 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
490 values.put(LauncherSettings.Favorites.INTENT, intent.toURI());
491 values.put(LauncherSettings.Favorites.TITLE,
492 info.loadLabel(packageManager).toString());
493 values.put(LauncherSettings.Favorites.CONTAINER,
494 LauncherSettings.Favorites.CONTAINER_DESKTOP);
495 values.put(LauncherSettings.Favorites.ITEM_TYPE,
496 LauncherSettings.Favorites.ITEM_TYPE_APPLICATION);
497 values.put(LauncherSettings.Favorites.SCREEN,
498 parser.getAttributeValue(null, ATTRIBUTE_SCREEN));
499 values.put(LauncherSettings.Favorites.CELLX,
500 parser.getAttributeValue(null, ATTRIBUTE_X));
501 values.put(LauncherSettings.Favorites.CELLY,
502 parser.getAttributeValue(null, ATTRIBUTE_Y));
503 values.put(LauncherSettings.Favorites.SPANX, 1);
504 values.put(LauncherSettings.Favorites.SPANY, 1);
505 db.insert(TABLE_FAVORITES, null, values);
506 i++;
507 } catch (PackageManager.NameNotFoundException e) {
508 Log.w(LOG_TAG, "Unable to add favorite: " + pkg + "/" + cls, e);
509 }
510 }
511 } catch (XmlPullParserException e) {
512 Log.w(LOG_TAG, "Got exception parsing favorites.", e);
513 } catch (IOException e) {
514 Log.w(LOG_TAG, "Got exception parsing favorites.", e);
515 }
516
517 // Add a search box
518 values.clear();
519 values.put(LauncherSettings.Favorites.CONTAINER,
520 LauncherSettings.Favorites.CONTAINER_DESKTOP);
521 values.put(LauncherSettings.Favorites.ITEM_TYPE,
522 LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH);
523 values.put(LauncherSettings.Favorites.SCREEN, 2);
524 values.put(LauncherSettings.Favorites.CELLX, 0);
525 values.put(LauncherSettings.Favorites.CELLY, 0);
526 values.put(LauncherSettings.Favorites.SPANX, 4);
527 values.put(LauncherSettings.Favorites.SPANY, 1);
528 db.insert(TABLE_FAVORITES, null, values);
529
530 final int[] bindSources = new int[] {
531 Favorites.ITEM_TYPE_WIDGET_CLOCK,
532 };
533
534 final ArrayList<ComponentName> bindTargets = new ArrayList<ComponentName>();
535 bindTargets.add(new ComponentName("com.android.alarmclock",
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700536 "com.android.alarmclock.AnalogAppWidgetProvider"));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800537
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700538 boolean allocatedAppWidgets = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800539
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700540 // Try binding to an analog clock widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800541 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700542 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800543
544 values.clear();
545 values.put(LauncherSettings.Favorites.CONTAINER,
546 LauncherSettings.Favorites.CONTAINER_DESKTOP);
547 values.put(LauncherSettings.Favorites.ITEM_TYPE,
548 LauncherSettings.Favorites.ITEM_TYPE_WIDGET_CLOCK);
549 values.put(LauncherSettings.Favorites.SCREEN, 1);
550 values.put(LauncherSettings.Favorites.CELLX, 1);
551 values.put(LauncherSettings.Favorites.CELLY, 0);
552 values.put(LauncherSettings.Favorites.SPANX, 2);
553 values.put(LauncherSettings.Favorites.SPANY, 2);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700554 values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800555 db.insert(TABLE_FAVORITES, null, values);
556
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700557 allocatedAppWidgets = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800558 } catch (RuntimeException ex) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700559 Log.e(LOG_TAG, "Problem allocating appWidgetId", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560 }
561
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700562 // If any appWidgetIds allocated, then launch over to binder
563 if (allocatedAppWidgets) {
564 launchAppWidgetBinder(bindSources, bindTargets);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800565 }
566
567 return i;
568 }
569 }
570
571 /**
572 * Build a query string that will match any row where the column matches
573 * anything in the values list.
574 */
575 static String buildOrWhereString(String column, int[] values) {
576 StringBuilder selectWhere = new StringBuilder();
577 for (int i = values.length - 1; i >= 0; i--) {
578 selectWhere.append(column).append("=").append(values[i]);
579 if (i > 0) {
580 selectWhere.append(" OR ");
581 }
582 }
583 return selectWhere.toString();
584 }
585
586 static class SqlArguments {
587 public final String table;
588 public final String where;
589 public final String[] args;
590
591 SqlArguments(Uri url, String where, String[] args) {
592 if (url.getPathSegments().size() == 1) {
593 this.table = url.getPathSegments().get(0);
594 this.where = where;
595 this.args = args;
596 } else if (url.getPathSegments().size() != 2) {
597 throw new IllegalArgumentException("Invalid URI: " + url);
598 } else if (!TextUtils.isEmpty(where)) {
599 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
600 } else {
601 this.table = url.getPathSegments().get(0);
602 this.where = "_id=" + ContentUris.parseId(url);
603 this.args = null;
604 }
605 }
606
607 SqlArguments(Uri url) {
608 if (url.getPathSegments().size() == 1) {
609 table = url.getPathSegments().get(0);
610 where = null;
611 args = null;
612 } else {
613 throw new IllegalArgumentException("Invalid URI: " + url);
614 }
615 }
616 }
617}