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