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