blob: e43b727aa4448085aee97658e5d4e38c20932754 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
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 Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Bjorn Bringertcd8fec02010-01-14 13:26:43 +000019import android.app.SearchManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070020import android.appwidget.AppWidgetHost;
Mike Cleronb87bd162009-10-30 16:36:56 -070021import android.appwidget.AppWidgetManager;
Bjorn Bringertcd8fec02010-01-14 13:26:43 +000022import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.content.ComponentName;
Adam Cohen228da5a2011-07-27 22:23:47 -070024import android.content.ContentProvider;
Yura085c8532014-02-11 15:15:29 +000025import android.content.ContentProviderOperation;
26import android.content.ContentProviderResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.content.ContentResolver;
Adam Cohen228da5a2011-07-27 22:23:47 -070028import android.content.ContentUris;
29import android.content.ContentValues;
30import android.content.Context;
31import android.content.Intent;
Yura085c8532014-02-11 15:15:29 +000032import android.content.OperationApplicationException;
Michael Jurkab85f8a42012-04-25 15:48:32 -070033import android.content.SharedPreferences;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.pm.ActivityInfo;
Jason Monk41314972014-03-03 16:11:30 -050035import android.content.pm.ApplicationInfo;
Jeff Sharkey5aeef582014-04-14 13:26:42 -070036import android.content.pm.PackageInfo;
Adam Cohen228da5a2011-07-27 22:23:47 -070037import android.content.pm.PackageManager;
Jason Monk41314972014-03-03 16:11:30 -050038import android.content.pm.ResolveInfo;
Adam Cohen228da5a2011-07-27 22:23:47 -070039import android.content.res.Resources;
40import android.content.res.TypedArray;
41import android.content.res.XmlResourceParser;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.database.Cursor;
43import android.database.SQLException;
Adam Cohen228da5a2011-07-27 22:23:47 -070044import android.database.sqlite.SQLiteDatabase;
45import android.database.sqlite.SQLiteOpenHelper;
46import android.database.sqlite.SQLiteQueryBuilder;
47import android.database.sqlite.SQLiteStatement;
Joe Onorato0589f0f2010-02-08 13:44:00 -080048import android.graphics.Bitmap;
49import android.graphics.BitmapFactory;
Adam Cohen228da5a2011-07-27 22:23:47 -070050import android.net.Uri;
Winson Chungb3302ae2012-05-01 10:19:14 -070051import android.os.Bundle;
Adam Cohen228da5a2011-07-27 22:23:47 -070052import android.provider.Settings;
53import android.text.TextUtils;
54import android.util.AttributeSet;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.util.Log;
Dan Sandlerab5fa3a2014-03-06 23:48:04 -050056import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.util.Xml;
Adam Cohen228da5a2011-07-27 22:23:47 -070058
Daniel Sandler325dc232013-06-05 22:57:57 -040059import com.android.launcher3.LauncherSettings.Favorites;
Chris Wrene523e702013-10-09 10:36:55 -040060import com.android.launcher3.config.ProviderConfig;
Michael Jurka8b805b12012-04-18 14:23:14 -070061
62import org.xmlpull.v1.XmlPullParser;
63import org.xmlpull.v1.XmlPullParserException;
64
Dan Sandlerd5024042014-01-09 15:01:33 -050065import java.io.File;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import java.io.IOException;
Mike Cleronb87bd162009-10-30 16:36:56 -070067import java.net.URISyntaxException;
Adam Cohen228da5a2011-07-27 22:23:47 -070068import java.util.ArrayList;
Dan Sandlerd5024042014-01-09 15:01:33 -050069import java.util.HashSet;
Bjorn Bringertcd8fec02010-01-14 13:26:43 +000070import java.util.List;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072public class LauncherProvider extends ContentProvider {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080073 private static final String TAG = "Launcher.LauncherProvider";
74 private static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075
76 private static final String DATABASE_NAME = "launcher.db";
Winson Chung3d503fb2011-07-13 17:25:49 -070077
Adam Cohenf9c14de2014-04-17 18:20:45 -070078 private static final int DATABASE_VERSION = 19;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079
Adam Cohene25af792013-06-06 23:08:25 -070080 static final String OLD_AUTHORITY = "com.android.launcher2.settings";
Chris Wrene523e702013-10-09 10:36:55 -040081 static final String AUTHORITY = ProviderConfig.AUTHORITY;
Winson Chung3d503fb2011-07-13 17:25:49 -070082
Dan Sandlerf0b8dac2013-11-19 12:21:25 -050083 // Should we attempt to load anything from the com.android.launcher2 provider?
Dan Sandlerd5024042014-01-09 15:01:33 -050084 static final boolean IMPORT_LAUNCHER2_DATABASE = false;
Dan Sandlerf0b8dac2013-11-19 12:21:25 -050085
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 static final String TABLE_FAVORITES = "favorites";
Adam Cohendcd297f2013-06-18 13:13:40 -070087 static final String TABLE_WORKSPACE_SCREENS = "workspaceScreens";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088 static final String PARAMETER_NOTIFY = "notify";
Winson Chungc763c4e2013-07-19 13:49:06 -070089 static final String UPGRADED_FROM_OLD_DATABASE =
90 "UPGRADED_FROM_OLD_DATABASE";
91 static final String EMPTY_DATABASE_CREATED =
92 "EMPTY_DATABASE_CREATED";
Michael Jurka45355c42012-10-08 13:21:35 +020093 static final String DEFAULT_WORKSPACE_RESOURCE_ID =
94 "DEFAULT_WORKSPACE_RESOURCE_ID";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095
Winson Chungb3302ae2012-05-01 10:19:14 -070096 private static final String ACTION_APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE =
Adam Cohene25af792013-06-06 23:08:25 -070097 "com.android.launcher.action.APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE";
Winson Chungb3302ae2012-05-01 10:19:14 -070098
Anjali Koppal67e7cae2014-03-13 12:14:12 -070099 private LauncherProviderChangeListener mListener;
100
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700101 /**
Romain Guy73b979d2009-06-09 12:57:21 -0700102 * {@link Uri} triggered at any registered {@link android.database.ContentObserver} when
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700103 * {@link AppWidgetHost#deleteHost()} is called during database creation.
104 * Use this to recall {@link AppWidgetHost#startListening()} if needed.
105 */
106 static final Uri CONTENT_APPWIDGET_RESET_URI =
107 Uri.parse("content://" + AUTHORITY + "/appWidgetReset");
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700108
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700109 private DatabaseHelper mOpenHelper;
Winson Chungc763c4e2013-07-19 13:49:06 -0700110 private static boolean sJustLoadedFromOldDb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111
112 @Override
113 public boolean onCreate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400114 final Context context = getContext();
115 mOpenHelper = new DatabaseHelper(context);
116 LauncherAppState.setLauncherProvider(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117 return true;
118 }
119
Winson Chung0b560dd2014-01-21 13:00:26 -0800120 public boolean wasNewDbCreated() {
121 return mOpenHelper.wasNewDbCreated();
122 }
123
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700124 public void setLauncherProviderChangeListener(LauncherProviderChangeListener listener) {
125 mListener = listener;
126 }
127
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128 @Override
129 public String getType(Uri uri) {
130 SqlArguments args = new SqlArguments(uri, null, null);
131 if (TextUtils.isEmpty(args.where)) {
132 return "vnd.android.cursor.dir/" + args.table;
133 } else {
134 return "vnd.android.cursor.item/" + args.table;
135 }
136 }
137
138 @Override
139 public Cursor query(Uri uri, String[] projection, String selection,
140 String[] selectionArgs, String sortOrder) {
141
142 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
143 SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
144 qb.setTables(args.table);
145
Romain Guy73b979d2009-06-09 12:57:21 -0700146 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 Cursor result = qb.query(db, projection, args.where, args.args, null, null, sortOrder);
148 result.setNotificationUri(getContext().getContentResolver(), uri);
149
150 return result;
151 }
152
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700153 private static long dbInsertAndCheck(DatabaseHelper helper,
154 SQLiteDatabase db, String table, String nullColumnHack, ContentValues values) {
Dan Sandlerd5024042014-01-09 15:01:33 -0500155 if (values == null) {
156 throw new RuntimeException("Error: attempting to insert null values");
157 }
Chris Wren5dee7af2013-12-20 17:22:11 -0500158 if (!values.containsKey(LauncherSettings.BaseLauncherColumns._ID)) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700159 throw new RuntimeException("Error: attempting to add item without specifying an id");
160 }
Chris Wren5dee7af2013-12-20 17:22:11 -0500161 helper.checkId(table, values);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700162 return db.insert(table, nullColumnHack, values);
163 }
164
Adam Cohen228da5a2011-07-27 22:23:47 -0700165 private static void deleteId(SQLiteDatabase db, long id) {
166 Uri uri = LauncherSettings.Favorites.getContentUri(id, false);
167 SqlArguments args = new SqlArguments(uri, null, null);
168 db.delete(args.table, args.where, args.args);
169 }
170
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 @Override
172 public Uri insert(Uri uri, ContentValues initialValues) {
173 SqlArguments args = new SqlArguments(uri);
174
175 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
Chris Wren1ada10d2013-09-13 18:01:38 -0400176 addModifiedTime(initialValues);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700177 final long rowId = dbInsertAndCheck(mOpenHelper, db, args.table, null, initialValues);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 if (rowId <= 0) return null;
179
180 uri = ContentUris.withAppendedId(uri, rowId);
181 sendNotify(uri);
182
183 return uri;
184 }
185
186 @Override
187 public int bulkInsert(Uri uri, ContentValues[] values) {
188 SqlArguments args = new SqlArguments(uri);
189
190 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
191 db.beginTransaction();
192 try {
193 int numValues = values.length;
194 for (int i = 0; i < numValues; i++) {
Chris Wren1ada10d2013-09-13 18:01:38 -0400195 addModifiedTime(values[i]);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700196 if (dbInsertAndCheck(mOpenHelper, db, args.table, null, values[i]) < 0) {
197 return 0;
198 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 }
200 db.setTransactionSuccessful();
201 } finally {
202 db.endTransaction();
203 }
204
205 sendNotify(uri);
206 return values.length;
207 }
208
209 @Override
Yura085c8532014-02-11 15:15:29 +0000210 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
211 throws OperationApplicationException {
212 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
213 db.beginTransaction();
214 try {
215 ContentProviderResult[] result = super.applyBatch(operations);
216 db.setTransactionSuccessful();
217 return result;
218 } finally {
219 db.endTransaction();
220 }
221 }
222
223 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224 public int delete(Uri uri, String selection, String[] selectionArgs) {
225 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
226
227 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
228 int count = db.delete(args.table, args.where, args.args);
229 if (count > 0) sendNotify(uri);
230
231 return count;
232 }
233
234 @Override
235 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
236 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
237
Chris Wren1ada10d2013-09-13 18:01:38 -0400238 addModifiedTime(values);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
240 int count = db.update(args.table, values, args.where, args.args);
241 if (count > 0) sendNotify(uri);
242
243 return count;
244 }
245
246 private void sendNotify(Uri uri) {
247 String notify = uri.getQueryParameter(PARAMETER_NOTIFY);
248 if (notify == null || "true".equals(notify)) {
249 getContext().getContentResolver().notifyChange(uri, null);
250 }
Chris Wren1ada10d2013-09-13 18:01:38 -0400251
252 // always notify the backup agent
Chris Wren92aa4232013-10-04 11:29:36 -0400253 LauncherBackupAgentHelper.dataChanged(getContext());
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700254 if (mListener != null) {
255 mListener.onLauncherProviderChange();
256 }
Chris Wren1ada10d2013-09-13 18:01:38 -0400257 }
258
259 private void addModifiedTime(ContentValues values) {
260 values.put(LauncherSettings.ChangeLogColumns.MODIFIED, System.currentTimeMillis());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261 }
262
Adam Cohendcd297f2013-06-18 13:13:40 -0700263 public long generateNewItemId() {
264 return mOpenHelper.generateNewItemId();
265 }
266
Winson Chungc763c4e2013-07-19 13:49:06 -0700267 public void updateMaxItemId(long id) {
268 mOpenHelper.updateMaxItemId(id);
269 }
270
Adam Cohendcd297f2013-06-18 13:13:40 -0700271 public long generateNewScreenId() {
272 return mOpenHelper.generateNewScreenId();
273 }
274
275 // This is only required one time while loading the workspace during the
276 // upgrade path, and should never be called from anywhere else.
277 public void updateMaxScreenId(long maxScreenId) {
278 mOpenHelper.updateMaxScreenId(maxScreenId);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700279 }
280
Brian Muramatsu5524b492012-10-02 16:55:54 -0700281 /**
Adam Cohene25af792013-06-06 23:08:25 -0700282 * @param Should we load the old db for upgrade? first run only.
283 */
Winson Chungc763c4e2013-07-19 13:49:06 -0700284 synchronized public boolean justLoadedOldDb() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400285 String spKey = LauncherAppState.getSharedPreferencesKey();
Adam Cohene25af792013-06-06 23:08:25 -0700286 SharedPreferences sp = getContext().getSharedPreferences(spKey, Context.MODE_PRIVATE);
287
Winson Chungc763c4e2013-07-19 13:49:06 -0700288 boolean loadedOldDb = false || sJustLoadedFromOldDb;
Adam Cohendcd297f2013-06-18 13:13:40 -0700289
Winson Chungc763c4e2013-07-19 13:49:06 -0700290 sJustLoadedFromOldDb = false;
291 if (sp.getBoolean(UPGRADED_FROM_OLD_DATABASE, false)) {
Adam Cohene25af792013-06-06 23:08:25 -0700292
293 SharedPreferences.Editor editor = sp.edit();
Winson Chungc763c4e2013-07-19 13:49:06 -0700294 editor.remove(UPGRADED_FROM_OLD_DATABASE);
Adam Cohene25af792013-06-06 23:08:25 -0700295 editor.commit();
Winson Chungc763c4e2013-07-19 13:49:06 -0700296 loadedOldDb = true;
Adam Cohene25af792013-06-06 23:08:25 -0700297 }
Winson Chungc763c4e2013-07-19 13:49:06 -0700298 return loadedOldDb;
Adam Cohene25af792013-06-06 23:08:25 -0700299 }
300
301 /**
Brian Muramatsu5524b492012-10-02 16:55:54 -0700302 * @param workspaceResId that can be 0 to use default or non-zero for specific resource
303 */
Michael Jurka45355c42012-10-08 13:21:35 +0200304 synchronized public void loadDefaultFavoritesIfNecessary(int origWorkspaceResId) {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400305 String spKey = LauncherAppState.getSharedPreferencesKey();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700306 SharedPreferences sp = getContext().getSharedPreferences(spKey, Context.MODE_PRIVATE);
Adam Cohene25af792013-06-06 23:08:25 -0700307
Winson Chungc763c4e2013-07-19 13:49:06 -0700308 if (sp.getBoolean(EMPTY_DATABASE_CREATED, false)) {
Chris Wren5dee7af2013-12-20 17:22:11 -0500309 Log.d(TAG, "loading default workspace");
Michael Jurka45355c42012-10-08 13:21:35 +0200310 int workspaceResId = origWorkspaceResId;
311
Brian Muramatsu5524b492012-10-02 16:55:54 -0700312 // Use default workspace resource if none provided
313 if (workspaceResId == 0) {
Nilesh Agrawalda41ea62013-12-09 14:17:49 -0800314 workspaceResId =
315 sp.getInt(DEFAULT_WORKSPACE_RESOURCE_ID, getDefaultWorkspaceResourceId());
Brian Muramatsu5524b492012-10-02 16:55:54 -0700316 }
317
Michael Jurkab85f8a42012-04-25 15:48:32 -0700318 // Populate favorites table with initial favorites
319 SharedPreferences.Editor editor = sp.edit();
Winson Chungc763c4e2013-07-19 13:49:06 -0700320 editor.remove(EMPTY_DATABASE_CREATED);
Michael Jurka45355c42012-10-08 13:21:35 +0200321 if (origWorkspaceResId != 0) {
322 editor.putInt(DEFAULT_WORKSPACE_RESOURCE_ID, origWorkspaceResId);
323 }
Adam Cohene25af792013-06-06 23:08:25 -0700324
Brian Muramatsu5524b492012-10-02 16:55:54 -0700325 mOpenHelper.loadFavorites(mOpenHelper.getWritableDatabase(), workspaceResId);
Winson Chungc763c4e2013-07-19 13:49:06 -0700326 mOpenHelper.setFlagJustLoadedOldDb();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700327 editor.commit();
328 }
329 }
330
Dan Sandlerd5024042014-01-09 15:01:33 -0500331 public void migrateLauncher2Shortcuts() {
332 mOpenHelper.migrateLauncher2Shortcuts(mOpenHelper.getWritableDatabase(),
Jason Monk0bfcceb2014-03-21 15:42:06 -0400333 Uri.parse(getContext().getString(R.string.old_launcher_provider_uri)));
Dan Sandlerd5024042014-01-09 15:01:33 -0500334 }
335
Nilesh Agrawalda41ea62013-12-09 14:17:49 -0800336 private static int getDefaultWorkspaceResourceId() {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800337 if (LauncherAppState.isDisableAllApps()) {
Nilesh Agrawalda41ea62013-12-09 14:17:49 -0800338 return R.xml.default_workspace_no_all_apps;
339 } else {
340 return R.xml.default_workspace;
341 }
342 }
343
Winson Chungc763c4e2013-07-19 13:49:06 -0700344 private static interface ContentValuesCallback {
345 public void onRow(ContentValues values);
346 }
347
Adam Cohen6dbe0492013-12-02 17:00:14 -0800348 private static boolean shouldImportLauncher2Database(Context context) {
349 boolean isTablet = context.getResources().getBoolean(R.bool.is_tablet);
350
351 // We don't import the old databse for tablets, as the grid size has changed.
352 return !isTablet && IMPORT_LAUNCHER2_DATABASE;
353 }
354
Dan Sandlerd5024042014-01-09 15:01:33 -0500355 public void deleteDatabase() {
356 // Are you sure? (y/n)
357 final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
Dan Sandler2b471742014-01-21 14:14:41 -0500358 final File dbFile = new File(db.getPath());
Dan Sandlerd5024042014-01-09 15:01:33 -0500359 mOpenHelper.close();
Dan Sandler2b471742014-01-21 14:14:41 -0500360 if (dbFile.exists()) {
361 SQLiteDatabase.deleteDatabase(dbFile);
362 }
Dan Sandlerd5024042014-01-09 15:01:33 -0500363 mOpenHelper = new DatabaseHelper(getContext());
364 }
365
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366 private static class DatabaseHelper extends SQLiteOpenHelper {
Jason Monk41314972014-03-03 16:11:30 -0500367 private static final String TAG_RESOLVE = "resolve";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368 private static final String TAG_FAVORITES = "favorites";
369 private static final String TAG_FAVORITE = "favorite";
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700370 private static final String TAG_CLOCK = "clock";
371 private static final String TAG_SEARCH = "search";
Mike Cleronb87bd162009-10-30 16:36:56 -0700372 private static final String TAG_APPWIDGET = "appwidget";
373 private static final String TAG_SHORTCUT = "shortcut";
Adam Cohen228da5a2011-07-27 22:23:47 -0700374 private static final String TAG_FOLDER = "folder";
Jeff Sharkey5aeef582014-04-14 13:26:42 -0700375 private static final String TAG_PARTNER_FOLDER = "partner-folder";
Winson Chungb3302ae2012-05-01 10:19:14 -0700376 private static final String TAG_EXTRA = "extra";
Daniel Sandler57dac262013-10-03 13:28:36 -0400377 private static final String TAG_INCLUDE = "include";
Winson Chung3d503fb2011-07-13 17:25:49 -0700378
Jeff Sharkey5aeef582014-04-14 13:26:42 -0700379 private static final String ATTR_TITLE = "title";
380 private static final String ATTR_ICON = "icon";
381 private static final String ATTR_URI = "uri";
382 private static final String ATTR_PACKAGE_NAME = "packageName";
383 private static final String ATTR_CLASS_NAME = "className";
384
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 private final Context mContext;
Jeff Sharkey5aeef582014-04-14 13:26:42 -0700386 private final PackageManager mPackageManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700387 private final AppWidgetHost mAppWidgetHost;
Adam Cohendcd297f2013-06-18 13:13:40 -0700388 private long mMaxItemId = -1;
389 private long mMaxScreenId = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390
Winson Chung0b560dd2014-01-21 13:00:26 -0800391 private boolean mNewDbCreated = false;
392
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 DatabaseHelper(Context context) {
394 super(context, DATABASE_NAME, null, DATABASE_VERSION);
395 mContext = context;
Jeff Sharkey5aeef582014-04-14 13:26:42 -0700396 mPackageManager = context.getPackageManager();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700397 mAppWidgetHost = new AppWidgetHost(context, Launcher.APPWIDGET_HOST_ID);
Winson Chung3d503fb2011-07-13 17:25:49 -0700398
399 // In the case where neither onCreate nor onUpgrade gets called, we read the maxId from
400 // the DB here
Adam Cohendcd297f2013-06-18 13:13:40 -0700401 if (mMaxItemId == -1) {
402 mMaxItemId = initializeMaxItemId(getWritableDatabase());
403 }
404 if (mMaxScreenId == -1) {
405 mMaxScreenId = initializeMaxScreenId(getWritableDatabase());
Winson Chung3d503fb2011-07-13 17:25:49 -0700406 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 }
408
Winson Chung0b560dd2014-01-21 13:00:26 -0800409 public boolean wasNewDbCreated() {
410 return mNewDbCreated;
411 }
412
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700413 /**
414 * Send notification that we've deleted the {@link AppWidgetHost},
415 * probably as part of the initial database creation. The receiver may
416 * want to re-call {@link AppWidgetHost#startListening()} to ensure
417 * callbacks are correctly set.
418 */
419 private void sendAppWidgetResetNotify() {
420 final ContentResolver resolver = mContext.getContentResolver();
421 resolver.notifyChange(CONTENT_APPWIDGET_RESET_URI, null);
422 }
423
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800424 @Override
425 public void onCreate(SQLiteDatabase db) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800426 if (LOGD) Log.d(TAG, "creating new launcher database");
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700427
Adam Cohendcd297f2013-06-18 13:13:40 -0700428 mMaxItemId = 1;
429 mMaxScreenId = 0;
Winson Chung0b560dd2014-01-21 13:00:26 -0800430 mNewDbCreated = true;
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700431
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800432 db.execSQL("CREATE TABLE favorites (" +
433 "_id INTEGER PRIMARY KEY," +
434 "title TEXT," +
435 "intent TEXT," +
436 "container INTEGER," +
437 "screen INTEGER," +
438 "cellX INTEGER," +
439 "cellY INTEGER," +
440 "spanX INTEGER," +
441 "spanY INTEGER," +
442 "itemType INTEGER," +
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700443 "appWidgetId INTEGER NOT NULL DEFAULT -1," +
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444 "isShortcut INTEGER," +
445 "iconType INTEGER," +
446 "iconPackage TEXT," +
447 "iconResource TEXT," +
448 "icon BLOB," +
449 "uri TEXT," +
Chris Wrend5e66bf2013-09-16 14:02:29 -0400450 "displayMode INTEGER," +
Chris Wren1ada10d2013-09-13 18:01:38 -0400451 "appWidgetProvider TEXT," +
Chris Wrenf4d08112014-01-16 18:13:56 -0500452 "modified INTEGER NOT NULL DEFAULT 0," +
453 "restored INTEGER NOT NULL DEFAULT 0" +
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 ");");
Adam Cohendcd297f2013-06-18 13:13:40 -0700455 addWorkspacesTable(db);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700457 // Database was just created, so wipe any previous widgets
458 if (mAppWidgetHost != null) {
459 mAppWidgetHost.deleteHost();
Jeffrey Sharkey2bbcae12009-03-31 14:37:57 -0700460 sendAppWidgetResetNotify();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700462
Adam Cohen6dbe0492013-12-02 17:00:14 -0800463 if (shouldImportLauncher2Database(mContext)) {
Dan Sandlerf0b8dac2013-11-19 12:21:25 -0500464 // Try converting the old database
465 ContentValuesCallback permuteScreensCb = new ContentValuesCallback() {
466 public void onRow(ContentValues values) {
467 int container = values.getAsInteger(LauncherSettings.Favorites.CONTAINER);
468 if (container == Favorites.CONTAINER_DESKTOP) {
469 int screen = values.getAsInteger(LauncherSettings.Favorites.SCREEN);
470 screen = (int) upgradeLauncherDb_permuteScreens(screen);
471 values.put(LauncherSettings.Favorites.SCREEN, screen);
472 }
473 }
474 };
475 Uri uri = Uri.parse("content://" + Settings.AUTHORITY +
476 "/old_favorites?notify=true");
477 if (!convertDatabase(db, uri, permuteScreensCb, true)) {
478 // Try and upgrade from the Launcher2 db
Jason Monk0bfcceb2014-03-21 15:42:06 -0400479 uri = Uri.parse(mContext.getString(R.string.old_launcher_provider_uri));
Dan Sandlerf0b8dac2013-11-19 12:21:25 -0500480 if (!convertDatabase(db, uri, permuteScreensCb, false)) {
481 // If we fail, then set a flag to load the default workspace
482 setFlagEmptyDbCreated();
483 return;
Winson Chungc763c4e2013-07-19 13:49:06 -0700484 }
485 }
Dan Sandlerf0b8dac2013-11-19 12:21:25 -0500486 // Right now, in non-default workspace cases, we want to run the final
487 // upgrade code (ie. to fix workspace screen indices -> ids, etc.), so
488 // set that flag too.
489 setFlagJustLoadedOldDb();
490 } else {
491 // Fresh and clean launcher DB.
492 mMaxItemId = initializeMaxItemId(db);
493 setFlagEmptyDbCreated();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800494 }
495 }
496
Adam Cohendcd297f2013-06-18 13:13:40 -0700497 private void addWorkspacesTable(SQLiteDatabase db) {
498 db.execSQL("CREATE TABLE " + TABLE_WORKSPACE_SCREENS + " (" +
499 LauncherSettings.WorkspaceScreens._ID + " INTEGER," +
Chris Wren1ada10d2013-09-13 18:01:38 -0400500 LauncherSettings.WorkspaceScreens.SCREEN_RANK + " INTEGER," +
501 LauncherSettings.ChangeLogColumns.MODIFIED + " INTEGER NOT NULL DEFAULT 0" +
Adam Cohendcd297f2013-06-18 13:13:40 -0700502 ");");
503 }
504
Adam Cohen119285e2014-04-02 16:59:08 -0700505 private void removeOrphanedItems(SQLiteDatabase db) {
Adam Cohenf9c14de2014-04-17 18:20:45 -0700506 // Delete items directly on the workspace who's screen id doesn't exist
507 // "DELETE FROM favorites WHERE screen NOT IN (SELECT _id FROM workspaceScreens)
508 // AND container = -100"
509 String removeOrphanedDesktopItems = "DELETE FROM " + TABLE_FAVORITES +
510 " WHERE " +
Adam Cohen119285e2014-04-02 16:59:08 -0700511 LauncherSettings.Favorites.SCREEN + " NOT IN (SELECT " +
Adam Cohenf9c14de2014-04-17 18:20:45 -0700512 LauncherSettings.WorkspaceScreens._ID + " FROM " + TABLE_WORKSPACE_SCREENS + ")" +
513 " AND " +
514 LauncherSettings.Favorites.CONTAINER + " = " +
515 LauncherSettings.Favorites.CONTAINER_DESKTOP;
516 db.execSQL(removeOrphanedDesktopItems);
517
518 // Delete items contained in folders which no longer exist (after above statement)
519 // "DELETE FROM favorites WHERE container <> -100 AND container <> -101 AND container
520 // NOT IN (SELECT _id FROM favorites WHERE itemType = 2)"
521 String removeOrphanedFolderItems = "DELETE FROM " + TABLE_FAVORITES +
522 " WHERE " +
523 LauncherSettings.Favorites.CONTAINER + " <> " +
524 LauncherSettings.Favorites.CONTAINER_DESKTOP +
525 " AND "
526 + LauncherSettings.Favorites.CONTAINER + " <> " +
527 LauncherSettings.Favorites.CONTAINER_HOTSEAT +
528 " AND "
529 + LauncherSettings.Favorites.CONTAINER + " NOT IN (SELECT " +
530 LauncherSettings.Favorites._ID + " FROM " + TABLE_FAVORITES +
531 " WHERE " + LauncherSettings.Favorites.ITEM_TYPE + " = " +
532 LauncherSettings.Favorites.ITEM_TYPE_FOLDER + ")";
533 db.execSQL(removeOrphanedFolderItems);
Adam Cohen119285e2014-04-02 16:59:08 -0700534 }
535
Winson Chungc763c4e2013-07-19 13:49:06 -0700536 private void setFlagJustLoadedOldDb() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400537 String spKey = LauncherAppState.getSharedPreferencesKey();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700538 SharedPreferences sp = mContext.getSharedPreferences(spKey, Context.MODE_PRIVATE);
539 SharedPreferences.Editor editor = sp.edit();
Winson Chungc763c4e2013-07-19 13:49:06 -0700540 editor.putBoolean(UPGRADED_FROM_OLD_DATABASE, true);
541 editor.putBoolean(EMPTY_DATABASE_CREATED, false);
Michael Jurkab85f8a42012-04-25 15:48:32 -0700542 editor.commit();
543 }
544
Winson Chungc763c4e2013-07-19 13:49:06 -0700545 private void setFlagEmptyDbCreated() {
546 String spKey = LauncherAppState.getSharedPreferencesKey();
547 SharedPreferences sp = mContext.getSharedPreferences(spKey, Context.MODE_PRIVATE);
548 SharedPreferences.Editor editor = sp.edit();
549 editor.putBoolean(EMPTY_DATABASE_CREATED, true);
550 editor.putBoolean(UPGRADED_FROM_OLD_DATABASE, false);
551 editor.commit();
552 }
553
554 // We rearrange the screens from the old launcher
555 // 12345 -> 34512
556 private long upgradeLauncherDb_permuteScreens(long screen) {
557 if (screen >= 2) {
558 return screen - 2;
559 } else {
560 return screen + 3;
561 }
562 }
563
564 private boolean convertDatabase(SQLiteDatabase db, Uri uri,
565 ContentValuesCallback cb, boolean deleteRows) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800566 if (LOGD) Log.d(TAG, "converting database from an older format, but not onUpgrade");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800567 boolean converted = false;
568
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800569 final ContentResolver resolver = mContext.getContentResolver();
570 Cursor cursor = null;
571
572 try {
573 cursor = resolver.query(uri, null, null, null, null);
574 } catch (Exception e) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700575 // Ignore
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800576 }
577
578 // We already have a favorites database in the old provider
Winson Chungc763c4e2013-07-19 13:49:06 -0700579 if (cursor != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800580 try {
Winson Chungc763c4e2013-07-19 13:49:06 -0700581 if (cursor.getCount() > 0) {
582 converted = copyFromCursor(db, cursor, cb) > 0;
583 if (converted && deleteRows) {
584 resolver.delete(uri, null, null);
585 }
586 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800587 } finally {
588 cursor.close();
589 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800590 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700591
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800592 if (converted) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700593 // Convert widgets from this import into widgets
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800594 if (LOGD) Log.d(TAG, "converted and now triggering widget upgrade");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800595 convertWidgets(db);
Winson Chungc763c4e2013-07-19 13:49:06 -0700596
597 // Update max item id
598 mMaxItemId = initializeMaxItemId(db);
599 if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800600 }
601
602 return converted;
603 }
604
Winson Chungc763c4e2013-07-19 13:49:06 -0700605 private int copyFromCursor(SQLiteDatabase db, Cursor c, ContentValuesCallback cb) {
Romain Guy73b979d2009-06-09 12:57:21 -0700606 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800607 final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
608 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
609 final int iconTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
610 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
611 final int iconPackageIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
612 final int iconResourceIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
613 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
614 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
615 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
616 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
617 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
618 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
619 final int displayModeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
620
621 ContentValues[] rows = new ContentValues[c.getCount()];
622 int i = 0;
623 while (c.moveToNext()) {
624 ContentValues values = new ContentValues(c.getColumnCount());
Romain Guy73b979d2009-06-09 12:57:21 -0700625 values.put(LauncherSettings.Favorites._ID, c.getLong(idIndex));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 values.put(LauncherSettings.Favorites.INTENT, c.getString(intentIndex));
627 values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex));
628 values.put(LauncherSettings.Favorites.ICON_TYPE, c.getInt(iconTypeIndex));
629 values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex));
630 values.put(LauncherSettings.Favorites.ICON_PACKAGE, c.getString(iconPackageIndex));
631 values.put(LauncherSettings.Favorites.ICON_RESOURCE, c.getString(iconResourceIndex));
632 values.put(LauncherSettings.Favorites.CONTAINER, c.getInt(containerIndex));
633 values.put(LauncherSettings.Favorites.ITEM_TYPE, c.getInt(itemTypeIndex));
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700634 values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800635 values.put(LauncherSettings.Favorites.SCREEN, c.getInt(screenIndex));
636 values.put(LauncherSettings.Favorites.CELLX, c.getInt(cellXIndex));
637 values.put(LauncherSettings.Favorites.CELLY, c.getInt(cellYIndex));
638 values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex));
639 values.put(LauncherSettings.Favorites.DISPLAY_MODE, c.getInt(displayModeIndex));
Winson Chungc763c4e2013-07-19 13:49:06 -0700640 if (cb != null) {
641 cb.onRow(values);
642 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800643 rows[i++] = values;
644 }
645
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646 int total = 0;
Winson Chungc763c4e2013-07-19 13:49:06 -0700647 if (i > 0) {
648 db.beginTransaction();
649 try {
650 int numValues = rows.length;
651 for (i = 0; i < numValues; i++) {
652 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, rows[i]) < 0) {
653 return 0;
654 } else {
655 total++;
656 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 }
Winson Chungc763c4e2013-07-19 13:49:06 -0700658 db.setTransactionSuccessful();
659 } finally {
660 db.endTransaction();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 }
663
664 return total;
665 }
666
667 @Override
668 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Winson Chungc763c4e2013-07-19 13:49:06 -0700669 if (LOGD) Log.d(TAG, "onUpgrade triggered: " + oldVersion);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700670
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 int version = oldVersion;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700672 if (version < 3) {
673 // upgrade 1,2 -> 3 added appWidgetId column
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800674 db.beginTransaction();
675 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700676 // Insert new column for holding appWidgetIds
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 db.execSQL("ALTER TABLE favorites " +
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700678 "ADD COLUMN appWidgetId INTEGER NOT NULL DEFAULT -1;");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 db.setTransactionSuccessful();
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700680 version = 3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 } catch (SQLException ex) {
682 // Old version remains, which means we wipe old data
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800683 Log.e(TAG, ex.getMessage(), ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 } finally {
685 db.endTransaction();
686 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700687
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800688 // Convert existing widgets only if table upgrade was successful
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700689 if (version == 3) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 convertWidgets(db);
691 }
692 }
Romain Guy73b979d2009-06-09 12:57:21 -0700693
694 if (version < 4) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800695 version = 4;
Romain Guy73b979d2009-06-09 12:57:21 -0700696 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700697
Romain Guy509cd6a2010-03-23 15:10:56 -0700698 // Where's version 5?
699 // - Donut and sholes on 2.0 shipped with version 4 of launcher1.
Daniel Sandler325dc232013-06-05 22:57:57 -0400700 // - Passion shipped on 2.1 with version 6 of launcher3
Romain Guy509cd6a2010-03-23 15:10:56 -0700701 // - Sholes shipped on 2.1r1 (aka Mr. 3) with version 5 of launcher 1
702 // but version 5 on there was the updateContactsShortcuts change
703 // which was version 6 in launcher 2 (first shipped on passion 2.1r1).
704 // The updateContactsShortcuts change is idempotent, so running it twice
705 // is okay so we'll do that when upgrading the devices that shipped with it.
706 if (version < 6) {
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800707 // We went from 3 to 5 screens. Move everything 1 to the right
708 db.beginTransaction();
709 try {
710 db.execSQL("UPDATE favorites SET screen=(screen + 1);");
711 db.setTransactionSuccessful();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800712 } catch (SQLException ex) {
713 // Old version remains, which means we wipe old data
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800714 Log.e(TAG, ex.getMessage(), ex);
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800715 } finally {
716 db.endTransaction();
717 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700718
Romain Guy509cd6a2010-03-23 15:10:56 -0700719 // We added the fast track.
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800720 if (updateContactsShortcuts(db)) {
721 version = 6;
722 }
723 }
Bjorn Bringert7984c942009-12-09 15:38:25 +0000724
725 if (version < 7) {
726 // Version 7 gets rid of the special search widget.
727 convertWidgets(db);
728 version = 7;
729 }
730
Joe Onorato0589f0f2010-02-08 13:44:00 -0800731 if (version < 8) {
732 // Version 8 (froyo) has the icons all normalized. This should
733 // already be the case in practice, but we now rely on it and don't
734 // resample the images each time.
735 normalizeIcons(db);
736 version = 8;
737 }
738
Winson Chung3d503fb2011-07-13 17:25:49 -0700739 if (version < 9) {
740 // The max id is not yet set at this point (onUpgrade is triggered in the ctor
741 // before it gets a change to get set, so we need to read it here when we use it)
Adam Cohendcd297f2013-06-18 13:13:40 -0700742 if (mMaxItemId == -1) {
743 mMaxItemId = initializeMaxItemId(db);
Winson Chung3d503fb2011-07-13 17:25:49 -0700744 }
745
746 // Add default hotseat icons
Winson Chung6d092682011-11-16 18:43:26 -0800747 loadFavorites(db, R.xml.update_workspace);
Winson Chung3d503fb2011-07-13 17:25:49 -0700748 version = 9;
749 }
750
Daniel Lehmannd02402c2012-05-14 18:30:53 -0700751 // We bumped the version three time during JB, once to update the launch flags, once to
752 // update the override for the default launch animation and once to set the mimetype
753 // to improve startup performance
754 if (version < 12) {
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700755 // Contact shortcuts need a different set of flags to be launched now
756 // The updateContactsShortcuts change is idempotent, so we can keep using it like
757 // back in the Donut days
758 updateContactsShortcuts(db);
Daniel Lehmannd02402c2012-05-14 18:30:53 -0700759 version = 12;
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700760 }
761
Adam Cohendcd297f2013-06-18 13:13:40 -0700762 if (version < 13) {
763 // With the new shrink-wrapped and re-orderable workspaces, it makes sense
764 // to persist workspace screens and their relative order.
765 mMaxScreenId = 0;
766
767 // This will never happen in the wild, but when we switch to using workspace
768 // screen ids, redo the import from old launcher.
Winson Chungc763c4e2013-07-19 13:49:06 -0700769 sJustLoadedFromOldDb = true;
Adam Cohendcd297f2013-06-18 13:13:40 -0700770
771 addWorkspacesTable(db);
772 version = 13;
773 }
774
Chris Wrend5e66bf2013-09-16 14:02:29 -0400775 if (version < 14) {
776 db.beginTransaction();
777 try {
778 // Insert new column for holding widget provider name
779 db.execSQL("ALTER TABLE favorites " +
780 "ADD COLUMN appWidgetProvider TEXT;");
781 db.setTransactionSuccessful();
782 version = 14;
783 } catch (SQLException ex) {
784 // Old version remains, which means we wipe old data
785 Log.e(TAG, ex.getMessage(), ex);
786 } finally {
787 db.endTransaction();
788 }
789 }
790
Chris Wren1ada10d2013-09-13 18:01:38 -0400791 if (version < 15) {
792 db.beginTransaction();
793 try {
794 // Insert new column for holding update timestamp
795 db.execSQL("ALTER TABLE favorites " +
796 "ADD COLUMN modified INTEGER NOT NULL DEFAULT 0;");
797 db.execSQL("ALTER TABLE workspaceScreens " +
798 "ADD COLUMN modified INTEGER NOT NULL DEFAULT 0;");
799 db.setTransactionSuccessful();
800 version = 15;
801 } catch (SQLException ex) {
802 // Old version remains, which means we wipe old data
803 Log.e(TAG, ex.getMessage(), ex);
804 } finally {
805 db.endTransaction();
806 }
807 }
808
Chris Wrenf4d08112014-01-16 18:13:56 -0500809
810 if (version < 16) {
811 db.beginTransaction();
812 try {
813 // Insert new column for holding restore status
814 db.execSQL("ALTER TABLE favorites " +
815 "ADD COLUMN restored INTEGER NOT NULL DEFAULT 0;");
816 db.setTransactionSuccessful();
817 version = 16;
818 } catch (SQLException ex) {
819 // Old version remains, which means we wipe old data
820 Log.e(TAG, ex.getMessage(), ex);
821 } finally {
822 db.endTransaction();
823 }
824 }
825
Adam Cohen71e03b92014-02-21 14:09:53 -0800826 if (version < 17) {
827 // We use the db version upgrade here to identify users who may not have seen
828 // clings yet (because they weren't available), but for whom the clings are now
829 // available (tablet users). Because one of the possible cling flows (migration)
830 // is very destructive (wipes out workspaces), we want to prevent this from showing
831 // until clear data. We do so by marking that the clings have been shown.
832 LauncherClings.synchonouslyMarkFirstRunClingDismissed(mContext);
833 version = 17;
834 }
835
Adam Cohen119285e2014-04-02 16:59:08 -0700836 if (version < 18) {
Adam Cohenf9c14de2014-04-17 18:20:45 -0700837 // No-op
838 version = 18;
839 }
840
841 if (version < 19) {
Adam Cohen119285e2014-04-02 16:59:08 -0700842 // Due to a data loss bug, some users may have items associated with screen ids
843 // which no longer exist. Since this can cause other problems, and since the user
844 // will never see these items anyway, we use database upgrade as an opportunity to
845 // clean things up.
Adam Cohenf9c14de2014-04-17 18:20:45 -0700846 removeOrphanedItems(db);
847 version = 19;
Adam Cohen119285e2014-04-02 16:59:08 -0700848 }
849
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850 if (version != DATABASE_VERSION) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800851 Log.w(TAG, "Destroying all old data.");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800852 db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES);
Adam Cohendcd297f2013-06-18 13:13:40 -0700853 db.execSQL("DROP TABLE IF EXISTS " + TABLE_WORKSPACE_SCREENS);
854
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800855 onCreate(db);
856 }
857 }
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800858
859 private boolean updateContactsShortcuts(SQLiteDatabase db) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800860 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE,
861 new int[] { Favorites.ITEM_TYPE_SHORTCUT });
862
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700863 Cursor c = null;
864 final String actionQuickContact = "com.android.contacts.action.QUICK_CONTACT";
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800865 db.beginTransaction();
866 try {
867 // Select and iterate through each matching widget
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700868 c = db.query(TABLE_FAVORITES,
869 new String[] { Favorites._ID, Favorites.INTENT },
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800870 selectWhere, null, null, null, null);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700871 if (c == null) return false;
872
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800873 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700874
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800875 final int idIndex = c.getColumnIndex(Favorites._ID);
876 final int intentIndex = c.getColumnIndex(Favorites.INTENT);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700877
878 while (c.moveToNext()) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800879 long favoriteId = c.getLong(idIndex);
880 final String intentUri = c.getString(intentIndex);
881 if (intentUri != null) {
882 try {
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700883 final Intent intent = Intent.parseUri(intentUri, 0);
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800884 android.util.Log.d("Home", intent.toString());
885 final Uri uri = intent.getData();
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700886 if (uri != null) {
887 final String data = uri.toString();
888 if ((Intent.ACTION_VIEW.equals(intent.getAction()) ||
889 actionQuickContact.equals(intent.getAction())) &&
890 (data.startsWith("content://contacts/people/") ||
891 data.startsWith("content://com.android.contacts/" +
892 "contacts/lookup/"))) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800893
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700894 final Intent newIntent = new Intent(actionQuickContact);
895 // When starting from the launcher, start in a new, cleared task
896 // CLEAR_WHEN_TASK_RESET cannot reset the root of a task, so we
897 // clear the whole thing preemptively here since
898 // QuickContactActivity will finish itself when launching other
899 // detail activities.
900 newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
901 Intent.FLAG_ACTIVITY_CLEAR_TASK);
Winson Chung2672ff92012-05-04 16:22:30 -0700902 newIntent.putExtra(
903 Launcher.INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION, true);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700904 newIntent.setData(uri);
Daniel Lehmannd02402c2012-05-14 18:30:53 -0700905 // Determine the type and also put that in the shortcut
906 // (that can speed up launch a bit)
907 newIntent.setDataAndType(uri, newIntent.resolveType(mContext));
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800908
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700909 final ContentValues values = new ContentValues();
910 values.put(LauncherSettings.Favorites.INTENT,
911 newIntent.toUri(0));
912
913 String updateWhere = Favorites._ID + "=" + favoriteId;
914 db.update(TABLE_FAVORITES, values, updateWhere, null);
915 }
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800916 }
917 } catch (RuntimeException ex) {
918 Log.e(TAG, "Problem upgrading shortcut", ex);
919 } catch (URISyntaxException e) {
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700920 Log.e(TAG, "Problem upgrading shortcut", e);
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800921 }
922 }
923 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700924
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800925 db.setTransactionSuccessful();
926 } catch (SQLException ex) {
927 Log.w(TAG, "Problem while upgrading contacts", ex);
928 return false;
929 } finally {
930 db.endTransaction();
931 if (c != null) {
932 c.close();
933 }
934 }
935
936 return true;
937 }
938
Joe Onorato0589f0f2010-02-08 13:44:00 -0800939 private void normalizeIcons(SQLiteDatabase db) {
940 Log.d(TAG, "normalizing icons");
941
Joe Onorato346e1292010-02-18 10:34:24 -0500942 db.beginTransaction();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800943 Cursor c = null;
Joe Onorato9690b392010-03-23 17:34:37 -0400944 SQLiteStatement update = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800945 try {
946 boolean logged = false;
Joe Onorato9690b392010-03-23 17:34:37 -0400947 update = db.compileStatement("UPDATE favorites "
Jeff Hamiltoneaf77d62010-02-13 00:08:17 -0600948 + "SET icon=? WHERE _id=?");
Joe Onorato0589f0f2010-02-08 13:44:00 -0800949
950 c = db.rawQuery("SELECT _id, icon FROM favorites WHERE iconType=" +
951 Favorites.ICON_TYPE_BITMAP, null);
952
953 final int idIndex = c.getColumnIndexOrThrow(Favorites._ID);
954 final int iconIndex = c.getColumnIndexOrThrow(Favorites.ICON);
955
956 while (c.moveToNext()) {
957 long id = c.getLong(idIndex);
958 byte[] data = c.getBlob(iconIndex);
959 try {
960 Bitmap bitmap = Utilities.resampleIconBitmap(
961 BitmapFactory.decodeByteArray(data, 0, data.length),
962 mContext);
963 if (bitmap != null) {
964 update.bindLong(1, id);
965 data = ItemInfo.flattenBitmap(bitmap);
966 if (data != null) {
967 update.bindBlob(2, data);
968 update.execute();
969 }
970 bitmap.recycle();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800971 }
972 } catch (Exception e) {
973 if (!logged) {
974 Log.e(TAG, "Failed normalizing icon " + id, e);
975 } else {
976 Log.e(TAG, "Also failed normalizing icon " + id);
977 }
978 logged = true;
979 }
980 }
Bjorn Bringert3a928e42010-02-19 11:15:40 +0000981 db.setTransactionSuccessful();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800982 } catch (SQLException ex) {
983 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
984 } finally {
985 db.endTransaction();
Joe Onorato9690b392010-03-23 17:34:37 -0400986 if (update != null) {
987 update.close();
988 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800989 if (c != null) {
990 c.close();
991 }
992 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700993 }
994
995 // Generates a new ID to use for an object in your database. This method should be only
996 // called from the main UI thread. As an exception, we do call it when we call the
997 // constructor from the worker thread; however, this doesn't extend until after the
998 // constructor is called, and we only pass a reference to LauncherProvider to LauncherApp
999 // after that point
Adam Cohendcd297f2013-06-18 13:13:40 -07001000 public long generateNewItemId() {
1001 if (mMaxItemId < 0) {
1002 throw new RuntimeException("Error: max item id was not initialized");
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001003 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001004 mMaxItemId += 1;
1005 return mMaxItemId;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001006 }
1007
Winson Chungc763c4e2013-07-19 13:49:06 -07001008 public void updateMaxItemId(long id) {
1009 mMaxItemId = id + 1;
1010 }
1011
Chris Wren5dee7af2013-12-20 17:22:11 -05001012 public void checkId(String table, ContentValues values) {
1013 long id = values.getAsLong(LauncherSettings.BaseLauncherColumns._ID);
1014 if (table == LauncherProvider.TABLE_WORKSPACE_SCREENS) {
1015 mMaxScreenId = Math.max(id, mMaxScreenId);
1016 } else {
1017 mMaxItemId = Math.max(id, mMaxItemId);
1018 }
1019 }
1020
Adam Cohendcd297f2013-06-18 13:13:40 -07001021 private long initializeMaxItemId(SQLiteDatabase db) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001022 Cursor c = db.rawQuery("SELECT MAX(_id) FROM favorites", null);
1023
1024 // get the result
1025 final int maxIdIndex = 0;
1026 long id = -1;
1027 if (c != null && c.moveToNext()) {
1028 id = c.getLong(maxIdIndex);
1029 }
Michael Jurka5130e402011-10-13 04:55:35 -07001030 if (c != null) {
1031 c.close();
1032 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001033
1034 if (id == -1) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001035 throw new RuntimeException("Error: could not query max item id");
1036 }
1037
1038 return id;
1039 }
1040
1041 // Generates a new ID to use for an workspace screen in your database. This method
1042 // should be only called from the main UI thread. As an exception, we do call it when we
1043 // call the constructor from the worker thread; however, this doesn't extend until after the
1044 // constructor is called, and we only pass a reference to LauncherProvider to LauncherApp
1045 // after that point
1046 public long generateNewScreenId() {
1047 if (mMaxScreenId < 0) {
1048 throw new RuntimeException("Error: max screen id was not initialized");
1049 }
1050 mMaxScreenId += 1;
Winson Chunga90303b2013-11-15 13:05:06 -08001051 // Log to disk
1052 Launcher.addDumpLog(TAG, "11683562 - generateNewScreenId(): " + mMaxScreenId, true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001053 return mMaxScreenId;
1054 }
1055
1056 public void updateMaxScreenId(long maxScreenId) {
Winson Chunga90303b2013-11-15 13:05:06 -08001057 // Log to disk
1058 Launcher.addDumpLog(TAG, "11683562 - updateMaxScreenId(): " + maxScreenId, true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001059 mMaxScreenId = maxScreenId;
1060 }
1061
1062 private long initializeMaxScreenId(SQLiteDatabase db) {
1063 Cursor c = db.rawQuery("SELECT MAX(" + LauncherSettings.WorkspaceScreens._ID + ") FROM " + TABLE_WORKSPACE_SCREENS, null);
1064
1065 // get the result
1066 final int maxIdIndex = 0;
1067 long id = -1;
1068 if (c != null && c.moveToNext()) {
1069 id = c.getLong(maxIdIndex);
1070 }
1071 if (c != null) {
1072 c.close();
1073 }
1074
1075 if (id == -1) {
1076 throw new RuntimeException("Error: could not query max screen id");
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001077 }
1078
Winson Chunga90303b2013-11-15 13:05:06 -08001079 // Log to disk
1080 Launcher.addDumpLog(TAG, "11683562 - initializeMaxScreenId(): " + id, true);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001081 return id;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001082 }
1083
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001085 * Upgrade existing clock and photo frame widgets into their new widget
Bjorn Bringert93c45762009-12-16 13:19:47 +00001086 * equivalents.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087 */
1088 private void convertWidgets(SQLiteDatabase db) {
Bjorn Bringert34251342009-12-15 13:33:11 +00001089 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 final int[] bindSources = new int[] {
1091 Favorites.ITEM_TYPE_WIDGET_CLOCK,
1092 Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME,
Bjorn Bringert7984c942009-12-09 15:38:25 +00001093 Favorites.ITEM_TYPE_WIDGET_SEARCH,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 };
Bjorn Bringert7984c942009-12-09 15:38:25 +00001095
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE, bindSources);
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001097
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098 Cursor c = null;
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001099
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001100 db.beginTransaction();
1101 try {
1102 // Select and iterate through each matching widget
Bjorn Bringert7984c942009-12-09 15:38:25 +00001103 c = db.query(TABLE_FAVORITES, new String[] { Favorites._ID, Favorites.ITEM_TYPE },
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 selectWhere, null, null, null, null);
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001105
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001106 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001107
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001108 final ContentValues values = new ContentValues();
1109 while (c != null && c.moveToNext()) {
1110 long favoriteId = c.getLong(0);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001111 int favoriteType = c.getInt(1);
1112
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001113 // Allocate and update database with new appWidgetId
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001114 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001115 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001116
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001117 if (LOGD) {
1118 Log.d(TAG, "allocated appWidgetId=" + appWidgetId
1119 + " for favoriteId=" + favoriteId);
1120 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121 values.clear();
Bjorn Bringert7984c942009-12-09 15:38:25 +00001122 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
1123 values.put(Favorites.APPWIDGET_ID, appWidgetId);
1124
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 // Original widgets might not have valid spans when upgrading
Bjorn Bringert7984c942009-12-09 15:38:25 +00001126 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
1127 values.put(LauncherSettings.Favorites.SPANX, 4);
1128 values.put(LauncherSettings.Favorites.SPANY, 1);
1129 } else {
1130 values.put(LauncherSettings.Favorites.SPANX, 2);
1131 values.put(LauncherSettings.Favorites.SPANY, 2);
1132 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133
1134 String updateWhere = Favorites._ID + "=" + favoriteId;
1135 db.update(TABLE_FAVORITES, values, updateWhere, null);
Bjorn Bringert34251342009-12-15 13:33:11 +00001136
Bjorn Bringert34251342009-12-15 13:33:11 +00001137 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_CLOCK) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001138 // TODO: check return value
1139 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
Bjorn Bringert34251342009-12-15 13:33:11 +00001140 new ComponentName("com.android.alarmclock",
1141 "com.android.alarmclock.AnalogAppWidgetProvider"));
1142 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001143 // TODO: check return value
1144 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
Bjorn Bringert34251342009-12-15 13:33:11 +00001145 new ComponentName("com.android.camera",
1146 "com.android.camera.PhotoAppWidgetProvider"));
1147 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001148 // TODO: check return value
1149 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
Bjorn Bringertcd8fec02010-01-14 13:26:43 +00001150 getSearchWidgetProvider());
Bjorn Bringert34251342009-12-15 13:33:11 +00001151 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001153 Log.e(TAG, "Problem allocating appWidgetId", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001154 }
1155 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001156
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001157 db.setTransactionSuccessful();
1158 } catch (SQLException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001159 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160 } finally {
1161 db.endTransaction();
1162 if (c != null) {
1163 c.close();
1164 }
1165 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001166
1167 // Update max item id
1168 mMaxItemId = initializeMaxItemId(db);
1169 if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 }
1171
Michael Jurka8b805b12012-04-18 14:23:14 -07001172 private static final void beginDocument(XmlPullParser parser, String firstElementName)
1173 throws XmlPullParserException, IOException {
1174 int type;
Michael Jurka9bc8eba2012-05-21 20:36:44 -07001175 while ((type = parser.next()) != XmlPullParser.START_TAG
1176 && type != XmlPullParser.END_DOCUMENT) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001177 ;
1178 }
1179
Michael Jurka9bc8eba2012-05-21 20:36:44 -07001180 if (type != XmlPullParser.START_TAG) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001181 throw new XmlPullParserException("No start tag found");
1182 }
1183
1184 if (!parser.getName().equals(firstElementName)) {
1185 throw new XmlPullParserException("Unexpected start tag: found " + parser.getName() +
1186 ", expected " + firstElementName);
1187 }
1188 }
1189
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001190 private static Intent buildMainIntent() {
1191 Intent intent = new Intent(Intent.ACTION_MAIN, null);
1192 intent.addCategory(Intent.CATEGORY_LAUNCHER);
1193 return intent;
1194 }
1195
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 * Loads the default set of favorite packages from an xml file.
1198 *
1199 * @param db The database to write the values into
Winson Chung3d503fb2011-07-13 17:25:49 -07001200 * @param filterContainerId The specific container id of items to load
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 */
Winson Chung6d092682011-11-16 18:43:26 -08001202 private int loadFavorites(SQLiteDatabase db, int workspaceResourceId) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203 ContentValues values = new ContentValues();
1204
Daniel Sandler57dac262013-10-03 13:28:36 -04001205 if (LOGD) Log.v(TAG, String.format("Loading favorites from resid=0x%08x", workspaceResourceId));
1206
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207 int i = 0;
1208 try {
Winson Chung6d092682011-11-16 18:43:26 -08001209 XmlResourceParser parser = mContext.getResources().getXml(workspaceResourceId);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001210 AttributeSet attrs = Xml.asAttributeSet(parser);
Michael Jurka8b805b12012-04-18 14:23:14 -07001211 beginDocument(parser, TAG_FAVORITES);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001212
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001213 final int depth = parser.getDepth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001214
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001215 int type;
1216 while (((type = parser.next()) != XmlPullParser.END_TAG ||
1217 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
1218
1219 if (type != XmlPullParser.START_TAG) {
1220 continue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221 }
1222
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001223 boolean added = false;
1224 final String name = parser.getName();
1225
Daniel Sandler57dac262013-10-03 13:28:36 -04001226 if (TAG_INCLUDE.equals(name)) {
1227 final TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.Include);
1228
1229 final int resId = a.getResourceId(R.styleable.Include_workspace, 0);
1230
1231 if (LOGD) Log.v(TAG, String.format(("%" + (2*(depth+1)) + "s<include workspace=%08x>"),
1232 "", resId));
1233
1234 if (resId != 0 && resId != workspaceResourceId) {
1235 // recursively load some more favorites, why not?
1236 i += loadFavorites(db, resId);
1237 added = false;
Daniel Sandler57dac262013-10-03 13:28:36 -04001238 } else {
1239 Log.w(TAG, String.format("Skipping <include workspace=0x%08x>", resId));
1240 }
1241
1242 a.recycle();
1243
1244 if (LOGD) Log.v(TAG, String.format(("%" + (2*(depth+1)) + "s</include>"), ""));
1245 continue;
1246 }
1247
1248 // Assuming it's a <favorite> at this point
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001249 TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.Favorite);
1250
Winson Chung3d503fb2011-07-13 17:25:49 -07001251 long container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
1252 if (a.hasValue(R.styleable.Favorite_container)) {
1253 container = Long.valueOf(a.getString(R.styleable.Favorite_container));
1254 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001255
Winson Chung6d092682011-11-16 18:43:26 -08001256 String screen = a.getString(R.styleable.Favorite_screen);
1257 String x = a.getString(R.styleable.Favorite_x);
1258 String y = a.getString(R.styleable.Favorite_y);
1259
Winson Chung6d092682011-11-16 18:43:26 -08001260 values.clear();
1261 values.put(LauncherSettings.Favorites.CONTAINER, container);
1262 values.put(LauncherSettings.Favorites.SCREEN, screen);
1263 values.put(LauncherSettings.Favorites.CELLX, x);
1264 values.put(LauncherSettings.Favorites.CELLY, y);
1265
Daniel Sandler57dac262013-10-03 13:28:36 -04001266 if (LOGD) {
1267 final String title = a.getString(R.styleable.Favorite_title);
1268 final String pkg = a.getString(R.styleable.Favorite_packageName);
1269 final String something = title != null ? title : pkg;
1270 Log.v(TAG, String.format(
1271 ("%" + (2*(depth+1)) + "s<%s%s c=%d s=%s x=%s y=%s>"),
1272 "", name,
1273 (something == null ? "" : (" \"" + something + "\"")),
1274 container, screen, x, y));
1275 }
1276
Winson Chung6d092682011-11-16 18:43:26 -08001277 if (TAG_FAVORITE.equals(name)) {
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001278 long id = addAppShortcut(db, values, parser);
Winson Chung6d092682011-11-16 18:43:26 -08001279 added = id >= 0;
1280 } else if (TAG_SEARCH.equals(name)) {
1281 added = addSearchWidget(db, values);
1282 } else if (TAG_CLOCK.equals(name)) {
1283 added = addClockWidget(db, values);
1284 } else if (TAG_APPWIDGET.equals(name)) {
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001285 added = addAppWidget(parser, attrs, type, db, values, a);
Winson Chung6d092682011-11-16 18:43:26 -08001286 } else if (TAG_SHORTCUT.equals(name)) {
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001287 long id = addUriShortcut(db, values, mContext.getResources(), parser);
Winson Chung6d092682011-11-16 18:43:26 -08001288 added = id >= 0;
Jason Monk41314972014-03-03 16:11:30 -05001289 } else if (TAG_RESOLVE.equals(name)) {
1290 // This looks through the contained favorites (or meta-favorites) and
1291 // attempts to add them as shortcuts in the fallback group's location
1292 // until one is added successfully.
1293 added = false;
1294 final int groupDepth = parser.getDepth();
1295 while ((type = parser.next()) != XmlPullParser.END_TAG ||
1296 parser.getDepth() > groupDepth) {
1297 if (type != XmlPullParser.START_TAG) {
1298 continue;
1299 }
1300 final String fallback_item_name = parser.getName();
1301 final TypedArray ar = mContext.obtainStyledAttributes(attrs,
1302 R.styleable.Favorite);
1303 if (!added) {
1304 if (TAG_FAVORITE.equals(fallback_item_name)) {
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001305 final long id = addAppShortcut(db, values, parser);
Jason Monk41314972014-03-03 16:11:30 -05001306 added = id >= 0;
1307 } else {
1308 Log.e(TAG, "Fallback groups can contain only favorites "
1309 + ar.toString());
1310 }
1311 }
1312 ar.recycle();
1313 }
Winson Chung6d092682011-11-16 18:43:26 -08001314 } else if (TAG_FOLDER.equals(name)) {
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001315 // Folder contents are nested in this XML file
1316 added = loadFolder(db, values, mContext.getResources(), parser);
Winson Chung3d503fb2011-07-13 17:25:49 -07001317
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001318 } else if (TAG_PARTNER_FOLDER.equals(name)) {
1319 // Folder contents come from an external XML resource
1320 final Partner partner = Partner.get(mPackageManager);
1321 if (partner != null) {
1322 final Resources partnerRes = partner.getResources();
1323 final int resId = partnerRes.getIdentifier(Partner.RESOURCE_FOLDER,
1324 "xml", partner.getPackageName());
1325 if (resId != 0) {
1326 final XmlResourceParser partnerParser = partnerRes.getXml(resId);
1327 beginDocument(partnerParser, TAG_FOLDER);
1328 added = loadFolder(db, values, partnerRes, partnerParser);
Winson Chung6d092682011-11-16 18:43:26 -08001329 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001330 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001332 if (added) i++;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001333 a.recycle();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334 }
1335 } catch (XmlPullParserException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001336 Log.w(TAG, "Got exception parsing favorites.", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 } catch (IOException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001338 Log.w(TAG, "Got exception parsing favorites.", e);
Winson Chung3d503fb2011-07-13 17:25:49 -07001339 } catch (RuntimeException e) {
1340 Log.w(TAG, "Got exception parsing favorites.", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001342
Winson Chungc763c4e2013-07-19 13:49:06 -07001343 // Update the max item id after we have loaded the database
1344 if (mMaxItemId == -1) {
1345 mMaxItemId = initializeMaxItemId(db);
1346 }
1347
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001348 return i;
1349 }
1350
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001351 /**
1352 * Parse folder starting at current {@link XmlPullParser} location.
1353 */
1354 private boolean loadFolder(SQLiteDatabase db, ContentValues values, Resources res,
1355 XmlResourceParser parser) throws IOException, XmlPullParserException {
1356 final String title;
1357 final int titleResId = getAttributeResourceValue(parser, ATTR_TITLE, 0);
1358 if (titleResId != 0) {
1359 title = res.getString(titleResId);
1360 } else {
1361 title = mContext.getResources().getString(R.string.folder_name);
1362 }
1363
1364 values.put(LauncherSettings.Favorites.TITLE, title);
1365 long folderId = addFolder(db, values);
1366 boolean added = folderId >= 0;
1367
1368 ArrayList<Long> folderItems = new ArrayList<Long>();
1369
1370 int type;
1371 int folderDepth = parser.getDepth();
1372 while ((type = parser.next()) != XmlPullParser.END_TAG ||
1373 parser.getDepth() > folderDepth) {
1374 if (type != XmlPullParser.START_TAG) {
1375 continue;
1376 }
1377 final String tag = parser.getName();
1378
1379 final ContentValues childValues = new ContentValues();
1380 childValues.put(LauncherSettings.Favorites.CONTAINER, folderId);
1381
1382 if (LOGD) {
1383 final String pkg = getAttributeValue(parser, ATTR_PACKAGE_NAME);
1384 final String uri = getAttributeValue(parser, ATTR_URI);
1385 Log.v(TAG, String.format(("%" + (2*(folderDepth+1)) + "s<%s \"%s\">"), "",
1386 tag, uri != null ? uri : pkg));
1387 }
1388
1389 if (TAG_FAVORITE.equals(tag) && folderId >= 0) {
1390 final long id = addAppShortcut(db, childValues, parser);
1391 if (id >= 0) {
1392 folderItems.add(id);
1393 }
1394 } else if (TAG_SHORTCUT.equals(tag) && folderId >= 0) {
1395 final long id = addUriShortcut(db, childValues, res, parser);
1396 if (id >= 0) {
1397 folderItems.add(id);
1398 }
1399 } else {
1400 throw new RuntimeException("Folders can contain only shortcuts");
1401 }
1402 }
1403
1404 // We can only have folders with >= 2 items, so we need to remove the
1405 // folder and clean up if less than 2 items were included, or some
1406 // failed to add, and less than 2 were actually added
1407 if (folderItems.size() < 2 && folderId >= 0) {
1408 // Delete the folder
1409 deleteId(db, folderId);
1410
1411 // If we have a single item, promote it to where the folder
1412 // would have been.
1413 if (folderItems.size() == 1) {
1414 final ContentValues childValues = new ContentValues();
1415 copyInteger(values, childValues, LauncherSettings.Favorites.CONTAINER);
1416 copyInteger(values, childValues, LauncherSettings.Favorites.SCREEN);
1417 copyInteger(values, childValues, LauncherSettings.Favorites.CELLX);
1418 copyInteger(values, childValues, LauncherSettings.Favorites.CELLY);
1419
1420 final long id = folderItems.get(0);
1421 db.update(TABLE_FAVORITES, childValues,
1422 LauncherSettings.Favorites._ID + "=" + id, null);
1423 } else {
1424 added = false;
1425 }
1426 }
1427 return added;
1428 }
1429
Jason Monk41314972014-03-03 16:11:30 -05001430 // A meta shortcut attempts to resolve an intent specified as a URI in the XML, if a
1431 // logical choice for what shortcut should be used for that intent exists, then it is
1432 // added. Otherwise add nothing.
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001433 private long addAppShortcutByUri(SQLiteDatabase db, ContentValues values,
1434 String intentUri) {
Jason Monk41314972014-03-03 16:11:30 -05001435 Intent metaIntent;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001436 try {
Jason Monk41314972014-03-03 16:11:30 -05001437 metaIntent = Intent.parseUri(intentUri, 0);
1438 } catch (URISyntaxException e) {
1439 Log.e(TAG, "Unable to add meta-favorite: " + intentUri, e);
1440 return -1;
1441 }
1442
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001443 ResolveInfo resolved = mPackageManager.resolveActivity(metaIntent,
Jason Monk41314972014-03-03 16:11:30 -05001444 PackageManager.MATCH_DEFAULT_ONLY);
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001445 final List<ResolveInfo> appList = mPackageManager.queryIntentActivities(
Jason Monk41314972014-03-03 16:11:30 -05001446 metaIntent, PackageManager.MATCH_DEFAULT_ONLY);
1447
1448 // Verify that the result is an app and not just the resolver dialog asking which
1449 // app to use.
1450 if (wouldLaunchResolverActivity(resolved, appList)) {
1451 // If only one of the results is a system app then choose that as the default.
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001452 final ResolveInfo systemApp = getSingleSystemActivity(appList);
Jason Monk41314972014-03-03 16:11:30 -05001453 if (systemApp == null) {
1454 // There is no logical choice for this meta-favorite, so rather than making
1455 // a bad choice just add nothing.
1456 Log.w(TAG, "No preference or single system activity found for "
1457 + metaIntent.toString());
Adam Cohen228da5a2011-07-27 22:23:47 -07001458 return -1;
1459 }
Jason Monk41314972014-03-03 16:11:30 -05001460 resolved = systemApp;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001461 }
Jason Monk41314972014-03-03 16:11:30 -05001462 final ActivityInfo info = resolved.activityInfo;
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001463 final Intent intent = buildMainIntent();
Jason Monk41314972014-03-03 16:11:30 -05001464 intent.setComponent(new ComponentName(info.packageName, info.name));
1465 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1466 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1467
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001468 return addAppShortcut(db, values, info.loadLabel(mPackageManager).toString(), intent);
Jason Monk41314972014-03-03 16:11:30 -05001469 }
1470
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001471 private ResolveInfo getSingleSystemActivity(List<ResolveInfo> appList) {
Jason Monk41314972014-03-03 16:11:30 -05001472 ResolveInfo systemResolve = null;
1473 final int N = appList.size();
1474 for (int i = 0; i < N; ++i) {
1475 try {
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001476 ApplicationInfo info = mPackageManager.getApplicationInfo(
Jason Monk41314972014-03-03 16:11:30 -05001477 appList.get(i).activityInfo.packageName, 0);
1478 if ((info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
1479 if (systemResolve != null) {
1480 return null;
1481 } else {
1482 systemResolve = appList.get(i);
1483 }
1484 }
1485 } catch (PackageManager.NameNotFoundException e) {
1486 Log.w(TAG, "Unable to get info about resolve results", e);
1487 return null;
1488 }
1489 }
1490 return systemResolve;
1491 }
1492
1493 private boolean wouldLaunchResolverActivity(ResolveInfo resolved,
1494 List<ResolveInfo> appList) {
1495 // If the list contains the above resolved activity, then it can't be
1496 // ResolverActivity itself.
1497 for (int i = 0; i < appList.size(); ++i) {
1498 ResolveInfo tmp = appList.get(i);
1499 if (tmp.activityInfo.name.equals(resolved.activityInfo.name)
1500 && tmp.activityInfo.packageName.equals(resolved.activityInfo.packageName)) {
1501 return false;
1502 }
1503 }
1504 return true;
1505 }
1506
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001507 private long addAppShortcut(SQLiteDatabase db, ContentValues values,
1508 XmlResourceParser parser) {
1509 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
1510 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
1511 final String uri = getAttributeValue(parser, ATTR_URI);
1512
1513 if (!TextUtils.isEmpty(packageName) && !TextUtils.isEmpty(className)) {
Jason Monk41314972014-03-03 16:11:30 -05001514 ActivityInfo info;
Jason Monk41314972014-03-03 16:11:30 -05001515 try {
1516 ComponentName cn;
1517 try {
1518 cn = new ComponentName(packageName, className);
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001519 info = mPackageManager.getActivityInfo(cn, 0);
Jason Monk41314972014-03-03 16:11:30 -05001520 } catch (PackageManager.NameNotFoundException nnfe) {
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001521 String[] packages = mPackageManager.currentToCanonicalPackageNames(
Jason Monk41314972014-03-03 16:11:30 -05001522 new String[] { packageName });
1523 cn = new ComponentName(packages[0], className);
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001524 info = mPackageManager.getActivityInfo(cn, 0);
Jason Monk41314972014-03-03 16:11:30 -05001525 }
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001526 final Intent intent = buildMainIntent();
Jason Monk41314972014-03-03 16:11:30 -05001527 intent.setComponent(cn);
1528 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1529 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1530
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001531 return addAppShortcut(db, values, info.loadLabel(mPackageManager).toString(),
Jason Monk41314972014-03-03 16:11:30 -05001532 intent);
1533 } catch (PackageManager.NameNotFoundException e) {
1534 Log.w(TAG, "Unable to add favorite: " + packageName +
1535 "/" + className, e);
1536 }
1537 return -1;
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001538 } else if (!TextUtils.isEmpty(uri)) {
Jason Monk41314972014-03-03 16:11:30 -05001539 // If no component specified try to find a shortcut to add from the URI.
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001540 return addAppShortcutByUri(db, values, uri);
Jason Monk41314972014-03-03 16:11:30 -05001541 } else {
1542 Log.e(TAG, "Skipping invalid <favorite> with no component or uri");
1543 return -1;
1544 }
1545 }
1546
1547 private long addAppShortcut(SQLiteDatabase db, ContentValues values, String title,
1548 Intent intent) {
1549 long id = generateNewItemId();
1550 values.put(Favorites.INTENT, intent.toUri(0));
1551 values.put(Favorites.TITLE, title);
1552 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPLICATION);
1553 values.put(Favorites.SPANX, 1);
1554 values.put(Favorites.SPANY, 1);
1555 values.put(Favorites._ID, id);
1556 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values) < 0) {
1557 return -1;
1558 } else {
1559 return id;
1560 }
Adam Cohen228da5a2011-07-27 22:23:47 -07001561 }
1562
1563 private long addFolder(SQLiteDatabase db, ContentValues values) {
1564 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_FOLDER);
1565 values.put(Favorites.SPANX, 1);
1566 values.put(Favorites.SPANY, 1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001567 long id = generateNewItemId();
Adam Cohen228da5a2011-07-27 22:23:47 -07001568 values.put(Favorites._ID, id);
1569 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values) <= 0) {
1570 return -1;
1571 } else {
1572 return id;
1573 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001574 }
1575
Bjorn Bringertcd8fec02010-01-14 13:26:43 +00001576 private ComponentName getSearchWidgetProvider() {
1577 SearchManager searchManager =
1578 (SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE);
1579 ComponentName searchComponent = searchManager.getGlobalSearchActivity();
1580 if (searchComponent == null) return null;
1581 return getProviderInPackage(searchComponent.getPackageName());
1582 }
1583
1584 /**
1585 * Gets an appwidget provider from the given package. If the package contains more than
1586 * one appwidget provider, an arbitrary one is returned.
1587 */
1588 private ComponentName getProviderInPackage(String packageName) {
1589 AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
1590 List<AppWidgetProviderInfo> providers = appWidgetManager.getInstalledProviders();
1591 if (providers == null) return null;
1592 final int providerCount = providers.size();
1593 for (int i = 0; i < providerCount; i++) {
1594 ComponentName provider = providers.get(i).provider;
1595 if (provider != null && provider.getPackageName().equals(packageName)) {
1596 return provider;
1597 }
1598 }
1599 return null;
1600 }
1601
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001602 private boolean addSearchWidget(SQLiteDatabase db, ContentValues values) {
Bjorn Bringertcd8fec02010-01-14 13:26:43 +00001603 ComponentName cn = getSearchWidgetProvider();
Winson Chungb3302ae2012-05-01 10:19:14 -07001604 return addAppWidget(db, values, cn, 4, 1, null);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001605 }
1606
1607 private boolean addClockWidget(SQLiteDatabase db, ContentValues values) {
Bjorn Bringert34251342009-12-15 13:33:11 +00001608 ComponentName cn = new ComponentName("com.android.alarmclock",
1609 "com.android.alarmclock.AnalogAppWidgetProvider");
Winson Chungb3302ae2012-05-01 10:19:14 -07001610 return addAppWidget(db, values, cn, 2, 2, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001611 }
Adam Cohen228da5a2011-07-27 22:23:47 -07001612
Winson Chungb3302ae2012-05-01 10:19:14 -07001613 private boolean addAppWidget(XmlResourceParser parser, AttributeSet attrs, int type,
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001614 SQLiteDatabase db, ContentValues values, TypedArray a)
1615 throws XmlPullParserException, IOException {
Romain Guy693599f2010-03-23 10:58:18 -07001616
Mike Cleronb87bd162009-10-30 16:36:56 -07001617 String packageName = a.getString(R.styleable.Favorite_packageName);
1618 String className = a.getString(R.styleable.Favorite_className);
1619
1620 if (packageName == null || className == null) {
1621 return false;
1622 }
Romain Guy693599f2010-03-23 10:58:18 -07001623
1624 boolean hasPackage = true;
Mike Cleronb87bd162009-10-30 16:36:56 -07001625 ComponentName cn = new ComponentName(packageName, className);
Romain Guy693599f2010-03-23 10:58:18 -07001626 try {
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001627 mPackageManager.getReceiverInfo(cn, 0);
Romain Guy693599f2010-03-23 10:58:18 -07001628 } catch (Exception e) {
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001629 String[] packages = mPackageManager.currentToCanonicalPackageNames(
Romain Guy693599f2010-03-23 10:58:18 -07001630 new String[] { packageName });
1631 cn = new ComponentName(packages[0], className);
1632 try {
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001633 mPackageManager.getReceiverInfo(cn, 0);
Romain Guy693599f2010-03-23 10:58:18 -07001634 } catch (Exception e1) {
1635 hasPackage = false;
1636 }
1637 }
1638
1639 if (hasPackage) {
1640 int spanX = a.getInt(R.styleable.Favorite_spanX, 0);
1641 int spanY = a.getInt(R.styleable.Favorite_spanY, 0);
Winson Chungb3302ae2012-05-01 10:19:14 -07001642
1643 // Read the extras
1644 Bundle extras = new Bundle();
1645 int widgetDepth = parser.getDepth();
1646 while ((type = parser.next()) != XmlPullParser.END_TAG ||
1647 parser.getDepth() > widgetDepth) {
1648 if (type != XmlPullParser.START_TAG) {
1649 continue;
1650 }
1651
1652 TypedArray ar = mContext.obtainStyledAttributes(attrs, R.styleable.Extra);
1653 if (TAG_EXTRA.equals(parser.getName())) {
1654 String key = ar.getString(R.styleable.Extra_key);
1655 String value = ar.getString(R.styleable.Extra_value);
1656 if (key != null && value != null) {
1657 extras.putString(key, value);
1658 } else {
1659 throw new RuntimeException("Widget extras must have a key and value");
1660 }
1661 } else {
1662 throw new RuntimeException("Widgets can contain only extras");
1663 }
1664 ar.recycle();
1665 }
1666
1667 return addAppWidget(db, values, cn, spanX, spanY, extras);
Romain Guy693599f2010-03-23 10:58:18 -07001668 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001669
Romain Guy693599f2010-03-23 10:58:18 -07001670 return false;
Bjorn Bringert7984c942009-12-09 15:38:25 +00001671 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001672 private boolean addAppWidget(SQLiteDatabase db, ContentValues values, ComponentName cn,
Winson Chungb3302ae2012-05-01 10:19:14 -07001673 int spanX, int spanY, Bundle extras) {
Mike Cleronb87bd162009-10-30 16:36:56 -07001674 boolean allocatedAppWidgets = false;
1675 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
1676
1677 try {
1678 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001679
Mike Cleronb87bd162009-10-30 16:36:56 -07001680 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001681 values.put(Favorites.SPANX, spanX);
1682 values.put(Favorites.SPANY, spanY);
Mike Cleronb87bd162009-10-30 16:36:56 -07001683 values.put(Favorites.APPWIDGET_ID, appWidgetId);
Chris Wrend5e66bf2013-09-16 14:02:29 -04001684 values.put(Favorites.APPWIDGET_PROVIDER, cn.flattenToString());
Adam Cohendcd297f2013-06-18 13:13:40 -07001685 values.put(Favorites._ID, generateNewItemId());
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001686 dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values);
Mike Cleronb87bd162009-10-30 16:36:56 -07001687
1688 allocatedAppWidgets = true;
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001689
Michael Jurka8b805b12012-04-18 14:23:14 -07001690 // TODO: need to check return value
1691 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, cn);
Winson Chungb3302ae2012-05-01 10:19:14 -07001692
1693 // Send a broadcast to configure the widget
1694 if (extras != null && !extras.isEmpty()) {
1695 Intent intent = new Intent(ACTION_APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE);
1696 intent.setComponent(cn);
1697 intent.putExtras(extras);
1698 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1699 mContext.sendBroadcast(intent);
1700 }
Mike Cleronb87bd162009-10-30 16:36:56 -07001701 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001702 Log.e(TAG, "Problem allocating appWidgetId", ex);
Mike Cleronb87bd162009-10-30 16:36:56 -07001703 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001704
Mike Cleronb87bd162009-10-30 16:36:56 -07001705 return allocatedAppWidgets;
1706 }
Adam Cohen228da5a2011-07-27 22:23:47 -07001707
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001708 private long addUriShortcut(SQLiteDatabase db, ContentValues values, Resources res,
1709 XmlResourceParser parser) {
1710 final int iconResId = getAttributeResourceValue(parser, ATTR_ICON, 0);
1711 final int titleResId = getAttributeResourceValue(parser, ATTR_TITLE, 0);
Mike Cleronb87bd162009-10-30 16:36:56 -07001712
Romain Guy7eb9e5e2009-12-02 20:10:07 -08001713 Intent intent;
Mike Cleronb87bd162009-10-30 16:36:56 -07001714 String uri = null;
1715 try {
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001716 uri = getAttributeValue(parser, ATTR_URI);
Mike Cleronb87bd162009-10-30 16:36:56 -07001717 intent = Intent.parseUri(uri, 0);
1718 } catch (URISyntaxException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001719 Log.w(TAG, "Shortcut has malformed uri: " + uri);
Adam Cohen228da5a2011-07-27 22:23:47 -07001720 return -1; // Oh well
Mike Cleronb87bd162009-10-30 16:36:56 -07001721 }
1722
1723 if (iconResId == 0 || titleResId == 0) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001724 Log.w(TAG, "Shortcut is missing title or icon resource ID");
Adam Cohen228da5a2011-07-27 22:23:47 -07001725 return -1;
Mike Cleronb87bd162009-10-30 16:36:56 -07001726 }
1727
Adam Cohendcd297f2013-06-18 13:13:40 -07001728 long id = generateNewItemId();
Mike Cleronb87bd162009-10-30 16:36:56 -07001729 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1730 values.put(Favorites.INTENT, intent.toUri(0));
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001731 values.put(Favorites.TITLE, res.getString(titleResId));
Mike Cleronb87bd162009-10-30 16:36:56 -07001732 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_SHORTCUT);
1733 values.put(Favorites.SPANX, 1);
1734 values.put(Favorites.SPANY, 1);
1735 values.put(Favorites.ICON_TYPE, Favorites.ICON_TYPE_RESOURCE);
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001736 values.put(Favorites.ICON_PACKAGE, res.getResourcePackageName(iconResId));
1737 values.put(Favorites.ICON_RESOURCE, res.getResourceName(iconResId));
Adam Cohen228da5a2011-07-27 22:23:47 -07001738 values.put(Favorites._ID, id);
Mike Cleronb87bd162009-10-30 16:36:56 -07001739
Adam Cohen228da5a2011-07-27 22:23:47 -07001740 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values) < 0) {
1741 return -1;
1742 }
1743 return id;
Mike Cleronb87bd162009-10-30 16:36:56 -07001744 }
Dan Sandlerd5024042014-01-09 15:01:33 -05001745
1746 public void migrateLauncher2Shortcuts(SQLiteDatabase db, Uri uri) {
1747 final ContentResolver resolver = mContext.getContentResolver();
1748 Cursor c = null;
1749 int count = 0;
1750 int curScreen = 0;
1751
1752 try {
1753 c = resolver.query(uri, null, null, null, "title ASC");
1754 } catch (Exception e) {
1755 // Ignore
1756 }
1757
Dan Sandlerd5024042014-01-09 15:01:33 -05001758 // We already have a favorites database in the old provider
1759 if (c != null) {
1760 try {
1761 if (c.getCount() > 0) {
1762 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1763 final int intentIndex
1764 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
1765 final int titleIndex
1766 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
1767 final int iconTypeIndex
1768 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
1769 final int iconIndex
1770 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1771 final int iconPackageIndex
1772 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
1773 final int iconResourceIndex
1774 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
1775 final int containerIndex
1776 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
1777 final int itemTypeIndex
1778 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
1779 final int screenIndex
1780 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
1781 final int cellXIndex
1782 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
1783 final int cellYIndex
1784 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
1785 final int uriIndex
1786 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1787 final int displayModeIndex
1788 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
1789
1790 int i = 0;
1791 int curX = 0;
1792 int curY = 0;
1793
1794 final LauncherAppState app = LauncherAppState.getInstance();
1795 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1796 final int width = (int) grid.numColumns;
1797 final int height = (int) grid.numRows;
1798 final int hotseatWidth = (int) grid.numHotseatIcons;
Adam Cohen556f6132014-01-15 15:18:08 -08001799 PackageManager pm = mContext.getPackageManager();
Dan Sandlerd5024042014-01-09 15:01:33 -05001800
1801 final HashSet<String> seenIntents = new HashSet<String>(c.getCount());
1802
Adam Cohen72960972014-01-15 18:13:55 -08001803 final ArrayList<ContentValues> shortcuts = new ArrayList<ContentValues>();
1804 final ArrayList<ContentValues> folders = new ArrayList<ContentValues>();
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001805 final SparseArray<ContentValues> hotseat = new SparseArray<ContentValues>();
Dan Sandlerd5024042014-01-09 15:01:33 -05001806
1807 while (c.moveToNext()) {
1808 final int itemType = c.getInt(itemTypeIndex);
1809 if (itemType != Favorites.ITEM_TYPE_APPLICATION
1810 && itemType != Favorites.ITEM_TYPE_SHORTCUT
1811 && itemType != Favorites.ITEM_TYPE_FOLDER) {
1812 continue;
1813 }
1814
1815 final int cellX = c.getInt(cellXIndex);
1816 final int cellY = c.getInt(cellYIndex);
1817 final int screen = c.getInt(screenIndex);
1818 int container = c.getInt(containerIndex);
1819 final String intentStr = c.getString(intentIndex);
1820 Launcher.addDumpLog(TAG, "migrating \""
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001821 + c.getString(titleIndex) + "\" ("
1822 + cellX + "," + cellY + "@"
1823 + LauncherSettings.Favorites.containerToString(container)
1824 + "/" + screen
1825 + "): " + intentStr, true);
Dan Sandlerd5024042014-01-09 15:01:33 -05001826
1827 if (itemType != Favorites.ITEM_TYPE_FOLDER) {
Adam Cohen556f6132014-01-15 15:18:08 -08001828
1829 final Intent intent;
1830 final ComponentName cn;
1831 try {
1832 intent = Intent.parseUri(intentStr, 0);
1833 } catch (URISyntaxException e) {
1834 // bogus intent?
1835 Launcher.addDumpLog(TAG,
1836 "skipping invalid intent uri", true);
1837 continue;
1838 }
1839
1840 cn = intent.getComponent();
Dan Sandlerd5024042014-01-09 15:01:33 -05001841 if (TextUtils.isEmpty(intentStr)) {
1842 // no intent? no icon
1843 Launcher.addDumpLog(TAG, "skipping empty intent", true);
1844 continue;
Adam Cohen72960972014-01-15 18:13:55 -08001845 } else if (cn != null &&
1846 !LauncherModel.isValidPackageComponent(pm, cn)) {
Adam Cohen556f6132014-01-15 15:18:08 -08001847 // component no longer exists.
Adam Cohen72960972014-01-15 18:13:55 -08001848 Launcher.addDumpLog(TAG, "skipping item whose component " +
Adam Cohen556f6132014-01-15 15:18:08 -08001849 "no longer exists.", true);
1850 continue;
Adam Cohen72960972014-01-15 18:13:55 -08001851 } else if (container ==
1852 LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1853 // Dedupe icons directly on the workspace
1854
Adam Cohen556f6132014-01-15 15:18:08 -08001855 // Canonicalize
1856 // the Play Store sets the package parameter, but Launcher
1857 // does not, so we clear that out to keep them the same
1858 intent.setPackage(null);
1859 final String key = intent.toUri(0);
1860 if (seenIntents.contains(key)) {
1861 Launcher.addDumpLog(TAG, "skipping duplicate", true);
Dan Sandlerd5024042014-01-09 15:01:33 -05001862 continue;
Adam Cohen556f6132014-01-15 15:18:08 -08001863 } else {
1864 seenIntents.add(key);
Dan Sandlerd5024042014-01-09 15:01:33 -05001865 }
1866 }
1867 }
1868
1869 ContentValues values = new ContentValues(c.getColumnCount());
1870 values.put(LauncherSettings.Favorites._ID, c.getInt(idIndex));
1871 values.put(LauncherSettings.Favorites.INTENT, intentStr);
1872 values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex));
1873 values.put(LauncherSettings.Favorites.ICON_TYPE,
1874 c.getInt(iconTypeIndex));
1875 values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex));
1876 values.put(LauncherSettings.Favorites.ICON_PACKAGE,
1877 c.getString(iconPackageIndex));
1878 values.put(LauncherSettings.Favorites.ICON_RESOURCE,
1879 c.getString(iconResourceIndex));
1880 values.put(LauncherSettings.Favorites.ITEM_TYPE, itemType);
1881 values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1);
1882 values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex));
1883 values.put(LauncherSettings.Favorites.DISPLAY_MODE,
1884 c.getInt(displayModeIndex));
1885
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001886 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1887 hotseat.put(screen, values);
Dan Sandlerd5024042014-01-09 15:01:33 -05001888 }
1889
1890 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1891 // In a folder or in the hotseat, preserve position
1892 values.put(LauncherSettings.Favorites.SCREEN, screen);
1893 values.put(LauncherSettings.Favorites.CELLX, cellX);
1894 values.put(LauncherSettings.Favorites.CELLY, cellY);
1895 } else {
Adam Cohen72960972014-01-15 18:13:55 -08001896 // For items contained directly on one of the workspace screen,
1897 // we'll determine their location (screen, x, y) in a second pass.
Dan Sandlerd5024042014-01-09 15:01:33 -05001898 }
1899
1900 values.put(LauncherSettings.Favorites.CONTAINER, container);
1901
Adam Cohen72960972014-01-15 18:13:55 -08001902 if (itemType != Favorites.ITEM_TYPE_FOLDER) {
1903 shortcuts.add(values);
1904 } else {
1905 folders.add(values);
1906 }
Dan Sandlerd5024042014-01-09 15:01:33 -05001907 }
1908
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001909 // Now that we have all the hotseat icons, let's go through them left-right
1910 // and assign valid locations for them in the new hotseat
1911 final int N = hotseat.size();
1912 for (int idx=0; idx<N; idx++) {
1913 int hotseatX = hotseat.keyAt(idx);
1914 ContentValues values = hotseat.valueAt(idx);
1915
1916 if (hotseatX == grid.hotseatAllAppsRank) {
1917 // let's drop this in the next available hole in the hotseat
1918 while (++hotseatX < hotseatWidth) {
1919 if (hotseat.get(hotseatX) == null) {
1920 // found a spot! move it here
1921 values.put(LauncherSettings.Favorites.SCREEN,
1922 hotseatX);
1923 break;
1924 }
1925 }
1926 }
1927 if (hotseatX >= hotseatWidth) {
1928 // no room for you in the hotseat? it's off to the desktop with you
1929 values.put(LauncherSettings.Favorites.CONTAINER,
1930 Favorites.CONTAINER_DESKTOP);
1931 }
1932 }
1933
Adam Cohen72960972014-01-15 18:13:55 -08001934 final ArrayList<ContentValues> allItems = new ArrayList<ContentValues>();
1935 // Folders first
1936 allItems.addAll(folders);
1937 // Then shortcuts
1938 allItems.addAll(shortcuts);
1939
1940 // Layout all the folders
1941 for (ContentValues values: allItems) {
1942 if (values.getAsInteger(LauncherSettings.Favorites.CONTAINER) !=
1943 LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1944 // Hotseat items and folder items have already had their
1945 // location information set. Nothing to be done here.
1946 continue;
1947 }
1948 values.put(LauncherSettings.Favorites.SCREEN, curScreen);
1949 values.put(LauncherSettings.Favorites.CELLX, curX);
1950 values.put(LauncherSettings.Favorites.CELLY, curY);
1951 curX = (curX + 1) % width;
1952 if (curX == 0) {
1953 curY = (curY + 1);
1954 }
1955 // Leave the last row of icons blank on every screen
1956 if (curY == height - 1) {
1957 curScreen = (int) generateNewScreenId();
1958 curY = 0;
1959 }
1960 }
1961
1962 if (allItems.size() > 0) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001963 db.beginTransaction();
1964 try {
Adam Cohen72960972014-01-15 18:13:55 -08001965 for (ContentValues row: allItems) {
1966 if (row == null) continue;
1967 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, row)
Dan Sandlerd5024042014-01-09 15:01:33 -05001968 < 0) {
1969 return;
1970 } else {
1971 count++;
1972 }
1973 }
1974 db.setTransactionSuccessful();
1975 } finally {
1976 db.endTransaction();
1977 }
1978 }
1979
1980 db.beginTransaction();
1981 try {
1982 for (i=0; i<=curScreen; i++) {
1983 final ContentValues values = new ContentValues();
1984 values.put(LauncherSettings.WorkspaceScreens._ID, i);
1985 values.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
1986 if (dbInsertAndCheck(this, db, TABLE_WORKSPACE_SCREENS, null, values)
1987 < 0) {
1988 return;
1989 }
1990 }
1991 db.setTransactionSuccessful();
1992 } finally {
1993 db.endTransaction();
1994 }
1995 }
1996 } finally {
1997 c.close();
1998 }
1999 }
2000
2001 Launcher.addDumpLog(TAG, "migrated " + count + " icons from Launcher2 into "
2002 + (curScreen+1) + " screens", true);
2003
2004 // ensure that new screens are created to hold these icons
2005 setFlagJustLoadedOldDb();
2006
2007 // Update max IDs; very important since we just grabbed IDs from another database
2008 mMaxItemId = initializeMaxItemId(db);
2009 mMaxScreenId = initializeMaxScreenId(db);
2010 if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId + " mMaxScreenId: " + mMaxScreenId);
2011 }
Mike Cleronb87bd162009-10-30 16:36:56 -07002012 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07002013
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 /**
2015 * Build a query string that will match any row where the column matches
2016 * anything in the values list.
2017 */
2018 static String buildOrWhereString(String column, int[] values) {
2019 StringBuilder selectWhere = new StringBuilder();
2020 for (int i = values.length - 1; i >= 0; i--) {
2021 selectWhere.append(column).append("=").append(values[i]);
2022 if (i > 0) {
2023 selectWhere.append(" OR ");
2024 }
2025 }
2026 return selectWhere.toString();
2027 }
2028
Jeff Sharkey5aeef582014-04-14 13:26:42 -07002029 /**
2030 * Return attribute value, attempting launcher-specific namespace first
2031 * before falling back to anonymous attribute.
2032 */
2033 static String getAttributeValue(XmlResourceParser parser, String attribute) {
2034 String value = parser.getAttributeValue(
2035 "http://schemas.android.com/apk/res-auto/com.android.launcher3", attribute);
2036 if (value == null) {
2037 value = parser.getAttributeValue(null, attribute);
2038 }
2039 return value;
2040 }
2041
2042 /**
2043 * Return attribute resource value, attempting launcher-specific namespace
2044 * first before falling back to anonymous attribute.
2045 */
2046 static int getAttributeResourceValue(XmlResourceParser parser, String attribute,
2047 int defaultValue) {
2048 int value = parser.getAttributeResourceValue(
2049 "http://schemas.android.com/apk/res-auto/com.android.launcher3", attribute,
2050 defaultValue);
2051 if (value == defaultValue) {
2052 value = parser.getAttributeResourceValue(null, attribute, defaultValue);
2053 }
2054 return value;
2055 }
2056
2057 private static void copyInteger(ContentValues from, ContentValues to, String key) {
2058 to.put(key, from.getAsInteger(key));
2059 }
2060
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002061 static class SqlArguments {
2062 public final String table;
2063 public final String where;
2064 public final String[] args;
2065
2066 SqlArguments(Uri url, String where, String[] args) {
2067 if (url.getPathSegments().size() == 1) {
2068 this.table = url.getPathSegments().get(0);
2069 this.where = where;
2070 this.args = args;
2071 } else if (url.getPathSegments().size() != 2) {
2072 throw new IllegalArgumentException("Invalid URI: " + url);
2073 } else if (!TextUtils.isEmpty(where)) {
2074 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
2075 } else {
2076 this.table = url.getPathSegments().get(0);
Daniel Lehmannc3a80402012-04-23 21:35:11 -07002077 this.where = "_id=" + ContentUris.parseId(url);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002078 this.args = null;
2079 }
2080 }
2081
2082 SqlArguments(Uri url) {
2083 if (url.getPathSegments().size() == 1) {
2084 table = url.getPathSegments().get(0);
2085 where = null;
2086 args = null;
2087 } else {
2088 throw new IllegalArgumentException("Invalid URI: " + url);
2089 }
2090 }
2091 }
Adam Cohen72960972014-01-15 18:13:55 -08002092}