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