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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
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.ComponentName; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 24 | import android.content.ContentProvider; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 25 | import android.content.ContentResolver; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 26 | import android.content.ContentUris; |
| 27 | import android.content.ContentValues; |
| 28 | import android.content.Context; |
| 29 | import android.content.Intent; |
Michael Jurka | b85f8a4 | 2012-04-25 15:48:32 -0700 | [diff] [blame] | 30 | import android.content.SharedPreferences; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 31 | import android.content.pm.ActivityInfo; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 32 | import android.content.pm.PackageManager; |
| 33 | import android.content.res.Resources; |
| 34 | import android.content.res.TypedArray; |
| 35 | import android.content.res.XmlResourceParser; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 36 | import android.database.Cursor; |
| 37 | import android.database.SQLException; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 38 | import android.database.sqlite.SQLiteDatabase; |
| 39 | import android.database.sqlite.SQLiteOpenHelper; |
| 40 | import android.database.sqlite.SQLiteQueryBuilder; |
| 41 | import android.database.sqlite.SQLiteStatement; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 42 | import android.graphics.Bitmap; |
| 43 | import android.graphics.BitmapFactory; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 44 | import android.net.Uri; |
Winson Chung | b3302ae | 2012-05-01 10:19:14 -0700 | [diff] [blame] | 45 | import android.os.Bundle; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 46 | import android.provider.Settings; |
| 47 | import android.text.TextUtils; |
| 48 | import android.util.AttributeSet; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 49 | import android.util.Log; |
| 50 | import android.util.Xml; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 51 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 52 | import com.android.launcher3.R; |
| 53 | import com.android.launcher3.LauncherSettings.Favorites; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 54 | |
| 55 | import org.xmlpull.v1.XmlPullParser; |
| 56 | import org.xmlpull.v1.XmlPullParserException; |
| 57 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 58 | import java.io.IOException; |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 59 | import java.net.URISyntaxException; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 60 | import java.util.ArrayList; |
Bjorn Bringert | cd8fec0 | 2010-01-14 13:26:43 +0000 | [diff] [blame] | 61 | import java.util.List; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 62 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 63 | public class LauncherProvider extends ContentProvider { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 64 | private static final String TAG = "Launcher.LauncherProvider"; |
| 65 | private static final boolean LOGD = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 66 | |
| 67 | private static final String DATABASE_NAME = "launcher.db"; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 68 | |
Daniel Lehmann | d02402c | 2012-05-14 18:30:53 -0700 | [diff] [blame] | 69 | private static final int DATABASE_VERSION = 12; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 70 | |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 71 | static final String OLD_AUTHORITY = "com.android.launcher2.settings"; |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 72 | static final String AUTHORITY = "com.android.launcher3.settings"; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 73 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 74 | static final String TABLE_FAVORITES = "favorites"; |
| 75 | static final String PARAMETER_NOTIFY = "notify"; |
Michael Jurka | b85f8a4 | 2012-04-25 15:48:32 -0700 | [diff] [blame] | 76 | static final String DB_CREATED_BUT_DEFAULT_WORKSPACE_NOT_LOADED = |
| 77 | "DB_CREATED_BUT_DEFAULT_WORKSPACE_NOT_LOADED"; |
Michael Jurka | 45355c4 | 2012-10-08 13:21:35 +0200 | [diff] [blame] | 78 | static final String DEFAULT_WORKSPACE_RESOURCE_ID = |
| 79 | "DEFAULT_WORKSPACE_RESOURCE_ID"; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 80 | |
Winson Chung | b3302ae | 2012-05-01 10:19:14 -0700 | [diff] [blame] | 81 | private static final String ACTION_APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE = |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 82 | "com.android.launcher.action.APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE"; |
Winson Chung | b3302ae | 2012-05-01 10:19:14 -0700 | [diff] [blame] | 83 | |
Jeffrey Sharkey | 2bbcae1 | 2009-03-31 14:37:57 -0700 | [diff] [blame] | 84 | /** |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 85 | * {@link Uri} triggered at any registered {@link android.database.ContentObserver} when |
Jeffrey Sharkey | 2bbcae1 | 2009-03-31 14:37:57 -0700 | [diff] [blame] | 86 | * {@link AppWidgetHost#deleteHost()} is called during database creation. |
| 87 | * Use this to recall {@link AppWidgetHost#startListening()} if needed. |
| 88 | */ |
| 89 | static final Uri CONTENT_APPWIDGET_RESET_URI = |
| 90 | Uri.parse("content://" + AUTHORITY + "/appWidgetReset"); |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 91 | |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 92 | private DatabaseHelper mOpenHelper; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 93 | |
| 94 | @Override |
| 95 | public boolean onCreate() { |
| 96 | mOpenHelper = new DatabaseHelper(getContext()); |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 97 | ((LauncherApplication) getContext()).setLauncherProvider(this); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 98 | return true; |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public String getType(Uri uri) { |
| 103 | SqlArguments args = new SqlArguments(uri, null, null); |
| 104 | if (TextUtils.isEmpty(args.where)) { |
| 105 | return "vnd.android.cursor.dir/" + args.table; |
| 106 | } else { |
| 107 | return "vnd.android.cursor.item/" + args.table; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public Cursor query(Uri uri, String[] projection, String selection, |
| 113 | String[] selectionArgs, String sortOrder) { |
| 114 | |
| 115 | SqlArguments args = new SqlArguments(uri, selection, selectionArgs); |
| 116 | SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); |
| 117 | qb.setTables(args.table); |
| 118 | |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 119 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 120 | Cursor result = qb.query(db, projection, args.where, args.args, null, null, sortOrder); |
| 121 | result.setNotificationUri(getContext().getContentResolver(), uri); |
| 122 | |
| 123 | return result; |
| 124 | } |
| 125 | |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 126 | private static long dbInsertAndCheck(DatabaseHelper helper, |
| 127 | SQLiteDatabase db, String table, String nullColumnHack, ContentValues values) { |
| 128 | if (!values.containsKey(LauncherSettings.Favorites._ID)) { |
| 129 | throw new RuntimeException("Error: attempting to add item without specifying an id"); |
| 130 | } |
| 131 | return db.insert(table, nullColumnHack, values); |
| 132 | } |
| 133 | |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 134 | private static void deleteId(SQLiteDatabase db, long id) { |
| 135 | Uri uri = LauncherSettings.Favorites.getContentUri(id, false); |
| 136 | SqlArguments args = new SqlArguments(uri, null, null); |
| 137 | db.delete(args.table, args.where, args.args); |
| 138 | } |
| 139 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 140 | @Override |
| 141 | public Uri insert(Uri uri, ContentValues initialValues) { |
| 142 | SqlArguments args = new SqlArguments(uri); |
| 143 | |
| 144 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 145 | final long rowId = dbInsertAndCheck(mOpenHelper, db, args.table, null, initialValues); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 146 | if (rowId <= 0) return null; |
| 147 | |
| 148 | uri = ContentUris.withAppendedId(uri, rowId); |
| 149 | sendNotify(uri); |
| 150 | |
| 151 | return uri; |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public int bulkInsert(Uri uri, ContentValues[] values) { |
| 156 | SqlArguments args = new SqlArguments(uri); |
| 157 | |
| 158 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 159 | db.beginTransaction(); |
| 160 | try { |
| 161 | int numValues = values.length; |
| 162 | for (int i = 0; i < numValues; i++) { |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 163 | if (dbInsertAndCheck(mOpenHelper, db, args.table, null, values[i]) < 0) { |
| 164 | return 0; |
| 165 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 166 | } |
| 167 | db.setTransactionSuccessful(); |
| 168 | } finally { |
| 169 | db.endTransaction(); |
| 170 | } |
| 171 | |
| 172 | sendNotify(uri); |
| 173 | return values.length; |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public int delete(Uri uri, String selection, String[] selectionArgs) { |
| 178 | SqlArguments args = new SqlArguments(uri, selection, selectionArgs); |
| 179 | |
| 180 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 181 | int count = db.delete(args.table, args.where, args.args); |
| 182 | if (count > 0) sendNotify(uri); |
| 183 | |
| 184 | return count; |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { |
| 189 | SqlArguments args = new SqlArguments(uri, selection, selectionArgs); |
| 190 | |
| 191 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 192 | int count = db.update(args.table, values, args.where, args.args); |
| 193 | if (count > 0) sendNotify(uri); |
| 194 | |
| 195 | return count; |
| 196 | } |
| 197 | |
| 198 | private void sendNotify(Uri uri) { |
| 199 | String notify = uri.getQueryParameter(PARAMETER_NOTIFY); |
| 200 | if (notify == null || "true".equals(notify)) { |
| 201 | getContext().getContentResolver().notifyChange(uri, null); |
| 202 | } |
| 203 | } |
| 204 | |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 205 | public long generateNewId() { |
| 206 | return mOpenHelper.generateNewId(); |
| 207 | } |
| 208 | |
Brian Muramatsu | 5524b49 | 2012-10-02 16:55:54 -0700 | [diff] [blame] | 209 | /** |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 210 | * @param Should we load the old db for upgrade? first run only. |
| 211 | */ |
| 212 | synchronized public boolean shouldLoadOldDb() { |
| 213 | String spKey = LauncherApplication.getSharedPreferencesKey(); |
| 214 | SharedPreferences sp = getContext().getSharedPreferences(spKey, Context.MODE_PRIVATE); |
| 215 | |
| 216 | boolean loadOldDb = false; |
| 217 | if (sp.getBoolean(DB_CREATED_BUT_DEFAULT_WORKSPACE_NOT_LOADED, false)) { |
| 218 | |
| 219 | SharedPreferences.Editor editor = sp.edit(); |
| 220 | editor.remove(DB_CREATED_BUT_DEFAULT_WORKSPACE_NOT_LOADED); |
| 221 | editor.commit(); |
| 222 | loadOldDb = true; |
| 223 | } |
| 224 | return loadOldDb; |
| 225 | } |
| 226 | |
| 227 | /** |
Brian Muramatsu | 5524b49 | 2012-10-02 16:55:54 -0700 | [diff] [blame] | 228 | * @param workspaceResId that can be 0 to use default or non-zero for specific resource |
| 229 | */ |
Michael Jurka | 45355c4 | 2012-10-08 13:21:35 +0200 | [diff] [blame] | 230 | synchronized public void loadDefaultFavoritesIfNecessary(int origWorkspaceResId) { |
Michael Jurka | b85f8a4 | 2012-04-25 15:48:32 -0700 | [diff] [blame] | 231 | String spKey = LauncherApplication.getSharedPreferencesKey(); |
| 232 | SharedPreferences sp = getContext().getSharedPreferences(spKey, Context.MODE_PRIVATE); |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 233 | |
Michael Jurka | b85f8a4 | 2012-04-25 15:48:32 -0700 | [diff] [blame] | 234 | if (sp.getBoolean(DB_CREATED_BUT_DEFAULT_WORKSPACE_NOT_LOADED, false)) { |
Michael Jurka | 45355c4 | 2012-10-08 13:21:35 +0200 | [diff] [blame] | 235 | int workspaceResId = origWorkspaceResId; |
| 236 | |
Brian Muramatsu | 5524b49 | 2012-10-02 16:55:54 -0700 | [diff] [blame] | 237 | // Use default workspace resource if none provided |
| 238 | if (workspaceResId == 0) { |
Michael Jurka | 45355c4 | 2012-10-08 13:21:35 +0200 | [diff] [blame] | 239 | workspaceResId = sp.getInt(DEFAULT_WORKSPACE_RESOURCE_ID, R.xml.default_workspace); |
Brian Muramatsu | 5524b49 | 2012-10-02 16:55:54 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Michael Jurka | b85f8a4 | 2012-04-25 15:48:32 -0700 | [diff] [blame] | 242 | // Populate favorites table with initial favorites |
| 243 | SharedPreferences.Editor editor = sp.edit(); |
| 244 | editor.remove(DB_CREATED_BUT_DEFAULT_WORKSPACE_NOT_LOADED); |
Michael Jurka | 45355c4 | 2012-10-08 13:21:35 +0200 | [diff] [blame] | 245 | if (origWorkspaceResId != 0) { |
| 246 | editor.putInt(DEFAULT_WORKSPACE_RESOURCE_ID, origWorkspaceResId); |
| 247 | } |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 248 | |
Brian Muramatsu | 5524b49 | 2012-10-02 16:55:54 -0700 | [diff] [blame] | 249 | mOpenHelper.loadFavorites(mOpenHelper.getWritableDatabase(), workspaceResId); |
Michael Jurka | b85f8a4 | 2012-04-25 15:48:32 -0700 | [diff] [blame] | 250 | editor.commit(); |
| 251 | } |
| 252 | } |
| 253 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 254 | private static class DatabaseHelper extends SQLiteOpenHelper { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 255 | private static final String TAG_FAVORITES = "favorites"; |
| 256 | private static final String TAG_FAVORITE = "favorite"; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 257 | private static final String TAG_CLOCK = "clock"; |
| 258 | private static final String TAG_SEARCH = "search"; |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 259 | private static final String TAG_APPWIDGET = "appwidget"; |
| 260 | private static final String TAG_SHORTCUT = "shortcut"; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 261 | private static final String TAG_FOLDER = "folder"; |
Winson Chung | b3302ae | 2012-05-01 10:19:14 -0700 | [diff] [blame] | 262 | private static final String TAG_EXTRA = "extra"; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 263 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 264 | private final Context mContext; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 265 | private final AppWidgetHost mAppWidgetHost; |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 266 | private long mMaxId = -1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 267 | |
| 268 | DatabaseHelper(Context context) { |
| 269 | super(context, DATABASE_NAME, null, DATABASE_VERSION); |
| 270 | mContext = context; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 271 | mAppWidgetHost = new AppWidgetHost(context, Launcher.APPWIDGET_HOST_ID); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 272 | |
| 273 | // In the case where neither onCreate nor onUpgrade gets called, we read the maxId from |
| 274 | // the DB here |
| 275 | if (mMaxId == -1) { |
| 276 | mMaxId = initializeMaxId(getWritableDatabase()); |
| 277 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 278 | } |
| 279 | |
Jeffrey Sharkey | 2bbcae1 | 2009-03-31 14:37:57 -0700 | [diff] [blame] | 280 | /** |
| 281 | * Send notification that we've deleted the {@link AppWidgetHost}, |
| 282 | * probably as part of the initial database creation. The receiver may |
| 283 | * want to re-call {@link AppWidgetHost#startListening()} to ensure |
| 284 | * callbacks are correctly set. |
| 285 | */ |
| 286 | private void sendAppWidgetResetNotify() { |
| 287 | final ContentResolver resolver = mContext.getContentResolver(); |
| 288 | resolver.notifyChange(CONTENT_APPWIDGET_RESET_URI, null); |
| 289 | } |
| 290 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 291 | @Override |
| 292 | public void onCreate(SQLiteDatabase db) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 293 | if (LOGD) Log.d(TAG, "creating new launcher database"); |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 294 | |
| 295 | mMaxId = 1; |
| 296 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 297 | db.execSQL("CREATE TABLE favorites (" + |
| 298 | "_id INTEGER PRIMARY KEY," + |
| 299 | "title TEXT," + |
| 300 | "intent TEXT," + |
| 301 | "container INTEGER," + |
| 302 | "screen INTEGER," + |
| 303 | "cellX INTEGER," + |
| 304 | "cellY INTEGER," + |
| 305 | "spanX INTEGER," + |
| 306 | "spanY INTEGER," + |
| 307 | "itemType INTEGER," + |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 308 | "appWidgetId INTEGER NOT NULL DEFAULT -1," + |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 309 | "isShortcut INTEGER," + |
| 310 | "iconType INTEGER," + |
| 311 | "iconPackage TEXT," + |
| 312 | "iconResource TEXT," + |
| 313 | "icon BLOB," + |
| 314 | "uri TEXT," + |
| 315 | "displayMode INTEGER" + |
| 316 | ");"); |
| 317 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 318 | // Database was just created, so wipe any previous widgets |
| 319 | if (mAppWidgetHost != null) { |
| 320 | mAppWidgetHost.deleteHost(); |
Jeffrey Sharkey | 2bbcae1 | 2009-03-31 14:37:57 -0700 | [diff] [blame] | 321 | sendAppWidgetResetNotify(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 322 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 323 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 324 | if (!convertDatabase(db)) { |
Michael Jurka | b85f8a4 | 2012-04-25 15:48:32 -0700 | [diff] [blame] | 325 | // Set a shared pref so that we know we need to load the default workspace later |
| 326 | setFlagToLoadDefaultWorkspaceLater(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
Michael Jurka | b85f8a4 | 2012-04-25 15:48:32 -0700 | [diff] [blame] | 330 | private void setFlagToLoadDefaultWorkspaceLater() { |
| 331 | String spKey = LauncherApplication.getSharedPreferencesKey(); |
| 332 | SharedPreferences sp = mContext.getSharedPreferences(spKey, Context.MODE_PRIVATE); |
| 333 | SharedPreferences.Editor editor = sp.edit(); |
| 334 | editor.putBoolean(DB_CREATED_BUT_DEFAULT_WORKSPACE_NOT_LOADED, true); |
| 335 | editor.commit(); |
| 336 | } |
| 337 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 338 | private boolean convertDatabase(SQLiteDatabase db) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 339 | 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] | 340 | boolean converted = false; |
| 341 | |
| 342 | final Uri uri = Uri.parse("content://" + Settings.AUTHORITY + |
| 343 | "/old_favorites?notify=true"); |
| 344 | final ContentResolver resolver = mContext.getContentResolver(); |
| 345 | Cursor cursor = null; |
| 346 | |
| 347 | try { |
| 348 | cursor = resolver.query(uri, null, null, null, null); |
| 349 | } catch (Exception e) { |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 350 | // Ignore |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | // We already have a favorites database in the old provider |
| 354 | if (cursor != null && cursor.getCount() > 0) { |
| 355 | try { |
| 356 | converted = copyFromCursor(db, cursor) > 0; |
| 357 | } finally { |
| 358 | cursor.close(); |
| 359 | } |
| 360 | |
| 361 | if (converted) { |
| 362 | resolver.delete(uri, null, null); |
| 363 | } |
| 364 | } |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 365 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 366 | if (converted) { |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 367 | // Convert widgets from this import into widgets |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 368 | 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] | 369 | convertWidgets(db); |
| 370 | } |
| 371 | |
| 372 | return converted; |
| 373 | } |
| 374 | |
| 375 | private int copyFromCursor(SQLiteDatabase db, Cursor c) { |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 376 | final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 377 | final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT); |
| 378 | final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE); |
| 379 | final int iconTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE); |
| 380 | final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON); |
| 381 | final int iconPackageIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE); |
| 382 | final int iconResourceIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE); |
| 383 | final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER); |
| 384 | final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE); |
| 385 | final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN); |
| 386 | final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX); |
| 387 | final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY); |
| 388 | final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI); |
| 389 | final int displayModeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE); |
| 390 | |
| 391 | ContentValues[] rows = new ContentValues[c.getCount()]; |
| 392 | int i = 0; |
| 393 | while (c.moveToNext()) { |
| 394 | ContentValues values = new ContentValues(c.getColumnCount()); |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 395 | values.put(LauncherSettings.Favorites._ID, c.getLong(idIndex)); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 396 | values.put(LauncherSettings.Favorites.INTENT, c.getString(intentIndex)); |
| 397 | values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex)); |
| 398 | values.put(LauncherSettings.Favorites.ICON_TYPE, c.getInt(iconTypeIndex)); |
| 399 | values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex)); |
| 400 | values.put(LauncherSettings.Favorites.ICON_PACKAGE, c.getString(iconPackageIndex)); |
| 401 | values.put(LauncherSettings.Favorites.ICON_RESOURCE, c.getString(iconResourceIndex)); |
| 402 | values.put(LauncherSettings.Favorites.CONTAINER, c.getInt(containerIndex)); |
| 403 | values.put(LauncherSettings.Favorites.ITEM_TYPE, c.getInt(itemTypeIndex)); |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 404 | values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 405 | values.put(LauncherSettings.Favorites.SCREEN, c.getInt(screenIndex)); |
| 406 | values.put(LauncherSettings.Favorites.CELLX, c.getInt(cellXIndex)); |
| 407 | values.put(LauncherSettings.Favorites.CELLY, c.getInt(cellYIndex)); |
| 408 | values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex)); |
| 409 | values.put(LauncherSettings.Favorites.DISPLAY_MODE, c.getInt(displayModeIndex)); |
| 410 | rows[i++] = values; |
| 411 | } |
| 412 | |
| 413 | db.beginTransaction(); |
| 414 | int total = 0; |
| 415 | try { |
| 416 | int numValues = rows.length; |
| 417 | for (i = 0; i < numValues; i++) { |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 418 | if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, rows[i]) < 0) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 419 | return 0; |
| 420 | } else { |
| 421 | total++; |
| 422 | } |
| 423 | } |
| 424 | db.setTransactionSuccessful(); |
| 425 | } finally { |
| 426 | db.endTransaction(); |
| 427 | } |
| 428 | |
| 429 | return total; |
| 430 | } |
| 431 | |
| 432 | @Override |
| 433 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 434 | if (LOGD) Log.d(TAG, "onUpgrade triggered"); |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 435 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 436 | int version = oldVersion; |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 437 | if (version < 3) { |
| 438 | // upgrade 1,2 -> 3 added appWidgetId column |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 439 | db.beginTransaction(); |
| 440 | try { |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 441 | // Insert new column for holding appWidgetIds |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 442 | db.execSQL("ALTER TABLE favorites " + |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 443 | "ADD COLUMN appWidgetId INTEGER NOT NULL DEFAULT -1;"); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 444 | db.setTransactionSuccessful(); |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 445 | version = 3; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 446 | } catch (SQLException ex) { |
| 447 | // Old version remains, which means we wipe old data |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 448 | Log.e(TAG, ex.getMessage(), ex); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 449 | } finally { |
| 450 | db.endTransaction(); |
| 451 | } |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 452 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 453 | // Convert existing widgets only if table upgrade was successful |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 454 | if (version == 3) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 455 | convertWidgets(db); |
| 456 | } |
| 457 | } |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 458 | |
| 459 | if (version < 4) { |
Romain Guy | 7eb9e5e | 2009-12-02 20:10:07 -0800 | [diff] [blame] | 460 | version = 4; |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 461 | } |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 462 | |
Romain Guy | 509cd6a | 2010-03-23 15:10:56 -0700 | [diff] [blame] | 463 | // Where's version 5? |
| 464 | // - Donut and sholes on 2.0 shipped with version 4 of launcher1. |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 465 | // - Passion shipped on 2.1 with version 6 of launcher3 |
Romain Guy | 509cd6a | 2010-03-23 15:10:56 -0700 | [diff] [blame] | 466 | // - Sholes shipped on 2.1r1 (aka Mr. 3) with version 5 of launcher 1 |
| 467 | // but version 5 on there was the updateContactsShortcuts change |
| 468 | // which was version 6 in launcher 2 (first shipped on passion 2.1r1). |
| 469 | // The updateContactsShortcuts change is idempotent, so running it twice |
| 470 | // is okay so we'll do that when upgrading the devices that shipped with it. |
| 471 | if (version < 6) { |
Mike Cleron | 3a2b3f2 | 2009-11-05 17:17:42 -0800 | [diff] [blame] | 472 | // We went from 3 to 5 screens. Move everything 1 to the right |
| 473 | db.beginTransaction(); |
| 474 | try { |
| 475 | db.execSQL("UPDATE favorites SET screen=(screen + 1);"); |
| 476 | db.setTransactionSuccessful(); |
Mike Cleron | 3a2b3f2 | 2009-11-05 17:17:42 -0800 | [diff] [blame] | 477 | } catch (SQLException ex) { |
| 478 | // Old version remains, which means we wipe old data |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 479 | Log.e(TAG, ex.getMessage(), ex); |
Mike Cleron | 3a2b3f2 | 2009-11-05 17:17:42 -0800 | [diff] [blame] | 480 | } finally { |
| 481 | db.endTransaction(); |
| 482 | } |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 483 | |
Romain Guy | 509cd6a | 2010-03-23 15:10:56 -0700 | [diff] [blame] | 484 | // We added the fast track. |
Romain Guy | 7eb9e5e | 2009-12-02 20:10:07 -0800 | [diff] [blame] | 485 | if (updateContactsShortcuts(db)) { |
| 486 | version = 6; |
| 487 | } |
| 488 | } |
Bjorn Bringert | 7984c94 | 2009-12-09 15:38:25 +0000 | [diff] [blame] | 489 | |
| 490 | if (version < 7) { |
| 491 | // Version 7 gets rid of the special search widget. |
| 492 | convertWidgets(db); |
| 493 | version = 7; |
| 494 | } |
| 495 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 496 | if (version < 8) { |
| 497 | // Version 8 (froyo) has the icons all normalized. This should |
| 498 | // already be the case in practice, but we now rely on it and don't |
| 499 | // resample the images each time. |
| 500 | normalizeIcons(db); |
| 501 | version = 8; |
| 502 | } |
| 503 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 504 | if (version < 9) { |
| 505 | // The max id is not yet set at this point (onUpgrade is triggered in the ctor |
| 506 | // before it gets a change to get set, so we need to read it here when we use it) |
| 507 | if (mMaxId == -1) { |
| 508 | mMaxId = initializeMaxId(db); |
| 509 | } |
| 510 | |
| 511 | // Add default hotseat icons |
Winson Chung | 6d09268 | 2011-11-16 18:43:26 -0800 | [diff] [blame] | 512 | loadFavorites(db, R.xml.update_workspace); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 513 | version = 9; |
| 514 | } |
| 515 | |
Daniel Lehmann | d02402c | 2012-05-14 18:30:53 -0700 | [diff] [blame] | 516 | // We bumped the version three time during JB, once to update the launch flags, once to |
| 517 | // update the override for the default launch animation and once to set the mimetype |
| 518 | // to improve startup performance |
| 519 | if (version < 12) { |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 520 | // Contact shortcuts need a different set of flags to be launched now |
| 521 | // The updateContactsShortcuts change is idempotent, so we can keep using it like |
| 522 | // back in the Donut days |
| 523 | updateContactsShortcuts(db); |
Daniel Lehmann | d02402c | 2012-05-14 18:30:53 -0700 | [diff] [blame] | 524 | version = 12; |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 525 | } |
| 526 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 527 | if (version != DATABASE_VERSION) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 528 | Log.w(TAG, "Destroying all old data."); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 529 | db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES); |
| 530 | onCreate(db); |
| 531 | } |
| 532 | } |
Romain Guy | 7eb9e5e | 2009-12-02 20:10:07 -0800 | [diff] [blame] | 533 | |
| 534 | private boolean updateContactsShortcuts(SQLiteDatabase db) { |
Romain Guy | 7eb9e5e | 2009-12-02 20:10:07 -0800 | [diff] [blame] | 535 | final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE, |
| 536 | new int[] { Favorites.ITEM_TYPE_SHORTCUT }); |
| 537 | |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 538 | Cursor c = null; |
| 539 | final String actionQuickContact = "com.android.contacts.action.QUICK_CONTACT"; |
Romain Guy | 7eb9e5e | 2009-12-02 20:10:07 -0800 | [diff] [blame] | 540 | db.beginTransaction(); |
| 541 | try { |
| 542 | // Select and iterate through each matching widget |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 543 | c = db.query(TABLE_FAVORITES, |
| 544 | new String[] { Favorites._ID, Favorites.INTENT }, |
Romain Guy | 7eb9e5e | 2009-12-02 20:10:07 -0800 | [diff] [blame] | 545 | selectWhere, null, null, null, null); |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 546 | if (c == null) return false; |
| 547 | |
Romain Guy | 7eb9e5e | 2009-12-02 20:10:07 -0800 | [diff] [blame] | 548 | if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount()); |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 549 | |
Romain Guy | 7eb9e5e | 2009-12-02 20:10:07 -0800 | [diff] [blame] | 550 | final int idIndex = c.getColumnIndex(Favorites._ID); |
| 551 | final int intentIndex = c.getColumnIndex(Favorites.INTENT); |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 552 | |
| 553 | while (c.moveToNext()) { |
Romain Guy | 7eb9e5e | 2009-12-02 20:10:07 -0800 | [diff] [blame] | 554 | long favoriteId = c.getLong(idIndex); |
| 555 | final String intentUri = c.getString(intentIndex); |
| 556 | if (intentUri != null) { |
| 557 | try { |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 558 | final Intent intent = Intent.parseUri(intentUri, 0); |
Romain Guy | 7eb9e5e | 2009-12-02 20:10:07 -0800 | [diff] [blame] | 559 | android.util.Log.d("Home", intent.toString()); |
| 560 | final Uri uri = intent.getData(); |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 561 | if (uri != null) { |
| 562 | final String data = uri.toString(); |
| 563 | if ((Intent.ACTION_VIEW.equals(intent.getAction()) || |
| 564 | actionQuickContact.equals(intent.getAction())) && |
| 565 | (data.startsWith("content://contacts/people/") || |
| 566 | data.startsWith("content://com.android.contacts/" + |
| 567 | "contacts/lookup/"))) { |
Romain Guy | 7eb9e5e | 2009-12-02 20:10:07 -0800 | [diff] [blame] | 568 | |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 569 | final Intent newIntent = new Intent(actionQuickContact); |
| 570 | // When starting from the launcher, start in a new, cleared task |
| 571 | // CLEAR_WHEN_TASK_RESET cannot reset the root of a task, so we |
| 572 | // clear the whole thing preemptively here since |
| 573 | // QuickContactActivity will finish itself when launching other |
| 574 | // detail activities. |
| 575 | newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | |
| 576 | Intent.FLAG_ACTIVITY_CLEAR_TASK); |
Winson Chung | 2672ff9 | 2012-05-04 16:22:30 -0700 | [diff] [blame] | 577 | newIntent.putExtra( |
| 578 | Launcher.INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION, true); |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 579 | newIntent.setData(uri); |
Daniel Lehmann | d02402c | 2012-05-14 18:30:53 -0700 | [diff] [blame] | 580 | // Determine the type and also put that in the shortcut |
| 581 | // (that can speed up launch a bit) |
| 582 | newIntent.setDataAndType(uri, newIntent.resolveType(mContext)); |
Romain Guy | 7eb9e5e | 2009-12-02 20:10:07 -0800 | [diff] [blame] | 583 | |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 584 | final ContentValues values = new ContentValues(); |
| 585 | values.put(LauncherSettings.Favorites.INTENT, |
| 586 | newIntent.toUri(0)); |
| 587 | |
| 588 | String updateWhere = Favorites._ID + "=" + favoriteId; |
| 589 | db.update(TABLE_FAVORITES, values, updateWhere, null); |
| 590 | } |
Romain Guy | 7eb9e5e | 2009-12-02 20:10:07 -0800 | [diff] [blame] | 591 | } |
| 592 | } catch (RuntimeException ex) { |
| 593 | Log.e(TAG, "Problem upgrading shortcut", ex); |
| 594 | } catch (URISyntaxException e) { |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 595 | Log.e(TAG, "Problem upgrading shortcut", e); |
Romain Guy | 7eb9e5e | 2009-12-02 20:10:07 -0800 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | } |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 599 | |
Romain Guy | 7eb9e5e | 2009-12-02 20:10:07 -0800 | [diff] [blame] | 600 | db.setTransactionSuccessful(); |
| 601 | } catch (SQLException ex) { |
| 602 | Log.w(TAG, "Problem while upgrading contacts", ex); |
| 603 | return false; |
| 604 | } finally { |
| 605 | db.endTransaction(); |
| 606 | if (c != null) { |
| 607 | c.close(); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | return true; |
| 612 | } |
| 613 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 614 | private void normalizeIcons(SQLiteDatabase db) { |
| 615 | Log.d(TAG, "normalizing icons"); |
| 616 | |
Joe Onorato | 346e129 | 2010-02-18 10:34:24 -0500 | [diff] [blame] | 617 | db.beginTransaction(); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 618 | Cursor c = null; |
Joe Onorato | 9690b39 | 2010-03-23 17:34:37 -0400 | [diff] [blame] | 619 | SQLiteStatement update = null; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 620 | try { |
| 621 | boolean logged = false; |
Joe Onorato | 9690b39 | 2010-03-23 17:34:37 -0400 | [diff] [blame] | 622 | update = db.compileStatement("UPDATE favorites " |
Jeff Hamilton | eaf77d6 | 2010-02-13 00:08:17 -0600 | [diff] [blame] | 623 | + "SET icon=? WHERE _id=?"); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 624 | |
| 625 | c = db.rawQuery("SELECT _id, icon FROM favorites WHERE iconType=" + |
| 626 | Favorites.ICON_TYPE_BITMAP, null); |
| 627 | |
| 628 | final int idIndex = c.getColumnIndexOrThrow(Favorites._ID); |
| 629 | final int iconIndex = c.getColumnIndexOrThrow(Favorites.ICON); |
| 630 | |
| 631 | while (c.moveToNext()) { |
| 632 | long id = c.getLong(idIndex); |
| 633 | byte[] data = c.getBlob(iconIndex); |
| 634 | try { |
| 635 | Bitmap bitmap = Utilities.resampleIconBitmap( |
| 636 | BitmapFactory.decodeByteArray(data, 0, data.length), |
| 637 | mContext); |
| 638 | if (bitmap != null) { |
| 639 | update.bindLong(1, id); |
| 640 | data = ItemInfo.flattenBitmap(bitmap); |
| 641 | if (data != null) { |
| 642 | update.bindBlob(2, data); |
| 643 | update.execute(); |
| 644 | } |
| 645 | bitmap.recycle(); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 646 | } |
| 647 | } catch (Exception e) { |
| 648 | if (!logged) { |
| 649 | Log.e(TAG, "Failed normalizing icon " + id, e); |
| 650 | } else { |
| 651 | Log.e(TAG, "Also failed normalizing icon " + id); |
| 652 | } |
| 653 | logged = true; |
| 654 | } |
| 655 | } |
Bjorn Bringert | 3a928e4 | 2010-02-19 11:15:40 +0000 | [diff] [blame] | 656 | db.setTransactionSuccessful(); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 657 | } catch (SQLException ex) { |
| 658 | Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex); |
| 659 | } finally { |
| 660 | db.endTransaction(); |
Joe Onorato | 9690b39 | 2010-03-23 17:34:37 -0400 | [diff] [blame] | 661 | if (update != null) { |
| 662 | update.close(); |
| 663 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 664 | if (c != null) { |
| 665 | c.close(); |
| 666 | } |
| 667 | } |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | // Generates a new ID to use for an object in your database. This method should be only |
| 671 | // called from the main UI thread. As an exception, we do call it when we call the |
| 672 | // constructor from the worker thread; however, this doesn't extend until after the |
| 673 | // constructor is called, and we only pass a reference to LauncherProvider to LauncherApp |
| 674 | // after that point |
| 675 | public long generateNewId() { |
| 676 | if (mMaxId < 0) { |
| 677 | throw new RuntimeException("Error: max id was not initialized"); |
| 678 | } |
| 679 | mMaxId += 1; |
| 680 | return mMaxId; |
| 681 | } |
| 682 | |
| 683 | private long initializeMaxId(SQLiteDatabase db) { |
| 684 | Cursor c = db.rawQuery("SELECT MAX(_id) FROM favorites", null); |
| 685 | |
| 686 | // get the result |
| 687 | final int maxIdIndex = 0; |
| 688 | long id = -1; |
| 689 | if (c != null && c.moveToNext()) { |
| 690 | id = c.getLong(maxIdIndex); |
| 691 | } |
Michael Jurka | 5130e40 | 2011-10-13 04:55:35 -0700 | [diff] [blame] | 692 | if (c != null) { |
| 693 | c.close(); |
| 694 | } |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 695 | |
| 696 | if (id == -1) { |
| 697 | throw new RuntimeException("Error: could not query max id"); |
| 698 | } |
| 699 | |
| 700 | return id; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 701 | } |
| 702 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 703 | /** |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 704 | * Upgrade existing clock and photo frame widgets into their new widget |
Bjorn Bringert | 93c4576 | 2009-12-16 13:19:47 +0000 | [diff] [blame] | 705 | * equivalents. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 706 | */ |
| 707 | private void convertWidgets(SQLiteDatabase db) { |
Bjorn Bringert | 3425134 | 2009-12-15 13:33:11 +0000 | [diff] [blame] | 708 | final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 709 | final int[] bindSources = new int[] { |
| 710 | Favorites.ITEM_TYPE_WIDGET_CLOCK, |
| 711 | Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME, |
Bjorn Bringert | 7984c94 | 2009-12-09 15:38:25 +0000 | [diff] [blame] | 712 | Favorites.ITEM_TYPE_WIDGET_SEARCH, |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 713 | }; |
Bjorn Bringert | 7984c94 | 2009-12-09 15:38:25 +0000 | [diff] [blame] | 714 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 715 | final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE, bindSources); |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 716 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 717 | Cursor c = null; |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 718 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 719 | db.beginTransaction(); |
| 720 | try { |
| 721 | // Select and iterate through each matching widget |
Bjorn Bringert | 7984c94 | 2009-12-09 15:38:25 +0000 | [diff] [blame] | 722 | 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] | 723 | selectWhere, null, null, null, null); |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 724 | |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 725 | if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount()); |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 726 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 727 | final ContentValues values = new ContentValues(); |
| 728 | while (c != null && c.moveToNext()) { |
| 729 | long favoriteId = c.getLong(0); |
Bjorn Bringert | 7984c94 | 2009-12-09 15:38:25 +0000 | [diff] [blame] | 730 | int favoriteType = c.getInt(1); |
| 731 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 732 | // Allocate and update database with new appWidgetId |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 733 | try { |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 734 | int appWidgetId = mAppWidgetHost.allocateAppWidgetId(); |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 735 | |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 736 | if (LOGD) { |
| 737 | Log.d(TAG, "allocated appWidgetId=" + appWidgetId |
| 738 | + " for favoriteId=" + favoriteId); |
| 739 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 740 | values.clear(); |
Bjorn Bringert | 7984c94 | 2009-12-09 15:38:25 +0000 | [diff] [blame] | 741 | values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET); |
| 742 | values.put(Favorites.APPWIDGET_ID, appWidgetId); |
| 743 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 744 | // Original widgets might not have valid spans when upgrading |
Bjorn Bringert | 7984c94 | 2009-12-09 15:38:25 +0000 | [diff] [blame] | 745 | if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) { |
| 746 | values.put(LauncherSettings.Favorites.SPANX, 4); |
| 747 | values.put(LauncherSettings.Favorites.SPANY, 1); |
| 748 | } else { |
| 749 | values.put(LauncherSettings.Favorites.SPANX, 2); |
| 750 | values.put(LauncherSettings.Favorites.SPANY, 2); |
| 751 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 752 | |
| 753 | String updateWhere = Favorites._ID + "=" + favoriteId; |
| 754 | db.update(TABLE_FAVORITES, values, updateWhere, null); |
Bjorn Bringert | 3425134 | 2009-12-15 13:33:11 +0000 | [diff] [blame] | 755 | |
Bjorn Bringert | 3425134 | 2009-12-15 13:33:11 +0000 | [diff] [blame] | 756 | if (favoriteType == Favorites.ITEM_TYPE_WIDGET_CLOCK) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 757 | // TODO: check return value |
| 758 | appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, |
Bjorn Bringert | 3425134 | 2009-12-15 13:33:11 +0000 | [diff] [blame] | 759 | new ComponentName("com.android.alarmclock", |
| 760 | "com.android.alarmclock.AnalogAppWidgetProvider")); |
| 761 | } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 762 | // TODO: check return value |
| 763 | appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, |
Bjorn Bringert | 3425134 | 2009-12-15 13:33:11 +0000 | [diff] [blame] | 764 | new ComponentName("com.android.camera", |
| 765 | "com.android.camera.PhotoAppWidgetProvider")); |
| 766 | } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 767 | // TODO: check return value |
| 768 | appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, |
Bjorn Bringert | cd8fec0 | 2010-01-14 13:26:43 +0000 | [diff] [blame] | 769 | getSearchWidgetProvider()); |
Bjorn Bringert | 3425134 | 2009-12-15 13:33:11 +0000 | [diff] [blame] | 770 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 771 | } catch (RuntimeException ex) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 772 | Log.e(TAG, "Problem allocating appWidgetId", ex); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 773 | } |
| 774 | } |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 775 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 776 | db.setTransactionSuccessful(); |
| 777 | } catch (SQLException ex) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 778 | 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] | 779 | } finally { |
| 780 | db.endTransaction(); |
| 781 | if (c != null) { |
| 782 | c.close(); |
| 783 | } |
| 784 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 785 | } |
| 786 | |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 787 | private static final void beginDocument(XmlPullParser parser, String firstElementName) |
| 788 | throws XmlPullParserException, IOException { |
| 789 | int type; |
Michael Jurka | 9bc8eba | 2012-05-21 20:36:44 -0700 | [diff] [blame] | 790 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 791 | && type != XmlPullParser.END_DOCUMENT) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 792 | ; |
| 793 | } |
| 794 | |
Michael Jurka | 9bc8eba | 2012-05-21 20:36:44 -0700 | [diff] [blame] | 795 | if (type != XmlPullParser.START_TAG) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 796 | throw new XmlPullParserException("No start tag found"); |
| 797 | } |
| 798 | |
| 799 | if (!parser.getName().equals(firstElementName)) { |
| 800 | throw new XmlPullParserException("Unexpected start tag: found " + parser.getName() + |
| 801 | ", expected " + firstElementName); |
| 802 | } |
| 803 | } |
| 804 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 805 | /** |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 806 | * Loads the default set of favorite packages from an xml file. |
| 807 | * |
| 808 | * @param db The database to write the values into |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 809 | * @param filterContainerId The specific container id of items to load |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 810 | */ |
Winson Chung | 6d09268 | 2011-11-16 18:43:26 -0800 | [diff] [blame] | 811 | private int loadFavorites(SQLiteDatabase db, int workspaceResourceId) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 812 | Intent intent = new Intent(Intent.ACTION_MAIN, null); |
| 813 | intent.addCategory(Intent.CATEGORY_LAUNCHER); |
| 814 | ContentValues values = new ContentValues(); |
| 815 | |
| 816 | PackageManager packageManager = mContext.getPackageManager(); |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 817 | int allAppsButtonRank = |
| 818 | mContext.getResources().getInteger(R.integer.hotseat_all_apps_index); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 819 | int i = 0; |
| 820 | try { |
Winson Chung | 6d09268 | 2011-11-16 18:43:26 -0800 | [diff] [blame] | 821 | XmlResourceParser parser = mContext.getResources().getXml(workspaceResourceId); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 822 | AttributeSet attrs = Xml.asAttributeSet(parser); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 823 | beginDocument(parser, TAG_FAVORITES); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 824 | |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 825 | final int depth = parser.getDepth(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 826 | |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 827 | int type; |
| 828 | while (((type = parser.next()) != XmlPullParser.END_TAG || |
| 829 | parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) { |
| 830 | |
| 831 | if (type != XmlPullParser.START_TAG) { |
| 832 | continue; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 833 | } |
| 834 | |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 835 | boolean added = false; |
| 836 | final String name = parser.getName(); |
| 837 | |
| 838 | TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.Favorite); |
| 839 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 840 | long container = LauncherSettings.Favorites.CONTAINER_DESKTOP; |
| 841 | if (a.hasValue(R.styleable.Favorite_container)) { |
| 842 | container = Long.valueOf(a.getString(R.styleable.Favorite_container)); |
| 843 | } |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 844 | |
Winson Chung | 6d09268 | 2011-11-16 18:43:26 -0800 | [diff] [blame] | 845 | String screen = a.getString(R.styleable.Favorite_screen); |
| 846 | String x = a.getString(R.styleable.Favorite_x); |
| 847 | String y = a.getString(R.styleable.Favorite_y); |
| 848 | |
| 849 | // If we are adding to the hotseat, the screen is used as the position in the |
| 850 | // hotseat. This screen can't be at position 0 because AllApps is in the |
| 851 | // zeroth position. |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 852 | if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT |
| 853 | && Integer.valueOf(screen) == allAppsButtonRank) { |
Winson Chung | 6d09268 | 2011-11-16 18:43:26 -0800 | [diff] [blame] | 854 | throw new RuntimeException("Invalid screen position for hotseat item"); |
| 855 | } |
| 856 | |
| 857 | values.clear(); |
| 858 | values.put(LauncherSettings.Favorites.CONTAINER, container); |
| 859 | values.put(LauncherSettings.Favorites.SCREEN, screen); |
| 860 | values.put(LauncherSettings.Favorites.CELLX, x); |
| 861 | values.put(LauncherSettings.Favorites.CELLY, y); |
| 862 | |
| 863 | if (TAG_FAVORITE.equals(name)) { |
| 864 | long id = addAppShortcut(db, values, a, packageManager, intent); |
| 865 | added = id >= 0; |
| 866 | } else if (TAG_SEARCH.equals(name)) { |
| 867 | added = addSearchWidget(db, values); |
| 868 | } else if (TAG_CLOCK.equals(name)) { |
| 869 | added = addClockWidget(db, values); |
| 870 | } else if (TAG_APPWIDGET.equals(name)) { |
Winson Chung | b3302ae | 2012-05-01 10:19:14 -0700 | [diff] [blame] | 871 | added = addAppWidget(parser, attrs, type, db, values, a, packageManager); |
Winson Chung | 6d09268 | 2011-11-16 18:43:26 -0800 | [diff] [blame] | 872 | } else if (TAG_SHORTCUT.equals(name)) { |
| 873 | long id = addUriShortcut(db, values, a); |
| 874 | added = id >= 0; |
| 875 | } else if (TAG_FOLDER.equals(name)) { |
| 876 | String title; |
| 877 | int titleResId = a.getResourceId(R.styleable.Favorite_title, -1); |
| 878 | if (titleResId != -1) { |
| 879 | title = mContext.getResources().getString(titleResId); |
| 880 | } else { |
| 881 | title = mContext.getResources().getString(R.string.folder_name); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 882 | } |
Winson Chung | 6d09268 | 2011-11-16 18:43:26 -0800 | [diff] [blame] | 883 | values.put(LauncherSettings.Favorites.TITLE, title); |
| 884 | long folderId = addFolder(db, values); |
| 885 | added = folderId >= 0; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 886 | |
Winson Chung | 6d09268 | 2011-11-16 18:43:26 -0800 | [diff] [blame] | 887 | ArrayList<Long> folderItems = new ArrayList<Long>(); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 888 | |
Winson Chung | 6d09268 | 2011-11-16 18:43:26 -0800 | [diff] [blame] | 889 | int folderDepth = parser.getDepth(); |
| 890 | while ((type = parser.next()) != XmlPullParser.END_TAG || |
| 891 | parser.getDepth() > folderDepth) { |
| 892 | if (type != XmlPullParser.START_TAG) { |
| 893 | continue; |
| 894 | } |
| 895 | final String folder_item_name = parser.getName(); |
| 896 | |
| 897 | TypedArray ar = mContext.obtainStyledAttributes(attrs, |
| 898 | R.styleable.Favorite); |
| 899 | values.clear(); |
| 900 | values.put(LauncherSettings.Favorites.CONTAINER, folderId); |
| 901 | |
| 902 | if (TAG_FAVORITE.equals(folder_item_name) && folderId >= 0) { |
| 903 | long id = |
| 904 | addAppShortcut(db, values, ar, packageManager, intent); |
| 905 | if (id >= 0) { |
| 906 | folderItems.add(id); |
| 907 | } |
| 908 | } else if (TAG_SHORTCUT.equals(folder_item_name) && folderId >= 0) { |
| 909 | long id = addUriShortcut(db, values, ar); |
| 910 | if (id >= 0) { |
| 911 | folderItems.add(id); |
| 912 | } |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 913 | } else { |
Winson Chung | 6d09268 | 2011-11-16 18:43:26 -0800 | [diff] [blame] | 914 | throw new RuntimeException("Folders can " + |
| 915 | "contain only shortcuts"); |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 916 | } |
Winson Chung | 6d09268 | 2011-11-16 18:43:26 -0800 | [diff] [blame] | 917 | ar.recycle(); |
| 918 | } |
| 919 | // We can only have folders with >= 2 items, so we need to remove the |
| 920 | // folder and clean up if less than 2 items were included, or some |
| 921 | // failed to add, and less than 2 were actually added |
| 922 | if (folderItems.size() < 2 && folderId >= 0) { |
| 923 | // We just delete the folder and any items that made it |
| 924 | deleteId(db, folderId); |
| 925 | if (folderItems.size() > 0) { |
| 926 | deleteId(db, folderItems.get(0)); |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 927 | } |
Winson Chung | 6d09268 | 2011-11-16 18:43:26 -0800 | [diff] [blame] | 928 | added = false; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 929 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 930 | } |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 931 | if (added) i++; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 932 | a.recycle(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 933 | } |
| 934 | } catch (XmlPullParserException e) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 935 | Log.w(TAG, "Got exception parsing favorites.", e); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 936 | } catch (IOException e) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 937 | Log.w(TAG, "Got exception parsing favorites.", e); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 938 | } catch (RuntimeException e) { |
| 939 | Log.w(TAG, "Got exception parsing favorites.", e); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 940 | } |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 941 | |
| 942 | return i; |
| 943 | } |
| 944 | |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 945 | private long addAppShortcut(SQLiteDatabase db, ContentValues values, TypedArray a, |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 946 | PackageManager packageManager, Intent intent) { |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 947 | long id = -1; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 948 | ActivityInfo info; |
| 949 | String packageName = a.getString(R.styleable.Favorite_packageName); |
| 950 | String className = a.getString(R.styleable.Favorite_className); |
| 951 | try { |
Romain Guy | 693599f | 2010-03-23 10:58:18 -0700 | [diff] [blame] | 952 | ComponentName cn; |
| 953 | try { |
| 954 | cn = new ComponentName(packageName, className); |
| 955 | info = packageManager.getActivityInfo(cn, 0); |
| 956 | } catch (PackageManager.NameNotFoundException nnfe) { |
| 957 | String[] packages = packageManager.currentToCanonicalPackageNames( |
| 958 | new String[] { packageName }); |
| 959 | cn = new ComponentName(packages[0], className); |
| 960 | info = packageManager.getActivityInfo(cn, 0); |
| 961 | } |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 962 | id = generateNewId(); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 963 | intent.setComponent(cn); |
Romain Guy | 693599f | 2010-03-23 10:58:18 -0700 | [diff] [blame] | 964 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | |
| 965 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
Romain Guy | 1ce1a24 | 2009-06-23 17:34:54 -0700 | [diff] [blame] | 966 | values.put(Favorites.INTENT, intent.toUri(0)); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 967 | values.put(Favorites.TITLE, info.loadLabel(packageManager).toString()); |
| 968 | values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPLICATION); |
| 969 | values.put(Favorites.SPANX, 1); |
| 970 | values.put(Favorites.SPANY, 1); |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 971 | values.put(Favorites._ID, generateNewId()); |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 972 | if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values) < 0) { |
| 973 | return -1; |
| 974 | } |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 975 | } catch (PackageManager.NameNotFoundException e) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 976 | Log.w(TAG, "Unable to add favorite: " + packageName + |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 977 | "/" + className, e); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 978 | } |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 979 | return id; |
| 980 | } |
| 981 | |
| 982 | private long addFolder(SQLiteDatabase db, ContentValues values) { |
| 983 | values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_FOLDER); |
| 984 | values.put(Favorites.SPANX, 1); |
| 985 | values.put(Favorites.SPANY, 1); |
| 986 | long id = generateNewId(); |
| 987 | values.put(Favorites._ID, id); |
| 988 | if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values) <= 0) { |
| 989 | return -1; |
| 990 | } else { |
| 991 | return id; |
| 992 | } |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 993 | } |
| 994 | |
Bjorn Bringert | cd8fec0 | 2010-01-14 13:26:43 +0000 | [diff] [blame] | 995 | private ComponentName getSearchWidgetProvider() { |
| 996 | SearchManager searchManager = |
| 997 | (SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE); |
| 998 | ComponentName searchComponent = searchManager.getGlobalSearchActivity(); |
| 999 | if (searchComponent == null) return null; |
| 1000 | return getProviderInPackage(searchComponent.getPackageName()); |
| 1001 | } |
| 1002 | |
| 1003 | /** |
| 1004 | * Gets an appwidget provider from the given package. If the package contains more than |
| 1005 | * one appwidget provider, an arbitrary one is returned. |
| 1006 | */ |
| 1007 | private ComponentName getProviderInPackage(String packageName) { |
| 1008 | AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext); |
| 1009 | List<AppWidgetProviderInfo> providers = appWidgetManager.getInstalledProviders(); |
| 1010 | if (providers == null) return null; |
| 1011 | final int providerCount = providers.size(); |
| 1012 | for (int i = 0; i < providerCount; i++) { |
| 1013 | ComponentName provider = providers.get(i).provider; |
| 1014 | if (provider != null && provider.getPackageName().equals(packageName)) { |
| 1015 | return provider; |
| 1016 | } |
| 1017 | } |
| 1018 | return null; |
| 1019 | } |
| 1020 | |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1021 | private boolean addSearchWidget(SQLiteDatabase db, ContentValues values) { |
Bjorn Bringert | cd8fec0 | 2010-01-14 13:26:43 +0000 | [diff] [blame] | 1022 | ComponentName cn = getSearchWidgetProvider(); |
Winson Chung | b3302ae | 2012-05-01 10:19:14 -0700 | [diff] [blame] | 1023 | return addAppWidget(db, values, cn, 4, 1, null); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | private boolean addClockWidget(SQLiteDatabase db, ContentValues values) { |
Bjorn Bringert | 3425134 | 2009-12-15 13:33:11 +0000 | [diff] [blame] | 1027 | ComponentName cn = new ComponentName("com.android.alarmclock", |
| 1028 | "com.android.alarmclock.AnalogAppWidgetProvider"); |
Winson Chung | b3302ae | 2012-05-01 10:19:14 -0700 | [diff] [blame] | 1029 | return addAppWidget(db, values, cn, 2, 2, null); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1030 | } |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 1031 | |
Winson Chung | b3302ae | 2012-05-01 10:19:14 -0700 | [diff] [blame] | 1032 | private boolean addAppWidget(XmlResourceParser parser, AttributeSet attrs, int type, |
| 1033 | SQLiteDatabase db, ContentValues values, TypedArray a, |
| 1034 | PackageManager packageManager) throws XmlPullParserException, IOException { |
Romain Guy | 693599f | 2010-03-23 10:58:18 -0700 | [diff] [blame] | 1035 | |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 1036 | String packageName = a.getString(R.styleable.Favorite_packageName); |
| 1037 | String className = a.getString(R.styleable.Favorite_className); |
| 1038 | |
| 1039 | if (packageName == null || className == null) { |
| 1040 | return false; |
| 1041 | } |
Romain Guy | 693599f | 2010-03-23 10:58:18 -0700 | [diff] [blame] | 1042 | |
| 1043 | boolean hasPackage = true; |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 1044 | ComponentName cn = new ComponentName(packageName, className); |
Romain Guy | 693599f | 2010-03-23 10:58:18 -0700 | [diff] [blame] | 1045 | try { |
| 1046 | packageManager.getReceiverInfo(cn, 0); |
| 1047 | } catch (Exception e) { |
| 1048 | String[] packages = packageManager.currentToCanonicalPackageNames( |
| 1049 | new String[] { packageName }); |
| 1050 | cn = new ComponentName(packages[0], className); |
| 1051 | try { |
| 1052 | packageManager.getReceiverInfo(cn, 0); |
| 1053 | } catch (Exception e1) { |
| 1054 | hasPackage = false; |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | if (hasPackage) { |
| 1059 | int spanX = a.getInt(R.styleable.Favorite_spanX, 0); |
| 1060 | int spanY = a.getInt(R.styleable.Favorite_spanY, 0); |
Winson Chung | b3302ae | 2012-05-01 10:19:14 -0700 | [diff] [blame] | 1061 | |
| 1062 | // Read the extras |
| 1063 | Bundle extras = new Bundle(); |
| 1064 | int widgetDepth = parser.getDepth(); |
| 1065 | while ((type = parser.next()) != XmlPullParser.END_TAG || |
| 1066 | parser.getDepth() > widgetDepth) { |
| 1067 | if (type != XmlPullParser.START_TAG) { |
| 1068 | continue; |
| 1069 | } |
| 1070 | |
| 1071 | TypedArray ar = mContext.obtainStyledAttributes(attrs, R.styleable.Extra); |
| 1072 | if (TAG_EXTRA.equals(parser.getName())) { |
| 1073 | String key = ar.getString(R.styleable.Extra_key); |
| 1074 | String value = ar.getString(R.styleable.Extra_value); |
| 1075 | if (key != null && value != null) { |
| 1076 | extras.putString(key, value); |
| 1077 | } else { |
| 1078 | throw new RuntimeException("Widget extras must have a key and value"); |
| 1079 | } |
| 1080 | } else { |
| 1081 | throw new RuntimeException("Widgets can contain only extras"); |
| 1082 | } |
| 1083 | ar.recycle(); |
| 1084 | } |
| 1085 | |
| 1086 | return addAppWidget(db, values, cn, spanX, spanY, extras); |
Romain Guy | 693599f | 2010-03-23 10:58:18 -0700 | [diff] [blame] | 1087 | } |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 1088 | |
Romain Guy | 693599f | 2010-03-23 10:58:18 -0700 | [diff] [blame] | 1089 | return false; |
Bjorn Bringert | 7984c94 | 2009-12-09 15:38:25 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | private boolean addAppWidget(SQLiteDatabase db, ContentValues values, ComponentName cn, |
Winson Chung | b3302ae | 2012-05-01 10:19:14 -0700 | [diff] [blame] | 1093 | int spanX, int spanY, Bundle extras) { |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 1094 | boolean allocatedAppWidgets = false; |
| 1095 | final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext); |
| 1096 | |
| 1097 | try { |
| 1098 | int appWidgetId = mAppWidgetHost.allocateAppWidgetId(); |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 1099 | |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 1100 | values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET); |
Bjorn Bringert | 7984c94 | 2009-12-09 15:38:25 +0000 | [diff] [blame] | 1101 | values.put(Favorites.SPANX, spanX); |
| 1102 | values.put(Favorites.SPANY, spanY); |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 1103 | values.put(Favorites.APPWIDGET_ID, appWidgetId); |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 1104 | values.put(Favorites._ID, generateNewId()); |
| 1105 | dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values); |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 1106 | |
| 1107 | allocatedAppWidgets = true; |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 1108 | |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1109 | // TODO: need to check return value |
| 1110 | appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, cn); |
Winson Chung | b3302ae | 2012-05-01 10:19:14 -0700 | [diff] [blame] | 1111 | |
| 1112 | // Send a broadcast to configure the widget |
| 1113 | if (extras != null && !extras.isEmpty()) { |
| 1114 | Intent intent = new Intent(ACTION_APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE); |
| 1115 | intent.setComponent(cn); |
| 1116 | intent.putExtras(extras); |
| 1117 | intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); |
| 1118 | mContext.sendBroadcast(intent); |
| 1119 | } |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 1120 | } catch (RuntimeException ex) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 1121 | Log.e(TAG, "Problem allocating appWidgetId", ex); |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 1122 | } |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 1123 | |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 1124 | return allocatedAppWidgets; |
| 1125 | } |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 1126 | |
| 1127 | private long addUriShortcut(SQLiteDatabase db, ContentValues values, |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 1128 | TypedArray a) { |
| 1129 | Resources r = mContext.getResources(); |
| 1130 | |
| 1131 | final int iconResId = a.getResourceId(R.styleable.Favorite_icon, 0); |
| 1132 | final int titleResId = a.getResourceId(R.styleable.Favorite_title, 0); |
| 1133 | |
Romain Guy | 7eb9e5e | 2009-12-02 20:10:07 -0800 | [diff] [blame] | 1134 | Intent intent; |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 1135 | String uri = null; |
| 1136 | try { |
| 1137 | uri = a.getString(R.styleable.Favorite_uri); |
| 1138 | intent = Intent.parseUri(uri, 0); |
| 1139 | } catch (URISyntaxException e) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 1140 | Log.w(TAG, "Shortcut has malformed uri: " + uri); |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 1141 | return -1; // Oh well |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | if (iconResId == 0 || titleResId == 0) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 1145 | Log.w(TAG, "Shortcut is missing title or icon resource ID"); |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 1146 | return -1; |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 1147 | } |
| 1148 | |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 1149 | long id = generateNewId(); |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 1150 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 1151 | values.put(Favorites.INTENT, intent.toUri(0)); |
| 1152 | values.put(Favorites.TITLE, r.getString(titleResId)); |
| 1153 | values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_SHORTCUT); |
| 1154 | values.put(Favorites.SPANX, 1); |
| 1155 | values.put(Favorites.SPANY, 1); |
| 1156 | values.put(Favorites.ICON_TYPE, Favorites.ICON_TYPE_RESOURCE); |
| 1157 | values.put(Favorites.ICON_PACKAGE, mContext.getPackageName()); |
| 1158 | values.put(Favorites.ICON_RESOURCE, r.getResourceName(iconResId)); |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 1159 | values.put(Favorites._ID, id); |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 1160 | |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 1161 | if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values) < 0) { |
| 1162 | return -1; |
| 1163 | } |
| 1164 | return id; |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 1165 | } |
| 1166 | } |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 1167 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1168 | /** |
| 1169 | * Build a query string that will match any row where the column matches |
| 1170 | * anything in the values list. |
| 1171 | */ |
| 1172 | static String buildOrWhereString(String column, int[] values) { |
| 1173 | StringBuilder selectWhere = new StringBuilder(); |
| 1174 | for (int i = values.length - 1; i >= 0; i--) { |
| 1175 | selectWhere.append(column).append("=").append(values[i]); |
| 1176 | if (i > 0) { |
| 1177 | selectWhere.append(" OR "); |
| 1178 | } |
| 1179 | } |
| 1180 | return selectWhere.toString(); |
| 1181 | } |
| 1182 | |
| 1183 | static class SqlArguments { |
| 1184 | public final String table; |
| 1185 | public final String where; |
| 1186 | public final String[] args; |
| 1187 | |
| 1188 | SqlArguments(Uri url, String where, String[] args) { |
| 1189 | if (url.getPathSegments().size() == 1) { |
| 1190 | this.table = url.getPathSegments().get(0); |
| 1191 | this.where = where; |
| 1192 | this.args = args; |
| 1193 | } else if (url.getPathSegments().size() != 2) { |
| 1194 | throw new IllegalArgumentException("Invalid URI: " + url); |
| 1195 | } else if (!TextUtils.isEmpty(where)) { |
| 1196 | throw new UnsupportedOperationException("WHERE clause not supported: " + url); |
| 1197 | } else { |
| 1198 | this.table = url.getPathSegments().get(0); |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 1199 | this.where = "_id=" + ContentUris.parseId(url); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1200 | this.args = null; |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | SqlArguments(Uri url) { |
| 1205 | if (url.getPathSegments().size() == 1) { |
| 1206 | table = url.getPathSegments().get(0); |
| 1207 | where = null; |
| 1208 | args = null; |
| 1209 | } else { |
| 1210 | throw new IllegalArgumentException("Invalid URI: " + url); |
| 1211 | } |
| 1212 | } |
| 1213 | } |
| 1214 | } |