blob: 03ca242266ecd6a2d84a8f0bb6a6bcc29ec16199 [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
The Android Open Source Project7376fae2009-03-11 12:11:58 -070019import android.appwidget.AppWidgetHost;
Mike Cleronb87bd162009-10-30 16:36:56 -070020import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.content.ComponentName;
Adam Cohen228da5a2011-07-27 22:23:47 -070022import android.content.ContentProvider;
Yura085c8532014-02-11 15:15:29 +000023import android.content.ContentProviderOperation;
24import android.content.ContentProviderResult;
Adam Cohen228da5a2011-07-27 22:23:47 -070025import android.content.ContentUris;
26import android.content.ContentValues;
27import android.content.Context;
28import android.content.Intent;
Yura085c8532014-02-11 15:15:29 +000029import android.content.OperationApplicationException;
Michael Jurkab85f8a42012-04-25 15:48:32 -070030import android.content.SharedPreferences;
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070031import android.content.pm.PackageManager.NameNotFoundException;
Adam Cohen228da5a2011-07-27 22:23:47 -070032import android.content.res.Resources;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.database.Cursor;
34import android.database.SQLException;
Adam Cohen228da5a2011-07-27 22:23:47 -070035import android.database.sqlite.SQLiteDatabase;
36import android.database.sqlite.SQLiteOpenHelper;
37import android.database.sqlite.SQLiteQueryBuilder;
Sunny Goyal0b037782015-04-02 10:27:03 -070038import android.database.sqlite.SQLiteStatement;
Adam Cohen228da5a2011-07-27 22:23:47 -070039import android.net.Uri;
Sunny Goyal7779d622015-06-11 16:18:39 -070040import android.os.Binder;
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070041import android.os.Bundle;
Sunny Goyalb5b55c82016-05-10 12:28:59 -070042import android.os.Handler;
43import android.os.Message;
Sunny Goyal7779d622015-06-11 16:18:39 -070044import android.os.Process;
Sunny Goyale26d1002016-06-20 14:52:14 -070045import android.os.Trace;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080046import android.os.UserHandle;
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070047import android.os.UserManager;
Adam Cohen228da5a2011-07-27 22:23:47 -070048import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.util.Log;
Adam Cohen228da5a2011-07-27 22:23:47 -070050
Sunny Goyal0fe505b2014-08-06 09:55:36 -070051import com.android.launcher3.AutoInstallsLayout.LayoutParserCallback;
52import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyalb5b55c82016-05-10 12:28:59 -070053import com.android.launcher3.LauncherSettings.WorkspaceScreens;
Kenny Guyed131872014-04-30 03:02:21 +010054import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070055import com.android.launcher3.config.FeatureFlags;
Tony Wickham827cef22016-03-17 15:39:39 -070056import com.android.launcher3.dynamicui.ExtractionUtils;
Sunny Goyalca187462017-03-31 20:09:34 -070057import com.android.launcher3.graphics.IconShapeOverride;
Sunny Goyalfdbef272017-02-01 12:52:54 -080058import com.android.launcher3.logging.FileLog;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070059import com.android.launcher3.provider.LauncherDbUtils;
Sunny Goyale8f7d5a2016-05-24 11:30:14 -070060import com.android.launcher3.provider.RestoreDbTask;
Sunny Goyale2fba6c2015-05-12 10:39:59 -070061import com.android.launcher3.util.ManagedProfileHeuristic;
Sunny Goyalbf67f3b2016-03-15 15:30:11 -070062import com.android.launcher3.util.NoLocaleSqliteContext;
Sunny Goyalb5b55c82016-05-10 12:28:59 -070063import com.android.launcher3.util.Preconditions;
Adam Cohen091440a2015-03-18 14:16:05 -070064import com.android.launcher3.util.Thunk;
Michael Jurka8b805b12012-04-18 14:23:14 -070065
Hyunyoung Song6aa37292017-02-06 10:46:24 -080066import java.io.FileDescriptor;
67import java.io.PrintWriter;
Sunny Goyal55fddc82017-04-06 15:02:52 -070068import java.lang.reflect.Method;
Mike Cleronb87bd162009-10-30 16:36:56 -070069import java.net.URISyntaxException;
Adam Cohen228da5a2011-07-27 22:23:47 -070070import java.util.ArrayList;
Adam Cohen71483f42014-05-15 14:04:01 -070071import java.util.Collections;
Sunny Goyal55fddc82017-04-06 15:02:52 -070072import java.util.HashSet;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074public class LauncherProvider extends ContentProvider {
Sunny Goyalc74e4192015-09-08 14:01:03 -070075 private static final String TAG = "LauncherProvider";
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080076 private static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077
Sunny Goyal3c7d55b2017-04-13 12:01:47 -070078 /**
79 * Represents the schema of the database. Changes in scheme need not be backwards compatible.
80 */
81 private static final int SCHEMA_VERSION = 27;
82 /**
83 * Represents the actual data. It could include additional validations and normalizations added
84 * overtime. These must be backwards compatible, else we risk breaking old devices during
85 * restore or binary version downgrade.
86 */
Sunny Goyal55fddc82017-04-06 15:02:52 -070087 private static final int DATA_VERSION = 3;
Sunny Goyal3c7d55b2017-04-13 12:01:47 -070088
89 private static final String PREF_KEY_DATA_VERISON = "provider_data_version";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090
Sunny Goyal3d706ad2017-03-06 16:56:39 -080091 public static final String AUTHORITY = (BuildConfig.APPLICATION_ID + ".settings").intern();
Winson Chung3d503fb2011-07-13 17:25:49 -070092
Sunny Goyale87e6ab2014-11-21 22:42:53 -080093 static final String EMPTY_DATABASE_CREATED = "EMPTY_DATABASE_CREATED";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070095 private static final String RESTRICTION_PACKAGE_NAME = "workspace.configuration.package.name";
96
Sunny Goyalb5b55c82016-05-10 12:28:59 -070097 private final ChangeListenerWrapper mListenerWrapper = new ChangeListenerWrapper();
98 private Handler mListenerHandler;
99
Sunny Goyalf076eae2016-01-11 12:25:10 -0800100 protected DatabaseHelper mOpenHelper;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101
Hyunyoung Song6aa37292017-02-06 10:46:24 -0800102 /**
103 * $ adb shell dumpsys activity provider com.android.launcher3
104 */
105 @Override
106 public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
107 LauncherAppState appState = LauncherAppState.getInstanceNoCreate();
108 if (appState == null || !appState.getModel().isModelLoaded()) {
109 return;
110 }
111 appState.getModel().dumpState("", fd, writer, args);
112 }
113
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114 @Override
115 public boolean onCreate() {
Sunny Goyal3d706ad2017-03-06 16:56:39 -0800116 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyale26d1002016-06-20 14:52:14 -0700117 Log.d(TAG, "Launcher process started");
118 }
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700119 mListenerHandler = new Handler(mListenerWrapper);
120
Sunny Goyalfdbef272017-02-01 12:52:54 -0800121 // The content provider exists for the entire duration of the launcher main process and
122 // is the first component to get created. Initializing FileLog here ensures that it's
123 // always available in the main process.
124 FileLog.setDir(getContext().getApplicationContext().getFilesDir());
Sunny Goyalca187462017-03-31 20:09:34 -0700125 IconShapeOverride.apply(getContext());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 return true;
127 }
128
Sunny Goyald3849d12015-10-29 10:28:32 -0700129 /**
130 * Sets a provider listener.
131 */
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700132 public void setLauncherProviderChangeListener(LauncherProviderChangeListener listener) {
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700133 Preconditions.assertUIThread();
134 mListenerWrapper.mListener = listener;
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700135 }
136
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 @Override
138 public String getType(Uri uri) {
139 SqlArguments args = new SqlArguments(uri, null, null);
140 if (TextUtils.isEmpty(args.where)) {
141 return "vnd.android.cursor.dir/" + args.table;
142 } else {
143 return "vnd.android.cursor.item/" + args.table;
144 }
145 }
146
Sunny Goyalf076eae2016-01-11 12:25:10 -0800147 /**
148 * Overridden in tests
149 */
150 protected synchronized void createDbIfNotExists() {
Sunny Goyald3849d12015-10-29 10:28:32 -0700151 if (mOpenHelper == null) {
Sunny Goyale26d1002016-06-20 14:52:14 -0700152 if (LauncherAppState.PROFILE_STARTUP) {
153 Trace.beginSection("Opening workspace DB");
154 }
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700155 mOpenHelper = new DatabaseHelper(getContext(), mListenerHandler);
Sunny Goyale8f7d5a2016-05-24 11:30:14 -0700156
157 if (RestoreDbTask.isPending(getContext())) {
158 if (!RestoreDbTask.performRestore(mOpenHelper)) {
159 mOpenHelper.createEmptyDB(mOpenHelper.getWritableDatabase());
160 }
161 // Set is pending to false irrespective of the result, so that it doesn't get
162 // executed again.
163 RestoreDbTask.setPending(getContext(), false);
164 }
Sunny Goyale26d1002016-06-20 14:52:14 -0700165
166 if (LauncherAppState.PROFILE_STARTUP) {
167 Trace.endSection();
168 }
Sunny Goyald3849d12015-10-29 10:28:32 -0700169 }
170 }
171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 @Override
173 public Cursor query(Uri uri, String[] projection, String selection,
174 String[] selectionArgs, String sortOrder) {
Sunny Goyald3849d12015-10-29 10:28:32 -0700175 createDbIfNotExists();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
177 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
178 SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
179 qb.setTables(args.table);
180
Romain Guy73b979d2009-06-09 12:57:21 -0700181 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 Cursor result = qb.query(db, projection, args.where, args.args, null, null, sortOrder);
183 result.setNotificationUri(getContext().getContentResolver(), uri);
184
185 return result;
186 }
187
Adam Cohen091440a2015-03-18 14:16:05 -0700188 @Thunk static long dbInsertAndCheck(DatabaseHelper helper,
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700189 SQLiteDatabase db, String table, String nullColumnHack, ContentValues values) {
Dan Sandlerd5024042014-01-09 15:01:33 -0500190 if (values == null) {
191 throw new RuntimeException("Error: attempting to insert null values");
192 }
Adam Cohen71483f42014-05-15 14:04:01 -0700193 if (!values.containsKey(LauncherSettings.ChangeLogColumns._ID)) {
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700194 throw new RuntimeException("Error: attempting to add item without specifying an id");
195 }
Chris Wren5dee7af2013-12-20 17:22:11 -0500196 helper.checkId(table, values);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700197 return db.insert(table, nullColumnHack, values);
198 }
199
Sunny Goyald1064182015-08-13 12:08:30 -0700200 private void reloadLauncherIfExternal() {
Sunny Goyalc74e4192015-09-08 14:01:03 -0700201 if (Utilities.ATLEAST_MARSHMALLOW && Binder.getCallingPid() != Process.myPid()) {
Sunny Goyald1064182015-08-13 12:08:30 -0700202 LauncherAppState app = LauncherAppState.getInstanceNoCreate();
203 if (app != null) {
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800204 app.getModel().forceReload();
Sunny Goyald1064182015-08-13 12:08:30 -0700205 }
206 }
207 }
208
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209 @Override
210 public Uri insert(Uri uri, ContentValues initialValues) {
Sunny Goyald3849d12015-10-29 10:28:32 -0700211 createDbIfNotExists();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212 SqlArguments args = new SqlArguments(uri);
213
Sunny Goyald1064182015-08-13 12:08:30 -0700214 // In very limited cases, we support system|signature permission apps to modify the db.
215 if (Binder.getCallingPid() != Process.myPid()) {
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700216 if (!initializeExternalAdd(initialValues)) {
Adam Cohena043fa82014-07-23 14:49:38 -0700217 return null;
218 }
219 }
220
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
Chris Wren1ada10d2013-09-13 18:01:38 -0400222 addModifiedTime(initialValues);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700223 final long rowId = dbInsertAndCheck(mOpenHelper, db, args.table, null, initialValues);
Sunny Goyale72f3d52015-03-02 14:24:21 -0800224 if (rowId < 0) return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225
226 uri = ContentUris.withAppendedId(uri, rowId);
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700227 notifyListeners();
228
Sunny Goyalc74e4192015-09-08 14:01:03 -0700229 if (Utilities.ATLEAST_MARSHMALLOW) {
230 reloadLauncherIfExternal();
231 } else {
232 // Deprecated behavior to support legacy devices which rely on provider callbacks.
233 LauncherAppState app = LauncherAppState.getInstanceNoCreate();
234 if (app != null && "true".equals(uri.getQueryParameter("isExternalAdd"))) {
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800235 app.getModel().forceReload();
Sunny Goyalc74e4192015-09-08 14:01:03 -0700236 }
237
238 String notify = uri.getQueryParameter("notify");
239 if (notify == null || "true".equals(notify)) {
240 getContext().getContentResolver().notifyChange(uri, null);
241 }
242 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243 return uri;
244 }
245
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700246 private boolean initializeExternalAdd(ContentValues values) {
247 // 1. Ensure that externally added items have a valid item id
248 long id = mOpenHelper.generateNewItemId();
249 values.put(LauncherSettings.Favorites._ID, id);
250
251 // 2. In the case of an app widget, and if no app widget id is specified, we
252 // attempt allocate and bind the widget.
253 Integer itemType = values.getAsInteger(LauncherSettings.Favorites.ITEM_TYPE);
254 if (itemType != null &&
255 itemType.intValue() == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET &&
256 !values.containsKey(LauncherSettings.Favorites.APPWIDGET_ID)) {
257
258 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getContext());
259 ComponentName cn = ComponentName.unflattenFromString(
260 values.getAsString(Favorites.APPWIDGET_PROVIDER));
261
262 if (cn != null) {
263 try {
Sunny Goyal55fddc82017-04-06 15:02:52 -0700264 AppWidgetHost widgetHost = mOpenHelper.newLauncherWidgetHost();
265 int appWidgetId = widgetHost.allocateAppWidgetId();
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700266 values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId);
267 if (!appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,cn)) {
Sunny Goyal55fddc82017-04-06 15:02:52 -0700268 widgetHost.deleteAppWidgetId(appWidgetId);
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700269 return false;
270 }
271 } catch (RuntimeException e) {
272 Log.e(TAG, "Failed to initialize external widget", e);
273 return false;
274 }
275 } else {
276 return false;
277 }
278 }
279
280 // Add screen id if not present
281 long screenId = values.getAsLong(LauncherSettings.Favorites.SCREEN);
282 SQLiteStatement stmp = null;
283 try {
284 stmp = mOpenHelper.getWritableDatabase().compileStatement(
285 "INSERT OR IGNORE INTO workspaceScreens (_id, screenRank) " +
286 "select ?, (ifnull(MAX(screenRank), -1)+1) from workspaceScreens");
287 stmp.bindLong(1, screenId);
288
289 ContentValues valuesInserted = new ContentValues();
290 valuesInserted.put(LauncherSettings.BaseLauncherColumns._ID, stmp.executeInsert());
291 mOpenHelper.checkId(WorkspaceScreens.TABLE_NAME, valuesInserted);
292 return true;
293 } catch (Exception e) {
294 return false;
295 } finally {
296 Utilities.closeSilently(stmp);
297 }
298 }
299
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800300 @Override
301 public int bulkInsert(Uri uri, ContentValues[] values) {
Sunny Goyald3849d12015-10-29 10:28:32 -0700302 createDbIfNotExists();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800303 SqlArguments args = new SqlArguments(uri);
304
305 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
306 db.beginTransaction();
307 try {
308 int numValues = values.length;
309 for (int i = 0; i < numValues; i++) {
Chris Wren1ada10d2013-09-13 18:01:38 -0400310 addModifiedTime(values[i]);
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700311 if (dbInsertAndCheck(mOpenHelper, db, args.table, null, values[i]) < 0) {
312 return 0;
313 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800314 }
315 db.setTransactionSuccessful();
316 } finally {
317 db.endTransaction();
318 }
319
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700320 notifyListeners();
Sunny Goyald1064182015-08-13 12:08:30 -0700321 reloadLauncherIfExternal();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800322 return values.length;
323 }
324
325 @Override
Yura085c8532014-02-11 15:15:29 +0000326 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
327 throws OperationApplicationException {
Sunny Goyald3849d12015-10-29 10:28:32 -0700328 createDbIfNotExists();
Yura085c8532014-02-11 15:15:29 +0000329 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
330 db.beginTransaction();
331 try {
332 ContentProviderResult[] result = super.applyBatch(operations);
333 db.setTransactionSuccessful();
Sunny Goyald1064182015-08-13 12:08:30 -0700334 reloadLauncherIfExternal();
Yura085c8532014-02-11 15:15:29 +0000335 return result;
336 } finally {
337 db.endTransaction();
338 }
339 }
340
341 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800342 public int delete(Uri uri, String selection, String[] selectionArgs) {
Sunny Goyald3849d12015-10-29 10:28:32 -0700343 createDbIfNotExists();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800344 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
345
346 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800347
Louis Begina9c21c62016-08-15 15:18:41 -0700348 if (Binder.getCallingPid() != Process.myPid()
349 && Favorites.TABLE_NAME.equalsIgnoreCase(args.table)) {
Sunny Goyal55fddc82017-04-06 15:02:52 -0700350 mOpenHelper.removeGhostWidgets(mOpenHelper.getWritableDatabase());
Louis Begina9c21c62016-08-15 15:18:41 -0700351 }
352 int count = db.delete(args.table, args.where, args.args);
353 if (count > 0) {
354 notifyListeners();
355 reloadLauncherIfExternal();
356 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 return count;
358 }
359
360 @Override
361 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
Sunny Goyald3849d12015-10-29 10:28:32 -0700362 createDbIfNotExists();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
364
Chris Wren1ada10d2013-09-13 18:01:38 -0400365 addModifiedTime(values);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
367 int count = db.update(args.table, values, args.where, args.args);
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700368 if (count > 0) notifyListeners();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369
Sunny Goyald1064182015-08-13 12:08:30 -0700370 reloadLauncherIfExternal();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800371 return count;
372 }
373
Sunny Goyal7779d622015-06-11 16:18:39 -0700374 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700375 public Bundle call(String method, final String arg, final Bundle extras) {
Sunny Goyal7779d622015-06-11 16:18:39 -0700376 if (Binder.getCallingUid() != Process.myUid()) {
377 return null;
378 }
Sunny Goyald3849d12015-10-29 10:28:32 -0700379 createDbIfNotExists();
Sunny Goyal7779d622015-06-11 16:18:39 -0700380
381 switch (method) {
Tony Wickham827cef22016-03-17 15:39:39 -0700382 case LauncherSettings.Settings.METHOD_SET_EXTRACTED_COLORS_AND_WALLPAPER_ID: {
383 String extractedColors = extras.getString(
384 LauncherSettings.Settings.EXTRA_EXTRACTED_COLORS);
385 int wallpaperId = extras.getInt(LauncherSettings.Settings.EXTRA_WALLPAPER_ID);
386 Utilities.getPrefs(getContext()).edit()
387 .putString(ExtractionUtils.EXTRACTED_COLORS_PREFERENCE_KEY, extractedColors)
388 .putInt(ExtractionUtils.WALLPAPER_ID_PREFERENCE_KEY, wallpaperId)
389 .apply();
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700390 mListenerHandler.sendEmptyMessage(ChangeListenerWrapper.MSG_EXTRACTED_COLORS_CHANGED);
Tony Wickham827cef22016-03-17 15:39:39 -0700391 Bundle result = new Bundle();
392 result.putString(LauncherSettings.Settings.EXTRA_VALUE, extractedColors);
393 return result;
394 }
Sunny Goyald2497482015-09-22 18:24:19 -0700395 case LauncherSettings.Settings.METHOD_CLEAR_EMPTY_DB_FLAG: {
396 clearFlagEmptyDbCreated();
397 return null;
398 }
Sunny Goyala5c8a9e2016-07-08 08:32:44 -0700399 case LauncherSettings.Settings.METHOD_WAS_EMPTY_DB_CREATED : {
400 Bundle result = new Bundle();
401 result.putBoolean(LauncherSettings.Settings.EXTRA_VALUE,
402 Utilities.getPrefs(getContext()).getBoolean(EMPTY_DATABASE_CREATED, false));
403 return result;
404 }
Sunny Goyald2497482015-09-22 18:24:19 -0700405 case LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS: {
406 Bundle result = new Bundle();
407 result.putSerializable(LauncherSettings.Settings.EXTRA_VALUE, deleteEmptyFolders());
408 return result;
409 }
410 case LauncherSettings.Settings.METHOD_NEW_ITEM_ID: {
411 Bundle result = new Bundle();
412 result.putLong(LauncherSettings.Settings.EXTRA_VALUE, mOpenHelper.generateNewItemId());
413 return result;
414 }
415 case LauncherSettings.Settings.METHOD_NEW_SCREEN_ID: {
416 Bundle result = new Bundle();
417 result.putLong(LauncherSettings.Settings.EXTRA_VALUE, mOpenHelper.generateNewScreenId());
418 return result;
419 }
420 case LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB: {
Sunny Goyale05b08f2017-02-23 18:30:22 -0800421 mOpenHelper.createEmptyDB(mOpenHelper.getWritableDatabase());
Sunny Goyald2497482015-09-22 18:24:19 -0700422 return null;
423 }
424 case LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES: {
425 loadDefaultFavoritesIfNecessary();
426 return null;
427 }
Sunny Goyal55fddc82017-04-06 15:02:52 -0700428 case LauncherSettings.Settings.METHOD_REMOVE_GHOST_WIDGETS: {
429 mOpenHelper.removeGhostWidgets(mOpenHelper.getWritableDatabase());
430 return null;
431 }
Sunny Goyal7779d622015-06-11 16:18:39 -0700432 }
433 return null;
434 }
435
Sunny Goyalb1622cc2015-06-10 16:00:42 -0700436 /**
437 * Deletes any empty folder from the DB.
438 * @return Ids of deleted folders.
439 */
Sunny Goyald2497482015-09-22 18:24:19 -0700440 private ArrayList<Long> deleteEmptyFolders() {
441 ArrayList<Long> folderIds = new ArrayList<>();
Sunny Goyalb1622cc2015-06-10 16:00:42 -0700442 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
443 db.beginTransaction();
444 try {
445 // Select folders whose id do not match any container value.
446 String selection = LauncherSettings.Favorites.ITEM_TYPE + " = "
447 + LauncherSettings.Favorites.ITEM_TYPE_FOLDER + " AND "
448 + LauncherSettings.Favorites._ID + " NOT IN (SELECT " +
449 LauncherSettings.Favorites.CONTAINER + " FROM "
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700450 + Favorites.TABLE_NAME + ")";
451 Cursor c = db.query(Favorites.TABLE_NAME,
Sunny Goyalb1622cc2015-06-10 16:00:42 -0700452 new String[] {LauncherSettings.Favorites._ID},
453 selection, null, null, null, null);
454 while (c.moveToNext()) {
455 folderIds.add(c.getLong(0));
456 }
457 c.close();
Sunny Goyald2497482015-09-22 18:24:19 -0700458 if (!folderIds.isEmpty()) {
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700459 db.delete(Favorites.TABLE_NAME, Utilities.createDbSelectionQuery(
Sunny Goyalb1622cc2015-06-10 16:00:42 -0700460 LauncherSettings.Favorites._ID, folderIds), null);
461 }
462 db.setTransactionSuccessful();
463 } catch (SQLException ex) {
464 Log.e(TAG, ex.getMessage(), ex);
465 folderIds.clear();
466 } finally {
467 db.endTransaction();
468 }
469 return folderIds;
470 }
471
Sunny Goyalf076eae2016-01-11 12:25:10 -0800472 /**
473 * Overridden in tests
474 */
475 protected void notifyListeners() {
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700476 mListenerHandler.sendEmptyMessage(ChangeListenerWrapper.MSG_LAUNCHER_PROVIDER_CHANGED);
Chris Wren1ada10d2013-09-13 18:01:38 -0400477 }
478
Adam Cohen091440a2015-03-18 14:16:05 -0700479 @Thunk static void addModifiedTime(ContentValues values) {
Chris Wren1ada10d2013-09-13 18:01:38 -0400480 values.put(LauncherSettings.ChangeLogColumns.MODIFIED, System.currentTimeMillis());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800481 }
482
Sunny Goyald2497482015-09-22 18:24:19 -0700483 private void clearFlagEmptyDbCreated() {
Sunny Goyalf7258242015-10-19 16:59:07 -0700484 Utilities.getPrefs(getContext()).edit().remove(EMPTY_DATABASE_CREATED).commit();
Sunny Goyal33d44382014-10-16 09:24:19 -0700485 }
486
Sunny Goyal42de82f2014-09-26 22:09:29 -0700487 /**
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700488 * Loads the default workspace based on the following priority scheme:
Sunny Goyalb2d46ce2015-03-26 11:32:11 -0700489 * 1) From the app restrictions
490 * 2) From a package provided by play store
491 * 3) From a partner configuration APK, already in the system image
492 * 4) The default configuration for the particular device
Brian Muramatsu5524b492012-10-02 16:55:54 -0700493 */
Sunny Goyald2497482015-09-22 18:24:19 -0700494 synchronized private void loadDefaultFavoritesIfNecessary() {
Sunny Goyalf7258242015-10-19 16:59:07 -0700495 SharedPreferences sp = Utilities.getPrefs(getContext());
Adam Cohene25af792013-06-06 23:08:25 -0700496
Winson Chungc763c4e2013-07-19 13:49:06 -0700497 if (sp.getBoolean(EMPTY_DATABASE_CREATED, false)) {
Chris Wren5dee7af2013-12-20 17:22:11 -0500498 Log.d(TAG, "loading default workspace");
Michael Jurka45355c42012-10-08 13:21:35 +0200499
Sunny Goyal55fddc82017-04-06 15:02:52 -0700500 AppWidgetHost widgetHost = mOpenHelper.newLauncherWidgetHost();
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700501 AutoInstallsLayout loader = createWorkspaceLoaderFromAppRestriction(widgetHost);
Sunny Goyalb2d46ce2015-03-26 11:32:11 -0700502 if (loader == null) {
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700503 loader = AutoInstallsLayout.get(getContext(),widgetHost, mOpenHelper);
Sunny Goyalb2d46ce2015-03-26 11:32:11 -0700504 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700505 if (loader == null) {
Adam Cohen9b8f51f2014-05-30 15:34:09 -0700506 final Partner partner = Partner.get(getContext().getPackageManager());
507 if (partner != null && partner.hasDefaultLayout()) {
508 final Resources partnerRes = partner.getResources();
Adam Cohen4ae96ce2014-08-29 15:05:48 -0700509 int workspaceResId = partnerRes.getIdentifier(Partner.RES_DEFAULT_LAYOUT,
Adam Cohen9b8f51f2014-05-30 15:34:09 -0700510 "xml", partner.getPackageName());
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700511 if (workspaceResId != 0) {
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700512 loader = new DefaultLayoutParser(getContext(), widgetHost,
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700513 mOpenHelper, partnerRes, workspaceResId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700514 }
Adam Cohen9b8f51f2014-05-30 15:34:09 -0700515 }
516 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700517
Sunny Goyal9d219682014-10-23 14:21:02 -0700518 final boolean usingExternallyProvidedLayout = loader != null;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700519 if (loader == null) {
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700520 loader = getDefaultLayoutParser(widgetHost);
Brian Muramatsu5524b492012-10-02 16:55:54 -0700521 }
Sunny Goyalc6c8fef2015-03-04 09:51:18 -0800522
523 // There might be some partially restored DB items, due to buggy restore logic in
524 // previous versions of launcher.
Sunny Goyale05b08f2017-02-23 18:30:22 -0800525 mOpenHelper.createEmptyDB(mOpenHelper.getWritableDatabase());
Michael Jurkab85f8a42012-04-25 15:48:32 -0700526 // Populate favorites table with initial favorites
Sunny Goyal9d219682014-10-23 14:21:02 -0700527 if ((mOpenHelper.loadFavorites(mOpenHelper.getWritableDatabase(), loader) <= 0)
528 && usingExternallyProvidedLayout) {
529 // Unable to load external layout. Cleanup and load the internal layout.
Sunny Goyale05b08f2017-02-23 18:30:22 -0800530 mOpenHelper.createEmptyDB(mOpenHelper.getWritableDatabase());
Sunny Goyal9d219682014-10-23 14:21:02 -0700531 mOpenHelper.loadFavorites(mOpenHelper.getWritableDatabase(),
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700532 getDefaultLayoutParser(widgetHost));
Sunny Goyal9d219682014-10-23 14:21:02 -0700533 }
Sunny Goyal33d44382014-10-16 09:24:19 -0700534 clearFlagEmptyDbCreated();
Michael Jurkab85f8a42012-04-25 15:48:32 -0700535 }
536 }
537
Sunny Goyalb2d46ce2015-03-26 11:32:11 -0700538 /**
539 * Creates workspace loader from an XML resource listed in the app restrictions.
540 *
541 * @return the loader if the restrictions are set and the resource exists; null otherwise.
542 */
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700543 private AutoInstallsLayout createWorkspaceLoaderFromAppRestriction(AppWidgetHost widgetHost) {
Sunny Goyalb2d46ce2015-03-26 11:32:11 -0700544 Context ctx = getContext();
545 UserManager um = (UserManager) ctx.getSystemService(Context.USER_SERVICE);
546 Bundle bundle = um.getApplicationRestrictions(ctx.getPackageName());
Sunny Goyal35ca8732015-04-06 10:45:31 -0700547 if (bundle == null) {
548 return null;
549 }
Sunny Goyalb2d46ce2015-03-26 11:32:11 -0700550
Sunny Goyal35ca8732015-04-06 10:45:31 -0700551 String packageName = bundle.getString(RESTRICTION_PACKAGE_NAME);
Sunny Goyalb2d46ce2015-03-26 11:32:11 -0700552 if (packageName != null) {
553 try {
554 Resources targetResources = ctx.getPackageManager()
555 .getResourcesForApplication(packageName);
556 return AutoInstallsLayout.get(ctx, packageName, targetResources,
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700557 widgetHost, mOpenHelper);
Sunny Goyalb2d46ce2015-03-26 11:32:11 -0700558 } catch (NameNotFoundException e) {
559 Log.e(TAG, "Target package for restricted profile not found", e);
560 return null;
561 }
562 }
563 return null;
564 }
565
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700566 private DefaultLayoutParser getDefaultLayoutParser(AppWidgetHost widgetHost) {
Sunny Goyal87f784c2017-01-11 10:48:34 -0800567 int defaultLayout = LauncherAppState.getIDP(getContext()).defaultLayoutId;
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700568 return new DefaultLayoutParser(getContext(), widgetHost,
Sunny Goyal9d219682014-10-23 14:21:02 -0700569 mOpenHelper, getContext().getResources(), defaultLayout);
570 }
571
Sunny Goyald3849d12015-10-29 10:28:32 -0700572 /**
Sunny Goyalf076eae2016-01-11 12:25:10 -0800573 * The class is subclassed in tests to create an in-memory db.
574 */
Sunny Goyalc190dbf2016-05-05 14:37:05 -0700575 public static class DatabaseHelper extends SQLiteOpenHelper implements LayoutParserCallback {
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700576 private final Handler mWidgetHostResetHandler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800577 private final Context mContext;
Adam Cohendcd297f2013-06-18 13:13:40 -0700578 private long mMaxItemId = -1;
579 private long mMaxScreenId = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800580
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700581 DatabaseHelper(Context context, Handler widgetHostResetHandler) {
582 this(context, widgetHostResetHandler, LauncherFiles.LAUNCHER_DB);
Sunny Goyal6fb929e2015-09-23 11:40:53 -0700583 // Table creation sometimes fails silently, which leads to a crash loop.
584 // This way, we will try to create a table every time after crash, so the device
585 // would eventually be able to recover.
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700586 if (!tableExists(Favorites.TABLE_NAME) || !tableExists(WorkspaceScreens.TABLE_NAME)) {
Sunny Goyal6fb929e2015-09-23 11:40:53 -0700587 Log.e(TAG, "Tables are missing after onCreate has been called. Trying to recreate");
588 // This operation is a no-op if the table already exists.
589 addFavoritesTable(getWritableDatabase(), true);
590 addWorkspacesTable(getWritableDatabase(), true);
591 }
592
Sunny Goyal7eab3cc2016-03-18 17:42:55 -0700593 initIds();
594 }
595
596 /**
Sunny Goyalc190dbf2016-05-05 14:37:05 -0700597 * Constructor used in tests and for restore.
Sunny Goyal7eab3cc2016-03-18 17:42:55 -0700598 */
599 public DatabaseHelper(
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700600 Context context, Handler widgetHostResetHandler, String tableName) {
Sunny Goyal3c7d55b2017-04-13 12:01:47 -0700601 super(new NoLocaleSqliteContext(context), tableName, null, SCHEMA_VERSION);
Sunny Goyal7eab3cc2016-03-18 17:42:55 -0700602 mContext = context;
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700603 mWidgetHostResetHandler = widgetHostResetHandler;
Sunny Goyal7eab3cc2016-03-18 17:42:55 -0700604 }
605
606 protected void initIds() {
Winson Chung3d503fb2011-07-13 17:25:49 -0700607 // In the case where neither onCreate nor onUpgrade gets called, we read the maxId from
608 // the DB here
Adam Cohendcd297f2013-06-18 13:13:40 -0700609 if (mMaxItemId == -1) {
610 mMaxItemId = initializeMaxItemId(getWritableDatabase());
611 }
612 if (mMaxScreenId == -1) {
613 mMaxScreenId = initializeMaxScreenId(getWritableDatabase());
Winson Chung3d503fb2011-07-13 17:25:49 -0700614 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800615 }
616
Sunny Goyal6fb929e2015-09-23 11:40:53 -0700617 private boolean tableExists(String tableName) {
618 Cursor c = getReadableDatabase().query(
619 true, "sqlite_master", new String[] {"tbl_name"},
620 "tbl_name = ?", new String[] {tableName},
621 null, null, null, null, null);
622 try {
623 return c.getCount() > 0;
624 } finally {
625 c.close();
626 }
627 }
628
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800629 @Override
630 public void onCreate(SQLiteDatabase db) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800631 if (LOGD) Log.d(TAG, "creating new launcher database");
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700632
Adam Cohendcd297f2013-06-18 13:13:40 -0700633 mMaxItemId = 1;
634 mMaxScreenId = 0;
Michael Jurkaa8c760d2011-04-28 14:59:33 -0700635
Sunny Goyal6fb929e2015-09-23 11:40:53 -0700636 addFavoritesTable(db, false);
637 addWorkspacesTable(db, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638
Sunny Goyale87e6ab2014-11-21 22:42:53 -0800639 // Fresh and clean launcher DB.
640 mMaxItemId = initializeMaxItemId(db);
Sunny Goyalf076eae2016-01-11 12:25:10 -0800641 onEmptyDbCreated();
642 }
643
644 /**
645 * Overriden in tests.
646 */
647 protected void onEmptyDbCreated() {
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700648 // Database was just created, so wipe any previous widgets
649 if (mWidgetHostResetHandler != null) {
Sunny Goyal55fddc82017-04-06 15:02:52 -0700650 newLauncherWidgetHost().deleteHost();
Sunny Goyal2e013ea2016-10-07 16:17:19 -0700651 mWidgetHostResetHandler.sendEmptyMessage(
Sunny Goyal72db9af2016-11-29 07:25:49 +0530652 ChangeListenerWrapper.MSG_APP_WIDGET_HOST_RESET);
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700653 }
654
Sunny Goyalf076eae2016-01-11 12:25:10 -0800655 // Set the flag for empty DB
656 Utilities.getPrefs(mContext).edit().putBoolean(EMPTY_DATABASE_CREATED, true).commit();
Sunny Goyale2fba6c2015-05-12 10:39:59 -0700657
658 // When a new DB is created, remove all previously stored managed profile information.
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800659 ManagedProfileHeuristic.processAllUsers(Collections.<UserHandle>emptyList(),
Sunny Goyalf076eae2016-01-11 12:25:10 -0800660 mContext);
661 }
662
Sunny Goyale8f7d5a2016-05-24 11:30:14 -0700663 public long getDefaultUserSerial() {
Sunny Goyalf076eae2016-01-11 12:25:10 -0800664 return UserManagerCompat.getInstance(mContext).getSerialNumberForUser(
Sunny Goyal7c74e4a2016-12-15 15:53:17 -0800665 Process.myUserHandle());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800666 }
667
Sunny Goyal6fb929e2015-09-23 11:40:53 -0700668 private void addFavoritesTable(SQLiteDatabase db, boolean optional) {
Sunny Goyalc190dbf2016-05-05 14:37:05 -0700669 Favorites.addTableToDb(db, getDefaultUserSerial(), optional);
Sunny Goyal6fb929e2015-09-23 11:40:53 -0700670 }
671
672 private void addWorkspacesTable(SQLiteDatabase db, boolean optional) {
673 String ifNotExists = optional ? " IF NOT EXISTS " : "";
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700674 db.execSQL("CREATE TABLE " + ifNotExists + WorkspaceScreens.TABLE_NAME + " (" +
Sunny Goyald2f38192015-02-25 10:46:34 -0800675 LauncherSettings.WorkspaceScreens._ID + " INTEGER PRIMARY KEY," +
Chris Wren1ada10d2013-09-13 18:01:38 -0400676 LauncherSettings.WorkspaceScreens.SCREEN_RANK + " INTEGER," +
677 LauncherSettings.ChangeLogColumns.MODIFIED + " INTEGER NOT NULL DEFAULT 0" +
Adam Cohendcd297f2013-06-18 13:13:40 -0700678 ");");
679 }
680
Adam Cohen119285e2014-04-02 16:59:08 -0700681 private void removeOrphanedItems(SQLiteDatabase db) {
Adam Cohenf9c14de2014-04-17 18:20:45 -0700682 // Delete items directly on the workspace who's screen id doesn't exist
683 // "DELETE FROM favorites WHERE screen NOT IN (SELECT _id FROM workspaceScreens)
684 // AND container = -100"
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700685 String removeOrphanedDesktopItems = "DELETE FROM " + Favorites.TABLE_NAME +
Adam Cohenf9c14de2014-04-17 18:20:45 -0700686 " WHERE " +
Adam Cohen119285e2014-04-02 16:59:08 -0700687 LauncherSettings.Favorites.SCREEN + " NOT IN (SELECT " +
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700688 LauncherSettings.WorkspaceScreens._ID + " FROM " + WorkspaceScreens.TABLE_NAME + ")" +
Adam Cohenf9c14de2014-04-17 18:20:45 -0700689 " AND " +
690 LauncherSettings.Favorites.CONTAINER + " = " +
691 LauncherSettings.Favorites.CONTAINER_DESKTOP;
692 db.execSQL(removeOrphanedDesktopItems);
693
694 // Delete items contained in folders which no longer exist (after above statement)
695 // "DELETE FROM favorites WHERE container <> -100 AND container <> -101 AND container
696 // NOT IN (SELECT _id FROM favorites WHERE itemType = 2)"
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700697 String removeOrphanedFolderItems = "DELETE FROM " + Favorites.TABLE_NAME +
Adam Cohenf9c14de2014-04-17 18:20:45 -0700698 " WHERE " +
699 LauncherSettings.Favorites.CONTAINER + " <> " +
700 LauncherSettings.Favorites.CONTAINER_DESKTOP +
701 " AND "
702 + LauncherSettings.Favorites.CONTAINER + " <> " +
703 LauncherSettings.Favorites.CONTAINER_HOTSEAT +
704 " AND "
705 + LauncherSettings.Favorites.CONTAINER + " NOT IN (SELECT " +
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700706 LauncherSettings.Favorites._ID + " FROM " + Favorites.TABLE_NAME +
Adam Cohenf9c14de2014-04-17 18:20:45 -0700707 " WHERE " + LauncherSettings.Favorites.ITEM_TYPE + " = " +
708 LauncherSettings.Favorites.ITEM_TYPE_FOLDER + ")";
709 db.execSQL(removeOrphanedFolderItems);
Adam Cohen119285e2014-04-02 16:59:08 -0700710 }
711
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 @Override
Sunny Goyal3c7d55b2017-04-13 12:01:47 -0700713 public void onOpen(SQLiteDatabase db) {
714 super.onOpen(db);
715 SharedPreferences prefs = mContext
716 .getSharedPreferences(LauncherFiles.DEVICE_PREFERENCES_KEY, 0);
717 int oldVersion = prefs.getInt(PREF_KEY_DATA_VERISON, 0);
718 if (oldVersion != DATA_VERSION) {
719 // Only run the data upgrade path for an existing db.
720 if (!Utilities.getPrefs(mContext).getBoolean(EMPTY_DATABASE_CREATED, false)) {
721 db.beginTransaction();
722 try {
723 onDataUpgrade(db, oldVersion);
724 db.setTransactionSuccessful();
725 } catch (Exception e) {
726 Log.d(TAG, "Error updating data version, ignoring", e);
727 return;
728 } finally {
729 db.endTransaction();
730 }
731 }
732 prefs.edit().putInt(PREF_KEY_DATA_VERISON, DATA_VERSION).apply();
733 }
734 }
735
736 /**
737 * Called when the data is updated as part of app update. It can be called multiple times
738 * with old version, even though it had been run before. The changes made here must be
739 * backwards compatible, else we risk breaking old devices during restore or binary
740 * version downgrade.
741 */
742 protected void onDataUpgrade(SQLiteDatabase db, int oldVersion) {
743 switch (oldVersion) {
744 case 0:
745 case 1: {
746 // Remove "profile extra"
747 UserManagerCompat um = UserManagerCompat.getInstance(mContext);
748 for (UserHandle user : um.getUserProfiles()) {
749 long serial = um.getSerialNumberForUser(user);
750 String sql = "update favorites set intent = replace(intent, "
751 + "';l.profile=" + serial + ";', ';') where itemType = 0;";
752 db.execSQL(sql);
753 }
754 }
755 case 2:
Sunny Goyal55fddc82017-04-06 15:02:52 -0700756 removeGhostWidgets(db);
757 case 3:
Sunny Goyal3c7d55b2017-04-13 12:01:47 -0700758 // data updated
759 return;
760 }
761 }
762
763 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800764 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Winson Chungc763c4e2013-07-19 13:49:06 -0700765 if (LOGD) Log.d(TAG, "onUpgrade triggered: " + oldVersion);
Sunny Goyala2cc6242015-01-14 14:23:02 -0800766 switch (oldVersion) {
767 // The version cannot be lower that 12, as Launcher3 never supported a lower
Sunny Goyale87e6ab2014-11-21 22:42:53 -0800768 // version of the DB.
Sunny Goyala2cc6242015-01-14 14:23:02 -0800769 case 12: {
770 // With the new shrink-wrapped and re-orderable workspaces, it makes sense
771 // to persist workspace screens and their relative order.
772 mMaxScreenId = 0;
Sunny Goyal6fb929e2015-09-23 11:40:53 -0700773 addWorkspacesTable(db, false);
Sunny Goyala2cc6242015-01-14 14:23:02 -0800774 }
775 case 13: {
776 db.beginTransaction();
777 try {
778 // Insert new column for holding widget provider name
779 db.execSQL("ALTER TABLE favorites " +
780 "ADD COLUMN appWidgetProvider TEXT;");
781 db.setTransactionSuccessful();
782 } catch (SQLException ex) {
783 Log.e(TAG, ex.getMessage(), ex);
784 // Old version remains, which means we wipe old data
785 break;
786 } finally {
787 db.endTransaction();
788 }
789 }
790 case 14: {
791 db.beginTransaction();
792 try {
793 // Insert new column for holding update timestamp
794 db.execSQL("ALTER TABLE favorites " +
795 "ADD COLUMN modified INTEGER NOT NULL DEFAULT 0;");
796 db.execSQL("ALTER TABLE workspaceScreens " +
797 "ADD COLUMN modified INTEGER NOT NULL DEFAULT 0;");
798 db.setTransactionSuccessful();
799 } catch (SQLException ex) {
800 Log.e(TAG, ex.getMessage(), ex);
801 // Old version remains, which means we wipe old data
802 break;
803 } finally {
804 db.endTransaction();
805 }
806 }
807 case 15: {
Sunny Goyal5d85c442015-03-10 13:14:47 -0700808 if (!addIntegerColumn(db, Favorites.RESTORED, 0)) {
Sunny Goyala2cc6242015-01-14 14:23:02 -0800809 // Old version remains, which means we wipe old data
810 break;
Sunny Goyala2cc6242015-01-14 14:23:02 -0800811 }
812 }
813 case 16: {
Sunny Goyal10629b02016-09-01 12:50:11 -0700814 // No-op
Sunny Goyala2cc6242015-01-14 14:23:02 -0800815 }
816 case 17: {
817 // No-op
818 }
819 case 18: {
820 // Due to a data loss bug, some users may have items associated with screen ids
821 // which no longer exist. Since this can cause other problems, and since the user
822 // will never see these items anyway, we use database upgrade as an opportunity to
823 // clean things up.
824 removeOrphanedItems(db);
825 }
826 case 19: {
827 // Add userId column
828 if (!addProfileColumn(db)) {
829 // Old version remains, which means we wipe old data
830 break;
831 }
832 }
833 case 20:
834 if (!updateFolderItemsRank(db, true)) {
835 break;
836 }
Sunny Goyald2f38192015-02-25 10:46:34 -0800837 case 21:
838 // Recreate workspace table with screen id a primary key
839 if (!recreateWorkspaceTable(db)) {
840 break;
841 }
842 case 22: {
Sunny Goyal5d85c442015-03-10 13:14:47 -0700843 if (!addIntegerColumn(db, Favorites.OPTIONS, 0)) {
844 // Old version remains, which means we wipe old data
845 break;
846 }
847 }
Sunny Goyal0b037782015-04-02 10:27:03 -0700848 case 23:
Sunny Goyal5c97f512015-05-19 16:03:28 -0700849 // No-op
Sunny Goyale2fba6c2015-05-12 10:39:59 -0700850 case 24:
851 ManagedProfileHeuristic.markExistingUsersForNoFolderCreation(mContext);
Sunny Goyal5c97f512015-05-19 16:03:28 -0700852 case 25:
853 convertShortcutsToLauncherActivities(db);
Sunny Goyala9e2f5a2016-06-10 12:22:04 -0700854 case 26:
855 // QSB was moved to the grid. Clear the first row on screen 0.
856 if (FeatureFlags.QSB_ON_FIRST_SCREEN &&
Sunny Goyal8ad02b82016-12-29 13:31:43 -0800857 !LauncherDbUtils.prepareScreenZeroToHostQsb(mContext, db)) {
Sunny Goyala9e2f5a2016-06-10 12:22:04 -0700858 break;
859 }
Sunny Goyal3c7d55b2017-04-13 12:01:47 -0700860 case 27:
Sunny Goyala2cc6242015-01-14 14:23:02 -0800861 // DB Upgraded successfully
862 return;
Chris Wrend5e66bf2013-09-16 14:02:29 -0400863 }
864
Sunny Goyala2cc6242015-01-14 14:23:02 -0800865 // DB was not upgraded
866 Log.w(TAG, "Destroying all old data.");
867 createEmptyDB(db);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 }
Romain Guy7eb9e5e2009-12-02 20:10:07 -0800869
Adam Cohen9b1d0622014-05-21 19:01:57 -0700870 @Override
871 public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Sunny Goyal3c7d55b2017-04-13 12:01:47 -0700872 if (oldVersion == 28 && newVersion == 27) {
873 // TODO: remove this check. This is only applicable for internal development/testing
874 // and for any released version of Launcher.
875 return;
876 }
Adam Cohen9b1d0622014-05-21 19:01:57 -0700877 // This shouldn't happen -- throw our hands up in the air and start over.
878 Log.w(TAG, "Database version downgrade from: " + oldVersion + " to " + newVersion +
879 ". Wiping databse.");
Sunny Goyal42de82f2014-09-26 22:09:29 -0700880 createEmptyDB(db);
881 }
Adam Cohen9b1d0622014-05-21 19:01:57 -0700882
Sunny Goyal42de82f2014-09-26 22:09:29 -0700883 /**
884 * Clears all the data for a fresh start.
885 */
886 public void createEmptyDB(SQLiteDatabase db) {
Sunny Goyale05b08f2017-02-23 18:30:22 -0800887 db.beginTransaction();
888 try {
889 db.execSQL("DROP TABLE IF EXISTS " + Favorites.TABLE_NAME);
890 db.execSQL("DROP TABLE IF EXISTS " + WorkspaceScreens.TABLE_NAME);
891 onCreate(db);
892 db.setTransactionSuccessful();
893 } finally {
894 db.endTransaction();
895 }
Adam Cohen9b1d0622014-05-21 19:01:57 -0700896 }
897
Sunny Goyald2f38192015-02-25 10:46:34 -0800898 /**
Sunny Goyal55fddc82017-04-06 15:02:52 -0700899 * Removes widgets which are registered to the Launcher's host, but are not present
900 * in our model.
901 */
902 public void removeGhostWidgets(SQLiteDatabase db) {
903 // Get all existing widget ids.
904 final AppWidgetHost host = newLauncherWidgetHost();
905 final int[] allWidgets;
906 try {
907 Method getter = AppWidgetHost.class.getDeclaredMethod("getAppWidgetIds");
908 getter.setAccessible(true);
909 allWidgets = (int[]) getter.invoke(host);
910 } catch (Exception e) {
911 Log.e(TAG, "getAppWidgetIds not supported", e);
912 return;
913 }
914 try {
915 Cursor c = db.query(Favorites.TABLE_NAME,
916 new String[] {Favorites.APPWIDGET_ID },
917 "itemType=" + Favorites.ITEM_TYPE_APPWIDGET, null, null, null, null);
918 HashSet<Integer> validWidgets = new HashSet<>();
919 while (c.moveToNext()) {
920 validWidgets.add(c.getInt(0));
921 }
922 c.close();
923
924 for (int widgetId : allWidgets) {
925 if (!validWidgets.contains(widgetId)) {
926 try {
927 FileLog.d(TAG, "Deleting invalid widget " + widgetId);
928 host.deleteAppWidgetId(widgetId);
929 } catch (RuntimeException e) {
930 // Ignore
931 }
932 }
933 }
934 } catch (SQLException ex) {
935 Log.w(TAG, "Error getting widgets list", ex);
936 }
937 }
938
939 /**
Sunny Goyal0b037782015-04-02 10:27:03 -0700940 * Replaces all shortcuts of type {@link Favorites#ITEM_TYPE_SHORTCUT} which have a valid
941 * launcher activity target with {@link Favorites#ITEM_TYPE_APPLICATION}.
942 */
Sunny Goyal316490e2015-06-02 09:38:28 -0700943 @Thunk void convertShortcutsToLauncherActivities(SQLiteDatabase db) {
Sunny Goyal0b037782015-04-02 10:27:03 -0700944 db.beginTransaction();
945 Cursor c = null;
946 SQLiteStatement updateStmt = null;
947
948 try {
949 // Only consider the primary user as other users can't have a shortcut.
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700950 long userSerial = getDefaultUserSerial();
951 c = db.query(Favorites.TABLE_NAME, new String[] {
Sunny Goyal0b037782015-04-02 10:27:03 -0700952 Favorites._ID,
953 Favorites.INTENT,
954 }, "itemType=" + Favorites.ITEM_TYPE_SHORTCUT + " AND profileId=" + userSerial,
955 null, null, null, null);
956
957 updateStmt = db.compileStatement("UPDATE favorites SET itemType="
958 + Favorites.ITEM_TYPE_APPLICATION + " WHERE _id=?");
959
960 final int idIndex = c.getColumnIndexOrThrow(Favorites._ID);
961 final int intentIndex = c.getColumnIndexOrThrow(Favorites.INTENT);
962
963 while (c.moveToNext()) {
964 String intentDescription = c.getString(intentIndex);
965 Intent intent;
966 try {
967 intent = Intent.parseUri(intentDescription, 0);
968 } catch (URISyntaxException e) {
969 Log.e(TAG, "Unable to parse intent", e);
970 continue;
971 }
972
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700973 if (!Utilities.isLauncherAppTarget(intent)) {
Sunny Goyal0b037782015-04-02 10:27:03 -0700974 continue;
975 }
976
977 long id = c.getLong(idIndex);
978 updateStmt.bindLong(1, id);
Sunny Goyalc22841b2015-07-13 19:59:50 -0700979 updateStmt.executeUpdateDelete();
Sunny Goyal0b037782015-04-02 10:27:03 -0700980 }
981 db.setTransactionSuccessful();
982 } catch (SQLException ex) {
983 Log.w(TAG, "Error deduping shortcuts", ex);
984 } finally {
985 db.endTransaction();
986 if (c != null) {
987 c.close();
988 }
989 if (updateStmt != null) {
990 updateStmt.close();
991 }
992 }
993 }
994
995 /**
Sunny Goyald2f38192015-02-25 10:46:34 -0800996 * Recreates workspace table and migrates data to the new table.
997 */
998 public boolean recreateWorkspaceTable(SQLiteDatabase db) {
999 db.beginTransaction();
1000 try {
Sunny Goyalb5b55c82016-05-10 12:28:59 -07001001 Cursor c = db.query(WorkspaceScreens.TABLE_NAME,
Sunny Goyald2f38192015-02-25 10:46:34 -08001002 new String[] {LauncherSettings.WorkspaceScreens._ID},
1003 null, null, null, null,
1004 LauncherSettings.WorkspaceScreens.SCREEN_RANK);
1005 ArrayList<Long> sortedIDs = new ArrayList<Long>();
1006 long maxId = 0;
1007 try {
1008 while (c.moveToNext()) {
1009 Long id = c.getLong(0);
1010 if (!sortedIDs.contains(id)) {
1011 sortedIDs.add(id);
1012 maxId = Math.max(maxId, id);
1013 }
1014 }
1015 } finally {
1016 c.close();
1017 }
1018
Sunny Goyalb5b55c82016-05-10 12:28:59 -07001019 db.execSQL("DROP TABLE IF EXISTS " + WorkspaceScreens.TABLE_NAME);
Sunny Goyal6fb929e2015-09-23 11:40:53 -07001020 addWorkspacesTable(db, false);
Sunny Goyald2f38192015-02-25 10:46:34 -08001021
1022 // Add all screen ids back
1023 int total = sortedIDs.size();
1024 for (int i = 0; i < total; i++) {
1025 ContentValues values = new ContentValues();
1026 values.put(LauncherSettings.WorkspaceScreens._ID, sortedIDs.get(i));
1027 values.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
1028 addModifiedTime(values);
Sunny Goyalb5b55c82016-05-10 12:28:59 -07001029 db.insertOrThrow(WorkspaceScreens.TABLE_NAME, null, values);
Sunny Goyald2f38192015-02-25 10:46:34 -08001030 }
1031 db.setTransactionSuccessful();
1032 mMaxScreenId = maxId;
1033 } catch (SQLException ex) {
1034 // Old version remains, which means we wipe old data
1035 Log.e(TAG, ex.getMessage(), ex);
1036 return false;
1037 } finally {
1038 db.endTransaction();
1039 }
1040 return true;
1041 }
1042
Adam Cohen091440a2015-03-18 14:16:05 -07001043 @Thunk boolean updateFolderItemsRank(SQLiteDatabase db, boolean addRankColumn) {
Sunny Goyal08f72612015-01-05 13:41:43 -08001044 db.beginTransaction();
1045 try {
1046 if (addRankColumn) {
1047 // Insert new column for holding rank
1048 db.execSQL("ALTER TABLE favorites ADD COLUMN rank INTEGER NOT NULL DEFAULT 0;");
1049 }
1050
1051 // Get a map for folder ID to folder width
1052 Cursor c = db.rawQuery("SELECT container, MAX(cellX) FROM favorites"
1053 + " WHERE container IN (SELECT _id FROM favorites WHERE itemType = ?)"
1054 + " GROUP BY container;",
1055 new String[] {Integer.toString(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)});
1056
1057 while (c.moveToNext()) {
Sunny Goyalc87775d2015-02-10 19:52:36 -08001058 db.execSQL("UPDATE favorites SET rank=cellX+(cellY*?) WHERE "
1059 + "container=? AND cellX IS NOT NULL AND cellY IS NOT NULL;",
Sunny Goyal08f72612015-01-05 13:41:43 -08001060 new Object[] {c.getLong(1) + 1, c.getLong(0)});
1061 }
1062
1063 c.close();
1064 db.setTransactionSuccessful();
1065 } catch (SQLException ex) {
1066 // Old version remains, which means we wipe old data
1067 Log.e(TAG, ex.getMessage(), ex);
1068 return false;
1069 } finally {
1070 db.endTransaction();
1071 }
1072 return true;
1073 }
1074
Kenny Guyed131872014-04-30 03:02:21 +01001075 private boolean addProfileColumn(SQLiteDatabase db) {
Sunny Goyalb5b55c82016-05-10 12:28:59 -07001076 return addIntegerColumn(db, Favorites.PROFILE_ID, getDefaultUserSerial());
Sunny Goyal5d85c442015-03-10 13:14:47 -07001077 }
1078
1079 private boolean addIntegerColumn(SQLiteDatabase db, String columnName, long defaultValue) {
Kenny Guyed131872014-04-30 03:02:21 +01001080 db.beginTransaction();
1081 try {
Sunny Goyal5d85c442015-03-10 13:14:47 -07001082 db.execSQL("ALTER TABLE favorites ADD COLUMN "
1083 + columnName + " INTEGER NOT NULL DEFAULT " + defaultValue + ";");
Kenny Guyed131872014-04-30 03:02:21 +01001084 db.setTransactionSuccessful();
1085 } catch (SQLException ex) {
Kenny Guyed131872014-04-30 03:02:21 +01001086 Log.e(TAG, ex.getMessage(), ex);
1087 return false;
1088 } finally {
1089 db.endTransaction();
1090 }
1091 return true;
1092 }
1093
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001094 // Generates a new ID to use for an object in your database. This method should be only
1095 // called from the main UI thread. As an exception, we do call it when we call the
1096 // constructor from the worker thread; however, this doesn't extend until after the
1097 // constructor is called, and we only pass a reference to LauncherProvider to LauncherApp
1098 // after that point
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001099 @Override
Adam Cohendcd297f2013-06-18 13:13:40 -07001100 public long generateNewItemId() {
1101 if (mMaxItemId < 0) {
1102 throw new RuntimeException("Error: max item id was not initialized");
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001103 }
Adam Cohendcd297f2013-06-18 13:13:40 -07001104 mMaxItemId += 1;
1105 return mMaxItemId;
Michael Jurkaa8c760d2011-04-28 14:59:33 -07001106 }
1107
Sunny Goyal55fddc82017-04-06 15:02:52 -07001108 public AppWidgetHost newLauncherWidgetHost() {
1109 return new AppWidgetHost(mContext, Launcher.APPWIDGET_HOST_ID);
1110 }
1111
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001112 @Override
1113 public long insertAndCheck(SQLiteDatabase db, ContentValues values) {
Sunny Goyalb5b55c82016-05-10 12:28:59 -07001114 return dbInsertAndCheck(this, db, Favorites.TABLE_NAME, null, values);
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001115 }
1116
Chris Wren5dee7af2013-12-20 17:22:11 -05001117 public void checkId(String table, ContentValues values) {
1118 long id = values.getAsLong(LauncherSettings.BaseLauncherColumns._ID);
Sunny Goyal8e0e1d72016-10-10 10:41:41 -07001119 if (WorkspaceScreens.TABLE_NAME.equals(table)) {
Chris Wren5dee7af2013-12-20 17:22:11 -05001120 mMaxScreenId = Math.max(id, mMaxScreenId);
1121 } else {
1122 mMaxItemId = Math.max(id, mMaxItemId);
1123 }
1124 }
1125
Adam Cohendcd297f2013-06-18 13:13:40 -07001126 private long initializeMaxItemId(SQLiteDatabase db) {
Sunny Goyalb5b55c82016-05-10 12:28:59 -07001127 return getMaxId(db, Favorites.TABLE_NAME);
Adam Cohendcd297f2013-06-18 13:13:40 -07001128 }
1129
1130 // Generates a new ID to use for an workspace screen in your database. This method
1131 // should be only called from the main UI thread. As an exception, we do call it when we
1132 // call the constructor from the worker thread; however, this doesn't extend until after the
1133 // constructor is called, and we only pass a reference to LauncherProvider to LauncherApp
1134 // after that point
1135 public long generateNewScreenId() {
1136 if (mMaxScreenId < 0) {
1137 throw new RuntimeException("Error: max screen id was not initialized");
1138 }
1139 mMaxScreenId += 1;
1140 return mMaxScreenId;
1141 }
1142
Adam Cohendcd297f2013-06-18 13:13:40 -07001143 private long initializeMaxScreenId(SQLiteDatabase db) {
Sunny Goyalb5b55c82016-05-10 12:28:59 -07001144 return getMaxId(db, WorkspaceScreens.TABLE_NAME);
Adam Cohen7ec3bbf2014-07-31 00:09:45 -07001145 }
1146
Adam Cohen091440a2015-03-18 14:16:05 -07001147 @Thunk int loadFavorites(SQLiteDatabase db, AutoInstallsLayout loader) {
Adam Cohen71483f42014-05-15 14:04:01 -07001148 ArrayList<Long> screenIds = new ArrayList<Long>();
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001149 // TODO: Use multiple loaders with fall-back and transaction.
1150 int count = loader.loadLayout(db, screenIds);
Adam Cohen71483f42014-05-15 14:04:01 -07001151
1152 // Add the screens specified by the items above
1153 Collections.sort(screenIds);
1154 int rank = 0;
1155 ContentValues values = new ContentValues();
1156 for (Long id : screenIds) {
1157 values.clear();
1158 values.put(LauncherSettings.WorkspaceScreens._ID, id);
1159 values.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, rank);
Sunny Goyalb5b55c82016-05-10 12:28:59 -07001160 if (dbInsertAndCheck(this, db, WorkspaceScreens.TABLE_NAME, null, values) < 0) {
Adam Cohen71483f42014-05-15 14:04:01 -07001161 throw new RuntimeException("Failed initialize screen table"
1162 + "from default layout");
1163 }
1164 rank++;
1165 }
1166
1167 // Ensure that the max ids are initialized
1168 mMaxItemId = initializeMaxItemId(db);
1169 mMaxScreenId = initializeMaxScreenId(db);
Adam Cohen7ec3bbf2014-07-31 00:09:45 -07001170
Adam Cohen71483f42014-05-15 14:04:01 -07001171 return count;
1172 }
Mike Cleronb87bd162009-10-30 16:36:56 -07001173 }
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001174
Sunny Goyalfe4e4b92015-03-04 10:43:45 -08001175 /**
1176 * @return the max _id in the provided table.
1177 */
Adam Cohen091440a2015-03-18 14:16:05 -07001178 @Thunk static long getMaxId(SQLiteDatabase db, String table) {
Sunny Goyalfe4e4b92015-03-04 10:43:45 -08001179 Cursor c = db.rawQuery("SELECT MAX(_id) FROM " + table, null);
1180 // get the result
1181 long id = -1;
1182 if (c != null && c.moveToNext()) {
1183 id = c.getLong(0);
1184 }
1185 if (c != null) {
1186 c.close();
1187 }
1188
1189 if (id == -1) {
1190 throw new RuntimeException("Error: could not query max id in " + table);
1191 }
1192
1193 return id;
1194 }
1195
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 static class SqlArguments {
1197 public final String table;
1198 public final String where;
1199 public final String[] args;
1200
1201 SqlArguments(Uri url, String where, String[] args) {
1202 if (url.getPathSegments().size() == 1) {
1203 this.table = url.getPathSegments().get(0);
1204 this.where = where;
1205 this.args = args;
1206 } else if (url.getPathSegments().size() != 2) {
1207 throw new IllegalArgumentException("Invalid URI: " + url);
1208 } else if (!TextUtils.isEmpty(where)) {
1209 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
1210 } else {
1211 this.table = url.getPathSegments().get(0);
Daniel Lehmannc3a80402012-04-23 21:35:11 -07001212 this.where = "_id=" + ContentUris.parseId(url);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001213 this.args = null;
1214 }
1215 }
1216
1217 SqlArguments(Uri url) {
1218 if (url.getPathSegments().size() == 1) {
1219 table = url.getPathSegments().get(0);
1220 where = null;
1221 args = null;
1222 } else {
1223 throw new IllegalArgumentException("Invalid URI: " + url);
1224 }
1225 }
1226 }
Sunny Goyalb5b55c82016-05-10 12:28:59 -07001227
1228 private static class ChangeListenerWrapper implements Handler.Callback {
1229
1230 private static final int MSG_LAUNCHER_PROVIDER_CHANGED = 1;
1231 private static final int MSG_EXTRACTED_COLORS_CHANGED = 2;
1232 private static final int MSG_APP_WIDGET_HOST_RESET = 3;
1233
1234 private LauncherProviderChangeListener mListener;
1235
1236 @Override
1237 public boolean handleMessage(Message msg) {
1238 if (mListener != null) {
1239 switch (msg.what) {
1240 case MSG_LAUNCHER_PROVIDER_CHANGED:
Sunny Goyal2e013ea2016-10-07 16:17:19 -07001241 mListener.onLauncherProviderChanged();
Sunny Goyalb5b55c82016-05-10 12:28:59 -07001242 break;
1243 case MSG_EXTRACTED_COLORS_CHANGED:
1244 mListener.onExtractedColorsChanged();
1245 break;
1246 case MSG_APP_WIDGET_HOST_RESET:
Sunny Goyal2e013ea2016-10-07 16:17:19 -07001247 mListener.onAppWidgetHostReset();
Sunny Goyalb5b55c82016-05-10 12:28:59 -07001248 break;
1249 }
1250 }
1251 return true;
1252 }
1253 }
Adam Cohen72960972014-01-15 18:13:55 -08001254}