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