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