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