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