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 | 7b9e28f | 2023-05-17 12:44:03 -0700 | [diff] [blame] | 19 | import static com.android.launcher3.util.Executors.MAIN_EXECUTOR; |
| 20 | import static com.android.launcher3.util.Executors.MODEL_EXECUTOR; |
| 21 | |
Mike Cleron | b87bd16 | 2009-10-30 16:36:56 -0700 | [diff] [blame] | 22 | import android.appwidget.AppWidgetManager; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 23 | import android.content.ComponentName; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 24 | import android.content.ContentProvider; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 25 | import android.content.ContentUris; |
| 26 | import android.content.ContentValues; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 27 | import android.database.Cursor; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 28 | import android.database.sqlite.SQLiteQueryBuilder; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 29 | import android.net.Uri; |
Sunny Goyal | 7779d62 | 2015-06-11 16:18:39 -0700 | [diff] [blame] | 30 | import android.os.Binder; |
Sunny Goyal | 7779d62 | 2015-06-11 16:18:39 -0700 | [diff] [blame] | 31 | import android.os.Process; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 32 | import android.text.TextUtils; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 33 | import android.util.Log; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 34 | |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 35 | import com.android.launcher3.LauncherSettings.Favorites; |
Sunny Goyal | a9e2f5a | 2016-06-10 12:22:04 -0700 | [diff] [blame] | 36 | import com.android.launcher3.config.FeatureFlags; |
Sunny Goyal | 1ae46ca | 2023-04-10 15:28:59 -0700 | [diff] [blame] | 37 | import com.android.launcher3.model.ModelDbController; |
Sihua Ma | 8bbfcb6 | 2022-11-08 16:46:07 -0800 | [diff] [blame] | 38 | import com.android.launcher3.widget.LauncherWidgetHolder; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 39 | |
Hyunyoung Song | 6aa3729 | 2017-02-06 10:46:24 -0800 | [diff] [blame] | 40 | import java.io.FileDescriptor; |
| 41 | import java.io.PrintWriter; |
Sunny Goyal | 7b9e28f | 2023-05-17 12:44:03 -0700 | [diff] [blame] | 42 | import java.util.function.ToIntFunction; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 43 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 44 | public class LauncherProvider extends ContentProvider { |
Sunny Goyal | c74e419 | 2015-09-08 14:01:03 -0700 | [diff] [blame] | 45 | private static final String TAG = "LauncherProvider"; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 46 | |
Sunny Goyal | 4276e7b | 2018-11-26 23:44:41 -0800 | [diff] [blame] | 47 | public static final String AUTHORITY = BuildConfig.APPLICATION_ID + ".settings"; |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 48 | |
Hyunyoung Song | 6aa3729 | 2017-02-06 10:46:24 -0800 | [diff] [blame] | 49 | /** |
| 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 Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 61 | @Override |
| 62 | public boolean onCreate() { |
Zak Cohen | 3eeb41d | 2020-02-14 14:15:13 -0800 | [diff] [blame] | 63 | if (FeatureFlags.IS_STUDIO_BUILD) { |
Sunny Goyal | e26d100 | 2016-06-20 14:52:14 -0700 | [diff] [blame] | 64 | Log.d(TAG, "Launcher process started"); |
| 65 | } |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 66 | |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 67 | // 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] | 68 | // is the first component to get created. |
| 69 | MainProcessInitializer.initialize(getContext().getApplicationContext()); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 70 | 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 Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 86 | SqlArguments args = new SqlArguments(uri, selection, selectionArgs); |
| 87 | SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); |
| 88 | qb.setTables(args.table); |
| 89 | |
Sunny Goyal | 7b9e28f | 2023-05-17 12:44:03 -0700 | [diff] [blame] | 90 | 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 Goyal | d106418 | 2015-08-13 12:08:30 -0700 | [diff] [blame] | 96 | } |
| 97 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 98 | @Override |
Sunny Goyal | 7b9e28f | 2023-05-17 12:44:03 -0700 | [diff] [blame] | 99 | 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 Cohen | a043fa8 | 2014-07-23 14:49:38 -0700 | [diff] [blame] | 104 | |
Sunny Goyal | 7b9e28f | 2023-05-17 12:44:03 -0700 | [diff] [blame] | 105 | // 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 Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 111 | |
Sunny Goyal | 7b9e28f | 2023-05-17 12:44:03 -0700 | [diff] [blame] | 112 | ComponentName cn = ComponentName.unflattenFromString( |
| 113 | values.getAsString(Favorites.APPWIDGET_PROVIDER)); |
| 114 | if (cn == null) { |
| 115 | return 0; |
| 116 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 117 | |
Sunny Goyal | 1ae46ca | 2023-04-10 15:28:59 -0700 | [diff] [blame] | 118 | LauncherWidgetHolder widgetHolder = LauncherWidgetHolder.newInstance(getContext()); |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 119 | try { |
Sihua Ma | aa2b872 | 2022-10-25 15:17:58 -0700 | [diff] [blame] | 120 | int appWidgetId = widgetHolder.allocateAppWidgetId(); |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 121 | values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId); |
Sunny Goyal | 7b9e28f | 2023-05-17 12:44:03 -0700 | [diff] [blame] | 122 | if (!AppWidgetManager.getInstance(getContext()) |
| 123 | .bindAppWidgetIdIfAllowed(appWidgetId, cn)) { |
Sihua Ma | aa2b872 | 2022-10-25 15:17:58 -0700 | [diff] [blame] | 124 | widgetHolder.deleteAppWidgetId(appWidgetId); |
Sunny Goyal | 7b9e28f | 2023-05-17 12:44:03 -0700 | [diff] [blame] | 125 | return 0; |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 126 | } |
| 127 | } catch (RuntimeException e) { |
| 128 | Log.e(TAG, "Failed to initialize external widget", e); |
Sunny Goyal | 7b9e28f | 2023-05-17 12:44:03 -0700 | [diff] [blame] | 129 | return 0; |
Sihua Ma | aa2b872 | 2022-10-25 15:17:58 -0700 | [diff] [blame] | 130 | } finally { |
| 131 | // Necessary to destroy the holder to free up possible activity context |
| 132 | widgetHolder.destroy(); |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 133 | } |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 134 | } |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 135 | |
Sunny Goyal | 7b9e28f | 2023-05-17 12:44:03 -0700 | [diff] [blame] | 136 | SqlArguments args = new SqlArguments(uri); |
| 137 | return controller.insert(args.table, values); |
| 138 | }); |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 139 | |
Sunny Goyal | 7b9e28f | 2023-05-17 12:44:03 -0700 | [diff] [blame] | 140 | return rowId < 0 ? null : ContentUris.withAppendedId(uri, rowId); |
Yura | 085c853 | 2014-02-11 15:15:29 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 144 | public int delete(Uri uri, String selection, String[] selectionArgs) { |
| 145 | SqlArguments args = new SqlArguments(uri, selection, selectionArgs); |
Sunny Goyal | 7b9e28f | 2023-05-17 12:44:03 -0700 | [diff] [blame] | 146 | return executeControllerTask(c -> c.delete(args.table, args.where, args.args)); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { |
| 151 | SqlArguments args = new SqlArguments(uri, selection, selectionArgs); |
Sunny Goyal | 7b9e28f | 2023-05-17 12:44:03 -0700 | [diff] [blame] | 152 | return executeControllerTask(c -> c.update(args.table, values, args.where, args.args)); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Sunny Goyal | 7b9e28f | 2023-05-17 12:44:03 -0700 | [diff] [blame] | 155 | private int executeControllerTask(ToIntFunction<ModelDbController> task) { |
| 156 | if (Binder.getCallingPid() == Process.myPid()) { |
| 157 | throw new IllegalArgumentException("Same process should call model directly"); |
Sunny Goyal | 7779d62 | 2015-06-11 16:18:39 -0700 | [diff] [blame] | 158 | } |
Sunny Goyal | 7b9e28f | 2023-05-17 12:44:03 -0700 | [diff] [blame] | 159 | 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 Goyal | 7779d62 | 2015-06-11 16:18:39 -0700 | [diff] [blame] | 170 | } |
Sunny Goyal | 7779d62 | 2015-06-11 16:18:39 -0700 | [diff] [blame] | 171 | } |
| 172 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 173 | 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 Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 189 | this.where = "_id=" + ContentUris.parseId(url); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 190 | 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 Cohen | 7296097 | 2014-01-15 18:13:55 -0800 | [diff] [blame] | 204 | } |