blob: 9a004f2ddee0755ba5799400afa38126b547e590 [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 Cohen119285e2014-04-02 16:59:08 -070078 private static final int DATABASE_VERSION = 18;
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) {
506 db.execSQL("DELETE FROM " + TABLE_FAVORITES + " WHERE " +
507 LauncherSettings.Favorites.SCREEN + " NOT IN (SELECT " +
508 LauncherSettings.WorkspaceScreens._ID + " FROM " + TABLE_WORKSPACE_SCREENS +
509 ")");
510 }
511
Winson Chungc763c4e2013-07-19 13:49:06 -0700512 private void setFlagJustLoadedOldDb() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400513 String spKey = LauncherAppState.getSharedPreferencesKey();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700514 SharedPreferences sp = mContext.getSharedPreferences(spKey, Context.MODE_PRIVATE);
515 SharedPreferences.Editor editor = sp.edit();
Winson Chungc763c4e2013-07-19 13:49:06 -0700516 editor.putBoolean(UPGRADED_FROM_OLD_DATABASE, true);
517 editor.putBoolean(EMPTY_DATABASE_CREATED, false);
Michael Jurkab85f8a42012-04-25 15:48:32 -0700518 editor.commit();
519 }
520
Winson Chungc763c4e2013-07-19 13:49:06 -0700521 private void setFlagEmptyDbCreated() {
522 String spKey = LauncherAppState.getSharedPreferencesKey();
523 SharedPreferences sp = mContext.getSharedPreferences(spKey, Context.MODE_PRIVATE);
524 SharedPreferences.Editor editor = sp.edit();
525 editor.putBoolean(EMPTY_DATABASE_CREATED, true);
526 editor.putBoolean(UPGRADED_FROM_OLD_DATABASE, false);
527 editor.commit();
528 }
529
530 // We rearrange the screens from the old launcher
531 // 12345 -> 34512
532 private long upgradeLauncherDb_permuteScreens(long screen) {
533 if (screen >= 2) {
534 return screen - 2;
535 } else {
536 return screen + 3;
537 }
538 }
539
540 private boolean convertDatabase(SQLiteDatabase db, Uri uri,
541 ContentValuesCallback cb, boolean deleteRows) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800542 if (LOGD) Log.d(TAG, "converting database from an older format, but not onUpgrade");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800543 boolean converted = false;
544
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800545 final ContentResolver resolver = mContext.getContentResolver();
546 Cursor cursor = null;
547
548 try {
549 cursor = resolver.query(uri, null, null, null, null);
550 } catch (Exception e) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700551 // Ignore
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800552 }
553
554 // We already have a favorites database in the old provider
Winson Chungc763c4e2013-07-19 13:49:06 -0700555 if (cursor != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800556 try {
Winson Chungc763c4e2013-07-19 13:49:06 -0700557 if (cursor.getCount() > 0) {
558 converted = copyFromCursor(db, cursor, cb) > 0;
559 if (converted && deleteRows) {
560 resolver.delete(uri, null, null);
561 }
562 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800563 } finally {
564 cursor.close();
565 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800566 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700567
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800568 if (converted) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700569 // Convert widgets from this import into widgets
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800570 if (LOGD) Log.d(TAG, "converted and now triggering widget upgrade");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800571 convertWidgets(db);
Winson Chungc763c4e2013-07-19 13:49:06 -0700572
573 // Update max item id
574 mMaxItemId = initializeMaxItemId(db);
575 if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800576 }
577
578 return converted;
579 }
580
Winson Chungc763c4e2013-07-19 13:49:06 -0700581 private int copyFromCursor(SQLiteDatabase db, Cursor c, ContentValuesCallback cb) {
Romain Guy73b979d2009-06-09 12:57:21 -0700582 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800583 final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
584 final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
585 final int iconTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
586 final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
587 final int iconPackageIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
588 final int iconResourceIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
589 final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
590 final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
591 final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
592 final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
593 final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
594 final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
595 final int displayModeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
596
597 ContentValues[] rows = new ContentValues[c.getCount()];
598 int i = 0;
599 while (c.moveToNext()) {
600 ContentValues values = new ContentValues(c.getColumnCount());
Romain Guy73b979d2009-06-09 12:57:21 -0700601 values.put(LauncherSettings.Favorites._ID, c.getLong(idIndex));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800602 values.put(LauncherSettings.Favorites.INTENT, c.getString(intentIndex));
603 values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex));
604 values.put(LauncherSettings.Favorites.ICON_TYPE, c.getInt(iconTypeIndex));
605 values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex));
606 values.put(LauncherSettings.Favorites.ICON_PACKAGE, c.getString(iconPackageIndex));
607 values.put(LauncherSettings.Favorites.ICON_RESOURCE, c.getString(iconResourceIndex));
608 values.put(LauncherSettings.Favorites.CONTAINER, c.getInt(containerIndex));
609 values.put(LauncherSettings.Favorites.ITEM_TYPE, c.getInt(itemTypeIndex));
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700610 values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611 values.put(LauncherSettings.Favorites.SCREEN, c.getInt(screenIndex));
612 values.put(LauncherSettings.Favorites.CELLX, c.getInt(cellXIndex));
613 values.put(LauncherSettings.Favorites.CELLY, c.getInt(cellYIndex));
614 values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex));
615 values.put(LauncherSettings.Favorites.DISPLAY_MODE, c.getInt(displayModeIndex));
Winson Chungc763c4e2013-07-19 13:49:06 -0700616 if (cb != null) {
617 cb.onRow(values);
618 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800619 rows[i++] = values;
620 }
621
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622 int total = 0;
Winson Chungc763c4e2013-07-19 13:49:06 -0700623 if (i > 0) {
624 db.beginTransaction();
625 try {
626 int numValues = rows.length;
627 for (i = 0; i < numValues; i++) {
628 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, rows[i]) < 0) {
629 return 0;
630 } else {
631 total++;
632 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633 }
Winson Chungc763c4e2013-07-19 13:49:06 -0700634 db.setTransactionSuccessful();
635 } finally {
636 db.endTransaction();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638 }
639
640 return total;
641 }
642
643 @Override
644 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Winson Chungc763c4e2013-07-19 13:49:06 -0700645 if (LOGD) Log.d(TAG, "onUpgrade triggered: " + oldVersion);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700646
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647 int version = oldVersion;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700648 if (version < 3) {
649 // upgrade 1,2 -> 3 added appWidgetId column
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 db.beginTransaction();
651 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700652 // Insert new column for holding appWidgetIds
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 db.execSQL("ALTER TABLE favorites " +
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700654 "ADD COLUMN appWidgetId INTEGER NOT NULL DEFAULT -1;");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800655 db.setTransactionSuccessful();
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700656 version = 3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 } catch (SQLException ex) {
658 // Old version remains, which means we wipe old data
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800659 Log.e(TAG, ex.getMessage(), ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 } finally {
661 db.endTransaction();
662 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700663
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 // Convert existing widgets only if table upgrade was successful
The Android Open Source Projectca9475f2009-03-13 13:04:24 -0700665 if (version == 3) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800666 convertWidgets(db);
667 }
668 }
Romain Guy73b979d2009-06-09 12:57:21 -0700669
670 if (version < 4) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800671 version = 4;
Romain Guy73b979d2009-06-09 12:57:21 -0700672 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700673
Romain Guy509cd6a2010-03-23 15:10:56 -0700674 // Where's version 5?
675 // - Donut and sholes on 2.0 shipped with version 4 of launcher1.
Daniel Sandler325dc232013-06-05 22:57:57 -0400676 // - Passion shipped on 2.1 with version 6 of launcher3
Romain Guy509cd6a2010-03-23 15:10:56 -0700677 // - Sholes shipped on 2.1r1 (aka Mr. 3) with version 5 of launcher 1
678 // but version 5 on there was the updateContactsShortcuts change
679 // which was version 6 in launcher 2 (first shipped on passion 2.1r1).
680 // The updateContactsShortcuts change is idempotent, so running it twice
681 // is okay so we'll do that when upgrading the devices that shipped with it.
682 if (version < 6) {
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800683 // We went from 3 to 5 screens. Move everything 1 to the right
684 db.beginTransaction();
685 try {
686 db.execSQL("UPDATE favorites SET screen=(screen + 1);");
687 db.setTransactionSuccessful();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800688 } catch (SQLException ex) {
689 // Old version remains, which means we wipe old data
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800690 Log.e(TAG, ex.getMessage(), ex);
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800691 } finally {
692 db.endTransaction();
693 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700694
Romain Guy509cd6a2010-03-23 15:10:56 -0700695 // We added the fast track.
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800696 if (updateContactsShortcuts(db)) {
697 version = 6;
698 }
699 }
Bjorn Bringert7984c942009-12-09 15:38:25 +0000700
701 if (version < 7) {
702 // Version 7 gets rid of the special search widget.
703 convertWidgets(db);
704 version = 7;
705 }
706
Joe Onorato0589f0f2010-02-08 13:44:00 -0800707 if (version < 8) {
708 // Version 8 (froyo) has the icons all normalized. This should
709 // already be the case in practice, but we now rely on it and don't
710 // resample the images each time.
711 normalizeIcons(db);
712 version = 8;
713 }
714
Winson Chung3d503fb2011-07-13 17:25:49 -0700715 if (version < 9) {
716 // The max id is not yet set at this point (onUpgrade is triggered in the ctor
717 // before it gets a change to get set, so we need to read it here when we use it)
Adam Cohendcd297f2013-06-18 13:13:40 -0700718 if (mMaxItemId == -1) {
719 mMaxItemId = initializeMaxItemId(db);
Winson Chung3d503fb2011-07-13 17:25:49 -0700720 }
721
722 // Add default hotseat icons
Winson Chung6d092682011-11-16 18:43:26 -0800723 loadFavorites(db, R.xml.update_workspace);
Winson Chung3d503fb2011-07-13 17:25:49 -0700724 version = 9;
725 }
726
Daniel Lehmannd02402c2012-05-14 18:30:53 -0700727 // We bumped the version three time during JB, once to update the launch flags, once to
728 // update the override for the default launch animation and once to set the mimetype
729 // to improve startup performance
730 if (version < 12) {
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700731 // Contact shortcuts need a different set of flags to be launched now
732 // The updateContactsShortcuts change is idempotent, so we can keep using it like
733 // back in the Donut days
734 updateContactsShortcuts(db);
Daniel Lehmannd02402c2012-05-14 18:30:53 -0700735 version = 12;
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700736 }
737
Adam Cohendcd297f2013-06-18 13:13:40 -0700738 if (version < 13) {
739 // With the new shrink-wrapped and re-orderable workspaces, it makes sense
740 // to persist workspace screens and their relative order.
741 mMaxScreenId = 0;
742
743 // This will never happen in the wild, but when we switch to using workspace
744 // screen ids, redo the import from old launcher.
Winson Chungc763c4e2013-07-19 13:49:06 -0700745 sJustLoadedFromOldDb = true;
Adam Cohendcd297f2013-06-18 13:13:40 -0700746
747 addWorkspacesTable(db);
748 version = 13;
749 }
750
Chris Wrend5e66bf2013-09-16 14:02:29 -0400751 if (version < 14) {
752 db.beginTransaction();
753 try {
754 // Insert new column for holding widget provider name
755 db.execSQL("ALTER TABLE favorites " +
756 "ADD COLUMN appWidgetProvider TEXT;");
757 db.setTransactionSuccessful();
758 version = 14;
759 } catch (SQLException ex) {
760 // Old version remains, which means we wipe old data
761 Log.e(TAG, ex.getMessage(), ex);
762 } finally {
763 db.endTransaction();
764 }
765 }
766
Chris Wren1ada10d2013-09-13 18:01:38 -0400767 if (version < 15) {
768 db.beginTransaction();
769 try {
770 // Insert new column for holding update timestamp
771 db.execSQL("ALTER TABLE favorites " +
772 "ADD COLUMN modified INTEGER NOT NULL DEFAULT 0;");
773 db.execSQL("ALTER TABLE workspaceScreens " +
774 "ADD COLUMN modified INTEGER NOT NULL DEFAULT 0;");
775 db.setTransactionSuccessful();
776 version = 15;
777 } catch (SQLException ex) {
778 // Old version remains, which means we wipe old data
779 Log.e(TAG, ex.getMessage(), ex);
780 } finally {
781 db.endTransaction();
782 }
783 }
784
Chris Wrenf4d08112014-01-16 18:13:56 -0500785
786 if (version < 16) {
787 db.beginTransaction();
788 try {
789 // Insert new column for holding restore status
790 db.execSQL("ALTER TABLE favorites " +
791 "ADD COLUMN restored INTEGER NOT NULL DEFAULT 0;");
792 db.setTransactionSuccessful();
793 version = 16;
794 } catch (SQLException ex) {
795 // Old version remains, which means we wipe old data
796 Log.e(TAG, ex.getMessage(), ex);
797 } finally {
798 db.endTransaction();
799 }
800 }
801
Adam Cohen71e03b92014-02-21 14:09:53 -0800802 if (version < 17) {
803 // We use the db version upgrade here to identify users who may not have seen
804 // clings yet (because they weren't available), but for whom the clings are now
805 // available (tablet users). Because one of the possible cling flows (migration)
806 // is very destructive (wipes out workspaces), we want to prevent this from showing
807 // until clear data. We do so by marking that the clings have been shown.
808 LauncherClings.synchonouslyMarkFirstRunClingDismissed(mContext);
809 version = 17;
810 }
811
Adam Cohen119285e2014-04-02 16:59:08 -0700812 if (version < 18) {
813 // Due to a data loss bug, some users may have items associated with screen ids
814 // which no longer exist. Since this can cause other problems, and since the user
815 // will never see these items anyway, we use database upgrade as an opportunity to
816 // clean things up.
Adam Cohena7946072014-04-10 18:06:59 -0700817
818 // TODO: this needs to be fixed, currently causes data loss.
819 //removeOrphanedItems(db);
Adam Cohen119285e2014-04-02 16:59:08 -0700820 version = 18;
821 }
822
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800823 if (version != DATABASE_VERSION) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800824 Log.w(TAG, "Destroying all old data.");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825 db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES);
Adam Cohendcd297f2013-06-18 13:13:40 -0700826 db.execSQL("DROP TABLE IF EXISTS " + TABLE_WORKSPACE_SCREENS);
827
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828 onCreate(db);
829 }
830 }
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800831
832 private boolean updateContactsShortcuts(SQLiteDatabase db) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800833 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE,
834 new int[] { Favorites.ITEM_TYPE_SHORTCUT });
835
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700836 Cursor c = null;
837 final String actionQuickContact = "com.android.contacts.action.QUICK_CONTACT";
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800838 db.beginTransaction();
839 try {
840 // Select and iterate through each matching widget
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700841 c = db.query(TABLE_FAVORITES,
842 new String[] { Favorites._ID, Favorites.INTENT },
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800843 selectWhere, null, null, null, null);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700844 if (c == null) return false;
845
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800846 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700847
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800848 final int idIndex = c.getColumnIndex(Favorites._ID);
849 final int intentIndex = c.getColumnIndex(Favorites.INTENT);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700850
851 while (c.moveToNext()) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800852 long favoriteId = c.getLong(idIndex);
853 final String intentUri = c.getString(intentIndex);
854 if (intentUri != null) {
855 try {
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700856 final Intent intent = Intent.parseUri(intentUri, 0);
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800857 android.util.Log.d("Home", intent.toString());
858 final Uri uri = intent.getData();
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700859 if (uri != null) {
860 final String data = uri.toString();
861 if ((Intent.ACTION_VIEW.equals(intent.getAction()) ||
862 actionQuickContact.equals(intent.getAction())) &&
863 (data.startsWith("content://contacts/people/") ||
864 data.startsWith("content://com.android.contacts/" +
865 "contacts/lookup/"))) {
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800866
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700867 final Intent newIntent = new Intent(actionQuickContact);
868 // When starting from the launcher, start in a new, cleared task
869 // CLEAR_WHEN_TASK_RESET cannot reset the root of a task, so we
870 // clear the whole thing preemptively here since
871 // QuickContactActivity will finish itself when launching other
872 // detail activities.
873 newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
874 Intent.FLAG_ACTIVITY_CLEAR_TASK);
Winson Chung2672ff92012-05-04 16:22:30 -0700875 newIntent.putExtra(
876 Launcher.INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION, true);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700877 newIntent.setData(uri);
Daniel Lehmannd02402c2012-05-14 18:30:53 -0700878 // Determine the type and also put that in the shortcut
879 // (that can speed up launch a bit)
880 newIntent.setDataAndType(uri, newIntent.resolveType(mContext));
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800881
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700882 final ContentValues values = new ContentValues();
883 values.put(LauncherSettings.Favorites.INTENT,
884 newIntent.toUri(0));
885
886 String updateWhere = Favorites._ID + "=" + favoriteId;
887 db.update(TABLE_FAVORITES, values, updateWhere, null);
888 }
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800889 }
890 } catch (RuntimeException ex) {
891 Log.e(TAG, "Problem upgrading shortcut", ex);
892 } catch (URISyntaxException e) {
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700893 Log.e(TAG, "Problem upgrading shortcut", e);
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800894 }
895 }
896 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700897
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800898 db.setTransactionSuccessful();
899 } catch (SQLException ex) {
900 Log.w(TAG, "Problem while upgrading contacts", ex);
901 return false;
902 } finally {
903 db.endTransaction();
904 if (c != null) {
905 c.close();
906 }
907 }
908
909 return true;
910 }
911
Joe Onorato0589f0f2010-02-08 13:44:00 -0800912 private void normalizeIcons(SQLiteDatabase db) {
913 Log.d(TAG, "normalizing icons");
914
Joe Onorato346e1292010-02-18 10:34:24 -0500915 db.beginTransaction();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800916 Cursor c = null;
Joe Onorato9690b392010-03-23 17:34:37 -0400917 SQLiteStatement update = null;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800918 try {
919 boolean logged = false;
Joe Onorato9690b392010-03-23 17:34:37 -0400920 update = db.compileStatement("UPDATE favorites "
Jeff Hamiltoneaf77d62010-02-13 00:08:17 -0600921 + "SET icon=? WHERE _id=?");
Joe Onorato0589f0f2010-02-08 13:44:00 -0800922
923 c = db.rawQuery("SELECT _id, icon FROM favorites WHERE iconType=" +
924 Favorites.ICON_TYPE_BITMAP, null);
925
926 final int idIndex = c.getColumnIndexOrThrow(Favorites._ID);
927 final int iconIndex = c.getColumnIndexOrThrow(Favorites.ICON);
928
929 while (c.moveToNext()) {
930 long id = c.getLong(idIndex);
931 byte[] data = c.getBlob(iconIndex);
932 try {
933 Bitmap bitmap = Utilities.resampleIconBitmap(
934 BitmapFactory.decodeByteArray(data, 0, data.length),
935 mContext);
936 if (bitmap != null) {
937 update.bindLong(1, id);
938 data = ItemInfo.flattenBitmap(bitmap);
939 if (data != null) {
940 update.bindBlob(2, data);
941 update.execute();
942 }
943 bitmap.recycle();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800944 }
945 } catch (Exception e) {
946 if (!logged) {
947 Log.e(TAG, "Failed normalizing icon " + id, e);
948 } else {
949 Log.e(TAG, "Also failed normalizing icon " + id);
950 }
951 logged = true;
952 }
953 }
Bjorn Bringert3a928e42010-02-19 11:15:40 +0000954 db.setTransactionSuccessful();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800955 } catch (SQLException ex) {
956 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
957 } finally {
958 db.endTransaction();
Joe Onorato9690b392010-03-23 17:34:37 -0400959 if (update != null) {
960 update.close();
961 }
Joe Onorato0589f0f2010-02-08 13:44:00 -0800962 if (c != null) {
963 c.close();
964 }
965 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700966 }
967
968 // Generates a new ID to use for an object in your database. This method should be only
969 // called from the main UI thread. As an exception, we do call it when we call the
970 // constructor from the worker thread; however, this doesn't extend until after the
971 // constructor is called, and we only pass a reference to LauncherProvider to LauncherApp
972 // after that point
Adam Cohendcd297f2013-06-18 13:13:40 -0700973 public long generateNewItemId() {
974 if (mMaxItemId < 0) {
975 throw new RuntimeException("Error: max item id was not initialized");
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700976 }
Adam Cohendcd297f2013-06-18 13:13:40 -0700977 mMaxItemId += 1;
978 return mMaxItemId;
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700979 }
980
Winson Chungc763c4e2013-07-19 13:49:06 -0700981 public void updateMaxItemId(long id) {
982 mMaxItemId = id + 1;
983 }
984
Chris Wren5dee7af2013-12-20 17:22:11 -0500985 public void checkId(String table, ContentValues values) {
986 long id = values.getAsLong(LauncherSettings.BaseLauncherColumns._ID);
987 if (table == LauncherProvider.TABLE_WORKSPACE_SCREENS) {
988 mMaxScreenId = Math.max(id, mMaxScreenId);
989 } else {
990 mMaxItemId = Math.max(id, mMaxItemId);
991 }
992 }
993
Adam Cohendcd297f2013-06-18 13:13:40 -0700994 private long initializeMaxItemId(SQLiteDatabase db) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700995 Cursor c = db.rawQuery("SELECT MAX(_id) FROM favorites", null);
996
997 // get the result
998 final int maxIdIndex = 0;
999 long id = -1;
1000 if (c != null && c.moveToNext()) {
1001 id = c.getLong(maxIdIndex);
1002 }
Michael Jurka5130e402011-10-13 04:55:35 -07001003 if (c != null) {
1004 c.close();
1005 }
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001006
1007 if (id == -1) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001008 throw new RuntimeException("Error: could not query max item id");
1009 }
1010
1011 return id;
1012 }
1013
1014 // Generates a new ID to use for an workspace screen in your database. This method
1015 // should be only called from the main UI thread. As an exception, we do call it when we
1016 // call the constructor from the worker thread; however, this doesn't extend until after the
1017 // constructor is called, and we only pass a reference to LauncherProvider to LauncherApp
1018 // after that point
1019 public long generateNewScreenId() {
1020 if (mMaxScreenId < 0) {
1021 throw new RuntimeException("Error: max screen id was not initialized");
1022 }
1023 mMaxScreenId += 1;
Winson Chunga90303b2013-11-15 13:05:06 -08001024 // Log to disk
1025 Launcher.addDumpLog(TAG, "11683562 - generateNewScreenId(): " + mMaxScreenId, true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001026 return mMaxScreenId;
1027 }
1028
1029 public void updateMaxScreenId(long maxScreenId) {
Winson Chunga90303b2013-11-15 13:05:06 -08001030 // Log to disk
1031 Launcher.addDumpLog(TAG, "11683562 - updateMaxScreenId(): " + maxScreenId, true);
Adam Cohendcd297f2013-06-18 13:13:40 -07001032 mMaxScreenId = maxScreenId;
1033 }
1034
1035 private long initializeMaxScreenId(SQLiteDatabase db) {
1036 Cursor c = db.rawQuery("SELECT MAX(" + LauncherSettings.WorkspaceScreens._ID + ") FROM " + TABLE_WORKSPACE_SCREENS, null);
1037
1038 // get the result
1039 final int maxIdIndex = 0;
1040 long id = -1;
1041 if (c != null && c.moveToNext()) {
1042 id = c.getLong(maxIdIndex);
1043 }
1044 if (c != null) {
1045 c.close();
1046 }
1047
1048 if (id == -1) {
1049 throw new RuntimeException("Error: could not query max screen id");
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001050 }
1051
Winson Chunga90303b2013-11-15 13:05:06 -08001052 // Log to disk
1053 Launcher.addDumpLog(TAG, "11683562 - initializeMaxScreenId(): " + id, true);
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001054 return id;
Joe Onorato0589f0f2010-02-08 13:44:00 -08001055 }
1056
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001058 * Upgrade existing clock and photo frame widgets into their new widget
Bjorn Bringert93c45762009-12-16 13:19:47 +00001059 * equivalents.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001060 */
1061 private void convertWidgets(SQLiteDatabase db) {
Bjorn Bringert34251342009-12-15 13:33:11 +00001062 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001063 final int[] bindSources = new int[] {
1064 Favorites.ITEM_TYPE_WIDGET_CLOCK,
1065 Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME,
Bjorn Bringert7984c942009-12-09 15:38:25 +00001066 Favorites.ITEM_TYPE_WIDGET_SEARCH,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001067 };
Bjorn Bringert7984c942009-12-09 15:38:25 +00001068
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069 final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE, bindSources);
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001070
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001071 Cursor c = null;
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001072
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001073 db.beginTransaction();
1074 try {
1075 // Select and iterate through each matching widget
Bjorn Bringert7984c942009-12-09 15:38:25 +00001076 c = db.query(TABLE_FAVORITES, new String[] { Favorites._ID, Favorites.ITEM_TYPE },
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 selectWhere, null, null, null, null);
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001078
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001079 if (LOGD) Log.d(TAG, "found upgrade cursor count=" + c.getCount());
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001080
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081 final ContentValues values = new ContentValues();
1082 while (c != null && c.moveToNext()) {
1083 long favoriteId = c.getLong(0);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001084 int favoriteType = c.getInt(1);
1085
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001086 // Allocate and update database with new appWidgetId
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001088 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001089
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001090 if (LOGD) {
1091 Log.d(TAG, "allocated appWidgetId=" + appWidgetId
1092 + " for favoriteId=" + favoriteId);
1093 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 values.clear();
Bjorn Bringert7984c942009-12-09 15:38:25 +00001095 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
1096 values.put(Favorites.APPWIDGET_ID, appWidgetId);
1097
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098 // Original widgets might not have valid spans when upgrading
Bjorn Bringert7984c942009-12-09 15:38:25 +00001099 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
1100 values.put(LauncherSettings.Favorites.SPANX, 4);
1101 values.put(LauncherSettings.Favorites.SPANY, 1);
1102 } else {
1103 values.put(LauncherSettings.Favorites.SPANX, 2);
1104 values.put(LauncherSettings.Favorites.SPANY, 2);
1105 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001106
1107 String updateWhere = Favorites._ID + "=" + favoriteId;
1108 db.update(TABLE_FAVORITES, values, updateWhere, null);
Bjorn Bringert34251342009-12-15 13:33:11 +00001109
Bjorn Bringert34251342009-12-15 13:33:11 +00001110 if (favoriteType == Favorites.ITEM_TYPE_WIDGET_CLOCK) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001111 // TODO: check return value
1112 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
Bjorn Bringert34251342009-12-15 13:33:11 +00001113 new ComponentName("com.android.alarmclock",
1114 "com.android.alarmclock.AnalogAppWidgetProvider"));
1115 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001116 // TODO: check return value
1117 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
Bjorn Bringert34251342009-12-15 13:33:11 +00001118 new ComponentName("com.android.camera",
1119 "com.android.camera.PhotoAppWidgetProvider"));
1120 } else if (favoriteType == Favorites.ITEM_TYPE_WIDGET_SEARCH) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001121 // TODO: check return value
1122 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
Bjorn Bringertcd8fec02010-01-14 13:26:43 +00001123 getSearchWidgetProvider());
Bjorn Bringert34251342009-12-15 13:33:11 +00001124 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001126 Log.e(TAG, "Problem allocating appWidgetId", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001127 }
1128 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001129
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001130 db.setTransactionSuccessful();
1131 } catch (SQLException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001132 Log.w(TAG, "Problem while allocating appWidgetIds for existing widgets", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133 } finally {
1134 db.endTransaction();
1135 if (c != null) {
1136 c.close();
1137 }
1138 }
Winson Chungc763c4e2013-07-19 13:49:06 -07001139
1140 // Update max item id
1141 mMaxItemId = initializeMaxItemId(db);
1142 if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143 }
1144
Michael Jurka8b805b12012-04-18 14:23:14 -07001145 private static final void beginDocument(XmlPullParser parser, String firstElementName)
1146 throws XmlPullParserException, IOException {
1147 int type;
Michael Jurka9bc8eba2012-05-21 20:36:44 -07001148 while ((type = parser.next()) != XmlPullParser.START_TAG
1149 && type != XmlPullParser.END_DOCUMENT) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001150 ;
1151 }
1152
Michael Jurka9bc8eba2012-05-21 20:36:44 -07001153 if (type != XmlPullParser.START_TAG) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001154 throw new XmlPullParserException("No start tag found");
1155 }
1156
1157 if (!parser.getName().equals(firstElementName)) {
1158 throw new XmlPullParserException("Unexpected start tag: found " + parser.getName() +
1159 ", expected " + firstElementName);
1160 }
1161 }
1162
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001163 private static Intent buildMainIntent() {
1164 Intent intent = new Intent(Intent.ACTION_MAIN, null);
1165 intent.addCategory(Intent.CATEGORY_LAUNCHER);
1166 return intent;
1167 }
1168
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001169 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 * Loads the default set of favorite packages from an xml file.
1171 *
1172 * @param db The database to write the values into
Winson Chung3d503fb2011-07-13 17:25:49 -07001173 * @param filterContainerId The specific container id of items to load
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 */
Winson Chung6d092682011-11-16 18:43:26 -08001175 private int loadFavorites(SQLiteDatabase db, int workspaceResourceId) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001176 ContentValues values = new ContentValues();
1177
Daniel Sandler57dac262013-10-03 13:28:36 -04001178 if (LOGD) Log.v(TAG, String.format("Loading favorites from resid=0x%08x", workspaceResourceId));
1179
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001180 int i = 0;
1181 try {
Winson Chung6d092682011-11-16 18:43:26 -08001182 XmlResourceParser parser = mContext.getResources().getXml(workspaceResourceId);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001183 AttributeSet attrs = Xml.asAttributeSet(parser);
Michael Jurka8b805b12012-04-18 14:23:14 -07001184 beginDocument(parser, TAG_FAVORITES);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001185
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001186 final int depth = parser.getDepth();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001187
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001188 int type;
1189 while (((type = parser.next()) != XmlPullParser.END_TAG ||
1190 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
1191
1192 if (type != XmlPullParser.START_TAG) {
1193 continue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001194 }
1195
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001196 boolean added = false;
1197 final String name = parser.getName();
1198
Daniel Sandler57dac262013-10-03 13:28:36 -04001199 if (TAG_INCLUDE.equals(name)) {
1200 final TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.Include);
1201
1202 final int resId = a.getResourceId(R.styleable.Include_workspace, 0);
1203
1204 if (LOGD) Log.v(TAG, String.format(("%" + (2*(depth+1)) + "s<include workspace=%08x>"),
1205 "", resId));
1206
1207 if (resId != 0 && resId != workspaceResourceId) {
1208 // recursively load some more favorites, why not?
1209 i += loadFavorites(db, resId);
1210 added = false;
Daniel Sandler57dac262013-10-03 13:28:36 -04001211 } else {
1212 Log.w(TAG, String.format("Skipping <include workspace=0x%08x>", resId));
1213 }
1214
1215 a.recycle();
1216
1217 if (LOGD) Log.v(TAG, String.format(("%" + (2*(depth+1)) + "s</include>"), ""));
1218 continue;
1219 }
1220
1221 // Assuming it's a <favorite> at this point
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001222 TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.Favorite);
1223
Winson Chung3d503fb2011-07-13 17:25:49 -07001224 long container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
1225 if (a.hasValue(R.styleable.Favorite_container)) {
1226 container = Long.valueOf(a.getString(R.styleable.Favorite_container));
1227 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001228
Winson Chung6d092682011-11-16 18:43:26 -08001229 String screen = a.getString(R.styleable.Favorite_screen);
1230 String x = a.getString(R.styleable.Favorite_x);
1231 String y = a.getString(R.styleable.Favorite_y);
1232
Winson Chung6d092682011-11-16 18:43:26 -08001233 values.clear();
1234 values.put(LauncherSettings.Favorites.CONTAINER, container);
1235 values.put(LauncherSettings.Favorites.SCREEN, screen);
1236 values.put(LauncherSettings.Favorites.CELLX, x);
1237 values.put(LauncherSettings.Favorites.CELLY, y);
1238
Daniel Sandler57dac262013-10-03 13:28:36 -04001239 if (LOGD) {
1240 final String title = a.getString(R.styleable.Favorite_title);
1241 final String pkg = a.getString(R.styleable.Favorite_packageName);
1242 final String something = title != null ? title : pkg;
1243 Log.v(TAG, String.format(
1244 ("%" + (2*(depth+1)) + "s<%s%s c=%d s=%s x=%s y=%s>"),
1245 "", name,
1246 (something == null ? "" : (" \"" + something + "\"")),
1247 container, screen, x, y));
1248 }
1249
Winson Chung6d092682011-11-16 18:43:26 -08001250 if (TAG_FAVORITE.equals(name)) {
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001251 long id = addAppShortcut(db, values, parser);
Winson Chung6d092682011-11-16 18:43:26 -08001252 added = id >= 0;
1253 } else if (TAG_SEARCH.equals(name)) {
1254 added = addSearchWidget(db, values);
1255 } else if (TAG_CLOCK.equals(name)) {
1256 added = addClockWidget(db, values);
1257 } else if (TAG_APPWIDGET.equals(name)) {
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001258 added = addAppWidget(parser, attrs, type, db, values, a);
Winson Chung6d092682011-11-16 18:43:26 -08001259 } else if (TAG_SHORTCUT.equals(name)) {
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001260 long id = addUriShortcut(db, values, mContext.getResources(), parser);
Winson Chung6d092682011-11-16 18:43:26 -08001261 added = id >= 0;
Jason Monk41314972014-03-03 16:11:30 -05001262 } else if (TAG_RESOLVE.equals(name)) {
1263 // This looks through the contained favorites (or meta-favorites) and
1264 // attempts to add them as shortcuts in the fallback group's location
1265 // until one is added successfully.
1266 added = false;
1267 final int groupDepth = parser.getDepth();
1268 while ((type = parser.next()) != XmlPullParser.END_TAG ||
1269 parser.getDepth() > groupDepth) {
1270 if (type != XmlPullParser.START_TAG) {
1271 continue;
1272 }
1273 final String fallback_item_name = parser.getName();
1274 final TypedArray ar = mContext.obtainStyledAttributes(attrs,
1275 R.styleable.Favorite);
1276 if (!added) {
1277 if (TAG_FAVORITE.equals(fallback_item_name)) {
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001278 final long id = addAppShortcut(db, values, parser);
Jason Monk41314972014-03-03 16:11:30 -05001279 added = id >= 0;
1280 } else {
1281 Log.e(TAG, "Fallback groups can contain only favorites "
1282 + ar.toString());
1283 }
1284 }
1285 ar.recycle();
1286 }
Winson Chung6d092682011-11-16 18:43:26 -08001287 } else if (TAG_FOLDER.equals(name)) {
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001288 // Folder contents are nested in this XML file
1289 added = loadFolder(db, values, mContext.getResources(), parser);
Winson Chung3d503fb2011-07-13 17:25:49 -07001290
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001291 } else if (TAG_PARTNER_FOLDER.equals(name)) {
1292 // Folder contents come from an external XML resource
1293 final Partner partner = Partner.get(mPackageManager);
1294 if (partner != null) {
1295 final Resources partnerRes = partner.getResources();
1296 final int resId = partnerRes.getIdentifier(Partner.RESOURCE_FOLDER,
1297 "xml", partner.getPackageName());
1298 if (resId != 0) {
1299 final XmlResourceParser partnerParser = partnerRes.getXml(resId);
1300 beginDocument(partnerParser, TAG_FOLDER);
1301 added = loadFolder(db, values, partnerRes, partnerParser);
Winson Chung6d092682011-11-16 18:43:26 -08001302 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001303 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001305 if (added) i++;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001306 a.recycle();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 }
1308 } catch (XmlPullParserException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001309 Log.w(TAG, "Got exception parsing favorites.", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001310 } catch (IOException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001311 Log.w(TAG, "Got exception parsing favorites.", e);
Winson Chung3d503fb2011-07-13 17:25:49 -07001312 } catch (RuntimeException e) {
1313 Log.w(TAG, "Got exception parsing favorites.", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001314 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001315
Winson Chungc763c4e2013-07-19 13:49:06 -07001316 // Update the max item id after we have loaded the database
1317 if (mMaxItemId == -1) {
1318 mMaxItemId = initializeMaxItemId(db);
1319 }
1320
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001321 return i;
1322 }
1323
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001324 /**
1325 * Parse folder starting at current {@link XmlPullParser} location.
1326 */
1327 private boolean loadFolder(SQLiteDatabase db, ContentValues values, Resources res,
1328 XmlResourceParser parser) throws IOException, XmlPullParserException {
1329 final String title;
1330 final int titleResId = getAttributeResourceValue(parser, ATTR_TITLE, 0);
1331 if (titleResId != 0) {
1332 title = res.getString(titleResId);
1333 } else {
1334 title = mContext.getResources().getString(R.string.folder_name);
1335 }
1336
1337 values.put(LauncherSettings.Favorites.TITLE, title);
1338 long folderId = addFolder(db, values);
1339 boolean added = folderId >= 0;
1340
1341 ArrayList<Long> folderItems = new ArrayList<Long>();
1342
1343 int type;
1344 int folderDepth = parser.getDepth();
1345 while ((type = parser.next()) != XmlPullParser.END_TAG ||
1346 parser.getDepth() > folderDepth) {
1347 if (type != XmlPullParser.START_TAG) {
1348 continue;
1349 }
1350 final String tag = parser.getName();
1351
1352 final ContentValues childValues = new ContentValues();
1353 childValues.put(LauncherSettings.Favorites.CONTAINER, folderId);
1354
1355 if (LOGD) {
1356 final String pkg = getAttributeValue(parser, ATTR_PACKAGE_NAME);
1357 final String uri = getAttributeValue(parser, ATTR_URI);
1358 Log.v(TAG, String.format(("%" + (2*(folderDepth+1)) + "s<%s \"%s\">"), "",
1359 tag, uri != null ? uri : pkg));
1360 }
1361
1362 if (TAG_FAVORITE.equals(tag) && folderId >= 0) {
1363 final long id = addAppShortcut(db, childValues, parser);
1364 if (id >= 0) {
1365 folderItems.add(id);
1366 }
1367 } else if (TAG_SHORTCUT.equals(tag) && folderId >= 0) {
1368 final long id = addUriShortcut(db, childValues, res, parser);
1369 if (id >= 0) {
1370 folderItems.add(id);
1371 }
1372 } else {
1373 throw new RuntimeException("Folders can contain only shortcuts");
1374 }
1375 }
1376
1377 // We can only have folders with >= 2 items, so we need to remove the
1378 // folder and clean up if less than 2 items were included, or some
1379 // failed to add, and less than 2 were actually added
1380 if (folderItems.size() < 2 && folderId >= 0) {
1381 // Delete the folder
1382 deleteId(db, folderId);
1383
1384 // If we have a single item, promote it to where the folder
1385 // would have been.
1386 if (folderItems.size() == 1) {
1387 final ContentValues childValues = new ContentValues();
1388 copyInteger(values, childValues, LauncherSettings.Favorites.CONTAINER);
1389 copyInteger(values, childValues, LauncherSettings.Favorites.SCREEN);
1390 copyInteger(values, childValues, LauncherSettings.Favorites.CELLX);
1391 copyInteger(values, childValues, LauncherSettings.Favorites.CELLY);
1392
1393 final long id = folderItems.get(0);
1394 db.update(TABLE_FAVORITES, childValues,
1395 LauncherSettings.Favorites._ID + "=" + id, null);
1396 } else {
1397 added = false;
1398 }
1399 }
1400 return added;
1401 }
1402
Jason Monk41314972014-03-03 16:11:30 -05001403 // A meta shortcut attempts to resolve an intent specified as a URI in the XML, if a
1404 // logical choice for what shortcut should be used for that intent exists, then it is
1405 // added. Otherwise add nothing.
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001406 private long addAppShortcutByUri(SQLiteDatabase db, ContentValues values,
1407 String intentUri) {
Jason Monk41314972014-03-03 16:11:30 -05001408 Intent metaIntent;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001409 try {
Jason Monk41314972014-03-03 16:11:30 -05001410 metaIntent = Intent.parseUri(intentUri, 0);
1411 } catch (URISyntaxException e) {
1412 Log.e(TAG, "Unable to add meta-favorite: " + intentUri, e);
1413 return -1;
1414 }
1415
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001416 ResolveInfo resolved = mPackageManager.resolveActivity(metaIntent,
Jason Monk41314972014-03-03 16:11:30 -05001417 PackageManager.MATCH_DEFAULT_ONLY);
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001418 final List<ResolveInfo> appList = mPackageManager.queryIntentActivities(
Jason Monk41314972014-03-03 16:11:30 -05001419 metaIntent, PackageManager.MATCH_DEFAULT_ONLY);
1420
1421 // Verify that the result is an app and not just the resolver dialog asking which
1422 // app to use.
1423 if (wouldLaunchResolverActivity(resolved, appList)) {
1424 // If only one of the results is a system app then choose that as the default.
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001425 final ResolveInfo systemApp = getSingleSystemActivity(appList);
Jason Monk41314972014-03-03 16:11:30 -05001426 if (systemApp == null) {
1427 // There is no logical choice for this meta-favorite, so rather than making
1428 // a bad choice just add nothing.
1429 Log.w(TAG, "No preference or single system activity found for "
1430 + metaIntent.toString());
Adam Cohen228da5a2011-07-27 22:23:47 -07001431 return -1;
1432 }
Jason Monk41314972014-03-03 16:11:30 -05001433 resolved = systemApp;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001434 }
Jason Monk41314972014-03-03 16:11:30 -05001435 final ActivityInfo info = resolved.activityInfo;
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001436 final Intent intent = buildMainIntent();
Jason Monk41314972014-03-03 16:11:30 -05001437 intent.setComponent(new ComponentName(info.packageName, info.name));
1438 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1439 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1440
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001441 return addAppShortcut(db, values, info.loadLabel(mPackageManager).toString(), intent);
Jason Monk41314972014-03-03 16:11:30 -05001442 }
1443
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001444 private ResolveInfo getSingleSystemActivity(List<ResolveInfo> appList) {
Jason Monk41314972014-03-03 16:11:30 -05001445 ResolveInfo systemResolve = null;
1446 final int N = appList.size();
1447 for (int i = 0; i < N; ++i) {
1448 try {
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001449 ApplicationInfo info = mPackageManager.getApplicationInfo(
Jason Monk41314972014-03-03 16:11:30 -05001450 appList.get(i).activityInfo.packageName, 0);
1451 if ((info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
1452 if (systemResolve != null) {
1453 return null;
1454 } else {
1455 systemResolve = appList.get(i);
1456 }
1457 }
1458 } catch (PackageManager.NameNotFoundException e) {
1459 Log.w(TAG, "Unable to get info about resolve results", e);
1460 return null;
1461 }
1462 }
1463 return systemResolve;
1464 }
1465
1466 private boolean wouldLaunchResolverActivity(ResolveInfo resolved,
1467 List<ResolveInfo> appList) {
1468 // If the list contains the above resolved activity, then it can't be
1469 // ResolverActivity itself.
1470 for (int i = 0; i < appList.size(); ++i) {
1471 ResolveInfo tmp = appList.get(i);
1472 if (tmp.activityInfo.name.equals(resolved.activityInfo.name)
1473 && tmp.activityInfo.packageName.equals(resolved.activityInfo.packageName)) {
1474 return false;
1475 }
1476 }
1477 return true;
1478 }
1479
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001480 private long addAppShortcut(SQLiteDatabase db, ContentValues values,
1481 XmlResourceParser parser) {
1482 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
1483 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
1484 final String uri = getAttributeValue(parser, ATTR_URI);
1485
1486 if (!TextUtils.isEmpty(packageName) && !TextUtils.isEmpty(className)) {
Jason Monk41314972014-03-03 16:11:30 -05001487 ActivityInfo info;
Jason Monk41314972014-03-03 16:11:30 -05001488 try {
1489 ComponentName cn;
1490 try {
1491 cn = new ComponentName(packageName, className);
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001492 info = mPackageManager.getActivityInfo(cn, 0);
Jason Monk41314972014-03-03 16:11:30 -05001493 } catch (PackageManager.NameNotFoundException nnfe) {
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001494 String[] packages = mPackageManager.currentToCanonicalPackageNames(
Jason Monk41314972014-03-03 16:11:30 -05001495 new String[] { packageName });
1496 cn = new ComponentName(packages[0], className);
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001497 info = mPackageManager.getActivityInfo(cn, 0);
Jason Monk41314972014-03-03 16:11:30 -05001498 }
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001499 final Intent intent = buildMainIntent();
Jason Monk41314972014-03-03 16:11:30 -05001500 intent.setComponent(cn);
1501 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1502 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1503
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001504 return addAppShortcut(db, values, info.loadLabel(mPackageManager).toString(),
Jason Monk41314972014-03-03 16:11:30 -05001505 intent);
1506 } catch (PackageManager.NameNotFoundException e) {
1507 Log.w(TAG, "Unable to add favorite: " + packageName +
1508 "/" + className, e);
1509 }
1510 return -1;
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001511 } else if (!TextUtils.isEmpty(uri)) {
Jason Monk41314972014-03-03 16:11:30 -05001512 // If no component specified try to find a shortcut to add from the URI.
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001513 return addAppShortcutByUri(db, values, uri);
Jason Monk41314972014-03-03 16:11:30 -05001514 } else {
1515 Log.e(TAG, "Skipping invalid <favorite> with no component or uri");
1516 return -1;
1517 }
1518 }
1519
1520 private long addAppShortcut(SQLiteDatabase db, ContentValues values, String title,
1521 Intent intent) {
1522 long id = generateNewItemId();
1523 values.put(Favorites.INTENT, intent.toUri(0));
1524 values.put(Favorites.TITLE, title);
1525 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPLICATION);
1526 values.put(Favorites.SPANX, 1);
1527 values.put(Favorites.SPANY, 1);
1528 values.put(Favorites._ID, id);
1529 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values) < 0) {
1530 return -1;
1531 } else {
1532 return id;
1533 }
Adam Cohen228da5a2011-07-27 22:23:47 -07001534 }
1535
1536 private long addFolder(SQLiteDatabase db, ContentValues values) {
1537 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_FOLDER);
1538 values.put(Favorites.SPANX, 1);
1539 values.put(Favorites.SPANY, 1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001540 long id = generateNewItemId();
Adam Cohen228da5a2011-07-27 22:23:47 -07001541 values.put(Favorites._ID, id);
1542 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values) <= 0) {
1543 return -1;
1544 } else {
1545 return id;
1546 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001547 }
1548
Bjorn Bringertcd8fec02010-01-14 13:26:43 +00001549 private ComponentName getSearchWidgetProvider() {
1550 SearchManager searchManager =
1551 (SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE);
1552 ComponentName searchComponent = searchManager.getGlobalSearchActivity();
1553 if (searchComponent == null) return null;
1554 return getProviderInPackage(searchComponent.getPackageName());
1555 }
1556
1557 /**
1558 * Gets an appwidget provider from the given package. If the package contains more than
1559 * one appwidget provider, an arbitrary one is returned.
1560 */
1561 private ComponentName getProviderInPackage(String packageName) {
1562 AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
1563 List<AppWidgetProviderInfo> providers = appWidgetManager.getInstalledProviders();
1564 if (providers == null) return null;
1565 final int providerCount = providers.size();
1566 for (int i = 0; i < providerCount; i++) {
1567 ComponentName provider = providers.get(i).provider;
1568 if (provider != null && provider.getPackageName().equals(packageName)) {
1569 return provider;
1570 }
1571 }
1572 return null;
1573 }
1574
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001575 private boolean addSearchWidget(SQLiteDatabase db, ContentValues values) {
Bjorn Bringertcd8fec02010-01-14 13:26:43 +00001576 ComponentName cn = getSearchWidgetProvider();
Winson Chungb3302ae2012-05-01 10:19:14 -07001577 return addAppWidget(db, values, cn, 4, 1, null);
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001578 }
1579
1580 private boolean addClockWidget(SQLiteDatabase db, ContentValues values) {
Bjorn Bringert34251342009-12-15 13:33:11 +00001581 ComponentName cn = new ComponentName("com.android.alarmclock",
1582 "com.android.alarmclock.AnalogAppWidgetProvider");
Winson Chungb3302ae2012-05-01 10:19:14 -07001583 return addAppWidget(db, values, cn, 2, 2, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 }
Adam Cohen228da5a2011-07-27 22:23:47 -07001585
Winson Chungb3302ae2012-05-01 10:19:14 -07001586 private boolean addAppWidget(XmlResourceParser parser, AttributeSet attrs, int type,
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001587 SQLiteDatabase db, ContentValues values, TypedArray a)
1588 throws XmlPullParserException, IOException {
Romain Guy693599f2010-03-23 10:58:18 -07001589
Mike Cleronb87bd162009-10-30 16:36:56 -07001590 String packageName = a.getString(R.styleable.Favorite_packageName);
1591 String className = a.getString(R.styleable.Favorite_className);
1592
1593 if (packageName == null || className == null) {
1594 return false;
1595 }
Romain Guy693599f2010-03-23 10:58:18 -07001596
1597 boolean hasPackage = true;
Mike Cleronb87bd162009-10-30 16:36:56 -07001598 ComponentName cn = new ComponentName(packageName, className);
Romain Guy693599f2010-03-23 10:58:18 -07001599 try {
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001600 mPackageManager.getReceiverInfo(cn, 0);
Romain Guy693599f2010-03-23 10:58:18 -07001601 } catch (Exception e) {
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001602 String[] packages = mPackageManager.currentToCanonicalPackageNames(
Romain Guy693599f2010-03-23 10:58:18 -07001603 new String[] { packageName });
1604 cn = new ComponentName(packages[0], className);
1605 try {
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001606 mPackageManager.getReceiverInfo(cn, 0);
Romain Guy693599f2010-03-23 10:58:18 -07001607 } catch (Exception e1) {
1608 hasPackage = false;
1609 }
1610 }
1611
1612 if (hasPackage) {
1613 int spanX = a.getInt(R.styleable.Favorite_spanX, 0);
1614 int spanY = a.getInt(R.styleable.Favorite_spanY, 0);
Winson Chungb3302ae2012-05-01 10:19:14 -07001615
1616 // Read the extras
1617 Bundle extras = new Bundle();
1618 int widgetDepth = parser.getDepth();
1619 while ((type = parser.next()) != XmlPullParser.END_TAG ||
1620 parser.getDepth() > widgetDepth) {
1621 if (type != XmlPullParser.START_TAG) {
1622 continue;
1623 }
1624
1625 TypedArray ar = mContext.obtainStyledAttributes(attrs, R.styleable.Extra);
1626 if (TAG_EXTRA.equals(parser.getName())) {
1627 String key = ar.getString(R.styleable.Extra_key);
1628 String value = ar.getString(R.styleable.Extra_value);
1629 if (key != null && value != null) {
1630 extras.putString(key, value);
1631 } else {
1632 throw new RuntimeException("Widget extras must have a key and value");
1633 }
1634 } else {
1635 throw new RuntimeException("Widgets can contain only extras");
1636 }
1637 ar.recycle();
1638 }
1639
1640 return addAppWidget(db, values, cn, spanX, spanY, extras);
Romain Guy693599f2010-03-23 10:58:18 -07001641 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001642
Romain Guy693599f2010-03-23 10:58:18 -07001643 return false;
Bjorn Bringert7984c942009-12-09 15:38:25 +00001644 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001645 private boolean addAppWidget(SQLiteDatabase db, ContentValues values, ComponentName cn,
Winson Chungb3302ae2012-05-01 10:19:14 -07001646 int spanX, int spanY, Bundle extras) {
Mike Cleronb87bd162009-10-30 16:36:56 -07001647 boolean allocatedAppWidgets = false;
1648 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
1649
1650 try {
1651 int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001652
Mike Cleronb87bd162009-10-30 16:36:56 -07001653 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001654 values.put(Favorites.SPANX, spanX);
1655 values.put(Favorites.SPANY, spanY);
Mike Cleronb87bd162009-10-30 16:36:56 -07001656 values.put(Favorites.APPWIDGET_ID, appWidgetId);
Chris Wrend5e66bf2013-09-16 14:02:29 -04001657 values.put(Favorites.APPWIDGET_PROVIDER, cn.flattenToString());
Adam Cohendcd297f2013-06-18 13:13:40 -07001658 values.put(Favorites._ID, generateNewItemId());
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001659 dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values);
Mike Cleronb87bd162009-10-30 16:36:56 -07001660
1661 allocatedAppWidgets = true;
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001662
Michael Jurka8b805b12012-04-18 14:23:14 -07001663 // TODO: need to check return value
1664 appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, cn);
Winson Chungb3302ae2012-05-01 10:19:14 -07001665
1666 // Send a broadcast to configure the widget
1667 if (extras != null && !extras.isEmpty()) {
1668 Intent intent = new Intent(ACTION_APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE);
1669 intent.setComponent(cn);
1670 intent.putExtras(extras);
1671 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1672 mContext.sendBroadcast(intent);
1673 }
Mike Cleronb87bd162009-10-30 16:36:56 -07001674 } catch (RuntimeException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001675 Log.e(TAG, "Problem allocating appWidgetId", ex);
Mike Cleronb87bd162009-10-30 16:36:56 -07001676 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001677
Mike Cleronb87bd162009-10-30 16:36:56 -07001678 return allocatedAppWidgets;
1679 }
Adam Cohen228da5a2011-07-27 22:23:47 -07001680
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001681 private long addUriShortcut(SQLiteDatabase db, ContentValues values, Resources res,
1682 XmlResourceParser parser) {
1683 final int iconResId = getAttributeResourceValue(parser, ATTR_ICON, 0);
1684 final int titleResId = getAttributeResourceValue(parser, ATTR_TITLE, 0);
Mike Cleronb87bd162009-10-30 16:36:56 -07001685
Romain Guy7eb9e5e2009-12-02 20:10:07 -08001686 Intent intent;
Mike Cleronb87bd162009-10-30 16:36:56 -07001687 String uri = null;
1688 try {
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001689 uri = getAttributeValue(parser, ATTR_URI);
Mike Cleronb87bd162009-10-30 16:36:56 -07001690 intent = Intent.parseUri(uri, 0);
1691 } catch (URISyntaxException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001692 Log.w(TAG, "Shortcut has malformed uri: " + uri);
Adam Cohen228da5a2011-07-27 22:23:47 -07001693 return -1; // Oh well
Mike Cleronb87bd162009-10-30 16:36:56 -07001694 }
1695
1696 if (iconResId == 0 || titleResId == 0) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001697 Log.w(TAG, "Shortcut is missing title or icon resource ID");
Adam Cohen228da5a2011-07-27 22:23:47 -07001698 return -1;
Mike Cleronb87bd162009-10-30 16:36:56 -07001699 }
1700
Adam Cohendcd297f2013-06-18 13:13:40 -07001701 long id = generateNewItemId();
Mike Cleronb87bd162009-10-30 16:36:56 -07001702 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1703 values.put(Favorites.INTENT, intent.toUri(0));
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001704 values.put(Favorites.TITLE, res.getString(titleResId));
Mike Cleronb87bd162009-10-30 16:36:56 -07001705 values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_SHORTCUT);
1706 values.put(Favorites.SPANX, 1);
1707 values.put(Favorites.SPANY, 1);
1708 values.put(Favorites.ICON_TYPE, Favorites.ICON_TYPE_RESOURCE);
Jeff Sharkey5aeef582014-04-14 13:26:42 -07001709 values.put(Favorites.ICON_PACKAGE, res.getResourcePackageName(iconResId));
1710 values.put(Favorites.ICON_RESOURCE, res.getResourceName(iconResId));
Adam Cohen228da5a2011-07-27 22:23:47 -07001711 values.put(Favorites._ID, id);
Mike Cleronb87bd162009-10-30 16:36:56 -07001712
Adam Cohen228da5a2011-07-27 22:23:47 -07001713 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values) < 0) {
1714 return -1;
1715 }
1716 return id;
Mike Cleronb87bd162009-10-30 16:36:56 -07001717 }
Dan Sandlerd5024042014-01-09 15:01:33 -05001718
1719 public void migrateLauncher2Shortcuts(SQLiteDatabase db, Uri uri) {
1720 final ContentResolver resolver = mContext.getContentResolver();
1721 Cursor c = null;
1722 int count = 0;
1723 int curScreen = 0;
1724
1725 try {
1726 c = resolver.query(uri, null, null, null, "title ASC");
1727 } catch (Exception e) {
1728 // Ignore
1729 }
1730
Dan Sandlerd5024042014-01-09 15:01:33 -05001731 // We already have a favorites database in the old provider
1732 if (c != null) {
1733 try {
1734 if (c.getCount() > 0) {
1735 final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
1736 final int intentIndex
1737 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
1738 final int titleIndex
1739 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
1740 final int iconTypeIndex
1741 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
1742 final int iconIndex
1743 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
1744 final int iconPackageIndex
1745 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
1746 final int iconResourceIndex
1747 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
1748 final int containerIndex
1749 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
1750 final int itemTypeIndex
1751 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
1752 final int screenIndex
1753 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
1754 final int cellXIndex
1755 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
1756 final int cellYIndex
1757 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
1758 final int uriIndex
1759 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
1760 final int displayModeIndex
1761 = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
1762
1763 int i = 0;
1764 int curX = 0;
1765 int curY = 0;
1766
1767 final LauncherAppState app = LauncherAppState.getInstance();
1768 final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
1769 final int width = (int) grid.numColumns;
1770 final int height = (int) grid.numRows;
1771 final int hotseatWidth = (int) grid.numHotseatIcons;
Adam Cohen556f6132014-01-15 15:18:08 -08001772 PackageManager pm = mContext.getPackageManager();
Dan Sandlerd5024042014-01-09 15:01:33 -05001773
1774 final HashSet<String> seenIntents = new HashSet<String>(c.getCount());
1775
Adam Cohen72960972014-01-15 18:13:55 -08001776 final ArrayList<ContentValues> shortcuts = new ArrayList<ContentValues>();
1777 final ArrayList<ContentValues> folders = new ArrayList<ContentValues>();
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001778 final SparseArray<ContentValues> hotseat = new SparseArray<ContentValues>();
Dan Sandlerd5024042014-01-09 15:01:33 -05001779
1780 while (c.moveToNext()) {
1781 final int itemType = c.getInt(itemTypeIndex);
1782 if (itemType != Favorites.ITEM_TYPE_APPLICATION
1783 && itemType != Favorites.ITEM_TYPE_SHORTCUT
1784 && itemType != Favorites.ITEM_TYPE_FOLDER) {
1785 continue;
1786 }
1787
1788 final int cellX = c.getInt(cellXIndex);
1789 final int cellY = c.getInt(cellYIndex);
1790 final int screen = c.getInt(screenIndex);
1791 int container = c.getInt(containerIndex);
1792 final String intentStr = c.getString(intentIndex);
1793 Launcher.addDumpLog(TAG, "migrating \""
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001794 + c.getString(titleIndex) + "\" ("
1795 + cellX + "," + cellY + "@"
1796 + LauncherSettings.Favorites.containerToString(container)
1797 + "/" + screen
1798 + "): " + intentStr, true);
Dan Sandlerd5024042014-01-09 15:01:33 -05001799
1800 if (itemType != Favorites.ITEM_TYPE_FOLDER) {
Adam Cohen556f6132014-01-15 15:18:08 -08001801
1802 final Intent intent;
1803 final ComponentName cn;
1804 try {
1805 intent = Intent.parseUri(intentStr, 0);
1806 } catch (URISyntaxException e) {
1807 // bogus intent?
1808 Launcher.addDumpLog(TAG,
1809 "skipping invalid intent uri", true);
1810 continue;
1811 }
1812
1813 cn = intent.getComponent();
Dan Sandlerd5024042014-01-09 15:01:33 -05001814 if (TextUtils.isEmpty(intentStr)) {
1815 // no intent? no icon
1816 Launcher.addDumpLog(TAG, "skipping empty intent", true);
1817 continue;
Adam Cohen72960972014-01-15 18:13:55 -08001818 } else if (cn != null &&
1819 !LauncherModel.isValidPackageComponent(pm, cn)) {
Adam Cohen556f6132014-01-15 15:18:08 -08001820 // component no longer exists.
Adam Cohen72960972014-01-15 18:13:55 -08001821 Launcher.addDumpLog(TAG, "skipping item whose component " +
Adam Cohen556f6132014-01-15 15:18:08 -08001822 "no longer exists.", true);
1823 continue;
Adam Cohen72960972014-01-15 18:13:55 -08001824 } else if (container ==
1825 LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1826 // Dedupe icons directly on the workspace
1827
Adam Cohen556f6132014-01-15 15:18:08 -08001828 // Canonicalize
1829 // the Play Store sets the package parameter, but Launcher
1830 // does not, so we clear that out to keep them the same
1831 intent.setPackage(null);
1832 final String key = intent.toUri(0);
1833 if (seenIntents.contains(key)) {
1834 Launcher.addDumpLog(TAG, "skipping duplicate", true);
Dan Sandlerd5024042014-01-09 15:01:33 -05001835 continue;
Adam Cohen556f6132014-01-15 15:18:08 -08001836 } else {
1837 seenIntents.add(key);
Dan Sandlerd5024042014-01-09 15:01:33 -05001838 }
1839 }
1840 }
1841
1842 ContentValues values = new ContentValues(c.getColumnCount());
1843 values.put(LauncherSettings.Favorites._ID, c.getInt(idIndex));
1844 values.put(LauncherSettings.Favorites.INTENT, intentStr);
1845 values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex));
1846 values.put(LauncherSettings.Favorites.ICON_TYPE,
1847 c.getInt(iconTypeIndex));
1848 values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex));
1849 values.put(LauncherSettings.Favorites.ICON_PACKAGE,
1850 c.getString(iconPackageIndex));
1851 values.put(LauncherSettings.Favorites.ICON_RESOURCE,
1852 c.getString(iconResourceIndex));
1853 values.put(LauncherSettings.Favorites.ITEM_TYPE, itemType);
1854 values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1);
1855 values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex));
1856 values.put(LauncherSettings.Favorites.DISPLAY_MODE,
1857 c.getInt(displayModeIndex));
1858
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001859 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1860 hotseat.put(screen, values);
Dan Sandlerd5024042014-01-09 15:01:33 -05001861 }
1862
1863 if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1864 // In a folder or in the hotseat, preserve position
1865 values.put(LauncherSettings.Favorites.SCREEN, screen);
1866 values.put(LauncherSettings.Favorites.CELLX, cellX);
1867 values.put(LauncherSettings.Favorites.CELLY, cellY);
1868 } else {
Adam Cohen72960972014-01-15 18:13:55 -08001869 // For items contained directly on one of the workspace screen,
1870 // we'll determine their location (screen, x, y) in a second pass.
Dan Sandlerd5024042014-01-09 15:01:33 -05001871 }
1872
1873 values.put(LauncherSettings.Favorites.CONTAINER, container);
1874
Adam Cohen72960972014-01-15 18:13:55 -08001875 if (itemType != Favorites.ITEM_TYPE_FOLDER) {
1876 shortcuts.add(values);
1877 } else {
1878 folders.add(values);
1879 }
Dan Sandlerd5024042014-01-09 15:01:33 -05001880 }
1881
Dan Sandlerab5fa3a2014-03-06 23:48:04 -05001882 // Now that we have all the hotseat icons, let's go through them left-right
1883 // and assign valid locations for them in the new hotseat
1884 final int N = hotseat.size();
1885 for (int idx=0; idx<N; idx++) {
1886 int hotseatX = hotseat.keyAt(idx);
1887 ContentValues values = hotseat.valueAt(idx);
1888
1889 if (hotseatX == grid.hotseatAllAppsRank) {
1890 // let's drop this in the next available hole in the hotseat
1891 while (++hotseatX < hotseatWidth) {
1892 if (hotseat.get(hotseatX) == null) {
1893 // found a spot! move it here
1894 values.put(LauncherSettings.Favorites.SCREEN,
1895 hotseatX);
1896 break;
1897 }
1898 }
1899 }
1900 if (hotseatX >= hotseatWidth) {
1901 // no room for you in the hotseat? it's off to the desktop with you
1902 values.put(LauncherSettings.Favorites.CONTAINER,
1903 Favorites.CONTAINER_DESKTOP);
1904 }
1905 }
1906
Adam Cohen72960972014-01-15 18:13:55 -08001907 final ArrayList<ContentValues> allItems = new ArrayList<ContentValues>();
1908 // Folders first
1909 allItems.addAll(folders);
1910 // Then shortcuts
1911 allItems.addAll(shortcuts);
1912
1913 // Layout all the folders
1914 for (ContentValues values: allItems) {
1915 if (values.getAsInteger(LauncherSettings.Favorites.CONTAINER) !=
1916 LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1917 // Hotseat items and folder items have already had their
1918 // location information set. Nothing to be done here.
1919 continue;
1920 }
1921 values.put(LauncherSettings.Favorites.SCREEN, curScreen);
1922 values.put(LauncherSettings.Favorites.CELLX, curX);
1923 values.put(LauncherSettings.Favorites.CELLY, curY);
1924 curX = (curX + 1) % width;
1925 if (curX == 0) {
1926 curY = (curY + 1);
1927 }
1928 // Leave the last row of icons blank on every screen
1929 if (curY == height - 1) {
1930 curScreen = (int) generateNewScreenId();
1931 curY = 0;
1932 }
1933 }
1934
1935 if (allItems.size() > 0) {
Dan Sandlerd5024042014-01-09 15:01:33 -05001936 db.beginTransaction();
1937 try {
Adam Cohen72960972014-01-15 18:13:55 -08001938 for (ContentValues row: allItems) {
1939 if (row == null) continue;
1940 if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, row)
Dan Sandlerd5024042014-01-09 15:01:33 -05001941 < 0) {
1942 return;
1943 } else {
1944 count++;
1945 }
1946 }
1947 db.setTransactionSuccessful();
1948 } finally {
1949 db.endTransaction();
1950 }
1951 }
1952
1953 db.beginTransaction();
1954 try {
1955 for (i=0; i<=curScreen; i++) {
1956 final ContentValues values = new ContentValues();
1957 values.put(LauncherSettings.WorkspaceScreens._ID, i);
1958 values.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
1959 if (dbInsertAndCheck(this, db, TABLE_WORKSPACE_SCREENS, null, values)
1960 < 0) {
1961 return;
1962 }
1963 }
1964 db.setTransactionSuccessful();
1965 } finally {
1966 db.endTransaction();
1967 }
1968 }
1969 } finally {
1970 c.close();
1971 }
1972 }
1973
1974 Launcher.addDumpLog(TAG, "migrated " + count + " icons from Launcher2 into "
1975 + (curScreen+1) + " screens", true);
1976
1977 // ensure that new screens are created to hold these icons
1978 setFlagJustLoadedOldDb();
1979
1980 // Update max IDs; very important since we just grabbed IDs from another database
1981 mMaxItemId = initializeMaxItemId(db);
1982 mMaxScreenId = initializeMaxScreenId(db);
1983 if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId + " mMaxScreenId: " + mMaxScreenId);
1984 }
Mike Cleronb87bd162009-10-30 16:36:56 -07001985 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001986
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 /**
1988 * Build a query string that will match any row where the column matches
1989 * anything in the values list.
1990 */
1991 static String buildOrWhereString(String column, int[] values) {
1992 StringBuilder selectWhere = new StringBuilder();
1993 for (int i = values.length - 1; i >= 0; i--) {
1994 selectWhere.append(column).append("=").append(values[i]);
1995 if (i > 0) {
1996 selectWhere.append(" OR ");
1997 }
1998 }
1999 return selectWhere.toString();
2000 }
2001
Jeff Sharkey5aeef582014-04-14 13:26:42 -07002002 /**
2003 * Return attribute value, attempting launcher-specific namespace first
2004 * before falling back to anonymous attribute.
2005 */
2006 static String getAttributeValue(XmlResourceParser parser, String attribute) {
2007 String value = parser.getAttributeValue(
2008 "http://schemas.android.com/apk/res-auto/com.android.launcher3", attribute);
2009 if (value == null) {
2010 value = parser.getAttributeValue(null, attribute);
2011 }
2012 return value;
2013 }
2014
2015 /**
2016 * Return attribute resource value, attempting launcher-specific namespace
2017 * first before falling back to anonymous attribute.
2018 */
2019 static int getAttributeResourceValue(XmlResourceParser parser, String attribute,
2020 int defaultValue) {
2021 int value = parser.getAttributeResourceValue(
2022 "http://schemas.android.com/apk/res-auto/com.android.launcher3", attribute,
2023 defaultValue);
2024 if (value == defaultValue) {
2025 value = parser.getAttributeResourceValue(null, attribute, defaultValue);
2026 }
2027 return value;
2028 }
2029
2030 private static void copyInteger(ContentValues from, ContentValues to, String key) {
2031 to.put(key, from.getAsInteger(key));
2032 }
2033
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 static class SqlArguments {
2035 public final String table;
2036 public final String where;
2037 public final String[] args;
2038
2039 SqlArguments(Uri url, String where, String[] args) {
2040 if (url.getPathSegments().size() == 1) {
2041 this.table = url.getPathSegments().get(0);
2042 this.where = where;
2043 this.args = args;
2044 } else if (url.getPathSegments().size() != 2) {
2045 throw new IllegalArgumentException("Invalid URI: " + url);
2046 } else if (!TextUtils.isEmpty(where)) {
2047 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
2048 } else {
2049 this.table = url.getPathSegments().get(0);
Daniel Lehmannc3a80402012-04-23 21:35:11 -07002050 this.where = "_id=" + ContentUris.parseId(url);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051 this.args = null;
2052 }
2053 }
2054
2055 SqlArguments(Uri url) {
2056 if (url.getPathSegments().size() == 1) {
2057 table = url.getPathSegments().get(0);
2058 where = null;
2059 args = null;
2060 } else {
2061 throw new IllegalArgumentException("Invalid URI: " + url);
2062 }
2063 }
2064 }
Adam Cohen72960972014-01-15 18:13:55 -08002065}