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