blob: 440e1465eafc66894b4d49166d42bd34fd9d749c [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 Goyal7b9e28f2023-05-17 12:44:03 -070019import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
20import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;
21
Mike Cleronb87bd162009-10-30 16:36:56 -070022import android.appwidget.AppWidgetManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.content.ComponentName;
Adam Cohen228da5a2011-07-27 22:23:47 -070024import android.content.ContentProvider;
Adam Cohen228da5a2011-07-27 22:23:47 -070025import android.content.ContentUris;
26import android.content.ContentValues;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.database.Cursor;
Adam Cohen228da5a2011-07-27 22:23:47 -070028import android.database.sqlite.SQLiteQueryBuilder;
Adam Cohen228da5a2011-07-27 22:23:47 -070029import android.net.Uri;
Sunny Goyal7779d622015-06-11 16:18:39 -070030import android.os.Binder;
Sunny Goyal7779d622015-06-11 16:18:39 -070031import android.os.Process;
Adam Cohen228da5a2011-07-27 22:23:47 -070032import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.util.Log;
Adam Cohen228da5a2011-07-27 22:23:47 -070034
Sunny Goyal0fe505b2014-08-06 09:55:36 -070035import com.android.launcher3.LauncherSettings.Favorites;
Sunny Goyala9e2f5a2016-06-10 12:22:04 -070036import com.android.launcher3.config.FeatureFlags;
Sunny Goyal1ae46ca2023-04-10 15:28:59 -070037import com.android.launcher3.model.ModelDbController;
Sihua Ma8bbfcb62022-11-08 16:46:07 -080038import com.android.launcher3.widget.LauncherWidgetHolder;
Michael Jurka8b805b12012-04-18 14:23:14 -070039
Hyunyoung Song6aa37292017-02-06 10:46:24 -080040import java.io.FileDescriptor;
41import java.io.PrintWriter;
Sunny Goyal7b9e28f2023-05-17 12:44:03 -070042import java.util.function.ToIntFunction;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044public class LauncherProvider extends ContentProvider {
Sunny Goyalc74e4192015-09-08 14:01:03 -070045 private static final String TAG = "LauncherProvider";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046
Sunny Goyal4276e7b2018-11-26 23:44:41 -080047 public static final String AUTHORITY = BuildConfig.APPLICATION_ID + ".settings";
Winson Chung3d503fb2011-07-13 17:25:49 -070048
Hyunyoung Song6aa37292017-02-06 10:46:24 -080049 /**
50 * $ adb shell dumpsys activity provider com.android.launcher3
51 */
52 @Override
53 public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
54 LauncherAppState appState = LauncherAppState.getInstanceNoCreate();
55 if (appState == null || !appState.getModel().isModelLoaded()) {
56 return;
57 }
58 appState.getModel().dumpState("", fd, writer, args);
59 }
60
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061 @Override
62 public boolean onCreate() {
Zak Cohen3eeb41d2020-02-14 14:15:13 -080063 if (FeatureFlags.IS_STUDIO_BUILD) {
Sunny Goyale26d1002016-06-20 14:52:14 -070064 Log.d(TAG, "Launcher process started");
65 }
Sunny Goyalb5b55c82016-05-10 12:28:59 -070066
Sunny Goyalfdbef272017-02-01 12:52:54 -080067 // The content provider exists for the entire duration of the launcher main process and
Sunny Goyal66f2b352018-02-09 10:57:12 -080068 // is the first component to get created.
69 MainProcessInitializer.initialize(getContext().getApplicationContext());
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070 return true;
71 }
72
73 @Override
74 public String getType(Uri uri) {
75 SqlArguments args = new SqlArguments(uri, null, null);
76 if (TextUtils.isEmpty(args.where)) {
77 return "vnd.android.cursor.dir/" + args.table;
78 } else {
79 return "vnd.android.cursor.item/" + args.table;
80 }
81 }
82
83 @Override
84 public Cursor query(Uri uri, String[] projection, String selection,
85 String[] selectionArgs, String sortOrder) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
87 SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
88 qb.setTables(args.table);
89
Sunny Goyal7b9e28f2023-05-17 12:44:03 -070090 Cursor[] result = new Cursor[1];
91 executeControllerTask(controller -> {
92 result[0] = controller.query(args.table, projection, args.where, args.args, sortOrder);
93 return 0;
94 });
95 return result[0];
Sunny Goyald1064182015-08-13 12:08:30 -070096 }
97
The Android Open Source Project31dd5032009-03-03 19:32:27 -080098 @Override
Sunny Goyal7b9e28f2023-05-17 12:44:03 -070099 public Uri insert(Uri uri, ContentValues values) {
100 int rowId = executeControllerTask(controller -> {
101 // 1. Ensure that externally added items have a valid item id
102 int id = controller.generateNewItemId();
103 values.put(LauncherSettings.Favorites._ID, id);
Adam Cohena043fa82014-07-23 14:49:38 -0700104
Sunny Goyal7b9e28f2023-05-17 12:44:03 -0700105 // 2. In the case of an app widget, and if no app widget id is specified, we
106 // attempt allocate and bind the widget.
107 Integer itemType = values.getAsInteger(Favorites.ITEM_TYPE);
108 if (itemType != null
109 && itemType.intValue() == Favorites.ITEM_TYPE_APPWIDGET
110 && !values.containsKey(Favorites.APPWIDGET_ID)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111
Sunny Goyal7b9e28f2023-05-17 12:44:03 -0700112 ComponentName cn = ComponentName.unflattenFromString(
113 values.getAsString(Favorites.APPWIDGET_PROVIDER));
114 if (cn == null) {
115 return 0;
116 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117
Sunny Goyal1ae46ca2023-04-10 15:28:59 -0700118 LauncherWidgetHolder widgetHolder = LauncherWidgetHolder.newInstance(getContext());
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700119 try {
Sihua Maaa2b8722022-10-25 15:17:58 -0700120 int appWidgetId = widgetHolder.allocateAppWidgetId();
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700121 values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId);
Sunny Goyal7b9e28f2023-05-17 12:44:03 -0700122 if (!AppWidgetManager.getInstance(getContext())
123 .bindAppWidgetIdIfAllowed(appWidgetId, cn)) {
Sihua Maaa2b8722022-10-25 15:17:58 -0700124 widgetHolder.deleteAppWidgetId(appWidgetId);
Sunny Goyal7b9e28f2023-05-17 12:44:03 -0700125 return 0;
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700126 }
127 } catch (RuntimeException e) {
128 Log.e(TAG, "Failed to initialize external widget", e);
Sunny Goyal7b9e28f2023-05-17 12:44:03 -0700129 return 0;
Sihua Maaa2b8722022-10-25 15:17:58 -0700130 } finally {
131 // Necessary to destroy the holder to free up possible activity context
132 widgetHolder.destroy();
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700133 }
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700134 }
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700135
Sunny Goyal7b9e28f2023-05-17 12:44:03 -0700136 SqlArguments args = new SqlArguments(uri);
137 return controller.insert(args.table, values);
138 });
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700139
Sunny Goyal7b9e28f2023-05-17 12:44:03 -0700140 return rowId < 0 ? null : ContentUris.withAppendedId(uri, rowId);
Yura085c8532014-02-11 15:15:29 +0000141 }
142
143 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 public int delete(Uri uri, String selection, String[] selectionArgs) {
145 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
Sunny Goyal7b9e28f2023-05-17 12:44:03 -0700146 return executeControllerTask(c -> c.delete(args.table, args.where, args.args));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 }
148
149 @Override
150 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
151 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
Sunny Goyal7b9e28f2023-05-17 12:44:03 -0700152 return executeControllerTask(c -> c.update(args.table, values, args.where, args.args));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 }
154
Sunny Goyal7b9e28f2023-05-17 12:44:03 -0700155 private int executeControllerTask(ToIntFunction<ModelDbController> task) {
156 if (Binder.getCallingPid() == Process.myPid()) {
157 throw new IllegalArgumentException("Same process should call model directly");
Sunny Goyal7779d622015-06-11 16:18:39 -0700158 }
Sunny Goyal7b9e28f2023-05-17 12:44:03 -0700159 try {
160 return MODEL_EXECUTOR.submit(() -> {
161 LauncherModel model = LauncherAppState.getInstance(getContext()).getModel();
162 int count = task.applyAsInt(model.getModelDbController());
163 if (count > 0) {
164 MAIN_EXECUTOR.submit(model::forceReload);
165 }
166 return count;
167 }).get();
168 } catch (Exception e) {
169 throw new IllegalStateException(e);
Sunny Goyal7779d622015-06-11 16:18:39 -0700170 }
Sunny Goyal7779d622015-06-11 16:18:39 -0700171 }
172
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 static class SqlArguments {
174 public final String table;
175 public final String where;
176 public final String[] args;
177
178 SqlArguments(Uri url, String where, String[] args) {
179 if (url.getPathSegments().size() == 1) {
180 this.table = url.getPathSegments().get(0);
181 this.where = where;
182 this.args = args;
183 } else if (url.getPathSegments().size() != 2) {
184 throw new IllegalArgumentException("Invalid URI: " + url);
185 } else if (!TextUtils.isEmpty(where)) {
186 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
187 } else {
188 this.table = url.getPathSegments().get(0);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700189 this.where = "_id=" + ContentUris.parseId(url);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 this.args = null;
191 }
192 }
193
194 SqlArguments(Uri url) {
195 if (url.getPathSegments().size() == 1) {
196 table = url.getPathSegments().get(0);
197 where = null;
198 args = null;
199 } else {
200 throw new IllegalArgumentException("Invalid URI: " + url);
201 }
202 }
203 }
Adam Cohen72960972014-01-15 18:13:55 -0800204}