blob: 4e0ba62107759ad5ee52bf97914c8e426b26d628 [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 Goyal1ae46ca2023-04-10 15:28:59 -070036import com.android.launcher3.model.ModelDbController;
Sihua Ma8bbfcb62022-11-08 16:46:07 -080037import com.android.launcher3.widget.LauncherWidgetHolder;
Michael Jurka8b805b12012-04-18 14:23:14 -070038
Hyunyoung Song6aa37292017-02-06 10:46:24 -080039import java.io.FileDescriptor;
40import java.io.PrintWriter;
Sunny Goyal7b9e28f2023-05-17 12:44:03 -070041import java.util.function.ToIntFunction;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043public class LauncherProvider extends ContentProvider {
Sunny Goyalc74e4192015-09-08 14:01:03 -070044 private static final String TAG = "LauncherProvider";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045
Hyunyoung Song6aa37292017-02-06 10:46:24 -080046 /**
47 * $ adb shell dumpsys activity provider com.android.launcher3
48 */
49 @Override
50 public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
51 LauncherAppState appState = LauncherAppState.getInstanceNoCreate();
52 if (appState == null || !appState.getModel().isModelLoaded()) {
53 return;
54 }
55 appState.getModel().dumpState("", fd, writer, args);
56 }
57
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058 @Override
59 public boolean onCreate() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060 return true;
61 }
62
63 @Override
64 public String getType(Uri uri) {
65 SqlArguments args = new SqlArguments(uri, null, null);
66 if (TextUtils.isEmpty(args.where)) {
67 return "vnd.android.cursor.dir/" + args.table;
68 } else {
69 return "vnd.android.cursor.item/" + args.table;
70 }
71 }
72
73 @Override
74 public Cursor query(Uri uri, String[] projection, String selection,
75 String[] selectionArgs, String sortOrder) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
77 SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
78 qb.setTables(args.table);
79
Sunny Goyal7b9e28f2023-05-17 12:44:03 -070080 Cursor[] result = new Cursor[1];
81 executeControllerTask(controller -> {
82 result[0] = controller.query(args.table, projection, args.where, args.args, sortOrder);
83 return 0;
84 });
85 return result[0];
Sunny Goyald1064182015-08-13 12:08:30 -070086 }
87
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088 @Override
Sunny Goyal7b9e28f2023-05-17 12:44:03 -070089 public Uri insert(Uri uri, ContentValues values) {
90 int rowId = executeControllerTask(controller -> {
91 // 1. Ensure that externally added items have a valid item id
92 int id = controller.generateNewItemId();
93 values.put(LauncherSettings.Favorites._ID, id);
Adam Cohena043fa82014-07-23 14:49:38 -070094
Sunny Goyal7b9e28f2023-05-17 12:44:03 -070095 // 2. In the case of an app widget, and if no app widget id is specified, we
96 // attempt allocate and bind the widget.
97 Integer itemType = values.getAsInteger(Favorites.ITEM_TYPE);
98 if (itemType != null
99 && itemType.intValue() == Favorites.ITEM_TYPE_APPWIDGET
100 && !values.containsKey(Favorites.APPWIDGET_ID)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101
Sunny Goyal7b9e28f2023-05-17 12:44:03 -0700102 ComponentName cn = ComponentName.unflattenFromString(
103 values.getAsString(Favorites.APPWIDGET_PROVIDER));
104 if (cn == null) {
105 return 0;
106 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107
Sunny Goyal1ae46ca2023-04-10 15:28:59 -0700108 LauncherWidgetHolder widgetHolder = LauncherWidgetHolder.newInstance(getContext());
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700109 try {
Sihua Maaa2b8722022-10-25 15:17:58 -0700110 int appWidgetId = widgetHolder.allocateAppWidgetId();
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700111 values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId);
Sunny Goyal7b9e28f2023-05-17 12:44:03 -0700112 if (!AppWidgetManager.getInstance(getContext())
113 .bindAppWidgetIdIfAllowed(appWidgetId, cn)) {
Sihua Maaa2b8722022-10-25 15:17:58 -0700114 widgetHolder.deleteAppWidgetId(appWidgetId);
Sunny Goyal7b9e28f2023-05-17 12:44:03 -0700115 return 0;
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700116 }
117 } catch (RuntimeException e) {
118 Log.e(TAG, "Failed to initialize external widget", e);
Sunny Goyal7b9e28f2023-05-17 12:44:03 -0700119 return 0;
Sihua Maaa2b8722022-10-25 15:17:58 -0700120 } finally {
121 // Necessary to destroy the holder to free up possible activity context
122 widgetHolder.destroy();
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700123 }
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700124 }
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700125
Sunny Goyal7b9e28f2023-05-17 12:44:03 -0700126 SqlArguments args = new SqlArguments(uri);
127 return controller.insert(args.table, values);
128 });
Sunny Goyalb5b55c82016-05-10 12:28:59 -0700129
Sunny Goyal7b9e28f2023-05-17 12:44:03 -0700130 return rowId < 0 ? null : ContentUris.withAppendedId(uri, rowId);
Yura085c8532014-02-11 15:15:29 +0000131 }
132
133 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134 public int delete(Uri uri, String selection, String[] selectionArgs) {
135 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
Sunny Goyal7b9e28f2023-05-17 12:44:03 -0700136 return executeControllerTask(c -> c.delete(args.table, args.where, args.args));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 }
138
139 @Override
140 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
141 SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
Sunny Goyal7b9e28f2023-05-17 12:44:03 -0700142 return executeControllerTask(c -> c.update(args.table, values, args.where, args.args));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 }
144
Sunny Goyal7b9e28f2023-05-17 12:44:03 -0700145 private int executeControllerTask(ToIntFunction<ModelDbController> task) {
146 if (Binder.getCallingPid() == Process.myPid()) {
147 throw new IllegalArgumentException("Same process should call model directly");
Sunny Goyal7779d622015-06-11 16:18:39 -0700148 }
Sunny Goyal7b9e28f2023-05-17 12:44:03 -0700149 try {
150 return MODEL_EXECUTOR.submit(() -> {
151 LauncherModel model = LauncherAppState.getInstance(getContext()).getModel();
152 int count = task.applyAsInt(model.getModelDbController());
153 if (count > 0) {
154 MAIN_EXECUTOR.submit(model::forceReload);
155 }
156 return count;
157 }).get();
158 } catch (Exception e) {
159 throw new IllegalStateException(e);
Sunny Goyal7779d622015-06-11 16:18:39 -0700160 }
Sunny Goyal7779d622015-06-11 16:18:39 -0700161 }
162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 static class SqlArguments {
164 public final String table;
165 public final String where;
166 public final String[] args;
167
168 SqlArguments(Uri url, String where, String[] args) {
169 if (url.getPathSegments().size() == 1) {
170 this.table = url.getPathSegments().get(0);
171 this.where = where;
172 this.args = args;
173 } else if (url.getPathSegments().size() != 2) {
174 throw new IllegalArgumentException("Invalid URI: " + url);
175 } else if (!TextUtils.isEmpty(where)) {
176 throw new UnsupportedOperationException("WHERE clause not supported: " + url);
177 } else {
178 this.table = url.getPathSegments().get(0);
Daniel Lehmannc3a80402012-04-23 21:35:11 -0700179 this.where = "_id=" + ContentUris.parseId(url);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 this.args = null;
181 }
182 }
183
184 SqlArguments(Uri url) {
185 if (url.getPathSegments().size() == 1) {
186 table = url.getPathSegments().get(0);
187 where = null;
188 args = null;
189 } else {
190 throw new IllegalArgumentException("Invalid URI: " + url);
191 }
192 }
193 }
Adam Cohen72960972014-01-15 18:13:55 -0800194}