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