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