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