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