The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
Sunny Goyal | 3e58eea | 2022-11-14 14:30:07 -0800 | [diff] [blame] | 19 | import static com.android.launcher3.DefaultLayoutParser.RES_PARTNER_DEFAULT_LAYOUT; |
Tracy Zhou | f601872 | 2020-02-06 16:37:16 -0800 | [diff] [blame] | 20 | import static com.android.launcher3.provider.LauncherDbUtils.copyTable; |
Sunny Goyal | 161a214 | 2018-10-29 14:02:20 -0700 | [diff] [blame] | 21 | import static com.android.launcher3.provider.LauncherDbUtils.tableExists; |
| 22 | |
Sunny Goyal | 22ca9ec | 2017-05-18 15:03:13 -0700 | [diff] [blame] | 23 | import android.annotation.TargetApi; |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 24 | import android.appwidget.AppWidgetManager; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 25 | import android.content.ComponentName; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 26 | import android.content.ContentProvider; |
Yura | 085c853 | 2014-02-11 15:15:29 +0000 | [diff] [blame] | 27 | import android.content.ContentProviderOperation; |
| 28 | import android.content.ContentProviderResult; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 29 | import android.content.ContentUris; |
| 30 | import android.content.ContentValues; |
| 31 | import android.content.Context; |
Yura | 085c853 | 2014-02-11 15:15:29 +0000 | [diff] [blame] | 32 | import android.content.OperationApplicationException; |
Michael Jurka | b85f8a4 | 2012-04-25 15:48:32 -0700 | [diff] [blame] | 33 | import android.content.SharedPreferences; |
Sunny Goyal | c0f03d9 | 2019-03-22 14:13:36 -0700 | [diff] [blame] | 34 | import android.content.pm.ProviderInfo; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 35 | import android.database.Cursor; |
| 36 | import android.database.SQLException; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 37 | import android.database.sqlite.SQLiteDatabase; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 38 | import android.database.sqlite.SQLiteQueryBuilder; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 39 | import android.net.Uri; |
Sunny Goyal | 7779d62 | 2015-06-11 16:18:39 -0700 | [diff] [blame] | 40 | import android.os.Binder; |
Sunny Goyal | 22ca9ec | 2017-05-18 15:03:13 -0700 | [diff] [blame] | 41 | import android.os.Build; |
Sunny Goyal | b2d46ce | 2015-03-26 11:32:11 -0700 | [diff] [blame] | 42 | import android.os.Bundle; |
Sunny Goyal | 7779d62 | 2015-06-11 16:18:39 -0700 | [diff] [blame] | 43 | import android.os.Process; |
Sunny Goyal | 337c81f | 2019-12-10 12:19:13 -0800 | [diff] [blame] | 44 | import android.os.UserManager; |
Sunny Goyal | c0f03d9 | 2019-03-22 14:13:36 -0700 | [diff] [blame] | 45 | import android.provider.Settings; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 46 | import android.text.TextUtils; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 47 | import android.util.Log; |
Sunny Goyal | c0f03d9 | 2019-03-22 14:13:36 -0700 | [diff] [blame] | 48 | import android.util.Xml; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 49 | |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 50 | import com.android.launcher3.LauncherSettings.Favorites; |
Sunny Goyal | a9e2f5a | 2016-06-10 12:22:04 -0700 | [diff] [blame] | 51 | import com.android.launcher3.config.FeatureFlags; |
Sunny Goyal | ba27958 | 2023-04-07 11:45:09 -0700 | [diff] [blame] | 52 | import com.android.launcher3.model.DatabaseHelper; |
Sunny Goyal | a9e2f5a | 2016-06-10 12:22:04 -0700 | [diff] [blame] | 53 | import com.android.launcher3.provider.LauncherDbUtils; |
Sunny Goyal | dbfc901 | 2017-04-17 16:58:36 -0700 | [diff] [blame] | 54 | import com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction; |
Sunny Goyal | e8f7d5a | 2016-05-24 11:30:14 -0700 | [diff] [blame] | 55 | import com.android.launcher3.provider.RestoreDbTask; |
Sunny Goyal | c0f03d9 | 2019-03-22 14:13:36 -0700 | [diff] [blame] | 56 | import com.android.launcher3.util.IOUtils; |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 57 | import com.android.launcher3.util.IntArray; |
Sunny Goyal | 3e58eea | 2022-11-14 14:30:07 -0800 | [diff] [blame] | 58 | import com.android.launcher3.util.Partner; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 59 | import com.android.launcher3.util.Thunk; |
Sihua Ma | 8bbfcb6 | 2022-11-08 16:46:07 -0800 | [diff] [blame] | 60 | import com.android.launcher3.widget.LauncherWidgetHolder; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 61 | |
Sunny Goyal | c0f03d9 | 2019-03-22 14:13:36 -0700 | [diff] [blame] | 62 | import org.xmlpull.v1.XmlPullParser; |
| 63 | |
Hyunyoung Song | 6aa3729 | 2017-02-06 10:46:24 -0800 | [diff] [blame] | 64 | import java.io.FileDescriptor; |
Sunny Goyal | c0f03d9 | 2019-03-22 14:13:36 -0700 | [diff] [blame] | 65 | import java.io.InputStream; |
Hyunyoung Song | 6aa3729 | 2017-02-06 10:46:24 -0800 | [diff] [blame] | 66 | import java.io.PrintWriter; |
Sunny Goyal | c0f03d9 | 2019-03-22 14:13:36 -0700 | [diff] [blame] | 67 | import java.io.StringReader; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 68 | import java.util.ArrayList; |
Tracy Zhou | c000045 | 2020-03-17 18:28:38 -0700 | [diff] [blame] | 69 | import java.util.function.Supplier; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 70 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 71 | public class LauncherProvider extends ContentProvider { |
Sunny Goyal | c74e419 | 2015-09-08 14:01:03 -0700 | [diff] [blame] | 72 | private static final String TAG = "LauncherProvider"; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 73 | |
Sunny Goyal | 4276e7b | 2018-11-26 23:44:41 -0800 | [diff] [blame] | 74 | public static final String AUTHORITY = BuildConfig.APPLICATION_ID + ".settings"; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 75 | |
Schneider Victor-tulias | fb252e7 | 2022-02-10 11:10:21 -0800 | [diff] [blame] | 76 | private static final int TEST_WORKSPACE_LAYOUT_RES_XML = R.xml.default_test_workspace; |
Alex Chau | f8b79d4 | 2022-12-15 13:41:49 +0000 | [diff] [blame] | 77 | private static final int TEST2_WORKSPACE_LAYOUT_RES_XML = R.xml.default_test2_workspace; |
Sebastian Franco | 239ae0a | 2023-03-03 15:56:52 -0800 | [diff] [blame] | 78 | private static final int TAPL_WORKSPACE_LAYOUT_RES_XML = R.xml.default_tapl_test_workspace; |
Schneider Victor-tulias | fb252e7 | 2022-02-10 11:10:21 -0800 | [diff] [blame] | 79 | |
Sunny Goyal | ba27958 | 2023-04-07 11:45:09 -0700 | [diff] [blame] | 80 | public static final String EMPTY_DATABASE_CREATED = "EMPTY_DATABASE_CREATED"; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 81 | |
Sunny Goyal | f076eae | 2016-01-11 12:25:10 -0800 | [diff] [blame] | 82 | protected DatabaseHelper mOpenHelper; |
Pinyao Ting | 96186af | 2020-07-20 11:03:39 -0700 | [diff] [blame] | 83 | protected String mProviderAuthority; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 84 | |
Alex Chau | f8b79d4 | 2022-12-15 13:41:49 +0000 | [diff] [blame] | 85 | private int mDefaultWorkspaceLayoutOverride = 0; |
Schneider Victor-tulias | fb252e7 | 2022-02-10 11:10:21 -0800 | [diff] [blame] | 86 | |
Hyunyoung Song | 6aa3729 | 2017-02-06 10:46:24 -0800 | [diff] [blame] | 87 | /** |
| 88 | * $ adb shell dumpsys activity provider com.android.launcher3 |
| 89 | */ |
| 90 | @Override |
| 91 | public void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
| 92 | LauncherAppState appState = LauncherAppState.getInstanceNoCreate(); |
| 93 | if (appState == null || !appState.getModel().isModelLoaded()) { |
| 94 | return; |
| 95 | } |
| 96 | appState.getModel().dumpState("", fd, writer, args); |
| 97 | } |
| 98 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 99 | @Override |
| 100 | public boolean onCreate() { |
Zak Cohen | 3eeb41d | 2020-02-14 14:15:13 -0800 | [diff] [blame] | 101 | if (FeatureFlags.IS_STUDIO_BUILD) { |
Sunny Goyal | e26d100 | 2016-06-20 14:52:14 -0700 | [diff] [blame] | 102 | Log.d(TAG, "Launcher process started"); |
| 103 | } |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 104 | |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 105 | // The content provider exists for the entire duration of the launcher main process and |
Sunny Goyal | 66f2b35 | 2018-02-09 10:57:12 -0800 | [diff] [blame] | 106 | // is the first component to get created. |
| 107 | MainProcessInitializer.initialize(getContext().getApplicationContext()); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 108 | return true; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public String getType(Uri uri) { |
| 113 | SqlArguments args = new SqlArguments(uri, null, null); |
| 114 | if (TextUtils.isEmpty(args.where)) { |
| 115 | return "vnd.android.cursor.dir/" + args.table; |
| 116 | } else { |
| 117 | return "vnd.android.cursor.item/" + args.table; |
| 118 | } |
| 119 | } |
| 120 | |
Sunny Goyal | f076eae | 2016-01-11 12:25:10 -0800 | [diff] [blame] | 121 | /** |
| 122 | * Overridden in tests |
| 123 | */ |
| 124 | protected synchronized void createDbIfNotExists() { |
Sunny Goyal | d3849d1 | 2015-10-29 10:28:32 -0700 | [diff] [blame] | 125 | if (mOpenHelper == null) { |
Tracy Zhou | ed5f308 | 2020-04-20 01:13:26 -0700 | [diff] [blame] | 126 | mOpenHelper = DatabaseHelper.createDatabaseHelper( |
| 127 | getContext(), false /* forMigration */); |
Sunny Goyal | e8f7d5a | 2016-05-24 11:30:14 -0700 | [diff] [blame] | 128 | |
Sunny Goyal | 68031ca | 2021-08-02 12:23:44 -0700 | [diff] [blame] | 129 | RestoreDbTask.restoreIfNeeded(getContext(), mOpenHelper); |
Sunny Goyal | d3849d1 | 2015-10-29 10:28:32 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Tracy Zhou | c000045 | 2020-03-17 18:28:38 -0700 | [diff] [blame] | 133 | private synchronized boolean prepForMigration(String dbFile, String targetTableName, |
| 134 | Supplier<DatabaseHelper> src, Supplier<DatabaseHelper> dst) { |
| 135 | if (TextUtils.equals(dbFile, mOpenHelper.getDatabaseName())) { |
Alex Chau | 0adc366 | 2022-07-04 13:05:38 +0100 | [diff] [blame] | 136 | Log.e(TAG, "prepForMigration - target db is same as current: " + dbFile); |
Tracy Zhou | 7df93d2 | 2020-01-27 13:44:06 -0800 | [diff] [blame] | 137 | return false; |
| 138 | } |
| 139 | |
Tracy Zhou | c000045 | 2020-03-17 18:28:38 -0700 | [diff] [blame] | 140 | final DatabaseHelper helper = src.get(); |
| 141 | mOpenHelper = dst.get(); |
| 142 | copyTable(helper.getReadableDatabase(), Favorites.TABLE_NAME, |
| 143 | mOpenHelper.getWritableDatabase(), targetTableName, getContext()); |
| 144 | helper.close(); |
Tracy Zhou | 7df93d2 | 2020-01-27 13:44:06 -0800 | [diff] [blame] | 145 | return true; |
| 146 | } |
| 147 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 148 | @Override |
| 149 | public Cursor query(Uri uri, String[] projection, String selection, |
| 150 | String[] selectionArgs, String sortOrder) { |
Sunny Goyal | d3849d1 | 2015-10-29 10:28:32 -0700 | [diff] [blame] | 151 | createDbIfNotExists(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 152 | |
| 153 | SqlArguments args = new SqlArguments(uri, selection, selectionArgs); |
| 154 | SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); |
| 155 | qb.setTables(args.table); |
| 156 | |
Romain Guy | 73b979d | 2009-06-09 12:57:21 -0700 | [diff] [blame] | 157 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 158 | Cursor result = qb.query(db, projection, args.where, args.args, null, null, sortOrder); |
Pinyao Ting | 0960b45 | 2020-10-14 11:17:04 -0700 | [diff] [blame] | 159 | final Bundle extra = new Bundle(); |
| 160 | extra.putString(LauncherSettings.Settings.EXTRA_DB_NAME, mOpenHelper.getDatabaseName()); |
| 161 | result.setExtras(extra); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 162 | result.setNotificationUri(getContext().getContentResolver(), uri); |
| 163 | |
| 164 | return result; |
| 165 | } |
| 166 | |
Sunny Goyal | d106418 | 2015-08-13 12:08:30 -0700 | [diff] [blame] | 167 | private void reloadLauncherIfExternal() { |
Sunny Goyal | 8c48d8b | 2019-01-25 15:10:18 -0800 | [diff] [blame] | 168 | if (Binder.getCallingPid() != Process.myPid()) { |
Sunny Goyal | d106418 | 2015-08-13 12:08:30 -0700 | [diff] [blame] | 169 | LauncherAppState app = LauncherAppState.getInstanceNoCreate(); |
| 170 | if (app != null) { |
Sunny Goyal | dd96a5e | 2017-02-17 11:22:34 -0800 | [diff] [blame] | 171 | app.getModel().forceReload(); |
Sunny Goyal | d106418 | 2015-08-13 12:08:30 -0700 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 176 | @Override |
| 177 | public Uri insert(Uri uri, ContentValues initialValues) { |
Sunny Goyal | d3849d1 | 2015-10-29 10:28:32 -0700 | [diff] [blame] | 178 | createDbIfNotExists(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 179 | SqlArguments args = new SqlArguments(uri); |
| 180 | |
Sunny Goyal | d106418 | 2015-08-13 12:08:30 -0700 | [diff] [blame] | 181 | // In very limited cases, we support system|signature permission apps to modify the db. |
| 182 | if (Binder.getCallingPid() != Process.myPid()) { |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 183 | if (!initializeExternalAdd(initialValues)) { |
Adam Cohen | a043fa8 | 2014-07-23 14:49:38 -0700 | [diff] [blame] | 184 | return null; |
| 185 | } |
| 186 | } |
| 187 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 188 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 189 | addModifiedTime(initialValues); |
Sunny Goyal | ba27958 | 2023-04-07 11:45:09 -0700 | [diff] [blame] | 190 | final int rowId = mOpenHelper.dbInsertAndCheck(db, args.table, initialValues); |
Sunny Goyal | e72f3d5 | 2015-03-02 14:24:21 -0800 | [diff] [blame] | 191 | if (rowId < 0) return null; |
Tracy Zhou | 7df93d2 | 2020-01-27 13:44:06 -0800 | [diff] [blame] | 192 | onAddOrDeleteOp(db); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 193 | |
| 194 | uri = ContentUris.withAppendedId(uri, rowId); |
Sunny Goyal | 8c48d8b | 2019-01-25 15:10:18 -0800 | [diff] [blame] | 195 | reloadLauncherIfExternal(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 196 | return uri; |
| 197 | } |
| 198 | |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 199 | private boolean initializeExternalAdd(ContentValues values) { |
| 200 | // 1. Ensure that externally added items have a valid item id |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 201 | int id = mOpenHelper.generateNewItemId(); |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 202 | values.put(LauncherSettings.Favorites._ID, id); |
| 203 | |
| 204 | // 2. In the case of an app widget, and if no app widget id is specified, we |
| 205 | // attempt allocate and bind the widget. |
| 206 | Integer itemType = values.getAsInteger(LauncherSettings.Favorites.ITEM_TYPE); |
| 207 | if (itemType != null && |
| 208 | itemType.intValue() == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET && |
| 209 | !values.containsKey(LauncherSettings.Favorites.APPWIDGET_ID)) { |
| 210 | |
| 211 | final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getContext()); |
| 212 | ComponentName cn = ComponentName.unflattenFromString( |
| 213 | values.getAsString(Favorites.APPWIDGET_PROVIDER)); |
| 214 | |
| 215 | if (cn != null) { |
Sihua Ma | aa2b872 | 2022-10-25 15:17:58 -0700 | [diff] [blame] | 216 | LauncherWidgetHolder widgetHolder = mOpenHelper.newLauncherWidgetHolder(); |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 217 | try { |
Sihua Ma | aa2b872 | 2022-10-25 15:17:58 -0700 | [diff] [blame] | 218 | int appWidgetId = widgetHolder.allocateAppWidgetId(); |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 219 | values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId); |
| 220 | if (!appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,cn)) { |
Sihua Ma | aa2b872 | 2022-10-25 15:17:58 -0700 | [diff] [blame] | 221 | widgetHolder.deleteAppWidgetId(appWidgetId); |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 222 | return false; |
| 223 | } |
| 224 | } catch (RuntimeException e) { |
| 225 | Log.e(TAG, "Failed to initialize external widget", e); |
| 226 | return false; |
Sihua Ma | aa2b872 | 2022-10-25 15:17:58 -0700 | [diff] [blame] | 227 | } finally { |
| 228 | // Necessary to destroy the holder to free up possible activity context |
| 229 | widgetHolder.destroy(); |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 230 | } |
| 231 | } else { |
| 232 | return false; |
| 233 | } |
| 234 | } |
| 235 | |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 236 | return true; |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 237 | } |
| 238 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 239 | @Override |
| 240 | public int bulkInsert(Uri uri, ContentValues[] values) { |
Sunny Goyal | d3849d1 | 2015-10-29 10:28:32 -0700 | [diff] [blame] | 241 | createDbIfNotExists(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 242 | SqlArguments args = new SqlArguments(uri); |
| 243 | |
| 244 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
Sunny Goyal | dbfc901 | 2017-04-17 16:58:36 -0700 | [diff] [blame] | 245 | try (SQLiteTransaction t = new SQLiteTransaction(db)) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 246 | int numValues = values.length; |
| 247 | for (int i = 0; i < numValues; i++) { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 248 | addModifiedTime(values[i]); |
Sunny Goyal | ba27958 | 2023-04-07 11:45:09 -0700 | [diff] [blame] | 249 | if (mOpenHelper.dbInsertAndCheck(db, args.table, values[i]) < 0) { |
Michael Jurka | a8c760d | 2011-04-28 14:59:33 -0700 | [diff] [blame] | 250 | return 0; |
| 251 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 252 | } |
Tracy Zhou | 7df93d2 | 2020-01-27 13:44:06 -0800 | [diff] [blame] | 253 | onAddOrDeleteOp(db); |
Sunny Goyal | dbfc901 | 2017-04-17 16:58:36 -0700 | [diff] [blame] | 254 | t.commit(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Sunny Goyal | d106418 | 2015-08-13 12:08:30 -0700 | [diff] [blame] | 257 | reloadLauncherIfExternal(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 258 | return values.length; |
| 259 | } |
| 260 | |
Sunny Goyal | 161a214 | 2018-10-29 14:02:20 -0700 | [diff] [blame] | 261 | @TargetApi(Build.VERSION_CODES.M) |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 262 | @Override |
Yura | 085c853 | 2014-02-11 15:15:29 +0000 | [diff] [blame] | 263 | public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) |
| 264 | throws OperationApplicationException { |
Sunny Goyal | d3849d1 | 2015-10-29 10:28:32 -0700 | [diff] [blame] | 265 | createDbIfNotExists(); |
Sunny Goyal | dbfc901 | 2017-04-17 16:58:36 -0700 | [diff] [blame] | 266 | try (SQLiteTransaction t = new SQLiteTransaction(mOpenHelper.getWritableDatabase())) { |
Sunny Goyal | 8c48d8b | 2019-01-25 15:10:18 -0800 | [diff] [blame] | 267 | boolean isAddOrDelete = false; |
Sunny Goyal | 161a214 | 2018-10-29 14:02:20 -0700 | [diff] [blame] | 268 | |
| 269 | final int numOperations = operations.size(); |
| 270 | final ContentProviderResult[] results = new ContentProviderResult[numOperations]; |
| 271 | for (int i = 0; i < numOperations; i++) { |
| 272 | ContentProviderOperation op = operations.get(i); |
| 273 | results[i] = op.apply(this, results, i); |
| 274 | |
| 275 | isAddOrDelete |= (op.isInsert() || op.isDelete()) && |
| 276 | results[i].count != null && results[i].count > 0; |
| 277 | } |
| 278 | if (isAddOrDelete) { |
Tracy Zhou | 7df93d2 | 2020-01-27 13:44:06 -0800 | [diff] [blame] | 279 | onAddOrDeleteOp(t.getDb()); |
Sunny Goyal | 161a214 | 2018-10-29 14:02:20 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Sunny Goyal | dbfc901 | 2017-04-17 16:58:36 -0700 | [diff] [blame] | 282 | t.commit(); |
Sunny Goyal | d106418 | 2015-08-13 12:08:30 -0700 | [diff] [blame] | 283 | reloadLauncherIfExternal(); |
Sunny Goyal | 161a214 | 2018-10-29 14:02:20 -0700 | [diff] [blame] | 284 | return results; |
Yura | 085c853 | 2014-02-11 15:15:29 +0000 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
| 288 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 289 | public int delete(Uri uri, String selection, String[] selectionArgs) { |
Sunny Goyal | d3849d1 | 2015-10-29 10:28:32 -0700 | [diff] [blame] | 290 | createDbIfNotExists(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 291 | SqlArguments args = new SqlArguments(uri, selection, selectionArgs); |
| 292 | |
| 293 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 294 | |
Louis Begin | a9c21c6 | 2016-08-15 15:18:41 -0700 | [diff] [blame] | 295 | if (Binder.getCallingPid() != Process.myPid() |
| 296 | && Favorites.TABLE_NAME.equalsIgnoreCase(args.table)) { |
Sunny Goyal | 55fddc8 | 2017-04-06 15:02:52 -0700 | [diff] [blame] | 297 | mOpenHelper.removeGhostWidgets(mOpenHelper.getWritableDatabase()); |
Louis Begin | a9c21c6 | 2016-08-15 15:18:41 -0700 | [diff] [blame] | 298 | } |
| 299 | int count = db.delete(args.table, args.where, args.args); |
| 300 | if (count > 0) { |
Tracy Zhou | 7df93d2 | 2020-01-27 13:44:06 -0800 | [diff] [blame] | 301 | onAddOrDeleteOp(db); |
Louis Begin | a9c21c6 | 2016-08-15 15:18:41 -0700 | [diff] [blame] | 302 | reloadLauncherIfExternal(); |
| 303 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 304 | return count; |
| 305 | } |
| 306 | |
| 307 | @Override |
| 308 | public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { |
Sunny Goyal | d3849d1 | 2015-10-29 10:28:32 -0700 | [diff] [blame] | 309 | createDbIfNotExists(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 310 | SqlArguments args = new SqlArguments(uri, selection, selectionArgs); |
| 311 | |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 312 | addModifiedTime(values); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 313 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 314 | int count = db.update(args.table, values, args.where, args.args); |
Sunny Goyal | d106418 | 2015-08-13 12:08:30 -0700 | [diff] [blame] | 315 | reloadLauncherIfExternal(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 316 | return count; |
| 317 | } |
| 318 | |
Sunny Goyal | 7779d62 | 2015-06-11 16:18:39 -0700 | [diff] [blame] | 319 | @Override |
Tony Wickham | 827cef2 | 2016-03-17 15:39:39 -0700 | [diff] [blame] | 320 | public Bundle call(String method, final String arg, final Bundle extras) { |
Sunny Goyal | 7779d62 | 2015-06-11 16:18:39 -0700 | [diff] [blame] | 321 | if (Binder.getCallingUid() != Process.myUid()) { |
| 322 | return null; |
| 323 | } |
Sunny Goyal | d3849d1 | 2015-10-29 10:28:32 -0700 | [diff] [blame] | 324 | createDbIfNotExists(); |
Sunny Goyal | 7779d62 | 2015-06-11 16:18:39 -0700 | [diff] [blame] | 325 | |
| 326 | switch (method) { |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 327 | case LauncherSettings.Settings.METHOD_CLEAR_EMPTY_DB_FLAG: { |
| 328 | clearFlagEmptyDbCreated(); |
| 329 | return null; |
| 330 | } |
| 331 | case LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS: { |
| 332 | Bundle result = new Bundle(); |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 333 | result.putIntArray(LauncherSettings.Settings.EXTRA_VALUE, deleteEmptyFolders() |
| 334 | .toArray()); |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 335 | return result; |
| 336 | } |
| 337 | case LauncherSettings.Settings.METHOD_NEW_ITEM_ID: { |
| 338 | Bundle result = new Bundle(); |
Tracy Zhou | 7df93d2 | 2020-01-27 13:44:06 -0800 | [diff] [blame] | 339 | result.putInt(LauncherSettings.Settings.EXTRA_VALUE, |
| 340 | mOpenHelper.generateNewItemId()); |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 341 | return result; |
| 342 | } |
| 343 | case LauncherSettings.Settings.METHOD_NEW_SCREEN_ID: { |
| 344 | Bundle result = new Bundle(); |
Tracy Zhou | 7df93d2 | 2020-01-27 13:44:06 -0800 | [diff] [blame] | 345 | result.putInt(LauncherSettings.Settings.EXTRA_VALUE, |
Andras Kloczl | 953eb80 | 2021-10-15 21:16:48 +0100 | [diff] [blame] | 346 | mOpenHelper.getNewScreenId()); |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 347 | return result; |
| 348 | } |
| 349 | case LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB: { |
Sunny Goyal | e05b08f | 2017-02-23 18:30:22 -0800 | [diff] [blame] | 350 | mOpenHelper.createEmptyDB(mOpenHelper.getWritableDatabase()); |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 351 | return null; |
| 352 | } |
Schneider Victor-tulias | fb252e7 | 2022-02-10 11:10:21 -0800 | [diff] [blame] | 353 | case LauncherSettings.Settings.METHOD_SET_USE_TEST_WORKSPACE_LAYOUT_FLAG: { |
Alex Chau | f8b79d4 | 2022-12-15 13:41:49 +0000 | [diff] [blame] | 354 | switch (arg) { |
| 355 | case LauncherSettings.Settings.ARG_DEFAULT_WORKSPACE_LAYOUT_TEST: |
| 356 | mDefaultWorkspaceLayoutOverride = TEST_WORKSPACE_LAYOUT_RES_XML; |
| 357 | break; |
| 358 | case LauncherSettings.Settings.ARG_DEFAULT_WORKSPACE_LAYOUT_TEST2: |
| 359 | mDefaultWorkspaceLayoutOverride = TEST2_WORKSPACE_LAYOUT_RES_XML; |
| 360 | break; |
Sebastian Franco | 239ae0a | 2023-03-03 15:56:52 -0800 | [diff] [blame] | 361 | case LauncherSettings.Settings.ARG_DEFAULT_WORKSPACE_LAYOUT_TAPL: |
| 362 | mDefaultWorkspaceLayoutOverride = TAPL_WORKSPACE_LAYOUT_RES_XML; |
| 363 | break; |
Alex Chau | f8b79d4 | 2022-12-15 13:41:49 +0000 | [diff] [blame] | 364 | default: |
| 365 | mDefaultWorkspaceLayoutOverride = 0; |
| 366 | break; |
| 367 | } |
Schneider Victor-tulias | fb252e7 | 2022-02-10 11:10:21 -0800 | [diff] [blame] | 368 | return null; |
| 369 | } |
| 370 | case LauncherSettings.Settings.METHOD_CLEAR_USE_TEST_WORKSPACE_LAYOUT_FLAG: { |
Alex Chau | f8b79d4 | 2022-12-15 13:41:49 +0000 | [diff] [blame] | 371 | mDefaultWorkspaceLayoutOverride = 0; |
Schneider Victor-tulias | fb252e7 | 2022-02-10 11:10:21 -0800 | [diff] [blame] | 372 | return null; |
| 373 | } |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 374 | case LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES: { |
| 375 | loadDefaultFavoritesIfNecessary(); |
| 376 | return null; |
| 377 | } |
Sunny Goyal | 55fddc8 | 2017-04-06 15:02:52 -0700 | [diff] [blame] | 378 | case LauncherSettings.Settings.METHOD_REMOVE_GHOST_WIDGETS: { |
| 379 | mOpenHelper.removeGhostWidgets(mOpenHelper.getWritableDatabase()); |
| 380 | return null; |
| 381 | } |
Sunny Goyal | 161a214 | 2018-10-29 14:02:20 -0700 | [diff] [blame] | 382 | case LauncherSettings.Settings.METHOD_NEW_TRANSACTION: { |
| 383 | Bundle result = new Bundle(); |
| 384 | result.putBinder(LauncherSettings.Settings.EXTRA_VALUE, |
| 385 | new SQLiteTransaction(mOpenHelper.getWritableDatabase())); |
| 386 | return result; |
| 387 | } |
Samuel Fufa | f667a13 | 2020-05-29 14:47:42 -0700 | [diff] [blame] | 388 | case LauncherSettings.Settings.METHOD_REFRESH_HOTSEAT_RESTORE_TABLE: { |
| 389 | mOpenHelper.mHotseatRestoreTableExists = tableExists( |
| 390 | mOpenHelper.getReadableDatabase(), Favorites.HYBRID_HOTSEAT_BACKUP_TABLE); |
| 391 | return null; |
| 392 | } |
Tracy Zhou | 7df93d2 | 2020-01-27 13:44:06 -0800 | [diff] [blame] | 393 | case LauncherSettings.Settings.METHOD_UPDATE_CURRENT_OPEN_HELPER: { |
Sunny Goyal | 278939f | 2021-07-28 14:35:47 -0700 | [diff] [blame] | 394 | Bundle result = new Bundle(); |
| 395 | result.putBoolean(LauncherSettings.Settings.EXTRA_VALUE, |
| 396 | prepForMigration( |
Alex Chau | fe9a07b | 2022-03-08 21:48:34 +0000 | [diff] [blame] | 397 | arg /* dbFile */, |
Sunny Goyal | 278939f | 2021-07-28 14:35:47 -0700 | [diff] [blame] | 398 | Favorites.TMP_TABLE, |
| 399 | () -> mOpenHelper, |
| 400 | () -> DatabaseHelper.createDatabaseHelper( |
| 401 | getContext(), true /* forMigration */))); |
| 402 | return result; |
Tracy Zhou | c000045 | 2020-03-17 18:28:38 -0700 | [diff] [blame] | 403 | } |
| 404 | case LauncherSettings.Settings.METHOD_PREP_FOR_PREVIEW: { |
Sunny Goyal | 278939f | 2021-07-28 14:35:47 -0700 | [diff] [blame] | 405 | Bundle result = new Bundle(); |
| 406 | result.putBoolean(LauncherSettings.Settings.EXTRA_VALUE, |
| 407 | prepForMigration( |
| 408 | arg /* dbFile */, |
| 409 | Favorites.PREVIEW_TABLE_NAME, |
| 410 | () -> DatabaseHelper.createDatabaseHelper( |
| 411 | getContext(), arg, true /* forMigration */), |
| 412 | () -> mOpenHelper)); |
| 413 | return result; |
Pinyao Ting | 96186af | 2020-07-20 11:03:39 -0700 | [diff] [blame] | 414 | } |
Sunny Goyal | 7779d62 | 2015-06-11 16:18:39 -0700 | [diff] [blame] | 415 | } |
| 416 | return null; |
| 417 | } |
| 418 | |
Tracy Zhou | 7df93d2 | 2020-01-27 13:44:06 -0800 | [diff] [blame] | 419 | private void onAddOrDeleteOp(SQLiteDatabase db) { |
Pinyao Ting | 26c4e23 | 2020-04-15 22:25:06 -0700 | [diff] [blame] | 420 | mOpenHelper.onAddOrDeleteOp(db); |
Tracy Zhou | 7df93d2 | 2020-01-27 13:44:06 -0800 | [diff] [blame] | 421 | } |
| 422 | |
Sunny Goyal | b1622cc | 2015-06-10 16:00:42 -0700 | [diff] [blame] | 423 | /** |
| 424 | * Deletes any empty folder from the DB. |
| 425 | * @return Ids of deleted folders. |
| 426 | */ |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 427 | private IntArray deleteEmptyFolders() { |
Sunny Goyal | b1622cc | 2015-06-10 16:00:42 -0700 | [diff] [blame] | 428 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
Sunny Goyal | dbfc901 | 2017-04-17 16:58:36 -0700 | [diff] [blame] | 429 | try (SQLiteTransaction t = new SQLiteTransaction(db)) { |
Sunny Goyal | b1622cc | 2015-06-10 16:00:42 -0700 | [diff] [blame] | 430 | // Select folders whose id do not match any container value. |
| 431 | String selection = LauncherSettings.Favorites.ITEM_TYPE + " = " |
| 432 | + LauncherSettings.Favorites.ITEM_TYPE_FOLDER + " AND " |
| 433 | + LauncherSettings.Favorites._ID + " NOT IN (SELECT " + |
| 434 | LauncherSettings.Favorites.CONTAINER + " FROM " |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 435 | + Favorites.TABLE_NAME + ")"; |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 436 | |
Alex Chau | ac03843 | 2021-11-04 16:49:59 +0000 | [diff] [blame] | 437 | IntArray folderIds = LauncherDbUtils.queryIntArray(false, db, Favorites.TABLE_NAME, |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 438 | Favorites._ID, selection, null, null); |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 439 | if (!folderIds.isEmpty()) { |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 440 | db.delete(Favorites.TABLE_NAME, Utilities.createDbSelectionQuery( |
Sunny Goyal | b1622cc | 2015-06-10 16:00:42 -0700 | [diff] [blame] | 441 | LauncherSettings.Favorites._ID, folderIds), null); |
| 442 | } |
Sunny Goyal | dbfc901 | 2017-04-17 16:58:36 -0700 | [diff] [blame] | 443 | t.commit(); |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 444 | return folderIds; |
Sunny Goyal | b1622cc | 2015-06-10 16:00:42 -0700 | [diff] [blame] | 445 | } catch (SQLException ex) { |
| 446 | Log.e(TAG, ex.getMessage(), ex); |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 447 | return new IntArray(); |
Sunny Goyal | b1622cc | 2015-06-10 16:00:42 -0700 | [diff] [blame] | 448 | } |
Sunny Goyal | b1622cc | 2015-06-10 16:00:42 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 451 | @Thunk static void addModifiedTime(ContentValues values) { |
Sunny Goyal | c593939 | 2018-12-07 11:43:47 -0800 | [diff] [blame] | 452 | values.put(LauncherSettings.Favorites.MODIFIED, System.currentTimeMillis()); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 453 | } |
| 454 | |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 455 | private void clearFlagEmptyDbCreated() { |
Stefan Andonian | 146701c | 2022-11-10 23:07:40 +0000 | [diff] [blame] | 456 | LauncherPrefs.getPrefs(getContext()).edit() |
Pinyao Ting | 96186af | 2020-07-20 11:03:39 -0700 | [diff] [blame] | 457 | .remove(mOpenHelper.getKey(EMPTY_DATABASE_CREATED)).commit(); |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Sunny Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 460 | /** |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 461 | * Loads the default workspace based on the following priority scheme: |
Sunny Goyal | b2d46ce | 2015-03-26 11:32:11 -0700 | [diff] [blame] | 462 | * 1) From the app restrictions |
| 463 | * 2) From a package provided by play store |
| 464 | * 3) From a partner configuration APK, already in the system image |
| 465 | * 4) The default configuration for the particular device |
Brian Muramatsu | 5524b49 | 2012-10-02 16:55:54 -0700 | [diff] [blame] | 466 | */ |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 467 | synchronized private void loadDefaultFavoritesIfNecessary() { |
Stefan Andonian | 146701c | 2022-11-10 23:07:40 +0000 | [diff] [blame] | 468 | SharedPreferences sp = LauncherPrefs.getPrefs(getContext()); |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 469 | |
Pinyao Ting | 96186af | 2020-07-20 11:03:39 -0700 | [diff] [blame] | 470 | if (sp.getBoolean(mOpenHelper.getKey(EMPTY_DATABASE_CREATED), false)) { |
Chris Wren | 5dee7af | 2013-12-20 17:22:11 -0500 | [diff] [blame] | 471 | Log.d(TAG, "loading default workspace"); |
Michael Jurka | 45355c4 | 2012-10-08 13:21:35 +0200 | [diff] [blame] | 472 | |
Sihua Ma | aa2b872 | 2022-10-25 15:17:58 -0700 | [diff] [blame] | 473 | LauncherWidgetHolder widgetHolder = mOpenHelper.newLauncherWidgetHolder(); |
Sihua Ma | cdb3c0a | 2023-02-13 15:12:32 -0800 | [diff] [blame] | 474 | try { |
| 475 | AutoInstallsLayout loader = createWorkspaceLoaderFromAppRestriction(widgetHolder); |
| 476 | if (loader == null) { |
| 477 | loader = AutoInstallsLayout.get(getContext(), widgetHolder, mOpenHelper); |
| 478 | } |
| 479 | if (loader == null) { |
| 480 | final Partner partner = Partner.get(getContext().getPackageManager()); |
| 481 | if (partner != null) { |
| 482 | int workspaceResId = partner.getXmlResId(RES_PARTNER_DEFAULT_LAYOUT); |
| 483 | if (workspaceResId != 0) { |
| 484 | loader = new DefaultLayoutParser(getContext(), widgetHolder, |
| 485 | mOpenHelper, partner.getResources(), workspaceResId); |
| 486 | } |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 487 | } |
Adam Cohen | 9b8f51f | 2014-05-30 15:34:09 -0700 | [diff] [blame] | 488 | } |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 489 | |
Sihua Ma | cdb3c0a | 2023-02-13 15:12:32 -0800 | [diff] [blame] | 490 | final boolean usingExternallyProvidedLayout = loader != null; |
| 491 | if (loader == null) { |
| 492 | loader = getDefaultLayoutParser(widgetHolder); |
| 493 | } |
Sunny Goyal | c6c8fef | 2015-03-04 09:51:18 -0800 | [diff] [blame] | 494 | |
Sihua Ma | cdb3c0a | 2023-02-13 15:12:32 -0800 | [diff] [blame] | 495 | // There might be some partially restored DB items, due to buggy restore logic in |
| 496 | // previous versions of launcher. |
Sunny Goyal | e05b08f | 2017-02-23 18:30:22 -0800 | [diff] [blame] | 497 | mOpenHelper.createEmptyDB(mOpenHelper.getWritableDatabase()); |
Sihua Ma | cdb3c0a | 2023-02-13 15:12:32 -0800 | [diff] [blame] | 498 | // Populate favorites table with initial favorites |
| 499 | if ((mOpenHelper.loadFavorites(mOpenHelper.getWritableDatabase(), loader) <= 0) |
| 500 | && usingExternallyProvidedLayout) { |
| 501 | // Unable to load external layout. Cleanup and load the internal layout. |
| 502 | mOpenHelper.createEmptyDB(mOpenHelper.getWritableDatabase()); |
| 503 | mOpenHelper.loadFavorites(mOpenHelper.getWritableDatabase(), |
| 504 | getDefaultLayoutParser(widgetHolder)); |
| 505 | } |
| 506 | clearFlagEmptyDbCreated(); |
| 507 | } finally { |
| 508 | widgetHolder.destroy(); |
Sunny Goyal | 9d21968 | 2014-10-23 14:21:02 -0700 | [diff] [blame] | 509 | } |
Michael Jurka | b85f8a4 | 2012-04-25 15:48:32 -0700 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | |
Sunny Goyal | b2d46ce | 2015-03-26 11:32:11 -0700 | [diff] [blame] | 513 | /** |
| 514 | * Creates workspace loader from an XML resource listed in the app restrictions. |
| 515 | * |
| 516 | * @return the loader if the restrictions are set and the resource exists; null otherwise. |
| 517 | */ |
Sihua Ma | aa2b872 | 2022-10-25 15:17:58 -0700 | [diff] [blame] | 518 | private AutoInstallsLayout createWorkspaceLoaderFromAppRestriction( |
| 519 | LauncherWidgetHolder widgetHolder) { |
Sunny Goyal | b2d46ce | 2015-03-26 11:32:11 -0700 | [diff] [blame] | 520 | Context ctx = getContext(); |
Pinyao Ting | 96186af | 2020-07-20 11:03:39 -0700 | [diff] [blame] | 521 | final String authority; |
| 522 | if (!TextUtils.isEmpty(mProviderAuthority)) { |
| 523 | authority = mProviderAuthority; |
| 524 | } else { |
| 525 | authority = Settings.Secure.getString(ctx.getContentResolver(), |
| 526 | "launcher3.layout.provider"); |
| 527 | } |
Sunny Goyal | c0f03d9 | 2019-03-22 14:13:36 -0700 | [diff] [blame] | 528 | if (TextUtils.isEmpty(authority)) { |
Sunny Goyal | 35ca873 | 2015-04-06 10:45:31 -0700 | [diff] [blame] | 529 | return null; |
| 530 | } |
Sunny Goyal | b2d46ce | 2015-03-26 11:32:11 -0700 | [diff] [blame] | 531 | |
Sunny Goyal | c0f03d9 | 2019-03-22 14:13:36 -0700 | [diff] [blame] | 532 | ProviderInfo pi = ctx.getPackageManager().resolveContentProvider(authority, 0); |
| 533 | if (pi == null) { |
| 534 | Log.e(TAG, "No provider found for authority " + authority); |
| 535 | return null; |
Sunny Goyal | b2d46ce | 2015-03-26 11:32:11 -0700 | [diff] [blame] | 536 | } |
Sunny Goyal | 4b5b0eb | 2019-12-02 17:00:35 -0800 | [diff] [blame] | 537 | Uri uri = getLayoutUri(authority, ctx); |
Sunny Goyal | c0f03d9 | 2019-03-22 14:13:36 -0700 | [diff] [blame] | 538 | try (InputStream in = ctx.getContentResolver().openInputStream(uri)) { |
| 539 | // Read the full xml so that we fail early in case of any IO error. |
| 540 | String layout = new String(IOUtils.toByteArray(in)); |
| 541 | XmlPullParser parser = Xml.newPullParser(); |
| 542 | parser.setInput(new StringReader(layout)); |
| 543 | |
| 544 | Log.d(TAG, "Loading layout from " + authority); |
Sihua Ma | aa2b872 | 2022-10-25 15:17:58 -0700 | [diff] [blame] | 545 | return new AutoInstallsLayout(ctx, widgetHolder, mOpenHelper, |
Sunny Goyal | c0f03d9 | 2019-03-22 14:13:36 -0700 | [diff] [blame] | 546 | ctx.getPackageManager().getResourcesForApplication(pi.applicationInfo), |
| 547 | () -> parser, AutoInstallsLayout.TAG_WORKSPACE); |
| 548 | } catch (Exception e) { |
| 549 | Log.e(TAG, "Error getting layout stream from: " + authority , e); |
| 550 | return null; |
| 551 | } |
Sunny Goyal | b2d46ce | 2015-03-26 11:32:11 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Sunny Goyal | 4b5b0eb | 2019-12-02 17:00:35 -0800 | [diff] [blame] | 554 | public static Uri getLayoutUri(String authority, Context ctx) { |
| 555 | InvariantDeviceProfile grid = LauncherAppState.getIDP(ctx); |
| 556 | return new Uri.Builder().scheme("content").authority(authority).path("launcher_layout") |
| 557 | .appendQueryParameter("version", "1") |
| 558 | .appendQueryParameter("gridWidth", Integer.toString(grid.numColumns)) |
| 559 | .appendQueryParameter("gridHeight", Integer.toString(grid.numRows)) |
Tony Wickham | b87f3cd | 2021-04-07 15:02:37 -0700 | [diff] [blame] | 560 | .appendQueryParameter("hotseatSize", Integer.toString(grid.numDatabaseHotseatIcons)) |
Sunny Goyal | 4b5b0eb | 2019-12-02 17:00:35 -0800 | [diff] [blame] | 561 | .build(); |
| 562 | } |
| 563 | |
Sihua Ma | aa2b872 | 2022-10-25 15:17:58 -0700 | [diff] [blame] | 564 | private DefaultLayoutParser getDefaultLayoutParser(LauncherWidgetHolder widgetHolder) { |
Adam Cohen | 2782449 | 2017-09-22 17:10:55 -0700 | [diff] [blame] | 565 | InvariantDeviceProfile idp = LauncherAppState.getIDP(getContext()); |
Alex Chau | f8b79d4 | 2022-12-15 13:41:49 +0000 | [diff] [blame] | 566 | int defaultLayout = mDefaultWorkspaceLayoutOverride > 0 |
| 567 | ? mDefaultWorkspaceLayoutOverride : idp.defaultLayoutId; |
Adam Cohen | 2782449 | 2017-09-22 17:10:55 -0700 | [diff] [blame] | 568 | |
Sunny Goyal | 337c81f | 2019-12-10 12:19:13 -0800 | [diff] [blame] | 569 | if (getContext().getSystemService(UserManager.class).isDemoUser() |
| 570 | && idp.demoModeLayoutId != 0) { |
Adam Cohen | 2782449 | 2017-09-22 17:10:55 -0700 | [diff] [blame] | 571 | defaultLayout = idp.demoModeLayoutId; |
| 572 | } |
| 573 | |
Sihua Ma | aa2b872 | 2022-10-25 15:17:58 -0700 | [diff] [blame] | 574 | return new DefaultLayoutParser(getContext(), widgetHolder, |
Sunny Goyal | 9d21968 | 2014-10-23 14:21:02 -0700 | [diff] [blame] | 575 | mOpenHelper, getContext().getResources(), defaultLayout); |
| 576 | } |
| 577 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 578 | static class SqlArguments { |
| 579 | public final String table; |
| 580 | public final String where; |
| 581 | public final String[] args; |
| 582 | |
| 583 | SqlArguments(Uri url, String where, String[] args) { |
| 584 | if (url.getPathSegments().size() == 1) { |
| 585 | this.table = url.getPathSegments().get(0); |
| 586 | this.where = where; |
| 587 | this.args = args; |
| 588 | } else if (url.getPathSegments().size() != 2) { |
| 589 | throw new IllegalArgumentException("Invalid URI: " + url); |
| 590 | } else if (!TextUtils.isEmpty(where)) { |
| 591 | throw new UnsupportedOperationException("WHERE clause not supported: " + url); |
| 592 | } else { |
| 593 | this.table = url.getPathSegments().get(0); |
Daniel Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 594 | this.where = "_id=" + ContentUris.parseId(url); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 595 | this.args = null; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | SqlArguments(Uri url) { |
| 600 | if (url.getPathSegments().size() == 1) { |
| 601 | table = url.getPathSegments().get(0); |
| 602 | where = null; |
| 603 | args = null; |
| 604 | } else { |
| 605 | throw new IllegalArgumentException("Invalid URI: " + url); |
| 606 | } |
| 607 | } |
| 608 | } |
Adam Cohen | 7296097 | 2014-01-15 18:13:55 -0800 | [diff] [blame] | 609 | } |