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 | 1ae46ca | 2023-04-10 15:28:59 -0700 | [diff] [blame] | 36 | import com.android.launcher3.model.ModelDbController; |
Sihua Ma | 8bbfcb6 | 2022-11-08 16:46:07 -0800 | [diff] [blame] | 37 | import com.android.launcher3.widget.LauncherWidgetHolder; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 38 | |
Hyunyoung Song | 6aa3729 | 2017-02-06 10:46:24 -0800 | [diff] [blame] | 39 | import java.io.FileDescriptor; |
| 40 | import java.io.PrintWriter; |
Sunny Goyal | 7b9e28f | 2023-05-17 12:44:03 -0700 | [diff] [blame] | 41 | import java.util.function.ToIntFunction; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 42 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 43 | public class LauncherProvider extends ContentProvider { |
Sunny Goyal | c74e419 | 2015-09-08 14:01:03 -0700 | [diff] [blame] | 44 | private static final String TAG = "LauncherProvider"; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 45 | |
Hyunyoung Song | 6aa3729 | 2017-02-06 10:46:24 -0800 | [diff] [blame] | 46 | /** |
| 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 Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 58 | @Override |
| 59 | public boolean onCreate() { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 60 | 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 Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 76 | SqlArguments args = new SqlArguments(uri, selection, selectionArgs); |
| 77 | SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); |
| 78 | qb.setTables(args.table); |
| 79 | |
Sunny Goyal | 7b9e28f | 2023-05-17 12:44:03 -0700 | [diff] [blame] | 80 | 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 Goyal | d106418 | 2015-08-13 12:08:30 -0700 | [diff] [blame] | 86 | } |
| 87 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 88 | @Override |
Sunny Goyal | 7b9e28f | 2023-05-17 12:44:03 -0700 | [diff] [blame] | 89 | 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 Cohen | a043fa8 | 2014-07-23 14:49:38 -0700 | [diff] [blame] | 94 | |
Sunny Goyal | 7b9e28f | 2023-05-17 12:44:03 -0700 | [diff] [blame] | 95 | // 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 Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 101 | |
Sunny Goyal | 7b9e28f | 2023-05-17 12:44:03 -0700 | [diff] [blame] | 102 | ComponentName cn = ComponentName.unflattenFromString( |
| 103 | values.getAsString(Favorites.APPWIDGET_PROVIDER)); |
| 104 | if (cn == null) { |
| 105 | return 0; |
| 106 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 107 | |
Sunny Goyal | 1ae46ca | 2023-04-10 15:28:59 -0700 | [diff] [blame] | 108 | LauncherWidgetHolder widgetHolder = LauncherWidgetHolder.newInstance(getContext()); |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 109 | try { |
Sihua Ma | aa2b872 | 2022-10-25 15:17:58 -0700 | [diff] [blame] | 110 | int appWidgetId = widgetHolder.allocateAppWidgetId(); |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 111 | values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId); |
Sunny Goyal | 7b9e28f | 2023-05-17 12:44:03 -0700 | [diff] [blame] | 112 | if (!AppWidgetManager.getInstance(getContext()) |
| 113 | .bindAppWidgetIdIfAllowed(appWidgetId, cn)) { |
Sihua Ma | aa2b872 | 2022-10-25 15:17:58 -0700 | [diff] [blame] | 114 | widgetHolder.deleteAppWidgetId(appWidgetId); |
Sunny Goyal | 7b9e28f | 2023-05-17 12:44:03 -0700 | [diff] [blame] | 115 | return 0; |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 116 | } |
| 117 | } catch (RuntimeException e) { |
| 118 | Log.e(TAG, "Failed to initialize external widget", e); |
Sunny Goyal | 7b9e28f | 2023-05-17 12:44:03 -0700 | [diff] [blame] | 119 | return 0; |
Sihua Ma | aa2b872 | 2022-10-25 15:17:58 -0700 | [diff] [blame] | 120 | } finally { |
| 121 | // Necessary to destroy the holder to free up possible activity context |
| 122 | widgetHolder.destroy(); |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 123 | } |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 124 | } |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 125 | |
Sunny Goyal | 7b9e28f | 2023-05-17 12:44:03 -0700 | [diff] [blame] | 126 | SqlArguments args = new SqlArguments(uri); |
| 127 | return controller.insert(args.table, values); |
| 128 | }); |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 129 | |
Sunny Goyal | 7b9e28f | 2023-05-17 12:44:03 -0700 | [diff] [blame] | 130 | return rowId < 0 ? null : ContentUris.withAppendedId(uri, rowId); |
Yura | 085c853 | 2014-02-11 15:15:29 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Override |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 134 | public int delete(Uri uri, String selection, String[] selectionArgs) { |
| 135 | SqlArguments args = new SqlArguments(uri, selection, selectionArgs); |
Sunny Goyal | 7b9e28f | 2023-05-17 12:44:03 -0700 | [diff] [blame] | 136 | 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] | 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { |
| 141 | SqlArguments args = new SqlArguments(uri, selection, selectionArgs); |
Sunny Goyal | 7b9e28f | 2023-05-17 12:44:03 -0700 | [diff] [blame] | 142 | 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] | 143 | } |
| 144 | |
Sunny Goyal | 7b9e28f | 2023-05-17 12:44:03 -0700 | [diff] [blame] | 145 | private int executeControllerTask(ToIntFunction<ModelDbController> task) { |
| 146 | if (Binder.getCallingPid() == Process.myPid()) { |
| 147 | throw new IllegalArgumentException("Same process should call model directly"); |
Sunny Goyal | 7779d62 | 2015-06-11 16:18:39 -0700 | [diff] [blame] | 148 | } |
Sunny Goyal | 7b9e28f | 2023-05-17 12:44:03 -0700 | [diff] [blame] | 149 | 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 Goyal | 7779d62 | 2015-06-11 16:18:39 -0700 | [diff] [blame] | 160 | } |
Sunny Goyal | 7779d62 | 2015-06-11 16:18:39 -0700 | [diff] [blame] | 161 | } |
| 162 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 163 | 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 Lehmann | c3a8040 | 2012-04-23 21:35:11 -0700 | [diff] [blame] | 179 | this.where = "_id=" + ContentUris.parseId(url); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 180 | 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 Cohen | 7296097 | 2014-01-15 18:13:55 -0800 | [diff] [blame] | 194 | } |