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