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