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