Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
| 17 | package com.android.launcher3; |
| 18 | |
Nicolas Sleiman | 31e9fa4 | 2023-01-25 16:30:32 +0000 | [diff] [blame] | 19 | import static com.android.launcher3.LauncherSettings.Favorites.ITEM_TYPE_APPLICATION; |
| 20 | |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 21 | import android.content.ComponentName; |
| 22 | import android.content.ContentValues; |
| 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
| 25 | import android.content.pm.ActivityInfo; |
Nicolas Sleiman | 31e9fa4 | 2023-01-25 16:30:32 +0000 | [diff] [blame] | 26 | import android.content.pm.LauncherActivityInfo; |
Sunny Goyal | afaa8f0 | 2023-03-30 12:34:43 -0700 | [diff] [blame] | 27 | import android.content.pm.LauncherApps; |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 28 | import android.content.pm.PackageManager; |
| 29 | import android.content.res.Resources; |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 30 | import android.database.sqlite.SQLiteDatabase; |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 31 | import android.net.Uri; |
| 32 | import android.os.Bundle; |
Sunny Goyal | afaa8f0 | 2023-03-30 12:34:43 -0700 | [diff] [blame] | 33 | import android.os.Process; |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 34 | import android.text.TextUtils; |
Rajeev Kumar | 26453a2 | 2017-06-09 16:02:25 -0700 | [diff] [blame] | 35 | import android.util.ArrayMap; |
Sunny Goyal | 0d74231 | 2019-03-04 20:22:26 -0800 | [diff] [blame] | 36 | import android.util.AttributeSet; |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 37 | import android.util.Log; |
Sunny Goyal | 0d74231 | 2019-03-04 20:22:26 -0800 | [diff] [blame] | 38 | import android.util.Xml; |
Sunny Goyal | 36b5422 | 2018-07-10 13:50:50 -0700 | [diff] [blame] | 39 | |
Samuel Fufa | ca37b8a | 2019-08-19 17:04:36 -0700 | [diff] [blame] | 40 | import androidx.annotation.Nullable; |
Stefan Andonian | 7fcee91 | 2023-04-07 17:56:29 +0000 | [diff] [blame] | 41 | import androidx.annotation.WorkerThread; |
Samuel Fufa | ca37b8a | 2019-08-19 17:04:36 -0700 | [diff] [blame] | 42 | |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 43 | import com.android.launcher3.LauncherProvider.SqlArguments; |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 44 | import com.android.launcher3.LauncherSettings.Favorites; |
Nicolas Sleiman | 31e9fa4 | 2023-01-25 16:30:32 +0000 | [diff] [blame] | 45 | import com.android.launcher3.model.data.AppInfo; |
Sunny Goyal | e396abf | 2020-04-06 15:11:17 -0700 | [diff] [blame] | 46 | import com.android.launcher3.model.data.LauncherAppWidgetInfo; |
| 47 | import com.android.launcher3.model.data.WorkspaceItemInfo; |
Nicolas Sleiman | 31e9fa4 | 2023-01-25 16:30:32 +0000 | [diff] [blame] | 48 | import com.android.launcher3.pm.UserCache; |
Samuel Fufa | ca37b8a | 2019-08-19 17:04:36 -0700 | [diff] [blame] | 49 | import com.android.launcher3.qsb.QsbContainerView; |
Sunny Goyal | afaa8f0 | 2023-03-30 12:34:43 -0700 | [diff] [blame] | 50 | import com.android.launcher3.shortcuts.ShortcutKey; |
Nicolas Sleiman | 31e9fa4 | 2023-01-25 16:30:32 +0000 | [diff] [blame] | 51 | import com.android.launcher3.uioverrides.ApiWrapper; |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 52 | import com.android.launcher3.util.IntArray; |
Sunny Goyal | 3e58eea | 2022-11-14 14:30:07 -0800 | [diff] [blame] | 53 | import com.android.launcher3.util.Partner; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 54 | import com.android.launcher3.util.Thunk; |
Sihua Ma | 8bbfcb6 | 2022-11-08 16:46:07 -0800 | [diff] [blame] | 55 | import com.android.launcher3.widget.LauncherWidgetHolder; |
Sunny Goyal | 36b5422 | 2018-07-10 13:50:50 -0700 | [diff] [blame] | 56 | |
| 57 | import org.xmlpull.v1.XmlPullParser; |
| 58 | import org.xmlpull.v1.XmlPullParserException; |
| 59 | |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 60 | import java.io.IOException; |
Sunny Goyal | afaa8f0 | 2023-03-30 12:34:43 -0700 | [diff] [blame] | 61 | import java.util.Collections; |
Sunny Goyal | b2d46ce | 2015-03-26 11:32:11 -0700 | [diff] [blame] | 62 | import java.util.Locale; |
Nicolas Sleiman | 31e9fa4 | 2023-01-25 16:30:32 +0000 | [diff] [blame] | 63 | import java.util.Map; |
Sunny Goyal | c0f03d9 | 2019-03-22 14:13:36 -0700 | [diff] [blame] | 64 | import java.util.function.Supplier; |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 65 | |
| 66 | /** |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 67 | * Layout parsing code for auto installs layout |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 68 | */ |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 69 | public class AutoInstallsLayout { |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 70 | private static final String TAG = "AutoInstalls"; |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 71 | private static final boolean LOGD = false; |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 72 | |
| 73 | /** Marker action used to discover a package which defines launcher customization */ |
| 74 | static final String ACTION_LAUNCHER_CUSTOMIZATION = |
Sunny Goyal | 2233c88 | 2014-09-18 14:36:48 -0700 | [diff] [blame] | 75 | "android.autoinstalls.config.action.PLAY_AUTO_INSTALL"; |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 76 | |
Sunny Goyal | b2d46ce | 2015-03-26 11:32:11 -0700 | [diff] [blame] | 77 | /** |
| 78 | * Layout resource which also includes grid size and hotseat count, e.g., default_layout_6x6_h5 |
| 79 | */ |
| 80 | private static final String FORMATTED_LAYOUT_RES_WITH_HOSTEAT = "default_layout_%dx%d_h%s"; |
| 81 | private static final String FORMATTED_LAYOUT_RES = "default_layout_%dx%d"; |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 82 | private static final String LAYOUT_RES = "default_layout"; |
| 83 | |
Sunny Goyal | 1ae46ca | 2023-04-10 15:28:59 -0700 | [diff] [blame] | 84 | public static AutoInstallsLayout get(Context context, LauncherWidgetHolder appWidgetHolder, |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 85 | LayoutParserCallback callback) { |
Sunny Goyal | 3e58eea | 2022-11-14 14:30:07 -0800 | [diff] [blame] | 86 | Partner partner = Partner.get(context.getPackageManager(), ACTION_LAUNCHER_CUSTOMIZATION); |
| 87 | if (partner == null) { |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 88 | return null; |
| 89 | } |
Sunny Goyal | 87f784c | 2017-01-11 10:48:34 -0800 | [diff] [blame] | 90 | InvariantDeviceProfile grid = LauncherAppState.getIDP(context); |
Sunny Goyal | b2d46ce | 2015-03-26 11:32:11 -0700 | [diff] [blame] | 91 | |
| 92 | // Try with grid size and hotseat count |
| 93 | String layoutName = String.format(Locale.ENGLISH, FORMATTED_LAYOUT_RES_WITH_HOSTEAT, |
Tony Wickham | b87f3cd | 2021-04-07 15:02:37 -0700 | [diff] [blame] | 94 | grid.numColumns, grid.numRows, grid.numDatabaseHotseatIcons); |
Sunny Goyal | 3e58eea | 2022-11-14 14:30:07 -0800 | [diff] [blame] | 95 | int layoutId = partner.getXmlResId(layoutName); |
Sunny Goyal | b2d46ce | 2015-03-26 11:32:11 -0700 | [diff] [blame] | 96 | |
| 97 | // Try with only grid size |
| 98 | if (layoutId == 0) { |
| 99 | Log.d(TAG, "Formatted layout: " + layoutName |
| 100 | + " not found. Trying layout without hosteat"); |
| 101 | layoutName = String.format(Locale.ENGLISH, FORMATTED_LAYOUT_RES, |
Samuel Fufa | ca37b8a | 2019-08-19 17:04:36 -0700 | [diff] [blame] | 102 | grid.numColumns, grid.numRows); |
Sunny Goyal | 3e58eea | 2022-11-14 14:30:07 -0800 | [diff] [blame] | 103 | layoutId = partner.getXmlResId(layoutName); |
Sunny Goyal | b2d46ce | 2015-03-26 11:32:11 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | // Try the default layout |
| 107 | if (layoutId == 0) { |
| 108 | Log.d(TAG, "Formatted layout: " + layoutName + " not found. Trying the default layout"); |
Sunny Goyal | 3e58eea | 2022-11-14 14:30:07 -0800 | [diff] [blame] | 109 | layoutId = partner.getXmlResId(LAYOUT_RES); |
Sunny Goyal | b2d46ce | 2015-03-26 11:32:11 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 112 | if (layoutId == 0) { |
Sunny Goyal | 3e58eea | 2022-11-14 14:30:07 -0800 | [diff] [blame] | 113 | Log.e(TAG, "Layout definition not found in package: " + partner.getPackageName()); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 114 | return null; |
| 115 | } |
Sunny Goyal | 3e58eea | 2022-11-14 14:30:07 -0800 | [diff] [blame] | 116 | return new AutoInstallsLayout(context, appWidgetHolder, callback, partner.getResources(), |
| 117 | layoutId, TAG_WORKSPACE); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | // Object Tags |
Sunny Goyal | b564efb | 2015-01-23 13:45:20 -0800 | [diff] [blame] | 121 | private static final String TAG_INCLUDE = "include"; |
Sunny Goyal | c0f03d9 | 2019-03-22 14:13:36 -0700 | [diff] [blame] | 122 | public static final String TAG_WORKSPACE = "workspace"; |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 123 | private static final String TAG_APP_ICON = "appicon"; |
| 124 | private static final String TAG_AUTO_INSTALL = "autoinstall"; |
| 125 | private static final String TAG_FOLDER = "folder"; |
| 126 | private static final String TAG_APPWIDGET = "appwidget"; |
Samuel Fufa | ca37b8a | 2019-08-19 17:04:36 -0700 | [diff] [blame] | 127 | protected static final String TAG_SEARCH_WIDGET = "searchwidget"; |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 128 | private static final String TAG_SHORTCUT = "shortcut"; |
| 129 | private static final String TAG_EXTRA = "extra"; |
| 130 | |
| 131 | private static final String ATTR_CONTAINER = "container"; |
| 132 | private static final String ATTR_RANK = "rank"; |
| 133 | |
| 134 | private static final String ATTR_PACKAGE_NAME = "packageName"; |
| 135 | private static final String ATTR_CLASS_NAME = "className"; |
| 136 | private static final String ATTR_TITLE = "title"; |
Sunny Goyal | 55e2b16 | 2020-06-09 15:44:48 -0700 | [diff] [blame] | 137 | private static final String ATTR_TITLE_TEXT = "titleText"; |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 138 | private static final String ATTR_SCREEN = "screen"; |
Sunny Goyal | afaa8f0 | 2023-03-30 12:34:43 -0700 | [diff] [blame] | 139 | private static final String ATTR_SHORTCUT_ID = "shortcutId"; |
Sunny Goyal | 96a0963 | 2015-12-16 11:32:54 -0800 | [diff] [blame] | 140 | |
| 141 | // x and y can be specified as negative integers, in which case -1 represents the |
| 142 | // last row / column, -2 represents the second last, and so on. |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 143 | private static final String ATTR_X = "x"; |
| 144 | private static final String ATTR_Y = "y"; |
Sunny Goyal | 96a0963 | 2015-12-16 11:32:54 -0800 | [diff] [blame] | 145 | |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 146 | private static final String ATTR_SPAN_X = "spanX"; |
| 147 | private static final String ATTR_SPAN_Y = "spanY"; |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 148 | |
Sunny Goyal | b564efb | 2015-01-23 13:45:20 -0800 | [diff] [blame] | 149 | // Attrs for "Include" |
| 150 | private static final String ATTR_WORKSPACE = "workspace"; |
| 151 | |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 152 | // Style attrs -- "Extra" |
| 153 | private static final String ATTR_KEY = "key"; |
| 154 | private static final String ATTR_VALUE = "value"; |
| 155 | |
| 156 | private static final String HOTSEAT_CONTAINER_NAME = |
| 157 | Favorites.containerToString(Favorites.CONTAINER_HOTSEAT); |
| 158 | |
Sunny Goyal | afaa8f0 | 2023-03-30 12:34:43 -0700 | [diff] [blame] | 159 | protected final Context mContext; |
| 160 | protected final LauncherWidgetHolder mAppWidgetHolder; |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 161 | protected final LayoutParserCallback mCallback; |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 162 | |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 163 | protected final PackageManager mPackageManager; |
| 164 | protected final Resources mSourceRes; |
Sunny Goyal | c0f03d9 | 2019-03-22 14:13:36 -0700 | [diff] [blame] | 165 | protected final Supplier<XmlPullParser> mInitialLayoutSupplier; |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 166 | |
Sunny Goyal | bb011da | 2016-06-15 15:42:29 -0700 | [diff] [blame] | 167 | private final InvariantDeviceProfile mIdp; |
Sunny Goyal | 96a0963 | 2015-12-16 11:32:54 -0800 | [diff] [blame] | 168 | private final int mRowCount; |
| 169 | private final int mColumnCount; |
Nicolas Sleiman | 31e9fa4 | 2023-01-25 16:30:32 +0000 | [diff] [blame] | 170 | private final Map<String, LauncherActivityInfo> mActivityOverride; |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 171 | private final int[] mTemp = new int[2]; |
Samuel Fufa | ca37b8a | 2019-08-19 17:04:36 -0700 | [diff] [blame] | 172 | @Thunk |
| 173 | final ContentValues mValues; |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 174 | protected final String mRootTag; |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 175 | |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 176 | protected SQLiteDatabase mDb; |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 177 | |
Sihua Ma | aa2b872 | 2022-10-25 15:17:58 -0700 | [diff] [blame] | 178 | public AutoInstallsLayout(Context context, LauncherWidgetHolder appWidgetHolder, |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 179 | LayoutParserCallback callback, Resources res, |
| 180 | int layoutId, String rootTag) { |
Sihua Ma | aa2b872 | 2022-10-25 15:17:58 -0700 | [diff] [blame] | 181 | this(context, appWidgetHolder, callback, res, () -> res.getXml(layoutId), rootTag); |
Sunny Goyal | c0f03d9 | 2019-03-22 14:13:36 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Sihua Ma | aa2b872 | 2022-10-25 15:17:58 -0700 | [diff] [blame] | 184 | public AutoInstallsLayout(Context context, LauncherWidgetHolder appWidgetHolder, |
Sunny Goyal | c0f03d9 | 2019-03-22 14:13:36 -0700 | [diff] [blame] | 185 | LayoutParserCallback callback, Resources res, |
| 186 | Supplier<XmlPullParser> initialLayoutSupplier, String rootTag) { |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 187 | mContext = context; |
Sihua Ma | aa2b872 | 2022-10-25 15:17:58 -0700 | [diff] [blame] | 188 | mAppWidgetHolder = appWidgetHolder; |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 189 | mCallback = callback; |
| 190 | |
| 191 | mPackageManager = context.getPackageManager(); |
| 192 | mValues = new ContentValues(); |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 193 | mRootTag = rootTag; |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 194 | |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 195 | mSourceRes = res; |
Sunny Goyal | c0f03d9 | 2019-03-22 14:13:36 -0700 | [diff] [blame] | 196 | mInitialLayoutSupplier = initialLayoutSupplier; |
Sunny Goyal | 96a0963 | 2015-12-16 11:32:54 -0800 | [diff] [blame] | 197 | |
Sunny Goyal | 87f784c | 2017-01-11 10:48:34 -0800 | [diff] [blame] | 198 | mIdp = LauncherAppState.getIDP(context); |
Sunny Goyal | bb011da | 2016-06-15 15:42:29 -0700 | [diff] [blame] | 199 | mRowCount = mIdp.numRows; |
| 200 | mColumnCount = mIdp.numColumns; |
Nicolas Sleiman | 31e9fa4 | 2023-01-25 16:30:32 +0000 | [diff] [blame] | 201 | mActivityOverride = ApiWrapper.getActivityOverrides(context); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 204 | /** |
| 205 | * Loads the layout in the db and returns the number of entries added on the desktop. |
| 206 | */ |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 207 | public int loadLayout(SQLiteDatabase db, IntArray screenIds) { |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 208 | mDb = db; |
| 209 | try { |
Sunny Goyal | c0f03d9 | 2019-03-22 14:13:36 -0700 | [diff] [blame] | 210 | return parseLayout(mInitialLayoutSupplier.get(), screenIds); |
Sameer Padala | 8fd7483 | 2014-09-08 16:00:29 -0700 | [diff] [blame] | 211 | } catch (Exception e) { |
Sunny Goyal | c0f03d9 | 2019-03-22 14:13:36 -0700 | [diff] [blame] | 212 | Log.e(TAG, "Error parsing layout: ", e); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 213 | return -1; |
| 214 | } |
| 215 | } |
| 216 | |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 217 | /** |
| 218 | * Parses the layout and returns the number of elements added on the homescreen. |
| 219 | */ |
Sunny Goyal | c0f03d9 | 2019-03-22 14:13:36 -0700 | [diff] [blame] | 220 | protected int parseLayout(XmlPullParser parser, IntArray screenIds) |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 221 | throws XmlPullParserException, IOException { |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 222 | beginDocument(parser, mRootTag); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 223 | final int depth = parser.getDepth(); |
| 224 | int type; |
Rajeev Kumar | 26453a2 | 2017-06-09 16:02:25 -0700 | [diff] [blame] | 225 | ArrayMap<String, TagParser> tagParserMap = getLayoutElementsMap(); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 226 | int count = 0; |
| 227 | |
| 228 | while (((type = parser.next()) != XmlPullParser.END_TAG || |
| 229 | parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) { |
| 230 | if (type != XmlPullParser.START_TAG) { |
| 231 | continue; |
| 232 | } |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 233 | count += parseAndAddNode(parser, tagParserMap, screenIds); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 234 | } |
| 235 | return count; |
| 236 | } |
| 237 | |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 238 | /** |
| 239 | * Parses container and screenId attribute from the current tag, and puts it in the out. |
| 240 | * @param out array of size 2. |
| 241 | */ |
Sunny Goyal | 0d74231 | 2019-03-04 20:22:26 -0800 | [diff] [blame] | 242 | protected void parseContainerAndScreen(XmlPullParser parser, int[] out) { |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 243 | if (HOTSEAT_CONTAINER_NAME.equals(getAttributeValue(parser, ATTR_CONTAINER))) { |
| 244 | out[0] = Favorites.CONTAINER_HOTSEAT; |
| 245 | // Hack: hotseat items are stored using screen ids |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 246 | out[1] = Integer.parseInt(getAttributeValue(parser, ATTR_RANK)); |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 247 | } else { |
| 248 | out[0] = Favorites.CONTAINER_DESKTOP; |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 249 | out[1] = Integer.parseInt(getAttributeValue(parser, ATTR_SCREEN)); |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Parses the current node and returns the number of elements added. |
| 255 | */ |
| 256 | protected int parseAndAddNode( |
Sunny Goyal | 0d74231 | 2019-03-04 20:22:26 -0800 | [diff] [blame] | 257 | XmlPullParser parser, ArrayMap<String, TagParser> tagParserMap, IntArray screenIds) |
Samuel Fufa | ca37b8a | 2019-08-19 17:04:36 -0700 | [diff] [blame] | 258 | throws XmlPullParserException, IOException { |
Sunny Goyal | b564efb | 2015-01-23 13:45:20 -0800 | [diff] [blame] | 259 | |
| 260 | if (TAG_INCLUDE.equals(parser.getName())) { |
| 261 | final int resId = getAttributeResourceValue(parser, ATTR_WORKSPACE, 0); |
| 262 | if (resId != 0) { |
| 263 | // recursively load some more favorites, why not? |
Sunny Goyal | c0f03d9 | 2019-03-22 14:13:36 -0700 | [diff] [blame] | 264 | return parseLayout(mSourceRes.getXml(resId), screenIds); |
Sunny Goyal | b564efb | 2015-01-23 13:45:20 -0800 | [diff] [blame] | 265 | } else { |
| 266 | return 0; |
| 267 | } |
| 268 | } |
| 269 | |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 270 | mValues.clear(); |
| 271 | parseContainerAndScreen(parser, mTemp); |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 272 | final int container = mTemp[0]; |
| 273 | final int screenId = mTemp[1]; |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 274 | |
| 275 | mValues.put(Favorites.CONTAINER, container); |
| 276 | mValues.put(Favorites.SCREEN, screenId); |
Sunny Goyal | 96a0963 | 2015-12-16 11:32:54 -0800 | [diff] [blame] | 277 | |
| 278 | mValues.put(Favorites.CELLX, |
Sunny Goyal | f82e547 | 2016-01-06 15:09:22 -0800 | [diff] [blame] | 279 | convertToDistanceFromEnd(getAttributeValue(parser, ATTR_X), mColumnCount)); |
Sunny Goyal | 96a0963 | 2015-12-16 11:32:54 -0800 | [diff] [blame] | 280 | mValues.put(Favorites.CELLY, |
Sunny Goyal | f82e547 | 2016-01-06 15:09:22 -0800 | [diff] [blame] | 281 | convertToDistanceFromEnd(getAttributeValue(parser, ATTR_Y), mRowCount)); |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 282 | |
| 283 | TagParser tagParser = tagParserMap.get(parser.getName()); |
| 284 | if (tagParser == null) { |
| 285 | if (LOGD) Log.d(TAG, "Ignoring unknown element tag: " + parser.getName()); |
| 286 | return 0; |
| 287 | } |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 288 | int newElementId = tagParser.parseAndAdd(parser); |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 289 | if (newElementId >= 0) { |
| 290 | // Keep track of the set of screens which need to be added to the db. |
| 291 | if (!screenIds.contains(screenId) && |
| 292 | container == Favorites.CONTAINER_DESKTOP) { |
| 293 | screenIds.add(screenId); |
| 294 | } |
| 295 | return 1; |
| 296 | } |
| 297 | return 0; |
| 298 | } |
| 299 | |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 300 | protected int addShortcut(String title, Intent intent, int type) { |
| 301 | int id = mCallback.generateNewItemId(); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 302 | mValues.put(Favorites.INTENT, intent.toUri(0)); |
| 303 | mValues.put(Favorites.TITLE, title); |
| 304 | mValues.put(Favorites.ITEM_TYPE, type); |
| 305 | mValues.put(Favorites.SPANX, 1); |
| 306 | mValues.put(Favorites.SPANY, 1); |
| 307 | mValues.put(Favorites._ID, id); |
Nicolas Sleiman | 31e9fa4 | 2023-01-25 16:30:32 +0000 | [diff] [blame] | 308 | |
Sunny Goyal | afaa8f0 | 2023-03-30 12:34:43 -0700 | [diff] [blame] | 309 | if (type == ITEM_TYPE_APPLICATION) { |
| 310 | ComponentName cn = intent.getComponent(); |
| 311 | if (cn != null && mActivityOverride.containsKey(cn.getPackageName())) { |
| 312 | LauncherActivityInfo replacementInfo = mActivityOverride.get(cn.getPackageName()); |
| 313 | mValues.put(Favorites.PROFILE_ID, UserCache.INSTANCE.get(mContext) |
| 314 | .getSerialNumberForUser(replacementInfo.getUser())); |
| 315 | mValues.put(Favorites.INTENT, AppInfo.makeLaunchIntent(replacementInfo).toUri(0)); |
| 316 | } |
| 317 | } |
Nicolas Sleiman | 31e9fa4 | 2023-01-25 16:30:32 +0000 | [diff] [blame] | 318 | |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 319 | if (mCallback.insertAndCheck(mDb, mValues) < 0) { |
| 320 | return -1; |
| 321 | } else { |
| 322 | return id; |
| 323 | } |
| 324 | } |
| 325 | |
Rajeev Kumar | 26453a2 | 2017-06-09 16:02:25 -0700 | [diff] [blame] | 326 | protected ArrayMap<String, TagParser> getFolderElementsMap() { |
| 327 | ArrayMap<String, TagParser> parsers = new ArrayMap<>(); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 328 | parsers.put(TAG_APP_ICON, new AppShortcutParser()); |
| 329 | parsers.put(TAG_AUTO_INSTALL, new AutoInstallParser()); |
Sunny Goyal | afaa8f0 | 2023-03-30 12:34:43 -0700 | [diff] [blame] | 330 | parsers.put(TAG_SHORTCUT, new ShortcutParser()); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 331 | return parsers; |
| 332 | } |
| 333 | |
Rajeev Kumar | 26453a2 | 2017-06-09 16:02:25 -0700 | [diff] [blame] | 334 | protected ArrayMap<String, TagParser> getLayoutElementsMap() { |
| 335 | ArrayMap<String, TagParser> parsers = new ArrayMap<>(); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 336 | parsers.put(TAG_APP_ICON, new AppShortcutParser()); |
| 337 | parsers.put(TAG_AUTO_INSTALL, new AutoInstallParser()); |
| 338 | parsers.put(TAG_FOLDER, new FolderParser()); |
Sunny Goyal | 86df138 | 2016-08-10 15:03:22 -0700 | [diff] [blame] | 339 | parsers.put(TAG_APPWIDGET, new PendingWidgetParser()); |
Samuel Fufa | ca37b8a | 2019-08-19 17:04:36 -0700 | [diff] [blame] | 340 | parsers.put(TAG_SEARCH_WIDGET, new SearchWidgetParser()); |
Sunny Goyal | afaa8f0 | 2023-03-30 12:34:43 -0700 | [diff] [blame] | 341 | parsers.put(TAG_SHORTCUT, new ShortcutParser()); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 342 | return parsers; |
| 343 | } |
| 344 | |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 345 | protected interface TagParser { |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 346 | /** |
| 347 | * Parses the tag and adds to the db |
| 348 | * @return the id of the row added or -1; |
| 349 | */ |
Sunny Goyal | 0d74231 | 2019-03-04 20:22:26 -0800 | [diff] [blame] | 350 | int parseAndAdd(XmlPullParser parser) |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 351 | throws XmlPullParserException, IOException; |
| 352 | } |
| 353 | |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 354 | /** |
| 355 | * App shortcuts: required attributes packageName and className |
| 356 | */ |
| 357 | protected class AppShortcutParser implements TagParser { |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 358 | |
| 359 | @Override |
Sunny Goyal | 0d74231 | 2019-03-04 20:22:26 -0800 | [diff] [blame] | 360 | public int parseAndAdd(XmlPullParser parser) { |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 361 | final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME); |
| 362 | final String className = getAttributeValue(parser, ATTR_CLASS_NAME); |
| 363 | |
| 364 | if (!TextUtils.isEmpty(packageName) && !TextUtils.isEmpty(className)) { |
| 365 | ActivityInfo info; |
| 366 | try { |
| 367 | ComponentName cn; |
| 368 | try { |
| 369 | cn = new ComponentName(packageName, className); |
| 370 | info = mPackageManager.getActivityInfo(cn, 0); |
| 371 | } catch (PackageManager.NameNotFoundException nnfe) { |
| 372 | String[] packages = mPackageManager.currentToCanonicalPackageNames( |
Samuel Fufa | ca37b8a | 2019-08-19 17:04:36 -0700 | [diff] [blame] | 373 | new String[]{packageName}); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 374 | cn = new ComponentName(packages[0], className); |
| 375 | info = mPackageManager.getActivityInfo(cn, 0); |
| 376 | } |
| 377 | final Intent intent = new Intent(Intent.ACTION_MAIN, null) |
Samuel Fufa | ca37b8a | 2019-08-19 17:04:36 -0700 | [diff] [blame] | 378 | .addCategory(Intent.CATEGORY_LAUNCHER) |
| 379 | .setComponent(cn) |
| 380 | .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 381 | | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 382 | |
| 383 | return addShortcut(info.loadLabel(mPackageManager).toString(), |
Nicolas Sleiman | 31e9fa4 | 2023-01-25 16:30:32 +0000 | [diff] [blame] | 384 | intent, ITEM_TYPE_APPLICATION); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 385 | } catch (PackageManager.NameNotFoundException e) { |
Sunny Goyal | eb3ba0f | 2017-03-27 11:22:36 -0700 | [diff] [blame] | 386 | Log.e(TAG, "Favorite not found: " + packageName + "/" + className); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 387 | } |
| 388 | return -1; |
| 389 | } else { |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 390 | return invalidPackageOrClass(parser); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 391 | } |
| 392 | } |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 393 | |
| 394 | /** |
| 395 | * Helper method to allow extending the parser capabilities |
| 396 | */ |
Sunny Goyal | 0d74231 | 2019-03-04 20:22:26 -0800 | [diff] [blame] | 397 | protected int invalidPackageOrClass(XmlPullParser parser) { |
Adam Cohen | cf0c746 | 2015-08-06 14:02:23 -0700 | [diff] [blame] | 398 | Log.w(TAG, "Skipping invalid <favorite> with no component"); |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 399 | return -1; |
| 400 | } |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 403 | /** |
| 404 | * AutoInstall: required attributes packageName and className |
| 405 | */ |
| 406 | protected class AutoInstallParser implements TagParser { |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 407 | |
| 408 | @Override |
Sunny Goyal | 0d74231 | 2019-03-04 20:22:26 -0800 | [diff] [blame] | 409 | public int parseAndAdd(XmlPullParser parser) { |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 410 | final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME); |
| 411 | final String className = getAttributeValue(parser, ATTR_CLASS_NAME); |
| 412 | if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(className)) { |
| 413 | if (LOGD) Log.d(TAG, "Skipping invalid <favorite> with no component"); |
| 414 | return -1; |
| 415 | } |
| 416 | |
Sunny Goyal | 9589916 | 2019-03-27 16:03:06 -0700 | [diff] [blame] | 417 | mValues.put(Favorites.RESTORED, WorkspaceItemInfo.FLAG_AUTOINSTALL_ICON); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 418 | final Intent intent = new Intent(Intent.ACTION_MAIN, null) |
Samuel Fufa | ca37b8a | 2019-08-19 17:04:36 -0700 | [diff] [blame] | 419 | .addCategory(Intent.CATEGORY_LAUNCHER) |
| 420 | .setComponent(new ComponentName(packageName, className)) |
| 421 | .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 422 | | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 423 | return addShortcut(mContext.getString(R.string.package_state_unknown), intent, |
Nicolas Sleiman | 31e9fa4 | 2023-01-25 16:30:32 +0000 | [diff] [blame] | 424 | ITEM_TYPE_APPLICATION); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 428 | /** |
Sunny Goyal | afaa8f0 | 2023-03-30 12:34:43 -0700 | [diff] [blame] | 429 | * Parses a deep shortcut. Required attributes packageName and shortcutId |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 430 | */ |
| 431 | protected class ShortcutParser implements TagParser { |
| 432 | |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 433 | @Override |
Sunny Goyal | 0d74231 | 2019-03-04 20:22:26 -0800 | [diff] [blame] | 434 | public int parseAndAdd(XmlPullParser parser) { |
Sunny Goyal | afaa8f0 | 2023-03-30 12:34:43 -0700 | [diff] [blame] | 435 | final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME); |
| 436 | final String shortcutId = getAttributeValue(parser, ATTR_SHORTCUT_ID); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 437 | |
Sunny Goyal | afaa8f0 | 2023-03-30 12:34:43 -0700 | [diff] [blame] | 438 | try { |
| 439 | LauncherApps launcherApps = mContext.getSystemService(LauncherApps.class); |
| 440 | launcherApps.pinShortcuts(packageName, Collections.singletonList(shortcutId), |
| 441 | Process.myUserHandle()); |
| 442 | Intent intent = ShortcutKey.makeIntent(shortcutId, packageName); |
| 443 | mValues.put(Favorites.RESTORED, WorkspaceItemInfo.FLAG_RESTORED_ICON); |
| 444 | return addShortcut(null, intent, Favorites.ITEM_TYPE_DEEP_SHORTCUT); |
| 445 | } catch (Exception e) { |
| 446 | Log.e(TAG, "Unable to pin the shortcut for shortcut id = " + shortcutId |
| 447 | + " and package name = " + packageName, e); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 448 | } |
Sunny Goyal | afaa8f0 | 2023-03-30 12:34:43 -0700 | [diff] [blame] | 449 | return -1; |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 453 | /** |
| 454 | * AppWidget parser: Required attributes packageName, className, spanX and spanY. |
| 455 | * Options child nodes: <extra key=... value=... /> |
Sunny Goyal | 86df138 | 2016-08-10 15:03:22 -0700 | [diff] [blame] | 456 | * It adds a pending widget which allows the widget to come later. If there are extras, those |
| 457 | * are passed to widget options during bind. |
| 458 | * The config activity for the widget (if present) is not shown, so any optional configurations |
| 459 | * should be passed as extras and the widget should support reading these widget options. |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 460 | */ |
Sunny Goyal | 86df138 | 2016-08-10 15:03:22 -0700 | [diff] [blame] | 461 | protected class PendingWidgetParser implements TagParser { |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 462 | |
Samuel Fufa | ca37b8a | 2019-08-19 17:04:36 -0700 | [diff] [blame] | 463 | @Nullable |
| 464 | public ComponentName getComponentName(XmlPullParser parser) { |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 465 | final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME); |
| 466 | final String className = getAttributeValue(parser, ATTR_CLASS_NAME); |
| 467 | if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(className)) { |
Samuel Fufa | ca37b8a | 2019-08-19 17:04:36 -0700 | [diff] [blame] | 468 | return null; |
| 469 | } |
| 470 | return new ComponentName(packageName, className); |
| 471 | } |
| 472 | |
| 473 | |
| 474 | @Override |
| 475 | public int parseAndAdd(XmlPullParser parser) |
| 476 | throws XmlPullParserException, IOException { |
| 477 | ComponentName cn = getComponentName(parser); |
| 478 | if (cn == null) { |
Sunny Goyal | 86df138 | 2016-08-10 15:03:22 -0700 | [diff] [blame] | 479 | if (LOGD) Log.d(TAG, "Skipping invalid <appwidget> with no component"); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 480 | return -1; |
| 481 | } |
| 482 | |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 483 | mValues.put(Favorites.SPANX, getAttributeValue(parser, ATTR_SPAN_X)); |
| 484 | mValues.put(Favorites.SPANY, getAttributeValue(parser, ATTR_SPAN_Y)); |
Sunny Goyal | 86df138 | 2016-08-10 15:03:22 -0700 | [diff] [blame] | 485 | mValues.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 486 | |
| 487 | // Read the extras |
| 488 | Bundle extras = new Bundle(); |
| 489 | int widgetDepth = parser.getDepth(); |
| 490 | int type; |
| 491 | while ((type = parser.next()) != XmlPullParser.END_TAG || |
| 492 | parser.getDepth() > widgetDepth) { |
| 493 | if (type != XmlPullParser.START_TAG) { |
| 494 | continue; |
| 495 | } |
| 496 | |
| 497 | if (TAG_EXTRA.equals(parser.getName())) { |
| 498 | String key = getAttributeValue(parser, ATTR_KEY); |
| 499 | String value = getAttributeValue(parser, ATTR_VALUE); |
| 500 | if (key != null && value != null) { |
| 501 | extras.putString(key, value); |
| 502 | } else { |
| 503 | throw new RuntimeException("Widget extras must have a key and value"); |
| 504 | } |
| 505 | } else { |
| 506 | throw new RuntimeException("Widgets can contain only extras"); |
| 507 | } |
| 508 | } |
Samuel Fufa | ca37b8a | 2019-08-19 17:04:36 -0700 | [diff] [blame] | 509 | return verifyAndInsert(cn, extras); |
Sunny Goyal | 86df138 | 2016-08-10 15:03:22 -0700 | [diff] [blame] | 510 | } |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 511 | |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 512 | protected int verifyAndInsert(ComponentName cn, Bundle extras) { |
Sunny Goyal | 86df138 | 2016-08-10 15:03:22 -0700 | [diff] [blame] | 513 | mValues.put(Favorites.APPWIDGET_PROVIDER, cn.flattenToString()); |
| 514 | mValues.put(Favorites.RESTORED, |
Samuel Fufa | ca37b8a | 2019-08-19 17:04:36 -0700 | [diff] [blame] | 515 | LauncherAppWidgetInfo.FLAG_ID_NOT_VALID |
| 516 | | LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY |
| 517 | | LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG); |
Sunny Goyal | 86df138 | 2016-08-10 15:03:22 -0700 | [diff] [blame] | 518 | mValues.put(Favorites._ID, mCallback.generateNewItemId()); |
| 519 | if (!extras.isEmpty()) { |
| 520 | mValues.put(Favorites.INTENT, new Intent().putExtras(extras).toUri(0)); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 521 | } |
Sunny Goyal | 86df138 | 2016-08-10 15:03:22 -0700 | [diff] [blame] | 522 | |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 523 | int insertedId = mCallback.insertAndCheck(mDb, mValues); |
Sunny Goyal | 86df138 | 2016-08-10 15:03:22 -0700 | [diff] [blame] | 524 | if (insertedId < 0) { |
| 525 | return -1; |
| 526 | } else { |
| 527 | return insertedId; |
| 528 | } |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | |
Samuel Fufa | ca37b8a | 2019-08-19 17:04:36 -0700 | [diff] [blame] | 532 | protected class SearchWidgetParser extends PendingWidgetParser { |
| 533 | @Override |
| 534 | @Nullable |
Stefan Andonian | 7fcee91 | 2023-04-07 17:56:29 +0000 | [diff] [blame] | 535 | @WorkerThread |
Samuel Fufa | ca37b8a | 2019-08-19 17:04:36 -0700 | [diff] [blame] | 536 | public ComponentName getComponentName(XmlPullParser parser) { |
| 537 | return QsbContainerView.getSearchComponentName(mContext); |
| 538 | } |
| 539 | |
| 540 | @Override |
| 541 | protected int verifyAndInsert(ComponentName cn, Bundle extras) { |
| 542 | mValues.put(Favorites.OPTIONS, LauncherAppWidgetInfo.OPTION_SEARCH_WIDGET); |
| 543 | int flags = mValues.getAsInteger(Favorites.RESTORED) |
| 544 | | WorkspaceItemInfo.FLAG_RESTORE_STARTED; |
| 545 | mValues.put(Favorites.RESTORED, flags); |
| 546 | return super.verifyAndInsert(cn, extras); |
| 547 | } |
| 548 | } |
| 549 | |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 550 | protected class FolderParser implements TagParser { |
Rajeev Kumar | 26453a2 | 2017-06-09 16:02:25 -0700 | [diff] [blame] | 551 | private final ArrayMap<String, TagParser> mFolderElements; |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 552 | |
| 553 | public FolderParser() { |
| 554 | this(getFolderElementsMap()); |
| 555 | } |
| 556 | |
Rajeev Kumar | 26453a2 | 2017-06-09 16:02:25 -0700 | [diff] [blame] | 557 | public FolderParser(ArrayMap<String, TagParser> elements) { |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 558 | mFolderElements = elements; |
| 559 | } |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 560 | |
| 561 | @Override |
Nicolas Sleiman | 31e9fa4 | 2023-01-25 16:30:32 +0000 | [diff] [blame] | 562 | public int parseAndAdd(XmlPullParser parser) throws XmlPullParserException, IOException { |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 563 | final String title; |
| 564 | final int titleResId = getAttributeResourceValue(parser, ATTR_TITLE, 0); |
| 565 | if (titleResId != 0) { |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 566 | title = mSourceRes.getString(titleResId); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 567 | } else { |
Sunny Goyal | 55e2b16 | 2020-06-09 15:44:48 -0700 | [diff] [blame] | 568 | String titleText = getAttributeValue(parser, ATTR_TITLE_TEXT); |
| 569 | title = TextUtils.isEmpty(titleText) ? "" : titleText; |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | mValues.put(Favorites.TITLE, title); |
| 573 | mValues.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_FOLDER); |
| 574 | mValues.put(Favorites.SPANX, 1); |
| 575 | mValues.put(Favorites.SPANY, 1); |
| 576 | mValues.put(Favorites._ID, mCallback.generateNewItemId()); |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 577 | int folderId = mCallback.insertAndCheck(mDb, mValues); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 578 | if (folderId < 0) { |
| 579 | if (LOGD) Log.e(TAG, "Unable to add folder"); |
| 580 | return -1; |
| 581 | } |
| 582 | |
| 583 | final ContentValues myValues = new ContentValues(mValues); |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 584 | IntArray folderItems = new IntArray(); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 585 | |
| 586 | int type; |
| 587 | int folderDepth = parser.getDepth(); |
Sunny Goyal | 56a57bb | 2015-07-06 11:15:45 -0700 | [diff] [blame] | 588 | int rank = 0; |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 589 | while ((type = parser.next()) != XmlPullParser.END_TAG || |
| 590 | parser.getDepth() > folderDepth) { |
| 591 | if (type != XmlPullParser.START_TAG) { |
| 592 | continue; |
| 593 | } |
| 594 | mValues.clear(); |
| 595 | mValues.put(Favorites.CONTAINER, folderId); |
Sunny Goyal | 56a57bb | 2015-07-06 11:15:45 -0700 | [diff] [blame] | 596 | mValues.put(Favorites.RANK, rank); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 597 | |
| 598 | TagParser tagParser = mFolderElements.get(parser.getName()); |
| 599 | if (tagParser != null) { |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 600 | final int id = tagParser.parseAndAdd(parser); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 601 | if (id >= 0) { |
| 602 | folderItems.add(id); |
Sunny Goyal | 56a57bb | 2015-07-06 11:15:45 -0700 | [diff] [blame] | 603 | rank++; |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 604 | } |
| 605 | } else { |
| 606 | throw new RuntimeException("Invalid folder item " + parser.getName()); |
| 607 | } |
| 608 | } |
| 609 | |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 610 | int addedId = folderId; |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 611 | |
| 612 | // We can only have folders with >= 2 items, so we need to remove the |
| 613 | // folder and clean up if less than 2 items were included, or some |
| 614 | // failed to add, and less than 2 were actually added |
| 615 | if (folderItems.size() < 2) { |
| 616 | // Delete the folder |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame] | 617 | Uri uri = Favorites.getContentUri(folderId); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 618 | SqlArguments args = new SqlArguments(uri, null, null); |
| 619 | mDb.delete(args.table, args.where, args.args); |
| 620 | addedId = -1; |
| 621 | |
| 622 | // If we have a single item, promote it to where the folder |
| 623 | // would have been. |
| 624 | if (folderItems.size() == 1) { |
| 625 | final ContentValues childValues = new ContentValues(); |
| 626 | copyInteger(myValues, childValues, Favorites.CONTAINER); |
| 627 | copyInteger(myValues, childValues, Favorites.SCREEN); |
| 628 | copyInteger(myValues, childValues, Favorites.CELLX); |
| 629 | copyInteger(myValues, childValues, Favorites.CELLY); |
| 630 | |
| 631 | addedId = folderItems.get(0); |
Sunny Goyal | b5b55c8 | 2016-05-10 12:28:59 -0700 | [diff] [blame] | 632 | mDb.update(Favorites.TABLE_NAME, childValues, |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 633 | Favorites._ID + "=" + addedId, null); |
| 634 | } |
| 635 | } |
| 636 | return addedId; |
| 637 | } |
| 638 | } |
| 639 | |
Sunny Goyal | 762d061 | 2020-07-29 15:03:46 -0700 | [diff] [blame] | 640 | public static void beginDocument(XmlPullParser parser, String firstElementName) |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 641 | throws XmlPullParserException, IOException { |
| 642 | int type; |
| 643 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 644 | && type != XmlPullParser.END_DOCUMENT); |
| 645 | |
| 646 | if (type != XmlPullParser.START_TAG) { |
| 647 | throw new XmlPullParserException("No start tag found"); |
| 648 | } |
| 649 | |
| 650 | if (!parser.getName().equals(firstElementName)) { |
| 651 | throw new XmlPullParserException("Unexpected start tag: found " + parser.getName() + |
| 652 | ", expected " + firstElementName); |
| 653 | } |
| 654 | } |
| 655 | |
Sunny Goyal | 96a0963 | 2015-12-16 11:32:54 -0800 | [diff] [blame] | 656 | private static String convertToDistanceFromEnd(String value, int endValue) { |
| 657 | if (!TextUtils.isEmpty(value)) { |
| 658 | int x = Integer.parseInt(value); |
| 659 | if (x < 0) { |
| 660 | return Integer.toString(endValue + x); |
| 661 | } |
| 662 | } |
| 663 | return value; |
| 664 | } |
| 665 | |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 666 | /** |
| 667 | * Return attribute value, attempting launcher-specific namespace first |
| 668 | * before falling back to anonymous attribute. |
| 669 | */ |
Sunny Goyal | 0d74231 | 2019-03-04 20:22:26 -0800 | [diff] [blame] | 670 | protected static String getAttributeValue(XmlPullParser parser, String attribute) { |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 671 | String value = parser.getAttributeValue( |
| 672 | "http://schemas.android.com/apk/res-auto/com.android.launcher3", attribute); |
| 673 | if (value == null) { |
| 674 | value = parser.getAttributeValue(null, attribute); |
| 675 | } |
| 676 | return value; |
| 677 | } |
| 678 | |
| 679 | /** |
| 680 | * Return attribute resource value, attempting launcher-specific namespace |
| 681 | * first before falling back to anonymous attribute. |
| 682 | */ |
Sunny Goyal | 0d74231 | 2019-03-04 20:22:26 -0800 | [diff] [blame] | 683 | protected static int getAttributeResourceValue(XmlPullParser parser, String attribute, |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 684 | int defaultValue) { |
Sunny Goyal | 0d74231 | 2019-03-04 20:22:26 -0800 | [diff] [blame] | 685 | AttributeSet attrs = Xml.asAttributeSet(parser); |
| 686 | int value = attrs.getAttributeResourceValue( |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 687 | "http://schemas.android.com/apk/res-auto/com.android.launcher3", attribute, |
| 688 | defaultValue); |
| 689 | if (value == defaultValue) { |
Sunny Goyal | 0d74231 | 2019-03-04 20:22:26 -0800 | [diff] [blame] | 690 | value = attrs.getAttributeResourceValue(null, attribute, defaultValue); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 691 | } |
| 692 | return value; |
| 693 | } |
| 694 | |
Rajeev Kumar | 26453a2 | 2017-06-09 16:02:25 -0700 | [diff] [blame] | 695 | public interface LayoutParserCallback { |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 696 | int generateNewItemId(); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 697 | |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 698 | int insertAndCheck(SQLiteDatabase db, ContentValues values); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 699 | } |
| 700 | |
Samuel Fufa | ca37b8a | 2019-08-19 17:04:36 -0700 | [diff] [blame] | 701 | @Thunk |
| 702 | static void copyInteger(ContentValues from, ContentValues to, String key) { |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 703 | to.put(key, from.getAsInteger(key)); |
| 704 | } |
Nicolas Sleiman | 31e9fa4 | 2023-01-25 16:30:32 +0000 | [diff] [blame] | 705 | |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 706 | } |