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