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