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