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