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