blob: d30d23ceda8074ca96325bb1cc558f9bcc735ec2 [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
Sunny Goyal1ae46ca2023-04-10 15:28:59 -070053 protected ModelDbController mModelDbController;
Schneider Victor-tuliasfb252e72022-02-10 11:10:21 -080054
Hyunyoung Song6aa37292017-02-06 10:46:24 -080055 /**
56 * $ adb shell dumpsys activity provider com.android.launcher3
57 */
58 @Override
59 public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
60 LauncherAppState appState = LauncherAppState.getInstanceNoCreate();
61 if (appState == null || !appState.getModel().isModelLoaded()) {
62 return;
63 }
64 appState.getModel().dumpState("", fd, writer, args);
65 }
66
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067 @Override
68 public boolean onCreate() {
Zak Cohen3eeb41d2020-02-14 14:15:13 -080069 if (FeatureFlags.IS_STUDIO_BUILD) {
Sunny Goyale26d1002016-06-20 14:52:14 -070070 Log.d(TAG, "Launcher process started");
71 }
Sunny Goyal1ae46ca2023-04-10 15:28:59 -070072 mModelDbController = new ModelDbController(getContext());
Sunny Goyalb5b55c82016-05-10 12:28:59 -070073
Sunny Goyalfdbef272017-02-01 12:52:54 -080074 // The content provider exists for the entire duration of the launcher main process and
Sunny Goyal66f2b352018-02-09 10:57:12 -080075 // is the first component to get created.
76 MainProcessInitializer.initialize(getContext().getApplicationContext());
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077 return true;
78 }
79
80 @Override
81 public String getType(Uri uri) {
82 SqlArguments args = new SqlArguments(uri, null, null);
83 if (TextUtils.isEmpty(args.where)) {
84 return "vnd.android.cursor.dir/" + args.table;
85 } else {
86 return "vnd.android.cursor.item/" + args.table;
87 }
88 }
89
90 @Override
91 public Cursor query(Uri uri, String[] projection, String selection,
92 String[] selectionArgs, String sortOrder) {
93
94 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
95 SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
96 qb.setTables(args.table);
97
Sunny Goyal1ae46ca2023-04-10 15:28:59 -070098 Cursor result = mModelDbController.query(
99 args.table, projection, args.where, args.args, sortOrder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100 result.setNotificationUri(getContext().getContentResolver(), uri);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101 return result;
102 }
103
Sunny Goyald1064182015-08-13 12:08:30 -0700104 private void reloadLauncherIfExternal() {
Sunny Goyal8c48d8b2019-01-25 15:10:18 -0800105 if (Binder.getCallingPid() != Process.myPid()) {
Sunny Goyald1064182015-08-13 12:08:30 -0700106 LauncherAppState app = LauncherAppState.getInstanceNoCreate();
107 if (app != null) {
Sunny Goyaldd96a5e2017-02-17 11:22:34 -0800108 app.getModel().forceReload();
Sunny Goyald1064182015-08-13 12:08:30 -0700109 }
110 }
111 }
112
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113 @Override
114 public Uri insert(Uri uri, ContentValues initialValues) {
Sunny Goyald1064182015-08-13 12:08:30 -0700115 // In very limited cases, we support system|signature permission apps to modify the db.
116 if (Binder.getCallingPid() != Process.myPid()) {
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700117 if (!initializeExternalAdd(initialValues)) {
Adam Cohena043fa82014-07-23 14:49:38 -0700118 return null;
119 }
120 }
121
Sunny Goyal1ae46ca2023-04-10 15:28:59 -0700122 SqlArguments args = new SqlArguments(uri);
123 int rowId = mModelDbController.insert(args.table, initialValues);
Sunny Goyale72f3d52015-03-02 14:24:21 -0800124 if (rowId < 0) return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
126 uri = ContentUris.withAppendedId(uri, rowId);
Sunny Goyal8c48d8b2019-01-25 15:10:18 -0800127 reloadLauncherIfExternal();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128 return uri;
129 }
130
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700131 private boolean initializeExternalAdd(ContentValues values) {
132 // 1. Ensure that externally added items have a valid item id
Sunny Goyal1ae46ca2023-04-10 15:28:59 -0700133 int id = mModelDbController.generateNewItemId();
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700134 values.put(LauncherSettings.Favorites._ID, id);
135
136 // 2. In the case of an app widget, and if no app widget id is specified, we
137 // attempt allocate and bind the widget.
138 Integer itemType = values.getAsInteger(LauncherSettings.Favorites.ITEM_TYPE);
139 if (itemType != null &&
140 itemType.intValue() == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET &&
141 !values.containsKey(LauncherSettings.Favorites.APPWIDGET_ID)) {
142
143 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getContext());
144 ComponentName cn = ComponentName.unflattenFromString(
145 values.getAsString(Favorites.APPWIDGET_PROVIDER));
146
147 if (cn != null) {
Sunny Goyal1ae46ca2023-04-10 15:28:59 -0700148 LauncherWidgetHolder widgetHolder = LauncherWidgetHolder.newInstance(getContext());
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700149 try {
Sihua Maaa2b8722022-10-25 15:17:58 -0700150 int appWidgetId = widgetHolder.allocateAppWidgetId();
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700151 values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId);
152 if (!appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,cn)) {
Sihua Maaa2b8722022-10-25 15:17:58 -0700153 widgetHolder.deleteAppWidgetId(appWidgetId);
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700154 return false;
155 }
156 } catch (RuntimeException e) {
157 Log.e(TAG, "Failed to initialize external widget", e);
158 return false;
Sihua Maaa2b8722022-10-25 15:17:58 -0700159 } finally {
160 // Necessary to destroy the holder to free up possible activity context
161 widgetHolder.destroy();
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700162 }
163 } else {
164 return false;
165 }
166 }
167
Sunny Goyalc5939392018-12-07 11:43:47 -0800168 return true;
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700169 }
170
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 @Override
172 public int bulkInsert(Uri uri, ContentValues[] values) {
173 SqlArguments args = new SqlArguments(uri);
Sunny Goyal1ae46ca2023-04-10 15:28:59 -0700174 mModelDbController.bulkInsert(args.table, values);
Sunny Goyald1064182015-08-13 12:08:30 -0700175 reloadLauncherIfExternal();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 return values.length;
177 }
178
Sunny Goyal161a2142018-10-29 14:02:20 -0700179 @TargetApi(Build.VERSION_CODES.M)
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 @Override
Yura085c8532014-02-11 15:15:29 +0000181 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
182 throws OperationApplicationException {
Sunny Goyal1ae46ca2023-04-10 15:28:59 -0700183 try (SQLiteTransaction t = mModelDbController.newTransaction()) {
Sunny Goyal161a2142018-10-29 14:02:20 -0700184 final int numOperations = operations.size();
185 final ContentProviderResult[] results = new ContentProviderResult[numOperations];
186 for (int i = 0; i < numOperations; i++) {
187 ContentProviderOperation op = operations.get(i);
188 results[i] = op.apply(this, results, i);
Sunny Goyal161a2142018-10-29 14:02:20 -0700189 }
Sunny Goyaldbfc9012017-04-17 16:58:36 -0700190 t.commit();
Sunny Goyald1064182015-08-13 12:08:30 -0700191 reloadLauncherIfExternal();
Sunny Goyal161a2142018-10-29 14:02:20 -0700192 return results;
Yura085c8532014-02-11 15:15:29 +0000193 }
194 }
195
196 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 public int delete(Uri uri, String selection, String[] selectionArgs) {
198 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
Sunny Goyal1ae46ca2023-04-10 15:28:59 -0700199 int count = mModelDbController.delete(args.table, args.where, args.args);
Louis Begina9c21c62016-08-15 15:18:41 -0700200 if (count > 0) {
Louis Begina9c21c62016-08-15 15:18:41 -0700201 reloadLauncherIfExternal();
202 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203 return count;
204 }
205
206 @Override
207 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
208 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
Sunny Goyal1ae46ca2023-04-10 15:28:59 -0700209 int count = mModelDbController.update(args.table, values, args.where, args.args);
Sunny Goyald1064182015-08-13 12:08:30 -0700210 reloadLauncherIfExternal();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 return count;
212 }
213
Sunny Goyal7779d622015-06-11 16:18:39 -0700214 @Override
Tony Wickham827cef22016-03-17 15:39:39 -0700215 public Bundle call(String method, final String arg, final Bundle extras) {
Sunny Goyal7779d622015-06-11 16:18:39 -0700216 if (Binder.getCallingUid() != Process.myUid()) {
217 return null;
218 }
219
220 switch (method) {
Sunny Goyald2497482015-09-22 18:24:19 -0700221 case LauncherSettings.Settings.METHOD_CLEAR_EMPTY_DB_FLAG: {
Sunny Goyal1ae46ca2023-04-10 15:28:59 -0700222 mModelDbController.clearEmptyDbFlag();
Sunny Goyald2497482015-09-22 18:24:19 -0700223 return null;
224 }
225 case LauncherSettings.Settings.METHOD_DELETE_EMPTY_FOLDERS: {
226 Bundle result = new Bundle();
Sunny Goyal1ae46ca2023-04-10 15:28:59 -0700227 result.putIntArray(LauncherSettings.Settings.EXTRA_VALUE,
228 mModelDbController.deleteEmptyFolders().toArray());
Sunny Goyald2497482015-09-22 18:24:19 -0700229 return result;
230 }
231 case LauncherSettings.Settings.METHOD_NEW_ITEM_ID: {
232 Bundle result = new Bundle();
Tracy Zhou7df93d22020-01-27 13:44:06 -0800233 result.putInt(LauncherSettings.Settings.EXTRA_VALUE,
Sunny Goyal1ae46ca2023-04-10 15:28:59 -0700234 mModelDbController.generateNewItemId());
Sunny Goyald2497482015-09-22 18:24:19 -0700235 return result;
236 }
237 case LauncherSettings.Settings.METHOD_NEW_SCREEN_ID: {
238 Bundle result = new Bundle();
Tracy Zhou7df93d22020-01-27 13:44:06 -0800239 result.putInt(LauncherSettings.Settings.EXTRA_VALUE,
Sunny Goyal1ae46ca2023-04-10 15:28:59 -0700240 mModelDbController.getNewScreenId());
Sunny Goyald2497482015-09-22 18:24:19 -0700241 return result;
242 }
243 case LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB: {
Sunny Goyal1ae46ca2023-04-10 15:28:59 -0700244 mModelDbController.createEmptyDB();
Sunny Goyald2497482015-09-22 18:24:19 -0700245 return null;
246 }
247 case LauncherSettings.Settings.METHOD_LOAD_DEFAULT_FAVORITES: {
Sunny Goyal1ae46ca2023-04-10 15:28:59 -0700248 mModelDbController.loadDefaultFavoritesIfNecessary();
Sunny Goyald2497482015-09-22 18:24:19 -0700249 return null;
250 }
Sunny Goyal55fddc82017-04-06 15:02:52 -0700251 case LauncherSettings.Settings.METHOD_REMOVE_GHOST_WIDGETS: {
Sunny Goyal1ae46ca2023-04-10 15:28:59 -0700252 mModelDbController.removeGhostWidgets();
Sunny Goyal55fddc82017-04-06 15:02:52 -0700253 return null;
254 }
Sunny Goyal161a2142018-10-29 14:02:20 -0700255 case LauncherSettings.Settings.METHOD_NEW_TRANSACTION: {
256 Bundle result = new Bundle();
257 result.putBinder(LauncherSettings.Settings.EXTRA_VALUE,
Sunny Goyal1ae46ca2023-04-10 15:28:59 -0700258 mModelDbController.newTransaction());
Sunny Goyal161a2142018-10-29 14:02:20 -0700259 return result;
260 }
Samuel Fufaf667a132020-05-29 14:47:42 -0700261 case LauncherSettings.Settings.METHOD_REFRESH_HOTSEAT_RESTORE_TABLE: {
Sunny Goyal1ae46ca2023-04-10 15:28:59 -0700262 mModelDbController.refreshHotseatRestoreTable();
Samuel Fufaf667a132020-05-29 14:47:42 -0700263 return null;
264 }
Tracy Zhou7df93d22020-01-27 13:44:06 -0800265 case LauncherSettings.Settings.METHOD_UPDATE_CURRENT_OPEN_HELPER: {
Sunny Goyal278939f2021-07-28 14:35:47 -0700266 Bundle result = new Bundle();
267 result.putBoolean(LauncherSettings.Settings.EXTRA_VALUE,
Sunny Goyal1ae46ca2023-04-10 15:28:59 -0700268 mModelDbController.updateCurrentOpenHelper(arg /* dbFile */));
Sunny Goyal278939f2021-07-28 14:35:47 -0700269 return result;
Tracy Zhouc0000452020-03-17 18:28:38 -0700270 }
271 case LauncherSettings.Settings.METHOD_PREP_FOR_PREVIEW: {
Sunny Goyal278939f2021-07-28 14:35:47 -0700272 Bundle result = new Bundle();
273 result.putBoolean(LauncherSettings.Settings.EXTRA_VALUE,
Sunny Goyal1ae46ca2023-04-10 15:28:59 -0700274 mModelDbController.prepareForPreview(arg /* dbFile */));
Sunny Goyal278939f2021-07-28 14:35:47 -0700275 return result;
Pinyao Ting96186af2020-07-20 11:03:39 -0700276 }
Sunny Goyal7779d622015-06-11 16:18:39 -0700277 }
278 return null;
279 }
280
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281 static class SqlArguments {
282 public final String table;
283 public final String where;
284 public final String[] args;
285
286 SqlArguments(Uri url, String where, String[] args) {
287 if (url.getPathSegments().size() == 1) {
288 this.table = url.getPathSegments().get(0);
289 this.where = where;
290 this.args = args;
291 } else if (url.getPathSegments().size() != 2) {
292 throw new IllegalArgumentException("Invalid URI: " + url);
293 } else if (!TextUtils.isEmpty(where)) {
294 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
295 } else {
296 this.table = url.getPathSegments().get(0);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700297 this.where = "_id=" + ContentUris.parseId(url);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800298 this.args = null;
299 }
300 }
301
302 SqlArguments(Uri url) {
303 if (url.getPathSegments().size() == 1) {
304 table = url.getPathSegments().get(0);
305 where = null;
306 args = null;
307 } else {
308 throw new IllegalArgumentException("Invalid URI: " + url);
309 }
310 }
311 }
Adam Cohen72960972014-01-15 18:13:55 -0800312}