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