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