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