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; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 25 | import android.content.ContentResolver; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 26 | import android.content.ContentUris; |
| 27 | import android.content.ContentValues; |
| 28 | import android.content.Context; |
| 29 | import android.content.Intent; |
Michael Jurka | b85f8a4 | 2012-04-25 15:48:32 -0700 | [diff] [blame] | 30 | import android.content.SharedPreferences; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 31 | import android.content.pm.ActivityInfo; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 32 | import android.content.pm.PackageManager; |
| 33 | import android.content.res.Resources; |
| 34 | import android.content.res.TypedArray; |
| 35 | import android.content.res.XmlResourceParser; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 36 | import android.database.Cursor; |
| 37 | import android.database.SQLException; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 38 | import android.database.sqlite.SQLiteDatabase; |
| 39 | import android.database.sqlite.SQLiteOpenHelper; |
| 40 | import android.database.sqlite.SQLiteQueryBuilder; |
| 41 | import android.database.sqlite.SQLiteStatement; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 42 | import android.graphics.Bitmap; |
| 43 | import android.graphics.BitmapFactory; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 44 | import android.net.Uri; |
Winson Chung | b3302ae | 2012-05-01 10:19:14 -0700 | [diff] [blame] | 45 | import android.os.Bundle; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 46 | import android.provider.Settings; |
| 47 | import android.text.TextUtils; |
| 48 | import android.util.AttributeSet; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 49 | import android.util.Log; |
| 50 | import android.util.Xml; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 51 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 52 | import com.android.launcher3.LauncherSettings.Favorites; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 53 | |
| 54 | import org.xmlpull.v1.XmlPullParser; |
| 55 | import org.xmlpull.v1.XmlPullParserException; |
| 56 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 57 | import java.io.IOException; |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 58 | import java.net.URISyntaxException; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 59 | import java.util.ArrayList; |
Bjorn Bringert | cd8fec0 | 2010-01-14 13:26:43 +0000 | [diff] [blame] | 60 | import java.util.List; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 61 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 62 | public class LauncherProvider extends ContentProvider { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 63 | private static final String TAG = "Launcher.LauncherProvider"; |
| 64 | private static final boolean LOGD = false; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 65 | |
| 66 | private static final String DATABASE_NAME = "launcher.db"; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 67 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 68 | private static final int DATABASE_VERSION = 13; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 69 | |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 70 | static final String OLD_AUTHORITY = "com.android.launcher2.settings"; |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 71 | static final String AUTHORITY = "com.android.launcher3.settings"; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 72 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 73 | static final String TABLE_FAVORITES = "favorites"; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 74 | static final String TABLE_WORKSPACE_SCREENS = "workspaceScreens"; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 75 | static final String PARAMETER_NOTIFY = "notify"; |
Michael Jurka | b85f8a4 | 2012-04-25 15:48:32 -0700 | [diff] [blame] | 76 | static final String DB_CREATED_BUT_DEFAULT_WORKSPACE_NOT_LOADED = |
| 77 | "DB_CREATED_BUT_DEFAULT_WORKSPACE_NOT_LOADED"; |
Michael Jurka | 45355c4 | 2012-10-08 13:21:35 +0200 | [diff] [blame] | 78 | static final String DEFAULT_WORKSPACE_RESOURCE_ID = |
| 79 | "DEFAULT_WORKSPACE_RESOURCE_ID"; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 80 | |
Winson Chung | b3302ae | 2012-05-01 10:19:14 -0700 | [diff] [blame] | 81 | private static final String ACTION_APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE = |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 82 | "com.android.launcher.action.APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE"; |
Winson Chung | b3302ae | 2012-05-01 10:19:14 -0700 | [diff] [blame] | 83 | |
Jeffrey Sharkey | 2bbcae1 | 2009-03-31 14:37:57 -0700 | [diff] [blame] | 84 | /** |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 85 | * {@link Uri} triggered at any registered {@link android.database.ContentObserver} when |
Jeffrey Sharkey | 2bbcae1 | 2009-03-31 14:37:57 -0700 | [diff] [blame] | 86 | * {@link AppWidgetHost#deleteHost()} is called during database creation. |
| 87 | * Use this to recall {@link AppWidgetHost#startListening()} if needed. |
| 88 | */ |
| 89 | static final Uri CONTENT_APPWIDGET_RESET_URI = |
| 90 | Uri.parse("content://" + AUTHORITY + "/appWidgetReset"); |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 91 | |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 92 | private DatabaseHelper mOpenHelper; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 93 | private static boolean sLoadOldDb; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 94 | |
| 95 | @Override |
| 96 | public boolean onCreate() { |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame^] | 97 | final Context context = getContext(); |
| 98 | mOpenHelper = new DatabaseHelper(context); |
| 99 | LauncherAppState.setLauncherProvider(this); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 100 | return true; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public String getType(Uri uri) { |
| 105 | SqlArguments args = new SqlArguments(uri, null, null); |
| 106 | if (TextUtils.isEmpty(args.where)) { |
| 107 | return "vnd.android.cursor.dir/" + args.table; |
| 108 | } else { |
| 109 | return "vnd.android.cursor.item/" + args.table; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public Cursor query(Uri uri, String[] projection, String selection, |
| 115 | String[] selectionArgs, String sortOrder) { |
| 116 | |
| 117 | SqlArguments args = new SqlArguments(uri, selection, selectionArgs); |
| 118 | SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); |
| 119 | qb.setTables(args.table); |
| 120 | |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 121 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 122 | Cursor result = qb.query(db, projection, args.where, args.args, null, null, sortOrder); |
| 123 | result.setNotificationUri(getContext().getContentResolver(), uri); |
| 124 | |
| 125 | return result; |
| 126 | } |
| 127 | |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 128 | private static long dbInsertAndCheck(DatabaseHelper helper, |
| 129 | SQLiteDatabase db, String table, String nullColumnHack, ContentValues values) { |
| 130 | if (!values.containsKey(LauncherSettings.Favorites._ID)) { |
| 131 | throw new RuntimeException("Error: attempting to add item without specifying an id"); |
| 132 | } |
| 133 | return db.insert(table, nullColumnHack, values); |
| 134 | } |
| 135 | |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 136 | private static void deleteId(SQLiteDatabase db, long id) { |
| 137 | Uri uri = LauncherSettings.Favorites.getContentUri(id, false); |
| 138 | SqlArguments args = new SqlArguments(uri, null, null); |
| 139 | db.delete(args.table, args.where, args.args); |
| 140 | } |
| 141 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 142 | @Override |
| 143 | public Uri insert(Uri uri, ContentValues initialValues) { |
| 144 | SqlArguments args = new SqlArguments(uri); |
| 145 | |
| 146 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 147 | 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] | 148 | if (rowId <= 0) return null; |
| 149 | |
| 150 | uri = ContentUris.withAppendedId(uri, rowId); |
| 151 | sendNotify(uri); |
| 152 | |
| 153 | return uri; |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public int bulkInsert(Uri uri, ContentValues[] values) { |
| 158 | SqlArguments args = new SqlArguments(uri); |
| 159 | |
| 160 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 161 | db.beginTransaction(); |
| 162 | try { |
| 163 | int numValues = values.length; |
| 164 | for (int i = 0; i < numValues; i++) { |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 165 | if (dbInsertAndCheck(mOpenHelper, db, args.table, null, values[i]) < 0) { |
| 166 | return 0; |
| 167 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 168 | } |
| 169 | db.setTransactionSuccessful(); |
| 170 | } finally { |
| 171 | db.endTransaction(); |
| 172 | } |
| 173 | |
| 174 | sendNotify(uri); |
| 175 | return values.length; |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public int delete(Uri uri, String selection, String[] selectionArgs) { |
| 180 | SqlArguments args = new SqlArguments(uri, selection, selectionArgs); |
| 181 | |
| 182 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 183 | int count = db.delete(args.table, args.where, args.args); |
| 184 | if (count > 0) sendNotify(uri); |
| 185 | |
| 186 | return count; |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { |
| 191 | SqlArguments args = new SqlArguments(uri, selection, selectionArgs); |
| 192 | |
| 193 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 194 | int count = db.update(args.table, values, args.where, args.args); |
| 195 | if (count > 0) sendNotify(uri); |
| 196 | |
| 197 | return count; |
| 198 | } |
| 199 | |
| 200 | private void sendNotify(Uri uri) { |
| 201 | String notify = uri.getQueryParameter(PARAMETER_NOTIFY); |
| 202 | if (notify == null || "true".equals(notify)) { |
| 203 | getContext().getContentResolver().notifyChange(uri, null); |
| 204 | } |
| 205 | } |
| 206 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 207 | public long generateNewItemId() { |
| 208 | return mOpenHelper.generateNewItemId(); |
| 209 | } |
| 210 | |
| 211 | public long generateNewScreenId() { |
| 212 | return mOpenHelper.generateNewScreenId(); |
| 213 | } |
| 214 | |
| 215 | // This is only required one time while loading the workspace during the |
| 216 | // upgrade path, and should never be called from anywhere else. |
| 217 | public void updateMaxScreenId(long maxScreenId) { |
| 218 | mOpenHelper.updateMaxScreenId(maxScreenId); |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Brian Muramatsu | 5524b49 | 2012-10-02 16:55:54 -0700 | [diff] [blame] | 221 | /** |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 222 | * @param Should we load the old db for upgrade? first run only. |
| 223 | */ |
| 224 | synchronized public boolean shouldLoadOldDb() { |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 225 | String spKey = LauncherAppState.getSharedPreferencesKey(); |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 226 | SharedPreferences sp = getContext().getSharedPreferences(spKey, Context.MODE_PRIVATE); |
| 227 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 228 | boolean loadOldDb = false || sLoadOldDb; |
| 229 | |
| 230 | sLoadOldDb = false; |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 231 | if (sp.getBoolean(DB_CREATED_BUT_DEFAULT_WORKSPACE_NOT_LOADED, false)) { |
| 232 | |
| 233 | SharedPreferences.Editor editor = sp.edit(); |
| 234 | editor.remove(DB_CREATED_BUT_DEFAULT_WORKSPACE_NOT_LOADED); |
| 235 | editor.commit(); |
| 236 | loadOldDb = true; |
| 237 | } |
| 238 | return loadOldDb; |
| 239 | } |
| 240 | |
| 241 | /** |
Brian Muramatsu | 5524b49 | 2012-10-02 16:55:54 -0700 | [diff] [blame] | 242 | * @param workspaceResId that can be 0 to use default or non-zero for specific resource |
| 243 | */ |
Michael Jurka | 45355c4 | 2012-10-08 13:21:35 +0200 | [diff] [blame] | 244 | synchronized public void loadDefaultFavoritesIfNecessary(int origWorkspaceResId) { |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 245 | String spKey = LauncherAppState.getSharedPreferencesKey(); |
Michael Jurka | b85f8a4 | 2012-04-25 15:48:32 -0700 | [diff] [blame] | 246 | SharedPreferences sp = getContext().getSharedPreferences(spKey, Context.MODE_PRIVATE); |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 247 | |
Michael Jurka | b85f8a4 | 2012-04-25 15:48:32 -0700 | [diff] [blame] | 248 | if (sp.getBoolean(DB_CREATED_BUT_DEFAULT_WORKSPACE_NOT_LOADED, false)) { |
Michael Jurka | 45355c4 | 2012-10-08 13:21:35 +0200 | [diff] [blame] | 249 | int workspaceResId = origWorkspaceResId; |
| 250 | |
Brian Muramatsu | 5524b49 | 2012-10-02 16:55:54 -0700 | [diff] [blame] | 251 | // Use default workspace resource if none provided |
| 252 | if (workspaceResId == 0) { |
Michael Jurka | 45355c4 | 2012-10-08 13:21:35 +0200 | [diff] [blame] | 253 | workspaceResId = sp.getInt(DEFAULT_WORKSPACE_RESOURCE_ID, R.xml.default_workspace); |
Brian Muramatsu | 5524b49 | 2012-10-02 16:55:54 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Michael Jurka | b85f8a4 | 2012-04-25 15:48:32 -0700 | [diff] [blame] | 256 | // Populate favorites table with initial favorites |
| 257 | SharedPreferences.Editor editor = sp.edit(); |
| 258 | editor.remove(DB_CREATED_BUT_DEFAULT_WORKSPACE_NOT_LOADED); |
Michael Jurka | 45355c4 | 2012-10-08 13:21:35 +0200 | [diff] [blame] | 259 | if (origWorkspaceResId != 0) { |
| 260 | editor.putInt(DEFAULT_WORKSPACE_RESOURCE_ID, origWorkspaceResId); |
| 261 | } |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 262 | |
Brian Muramatsu | 5524b49 | 2012-10-02 16:55:54 -0700 | [diff] [blame] | 263 | mOpenHelper.loadFavorites(mOpenHelper.getWritableDatabase(), workspaceResId); |
Michael Jurka | b85f8a4 | 2012-04-25 15:48:32 -0700 | [diff] [blame] | 264 | editor.commit(); |
| 265 | } |
| 266 | } |
| 267 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 268 | private static class DatabaseHelper extends SQLiteOpenHelper { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 269 | private static final String TAG_FAVORITES = "favorites"; |
| 270 | private static final String TAG_FAVORITE = "favorite"; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 271 | private static final String TAG_CLOCK = "clock"; |
| 272 | private static final String TAG_SEARCH = "search"; |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 273 | private static final String TAG_APPWIDGET = "appwidget"; |
| 274 | private static final String TAG_SHORTCUT = "shortcut"; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 275 | private static final String TAG_FOLDER = "folder"; |
Winson Chung | b3302ae | 2012-05-01 10:19:14 -0700 | [diff] [blame] | 276 | private static final String TAG_EXTRA = "extra"; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 277 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 278 | private final Context mContext; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 279 | private final AppWidgetHost mAppWidgetHost; |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 280 | private long mMaxItemId = -1; |
| 281 | private long mMaxScreenId = -1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 282 | |
| 283 | DatabaseHelper(Context context) { |
| 284 | super(context, DATABASE_NAME, null, DATABASE_VERSION); |
| 285 | mContext = context; |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 286 | mAppWidgetHost = new AppWidgetHost(context, Launcher.APPWIDGET_HOST_ID); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 287 | |
| 288 | // In the case where neither onCreate nor onUpgrade gets called, we read the maxId from |
| 289 | // the DB here |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 290 | if (mMaxItemId == -1) { |
| 291 | mMaxItemId = initializeMaxItemId(getWritableDatabase()); |
| 292 | } |
| 293 | if (mMaxScreenId == -1) { |
| 294 | mMaxScreenId = initializeMaxScreenId(getWritableDatabase()); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 295 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 296 | } |
| 297 | |
Jeffrey Sharkey | 2bbcae1 | 2009-03-31 14:37:57 -0700 | [diff] [blame] | 298 | /** |
| 299 | * Send notification that we've deleted the {@link AppWidgetHost}, |
| 300 | * probably as part of the initial database creation. The receiver may |
| 301 | * want to re-call {@link AppWidgetHost#startListening()} to ensure |
| 302 | * callbacks are correctly set. |
| 303 | */ |
| 304 | private void sendAppWidgetResetNotify() { |
| 305 | final ContentResolver resolver = mContext.getContentResolver(); |
| 306 | resolver.notifyChange(CONTENT_APPWIDGET_RESET_URI, null); |
| 307 | } |
| 308 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 309 | @Override |
| 310 | public void onCreate(SQLiteDatabase db) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 311 | if (LOGD) Log.d(TAG, "creating new launcher database"); |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 312 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 313 | mMaxItemId = 1; |
| 314 | mMaxScreenId = 0; |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 315 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 316 | db.execSQL("CREATE TABLE favorites (" + |
| 317 | "_id INTEGER PRIMARY KEY," + |
| 318 | "title TEXT," + |
| 319 | "intent TEXT," + |
| 320 | "container INTEGER," + |
| 321 | "screen INTEGER," + |
| 322 | "cellX INTEGER," + |
| 323 | "cellY INTEGER," + |
| 324 | "spanX INTEGER," + |
| 325 | "spanY INTEGER," + |
| 326 | "itemType INTEGER," + |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 327 | "appWidgetId INTEGER NOT NULL DEFAULT -1," + |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 328 | "isShortcut INTEGER," + |
| 329 | "iconType INTEGER," + |
| 330 | "iconPackage TEXT," + |
| 331 | "iconResource TEXT," + |
| 332 | "icon BLOB," + |
| 333 | "uri TEXT," + |
| 334 | "displayMode INTEGER" + |
| 335 | ");"); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 336 | addWorkspacesTable(db); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 337 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 338 | // Database was just created, so wipe any previous widgets |
| 339 | if (mAppWidgetHost != null) { |
| 340 | mAppWidgetHost.deleteHost(); |
Jeffrey Sharkey | 2bbcae1 | 2009-03-31 14:37:57 -0700 | [diff] [blame] | 341 | sendAppWidgetResetNotify(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 342 | } |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 343 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 344 | if (!convertDatabase(db)) { |
Michael Jurka | b85f8a4 | 2012-04-25 15:48:32 -0700 | [diff] [blame] | 345 | // Set a shared pref so that we know we need to load the default workspace later |
| 346 | setFlagToLoadDefaultWorkspaceLater(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 350 | private void addWorkspacesTable(SQLiteDatabase db) { |
| 351 | db.execSQL("CREATE TABLE " + TABLE_WORKSPACE_SCREENS + " (" + |
| 352 | LauncherSettings.WorkspaceScreens._ID + " INTEGER," + |
| 353 | LauncherSettings.WorkspaceScreens.SCREEN_RANK + " INTEGER" + |
| 354 | ");"); |
| 355 | } |
| 356 | |
Michael Jurka | b85f8a4 | 2012-04-25 15:48:32 -0700 | [diff] [blame] | 357 | private void setFlagToLoadDefaultWorkspaceLater() { |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 358 | String spKey = LauncherAppState.getSharedPreferencesKey(); |
Michael Jurka | b85f8a4 | 2012-04-25 15:48:32 -0700 | [diff] [blame] | 359 | SharedPreferences sp = mContext.getSharedPreferences(spKey, Context.MODE_PRIVATE); |
| 360 | SharedPreferences.Editor editor = sp.edit(); |
| 361 | editor.putBoolean(DB_CREATED_BUT_DEFAULT_WORKSPACE_NOT_LOADED, true); |
| 362 | editor.commit(); |
| 363 | } |
| 364 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 365 | private boolean convertDatabase(SQLiteDatabase db) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 366 | 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] | 367 | boolean converted = false; |
| 368 | |
| 369 | final Uri uri = Uri.parse("content://" + Settings.AUTHORITY + |
| 370 | "/old_favorites?notify=true"); |
| 371 | final ContentResolver resolver = mContext.getContentResolver(); |
| 372 | Cursor cursor = null; |
| 373 | |
| 374 | try { |
| 375 | cursor = resolver.query(uri, null, null, null, null); |
| 376 | } catch (Exception e) { |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 377 | // Ignore |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | // We already have a favorites database in the old provider |
| 381 | if (cursor != null && cursor.getCount() > 0) { |
| 382 | try { |
| 383 | converted = copyFromCursor(db, cursor) > 0; |
| 384 | } finally { |
| 385 | cursor.close(); |
| 386 | } |
| 387 | |
| 388 | if (converted) { |
| 389 | resolver.delete(uri, null, null); |
| 390 | } |
| 391 | } |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 392 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 393 | if (converted) { |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 394 | // Convert widgets from this import into widgets |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 395 | 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] | 396 | convertWidgets(db); |
| 397 | } |
| 398 | |
| 399 | return converted; |
| 400 | } |
| 401 | |
| 402 | private int copyFromCursor(SQLiteDatabase db, Cursor c) { |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 403 | final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 404 | final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT); |
| 405 | final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE); |
| 406 | final int iconTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE); |
| 407 | final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON); |
| 408 | final int iconPackageIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE); |
| 409 | final int iconResourceIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE); |
| 410 | final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER); |
| 411 | final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE); |
| 412 | final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN); |
| 413 | final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX); |
| 414 | final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY); |
| 415 | final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI); |
| 416 | final int displayModeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE); |
| 417 | |
| 418 | ContentValues[] rows = new ContentValues[c.getCount()]; |
| 419 | int i = 0; |
| 420 | while (c.moveToNext()) { |
| 421 | ContentValues values = new ContentValues(c.getColumnCount()); |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 422 | values.put(LauncherSettings.Favorites._ID, c.getLong(idIndex)); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 423 | values.put(LauncherSettings.Favorites.INTENT, c.getString(intentIndex)); |
| 424 | values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex)); |
| 425 | values.put(LauncherSettings.Favorites.ICON_TYPE, c.getInt(iconTypeIndex)); |
| 426 | values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex)); |
| 427 | values.put(LauncherSettings.Favorites.ICON_PACKAGE, c.getString(iconPackageIndex)); |
| 428 | values.put(LauncherSettings.Favorites.ICON_RESOURCE, c.getString(iconResourceIndex)); |
| 429 | values.put(LauncherSettings.Favorites.CONTAINER, c.getInt(containerIndex)); |
| 430 | values.put(LauncherSettings.Favorites.ITEM_TYPE, c.getInt(itemTypeIndex)); |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 431 | values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 432 | values.put(LauncherSettings.Favorites.SCREEN, c.getInt(screenIndex)); |
| 433 | values.put(LauncherSettings.Favorites.CELLX, c.getInt(cellXIndex)); |
| 434 | values.put(LauncherSettings.Favorites.CELLY, c.getInt(cellYIndex)); |
| 435 | values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex)); |
| 436 | values.put(LauncherSettings.Favorites.DISPLAY_MODE, c.getInt(displayModeIndex)); |
| 437 | rows[i++] = values; |
| 438 | } |
| 439 | |
| 440 | db.beginTransaction(); |
| 441 | int total = 0; |
| 442 | try { |
| 443 | int numValues = rows.length; |
| 444 | for (i = 0; i < numValues; i++) { |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 445 | if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, rows[i]) < 0) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 446 | return 0; |
| 447 | } else { |
| 448 | total++; |
| 449 | } |
| 450 | } |
| 451 | db.setTransactionSuccessful(); |
| 452 | } finally { |
| 453 | db.endTransaction(); |
| 454 | } |
| 455 | |
| 456 | return total; |
| 457 | } |
| 458 | |
| 459 | @Override |
| 460 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 461 | if (LOGD) Log.d(TAG, "onUpgrade triggered"); |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 462 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 463 | int version = oldVersion; |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 464 | if (version < 3) { |
| 465 | // upgrade 1,2 -> 3 added appWidgetId column |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 466 | db.beginTransaction(); |
| 467 | try { |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 468 | // Insert new column for holding appWidgetIds |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 469 | db.execSQL("ALTER TABLE favorites " + |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 470 | "ADD COLUMN appWidgetId INTEGER NOT NULL DEFAULT -1;"); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 471 | db.setTransactionSuccessful(); |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 472 | version = 3; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 473 | } catch (SQLException ex) { |
| 474 | // Old version remains, which means we wipe old data |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 475 | Log.e(TAG, ex.getMessage(), ex); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 476 | } finally { |
| 477 | db.endTransaction(); |
| 478 | } |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 479 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 480 | // Convert existing widgets only if table upgrade was successful |
The Android Open Source Project | ca9475f | 2009-03-13 13:04:24 -0700 | [diff] [blame] | 481 | if (version == 3) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 482 | convertWidgets(db); |
| 483 | } |
| 484 | } |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 485 | |
| 486 | if (version < 4) { |
Romain Guy | 7eb9e5e | 2009-12-02 20:10:07 -0800 | [diff] [blame] | 487 | version = 4; |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 488 | } |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 489 | |
Romain Guy | 509cd6a | 2010-03-23 15:10:56 -0700 | [diff] [blame] | 490 | // Where's version 5? |
| 491 | // - Donut and sholes on 2.0 shipped with version 4 of launcher1. |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 492 | // - Passion shipped on 2.1 with version 6 of launcher3 |
Romain Guy | 509cd6a | 2010-03-23 15:10:56 -0700 | [diff] [blame] | 493 | // - Sholes shipped on 2.1r1 (aka Mr. 3) with version 5 of launcher 1 |
| 494 | // but version 5 on there was the updateContactsShortcuts change |
| 495 | // which was version 6 in launcher 2 (first shipped on passion 2.1r1). |
| 496 | // The updateContactsShortcuts change is idempotent, so running it twice |
| 497 | // is okay so we'll do that when upgrading the devices that shipped with it. |
| 498 | if (version < 6) { |
Mike Cleron | 3a2b3f2 | 2009-11-05 17:17:42 -0800 | [diff] [blame] | 499 | // We went from 3 to 5 screens. Move everything 1 to the right |
| 500 | db.beginTransaction(); |
| 501 | try { |
| 502 | db.execSQL("UPDATE favorites SET screen=(screen + 1);"); |
| 503 | db.setTransactionSuccessful(); |
Mike Cleron | 3a2b3f2 | 2009-11-05 17:17:42 -0800 | [diff] [blame] | 504 | } catch (SQLException ex) { |
| 505 | // Old version remains, which means we wipe old data |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 506 | Log.e(TAG, ex.getMessage(), ex); |
Mike Cleron | 3a2b3f2 | 2009-11-05 17:17:42 -0800 | [diff] [blame] | 507 | } finally { |
| 508 | db.endTransaction(); |
| 509 | } |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 510 | |
Romain Guy | 509cd6a | 2010-03-23 15:10:56 -0700 | [diff] [blame] | 511 | // We added the fast track. |
Romain Guy | 7eb9e5e | 2009-12-02 20:10:07 -0800 | [diff] [blame] | 512 | if (updateContactsShortcuts(db)) { |
| 513 | version = 6; |
| 514 | } |
| 515 | } |
Bjorn Bringert | 7984c94 | 2009-12-09 15:38:25 +0000 | [diff] [blame] | 516 | |
| 517 | if (version < 7) { |
| 518 | // Version 7 gets rid of the special search widget. |
| 519 | convertWidgets(db); |
| 520 | version = 7; |
| 521 | } |
| 522 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 523 | if (version < 8) { |
| 524 | // Version 8 (froyo) has the icons all normalized. This should |
| 525 | // already be the case in practice, but we now rely on it and don't |
| 526 | // resample the images each time. |
| 527 | normalizeIcons(db); |
| 528 | version = 8; |
| 529 | } |
| 530 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 531 | if (version < 9) { |
| 532 | // The max id is not yet set at this point (onUpgrade is triggered in the ctor |
| 533 | // 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] | 534 | if (mMaxItemId == -1) { |
| 535 | mMaxItemId = initializeMaxItemId(db); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | // Add default hotseat icons |
Winson Chung | 6d09268 | 2011-11-16 18:43:26 -0800 | [diff] [blame] | 539 | loadFavorites(db, R.xml.update_workspace); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 540 | version = 9; |
| 541 | } |
| 542 | |
Daniel Lehmann | d02402c | 2012-05-14 18:30:53 -0700 | [diff] [blame] | 543 | // We bumped the version three time during JB, once to update the launch flags, once to |
| 544 | // update the override for the default launch animation and once to set the mimetype |
| 545 | // to improve startup performance |
| 546 | if (version < 12) { |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 547 | // Contact shortcuts need a different set of flags to be launched now |
| 548 | // The updateContactsShortcuts change is idempotent, so we can keep using it like |
| 549 | // back in the Donut days |
| 550 | updateContactsShortcuts(db); |
Daniel Lehmann | d02402c | 2012-05-14 18:30:53 -0700 | [diff] [blame] | 551 | version = 12; |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 554 | if (version < 13) { |
| 555 | // With the new shrink-wrapped and re-orderable workspaces, it makes sense |
| 556 | // to persist workspace screens and their relative order. |
| 557 | mMaxScreenId = 0; |
| 558 | |
| 559 | // This will never happen in the wild, but when we switch to using workspace |
| 560 | // screen ids, redo the import from old launcher. |
| 561 | sLoadOldDb = true; |
| 562 | |
| 563 | addWorkspacesTable(db); |
| 564 | version = 13; |
| 565 | } |
| 566 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 567 | if (version != DATABASE_VERSION) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 568 | Log.w(TAG, "Destroying all old data."); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 569 | db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 570 | db.execSQL("DROP TABLE IF EXISTS " + TABLE_WORKSPACE_SCREENS); |
| 571 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 572 | onCreate(db); |
| 573 | } |
| 574 | } |
Romain Guy | 7eb9e5e | 2009-12-02 20:10:07 -0800 | [diff] [blame] | 575 | |
| 576 | private boolean updateContactsShortcuts(SQLiteDatabase db) { |
Romain Guy | 7eb9e5e | 2009-12-02 20:10:07 -0800 | [diff] [blame] | 577 | final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE, |
| 578 | new int[] { Favorites.ITEM_TYPE_SHORTCUT }); |
| 579 | |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 580 | Cursor c = null; |
| 581 | final String actionQuickContact = "com.android.contacts.action.QUICK_CONTACT"; |
Romain Guy | 7eb9e5e | 2009-12-02 20:10:07 -0800 | [diff] [blame] | 582 | db.beginTransaction(); |
| 583 | try { |
| 584 | // Select and iterate through each matching widget |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 585 | c = db.query(TABLE_FAVORITES, |
| 586 | new String[] { Favorites._ID, Favorites.INTENT }, |
Romain Guy | 7eb9e5e | 2009-12-02 20:10:07 -0800 | [diff] [blame] | 587 | selectWhere, null, null, null, null); |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 588 | if (c == null) return false; |
| 589 | |
Romain Guy | 7eb9e5e | 2009-12-02 20:10:07 -0800 | [diff] [blame] | 590 | if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount()); |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 591 | |
Romain Guy | 7eb9e5e | 2009-12-02 20:10:07 -0800 | [diff] [blame] | 592 | final int idIndex = c.getColumnIndex(Favorites._ID); |
| 593 | final int intentIndex = c.getColumnIndex(Favorites.INTENT); |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 594 | |
| 595 | while (c.moveToNext()) { |
Romain Guy | 7eb9e5e | 2009-12-02 20:10:07 -0800 | [diff] [blame] | 596 | long favoriteId = c.getLong(idIndex); |
| 597 | final String intentUri = c.getString(intentIndex); |
| 598 | if (intentUri != null) { |
| 599 | try { |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 600 | final Intent intent = Intent.parseUri(intentUri, 0); |
Romain Guy | 7eb9e5e | 2009-12-02 20:10:07 -0800 | [diff] [blame] | 601 | android.util.Log.d("Home", intent.toString()); |
| 602 | final Uri uri = intent.getData(); |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 603 | if (uri != null) { |
| 604 | final String data = uri.toString(); |
| 605 | if ((Intent.ACTION_VIEW.equals(intent.getAction()) || |
| 606 | actionQuickContact.equals(intent.getAction())) && |
| 607 | (data.startsWith("content://contacts/people/") || |
| 608 | data.startsWith("content://com.android.contacts/" + |
| 609 | "contacts/lookup/"))) { |
Romain Guy | 7eb9e5e | 2009-12-02 20:10:07 -0800 | [diff] [blame] | 610 | |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 611 | final Intent newIntent = new Intent(actionQuickContact); |
| 612 | // When starting from the launcher, start in a new, cleared task |
| 613 | // CLEAR_WHEN_TASK_RESET cannot reset the root of a task, so we |
| 614 | // clear the whole thing preemptively here since |
| 615 | // QuickContactActivity will finish itself when launching other |
| 616 | // detail activities. |
| 617 | newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | |
| 618 | Intent.FLAG_ACTIVITY_CLEAR_TASK); |
Winson Chung | 2672ff9 | 2012-05-04 16:22:30 -0700 | [diff] [blame] | 619 | newIntent.putExtra( |
| 620 | Launcher.INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION, true); |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 621 | newIntent.setData(uri); |
Daniel Lehmann | d02402c | 2012-05-14 18:30:53 -0700 | [diff] [blame] | 622 | // Determine the type and also put that in the shortcut |
| 623 | // (that can speed up launch a bit) |
| 624 | newIntent.setDataAndType(uri, newIntent.resolveType(mContext)); |
Romain Guy | 7eb9e5e | 2009-12-02 20:10:07 -0800 | [diff] [blame] | 625 | |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 626 | final ContentValues values = new ContentValues(); |
| 627 | values.put(LauncherSettings.Favorites.INTENT, |
| 628 | newIntent.toUri(0)); |
| 629 | |
| 630 | String updateWhere = Favorites._ID + "=" + favoriteId; |
| 631 | db.update(TABLE_FAVORITES, values, updateWhere, null); |
| 632 | } |
Romain Guy | 7eb9e5e | 2009-12-02 20:10:07 -0800 | [diff] [blame] | 633 | } |
| 634 | } catch (RuntimeException ex) { |
| 635 | Log.e(TAG, "Problem upgrading shortcut", ex); |
| 636 | } catch (URISyntaxException e) { |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 637 | Log.e(TAG, "Problem upgrading shortcut", e); |
Romain Guy | 7eb9e5e | 2009-12-02 20:10:07 -0800 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | } |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 641 | |
Romain Guy | 7eb9e5e | 2009-12-02 20:10:07 -0800 | [diff] [blame] | 642 | db.setTransactionSuccessful(); |
| 643 | } catch (SQLException ex) { |
| 644 | Log.w(TAG, "Problem while upgrading contacts", ex); |
| 645 | return false; |
| 646 | } finally { |
| 647 | db.endTransaction(); |
| 648 | if (c != null) { |
| 649 | c.close(); |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | return true; |
| 654 | } |
| 655 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 656 | private void normalizeIcons(SQLiteDatabase db) { |
| 657 | Log.d(TAG, "normalizing icons"); |
| 658 | |
Joe Onorato | 346e129 | 2010-02-18 10:34:24 -0500 | [diff] [blame] | 659 | db.beginTransaction(); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 660 | Cursor c = null; |
Joe Onorato | 9690b39 | 2010-03-23 17:34:37 -0400 | [diff] [blame] | 661 | SQLiteStatement update = null; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 662 | try { |
| 663 | boolean logged = false; |
Joe Onorato | 9690b39 | 2010-03-23 17:34:37 -0400 | [diff] [blame] | 664 | update = db.compileStatement("UPDATE favorites " |
Jeff Hamilton | eaf77d6 | 2010-02-13 00:08:17 -0600 | [diff] [blame] | 665 | + "SET icon=? WHERE _id=?"); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 666 | |
| 667 | c = db.rawQuery("SELECT _id, icon FROM favorites WHERE iconType=" + |
| 668 | Favorites.ICON_TYPE_BITMAP, null); |
| 669 | |
| 670 | final int idIndex = c.getColumnIndexOrThrow(Favorites._ID); |
| 671 | final int iconIndex = c.getColumnIndexOrThrow(Favorites.ICON); |
| 672 | |
| 673 | while (c.moveToNext()) { |
| 674 | long id = c.getLong(idIndex); |
| 675 | byte[] data = c.getBlob(iconIndex); |
| 676 | try { |
| 677 | Bitmap bitmap = Utilities.resampleIconBitmap( |
| 678 | BitmapFactory.decodeByteArray(data, 0, data.length), |
| 679 | mContext); |
| 680 | if (bitmap != null) { |
| 681 | update.bindLong(1, id); |
| 682 | data = ItemInfo.flattenBitmap(bitmap); |
| 683 | if (data != null) { |
| 684 | update.bindBlob(2, data); |
| 685 | update.execute(); |
| 686 | } |
| 687 | bitmap.recycle(); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 688 | } |
| 689 | } catch (Exception e) { |
| 690 | if (!logged) { |
| 691 | Log.e(TAG, "Failed normalizing icon " + id, e); |
| 692 | } else { |
| 693 | Log.e(TAG, "Also failed normalizing icon " + id); |
| 694 | } |
| 695 | logged = true; |
| 696 | } |
| 697 | } |
Bjorn Bringert | 3a928e4 | 2010-02-19 11:15:40 +0000 | [diff] [blame] | 698 | db.setTransactionSuccessful(); |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 699 | } catch (SQLException ex) { |
| 700 | Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex); |
| 701 | } finally { |
| 702 | db.endTransaction(); |
Joe Onorato | 9690b39 | 2010-03-23 17:34:37 -0400 | [diff] [blame] | 703 | if (update != null) { |
| 704 | update.close(); |
| 705 | } |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 706 | if (c != null) { |
| 707 | c.close(); |
| 708 | } |
| 709 | } |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | // Generates a new ID to use for an object in your database. This method should be only |
| 713 | // called from the main UI thread. As an exception, we do call it when we call the |
| 714 | // constructor from the worker thread; however, this doesn't extend until after the |
| 715 | // constructor is called, and we only pass a reference to LauncherProvider to LauncherApp |
| 716 | // after that point |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 717 | public long generateNewItemId() { |
| 718 | if (mMaxItemId < 0) { |
| 719 | throw new RuntimeException("Error: max item id was not initialized"); |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 720 | } |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 721 | mMaxItemId += 1; |
| 722 | return mMaxItemId; |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 723 | } |
| 724 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 725 | private long initializeMaxItemId(SQLiteDatabase db) { |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 726 | Cursor c = db.rawQuery("SELECT MAX(_id) FROM favorites", null); |
| 727 | |
| 728 | // get the result |
| 729 | final int maxIdIndex = 0; |
| 730 | long id = -1; |
| 731 | if (c != null && c.moveToNext()) { |
| 732 | id = c.getLong(maxIdIndex); |
| 733 | } |
Michael Jurka | 5130e40 | 2011-10-13 04:55:35 -0700 | [diff] [blame] | 734 | if (c != null) { |
| 735 | c.close(); |
| 736 | } |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 737 | |
| 738 | if (id == -1) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 739 | throw new RuntimeException("Error: could not query max item id"); |
| 740 | } |
| 741 | |
| 742 | return id; |
| 743 | } |
| 744 | |
| 745 | // Generates a new ID to use for an workspace screen in your database. This method |
| 746 | // should be only called from the main UI thread. As an exception, we do call it when we |
| 747 | // call the constructor from the worker thread; however, this doesn't extend until after the |
| 748 | // constructor is called, and we only pass a reference to LauncherProvider to LauncherApp |
| 749 | // after that point |
| 750 | public long generateNewScreenId() { |
| 751 | if (mMaxScreenId < 0) { |
| 752 | throw new RuntimeException("Error: max screen id was not initialized"); |
| 753 | } |
| 754 | mMaxScreenId += 1; |
| 755 | return mMaxScreenId; |
| 756 | } |
| 757 | |
| 758 | public void updateMaxScreenId(long maxScreenId) { |
| 759 | mMaxScreenId = maxScreenId; |
| 760 | } |
| 761 | |
| 762 | private long initializeMaxScreenId(SQLiteDatabase db) { |
| 763 | Cursor c = db.rawQuery("SELECT MAX(" + LauncherSettings.WorkspaceScreens._ID + ") FROM " + TABLE_WORKSPACE_SCREENS, null); |
| 764 | |
| 765 | // get the result |
| 766 | final int maxIdIndex = 0; |
| 767 | long id = -1; |
| 768 | if (c != null && c.moveToNext()) { |
| 769 | id = c.getLong(maxIdIndex); |
| 770 | } |
| 771 | if (c != null) { |
| 772 | c.close(); |
| 773 | } |
| 774 | |
| 775 | if (id == -1) { |
| 776 | throw new RuntimeException("Error: could not query max screen id"); |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | return id; |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 780 | } |
| 781 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 782 | /** |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 783 | * Upgrade existing clock and photo frame widgets into their new widget |
Bjorn Bringert | 93c4576 | 2009-12-16 13:19:47 +0000 | [diff] [blame] | 784 | * equivalents. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 785 | */ |
| 786 | private void convertWidgets(SQLiteDatabase db) { |
Bjorn Bringert | 3425134 | 2009-12-15 13:33:11 +0000 | [diff] [blame] | 787 | final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 788 | final int[] bindSources = new int[] { |
| 789 | Favorites.ITEM_TYPE_WIDGET_CLOCK, |
| 790 | Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME, |
Bjorn Bringert | 7984c94 | 2009-12-09 15:38:25 +0000 | [diff] [blame] | 791 | Favorites.ITEM_TYPE_WIDGET_SEARCH, |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 792 | }; |
Bjorn Bringert | 7984c94 | 2009-12-09 15:38:25 +0000 | [diff] [blame] | 793 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 794 | final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE, bindSources); |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 795 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 796 | Cursor c = null; |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 797 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 798 | db.beginTransaction(); |
| 799 | try { |
| 800 | // Select and iterate through each matching widget |
Bjorn Bringert | 7984c94 | 2009-12-09 15:38:25 +0000 | [diff] [blame] | 801 | 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] | 802 | selectWhere, null, null, null, null); |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 803 | |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 804 | if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount()); |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 805 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 806 | final ContentValues values = new ContentValues(); |
| 807 | while (c != null && c.moveToNext()) { |
| 808 | long favoriteId = c.getLong(0); |
Bjorn Bringert | 7984c94 | 2009-12-09 15:38:25 +0000 | [diff] [blame] | 809 | int favoriteType = c.getInt(1); |
| 810 | |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 811 | // Allocate and update database with new appWidgetId |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 812 | try { |
The Android Open Source Project | 7376fae | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 813 | int appWidgetId = mAppWidgetHost.allocateAppWidgetId(); |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 814 | |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 815 | if (LOGD) { |
| 816 | Log.d(TAG, "allocated appWidgetId=" + appWidgetId |
| 817 | + " for favoriteId=" + favoriteId); |
| 818 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 819 | values.clear(); |
Bjorn Bringert | 7984c94 | 2009-12-09 15:38:25 +0000 | [diff] [blame] | 820 | values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET); |
| 821 | values.put(Favorites.APPWIDGET_ID, appWidgetId); |
| 822 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 823 | // Original widgets might not have valid spans when upgrading |
Bjorn Bringert | 7984c94 | 2009-12-09 15:38:25 +0000 | [diff] [blame] | 824 | if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) { |
| 825 | values.put(LauncherSettings.Favorites.SPANX, 4); |
| 826 | values.put(LauncherSettings.Favorites.SPANY, 1); |
| 827 | } else { |
| 828 | values.put(LauncherSettings.Favorites.SPANX, 2); |
| 829 | values.put(LauncherSettings.Favorites.SPANY, 2); |
| 830 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 831 | |
| 832 | String updateWhere = Favorites._ID + "=" + favoriteId; |
| 833 | db.update(TABLE_FAVORITES, values, updateWhere, null); |
Bjorn Bringert | 3425134 | 2009-12-15 13:33:11 +0000 | [diff] [blame] | 834 | |
Bjorn Bringert | 3425134 | 2009-12-15 13:33:11 +0000 | [diff] [blame] | 835 | if (favoriteType == Favorites.ITEM_TYPE_WIDGET_CLOCK) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 836 | // TODO: check return value |
| 837 | appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, |
Bjorn Bringert | 3425134 | 2009-12-15 13:33:11 +0000 | [diff] [blame] | 838 | new ComponentName("com.android.alarmclock", |
| 839 | "com.android.alarmclock.AnalogAppWidgetProvider")); |
| 840 | } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 841 | // TODO: check return value |
| 842 | appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, |
Bjorn Bringert | 3425134 | 2009-12-15 13:33:11 +0000 | [diff] [blame] | 843 | new ComponentName("com.android.camera", |
| 844 | "com.android.camera.PhotoAppWidgetProvider")); |
| 845 | } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 846 | // TODO: check return value |
| 847 | appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, |
Bjorn Bringert | cd8fec0 | 2010-01-14 13:26:43 +0000 | [diff] [blame] | 848 | getSearchWidgetProvider()); |
Bjorn Bringert | 3425134 | 2009-12-15 13:33:11 +0000 | [diff] [blame] | 849 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 850 | } catch (RuntimeException ex) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 851 | Log.e(TAG, "Problem allocating appWidgetId", ex); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 852 | } |
| 853 | } |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 854 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 855 | db.setTransactionSuccessful(); |
| 856 | } catch (SQLException ex) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 857 | 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] | 858 | } finally { |
| 859 | db.endTransaction(); |
| 860 | if (c != null) { |
| 861 | c.close(); |
| 862 | } |
| 863 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 864 | } |
| 865 | |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 866 | private static final void beginDocument(XmlPullParser parser, String firstElementName) |
| 867 | throws XmlPullParserException, IOException { |
| 868 | int type; |
Michael Jurka | 9bc8eba | 2012-05-21 20:36:44 -0700 | [diff] [blame] | 869 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 870 | && type != XmlPullParser.END_DOCUMENT) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 871 | ; |
| 872 | } |
| 873 | |
Michael Jurka | 9bc8eba | 2012-05-21 20:36:44 -0700 | [diff] [blame] | 874 | if (type != XmlPullParser.START_TAG) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 875 | throw new XmlPullParserException("No start tag found"); |
| 876 | } |
| 877 | |
| 878 | if (!parser.getName().equals(firstElementName)) { |
| 879 | throw new XmlPullParserException("Unexpected start tag: found " + parser.getName() + |
| 880 | ", expected " + firstElementName); |
| 881 | } |
| 882 | } |
| 883 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 884 | /** |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 885 | * Loads the default set of favorite packages from an xml file. |
| 886 | * |
| 887 | * @param db The database to write the values into |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 888 | * @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] | 889 | */ |
Winson Chung | 6d09268 | 2011-11-16 18:43:26 -0800 | [diff] [blame] | 890 | private int loadFavorites(SQLiteDatabase db, int workspaceResourceId) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 891 | Intent intent = new Intent(Intent.ACTION_MAIN, null); |
| 892 | intent.addCategory(Intent.CATEGORY_LAUNCHER); |
| 893 | ContentValues values = new ContentValues(); |
| 894 | |
| 895 | PackageManager packageManager = mContext.getPackageManager(); |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 896 | int allAppsButtonRank = |
| 897 | mContext.getResources().getInteger(R.integer.hotseat_all_apps_index); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 898 | int i = 0; |
| 899 | try { |
Winson Chung | 6d09268 | 2011-11-16 18:43:26 -0800 | [diff] [blame] | 900 | XmlResourceParser parser = mContext.getResources().getXml(workspaceResourceId); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 901 | AttributeSet attrs = Xml.asAttributeSet(parser); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 902 | beginDocument(parser, TAG_FAVORITES); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 903 | |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 904 | final int depth = parser.getDepth(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 905 | |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 906 | int type; |
| 907 | while (((type = parser.next()) != XmlPullParser.END_TAG || |
| 908 | parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) { |
| 909 | |
| 910 | if (type != XmlPullParser.START_TAG) { |
| 911 | continue; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 912 | } |
| 913 | |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 914 | boolean added = false; |
| 915 | final String name = parser.getName(); |
| 916 | |
| 917 | TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.Favorite); |
| 918 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 919 | long container = LauncherSettings.Favorites.CONTAINER_DESKTOP; |
| 920 | if (a.hasValue(R.styleable.Favorite_container)) { |
| 921 | container = Long.valueOf(a.getString(R.styleable.Favorite_container)); |
| 922 | } |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 923 | |
Winson Chung | 6d09268 | 2011-11-16 18:43:26 -0800 | [diff] [blame] | 924 | String screen = a.getString(R.styleable.Favorite_screen); |
| 925 | String x = a.getString(R.styleable.Favorite_x); |
| 926 | String y = a.getString(R.styleable.Favorite_y); |
| 927 | |
| 928 | // If we are adding to the hotseat, the screen is used as the position in the |
| 929 | // hotseat. This screen can't be at position 0 because AllApps is in the |
| 930 | // zeroth position. |
Andrew Flynn | 0dca1ec | 2012-02-29 13:33:22 -0800 | [diff] [blame] | 931 | if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT |
| 932 | && Integer.valueOf(screen) == allAppsButtonRank) { |
Winson Chung | 6d09268 | 2011-11-16 18:43:26 -0800 | [diff] [blame] | 933 | throw new RuntimeException("Invalid screen position for hotseat item"); |
| 934 | } |
| 935 | |
| 936 | values.clear(); |
| 937 | values.put(LauncherSettings.Favorites.CONTAINER, container); |
| 938 | values.put(LauncherSettings.Favorites.SCREEN, screen); |
| 939 | values.put(LauncherSettings.Favorites.CELLX, x); |
| 940 | values.put(LauncherSettings.Favorites.CELLY, y); |
| 941 | |
| 942 | if (TAG_FAVORITE.equals(name)) { |
| 943 | long id = addAppShortcut(db, values, a, packageManager, intent); |
| 944 | added = id >= 0; |
| 945 | } else if (TAG_SEARCH.equals(name)) { |
| 946 | added = addSearchWidget(db, values); |
| 947 | } else if (TAG_CLOCK.equals(name)) { |
| 948 | added = addClockWidget(db, values); |
| 949 | } else if (TAG_APPWIDGET.equals(name)) { |
Winson Chung | b3302ae | 2012-05-01 10:19:14 -0700 | [diff] [blame] | 950 | added = addAppWidget(parser, attrs, type, db, values, a, packageManager); |
Winson Chung | 6d09268 | 2011-11-16 18:43:26 -0800 | [diff] [blame] | 951 | } else if (TAG_SHORTCUT.equals(name)) { |
| 952 | long id = addUriShortcut(db, values, a); |
| 953 | added = id >= 0; |
| 954 | } else if (TAG_FOLDER.equals(name)) { |
| 955 | String title; |
| 956 | int titleResId = a.getResourceId(R.styleable.Favorite_title, -1); |
| 957 | if (titleResId != -1) { |
| 958 | title = mContext.getResources().getString(titleResId); |
| 959 | } else { |
| 960 | title = mContext.getResources().getString(R.string.folder_name); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 961 | } |
Winson Chung | 6d09268 | 2011-11-16 18:43:26 -0800 | [diff] [blame] | 962 | values.put(LauncherSettings.Favorites.TITLE, title); |
| 963 | long folderId = addFolder(db, values); |
| 964 | added = folderId >= 0; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 965 | |
Winson Chung | 6d09268 | 2011-11-16 18:43:26 -0800 | [diff] [blame] | 966 | ArrayList<Long> folderItems = new ArrayList<Long>(); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 967 | |
Winson Chung | 6d09268 | 2011-11-16 18:43:26 -0800 | [diff] [blame] | 968 | int folderDepth = parser.getDepth(); |
| 969 | while ((type = parser.next()) != XmlPullParser.END_TAG || |
| 970 | parser.getDepth() > folderDepth) { |
| 971 | if (type != XmlPullParser.START_TAG) { |
| 972 | continue; |
| 973 | } |
| 974 | final String folder_item_name = parser.getName(); |
| 975 | |
| 976 | TypedArray ar = mContext.obtainStyledAttributes(attrs, |
| 977 | R.styleable.Favorite); |
| 978 | values.clear(); |
| 979 | values.put(LauncherSettings.Favorites.CONTAINER, folderId); |
| 980 | |
| 981 | if (TAG_FAVORITE.equals(folder_item_name) && folderId >= 0) { |
| 982 | long id = |
| 983 | addAppShortcut(db, values, ar, packageManager, intent); |
| 984 | if (id >= 0) { |
| 985 | folderItems.add(id); |
| 986 | } |
| 987 | } else if (TAG_SHORTCUT.equals(folder_item_name) && folderId >= 0) { |
| 988 | long id = addUriShortcut(db, values, ar); |
| 989 | if (id >= 0) { |
| 990 | folderItems.add(id); |
| 991 | } |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 992 | } else { |
Winson Chung | 6d09268 | 2011-11-16 18:43:26 -0800 | [diff] [blame] | 993 | throw new RuntimeException("Folders can " + |
| 994 | "contain only shortcuts"); |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 995 | } |
Winson Chung | 6d09268 | 2011-11-16 18:43:26 -0800 | [diff] [blame] | 996 | ar.recycle(); |
| 997 | } |
| 998 | // We can only have folders with >= 2 items, so we need to remove the |
| 999 | // folder and clean up if less than 2 items were included, or some |
| 1000 | // failed to add, and less than 2 were actually added |
| 1001 | if (folderItems.size() < 2 && folderId >= 0) { |
| 1002 | // We just delete the folder and any items that made it |
| 1003 | deleteId(db, folderId); |
| 1004 | if (folderItems.size() > 0) { |
| 1005 | deleteId(db, folderItems.get(0)); |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 1006 | } |
Winson Chung | 6d09268 | 2011-11-16 18:43:26 -0800 | [diff] [blame] | 1007 | added = false; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 1008 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1009 | } |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1010 | if (added) i++; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1011 | a.recycle(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1012 | } |
| 1013 | } catch (XmlPullParserException e) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 1014 | Log.w(TAG, "Got exception parsing favorites.", e); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1015 | } catch (IOException e) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 1016 | Log.w(TAG, "Got exception parsing favorites.", e); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 1017 | } catch (RuntimeException e) { |
| 1018 | Log.w(TAG, "Got exception parsing favorites.", e); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1019 | } |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1020 | |
| 1021 | return i; |
| 1022 | } |
| 1023 | |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 1024 | private long addAppShortcut(SQLiteDatabase db, ContentValues values, TypedArray a, |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1025 | PackageManager packageManager, Intent intent) { |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 1026 | long id = -1; |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1027 | ActivityInfo info; |
| 1028 | String packageName = a.getString(R.styleable.Favorite_packageName); |
| 1029 | String className = a.getString(R.styleable.Favorite_className); |
| 1030 | try { |
Romain Guy | 693599f | 2010-03-23 10:58:18 -0700 | [diff] [blame] | 1031 | ComponentName cn; |
| 1032 | try { |
| 1033 | cn = new ComponentName(packageName, className); |
| 1034 | info = packageManager.getActivityInfo(cn, 0); |
| 1035 | } catch (PackageManager.NameNotFoundException nnfe) { |
| 1036 | String[] packages = packageManager.currentToCanonicalPackageNames( |
| 1037 | new String[] { packageName }); |
| 1038 | cn = new ComponentName(packages[0], className); |
| 1039 | info = packageManager.getActivityInfo(cn, 0); |
| 1040 | } |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1041 | id = generateNewItemId(); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1042 | intent.setComponent(cn); |
Romain Guy | 693599f | 2010-03-23 10:58:18 -0700 | [diff] [blame] | 1043 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | |
| 1044 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
Romain Guy | 1ce1a24 | 2009-06-23 17:34:54 -0700 | [diff] [blame] | 1045 | values.put(Favorites.INTENT, intent.toUri(0)); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1046 | values.put(Favorites.TITLE, info.loadLabel(packageManager).toString()); |
| 1047 | values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPLICATION); |
| 1048 | values.put(Favorites.SPANX, 1); |
| 1049 | values.put(Favorites.SPANY, 1); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1050 | values.put(Favorites._ID, generateNewItemId()); |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 1051 | if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values) < 0) { |
| 1052 | return -1; |
| 1053 | } |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1054 | } catch (PackageManager.NameNotFoundException e) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 1055 | Log.w(TAG, "Unable to add favorite: " + packageName + |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1056 | "/" + className, e); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1057 | } |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 1058 | return id; |
| 1059 | } |
| 1060 | |
| 1061 | private long addFolder(SQLiteDatabase db, ContentValues values) { |
| 1062 | values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_FOLDER); |
| 1063 | values.put(Favorites.SPANX, 1); |
| 1064 | values.put(Favorites.SPANY, 1); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1065 | long id = generateNewItemId(); |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 1066 | values.put(Favorites._ID, id); |
| 1067 | if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values) <= 0) { |
| 1068 | return -1; |
| 1069 | } else { |
| 1070 | return id; |
| 1071 | } |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1072 | } |
| 1073 | |
Bjorn Bringert | cd8fec0 | 2010-01-14 13:26:43 +0000 | [diff] [blame] | 1074 | private ComponentName getSearchWidgetProvider() { |
| 1075 | SearchManager searchManager = |
| 1076 | (SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE); |
| 1077 | ComponentName searchComponent = searchManager.getGlobalSearchActivity(); |
| 1078 | if (searchComponent == null) return null; |
| 1079 | return getProviderInPackage(searchComponent.getPackageName()); |
| 1080 | } |
| 1081 | |
| 1082 | /** |
| 1083 | * Gets an appwidget provider from the given package. If the package contains more than |
| 1084 | * one appwidget provider, an arbitrary one is returned. |
| 1085 | */ |
| 1086 | private ComponentName getProviderInPackage(String packageName) { |
| 1087 | AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext); |
| 1088 | List<AppWidgetProviderInfo> providers = appWidgetManager.getInstalledProviders(); |
| 1089 | if (providers == null) return null; |
| 1090 | final int providerCount = providers.size(); |
| 1091 | for (int i = 0; i < providerCount; i++) { |
| 1092 | ComponentName provider = providers.get(i).provider; |
| 1093 | if (provider != null && provider.getPackageName().equals(packageName)) { |
| 1094 | return provider; |
| 1095 | } |
| 1096 | } |
| 1097 | return null; |
| 1098 | } |
| 1099 | |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1100 | private boolean addSearchWidget(SQLiteDatabase db, ContentValues values) { |
Bjorn Bringert | cd8fec0 | 2010-01-14 13:26:43 +0000 | [diff] [blame] | 1101 | ComponentName cn = getSearchWidgetProvider(); |
Winson Chung | b3302ae | 2012-05-01 10:19:14 -0700 | [diff] [blame] | 1102 | return addAppWidget(db, values, cn, 4, 1, null); |
The Android Open Source Project | f96811c | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | private boolean addClockWidget(SQLiteDatabase db, ContentValues values) { |
Bjorn Bringert | 3425134 | 2009-12-15 13:33:11 +0000 | [diff] [blame] | 1106 | ComponentName cn = new ComponentName("com.android.alarmclock", |
| 1107 | "com.android.alarmclock.AnalogAppWidgetProvider"); |
Winson Chung | b3302ae | 2012-05-01 10:19:14 -0700 | [diff] [blame] | 1108 | return addAppWidget(db, values, cn, 2, 2, null); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1109 | } |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 1110 | |
Winson Chung | b3302ae | 2012-05-01 10:19:14 -0700 | [diff] [blame] | 1111 | private boolean addAppWidget(XmlResourceParser parser, AttributeSet attrs, int type, |
| 1112 | SQLiteDatabase db, ContentValues values, TypedArray a, |
| 1113 | PackageManager packageManager) throws XmlPullParserException, IOException { |
Romain Guy | 693599f | 2010-03-23 10:58:18 -0700 | [diff] [blame] | 1114 | |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 1115 | String packageName = a.getString(R.styleable.Favorite_packageName); |
| 1116 | String className = a.getString(R.styleable.Favorite_className); |
| 1117 | |
| 1118 | if (packageName == null || className == null) { |
| 1119 | return false; |
| 1120 | } |
Romain Guy | 693599f | 2010-03-23 10:58:18 -0700 | [diff] [blame] | 1121 | |
| 1122 | boolean hasPackage = true; |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 1123 | ComponentName cn = new ComponentName(packageName, className); |
Romain Guy | 693599f | 2010-03-23 10:58:18 -0700 | [diff] [blame] | 1124 | try { |
| 1125 | packageManager.getReceiverInfo(cn, 0); |
| 1126 | } catch (Exception e) { |
| 1127 | String[] packages = packageManager.currentToCanonicalPackageNames( |
| 1128 | new String[] { packageName }); |
| 1129 | cn = new ComponentName(packages[0], className); |
| 1130 | try { |
| 1131 | packageManager.getReceiverInfo(cn, 0); |
| 1132 | } catch (Exception e1) { |
| 1133 | hasPackage = false; |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | if (hasPackage) { |
| 1138 | int spanX = a.getInt(R.styleable.Favorite_spanX, 0); |
| 1139 | int spanY = a.getInt(R.styleable.Favorite_spanY, 0); |
Winson Chung | b3302ae | 2012-05-01 10:19:14 -0700 | [diff] [blame] | 1140 | |
| 1141 | // Read the extras |
| 1142 | Bundle extras = new Bundle(); |
| 1143 | int widgetDepth = parser.getDepth(); |
| 1144 | while ((type = parser.next()) != XmlPullParser.END_TAG || |
| 1145 | parser.getDepth() > widgetDepth) { |
| 1146 | if (type != XmlPullParser.START_TAG) { |
| 1147 | continue; |
| 1148 | } |
| 1149 | |
| 1150 | TypedArray ar = mContext.obtainStyledAttributes(attrs, R.styleable.Extra); |
| 1151 | if (TAG_EXTRA.equals(parser.getName())) { |
| 1152 | String key = ar.getString(R.styleable.Extra_key); |
| 1153 | String value = ar.getString(R.styleable.Extra_value); |
| 1154 | if (key != null && value != null) { |
| 1155 | extras.putString(key, value); |
| 1156 | } else { |
| 1157 | throw new RuntimeException("Widget extras must have a key and value"); |
| 1158 | } |
| 1159 | } else { |
| 1160 | throw new RuntimeException("Widgets can contain only extras"); |
| 1161 | } |
| 1162 | ar.recycle(); |
| 1163 | } |
| 1164 | |
| 1165 | return addAppWidget(db, values, cn, spanX, spanY, extras); |
Romain Guy | 693599f | 2010-03-23 10:58:18 -0700 | [diff] [blame] | 1166 | } |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 1167 | |
Romain Guy | 693599f | 2010-03-23 10:58:18 -0700 | [diff] [blame] | 1168 | return false; |
Bjorn Bringert | 7984c94 | 2009-12-09 15:38:25 +0000 | [diff] [blame] | 1169 | } |
Bjorn Bringert | 7984c94 | 2009-12-09 15:38:25 +0000 | [diff] [blame] | 1170 | private boolean addAppWidget(SQLiteDatabase db, ContentValues values, ComponentName cn, |
Winson Chung | b3302ae | 2012-05-01 10:19:14 -0700 | [diff] [blame] | 1171 | int spanX, int spanY, Bundle extras) { |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 1172 | boolean allocatedAppWidgets = false; |
| 1173 | final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext); |
| 1174 | |
| 1175 | try { |
| 1176 | int appWidgetId = mAppWidgetHost.allocateAppWidgetId(); |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 1177 | |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 1178 | values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET); |
Bjorn Bringert | 7984c94 | 2009-12-09 15:38:25 +0000 | [diff] [blame] | 1179 | values.put(Favorites.SPANX, spanX); |
| 1180 | values.put(Favorites.SPANY, spanY); |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 1181 | values.put(Favorites.APPWIDGET_ID, appWidgetId); |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1182 | values.put(Favorites._ID, generateNewItemId()); |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 1183 | dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values); |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 1184 | |
| 1185 | allocatedAppWidgets = true; |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 1186 | |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1187 | // TODO: need to check return value |
| 1188 | appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, cn); |
Winson Chung | b3302ae | 2012-05-01 10:19:14 -0700 | [diff] [blame] | 1189 | |
| 1190 | // Send a broadcast to configure the widget |
| 1191 | if (extras != null && !extras.isEmpty()) { |
| 1192 | Intent intent = new Intent(ACTION_APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE); |
| 1193 | intent.setComponent(cn); |
| 1194 | intent.putExtras(extras); |
| 1195 | intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); |
| 1196 | mContext.sendBroadcast(intent); |
| 1197 | } |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 1198 | } catch (RuntimeException ex) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 1199 | Log.e(TAG, "Problem allocating appWidgetId", ex); |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 1200 | } |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 1201 | |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 1202 | return allocatedAppWidgets; |
| 1203 | } |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 1204 | |
| 1205 | private long addUriShortcut(SQLiteDatabase db, ContentValues values, |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 1206 | TypedArray a) { |
| 1207 | Resources r = mContext.getResources(); |
| 1208 | |
| 1209 | final int iconResId = a.getResourceId(R.styleable.Favorite_icon, 0); |
| 1210 | final int titleResId = a.getResourceId(R.styleable.Favorite_title, 0); |
| 1211 | |
Romain Guy | 7eb9e5e | 2009-12-02 20:10:07 -0800 | [diff] [blame] | 1212 | Intent intent; |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 1213 | String uri = null; |
| 1214 | try { |
| 1215 | uri = a.getString(R.styleable.Favorite_uri); |
| 1216 | intent = Intent.parseUri(uri, 0); |
| 1217 | } catch (URISyntaxException e) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 1218 | Log.w(TAG, "Shortcut has malformed uri: " + uri); |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 1219 | return -1; // Oh well |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | if (iconResId == 0 || titleResId == 0) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 1223 | Log.w(TAG, "Shortcut is missing title or icon resource ID"); |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 1224 | return -1; |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 1225 | } |
| 1226 | |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1227 | long id = generateNewItemId(); |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 1228 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 1229 | values.put(Favorites.INTENT, intent.toUri(0)); |
| 1230 | values.put(Favorites.TITLE, r.getString(titleResId)); |
| 1231 | values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_SHORTCUT); |
| 1232 | values.put(Favorites.SPANX, 1); |
| 1233 | values.put(Favorites.SPANY, 1); |
| 1234 | values.put(Favorites.ICON_TYPE, Favorites.ICON_TYPE_RESOURCE); |
| 1235 | values.put(Favorites.ICON_PACKAGE, mContext.getPackageName()); |
| 1236 | values.put(Favorites.ICON_RESOURCE, r.getResourceName(iconResId)); |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 1237 | values.put(Favorites._ID, id); |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 1238 | |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 1239 | if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values) < 0) { |
| 1240 | return -1; |
| 1241 | } |
| 1242 | return id; |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 1243 | } |
| 1244 | } |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 1245 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1246 | /** |
| 1247 | * Build a query string that will match any row where the column matches |
| 1248 | * anything in the values list. |
| 1249 | */ |
| 1250 | static String buildOrWhereString(String column, int[] values) { |
| 1251 | StringBuilder selectWhere = new StringBuilder(); |
| 1252 | for (int i = values.length - 1; i >= 0; i--) { |
| 1253 | selectWhere.append(column).append("=").append(values[i]); |
| 1254 | if (i > 0) { |
| 1255 | selectWhere.append(" OR "); |
| 1256 | } |
| 1257 | } |
| 1258 | return selectWhere.toString(); |
| 1259 | } |
| 1260 | |
| 1261 | static class SqlArguments { |
| 1262 | public final String table; |
| 1263 | public final String where; |
| 1264 | public final String[] args; |
| 1265 | |
| 1266 | SqlArguments(Uri url, String where, String[] args) { |
| 1267 | if (url.getPathSegments().size() == 1) { |
| 1268 | this.table = url.getPathSegments().get(0); |
| 1269 | this.where = where; |
| 1270 | this.args = args; |
| 1271 | } else if (url.getPathSegments().size() != 2) { |
| 1272 | throw new IllegalArgumentException("Invalid URI: " + url); |
| 1273 | } else if (!TextUtils.isEmpty(where)) { |
| 1274 | throw new UnsupportedOperationException("WHERE clause not supported: " + url); |
| 1275 | } else { |
| 1276 | this.table = url.getPathSegments().get(0); |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 1277 | this.where = "_id=" + ContentUris.parseId(url); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1278 | this.args = null; |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | SqlArguments(Uri url) { |
| 1283 | if (url.getPathSegments().size() == 1) { |
| 1284 | table = url.getPathSegments().get(0); |
| 1285 | where = null; |
| 1286 | args = null; |
| 1287 | } else { |
| 1288 | throw new IllegalArgumentException("Invalid URI: " + url); |
| 1289 | } |
| 1290 | } |
| 1291 | } |
| 1292 | } |