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 | |
| 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 { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 165 | private static final String TAG_FAVORITES = "favorites"; |
| 166 | private static final String TAG_FAVORITE = "favorite"; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame^] | 167 | private static final String TAG_CLOCK = "clock"; |
| 168 | private static final String TAG_SEARCH = "search"; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 169 | |
| 170 | private final Context mContext; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 171 | private final AppWidgetHost mAppWidgetHost; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 172 | |
| 173 | DatabaseHelper(Context context) { |
| 174 | super(context, DATABASE_NAME, null, DATABASE_VERSION); |
| 175 | mContext = context; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 176 | mAppWidgetHost = new AppWidgetHost(context, Launcher.APPWIDGET_HOST_ID); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public void onCreate(SQLiteDatabase db) { |
| 181 | if (LOGD) Log.d(LOG_TAG, "creating new launcher database"); |
| 182 | |
| 183 | db.execSQL("CREATE TABLE favorites (" + |
| 184 | "_id INTEGER PRIMARY KEY," + |
| 185 | "title TEXT," + |
| 186 | "intent TEXT," + |
| 187 | "container INTEGER," + |
| 188 | "screen INTEGER," + |
| 189 | "cellX INTEGER," + |
| 190 | "cellY INTEGER," + |
| 191 | "spanX INTEGER," + |
| 192 | "spanY INTEGER," + |
| 193 | "itemType INTEGER," + |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 194 | "appWidgetId INTEGER NOT NULL DEFAULT -1," + |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 195 | "isShortcut INTEGER," + |
| 196 | "iconType INTEGER," + |
| 197 | "iconPackage TEXT," + |
| 198 | "iconResource TEXT," + |
| 199 | "icon BLOB," + |
| 200 | "uri TEXT," + |
| 201 | "displayMode INTEGER" + |
| 202 | ");"); |
| 203 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 204 | // Database was just created, so wipe any previous widgets |
| 205 | if (mAppWidgetHost != null) { |
| 206 | mAppWidgetHost.deleteHost(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | if (!convertDatabase(db)) { |
| 210 | // Populate favorites table with initial favorites |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame^] | 211 | loadFavorites(db); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
| 215 | private boolean convertDatabase(SQLiteDatabase db) { |
| 216 | if (LOGD) Log.d(LOG_TAG, "converting database from an older format, but not onUpgrade"); |
| 217 | boolean converted = false; |
| 218 | |
| 219 | final Uri uri = Uri.parse("content://" + Settings.AUTHORITY + |
| 220 | "/old_favorites?notify=true"); |
| 221 | final ContentResolver resolver = mContext.getContentResolver(); |
| 222 | Cursor cursor = null; |
| 223 | |
| 224 | try { |
| 225 | cursor = resolver.query(uri, null, null, null, null); |
| 226 | } catch (Exception e) { |
| 227 | // Ignore |
| 228 | } |
| 229 | |
| 230 | // We already have a favorites database in the old provider |
| 231 | if (cursor != null && cursor.getCount() > 0) { |
| 232 | try { |
| 233 | converted = copyFromCursor(db, cursor) > 0; |
| 234 | } finally { |
| 235 | cursor.close(); |
| 236 | } |
| 237 | |
| 238 | if (converted) { |
| 239 | resolver.delete(uri, null, null); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | if (converted) { |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 244 | // Convert widgets from this import into widgets |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 245 | if (LOGD) Log.d(LOG_TAG, "converted and now triggering widget upgrade"); |
| 246 | convertWidgets(db); |
| 247 | } |
| 248 | |
| 249 | return converted; |
| 250 | } |
| 251 | |
| 252 | private int copyFromCursor(SQLiteDatabase db, Cursor c) { |
| 253 | final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ID); |
| 254 | final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT); |
| 255 | final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE); |
| 256 | final int iconTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE); |
| 257 | final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON); |
| 258 | final int iconPackageIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE); |
| 259 | final int iconResourceIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE); |
| 260 | final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER); |
| 261 | final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE); |
| 262 | final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN); |
| 263 | final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX); |
| 264 | final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY); |
| 265 | final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI); |
| 266 | final int displayModeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE); |
| 267 | |
| 268 | ContentValues[] rows = new ContentValues[c.getCount()]; |
| 269 | int i = 0; |
| 270 | while (c.moveToNext()) { |
| 271 | ContentValues values = new ContentValues(c.getColumnCount()); |
| 272 | values.put(LauncherSettings.Favorites.ID, c.getLong(idIndex)); |
| 273 | values.put(LauncherSettings.Favorites.INTENT, c.getString(intentIndex)); |
| 274 | values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex)); |
| 275 | values.put(LauncherSettings.Favorites.ICON_TYPE, c.getInt(iconTypeIndex)); |
| 276 | values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex)); |
| 277 | values.put(LauncherSettings.Favorites.ICON_PACKAGE, c.getString(iconPackageIndex)); |
| 278 | values.put(LauncherSettings.Favorites.ICON_RESOURCE, c.getString(iconResourceIndex)); |
| 279 | values.put(LauncherSettings.Favorites.CONTAINER, c.getInt(containerIndex)); |
| 280 | values.put(LauncherSettings.Favorites.ITEM_TYPE, c.getInt(itemTypeIndex)); |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 281 | values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 282 | values.put(LauncherSettings.Favorites.SCREEN, c.getInt(screenIndex)); |
| 283 | values.put(LauncherSettings.Favorites.CELLX, c.getInt(cellXIndex)); |
| 284 | values.put(LauncherSettings.Favorites.CELLY, c.getInt(cellYIndex)); |
| 285 | values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex)); |
| 286 | values.put(LauncherSettings.Favorites.DISPLAY_MODE, c.getInt(displayModeIndex)); |
| 287 | rows[i++] = values; |
| 288 | } |
| 289 | |
| 290 | db.beginTransaction(); |
| 291 | int total = 0; |
| 292 | try { |
| 293 | int numValues = rows.length; |
| 294 | for (i = 0; i < numValues; i++) { |
| 295 | if (db.insert(TABLE_FAVORITES, null, rows[i]) < 0) { |
| 296 | return 0; |
| 297 | } else { |
| 298 | total++; |
| 299 | } |
| 300 | } |
| 301 | db.setTransactionSuccessful(); |
| 302 | } finally { |
| 303 | db.endTransaction(); |
| 304 | } |
| 305 | |
| 306 | return total; |
| 307 | } |
| 308 | |
| 309 | @Override |
| 310 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
| 311 | if (LOGD) Log.d(LOG_TAG, "onUpgrade triggered"); |
| 312 | |
| 313 | int version = oldVersion; |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 314 | if (version < 3) { |
| 315 | // upgrade 1,2 -> 3 added appWidgetId column |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 316 | db.beginTransaction(); |
| 317 | try { |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 318 | // Insert new column for holding appWidgetIds |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 319 | db.execSQL("ALTER TABLE favorites " + |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 320 | "ADD COLUMN appWidgetId INTEGER NOT NULL DEFAULT -1;"); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 321 | db.setTransactionSuccessful(); |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 322 | version = 3; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 323 | } catch (SQLException ex) { |
| 324 | // Old version remains, which means we wipe old data |
| 325 | Log.e(LOG_TAG, ex.getMessage(), ex); |
| 326 | } finally { |
| 327 | db.endTransaction(); |
| 328 | } |
| 329 | |
| 330 | // Convert existing widgets only if table upgrade was successful |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 331 | if (version == 3) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 332 | convertWidgets(db); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | if (version != DATABASE_VERSION) { |
| 337 | Log.w(LOG_TAG, "Destroying all old data."); |
| 338 | db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES); |
| 339 | onCreate(db); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | /** |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 344 | * Upgrade existing clock and photo frame widgets into their new widget |
| 345 | * equivalents. This method allocates appWidgetIds, and then hands off to |
| 346 | * LauncherAppWidgetBinder to finish the actual binding. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 347 | */ |
| 348 | private void convertWidgets(SQLiteDatabase db) { |
| 349 | final int[] bindSources = new int[] { |
| 350 | Favorites.ITEM_TYPE_WIDGET_CLOCK, |
| 351 | Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME, |
| 352 | }; |
| 353 | |
| 354 | final ArrayList<ComponentName> bindTargets = new ArrayList<ComponentName>(); |
| 355 | bindTargets.add(new ComponentName("com.android.alarmclock", |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 356 | "com.android.alarmclock.AnalogAppWidgetProvider")); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 357 | bindTargets.add(new ComponentName("com.android.camera", |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 358 | "com.android.camera.PhotoAppWidgetProvider")); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 359 | |
| 360 | final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE, bindSources); |
| 361 | |
| 362 | Cursor c = null; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 363 | boolean allocatedAppWidgets = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 364 | |
| 365 | db.beginTransaction(); |
| 366 | try { |
| 367 | // Select and iterate through each matching widget |
| 368 | c = db.query(TABLE_FAVORITES, new String[] { Favorites._ID }, |
| 369 | selectWhere, null, null, null, null); |
| 370 | |
| 371 | if (LOGD) Log.d(LOG_TAG, "found upgrade cursor count="+c.getCount()); |
| 372 | |
| 373 | final ContentValues values = new ContentValues(); |
| 374 | while (c != null && c.moveToNext()) { |
| 375 | long favoriteId = c.getLong(0); |
| 376 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 377 | // Allocate and update database with new appWidgetId |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 378 | try { |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 379 | int appWidgetId = mAppWidgetHost.allocateAppWidgetId(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 380 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 381 | 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] | 382 | |
| 383 | values.clear(); |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 384 | values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 385 | |
| 386 | // Original widgets might not have valid spans when upgrading |
| 387 | values.put(LauncherSettings.Favorites.SPANX, 2); |
| 388 | values.put(LauncherSettings.Favorites.SPANY, 2); |
| 389 | |
| 390 | String updateWhere = Favorites._ID + "=" + favoriteId; |
| 391 | db.update(TABLE_FAVORITES, values, updateWhere, null); |
| 392 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 393 | allocatedAppWidgets = true; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 394 | } catch (RuntimeException ex) { |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 395 | Log.e(LOG_TAG, "Problem allocating appWidgetId", ex); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | |
| 399 | db.setTransactionSuccessful(); |
| 400 | } catch (SQLException ex) { |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 401 | 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] | 402 | } finally { |
| 403 | db.endTransaction(); |
| 404 | if (c != null) { |
| 405 | c.close(); |
| 406 | } |
| 407 | } |
| 408 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 409 | // If any appWidgetIds allocated, then launch over to binder |
| 410 | if (allocatedAppWidgets) { |
| 411 | launchAppWidgetBinder(bindSources, bindTargets); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | |
| 415 | /** |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 416 | * 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] | 417 | * binding any matching widgets to the corresponding targets. We can't |
| 418 | * 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] | 419 | * BIND_APPWIDGET permission. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 420 | */ |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 421 | private void launchAppWidgetBinder(int[] bindSources, ArrayList<ComponentName> bindTargets) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 422 | final Intent intent = new Intent(); |
| 423 | intent.setComponent(new ComponentName("com.android.settings", |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 424 | "com.android.settings.LauncherAppWidgetBinder")); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 425 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 426 | |
| 427 | final Bundle extras = new Bundle(); |
| 428 | extras.putIntArray(EXTRA_BIND_SOURCES, bindSources); |
| 429 | extras.putParcelableArrayList(EXTRA_BIND_TARGETS, bindTargets); |
| 430 | intent.putExtras(extras); |
| 431 | |
| 432 | mContext.startActivity(intent); |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * Loads the default set of favorite packages from an xml file. |
| 437 | * |
| 438 | * @param db The database to write the values into |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 439 | */ |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame^] | 440 | private int loadFavorites(SQLiteDatabase db) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 441 | Intent intent = new Intent(Intent.ACTION_MAIN, null); |
| 442 | intent.addCategory(Intent.CATEGORY_LAUNCHER); |
| 443 | ContentValues values = new ContentValues(); |
| 444 | |
| 445 | PackageManager packageManager = mContext.getPackageManager(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 446 | int i = 0; |
| 447 | try { |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame^] | 448 | XmlResourceParser parser = mContext.getResources().getXml(R.xml.default_workspace); |
| 449 | AttributeSet attrs = Xml.asAttributeSet(parser); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 450 | XmlUtils.beginDocument(parser, TAG_FAVORITES); |
| 451 | |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame^] | 452 | final int depth = parser.getDepth(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 453 | |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame^] | 454 | int type; |
| 455 | while (((type = parser.next()) != XmlPullParser.END_TAG || |
| 456 | parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) { |
| 457 | |
| 458 | if (type != XmlPullParser.START_TAG) { |
| 459 | continue; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 460 | } |
| 461 | |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame^] | 462 | boolean added = false; |
| 463 | final String name = parser.getName(); |
| 464 | |
| 465 | TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.Favorite); |
| 466 | |
| 467 | values.clear(); |
| 468 | values.put(LauncherSettings.Favorites.CONTAINER, |
| 469 | LauncherSettings.Favorites.CONTAINER_DESKTOP); |
| 470 | values.put(LauncherSettings.Favorites.SCREEN, |
| 471 | a.getString(R.styleable.Favorite_screen)); |
| 472 | values.put(LauncherSettings.Favorites.CELLX, |
| 473 | a.getString(R.styleable.Favorite_x)); |
| 474 | values.put(LauncherSettings.Favorites.CELLY, |
| 475 | a.getString(R.styleable.Favorite_y)); |
| 476 | |
| 477 | if (TAG_FAVORITE.equals(name)) { |
| 478 | added = addShortcut(db, values, a, packageManager, intent); |
| 479 | } else if (TAG_SEARCH.equals(name)) { |
| 480 | added = addSearchWidget(db, values); |
| 481 | } else if (TAG_CLOCK.equals(name)) { |
| 482 | added = addClockWidget(db, values); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 483 | } |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame^] | 484 | |
| 485 | if (added) i++; |
| 486 | |
| 487 | a.recycle(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 488 | } |
| 489 | } catch (XmlPullParserException e) { |
| 490 | Log.w(LOG_TAG, "Got exception parsing favorites.", e); |
| 491 | } catch (IOException e) { |
| 492 | Log.w(LOG_TAG, "Got exception parsing favorites.", e); |
| 493 | } |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame^] | 494 | |
| 495 | return i; |
| 496 | } |
| 497 | |
| 498 | private boolean addShortcut(SQLiteDatabase db, ContentValues values, TypedArray a, |
| 499 | PackageManager packageManager, Intent intent) { |
| 500 | |
| 501 | ActivityInfo info; |
| 502 | String packageName = a.getString(R.styleable.Favorite_packageName); |
| 503 | String className = a.getString(R.styleable.Favorite_className); |
| 504 | try { |
| 505 | ComponentName cn = new ComponentName(packageName, className); |
| 506 | info = packageManager.getActivityInfo(cn, 0); |
| 507 | intent.setComponent(cn); |
| 508 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 509 | values.put(Favorites.INTENT, intent.toURI()); |
| 510 | values.put(Favorites.TITLE, info.loadLabel(packageManager).toString()); |
| 511 | values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPLICATION); |
| 512 | values.put(Favorites.SPANX, 1); |
| 513 | values.put(Favorites.SPANY, 1); |
| 514 | db.insert(TABLE_FAVORITES, null, values); |
| 515 | } catch (PackageManager.NameNotFoundException e) { |
| 516 | Log.w(LOG_TAG, "Unable to add favorite: " + packageName + |
| 517 | "/" + className, e); |
| 518 | return false; |
| 519 | } |
| 520 | return true; |
| 521 | } |
| 522 | |
| 523 | private boolean addSearchWidget(SQLiteDatabase db, ContentValues values) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 524 | // Add a search box |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame^] | 525 | values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_WIDGET_SEARCH); |
| 526 | values.put(Favorites.SPANX, 4); |
| 527 | values.put(Favorites.SPANY, 1); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 528 | db.insert(TABLE_FAVORITES, null, values); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame^] | 529 | |
| 530 | return true; |
| 531 | } |
| 532 | |
| 533 | private boolean addClockWidget(SQLiteDatabase db, ContentValues values) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 534 | final int[] bindSources = new int[] { |
| 535 | Favorites.ITEM_TYPE_WIDGET_CLOCK, |
| 536 | }; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame^] | 537 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 538 | final ArrayList<ComponentName> bindTargets = new ArrayList<ComponentName>(); |
| 539 | bindTargets.add(new ComponentName("com.android.alarmclock", |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 540 | "com.android.alarmclock.AnalogAppWidgetProvider")); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame^] | 541 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 542 | boolean allocatedAppWidgets = false; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame^] | 543 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 544 | // Try binding to an analog clock widget |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 545 | try { |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 546 | int appWidgetId = mAppWidgetHost.allocateAppWidgetId(); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame^] | 547 | |
| 548 | values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_WIDGET_CLOCK); |
| 549 | values.put(Favorites.SPANX, 2); |
| 550 | values.put(Favorites.SPANY, 2); |
| 551 | values.put(Favorites.APPWIDGET_ID, appWidgetId); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 552 | db.insert(TABLE_FAVORITES, null, values); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame^] | 553 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 554 | allocatedAppWidgets = true; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 555 | } catch (RuntimeException ex) { |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 556 | Log.e(LOG_TAG, "Problem allocating appWidgetId", ex); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 557 | } |
| 558 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 559 | // If any appWidgetIds allocated, then launch over to binder |
| 560 | if (allocatedAppWidgets) { |
| 561 | launchAppWidgetBinder(bindSources, bindTargets); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 562 | } |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame^] | 563 | |
| 564 | return allocatedAppWidgets; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | |
| 568 | /** |
| 569 | * Build a query string that will match any row where the column matches |
| 570 | * anything in the values list. |
| 571 | */ |
| 572 | static String buildOrWhereString(String column, int[] values) { |
| 573 | StringBuilder selectWhere = new StringBuilder(); |
| 574 | for (int i = values.length - 1; i >= 0; i--) { |
| 575 | selectWhere.append(column).append("=").append(values[i]); |
| 576 | if (i > 0) { |
| 577 | selectWhere.append(" OR "); |
| 578 | } |
| 579 | } |
| 580 | return selectWhere.toString(); |
| 581 | } |
| 582 | |
| 583 | static class SqlArguments { |
| 584 | public final String table; |
| 585 | public final String where; |
| 586 | public final String[] args; |
| 587 | |
| 588 | SqlArguments(Uri url, String where, String[] args) { |
| 589 | if (url.getPathSegments().size() == 1) { |
| 590 | this.table = url.getPathSegments().get(0); |
| 591 | this.where = where; |
| 592 | this.args = args; |
| 593 | } else if (url.getPathSegments().size() != 2) { |
| 594 | throw new IllegalArgumentException("Invalid URI: " + url); |
| 595 | } else if (!TextUtils.isEmpty(where)) { |
| 596 | throw new UnsupportedOperationException("WHERE clause not supported: " + url); |
| 597 | } else { |
| 598 | this.table = url.getPathSegments().get(0); |
| 599 | this.where = "_id=" + ContentUris.parseId(url); |
| 600 | this.args = null; |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | SqlArguments(Uri url) { |
| 605 | if (url.getPathSegments().size() == 1) { |
| 606 | table = url.getPathSegments().get(0); |
| 607 | where = null; |
| 608 | args = null; |
| 609 | } else { |
| 610 | throw new IllegalArgumentException("Invalid URI: " + url); |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | } |