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