blob: 9abec505c7270a32f519ffc60e04f6388adc7348 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070019import android.annotation.TargetApi;
Mike Cleronb87bd162009-10-30 16:36:56 -070020import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.content.ComponentName;
Adam Cohen228da5a2011-07-27 22:23:47 -070022import android.content.ContentProvider;
Yura085c8532014-02-11 15:15:29 +000023import android.content.ContentProviderOperation;
24import android.content.ContentProviderResult;
Adam Cohen228da5a2011-07-27 22:23:47 -070025import android.content.ContentUris;
26import android.content.ContentValues;
Yura085c8532014-02-11 15:15:29 +000027import android.content.OperationApplicationException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.database.Cursor;
Adam Cohen228da5a2011-07-27 22:23:47 -070029import android.database.sqlite.SQLiteQueryBuilder;
Adam Cohen228da5a2011-07-27 22:23:47 -070030import android.net.Uri;
Sunny Goyal7779d622015-06-11 16:18:39 -070031import android.os.Binder;
Sunny Goyal22ca9ec2017-05-18 15:03:13 -070032import android.os.Build;
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070033import android.os.Bundle;
Sunny Goyal7779d622015-06-11 16:18:39 -070034import android.os.Process;
Adam Cohen228da5a2011-07-27 22:23:47 -070035import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.util.Log;
Adam Cohen228da5a2011-07-27 22:23:47 -070037
Sunny Goyal0fe505b2014-08-06 09:55:36 -070038import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070039import com.android.launcher3.config.FeatureFlags;
Sunny Goyal1ae46ca2023-04-10 15:28:59 -070040import com.android.launcher3.model.ModelDbController;
Sunny Goyaldbfc9012017-04-17 16:58:36 -070041import com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction;
Sihua Ma8bbfcb62022-11-08 16:46:07 -080042import com.android.launcher3.widget.LauncherWidgetHolder;
Michael Jurka8b805b12012-04-18 14:23:14 -070043
Hyunyoung Song6aa37292017-02-06 10:46:24 -080044import java.io.FileDescriptor;
45import java.io.PrintWriter;
Adam Cohen228da5a2011-07-27 22:23:47 -070046import java.util.ArrayList;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048public class LauncherProvider extends ContentProvider {
Sunny Goyalc74e4192015-09-08 14:01:03 -070049 private static final String TAG = "LauncherProvider";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050
Sunny Goyal4276e7b2018-11-26 23:44:41 -080051 public static final String AUTHORITY = BuildConfig.APPLICATION_ID + ".settings";
Winson Chung3d503fb2011-07-13 17:25:49 -070052
Hyunyoung Song6aa37292017-02-06 10:46:24 -080053 /**
54 * $ adb shell dumpsys activity provider com.android.launcher3
55 */
56 @Override
57 public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
58 LauncherAppState appState = LauncherAppState.getInstanceNoCreate();
59 if (appState == null || !appState.getModel().isModelLoaded()) {
60 return;
61 }
62 appState.getModel().dumpState("", fd, writer, args);
63 }
64
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065 @Override
66 public boolean onCreate() {
Zak Cohen3eeb41d2020-02-14 14:15:13 -080067 if (FeatureFlags.IS_STUDIO_BUILD) {
Sunny Goyale26d1002016-06-20 14:52:14 -070068 Log.d(TAG, "Launcher process started");
69 }
Sunny Goyalb5b55c82016-05-10 12:28:59 -070070
Sunny Goyalfdbef272017-02-01 12:52:54 -080071 // The content provider exists for the entire duration of the launcher main process and
Sunny Goyal66f2b352018-02-09 10:57:12 -080072 // is the first component to get created.
73 MainProcessInitializer.initialize(getContext().getApplicationContext());
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074 return true;
75 }
76
Sunny Goyalce953a32023-04-14 14:08:37 -070077 public ModelDbController getModelDbController() {
78 return LauncherAppState.getInstance(getContext()).getModel().getModelDbController();
79 }
80
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081 @Override
82 public String getType(Uri uri) {
83 SqlArguments args = new SqlArguments(uri, null, null);
84 if (TextUtils.isEmpty(args.where)) {
85 return "vnd.android.cursor.dir/" + args.table;
86 } else {
87 return "vnd.android.cursor.item/" + args.table;
88 }
89 }
90
91 @Override
92 public Cursor query(Uri uri, String[] projection, String selection,
93 String[] selectionArgs, String sortOrder) {
94
95 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
96 SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
97 qb.setTables(args.table);
98
Sunny Goyalce953a32023-04-14 14:08:37 -070099 Cursor result = getModelDbController().query(
Sunny Goyal1ae46ca2023-04-10 15:28:59 -0700100 args.table, projection, args.where, args.args, sortOrder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101 result.setNotificationUri(getContext().getContentResolver(), uri);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102 return result;
103 }
104
Sunny Goyald1064182015-08-13 12:08:30 -0700105 private void reloadLauncherIfExternal() {
Sunny Goyal8c48d8b2019-01-25 15:10:18 -0800106 if (Binder.getCallingPid() != Process.myPid()) {
Sunny Goyald1064182015-08-13 12:08:30 -0700107 LauncherAppState app = LauncherAppState.getInstanceNoCreate();
108 if (app != null) {
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800109 app.getModel().forceReload();
Sunny Goyald1064182015-08-13 12:08:30 -0700110 }
111 }
112 }
113
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114 @Override
115 public Uri insert(Uri uri, ContentValues initialValues) {
Sunny Goyald1064182015-08-13 12:08:30 -0700116 // In very limited cases, we support system|signature permission apps to modify the db.
117 if (Binder.getCallingPid() != Process.myPid()) {
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700118 if (!initializeExternalAdd(initialValues)) {
Adam Cohena043fa82014-07-23 14:49:38 -0700119 return null;
120 }
121 }
122
Sunny Goyal1ae46ca2023-04-10 15:28:59 -0700123 SqlArguments args = new SqlArguments(uri);
Sunny Goyalce953a32023-04-14 14:08:37 -0700124 int rowId = getModelDbController().insert(args.table, initialValues);
Sunny Goyale72f3d52015-03-02 14:24:21 -0800125 if (rowId < 0) return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
127 uri = ContentUris.withAppendedId(uri, rowId);
Sunny Goyal8c48d8b2019-01-25 15:10:18 -0800128 reloadLauncherIfExternal();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129 return uri;
130 }
131
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700132 private boolean initializeExternalAdd(ContentValues values) {
133 // 1. Ensure that externally added items have a valid item id
Sunny Goyalce953a32023-04-14 14:08:37 -0700134 int id = getModelDbController().generateNewItemId();
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700135 values.put(LauncherSettings.Favorites._ID, id);
136
137 // 2. In the case of an app widget, and if no app widget id is specified, we
138 // attempt allocate and bind the widget.
139 Integer itemType = values.getAsInteger(LauncherSettings.Favorites.ITEM_TYPE);
140 if (itemType != null &&
141 itemType.intValue() == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET &&
142 !values.containsKey(LauncherSettings.Favorites.APPWIDGET_ID)) {
143
144 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getContext());
145 ComponentName cn = ComponentName.unflattenFromString(
146 values.getAsString(Favorites.APPWIDGET_PROVIDER));
147
148 if (cn != null) {
Sunny Goyal1ae46ca2023-04-10 15:28:59 -0700149 LauncherWidgetHolder widgetHolder = LauncherWidgetHolder.newInstance(getContext());
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700150 try {
Sihua Maaa2b8722022-10-25 15:17:58 -0700151 int appWidgetId = widgetHolder.allocateAppWidgetId();
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700152 values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId);
153 if (!appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,cn)) {
Sihua Maaa2b8722022-10-25 15:17:58 -0700154 widgetHolder.deleteAppWidgetId(appWidgetId);
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700155 return false;
156 }
157 } catch (RuntimeException e) {
158 Log.e(TAG, "Failed to initialize external widget", e);
159 return false;
Sihua Maaa2b8722022-10-25 15:17:58 -0700160 } finally {
161 // Necessary to destroy the holder to free up possible activity context
162 widgetHolder.destroy();
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700163 }
164 } else {
165 return false;
166 }
167 }
168
Sunny Goyalc5939392018-12-07 11:43:47 -0800169 return true;
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700170 }
171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 @Override
173 public int bulkInsert(Uri uri, ContentValues[] values) {
174 SqlArguments args = new SqlArguments(uri);
Sunny Goyalce953a32023-04-14 14:08:37 -0700175 getModelDbController().bulkInsert(args.table, values);
Sunny Goyald1064182015-08-13 12:08:30 -0700176 reloadLauncherIfExternal();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 return values.length;
178 }
179
Sunny Goyal161a2142018-10-29 14:02:20 -0700180 @TargetApi(Build.VERSION_CODES.M)
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 @Override
Yura085c8532014-02-11 15:15:29 +0000182 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
183 throws OperationApplicationException {
Sunny Goyalce953a32023-04-14 14:08:37 -0700184 try (SQLiteTransaction t = getModelDbController().newTransaction()) {
Sunny Goyal161a2142018-10-29 14:02:20 -0700185 final int numOperations = operations.size();
186 final ContentProviderResult[] results = new ContentProviderResult[numOperations];
187 for (int i = 0; i < numOperations; i++) {
188 ContentProviderOperation op = operations.get(i);
189 results[i] = op.apply(this, results, i);
Sunny Goyal161a2142018-10-29 14:02:20 -0700190 }
Sunny Goyaldbfc9012017-04-17 16:58:36 -0700191 t.commit();
Sunny Goyald1064182015-08-13 12:08:30 -0700192 reloadLauncherIfExternal();
Sunny Goyal161a2142018-10-29 14:02:20 -0700193 return results;
Yura085c8532014-02-11 15:15:29 +0000194 }
195 }
196
197 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 public int delete(Uri uri, String selection, String[] selectionArgs) {
199 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
Sunny Goyalce953a32023-04-14 14:08:37 -0700200 int count = getModelDbController().delete(args.table, args.where, args.args);
Louis Begina9c21c62016-08-15 15:18:41 -0700201 if (count > 0) {
Louis Begina9c21c62016-08-15 15:18:41 -0700202 reloadLauncherIfExternal();
203 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204 return count;
205 }
206
207 @Override
208 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
209 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
Sunny Goyalce953a32023-04-14 14:08:37 -0700210 int count = getModelDbController().update(args.table, values, args.where, args.args);
Sunny Goyald1064182015-08-13 12:08:30 -0700211 reloadLauncherIfExternal();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212 return count;
213 }
214
Sunny Goyal7779d622015-06-11 16:18:39 -0700215 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700216 public Bundle call(String method, final String arg, final Bundle extras) {
Sunny Goyal7779d622015-06-11 16:18:39 -0700217 if (Binder.getCallingUid() != Process.myUid()) {
218 return null;
219 }
220
221 switch (method) {
Sunny Goyald2497482015-09-22 18:24:19 -0700222 case LauncherSettings.Settings.METHOD_CLEAR_EMPTY_DB_FLAG: {
Sunny Goyalce953a32023-04-14 14:08:37 -0700223 getModelDbController().clearEmptyDbFlag();
Sunny Goyald2497482015-09-22 18:24:19 -0700224 return null;
225 }
226 case LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS: {
227 Bundle result = new Bundle();
Sunny Goyal1ae46ca2023-04-10 15:28:59 -0700228 result.putIntArray(LauncherSettings.Settings.EXTRA_VALUE,
Sunny Goyalce953a32023-04-14 14:08:37 -0700229 getModelDbController().deleteEmptyFolders().toArray());
Sunny Goyald2497482015-09-22 18:24:19 -0700230 return result;
231 }
232 case LauncherSettings.Settings.METHOD_NEW_ITEM_ID: {
233 Bundle result = new Bundle();
Tracy Zhou7df93d22020-01-27 13:44:06 -0800234 result.putInt(LauncherSettings.Settings.EXTRA_VALUE,
Sunny Goyalce953a32023-04-14 14:08:37 -0700235 getModelDbController().generateNewItemId());
Sunny Goyald2497482015-09-22 18:24:19 -0700236 return result;
237 }
238 case LauncherSettings.Settings.METHOD_NEW_SCREEN_ID: {
239 Bundle result = new Bundle();
Tracy Zhou7df93d22020-01-27 13:44:06 -0800240 result.putInt(LauncherSettings.Settings.EXTRA_VALUE,
Sunny Goyalce953a32023-04-14 14:08:37 -0700241 getModelDbController().getNewScreenId());
Sunny Goyald2497482015-09-22 18:24:19 -0700242 return result;
243 }
244 case LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB: {
Sunny Goyalce953a32023-04-14 14:08:37 -0700245 getModelDbController().createEmptyDB();
Sunny Goyald2497482015-09-22 18:24:19 -0700246 return null;
247 }
248 case LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES: {
Sunny Goyalce953a32023-04-14 14:08:37 -0700249 getModelDbController().loadDefaultFavoritesIfNecessary();
Sunny Goyald2497482015-09-22 18:24:19 -0700250 return null;
251 }
Sunny Goyal55fddc82017-04-06 15:02:52 -0700252 case LauncherSettings.Settings.METHOD_REMOVE_GHOST_WIDGETS: {
Sunny Goyalce953a32023-04-14 14:08:37 -0700253 getModelDbController().removeGhostWidgets();
Sunny Goyal55fddc82017-04-06 15:02:52 -0700254 return null;
255 }
Sunny Goyal161a2142018-10-29 14:02:20 -0700256 case LauncherSettings.Settings.METHOD_NEW_TRANSACTION: {
257 Bundle result = new Bundle();
258 result.putBinder(LauncherSettings.Settings.EXTRA_VALUE,
Sunny Goyalce953a32023-04-14 14:08:37 -0700259 getModelDbController().newTransaction());
Sunny Goyal161a2142018-10-29 14:02:20 -0700260 return result;
261 }
Samuel Fufaf667a132020-05-29 14:47:42 -0700262 case LauncherSettings.Settings.METHOD_REFRESH_HOTSEAT_RESTORE_TABLE: {
Sunny Goyalce953a32023-04-14 14:08:37 -0700263 getModelDbController().refreshHotseatRestoreTable();
Samuel Fufaf667a132020-05-29 14:47:42 -0700264 return null;
265 }
Sunny Goyal7779d622015-06-11 16:18:39 -0700266 }
267 return null;
268 }
269
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270 static class SqlArguments {
271 public final String table;
272 public final String where;
273 public final String[] args;
274
275 SqlArguments(Uri url, String where, String[] args) {
276 if (url.getPathSegments().size() == 1) {
277 this.table = url.getPathSegments().get(0);
278 this.where = where;
279 this.args = args;
280 } else if (url.getPathSegments().size() != 2) {
281 throw new IllegalArgumentException("Invalid URI: " + url);
282 } else if (!TextUtils.isEmpty(where)) {
283 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
284 } else {
285 this.table = url.getPathSegments().get(0);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700286 this.where = "_id=" + ContentUris.parseId(url);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287 this.args = null;
288 }
289 }
290
291 SqlArguments(Uri url) {
292 if (url.getPathSegments().size() == 1) {
293 table = url.getPathSegments().get(0);
294 where = null;
295 args = null;
296 } else {
297 throw new IllegalArgumentException("Invalid URI: " + url);
298 }
299 }
300 }
Adam Cohen72960972014-01-15 18:13:55 -0800301}