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