The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.launcher; |
| 18 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 19 | import android.appwidget.AppWidgetHost; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 20 | import android.content.ContentProvider; |
| 21 | import android.content.Context; |
| 22 | import android.content.ContentValues; |
| 23 | import android.content.Intent; |
| 24 | import android.content.ComponentName; |
| 25 | import android.content.ContentUris; |
| 26 | import android.content.ContentResolver; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 27 | import android.content.res.XmlResourceParser; |
| 28 | import android.content.res.TypedArray; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 29 | import android.content.pm.PackageManager; |
| 30 | import android.content.pm.ActivityInfo; |
| 31 | import android.database.sqlite.SQLiteOpenHelper; |
| 32 | import android.database.sqlite.SQLiteDatabase; |
| 33 | import android.database.sqlite.SQLiteQueryBuilder; |
| 34 | import android.database.Cursor; |
| 35 | import android.database.SQLException; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 36 | import android.util.Log; |
| 37 | import android.util.Xml; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 38 | import android.util.AttributeSet; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 39 | import android.net.Uri; |
| 40 | import android.text.TextUtils; |
| 41 | import android.os.*; |
| 42 | import android.provider.Settings; |
| 43 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 44 | import java.io.IOException; |
| 45 | import java.util.ArrayList; |
| 46 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 47 | import org.xmlpull.v1.XmlPullParserException; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 48 | import org.xmlpull.v1.XmlPullParser; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 49 | import com.android.internal.util.XmlUtils; |
| 50 | import com.android.launcher.LauncherSettings.Favorites; |
| 51 | |
| 52 | public 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 | |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 58 | private static final int DATABASE_VERSION = 3; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 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 | |
Jeffrey Sharkey | 2bbcae1 | 2009-03-31 14:37:57 -0700 | [diff] [blame^] | 68 | /** |
| 69 | * {@link Uri} triggered at any registered {@link ContentObserver} when |
| 70 | * {@link AppWidgetHost#deleteHost()} is called during database creation. |
| 71 | * Use this to recall {@link AppWidgetHost#startListening()} if needed. |
| 72 | */ |
| 73 | static final Uri CONTENT_APPWIDGET_RESET_URI = |
| 74 | Uri.parse("content://" + AUTHORITY + "/appWidgetReset"); |
| 75 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 76 | private SQLiteOpenHelper mOpenHelper; |
| 77 | |
| 78 | @Override |
| 79 | public boolean onCreate() { |
| 80 | mOpenHelper = new DatabaseHelper(getContext()); |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public String getType(Uri uri) { |
| 86 | SqlArguments args = new SqlArguments(uri, null, null); |
| 87 | if (TextUtils.isEmpty(args.where)) { |
| 88 | return "vnd.android.cursor.dir/" + args.table; |
| 89 | } else { |
| 90 | return "vnd.android.cursor.item/" + args.table; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public Cursor query(Uri uri, String[] projection, String selection, |
| 96 | String[] selectionArgs, String sortOrder) { |
| 97 | |
| 98 | SqlArguments args = new SqlArguments(uri, selection, selectionArgs); |
| 99 | SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); |
| 100 | qb.setTables(args.table); |
| 101 | |
| 102 | SQLiteDatabase db = mOpenHelper.getReadableDatabase(); |
| 103 | Cursor result = qb.query(db, projection, args.where, args.args, null, null, sortOrder); |
| 104 | result.setNotificationUri(getContext().getContentResolver(), uri); |
| 105 | |
| 106 | return result; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public Uri insert(Uri uri, ContentValues initialValues) { |
| 111 | SqlArguments args = new SqlArguments(uri); |
| 112 | |
| 113 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 114 | final long rowId = db.insert(args.table, null, initialValues); |
| 115 | if (rowId <= 0) return null; |
| 116 | |
| 117 | uri = ContentUris.withAppendedId(uri, rowId); |
| 118 | sendNotify(uri); |
| 119 | |
| 120 | return uri; |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public int bulkInsert(Uri uri, ContentValues[] values) { |
| 125 | SqlArguments args = new SqlArguments(uri); |
| 126 | |
| 127 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 128 | db.beginTransaction(); |
| 129 | try { |
| 130 | int numValues = values.length; |
| 131 | for (int i = 0; i < numValues; i++) { |
| 132 | if (db.insert(args.table, null, values[i]) < 0) return 0; |
| 133 | } |
| 134 | db.setTransactionSuccessful(); |
| 135 | } finally { |
| 136 | db.endTransaction(); |
| 137 | } |
| 138 | |
| 139 | sendNotify(uri); |
| 140 | return values.length; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public int delete(Uri uri, String selection, String[] selectionArgs) { |
| 145 | SqlArguments args = new SqlArguments(uri, selection, selectionArgs); |
| 146 | |
| 147 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 148 | int count = db.delete(args.table, args.where, args.args); |
| 149 | if (count > 0) sendNotify(uri); |
| 150 | |
| 151 | return count; |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { |
| 156 | SqlArguments args = new SqlArguments(uri, selection, selectionArgs); |
| 157 | |
| 158 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 159 | int count = db.update(args.table, values, args.where, args.args); |
| 160 | if (count > 0) sendNotify(uri); |
| 161 | |
| 162 | return count; |
| 163 | } |
| 164 | |
| 165 | private void sendNotify(Uri uri) { |
| 166 | String notify = uri.getQueryParameter(PARAMETER_NOTIFY); |
| 167 | if (notify == null || "true".equals(notify)) { |
| 168 | getContext().getContentResolver().notifyChange(uri, null); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | private static class DatabaseHelper extends SQLiteOpenHelper { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 173 | private static final String TAG_FAVORITES = "favorites"; |
| 174 | private static final String TAG_FAVORITE = "favorite"; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 175 | private static final String TAG_CLOCK = "clock"; |
| 176 | private static final String TAG_SEARCH = "search"; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 177 | |
| 178 | private final Context mContext; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 179 | private final AppWidgetHost mAppWidgetHost; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 180 | |
| 181 | DatabaseHelper(Context context) { |
| 182 | super(context, DATABASE_NAME, null, DATABASE_VERSION); |
| 183 | mContext = context; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 184 | mAppWidgetHost = new AppWidgetHost(context, Launcher.APPWIDGET_HOST_ID); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Jeffrey Sharkey | 2bbcae1 | 2009-03-31 14:37:57 -0700 | [diff] [blame^] | 187 | /** |
| 188 | * Send notification that we've deleted the {@link AppWidgetHost}, |
| 189 | * probably as part of the initial database creation. The receiver may |
| 190 | * want to re-call {@link AppWidgetHost#startListening()} to ensure |
| 191 | * callbacks are correctly set. |
| 192 | */ |
| 193 | private void sendAppWidgetResetNotify() { |
| 194 | final ContentResolver resolver = mContext.getContentResolver(); |
| 195 | resolver.notifyChange(CONTENT_APPWIDGET_RESET_URI, null); |
| 196 | } |
| 197 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 198 | @Override |
| 199 | public void onCreate(SQLiteDatabase db) { |
| 200 | if (LOGD) Log.d(LOG_TAG, "creating new launcher database"); |
| 201 | |
| 202 | db.execSQL("CREATE TABLE favorites (" + |
| 203 | "_id INTEGER PRIMARY KEY," + |
| 204 | "title TEXT," + |
| 205 | "intent TEXT," + |
| 206 | "container INTEGER," + |
| 207 | "screen INTEGER," + |
| 208 | "cellX INTEGER," + |
| 209 | "cellY INTEGER," + |
| 210 | "spanX INTEGER," + |
| 211 | "spanY INTEGER," + |
| 212 | "itemType INTEGER," + |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 213 | "appWidgetId INTEGER NOT NULL DEFAULT -1," + |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 214 | "isShortcut INTEGER," + |
| 215 | "iconType INTEGER," + |
| 216 | "iconPackage TEXT," + |
| 217 | "iconResource TEXT," + |
| 218 | "icon BLOB," + |
| 219 | "uri TEXT," + |
| 220 | "displayMode INTEGER" + |
| 221 | ");"); |
| 222 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 223 | // Database was just created, so wipe any previous widgets |
| 224 | if (mAppWidgetHost != null) { |
| 225 | mAppWidgetHost.deleteHost(); |
Jeffrey Sharkey | 2bbcae1 | 2009-03-31 14:37:57 -0700 | [diff] [blame^] | 226 | sendAppWidgetResetNotify(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | if (!convertDatabase(db)) { |
| 230 | // Populate favorites table with initial favorites |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 231 | loadFavorites(db); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
| 235 | private boolean convertDatabase(SQLiteDatabase db) { |
| 236 | if (LOGD) Log.d(LOG_TAG, "converting database from an older format, but not onUpgrade"); |
| 237 | boolean converted = false; |
| 238 | |
| 239 | final Uri uri = Uri.parse("content://" + Settings.AUTHORITY + |
| 240 | "/old_favorites?notify=true"); |
| 241 | final ContentResolver resolver = mContext.getContentResolver(); |
| 242 | Cursor cursor = null; |
| 243 | |
| 244 | try { |
| 245 | cursor = resolver.query(uri, null, null, null, null); |
| 246 | } catch (Exception e) { |
| 247 | // Ignore |
| 248 | } |
| 249 | |
| 250 | // We already have a favorites database in the old provider |
| 251 | if (cursor != null && cursor.getCount() > 0) { |
| 252 | try { |
| 253 | converted = copyFromCursor(db, cursor) > 0; |
| 254 | } finally { |
| 255 | cursor.close(); |
| 256 | } |
| 257 | |
| 258 | if (converted) { |
| 259 | resolver.delete(uri, null, null); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | if (converted) { |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 264 | // Convert widgets from this import into widgets |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 265 | if (LOGD) Log.d(LOG_TAG, "converted and now triggering widget upgrade"); |
| 266 | convertWidgets(db); |
| 267 | } |
| 268 | |
| 269 | return converted; |
| 270 | } |
| 271 | |
| 272 | private int copyFromCursor(SQLiteDatabase db, Cursor c) { |
| 273 | final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ID); |
| 274 | final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT); |
| 275 | final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE); |
| 276 | final int iconTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE); |
| 277 | final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON); |
| 278 | final int iconPackageIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE); |
| 279 | final int iconResourceIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE); |
| 280 | final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER); |
| 281 | final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE); |
| 282 | final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN); |
| 283 | final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX); |
| 284 | final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY); |
| 285 | final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI); |
| 286 | final int displayModeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE); |
| 287 | |
| 288 | ContentValues[] rows = new ContentValues[c.getCount()]; |
| 289 | int i = 0; |
| 290 | while (c.moveToNext()) { |
| 291 | ContentValues values = new ContentValues(c.getColumnCount()); |
| 292 | values.put(LauncherSettings.Favorites.ID, c.getLong(idIndex)); |
| 293 | values.put(LauncherSettings.Favorites.INTENT, c.getString(intentIndex)); |
| 294 | values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex)); |
| 295 | values.put(LauncherSettings.Favorites.ICON_TYPE, c.getInt(iconTypeIndex)); |
| 296 | values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex)); |
| 297 | values.put(LauncherSettings.Favorites.ICON_PACKAGE, c.getString(iconPackageIndex)); |
| 298 | values.put(LauncherSettings.Favorites.ICON_RESOURCE, c.getString(iconResourceIndex)); |
| 299 | values.put(LauncherSettings.Favorites.CONTAINER, c.getInt(containerIndex)); |
| 300 | values.put(LauncherSettings.Favorites.ITEM_TYPE, c.getInt(itemTypeIndex)); |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 301 | values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 302 | values.put(LauncherSettings.Favorites.SCREEN, c.getInt(screenIndex)); |
| 303 | values.put(LauncherSettings.Favorites.CELLX, c.getInt(cellXIndex)); |
| 304 | values.put(LauncherSettings.Favorites.CELLY, c.getInt(cellYIndex)); |
| 305 | values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex)); |
| 306 | values.put(LauncherSettings.Favorites.DISPLAY_MODE, c.getInt(displayModeIndex)); |
| 307 | rows[i++] = values; |
| 308 | } |
| 309 | |
| 310 | db.beginTransaction(); |
| 311 | int total = 0; |
| 312 | try { |
| 313 | int numValues = rows.length; |
| 314 | for (i = 0; i < numValues; i++) { |
| 315 | if (db.insert(TABLE_FAVORITES, null, rows[i]) < 0) { |
| 316 | return 0; |
| 317 | } else { |
| 318 | total++; |
| 319 | } |
| 320 | } |
| 321 | db.setTransactionSuccessful(); |
| 322 | } finally { |
| 323 | db.endTransaction(); |
| 324 | } |
| 325 | |
| 326 | return total; |
| 327 | } |
| 328 | |
| 329 | @Override |
| 330 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
| 331 | if (LOGD) Log.d(LOG_TAG, "onUpgrade triggered"); |
| 332 | |
| 333 | int version = oldVersion; |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 334 | if (version < 3) { |
| 335 | // upgrade 1,2 -> 3 added appWidgetId column |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 336 | db.beginTransaction(); |
| 337 | try { |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 338 | // Insert new column for holding appWidgetIds |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 339 | db.execSQL("ALTER TABLE favorites " + |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 340 | "ADD COLUMN appWidgetId INTEGER NOT NULL DEFAULT -1;"); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 341 | db.setTransactionSuccessful(); |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 342 | version = 3; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 343 | } catch (SQLException ex) { |
| 344 | // Old version remains, which means we wipe old data |
| 345 | Log.e(LOG_TAG, ex.getMessage(), ex); |
| 346 | } finally { |
| 347 | db.endTransaction(); |
| 348 | } |
| 349 | |
| 350 | // Convert existing widgets only if table upgrade was successful |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 351 | if (version == 3) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 352 | convertWidgets(db); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | if (version != DATABASE_VERSION) { |
| 357 | Log.w(LOG_TAG, "Destroying all old data."); |
| 358 | db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES); |
| 359 | onCreate(db); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | /** |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 364 | * Upgrade existing clock and photo frame widgets into their new widget |
| 365 | * equivalents. This method allocates appWidgetIds, and then hands off to |
| 366 | * LauncherAppWidgetBinder to finish the actual binding. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 367 | */ |
| 368 | private void convertWidgets(SQLiteDatabase db) { |
| 369 | final int[] bindSources = new int[] { |
| 370 | Favorites.ITEM_TYPE_WIDGET_CLOCK, |
| 371 | Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME, |
| 372 | }; |
| 373 | |
| 374 | final ArrayList<ComponentName> bindTargets = new ArrayList<ComponentName>(); |
| 375 | bindTargets.add(new ComponentName("com.android.alarmclock", |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 376 | "com.android.alarmclock.AnalogAppWidgetProvider")); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 377 | bindTargets.add(new ComponentName("com.android.camera", |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 378 | "com.android.camera.PhotoAppWidgetProvider")); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 379 | |
| 380 | final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE, bindSources); |
| 381 | |
| 382 | Cursor c = null; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 383 | boolean allocatedAppWidgets = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 384 | |
| 385 | db.beginTransaction(); |
| 386 | try { |
| 387 | // Select and iterate through each matching widget |
| 388 | c = db.query(TABLE_FAVORITES, new String[] { Favorites._ID }, |
| 389 | selectWhere, null, null, null, null); |
| 390 | |
| 391 | if (LOGD) Log.d(LOG_TAG, "found upgrade cursor count="+c.getCount()); |
| 392 | |
| 393 | final ContentValues values = new ContentValues(); |
| 394 | while (c != null && c.moveToNext()) { |
| 395 | long favoriteId = c.getLong(0); |
| 396 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 397 | // Allocate and update database with new appWidgetId |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 398 | try { |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 399 | int appWidgetId = mAppWidgetHost.allocateAppWidgetId(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 400 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 401 | if (LOGD) Log.d(LOG_TAG, "allocated appWidgetId="+appWidgetId+" for favoriteId="+favoriteId); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 402 | |
| 403 | values.clear(); |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 404 | values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 405 | |
| 406 | // Original widgets might not have valid spans when upgrading |
| 407 | values.put(LauncherSettings.Favorites.SPANX, 2); |
| 408 | values.put(LauncherSettings.Favorites.SPANY, 2); |
| 409 | |
| 410 | String updateWhere = Favorites._ID + "=" + favoriteId; |
| 411 | db.update(TABLE_FAVORITES, values, updateWhere, null); |
| 412 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 413 | allocatedAppWidgets = true; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 414 | } catch (RuntimeException ex) { |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 415 | Log.e(LOG_TAG, "Problem allocating appWidgetId", ex); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
| 419 | db.setTransactionSuccessful(); |
| 420 | } catch (SQLException ex) { |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 421 | Log.w(LOG_TAG, "Problem while allocating appWidgetIds for existing widgets", ex); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 422 | } finally { |
| 423 | db.endTransaction(); |
| 424 | if (c != null) { |
| 425 | c.close(); |
| 426 | } |
| 427 | } |
| 428 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 429 | // If any appWidgetIds allocated, then launch over to binder |
| 430 | if (allocatedAppWidgets) { |
| 431 | launchAppWidgetBinder(bindSources, bindTargets); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | |
| 435 | /** |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 436 | * Launch the widget binder that walks through the Launcher database, |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 437 | * binding any matching widgets to the corresponding targets. We can't |
| 438 | * bind ourselves because our parent process can't obtain the |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 439 | * BIND_APPWIDGET permission. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 440 | */ |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 441 | private void launchAppWidgetBinder(int[] bindSources, ArrayList<ComponentName> bindTargets) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 442 | final Intent intent = new Intent(); |
| 443 | intent.setComponent(new ComponentName("com.android.settings", |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 444 | "com.android.settings.LauncherAppWidgetBinder")); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 445 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 446 | |
| 447 | final Bundle extras = new Bundle(); |
| 448 | extras.putIntArray(EXTRA_BIND_SOURCES, bindSources); |
| 449 | extras.putParcelableArrayList(EXTRA_BIND_TARGETS, bindTargets); |
| 450 | intent.putExtras(extras); |
| 451 | |
| 452 | mContext.startActivity(intent); |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * Loads the default set of favorite packages from an xml file. |
| 457 | * |
| 458 | * @param db The database to write the values into |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 459 | */ |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 460 | private int loadFavorites(SQLiteDatabase db) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 461 | Intent intent = new Intent(Intent.ACTION_MAIN, null); |
| 462 | intent.addCategory(Intent.CATEGORY_LAUNCHER); |
| 463 | ContentValues values = new ContentValues(); |
| 464 | |
| 465 | PackageManager packageManager = mContext.getPackageManager(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 466 | int i = 0; |
| 467 | try { |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 468 | XmlResourceParser parser = mContext.getResources().getXml(R.xml.default_workspace); |
| 469 | AttributeSet attrs = Xml.asAttributeSet(parser); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 470 | XmlUtils.beginDocument(parser, TAG_FAVORITES); |
| 471 | |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 472 | final int depth = parser.getDepth(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 473 | |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 474 | int type; |
| 475 | while (((type = parser.next()) != XmlPullParser.END_TAG || |
| 476 | parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) { |
| 477 | |
| 478 | if (type != XmlPullParser.START_TAG) { |
| 479 | continue; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 480 | } |
| 481 | |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 482 | boolean added = false; |
| 483 | final String name = parser.getName(); |
| 484 | |
| 485 | TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.Favorite); |
| 486 | |
| 487 | values.clear(); |
| 488 | values.put(LauncherSettings.Favorites.CONTAINER, |
| 489 | LauncherSettings.Favorites.CONTAINER_DESKTOP); |
| 490 | values.put(LauncherSettings.Favorites.SCREEN, |
| 491 | a.getString(R.styleable.Favorite_screen)); |
| 492 | values.put(LauncherSettings.Favorites.CELLX, |
| 493 | a.getString(R.styleable.Favorite_x)); |
| 494 | values.put(LauncherSettings.Favorites.CELLY, |
| 495 | a.getString(R.styleable.Favorite_y)); |
| 496 | |
| 497 | if (TAG_FAVORITE.equals(name)) { |
| 498 | added = addShortcut(db, values, a, packageManager, intent); |
| 499 | } else if (TAG_SEARCH.equals(name)) { |
| 500 | added = addSearchWidget(db, values); |
| 501 | } else if (TAG_CLOCK.equals(name)) { |
| 502 | added = addClockWidget(db, values); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 503 | } |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 504 | |
| 505 | if (added) i++; |
| 506 | |
| 507 | a.recycle(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 508 | } |
| 509 | } catch (XmlPullParserException e) { |
| 510 | Log.w(LOG_TAG, "Got exception parsing favorites.", e); |
| 511 | } catch (IOException e) { |
| 512 | Log.w(LOG_TAG, "Got exception parsing favorites.", e); |
| 513 | } |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 514 | |
| 515 | return i; |
| 516 | } |
| 517 | |
| 518 | private boolean addShortcut(SQLiteDatabase db, ContentValues values, TypedArray a, |
| 519 | PackageManager packageManager, Intent intent) { |
| 520 | |
| 521 | ActivityInfo info; |
| 522 | String packageName = a.getString(R.styleable.Favorite_packageName); |
| 523 | String className = a.getString(R.styleable.Favorite_className); |
| 524 | try { |
| 525 | ComponentName cn = new ComponentName(packageName, className); |
| 526 | info = packageManager.getActivityInfo(cn, 0); |
| 527 | intent.setComponent(cn); |
Dianne Hackborn | bd2e54d | 2009-03-24 21:00:33 -0700 | [diff] [blame] | 528 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 529 | | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 530 | values.put(Favorites.INTENT, intent.toURI()); |
| 531 | values.put(Favorites.TITLE, info.loadLabel(packageManager).toString()); |
| 532 | values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPLICATION); |
| 533 | values.put(Favorites.SPANX, 1); |
| 534 | values.put(Favorites.SPANY, 1); |
| 535 | db.insert(TABLE_FAVORITES, null, values); |
| 536 | } catch (PackageManager.NameNotFoundException e) { |
| 537 | Log.w(LOG_TAG, "Unable to add favorite: " + packageName + |
| 538 | "/" + className, e); |
| 539 | return false; |
| 540 | } |
| 541 | return true; |
| 542 | } |
| 543 | |
| 544 | private boolean addSearchWidget(SQLiteDatabase db, ContentValues values) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 545 | // Add a search box |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 546 | values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_WIDGET_SEARCH); |
| 547 | values.put(Favorites.SPANX, 4); |
| 548 | values.put(Favorites.SPANY, 1); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 549 | db.insert(TABLE_FAVORITES, null, values); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 550 | |
| 551 | return true; |
| 552 | } |
| 553 | |
| 554 | private boolean addClockWidget(SQLiteDatabase db, ContentValues values) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 555 | final int[] bindSources = new int[] { |
| 556 | Favorites.ITEM_TYPE_WIDGET_CLOCK, |
| 557 | }; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 558 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 559 | final ArrayList<ComponentName> bindTargets = new ArrayList<ComponentName>(); |
| 560 | bindTargets.add(new ComponentName("com.android.alarmclock", |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 561 | "com.android.alarmclock.AnalogAppWidgetProvider")); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 562 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 563 | boolean allocatedAppWidgets = false; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 564 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 565 | // Try binding to an analog clock widget |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 566 | try { |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 567 | int appWidgetId = mAppWidgetHost.allocateAppWidgetId(); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 568 | |
| 569 | values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_WIDGET_CLOCK); |
| 570 | values.put(Favorites.SPANX, 2); |
| 571 | values.put(Favorites.SPANY, 2); |
| 572 | values.put(Favorites.APPWIDGET_ID, appWidgetId); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 573 | db.insert(TABLE_FAVORITES, null, values); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 574 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 575 | allocatedAppWidgets = true; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 576 | } catch (RuntimeException ex) { |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 577 | Log.e(LOG_TAG, "Problem allocating appWidgetId", ex); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 578 | } |
| 579 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 580 | // If any appWidgetIds allocated, then launch over to binder |
| 581 | if (allocatedAppWidgets) { |
| 582 | launchAppWidgetBinder(bindSources, bindTargets); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 583 | } |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 584 | |
| 585 | return allocatedAppWidgets; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | |
| 589 | /** |
| 590 | * Build a query string that will match any row where the column matches |
| 591 | * anything in the values list. |
| 592 | */ |
| 593 | static String buildOrWhereString(String column, int[] values) { |
| 594 | StringBuilder selectWhere = new StringBuilder(); |
| 595 | for (int i = values.length - 1; i >= 0; i--) { |
| 596 | selectWhere.append(column).append("=").append(values[i]); |
| 597 | if (i > 0) { |
| 598 | selectWhere.append(" OR "); |
| 599 | } |
| 600 | } |
| 601 | return selectWhere.toString(); |
| 602 | } |
| 603 | |
| 604 | static class SqlArguments { |
| 605 | public final String table; |
| 606 | public final String where; |
| 607 | public final String[] args; |
| 608 | |
| 609 | SqlArguments(Uri url, String where, String[] args) { |
| 610 | if (url.getPathSegments().size() == 1) { |
| 611 | this.table = url.getPathSegments().get(0); |
| 612 | this.where = where; |
| 613 | this.args = args; |
| 614 | } else if (url.getPathSegments().size() != 2) { |
| 615 | throw new IllegalArgumentException("Invalid URI: " + url); |
| 616 | } else if (!TextUtils.isEmpty(where)) { |
| 617 | throw new UnsupportedOperationException("WHERE clause not supported: " + url); |
| 618 | } else { |
| 619 | this.table = url.getPathSegments().get(0); |
| 620 | this.where = "_id=" + ContentUris.parseId(url); |
| 621 | this.args = null; |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | SqlArguments(Uri url) { |
| 626 | if (url.getPathSegments().size() == 1) { |
| 627 | table = url.getPathSegments().get(0); |
| 628 | where = null; |
| 629 | args = null; |
| 630 | } else { |
| 631 | throw new IllegalArgumentException("Invalid URI: " + url); |
| 632 | } |
| 633 | } |
| 634 | } |
| 635 | } |