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