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