2009-03-03 19:32:27 -08:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2013-06-05 22:57:57 -04:00
|
|
|
package com.android.launcher3;
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2015-03-26 11:32:11 -07:00
|
|
|
import android.annotation.TargetApi;
|
2009-03-11 12:11:58 -07:00
|
|
|
import android.appwidget.AppWidgetHost;
|
2009-10-30 16:36:56 -07:00
|
|
|
import android.appwidget.AppWidgetManager;
|
2011-07-27 22:23:47 -07:00
|
|
|
import android.content.ComponentName;
|
2009-03-03 19:32:27 -08:00
|
|
|
import android.content.ContentProvider;
|
2014-02-11 15:15:29 +00:00
|
|
|
import android.content.ContentProviderOperation;
|
|
|
|
|
import android.content.ContentProviderResult;
|
2011-07-27 22:23:47 -07:00
|
|
|
import android.content.ContentResolver;
|
|
|
|
|
import android.content.ContentUris;
|
2009-03-03 19:32:27 -08:00
|
|
|
import android.content.ContentValues;
|
2011-07-27 22:23:47 -07:00
|
|
|
import android.content.Context;
|
2009-03-03 19:32:27 -08:00
|
|
|
import android.content.Intent;
|
2014-02-11 15:15:29 +00:00
|
|
|
import android.content.OperationApplicationException;
|
2012-04-25 15:48:32 -07:00
|
|
|
import android.content.SharedPreferences;
|
2015-03-26 11:32:11 -07:00
|
|
|
import android.content.pm.PackageManager.NameNotFoundException;
|
2009-10-30 16:36:56 -07:00
|
|
|
import android.content.res.Resources;
|
2009-03-03 19:32:27 -08:00
|
|
|
import android.database.Cursor;
|
|
|
|
|
import android.database.SQLException;
|
2011-07-27 22:23:47 -07:00
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
|
import android.database.sqlite.SQLiteOpenHelper;
|
|
|
|
|
import android.database.sqlite.SQLiteQueryBuilder;
|
2015-04-02 10:27:03 -07:00
|
|
|
import android.database.sqlite.SQLiteStatement;
|
2009-03-03 19:32:27 -08:00
|
|
|
import android.net.Uri;
|
2015-06-11 16:18:39 -07:00
|
|
|
import android.os.Binder;
|
2015-03-26 11:32:11 -07:00
|
|
|
import android.os.Build;
|
|
|
|
|
import android.os.Bundle;
|
2015-06-11 16:18:39 -07:00
|
|
|
import android.os.Process;
|
2015-01-21 11:50:57 -08:00
|
|
|
import android.os.StrictMode;
|
2015-03-26 11:32:11 -07:00
|
|
|
import android.os.UserManager;
|
2011-07-27 22:23:47 -07:00
|
|
|
import android.text.TextUtils;
|
|
|
|
|
import android.util.Log;
|
2014-03-06 23:48:04 -05:00
|
|
|
import android.util.SparseArray;
|
2010-02-24 20:01:46 -08:00
|
|
|
|
2014-08-06 09:55:36 -07:00
|
|
|
import com.android.launcher3.AutoInstallsLayout.LayoutParserCallback;
|
|
|
|
|
import com.android.launcher3.LauncherSettings.Favorites;
|
2014-04-30 03:02:21 +01:00
|
|
|
import com.android.launcher3.compat.UserHandleCompat;
|
|
|
|
|
import com.android.launcher3.compat.UserManagerCompat;
|
2013-10-09 10:36:55 -04:00
|
|
|
import com.android.launcher3.config.ProviderConfig;
|
2015-05-12 10:39:59 -07:00
|
|
|
import com.android.launcher3.util.ManagedProfileHeuristic;
|
2015-03-18 14:16:05 -07:00
|
|
|
import com.android.launcher3.util.Thunk;
|
2012-04-18 14:23:14 -07:00
|
|
|
|
2014-01-09 15:01:33 -05:00
|
|
|
import java.io.File;
|
2011-07-27 22:23:47 -07:00
|
|
|
import java.net.URISyntaxException;
|
|
|
|
|
import java.util.ArrayList;
|
2014-05-15 14:04:01 -07:00
|
|
|
import java.util.Collections;
|
2014-01-09 15:01:33 -05:00
|
|
|
import java.util.HashSet;
|
2015-06-10 16:00:42 -07:00
|
|
|
import java.util.List;
|
2010-03-04 13:03:17 -08:00
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
public class LauncherProvider extends ContentProvider {
|
2015-09-08 14:01:03 -07:00
|
|
|
private static final String TAG = "LauncherProvider";
|
2009-11-11 08:16:49 -08:00
|
|
|
private static final boolean LOGD = false;
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2015-05-19 16:03:28 -07:00
|
|
|
private static final int DATABASE_VERSION = 26;
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2015-07-27 14:36:07 -07:00
|
|
|
public static final String AUTHORITY = ProviderConfig.AUTHORITY;
|
2011-07-13 17:25:49 -07:00
|
|
|
|
2015-04-17 09:24:01 -07:00
|
|
|
static final String TABLE_FAVORITES = LauncherSettings.Favorites.TABLE_NAME;
|
|
|
|
|
static final String TABLE_WORKSPACE_SCREENS = LauncherSettings.WorkspaceScreens.TABLE_NAME;
|
2014-11-21 22:42:53 -08:00
|
|
|
static final String EMPTY_DATABASE_CREATED = "EMPTY_DATABASE_CREATED";
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2015-03-26 11:32:11 -07:00
|
|
|
private static final String RESTRICTION_PACKAGE_NAME = "workspace.configuration.package.name";
|
|
|
|
|
|
2015-06-12 21:18:53 -07:00
|
|
|
@Thunk LauncherProviderChangeListener mListener;
|
2015-06-02 09:38:28 -07:00
|
|
|
@Thunk DatabaseHelper mOpenHelper;
|
2009-03-03 19:32:27 -08:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onCreate() {
|
2013-06-25 15:13:26 -04:00
|
|
|
final Context context = getContext();
|
2015-01-26 14:07:29 -08:00
|
|
|
StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
|
2013-06-25 15:13:26 -04:00
|
|
|
mOpenHelper = new DatabaseHelper(context);
|
2015-01-21 11:50:57 -08:00
|
|
|
StrictMode.setThreadPolicy(oldPolicy);
|
2013-06-25 15:13:26 -04:00
|
|
|
LauncherAppState.setLauncherProvider(this);
|
2009-03-03 19:32:27 -08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-21 13:00:26 -08:00
|
|
|
public boolean wasNewDbCreated() {
|
|
|
|
|
return mOpenHelper.wasNewDbCreated();
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-13 12:14:12 -07:00
|
|
|
public void setLauncherProviderChangeListener(LauncherProviderChangeListener listener) {
|
|
|
|
|
mListener = listener;
|
2015-06-16 15:10:36 -07:00
|
|
|
mOpenHelper.mListener = mListener;
|
2014-03-13 12:14:12 -07:00
|
|
|
}
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
@Override
|
|
|
|
|
public String getType(Uri uri) {
|
|
|
|
|
SqlArguments args = new SqlArguments(uri, null, null);
|
|
|
|
|
if (TextUtils.isEmpty(args.where)) {
|
|
|
|
|
return "vnd.android.cursor.dir/" + args.table;
|
|
|
|
|
} else {
|
|
|
|
|
return "vnd.android.cursor.item/" + args.table;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Cursor query(Uri uri, String[] projection, String selection,
|
|
|
|
|
String[] selectionArgs, String sortOrder) {
|
|
|
|
|
|
|
|
|
|
SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
|
|
|
|
|
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
|
|
|
|
|
qb.setTables(args.table);
|
|
|
|
|
|
2009-06-09 12:57:21 -07:00
|
|
|
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
|
2009-03-03 19:32:27 -08:00
|
|
|
Cursor result = qb.query(db, projection, args.where, args.args, null, null, sortOrder);
|
|
|
|
|
result.setNotificationUri(getContext().getContentResolver(), uri);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-18 14:16:05 -07:00
|
|
|
@Thunk static long dbInsertAndCheck(DatabaseHelper helper,
|
2011-04-28 14:59:33 -07:00
|
|
|
SQLiteDatabase db, String table, String nullColumnHack, ContentValues values) {
|
2014-01-09 15:01:33 -05:00
|
|
|
if (values == null) {
|
|
|
|
|
throw new RuntimeException("Error: attempting to insert null values");
|
|
|
|
|
}
|
2014-05-15 14:04:01 -07:00
|
|
|
if (!values.containsKey(LauncherSettings.ChangeLogColumns._ID)) {
|
2011-04-28 14:59:33 -07:00
|
|
|
throw new RuntimeException("Error: attempting to add item without specifying an id");
|
|
|
|
|
}
|
2013-12-20 17:22:11 -05:00
|
|
|
helper.checkId(table, values);
|
2011-04-28 14:59:33 -07:00
|
|
|
return db.insert(table, nullColumnHack, values);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-13 12:08:30 -07:00
|
|
|
private void reloadLauncherIfExternal() {
|
2015-09-08 14:01:03 -07:00
|
|
|
if (Utilities.ATLEAST_MARSHMALLOW && Binder.getCallingPid() != Process.myPid()) {
|
2015-08-13 12:08:30 -07:00
|
|
|
LauncherAppState app = LauncherAppState.getInstanceNoCreate();
|
|
|
|
|
if (app != null) {
|
|
|
|
|
app.reloadWorkspace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
@Override
|
|
|
|
|
public Uri insert(Uri uri, ContentValues initialValues) {
|
|
|
|
|
SqlArguments args = new SqlArguments(uri);
|
|
|
|
|
|
2015-08-13 12:08:30 -07:00
|
|
|
// In very limited cases, we support system|signature permission apps to modify the db.
|
|
|
|
|
if (Binder.getCallingPid() != Process.myPid()) {
|
2014-07-23 14:49:38 -07:00
|
|
|
if (!mOpenHelper.initializeExternalAdd(initialValues)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
|
2013-09-13 18:01:38 -04:00
|
|
|
addModifiedTime(initialValues);
|
2011-04-28 14:59:33 -07:00
|
|
|
final long rowId = dbInsertAndCheck(mOpenHelper, db, args.table, null, initialValues);
|
2015-03-02 14:24:21 -08:00
|
|
|
if (rowId < 0) return null;
|
2009-03-03 19:32:27 -08:00
|
|
|
|
|
|
|
|
uri = ContentUris.withAppendedId(uri, rowId);
|
2015-03-30 11:11:46 -07:00
|
|
|
notifyListeners();
|
|
|
|
|
|
2015-09-08 14:01:03 -07:00
|
|
|
if (Utilities.ATLEAST_MARSHMALLOW) {
|
|
|
|
|
reloadLauncherIfExternal();
|
|
|
|
|
} else {
|
|
|
|
|
// Deprecated behavior to support legacy devices which rely on provider callbacks.
|
|
|
|
|
LauncherAppState app = LauncherAppState.getInstanceNoCreate();
|
|
|
|
|
if (app != null && "true".equals(uri.getQueryParameter("isExternalAdd"))) {
|
|
|
|
|
app.reloadWorkspace();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String notify = uri.getQueryParameter("notify");
|
|
|
|
|
if (notify == null || "true".equals(notify)) {
|
|
|
|
|
getContext().getContentResolver().notifyChange(uri, null);
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-03-03 19:32:27 -08:00
|
|
|
return uri;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-23 14:49:38 -07:00
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
@Override
|
|
|
|
|
public int bulkInsert(Uri uri, ContentValues[] values) {
|
|
|
|
|
SqlArguments args = new SqlArguments(uri);
|
|
|
|
|
|
|
|
|
|
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
|
|
|
|
|
db.beginTransaction();
|
|
|
|
|
try {
|
|
|
|
|
int numValues = values.length;
|
|
|
|
|
for (int i = 0; i < numValues; i++) {
|
2013-09-13 18:01:38 -04:00
|
|
|
addModifiedTime(values[i]);
|
2011-04-28 14:59:33 -07:00
|
|
|
if (dbInsertAndCheck(mOpenHelper, db, args.table, null, values[i]) < 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
|
} finally {
|
|
|
|
|
db.endTransaction();
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-30 11:11:46 -07:00
|
|
|
notifyListeners();
|
2015-08-13 12:08:30 -07:00
|
|
|
reloadLauncherIfExternal();
|
2009-03-03 19:32:27 -08:00
|
|
|
return values.length;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-11 15:15:29 +00:00
|
|
|
@Override
|
|
|
|
|
public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
|
|
|
|
|
throws OperationApplicationException {
|
|
|
|
|
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
|
|
|
|
|
db.beginTransaction();
|
|
|
|
|
try {
|
|
|
|
|
ContentProviderResult[] result = super.applyBatch(operations);
|
|
|
|
|
db.setTransactionSuccessful();
|
2015-08-13 12:08:30 -07:00
|
|
|
reloadLauncherIfExternal();
|
2014-02-11 15:15:29 +00:00
|
|
|
return result;
|
|
|
|
|
} finally {
|
|
|
|
|
db.endTransaction();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
@Override
|
|
|
|
|
public int delete(Uri uri, String selection, String[] selectionArgs) {
|
|
|
|
|
SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
|
|
|
|
|
|
|
|
|
|
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
|
|
|
|
|
int count = db.delete(args.table, args.where, args.args);
|
2015-03-30 11:11:46 -07:00
|
|
|
if (count > 0) notifyListeners();
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2015-08-13 12:08:30 -07:00
|
|
|
reloadLauncherIfExternal();
|
2009-03-03 19:32:27 -08:00
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
|
|
|
|
|
SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
|
|
|
|
|
|
2013-09-13 18:01:38 -04:00
|
|
|
addModifiedTime(values);
|
2009-03-03 19:32:27 -08:00
|
|
|
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
|
|
|
|
|
int count = db.update(args.table, values, args.where, args.args);
|
2015-03-30 11:11:46 -07:00
|
|
|
if (count > 0) notifyListeners();
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2015-08-13 12:08:30 -07:00
|
|
|
reloadLauncherIfExternal();
|
2009-03-03 19:32:27 -08:00
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-11 16:18:39 -07:00
|
|
|
@Override
|
|
|
|
|
public Bundle call(String method, String arg, Bundle extras) {
|
|
|
|
|
if (Binder.getCallingUid() != Process.myUid()) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (method) {
|
|
|
|
|
case LauncherSettings.Settings.METHOD_GET_BOOLEAN: {
|
|
|
|
|
Bundle result = new Bundle();
|
|
|
|
|
result.putBoolean(LauncherSettings.Settings.EXTRA_VALUE,
|
|
|
|
|
getContext().getSharedPreferences(
|
|
|
|
|
LauncherAppState.getSharedPreferencesKey(), Context.MODE_PRIVATE)
|
|
|
|
|
.getBoolean(arg, extras.getBoolean(
|
|
|
|
|
LauncherSettings.Settings.EXTRA_DEFAULT_VALUE)));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
case LauncherSettings.Settings.METHOD_SET_BOOLEAN: {
|
|
|
|
|
boolean value = extras.getBoolean(LauncherSettings.Settings.EXTRA_VALUE);
|
|
|
|
|
getContext().getSharedPreferences(
|
|
|
|
|
LauncherAppState.getSharedPreferencesKey(), Context.MODE_PRIVATE)
|
|
|
|
|
.edit().putBoolean(arg, value).apply();
|
|
|
|
|
if (mListener != null) {
|
|
|
|
|
mListener.onSettingsChanged(arg, value);
|
|
|
|
|
}
|
|
|
|
|
Bundle result = new Bundle();
|
|
|
|
|
result.putBoolean(LauncherSettings.Settings.EXTRA_VALUE, value);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-10 16:00:42 -07:00
|
|
|
/**
|
|
|
|
|
* Deletes any empty folder from the DB.
|
|
|
|
|
* @return Ids of deleted folders.
|
|
|
|
|
*/
|
|
|
|
|
public List<Long> deleteEmptyFolders() {
|
|
|
|
|
ArrayList<Long> folderIds = new ArrayList<Long>();
|
|
|
|
|
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
|
|
|
|
|
db.beginTransaction();
|
|
|
|
|
try {
|
|
|
|
|
// Select folders whose id do not match any container value.
|
|
|
|
|
String selection = LauncherSettings.Favorites.ITEM_TYPE + " = "
|
|
|
|
|
+ LauncherSettings.Favorites.ITEM_TYPE_FOLDER + " AND "
|
|
|
|
|
+ LauncherSettings.Favorites._ID + " NOT IN (SELECT " +
|
|
|
|
|
LauncherSettings.Favorites.CONTAINER + " FROM "
|
|
|
|
|
+ TABLE_FAVORITES + ")";
|
|
|
|
|
Cursor c = db.query(TABLE_FAVORITES,
|
|
|
|
|
new String[] {LauncherSettings.Favorites._ID},
|
|
|
|
|
selection, null, null, null, null);
|
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
|
folderIds.add(c.getLong(0));
|
|
|
|
|
}
|
|
|
|
|
c.close();
|
|
|
|
|
if (folderIds.size() > 0) {
|
|
|
|
|
db.delete(TABLE_FAVORITES, Utilities.createDbSelectionQuery(
|
|
|
|
|
LauncherSettings.Favorites._ID, folderIds), null);
|
|
|
|
|
}
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
|
} catch (SQLException ex) {
|
|
|
|
|
Log.e(TAG, ex.getMessage(), ex);
|
|
|
|
|
folderIds.clear();
|
|
|
|
|
} finally {
|
|
|
|
|
db.endTransaction();
|
|
|
|
|
}
|
|
|
|
|
return folderIds;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-30 11:11:46 -07:00
|
|
|
private void notifyListeners() {
|
2013-09-13 18:01:38 -04:00
|
|
|
// always notify the backup agent
|
2013-10-04 11:29:36 -04:00
|
|
|
LauncherBackupAgentHelper.dataChanged(getContext());
|
2014-03-13 12:14:12 -07:00
|
|
|
if (mListener != null) {
|
|
|
|
|
mListener.onLauncherProviderChange();
|
|
|
|
|
}
|
2013-09-13 18:01:38 -04:00
|
|
|
}
|
|
|
|
|
|
2015-03-18 14:16:05 -07:00
|
|
|
@Thunk static void addModifiedTime(ContentValues values) {
|
2013-09-13 18:01:38 -04:00
|
|
|
values.put(LauncherSettings.ChangeLogColumns.MODIFIED, System.currentTimeMillis());
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
|
2013-06-18 13:13:40 -07:00
|
|
|
public long generateNewItemId() {
|
|
|
|
|
return mOpenHelper.generateNewItemId();
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-19 13:49:06 -07:00
|
|
|
public void updateMaxItemId(long id) {
|
|
|
|
|
mOpenHelper.updateMaxItemId(id);
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-18 13:13:40 -07:00
|
|
|
public long generateNewScreenId() {
|
|
|
|
|
return mOpenHelper.generateNewScreenId();
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-26 22:09:29 -07:00
|
|
|
/**
|
|
|
|
|
* Clears all the data for a fresh start.
|
|
|
|
|
*/
|
|
|
|
|
synchronized public void createEmptyDB() {
|
|
|
|
|
mOpenHelper.createEmptyDB(mOpenHelper.getWritableDatabase());
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-16 09:24:19 -07:00
|
|
|
public void clearFlagEmptyDbCreated() {
|
|
|
|
|
String spKey = LauncherAppState.getSharedPreferencesKey();
|
|
|
|
|
getContext().getSharedPreferences(spKey, Context.MODE_PRIVATE)
|
|
|
|
|
.edit()
|
|
|
|
|
.remove(EMPTY_DATABASE_CREATED)
|
|
|
|
|
.commit();
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-02 16:55:54 -07:00
|
|
|
/**
|
2014-08-06 09:55:36 -07:00
|
|
|
* Loads the default workspace based on the following priority scheme:
|
2015-03-26 11:32:11 -07:00
|
|
|
* 1) From the app restrictions
|
|
|
|
|
* 2) From a package provided by play store
|
|
|
|
|
* 3) From a partner configuration APK, already in the system image
|
|
|
|
|
* 4) The default configuration for the particular device
|
2012-10-02 16:55:54 -07:00
|
|
|
*/
|
2014-08-06 09:55:36 -07:00
|
|
|
synchronized public void loadDefaultFavoritesIfNecessary() {
|
2013-06-11 14:45:48 -04:00
|
|
|
String spKey = LauncherAppState.getSharedPreferencesKey();
|
2012-04-25 15:48:32 -07:00
|
|
|
SharedPreferences sp = getContext().getSharedPreferences(spKey, Context.MODE_PRIVATE);
|
2013-06-06 23:08:25 -07:00
|
|
|
|
2013-07-19 13:49:06 -07:00
|
|
|
if (sp.getBoolean(EMPTY_DATABASE_CREATED, false)) {
|
2013-12-20 17:22:11 -05:00
|
|
|
Log.d(TAG, "loading default workspace");
|
2012-10-08 13:21:35 +02:00
|
|
|
|
2015-03-26 11:32:11 -07:00
|
|
|
AutoInstallsLayout loader = createWorkspaceLoaderFromAppRestriction();
|
|
|
|
|
if (loader == null) {
|
|
|
|
|
loader = AutoInstallsLayout.get(getContext(),
|
|
|
|
|
mOpenHelper.mAppWidgetHost, mOpenHelper);
|
|
|
|
|
}
|
2014-08-06 09:55:36 -07:00
|
|
|
if (loader == null) {
|
2014-05-30 15:34:09 -07:00
|
|
|
final Partner partner = Partner.get(getContext().getPackageManager());
|
|
|
|
|
if (partner != null && partner.hasDefaultLayout()) {
|
|
|
|
|
final Resources partnerRes = partner.getResources();
|
2014-08-29 15:05:48 -07:00
|
|
|
int workspaceResId = partnerRes.getIdentifier(Partner.RES_DEFAULT_LAYOUT,
|
2014-05-30 15:34:09 -07:00
|
|
|
"xml", partner.getPackageName());
|
2014-08-06 09:55:36 -07:00
|
|
|
if (workspaceResId != 0) {
|
2014-10-01 15:33:41 -07:00
|
|
|
loader = new DefaultLayoutParser(getContext(), mOpenHelper.mAppWidgetHost,
|
|
|
|
|
mOpenHelper, partnerRes, workspaceResId);
|
2014-08-06 09:55:36 -07:00
|
|
|
}
|
2014-05-30 15:34:09 -07:00
|
|
|
}
|
|
|
|
|
}
|
2012-10-02 16:55:54 -07:00
|
|
|
|
2014-10-23 14:21:02 -07:00
|
|
|
final boolean usingExternallyProvidedLayout = loader != null;
|
2014-08-06 09:55:36 -07:00
|
|
|
if (loader == null) {
|
2014-10-23 14:21:02 -07:00
|
|
|
loader = getDefaultLayoutParser();
|
2012-10-08 13:21:35 +02:00
|
|
|
}
|
2015-03-04 09:51:18 -08:00
|
|
|
|
|
|
|
|
// There might be some partially restored DB items, due to buggy restore logic in
|
|
|
|
|
// previous versions of launcher.
|
|
|
|
|
createEmptyDB();
|
2014-08-06 09:55:36 -07:00
|
|
|
// Populate favorites table with initial favorites
|
2014-10-23 14:21:02 -07:00
|
|
|
if ((mOpenHelper.loadFavorites(mOpenHelper.getWritableDatabase(), loader) <= 0)
|
|
|
|
|
&& usingExternallyProvidedLayout) {
|
|
|
|
|
// Unable to load external layout. Cleanup and load the internal layout.
|
|
|
|
|
createEmptyDB();
|
|
|
|
|
mOpenHelper.loadFavorites(mOpenHelper.getWritableDatabase(),
|
|
|
|
|
getDefaultLayoutParser());
|
|
|
|
|
}
|
2014-10-16 09:24:19 -07:00
|
|
|
clearFlagEmptyDbCreated();
|
2012-04-25 15:48:32 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-26 11:32:11 -07:00
|
|
|
/**
|
|
|
|
|
* Creates workspace loader from an XML resource listed in the app restrictions.
|
|
|
|
|
*
|
|
|
|
|
* @return the loader if the restrictions are set and the resource exists; null otherwise.
|
|
|
|
|
*/
|
|
|
|
|
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
|
|
|
|
|
private AutoInstallsLayout createWorkspaceLoaderFromAppRestriction() {
|
|
|
|
|
// UserManager.getApplicationRestrictions() requires minSdkVersion >= 18
|
2015-08-17 12:24:25 -07:00
|
|
|
if (!Utilities.ATLEAST_JB_MR2) {
|
2015-03-26 11:32:11 -07:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Context ctx = getContext();
|
|
|
|
|
UserManager um = (UserManager) ctx.getSystemService(Context.USER_SERVICE);
|
|
|
|
|
Bundle bundle = um.getApplicationRestrictions(ctx.getPackageName());
|
2015-04-06 10:45:31 -07:00
|
|
|
if (bundle == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2015-03-26 11:32:11 -07:00
|
|
|
|
2015-04-06 10:45:31 -07:00
|
|
|
String packageName = bundle.getString(RESTRICTION_PACKAGE_NAME);
|
2015-03-26 11:32:11 -07:00
|
|
|
if (packageName != null) {
|
|
|
|
|
try {
|
|
|
|
|
Resources targetResources = ctx.getPackageManager()
|
|
|
|
|
.getResourcesForApplication(packageName);
|
|
|
|
|
return AutoInstallsLayout.get(ctx, packageName, targetResources,
|
|
|
|
|
mOpenHelper.mAppWidgetHost, mOpenHelper);
|
|
|
|
|
} catch (NameNotFoundException e) {
|
|
|
|
|
Log.e(TAG, "Target package for restricted profile not found", e);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-23 14:21:02 -07:00
|
|
|
private DefaultLayoutParser getDefaultLayoutParser() {
|
|
|
|
|
int defaultLayout = LauncherAppState.getInstance()
|
2015-05-06 11:42:25 -07:00
|
|
|
.getInvariantDeviceProfile().defaultLayoutId;
|
2014-10-23 14:21:02 -07:00
|
|
|
return new DefaultLayoutParser(getContext(), mOpenHelper.mAppWidgetHost,
|
|
|
|
|
mOpenHelper, getContext().getResources(), defaultLayout);
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-09 15:01:33 -05:00
|
|
|
public void migrateLauncher2Shortcuts() {
|
|
|
|
|
mOpenHelper.migrateLauncher2Shortcuts(mOpenHelper.getWritableDatabase(),
|
2014-03-21 15:42:06 -04:00
|
|
|
Uri.parse(getContext().getString(R.string.old_launcher_provider_uri)));
|
2014-01-09 15:01:33 -05:00
|
|
|
}
|
|
|
|
|
|
2015-01-05 13:41:43 -08:00
|
|
|
public void updateFolderItemsRank() {
|
|
|
|
|
mOpenHelper.updateFolderItemsRank(mOpenHelper.getWritableDatabase(), false);
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-19 16:03:28 -07:00
|
|
|
public void convertShortcutsToLauncherActivities() {
|
|
|
|
|
mOpenHelper.convertShortcutsToLauncherActivities(mOpenHelper.getWritableDatabase());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-01-09 15:01:33 -05:00
|
|
|
public void deleteDatabase() {
|
|
|
|
|
// Are you sure? (y/n)
|
|
|
|
|
final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
|
2014-01-21 14:14:41 -05:00
|
|
|
final File dbFile = new File(db.getPath());
|
2014-01-09 15:01:33 -05:00
|
|
|
mOpenHelper.close();
|
2014-01-21 14:14:41 -05:00
|
|
|
if (dbFile.exists()) {
|
|
|
|
|
SQLiteDatabase.deleteDatabase(dbFile);
|
|
|
|
|
}
|
2014-01-09 15:01:33 -05:00
|
|
|
mOpenHelper = new DatabaseHelper(getContext());
|
2015-06-16 15:10:36 -07:00
|
|
|
mOpenHelper.mListener = mListener;
|
2014-01-09 15:01:33 -05:00
|
|
|
}
|
|
|
|
|
|
2014-08-06 09:55:36 -07:00
|
|
|
private static class DatabaseHelper extends SQLiteOpenHelper implements LayoutParserCallback {
|
2009-03-03 19:32:27 -08:00
|
|
|
private final Context mContext;
|
2015-03-18 14:16:05 -07:00
|
|
|
@Thunk final AppWidgetHost mAppWidgetHost;
|
2013-06-18 13:13:40 -07:00
|
|
|
private long mMaxItemId = -1;
|
|
|
|
|
private long mMaxScreenId = -1;
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2014-01-21 13:00:26 -08:00
|
|
|
private boolean mNewDbCreated = false;
|
|
|
|
|
|
2015-06-16 15:10:36 -07:00
|
|
|
@Thunk LauncherProviderChangeListener mListener;
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
DatabaseHelper(Context context) {
|
2014-10-06 16:06:46 +01:00
|
|
|
super(context, LauncherFiles.LAUNCHER_DB, null, DATABASE_VERSION);
|
2009-03-03 19:32:27 -08:00
|
|
|
mContext = context;
|
2009-03-11 12:11:58 -07:00
|
|
|
mAppWidgetHost = new AppWidgetHost(context, Launcher.APPWIDGET_HOST_ID);
|
2011-07-13 17:25:49 -07:00
|
|
|
|
|
|
|
|
// In the case where neither onCreate nor onUpgrade gets called, we read the maxId from
|
|
|
|
|
// the DB here
|
2013-06-18 13:13:40 -07:00
|
|
|
if (mMaxItemId == -1) {
|
|
|
|
|
mMaxItemId = initializeMaxItemId(getWritableDatabase());
|
|
|
|
|
}
|
|
|
|
|
if (mMaxScreenId == -1) {
|
|
|
|
|
mMaxScreenId = initializeMaxScreenId(getWritableDatabase());
|
2011-07-13 17:25:49 -07:00
|
|
|
}
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
|
2014-01-21 13:00:26 -08:00
|
|
|
public boolean wasNewDbCreated() {
|
|
|
|
|
return mNewDbCreated;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
@Override
|
|
|
|
|
public void onCreate(SQLiteDatabase db) {
|
2009-11-11 08:16:49 -08:00
|
|
|
if (LOGD) Log.d(TAG, "creating new launcher database");
|
2011-04-28 14:59:33 -07:00
|
|
|
|
2013-06-18 13:13:40 -07:00
|
|
|
mMaxItemId = 1;
|
|
|
|
|
mMaxScreenId = 0;
|
2014-01-21 13:00:26 -08:00
|
|
|
mNewDbCreated = true;
|
2011-04-28 14:59:33 -07:00
|
|
|
|
2014-04-30 03:02:21 +01:00
|
|
|
UserManagerCompat userManager = UserManagerCompat.getInstance(mContext);
|
|
|
|
|
long userSerialNumber = userManager.getSerialNumberForUser(
|
|
|
|
|
UserHandleCompat.myUserHandle());
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
db.execSQL("CREATE TABLE favorites (" +
|
|
|
|
|
"_id INTEGER PRIMARY KEY," +
|
|
|
|
|
"title TEXT," +
|
|
|
|
|
"intent TEXT," +
|
|
|
|
|
"container INTEGER," +
|
|
|
|
|
"screen INTEGER," +
|
|
|
|
|
"cellX INTEGER," +
|
|
|
|
|
"cellY INTEGER," +
|
|
|
|
|
"spanX INTEGER," +
|
|
|
|
|
"spanY INTEGER," +
|
|
|
|
|
"itemType INTEGER," +
|
2009-03-13 13:04:24 -07:00
|
|
|
"appWidgetId INTEGER NOT NULL DEFAULT -1," +
|
2009-03-03 19:32:27 -08:00
|
|
|
"isShortcut INTEGER," +
|
|
|
|
|
"iconType INTEGER," +
|
|
|
|
|
"iconPackage TEXT," +
|
|
|
|
|
"iconResource TEXT," +
|
|
|
|
|
"icon BLOB," +
|
|
|
|
|
"uri TEXT," +
|
2013-09-16 14:02:29 -04:00
|
|
|
"displayMode INTEGER," +
|
2013-09-13 18:01:38 -04:00
|
|
|
"appWidgetProvider TEXT," +
|
2014-01-16 18:13:56 -05:00
|
|
|
"modified INTEGER NOT NULL DEFAULT 0," +
|
2014-04-30 03:02:21 +01:00
|
|
|
"restored INTEGER NOT NULL DEFAULT 0," +
|
2015-01-05 13:41:43 -08:00
|
|
|
"profileId INTEGER DEFAULT " + userSerialNumber + "," +
|
2015-03-10 13:14:47 -07:00
|
|
|
"rank INTEGER NOT NULL DEFAULT 0," +
|
|
|
|
|
"options INTEGER NOT NULL DEFAULT 0" +
|
2009-03-03 19:32:27 -08:00
|
|
|
");");
|
2013-06-18 13:13:40 -07:00
|
|
|
addWorkspacesTable(db);
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2009-03-11 12:11:58 -07:00
|
|
|
// Database was just created, so wipe any previous widgets
|
|
|
|
|
if (mAppWidgetHost != null) {
|
|
|
|
|
mAppWidgetHost.deleteHost();
|
2015-06-12 21:18:53 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Send notification that we've deleted the {@link AppWidgetHost},
|
|
|
|
|
* probably as part of the initial database creation. The receiver may
|
|
|
|
|
* want to re-call {@link AppWidgetHost#startListening()} to ensure
|
|
|
|
|
* callbacks are correctly set.
|
|
|
|
|
*/
|
2015-06-16 15:10:36 -07:00
|
|
|
new MainThreadExecutor().execute(new Runnable() {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
if (mListener != null) {
|
|
|
|
|
mListener.onAppWidgetHostReset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
2011-07-13 17:25:49 -07:00
|
|
|
|
2014-11-21 22:42:53 -08:00
|
|
|
// Fresh and clean launcher DB.
|
|
|
|
|
mMaxItemId = initializeMaxItemId(db);
|
|
|
|
|
setFlagEmptyDbCreated();
|
2015-05-12 10:39:59 -07:00
|
|
|
|
|
|
|
|
// When a new DB is created, remove all previously stored managed profile information.
|
2015-06-12 21:18:53 -07:00
|
|
|
ManagedProfileHeuristic.processAllUsers(Collections.<UserHandleCompat>emptyList(), mContext);
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
|
2013-06-18 13:13:40 -07:00
|
|
|
private void addWorkspacesTable(SQLiteDatabase db) {
|
|
|
|
|
db.execSQL("CREATE TABLE " + TABLE_WORKSPACE_SCREENS + " (" +
|
2015-02-25 10:46:34 -08:00
|
|
|
LauncherSettings.WorkspaceScreens._ID + " INTEGER PRIMARY KEY," +
|
2013-09-13 18:01:38 -04:00
|
|
|
LauncherSettings.WorkspaceScreens.SCREEN_RANK + " INTEGER," +
|
|
|
|
|
LauncherSettings.ChangeLogColumns.MODIFIED + " INTEGER NOT NULL DEFAULT 0" +
|
2013-06-18 13:13:40 -07:00
|
|
|
");");
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-02 16:59:08 -07:00
|
|
|
private void removeOrphanedItems(SQLiteDatabase db) {
|
2014-04-17 18:20:45 -07:00
|
|
|
// Delete items directly on the workspace who's screen id doesn't exist
|
|
|
|
|
// "DELETE FROM favorites WHERE screen NOT IN (SELECT _id FROM workspaceScreens)
|
|
|
|
|
// AND container = -100"
|
|
|
|
|
String removeOrphanedDesktopItems = "DELETE FROM " + TABLE_FAVORITES +
|
|
|
|
|
" WHERE " +
|
2014-04-02 16:59:08 -07:00
|
|
|
LauncherSettings.Favorites.SCREEN + " NOT IN (SELECT " +
|
2014-04-17 18:20:45 -07:00
|
|
|
LauncherSettings.WorkspaceScreens._ID + " FROM " + TABLE_WORKSPACE_SCREENS + ")" +
|
|
|
|
|
" AND " +
|
|
|
|
|
LauncherSettings.Favorites.CONTAINER + " = " +
|
|
|
|
|
LauncherSettings.Favorites.CONTAINER_DESKTOP;
|
|
|
|
|
db.execSQL(removeOrphanedDesktopItems);
|
|
|
|
|
|
|
|
|
|
// Delete items contained in folders which no longer exist (after above statement)
|
|
|
|
|
// "DELETE FROM favorites WHERE container <> -100 AND container <> -101 AND container
|
|
|
|
|
// NOT IN (SELECT _id FROM favorites WHERE itemType = 2)"
|
|
|
|
|
String removeOrphanedFolderItems = "DELETE FROM " + TABLE_FAVORITES +
|
|
|
|
|
" WHERE " +
|
|
|
|
|
LauncherSettings.Favorites.CONTAINER + " <> " +
|
|
|
|
|
LauncherSettings.Favorites.CONTAINER_DESKTOP +
|
|
|
|
|
" AND "
|
|
|
|
|
+ LauncherSettings.Favorites.CONTAINER + " <> " +
|
|
|
|
|
LauncherSettings.Favorites.CONTAINER_HOTSEAT +
|
|
|
|
|
" AND "
|
|
|
|
|
+ LauncherSettings.Favorites.CONTAINER + " NOT IN (SELECT " +
|
|
|
|
|
LauncherSettings.Favorites._ID + " FROM " + TABLE_FAVORITES +
|
|
|
|
|
" WHERE " + LauncherSettings.Favorites.ITEM_TYPE + " = " +
|
|
|
|
|
LauncherSettings.Favorites.ITEM_TYPE_FOLDER + ")";
|
|
|
|
|
db.execSQL(removeOrphanedFolderItems);
|
2014-04-02 16:59:08 -07:00
|
|
|
}
|
|
|
|
|
|
2013-07-19 13:49:06 -07:00
|
|
|
private void setFlagJustLoadedOldDb() {
|
|
|
|
|
String spKey = LauncherAppState.getSharedPreferencesKey();
|
|
|
|
|
SharedPreferences sp = mContext.getSharedPreferences(spKey, Context.MODE_PRIVATE);
|
2015-02-02 13:01:51 -08:00
|
|
|
sp.edit().putBoolean(EMPTY_DATABASE_CREATED, false).commit();
|
2013-07-19 13:49:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setFlagEmptyDbCreated() {
|
2013-06-11 14:45:48 -04:00
|
|
|
String spKey = LauncherAppState.getSharedPreferencesKey();
|
2012-04-25 15:48:32 -07:00
|
|
|
SharedPreferences sp = mContext.getSharedPreferences(spKey, Context.MODE_PRIVATE);
|
2015-02-02 13:01:51 -08:00
|
|
|
sp.edit().putBoolean(EMPTY_DATABASE_CREATED, true).commit();
|
2012-04-25 15:48:32 -07:00
|
|
|
}
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
@Override
|
|
|
|
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
2013-07-19 13:49:06 -07:00
|
|
|
if (LOGD) Log.d(TAG, "onUpgrade triggered: " + oldVersion);
|
2015-01-14 14:23:02 -08:00
|
|
|
switch (oldVersion) {
|
|
|
|
|
// The version cannot be lower that 12, as Launcher3 never supported a lower
|
2014-11-21 22:42:53 -08:00
|
|
|
// version of the DB.
|
2015-01-14 14:23:02 -08:00
|
|
|
case 12: {
|
|
|
|
|
// With the new shrink-wrapped and re-orderable workspaces, it makes sense
|
|
|
|
|
// to persist workspace screens and their relative order.
|
|
|
|
|
mMaxScreenId = 0;
|
|
|
|
|
addWorkspacesTable(db);
|
2013-09-16 14:02:29 -04:00
|
|
|
}
|
2015-01-14 14:23:02 -08:00
|
|
|
case 13: {
|
|
|
|
|
db.beginTransaction();
|
|
|
|
|
try {
|
|
|
|
|
// Insert new column for holding widget provider name
|
|
|
|
|
db.execSQL("ALTER TABLE favorites " +
|
|
|
|
|
"ADD COLUMN appWidgetProvider TEXT;");
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
|
} catch (SQLException ex) {
|
|
|
|
|
Log.e(TAG, ex.getMessage(), ex);
|
|
|
|
|
// Old version remains, which means we wipe old data
|
|
|
|
|
break;
|
|
|
|
|
} finally {
|
|
|
|
|
db.endTransaction();
|
|
|
|
|
}
|
2013-09-13 18:01:38 -04:00
|
|
|
}
|
2015-01-14 14:23:02 -08:00
|
|
|
case 14: {
|
|
|
|
|
db.beginTransaction();
|
|
|
|
|
try {
|
|
|
|
|
// Insert new column for holding update timestamp
|
|
|
|
|
db.execSQL("ALTER TABLE favorites " +
|
|
|
|
|
"ADD COLUMN modified INTEGER NOT NULL DEFAULT 0;");
|
|
|
|
|
db.execSQL("ALTER TABLE workspaceScreens " +
|
|
|
|
|
"ADD COLUMN modified INTEGER NOT NULL DEFAULT 0;");
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
|
} catch (SQLException ex) {
|
|
|
|
|
Log.e(TAG, ex.getMessage(), ex);
|
|
|
|
|
// Old version remains, which means we wipe old data
|
|
|
|
|
break;
|
|
|
|
|
} finally {
|
|
|
|
|
db.endTransaction();
|
|
|
|
|
}
|
2014-01-16 18:13:56 -05:00
|
|
|
}
|
2015-01-14 14:23:02 -08:00
|
|
|
case 15: {
|
2015-03-10 13:14:47 -07:00
|
|
|
if (!addIntegerColumn(db, Favorites.RESTORED, 0)) {
|
2015-01-14 14:23:02 -08:00
|
|
|
// Old version remains, which means we wipe old data
|
|
|
|
|
break;
|
|
|
|
|
}
|
2014-04-30 03:02:21 +01:00
|
|
|
}
|
2015-01-14 14:23:02 -08:00
|
|
|
case 16: {
|
|
|
|
|
// We use the db version upgrade here to identify users who may not have seen
|
|
|
|
|
// clings yet (because they weren't available), but for whom the clings are now
|
|
|
|
|
// available (tablet users). Because one of the possible cling flows (migration)
|
|
|
|
|
// is very destructive (wipes out workspaces), we want to prevent this from showing
|
|
|
|
|
// until clear data. We do so by marking that the clings have been shown.
|
|
|
|
|
LauncherClings.synchonouslyMarkFirstRunClingDismissed(mContext);
|
|
|
|
|
}
|
|
|
|
|
case 17: {
|
|
|
|
|
// No-op
|
|
|
|
|
}
|
|
|
|
|
case 18: {
|
|
|
|
|
// Due to a data loss bug, some users may have items associated with screen ids
|
|
|
|
|
// which no longer exist. Since this can cause other problems, and since the user
|
|
|
|
|
// will never see these items anyway, we use database upgrade as an opportunity to
|
|
|
|
|
// clean things up.
|
|
|
|
|
removeOrphanedItems(db);
|
|
|
|
|
}
|
|
|
|
|
case 19: {
|
|
|
|
|
// Add userId column
|
|
|
|
|
if (!addProfileColumn(db)) {
|
|
|
|
|
// Old version remains, which means we wipe old data
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
case 20:
|
|
|
|
|
if (!updateFolderItemsRank(db, true)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2015-02-25 10:46:34 -08:00
|
|
|
case 21:
|
|
|
|
|
// Recreate workspace table with screen id a primary key
|
|
|
|
|
if (!recreateWorkspaceTable(db)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 22: {
|
2015-03-10 13:14:47 -07:00
|
|
|
if (!addIntegerColumn(db, Favorites.OPTIONS, 0)) {
|
|
|
|
|
// Old version remains, which means we wipe old data
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-04-02 10:27:03 -07:00
|
|
|
case 23:
|
2015-05-19 16:03:28 -07:00
|
|
|
// No-op
|
2015-05-12 10:39:59 -07:00
|
|
|
case 24:
|
|
|
|
|
ManagedProfileHeuristic.markExistingUsersForNoFolderCreation(mContext);
|
2015-05-19 16:03:28 -07:00
|
|
|
case 25:
|
|
|
|
|
convertShortcutsToLauncherActivities(db);
|
|
|
|
|
case 26: {
|
2015-01-14 14:23:02 -08:00
|
|
|
// DB Upgraded successfully
|
|
|
|
|
return;
|
2015-01-05 13:41:43 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-14 14:23:02 -08:00
|
|
|
// DB was not upgraded
|
|
|
|
|
Log.w(TAG, "Destroying all old data.");
|
|
|
|
|
createEmptyDB(db);
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
2009-12-02 20:10:07 -08:00
|
|
|
|
2014-05-21 19:01:57 -07:00
|
|
|
@Override
|
|
|
|
|
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
|
|
|
|
// This shouldn't happen -- throw our hands up in the air and start over.
|
|
|
|
|
Log.w(TAG, "Database version downgrade from: " + oldVersion + " to " + newVersion +
|
|
|
|
|
". Wiping databse.");
|
2014-09-26 22:09:29 -07:00
|
|
|
createEmptyDB(db);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clears all the data for a fresh start.
|
|
|
|
|
*/
|
|
|
|
|
public void createEmptyDB(SQLiteDatabase db) {
|
2014-05-21 19:01:57 -07:00
|
|
|
db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES);
|
|
|
|
|
db.execSQL("DROP TABLE IF EXISTS " + TABLE_WORKSPACE_SCREENS);
|
|
|
|
|
onCreate(db);
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-02 10:27:03 -07:00
|
|
|
/**
|
|
|
|
|
* Replaces all shortcuts of type {@link Favorites#ITEM_TYPE_SHORTCUT} which have a valid
|
|
|
|
|
* launcher activity target with {@link Favorites#ITEM_TYPE_APPLICATION}.
|
|
|
|
|
*/
|
2015-06-02 09:38:28 -07:00
|
|
|
@Thunk void convertShortcutsToLauncherActivities(SQLiteDatabase db) {
|
2015-04-02 10:27:03 -07:00
|
|
|
db.beginTransaction();
|
|
|
|
|
Cursor c = null;
|
|
|
|
|
SQLiteStatement updateStmt = null;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// Only consider the primary user as other users can't have a shortcut.
|
|
|
|
|
long userSerial = UserManagerCompat.getInstance(mContext)
|
|
|
|
|
.getSerialNumberForUser(UserHandleCompat.myUserHandle());
|
|
|
|
|
c = db.query(TABLE_FAVORITES, new String[] {
|
|
|
|
|
Favorites._ID,
|
|
|
|
|
Favorites.INTENT,
|
|
|
|
|
}, "itemType=" + Favorites.ITEM_TYPE_SHORTCUT + " AND profileId=" + userSerial,
|
|
|
|
|
null, null, null, null);
|
|
|
|
|
|
|
|
|
|
updateStmt = db.compileStatement("UPDATE favorites SET itemType="
|
|
|
|
|
+ Favorites.ITEM_TYPE_APPLICATION + " WHERE _id=?");
|
|
|
|
|
|
|
|
|
|
final int idIndex = c.getColumnIndexOrThrow(Favorites._ID);
|
|
|
|
|
final int intentIndex = c.getColumnIndexOrThrow(Favorites.INTENT);
|
|
|
|
|
|
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
|
String intentDescription = c.getString(intentIndex);
|
|
|
|
|
Intent intent;
|
|
|
|
|
try {
|
|
|
|
|
intent = Intent.parseUri(intentDescription, 0);
|
|
|
|
|
} catch (URISyntaxException e) {
|
|
|
|
|
Log.e(TAG, "Unable to parse intent", e);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-25 16:37:44 -07:00
|
|
|
if (!Utilities.isLauncherAppTarget(intent)) {
|
2015-04-02 10:27:03 -07:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
long id = c.getLong(idIndex);
|
|
|
|
|
updateStmt.bindLong(1, id);
|
2015-07-13 19:59:50 -07:00
|
|
|
updateStmt.executeUpdateDelete();
|
2015-04-02 10:27:03 -07:00
|
|
|
}
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
|
} catch (SQLException ex) {
|
|
|
|
|
Log.w(TAG, "Error deduping shortcuts", ex);
|
|
|
|
|
} finally {
|
|
|
|
|
db.endTransaction();
|
|
|
|
|
if (c != null) {
|
|
|
|
|
c.close();
|
|
|
|
|
}
|
|
|
|
|
if (updateStmt != null) {
|
|
|
|
|
updateStmt.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-25 10:46:34 -08:00
|
|
|
/**
|
|
|
|
|
* Recreates workspace table and migrates data to the new table.
|
|
|
|
|
*/
|
|
|
|
|
public boolean recreateWorkspaceTable(SQLiteDatabase db) {
|
|
|
|
|
db.beginTransaction();
|
|
|
|
|
try {
|
|
|
|
|
Cursor c = db.query(TABLE_WORKSPACE_SCREENS,
|
|
|
|
|
new String[] {LauncherSettings.WorkspaceScreens._ID},
|
|
|
|
|
null, null, null, null,
|
|
|
|
|
LauncherSettings.WorkspaceScreens.SCREEN_RANK);
|
|
|
|
|
ArrayList<Long> sortedIDs = new ArrayList<Long>();
|
|
|
|
|
long maxId = 0;
|
|
|
|
|
try {
|
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
|
Long id = c.getLong(0);
|
|
|
|
|
if (!sortedIDs.contains(id)) {
|
|
|
|
|
sortedIDs.add(id);
|
|
|
|
|
maxId = Math.max(maxId, id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
c.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
db.execSQL("DROP TABLE IF EXISTS " + TABLE_WORKSPACE_SCREENS);
|
|
|
|
|
addWorkspacesTable(db);
|
|
|
|
|
|
|
|
|
|
// Add all screen ids back
|
|
|
|
|
int total = sortedIDs.size();
|
|
|
|
|
for (int i = 0; i < total; i++) {
|
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
|
values.put(LauncherSettings.WorkspaceScreens._ID, sortedIDs.get(i));
|
|
|
|
|
values.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
|
|
|
|
|
addModifiedTime(values);
|
|
|
|
|
db.insertOrThrow(TABLE_WORKSPACE_SCREENS, null, values);
|
|
|
|
|
}
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
|
mMaxScreenId = maxId;
|
|
|
|
|
} catch (SQLException ex) {
|
|
|
|
|
// Old version remains, which means we wipe old data
|
|
|
|
|
Log.e(TAG, ex.getMessage(), ex);
|
|
|
|
|
return false;
|
|
|
|
|
} finally {
|
|
|
|
|
db.endTransaction();
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-18 14:16:05 -07:00
|
|
|
@Thunk boolean updateFolderItemsRank(SQLiteDatabase db, boolean addRankColumn) {
|
2015-01-05 13:41:43 -08:00
|
|
|
db.beginTransaction();
|
|
|
|
|
try {
|
|
|
|
|
if (addRankColumn) {
|
|
|
|
|
// Insert new column for holding rank
|
|
|
|
|
db.execSQL("ALTER TABLE favorites ADD COLUMN rank INTEGER NOT NULL DEFAULT 0;");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get a map for folder ID to folder width
|
|
|
|
|
Cursor c = db.rawQuery("SELECT container, MAX(cellX) FROM favorites"
|
|
|
|
|
+ " WHERE container IN (SELECT _id FROM favorites WHERE itemType = ?)"
|
|
|
|
|
+ " GROUP BY container;",
|
|
|
|
|
new String[] {Integer.toString(LauncherSettings.Favorites.ITEM_TYPE_FOLDER)});
|
|
|
|
|
|
|
|
|
|
while (c.moveToNext()) {
|
2015-02-10 19:52:36 -08:00
|
|
|
db.execSQL("UPDATE favorites SET rank=cellX+(cellY*?) WHERE "
|
|
|
|
|
+ "container=? AND cellX IS NOT NULL AND cellY IS NOT NULL;",
|
2015-01-05 13:41:43 -08:00
|
|
|
new Object[] {c.getLong(1) + 1, c.getLong(0)});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.close();
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
|
} catch (SQLException ex) {
|
|
|
|
|
// Old version remains, which means we wipe old data
|
|
|
|
|
Log.e(TAG, ex.getMessage(), ex);
|
|
|
|
|
return false;
|
|
|
|
|
} finally {
|
|
|
|
|
db.endTransaction();
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-30 03:02:21 +01:00
|
|
|
private boolean addProfileColumn(SQLiteDatabase db) {
|
2015-03-10 13:14:47 -07:00
|
|
|
UserManagerCompat userManager = UserManagerCompat.getInstance(mContext);
|
|
|
|
|
// Default to the serial number of this user, for older
|
|
|
|
|
// shortcuts.
|
|
|
|
|
long userSerialNumber = userManager.getSerialNumberForUser(
|
|
|
|
|
UserHandleCompat.myUserHandle());
|
|
|
|
|
return addIntegerColumn(db, Favorites.PROFILE_ID, userSerialNumber);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean addIntegerColumn(SQLiteDatabase db, String columnName, long defaultValue) {
|
2014-04-30 03:02:21 +01:00
|
|
|
db.beginTransaction();
|
|
|
|
|
try {
|
2015-03-10 13:14:47 -07:00
|
|
|
db.execSQL("ALTER TABLE favorites ADD COLUMN "
|
|
|
|
|
+ columnName + " INTEGER NOT NULL DEFAULT " + defaultValue + ";");
|
2014-04-30 03:02:21 +01:00
|
|
|
db.setTransactionSuccessful();
|
|
|
|
|
} catch (SQLException ex) {
|
|
|
|
|
Log.e(TAG, ex.getMessage(), ex);
|
|
|
|
|
return false;
|
|
|
|
|
} finally {
|
|
|
|
|
db.endTransaction();
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-28 14:59:33 -07:00
|
|
|
// Generates a new ID to use for an object in your database. This method should be only
|
|
|
|
|
// called from the main UI thread. As an exception, we do call it when we call the
|
|
|
|
|
// constructor from the worker thread; however, this doesn't extend until after the
|
|
|
|
|
// constructor is called, and we only pass a reference to LauncherProvider to LauncherApp
|
|
|
|
|
// after that point
|
2014-08-06 09:55:36 -07:00
|
|
|
@Override
|
2013-06-18 13:13:40 -07:00
|
|
|
public long generateNewItemId() {
|
|
|
|
|
if (mMaxItemId < 0) {
|
|
|
|
|
throw new RuntimeException("Error: max item id was not initialized");
|
2011-04-28 14:59:33 -07:00
|
|
|
}
|
2013-06-18 13:13:40 -07:00
|
|
|
mMaxItemId += 1;
|
|
|
|
|
return mMaxItemId;
|
2011-04-28 14:59:33 -07:00
|
|
|
}
|
|
|
|
|
|
2014-08-06 09:55:36 -07:00
|
|
|
@Override
|
|
|
|
|
public long insertAndCheck(SQLiteDatabase db, ContentValues values) {
|
|
|
|
|
return dbInsertAndCheck(this, db, TABLE_FAVORITES, null, values);
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-19 13:49:06 -07:00
|
|
|
public void updateMaxItemId(long id) {
|
|
|
|
|
mMaxItemId = id + 1;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-20 17:22:11 -05:00
|
|
|
public void checkId(String table, ContentValues values) {
|
|
|
|
|
long id = values.getAsLong(LauncherSettings.BaseLauncherColumns._ID);
|
|
|
|
|
if (table == LauncherProvider.TABLE_WORKSPACE_SCREENS) {
|
|
|
|
|
mMaxScreenId = Math.max(id, mMaxScreenId);
|
|
|
|
|
} else {
|
|
|
|
|
mMaxItemId = Math.max(id, mMaxItemId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-18 13:13:40 -07:00
|
|
|
private long initializeMaxItemId(SQLiteDatabase db) {
|
2015-03-04 10:43:45 -08:00
|
|
|
return getMaxId(db, TABLE_FAVORITES);
|
2013-06-18 13:13:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Generates a new ID to use for an workspace screen in your database. This method
|
|
|
|
|
// should be only called from the main UI thread. As an exception, we do call it when we
|
|
|
|
|
// call the constructor from the worker thread; however, this doesn't extend until after the
|
|
|
|
|
// constructor is called, and we only pass a reference to LauncherProvider to LauncherApp
|
|
|
|
|
// after that point
|
|
|
|
|
public long generateNewScreenId() {
|
|
|
|
|
if (mMaxScreenId < 0) {
|
|
|
|
|
throw new RuntimeException("Error: max screen id was not initialized");
|
|
|
|
|
}
|
|
|
|
|
mMaxScreenId += 1;
|
|
|
|
|
return mMaxScreenId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private long initializeMaxScreenId(SQLiteDatabase db) {
|
2015-03-04 10:43:45 -08:00
|
|
|
return getMaxId(db, TABLE_WORKSPACE_SCREENS);
|
2010-02-08 13:44:00 -08:00
|
|
|
}
|
|
|
|
|
|
2015-03-18 14:16:05 -07:00
|
|
|
@Thunk boolean initializeExternalAdd(ContentValues values) {
|
2014-07-23 14:49:38 -07:00
|
|
|
// 1. Ensure that externally added items have a valid item id
|
|
|
|
|
long id = generateNewItemId();
|
|
|
|
|
values.put(LauncherSettings.Favorites._ID, id);
|
|
|
|
|
|
|
|
|
|
// 2. In the case of an app widget, and if no app widget id is specified, we
|
|
|
|
|
// attempt allocate and bind the widget.
|
|
|
|
|
Integer itemType = values.getAsInteger(LauncherSettings.Favorites.ITEM_TYPE);
|
|
|
|
|
if (itemType != null &&
|
|
|
|
|
itemType.intValue() == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET &&
|
|
|
|
|
!values.containsKey(LauncherSettings.Favorites.APPWIDGET_ID)) {
|
|
|
|
|
|
|
|
|
|
final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
|
|
|
|
|
ComponentName cn = ComponentName.unflattenFromString(
|
|
|
|
|
values.getAsString(Favorites.APPWIDGET_PROVIDER));
|
|
|
|
|
|
|
|
|
|
if (cn != null) {
|
|
|
|
|
try {
|
|
|
|
|
int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
|
2014-07-23 18:21:20 -07:00
|
|
|
values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId);
|
2014-10-14 08:55:28 -07:00
|
|
|
if (!appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,cn)) {
|
2014-10-14 15:04:34 -07:00
|
|
|
return false;
|
2014-07-23 14:49:38 -07:00
|
|
|
}
|
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
|
Log.e(TAG, "Failed to initialize external widget", e);
|
2014-10-14 08:55:28 -07:00
|
|
|
return false;
|
2014-07-23 14:49:38 -07:00
|
|
|
}
|
2014-10-14 08:55:28 -07:00
|
|
|
} else {
|
|
|
|
|
return false;
|
2014-07-23 14:49:38 -07:00
|
|
|
}
|
|
|
|
|
}
|
2014-07-31 00:09:45 -07:00
|
|
|
|
|
|
|
|
// Add screen id if not present
|
|
|
|
|
long screenId = values.getAsLong(LauncherSettings.Favorites.SCREEN);
|
|
|
|
|
if (!addScreenIdIfNecessary(screenId)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-07-23 14:49:38 -07:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-31 00:09:45 -07:00
|
|
|
// Returns true of screen id exists, or if successfully added
|
|
|
|
|
private boolean addScreenIdIfNecessary(long screenId) {
|
|
|
|
|
if (!hasScreenId(screenId)) {
|
|
|
|
|
int rank = getMaxScreenRank() + 1;
|
|
|
|
|
|
|
|
|
|
ContentValues v = new ContentValues();
|
|
|
|
|
v.put(LauncherSettings.WorkspaceScreens._ID, screenId);
|
|
|
|
|
v.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, rank);
|
|
|
|
|
if (dbInsertAndCheck(this, getWritableDatabase(),
|
|
|
|
|
TABLE_WORKSPACE_SCREENS, null, v) < 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean hasScreenId(long screenId) {
|
|
|
|
|
SQLiteDatabase db = getWritableDatabase();
|
|
|
|
|
Cursor c = db.rawQuery("SELECT * FROM " + TABLE_WORKSPACE_SCREENS + " WHERE "
|
|
|
|
|
+ LauncherSettings.WorkspaceScreens._ID + " = " + screenId, null);
|
|
|
|
|
if (c != null) {
|
|
|
|
|
int count = c.getCount();
|
|
|
|
|
c.close();
|
|
|
|
|
return count > 0;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int getMaxScreenRank() {
|
|
|
|
|
SQLiteDatabase db = getWritableDatabase();
|
|
|
|
|
Cursor c = db.rawQuery("SELECT MAX(" + LauncherSettings.WorkspaceScreens.SCREEN_RANK
|
|
|
|
|
+ ") FROM " + TABLE_WORKSPACE_SCREENS, null);
|
|
|
|
|
|
|
|
|
|
// get the result
|
|
|
|
|
final int maxRankIndex = 0;
|
|
|
|
|
int rank = -1;
|
|
|
|
|
if (c != null && c.moveToNext()) {
|
|
|
|
|
rank = c.getInt(maxRankIndex);
|
|
|
|
|
}
|
|
|
|
|
if (c != null) {
|
|
|
|
|
c.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rank;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-18 14:16:05 -07:00
|
|
|
@Thunk int loadFavorites(SQLiteDatabase db, AutoInstallsLayout loader) {
|
2014-05-15 14:04:01 -07:00
|
|
|
ArrayList<Long> screenIds = new ArrayList<Long>();
|
2014-08-06 09:55:36 -07:00
|
|
|
// TODO: Use multiple loaders with fall-back and transaction.
|
|
|
|
|
int count = loader.loadLayout(db, screenIds);
|
2014-05-15 14:04:01 -07:00
|
|
|
|
|
|
|
|
// Add the screens specified by the items above
|
|
|
|
|
Collections.sort(screenIds);
|
|
|
|
|
int rank = 0;
|
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
|
for (Long id : screenIds) {
|
|
|
|
|
values.clear();
|
|
|
|
|
values.put(LauncherSettings.WorkspaceScreens._ID, id);
|
|
|
|
|
values.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, rank);
|
|
|
|
|
if (dbInsertAndCheck(this, db, TABLE_WORKSPACE_SCREENS, null, values) < 0) {
|
|
|
|
|
throw new RuntimeException("Failed initialize screen table"
|
|
|
|
|
+ "from default layout");
|
|
|
|
|
}
|
|
|
|
|
rank++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ensure that the max ids are initialized
|
|
|
|
|
mMaxItemId = initializeMaxItemId(db);
|
|
|
|
|
mMaxScreenId = initializeMaxScreenId(db);
|
2014-07-31 00:09:45 -07:00
|
|
|
|
2014-05-15 14:04:01 -07:00
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-18 14:16:05 -07:00
|
|
|
@Thunk void migrateLauncher2Shortcuts(SQLiteDatabase db, Uri uri) {
|
2014-01-09 15:01:33 -05:00
|
|
|
final ContentResolver resolver = mContext.getContentResolver();
|
|
|
|
|
Cursor c = null;
|
|
|
|
|
int count = 0;
|
|
|
|
|
int curScreen = 0;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
c = resolver.query(uri, null, null, null, "title ASC");
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
// Ignore
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We already have a favorites database in the old provider
|
|
|
|
|
if (c != null) {
|
|
|
|
|
try {
|
|
|
|
|
if (c.getCount() > 0) {
|
|
|
|
|
final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
|
|
|
|
|
final int intentIndex
|
|
|
|
|
= c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
|
|
|
|
|
final int titleIndex
|
|
|
|
|
= c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
|
|
|
|
|
final int iconTypeIndex
|
|
|
|
|
= c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
|
|
|
|
|
final int iconIndex
|
|
|
|
|
= c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
|
|
|
|
|
final int iconPackageIndex
|
|
|
|
|
= c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
|
|
|
|
|
final int iconResourceIndex
|
|
|
|
|
= c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
|
|
|
|
|
final int containerIndex
|
|
|
|
|
= c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
|
|
|
|
|
final int itemTypeIndex
|
|
|
|
|
= c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
|
|
|
|
|
final int screenIndex
|
|
|
|
|
= c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
|
|
|
|
|
final int cellXIndex
|
|
|
|
|
= c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
|
|
|
|
|
final int cellYIndex
|
|
|
|
|
= c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
|
|
|
|
|
final int uriIndex
|
|
|
|
|
= c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
|
|
|
|
|
final int displayModeIndex
|
|
|
|
|
= c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
|
2014-04-30 03:02:21 +01:00
|
|
|
final int profileIndex
|
|
|
|
|
= c.getColumnIndex(LauncherSettings.Favorites.PROFILE_ID);
|
2014-01-09 15:01:33 -05:00
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
int curX = 0;
|
|
|
|
|
int curY = 0;
|
|
|
|
|
|
|
|
|
|
final LauncherAppState app = LauncherAppState.getInstance();
|
2015-05-06 11:42:25 -07:00
|
|
|
final InvariantDeviceProfile profile = app.getInvariantDeviceProfile();
|
|
|
|
|
final int width = (int) profile.numColumns;
|
|
|
|
|
final int height = (int) profile.numRows;
|
|
|
|
|
final int hotseatWidth = (int) profile.numHotseatIcons;
|
2014-01-09 15:01:33 -05:00
|
|
|
|
|
|
|
|
final HashSet<String> seenIntents = new HashSet<String>(c.getCount());
|
|
|
|
|
|
2014-01-15 18:13:55 -08:00
|
|
|
final ArrayList<ContentValues> shortcuts = new ArrayList<ContentValues>();
|
|
|
|
|
final ArrayList<ContentValues> folders = new ArrayList<ContentValues>();
|
2014-03-06 23:48:04 -05:00
|
|
|
final SparseArray<ContentValues> hotseat = new SparseArray<ContentValues>();
|
2014-01-09 15:01:33 -05:00
|
|
|
|
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
|
final int itemType = c.getInt(itemTypeIndex);
|
|
|
|
|
if (itemType != Favorites.ITEM_TYPE_APPLICATION
|
|
|
|
|
&& itemType != Favorites.ITEM_TYPE_SHORTCUT
|
|
|
|
|
&& itemType != Favorites.ITEM_TYPE_FOLDER) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final int cellX = c.getInt(cellXIndex);
|
|
|
|
|
final int cellY = c.getInt(cellYIndex);
|
|
|
|
|
final int screen = c.getInt(screenIndex);
|
|
|
|
|
int container = c.getInt(containerIndex);
|
|
|
|
|
final String intentStr = c.getString(intentIndex);
|
2014-04-30 03:02:21 +01:00
|
|
|
|
|
|
|
|
UserManagerCompat userManager = UserManagerCompat.getInstance(mContext);
|
|
|
|
|
UserHandleCompat userHandle;
|
|
|
|
|
final long userSerialNumber;
|
|
|
|
|
if (profileIndex != -1 && !c.isNull(profileIndex)) {
|
|
|
|
|
userSerialNumber = c.getInt(profileIndex);
|
|
|
|
|
userHandle = userManager.getUserForSerialNumber(userSerialNumber);
|
|
|
|
|
} else {
|
|
|
|
|
// Default to the serial number of this user, for older
|
|
|
|
|
// shortcuts.
|
|
|
|
|
userHandle = UserHandleCompat.myUserHandle();
|
|
|
|
|
userSerialNumber = userManager.getSerialNumberForUser(userHandle);
|
|
|
|
|
}
|
2014-11-14 11:59:57 -08:00
|
|
|
|
|
|
|
|
if (userHandle == null) {
|
|
|
|
|
Launcher.addDumpLog(TAG, "skipping deleted user", true);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-09 15:01:33 -05:00
|
|
|
Launcher.addDumpLog(TAG, "migrating \""
|
2014-03-06 23:48:04 -05:00
|
|
|
+ c.getString(titleIndex) + "\" ("
|
|
|
|
|
+ cellX + "," + cellY + "@"
|
|
|
|
|
+ LauncherSettings.Favorites.containerToString(container)
|
|
|
|
|
+ "/" + screen
|
|
|
|
|
+ "): " + intentStr, true);
|
2014-01-09 15:01:33 -05:00
|
|
|
|
|
|
|
|
if (itemType != Favorites.ITEM_TYPE_FOLDER) {
|
2014-01-15 15:18:08 -08:00
|
|
|
|
|
|
|
|
final Intent intent;
|
|
|
|
|
final ComponentName cn;
|
|
|
|
|
try {
|
|
|
|
|
intent = Intent.parseUri(intentStr, 0);
|
|
|
|
|
} catch (URISyntaxException e) {
|
|
|
|
|
// bogus intent?
|
|
|
|
|
Launcher.addDumpLog(TAG,
|
|
|
|
|
"skipping invalid intent uri", true);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cn = intent.getComponent();
|
2014-01-09 15:01:33 -05:00
|
|
|
if (TextUtils.isEmpty(intentStr)) {
|
|
|
|
|
// no intent? no icon
|
|
|
|
|
Launcher.addDumpLog(TAG, "skipping empty intent", true);
|
|
|
|
|
continue;
|
2014-01-15 18:13:55 -08:00
|
|
|
} else if (cn != null &&
|
2014-04-30 03:02:21 +01:00
|
|
|
!LauncherModel.isValidPackageActivity(mContext, cn,
|
|
|
|
|
userHandle)) {
|
2014-01-15 15:18:08 -08:00
|
|
|
// component no longer exists.
|
2014-01-15 18:13:55 -08:00
|
|
|
Launcher.addDumpLog(TAG, "skipping item whose component " +
|
2014-01-15 15:18:08 -08:00
|
|
|
"no longer exists.", true);
|
|
|
|
|
continue;
|
2014-01-15 18:13:55 -08:00
|
|
|
} else if (container ==
|
|
|
|
|
LauncherSettings.Favorites.CONTAINER_DESKTOP) {
|
|
|
|
|
// Dedupe icons directly on the workspace
|
|
|
|
|
|
2014-01-15 15:18:08 -08:00
|
|
|
// Canonicalize
|
|
|
|
|
// the Play Store sets the package parameter, but Launcher
|
2014-10-24 12:20:20 -07:00
|
|
|
// does not, so we clear that out to keep them the same.
|
|
|
|
|
// Also ignore intent flags for the purposes of deduping.
|
2014-01-15 15:18:08 -08:00
|
|
|
intent.setPackage(null);
|
2014-10-24 12:20:20 -07:00
|
|
|
int flags = intent.getFlags();
|
|
|
|
|
intent.setFlags(0);
|
2014-01-15 15:18:08 -08:00
|
|
|
final String key = intent.toUri(0);
|
2014-10-24 12:20:20 -07:00
|
|
|
intent.setFlags(flags);
|
2014-01-15 15:18:08 -08:00
|
|
|
if (seenIntents.contains(key)) {
|
|
|
|
|
Launcher.addDumpLog(TAG, "skipping duplicate", true);
|
2014-01-09 15:01:33 -05:00
|
|
|
continue;
|
2014-01-15 15:18:08 -08:00
|
|
|
} else {
|
|
|
|
|
seenIntents.add(key);
|
2014-01-09 15:01:33 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ContentValues values = new ContentValues(c.getColumnCount());
|
|
|
|
|
values.put(LauncherSettings.Favorites._ID, c.getInt(idIndex));
|
|
|
|
|
values.put(LauncherSettings.Favorites.INTENT, intentStr);
|
|
|
|
|
values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex));
|
|
|
|
|
values.put(LauncherSettings.Favorites.ICON_TYPE,
|
|
|
|
|
c.getInt(iconTypeIndex));
|
|
|
|
|
values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex));
|
|
|
|
|
values.put(LauncherSettings.Favorites.ICON_PACKAGE,
|
|
|
|
|
c.getString(iconPackageIndex));
|
|
|
|
|
values.put(LauncherSettings.Favorites.ICON_RESOURCE,
|
|
|
|
|
c.getString(iconResourceIndex));
|
|
|
|
|
values.put(LauncherSettings.Favorites.ITEM_TYPE, itemType);
|
|
|
|
|
values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1);
|
|
|
|
|
values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex));
|
|
|
|
|
values.put(LauncherSettings.Favorites.DISPLAY_MODE,
|
|
|
|
|
c.getInt(displayModeIndex));
|
2014-04-30 03:02:21 +01:00
|
|
|
values.put(LauncherSettings.Favorites.PROFILE_ID, userSerialNumber);
|
2014-01-09 15:01:33 -05:00
|
|
|
|
2014-03-06 23:48:04 -05:00
|
|
|
if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
|
|
|
|
|
hotseat.put(screen, values);
|
2014-01-09 15:01:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
|
|
|
|
|
// In a folder or in the hotseat, preserve position
|
|
|
|
|
values.put(LauncherSettings.Favorites.SCREEN, screen);
|
|
|
|
|
values.put(LauncherSettings.Favorites.CELLX, cellX);
|
|
|
|
|
values.put(LauncherSettings.Favorites.CELLY, cellY);
|
|
|
|
|
} else {
|
2014-01-15 18:13:55 -08:00
|
|
|
// For items contained directly on one of the workspace screen,
|
|
|
|
|
// we'll determine their location (screen, x, y) in a second pass.
|
2014-01-09 15:01:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
values.put(LauncherSettings.Favorites.CONTAINER, container);
|
|
|
|
|
|
2014-01-15 18:13:55 -08:00
|
|
|
if (itemType != Favorites.ITEM_TYPE_FOLDER) {
|
|
|
|
|
shortcuts.add(values);
|
|
|
|
|
} else {
|
|
|
|
|
folders.add(values);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-06 23:48:04 -05:00
|
|
|
// Now that we have all the hotseat icons, let's go through them left-right
|
|
|
|
|
// and assign valid locations for them in the new hotseat
|
|
|
|
|
final int N = hotseat.size();
|
|
|
|
|
for (int idx=0; idx<N; idx++) {
|
|
|
|
|
int hotseatX = hotseat.keyAt(idx);
|
|
|
|
|
ContentValues values = hotseat.valueAt(idx);
|
|
|
|
|
|
2015-05-06 11:42:25 -07:00
|
|
|
if (hotseatX == profile.hotseatAllAppsRank) {
|
2014-03-06 23:48:04 -05:00
|
|
|
// let's drop this in the next available hole in the hotseat
|
|
|
|
|
while (++hotseatX < hotseatWidth) {
|
|
|
|
|
if (hotseat.get(hotseatX) == null) {
|
|
|
|
|
// found a spot! move it here
|
|
|
|
|
values.put(LauncherSettings.Favorites.SCREEN,
|
|
|
|
|
hotseatX);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (hotseatX >= hotseatWidth) {
|
|
|
|
|
// no room for you in the hotseat? it's off to the desktop with you
|
|
|
|
|
values.put(LauncherSettings.Favorites.CONTAINER,
|
|
|
|
|
Favorites.CONTAINER_DESKTOP);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-15 18:13:55 -08:00
|
|
|
final ArrayList<ContentValues> allItems = new ArrayList<ContentValues>();
|
|
|
|
|
// Folders first
|
|
|
|
|
allItems.addAll(folders);
|
|
|
|
|
// Then shortcuts
|
|
|
|
|
allItems.addAll(shortcuts);
|
|
|
|
|
|
|
|
|
|
// Layout all the folders
|
|
|
|
|
for (ContentValues values: allItems) {
|
|
|
|
|
if (values.getAsInteger(LauncherSettings.Favorites.CONTAINER) !=
|
|
|
|
|
LauncherSettings.Favorites.CONTAINER_DESKTOP) {
|
|
|
|
|
// Hotseat items and folder items have already had their
|
|
|
|
|
// location information set. Nothing to be done here.
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
values.put(LauncherSettings.Favorites.SCREEN, curScreen);
|
|
|
|
|
values.put(LauncherSettings.Favorites.CELLX, curX);
|
|
|
|
|
values.put(LauncherSettings.Favorites.CELLY, curY);
|
|
|
|
|
curX = (curX + 1) % width;
|
|
|
|
|
if (curX == 0) {
|
|
|
|
|
curY = (curY + 1);
|
|
|
|
|
}
|
|
|
|
|
// Leave the last row of icons blank on every screen
|
|
|
|
|
if (curY == height - 1) {
|
|
|
|
|
curScreen = (int) generateNewScreenId();
|
|
|
|
|
curY = 0;
|
|
|
|
|
}
|
2014-01-09 15:01:33 -05:00
|
|
|
}
|
|
|
|
|
|
2014-01-15 18:13:55 -08:00
|
|
|
if (allItems.size() > 0) {
|
2014-01-09 15:01:33 -05:00
|
|
|
db.beginTransaction();
|
|
|
|
|
try {
|
2014-01-15 18:13:55 -08:00
|
|
|
for (ContentValues row: allItems) {
|
|
|
|
|
if (row == null) continue;
|
|
|
|
|
if (dbInsertAndCheck(this, db, TABLE_FAVORITES, null, row)
|
2014-01-09 15:01:33 -05:00
|
|
|
< 0) {
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
|
} finally {
|
|
|
|
|
db.endTransaction();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
db.beginTransaction();
|
|
|
|
|
try {
|
|
|
|
|
for (i=0; i<=curScreen; i++) {
|
|
|
|
|
final ContentValues values = new ContentValues();
|
|
|
|
|
values.put(LauncherSettings.WorkspaceScreens._ID, i);
|
|
|
|
|
values.put(LauncherSettings.WorkspaceScreens.SCREEN_RANK, i);
|
|
|
|
|
if (dbInsertAndCheck(this, db, TABLE_WORKSPACE_SCREENS, null, values)
|
|
|
|
|
< 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
|
} finally {
|
|
|
|
|
db.endTransaction();
|
|
|
|
|
}
|
2015-01-05 13:41:43 -08:00
|
|
|
|
|
|
|
|
updateFolderItemsRank(db, false);
|
2014-01-09 15:01:33 -05:00
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
c.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Launcher.addDumpLog(TAG, "migrated " + count + " icons from Launcher2 into "
|
|
|
|
|
+ (curScreen+1) + " screens", true);
|
|
|
|
|
|
|
|
|
|
// ensure that new screens are created to hold these icons
|
|
|
|
|
setFlagJustLoadedOldDb();
|
|
|
|
|
|
|
|
|
|
// Update max IDs; very important since we just grabbed IDs from another database
|
|
|
|
|
mMaxItemId = initializeMaxItemId(db);
|
|
|
|
|
mMaxScreenId = initializeMaxScreenId(db);
|
|
|
|
|
if (LOGD) Log.d(TAG, "mMaxItemId: " + mMaxItemId + " mMaxScreenId: " + mMaxScreenId);
|
|
|
|
|
}
|
2009-10-30 16:36:56 -07:00
|
|
|
}
|
2012-04-23 21:35:11 -07:00
|
|
|
|
2015-03-04 10:43:45 -08:00
|
|
|
/**
|
|
|
|
|
* @return the max _id in the provided table.
|
|
|
|
|
*/
|
2015-03-18 14:16:05 -07:00
|
|
|
@Thunk static long getMaxId(SQLiteDatabase db, String table) {
|
2015-03-04 10:43:45 -08:00
|
|
|
Cursor c = db.rawQuery("SELECT MAX(_id) FROM " + table, null);
|
|
|
|
|
// get the result
|
|
|
|
|
long id = -1;
|
|
|
|
|
if (c != null && c.moveToNext()) {
|
|
|
|
|
id = c.getLong(0);
|
|
|
|
|
}
|
|
|
|
|
if (c != null) {
|
|
|
|
|
c.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (id == -1) {
|
|
|
|
|
throw new RuntimeException("Error: could not query max id in " + table);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
static class SqlArguments {
|
|
|
|
|
public final String table;
|
|
|
|
|
public final String where;
|
|
|
|
|
public final String[] args;
|
|
|
|
|
|
|
|
|
|
SqlArguments(Uri url, String where, String[] args) {
|
|
|
|
|
if (url.getPathSegments().size() == 1) {
|
|
|
|
|
this.table = url.getPathSegments().get(0);
|
|
|
|
|
this.where = where;
|
|
|
|
|
this.args = args;
|
|
|
|
|
} else if (url.getPathSegments().size() != 2) {
|
|
|
|
|
throw new IllegalArgumentException("Invalid URI: " + url);
|
|
|
|
|
} else if (!TextUtils.isEmpty(where)) {
|
|
|
|
|
throw new UnsupportedOperationException("WHERE clause not supported: " + url);
|
|
|
|
|
} else {
|
|
|
|
|
this.table = url.getPathSegments().get(0);
|
2012-04-23 21:35:11 -07:00
|
|
|
this.where = "_id=" + ContentUris.parseId(url);
|
2009-03-03 19:32:27 -08:00
|
|
|
this.args = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SqlArguments(Uri url) {
|
|
|
|
|
if (url.getPathSegments().size() == 1) {
|
|
|
|
|
table = url.getPathSegments().get(0);
|
|
|
|
|
where = null;
|
|
|
|
|
args = null;
|
|
|
|
|
} else {
|
|
|
|
|
throw new IllegalArgumentException("Invalid URI: " + url);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-15 18:13:55 -08:00
|
|
|
}
|