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; |
| 20 | import android.appwidget.AppWidgetManager; |
| 21 | import android.content.ComponentName; |
| 22 | import android.content.ContentValues; |
| 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
| 25 | import android.content.pm.ActivityInfo; |
| 26 | import android.content.pm.PackageManager; |
| 27 | import android.content.res.Resources; |
| 28 | import android.content.res.XmlResourceParser; |
| 29 | import android.database.sqlite.SQLiteDatabase; |
| 30 | import android.graphics.drawable.Drawable; |
| 31 | import android.net.Uri; |
| 32 | import android.os.Bundle; |
| 33 | import android.text.TextUtils; |
| 34 | import android.util.Log; |
| 35 | import android.util.Pair; |
| 36 | import android.util.Patterns; |
| 37 | |
| 38 | import com.android.launcher3.LauncherProvider.SqlArguments; |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 39 | import com.android.launcher3.LauncherSettings.Favorites; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 40 | import com.android.launcher3.util.Thunk; |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 41 | |
| 42 | import org.xmlpull.v1.XmlPullParser; |
| 43 | import org.xmlpull.v1.XmlPullParserException; |
| 44 | |
| 45 | import java.io.IOException; |
| 46 | import java.util.ArrayList; |
| 47 | import java.util.HashMap; |
Sunny Goyal | b2d46ce | 2015-03-26 11:32:11 -0700 | [diff] [blame] | 48 | import java.util.Locale; |
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) { |
| 81 | DeviceProfile grid = LauncherAppState.getInstance().getDynamicGrid().getDeviceProfile(); |
| 82 | |
| 83 | // Try with grid size and hotseat count |
| 84 | String layoutName = String.format(Locale.ENGLISH, FORMATTED_LAYOUT_RES_WITH_HOSTEAT, |
| 85 | (int) grid.numColumns, (int) grid.numRows, (int) grid.numHotseatIcons); |
| 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, |
| 93 | (int) grid.numColumns, (int) grid.numRows); |
| 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"; |
| 128 | private static final String ATTR_X = "x"; |
| 129 | private static final String ATTR_Y = "y"; |
| 130 | private static final String ATTR_SPAN_X = "spanX"; |
| 131 | private static final String ATTR_SPAN_Y = "spanY"; |
| 132 | private static final String ATTR_ICON = "icon"; |
| 133 | private static final String ATTR_URL = "url"; |
| 134 | |
Sunny Goyal | b564efb | 2015-01-23 13:45:20 -0800 | [diff] [blame] | 135 | // Attrs for "Include" |
| 136 | private static final String ATTR_WORKSPACE = "workspace"; |
| 137 | |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 138 | // Style attrs -- "Extra" |
| 139 | private static final String ATTR_KEY = "key"; |
| 140 | private static final String ATTR_VALUE = "value"; |
| 141 | |
| 142 | private static final String HOTSEAT_CONTAINER_NAME = |
| 143 | Favorites.containerToString(Favorites.CONTAINER_HOTSEAT); |
| 144 | |
| 145 | private static final String ACTION_APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE = |
| 146 | "com.android.launcher.action.APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE"; |
| 147 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 148 | @Thunk final Context mContext; |
| 149 | @Thunk final AppWidgetHost mAppWidgetHost; |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 150 | protected final LayoutParserCallback mCallback; |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 151 | |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 152 | protected final PackageManager mPackageManager; |
| 153 | protected final Resources mSourceRes; |
| 154 | protected final int mLayoutId; |
| 155 | |
| 156 | private final int mHotseatAllAppsRank; |
| 157 | |
| 158 | private final long[] mTemp = new long[2]; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 159 | @Thunk final ContentValues mValues; |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 160 | protected final String mRootTag; |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 161 | |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 162 | protected SQLiteDatabase mDb; |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 163 | |
| 164 | public AutoInstallsLayout(Context context, AppWidgetHost appWidgetHost, |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 165 | LayoutParserCallback callback, Resources res, |
| 166 | int layoutId, String rootTag) { |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 167 | this(context, appWidgetHost, callback, res, layoutId, rootTag, |
| 168 | LauncherAppState.getInstance().getDynamicGrid().getDeviceProfile().hotseatAllAppsRank); |
| 169 | } |
| 170 | |
| 171 | public AutoInstallsLayout(Context context, AppWidgetHost appWidgetHost, |
| 172 | LayoutParserCallback callback, Resources res, |
| 173 | int layoutId, String rootTag, int hotseatAllAppsRank) { |
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 | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 184 | mHotseatAllAppsRank = hotseatAllAppsRank; |
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 | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 195 | Log.w(TAG, "Got exception parsing layout.", e); |
| 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; |
| 209 | HashMap<String, TagParser> tagParserMap = getLayoutElementsMap(); |
| 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)); |
| 231 | out[1] = (rank < mHotseatAllAppsRank) ? rank : (rank + 1); |
| 232 | } else { |
| 233 | out[0] = Favorites.CONTAINER_DESKTOP; |
| 234 | out[1] = Long.parseLong(getAttributeValue(parser, ATTR_SCREEN)); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Parses the current node and returns the number of elements added. |
| 240 | */ |
| 241 | protected int parseAndAddNode( |
| 242 | XmlResourceParser parser, |
| 243 | HashMap<String, TagParser> tagParserMap, |
| 244 | ArrayList<Long> screenIds) |
| 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); |
| 259 | final long container = mTemp[0]; |
| 260 | final long screenId = mTemp[1]; |
| 261 | |
| 262 | mValues.put(Favorites.CONTAINER, container); |
| 263 | mValues.put(Favorites.SCREEN, screenId); |
| 264 | mValues.put(Favorites.CELLX, getAttributeValue(parser, ATTR_X)); |
| 265 | mValues.put(Favorites.CELLY, getAttributeValue(parser, ATTR_Y)); |
| 266 | |
| 267 | TagParser tagParser = tagParserMap.get(parser.getName()); |
| 268 | if (tagParser == null) { |
| 269 | if (LOGD) Log.d(TAG, "Ignoring unknown element tag: " + parser.getName()); |
| 270 | return 0; |
| 271 | } |
| 272 | long newElementId = tagParser.parseAndAdd(parser); |
| 273 | if (newElementId >= 0) { |
| 274 | // Keep track of the set of screens which need to be added to the db. |
| 275 | if (!screenIds.contains(screenId) && |
| 276 | container == Favorites.CONTAINER_DESKTOP) { |
| 277 | screenIds.add(screenId); |
| 278 | } |
| 279 | return 1; |
| 280 | } |
| 281 | return 0; |
| 282 | } |
| 283 | |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 284 | protected long addShortcut(String title, Intent intent, int type) { |
| 285 | long id = mCallback.generateNewItemId(); |
| 286 | mValues.put(Favorites.INTENT, intent.toUri(0)); |
| 287 | mValues.put(Favorites.TITLE, title); |
| 288 | mValues.put(Favorites.ITEM_TYPE, type); |
| 289 | mValues.put(Favorites.SPANX, 1); |
| 290 | mValues.put(Favorites.SPANY, 1); |
| 291 | mValues.put(Favorites._ID, id); |
| 292 | if (mCallback.insertAndCheck(mDb, mValues) < 0) { |
| 293 | return -1; |
| 294 | } else { |
| 295 | return id; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | protected HashMap<String, TagParser> getFolderElementsMap() { |
| 300 | HashMap<String, TagParser> parsers = new HashMap<String, TagParser>(); |
| 301 | parsers.put(TAG_APP_ICON, new AppShortcutParser()); |
| 302 | parsers.put(TAG_AUTO_INSTALL, new AutoInstallParser()); |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 303 | parsers.put(TAG_SHORTCUT, new ShortcutParser(mSourceRes)); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 304 | return parsers; |
| 305 | } |
| 306 | |
| 307 | protected HashMap<String, TagParser> getLayoutElementsMap() { |
| 308 | HashMap<String, TagParser> parsers = new HashMap<String, TagParser>(); |
| 309 | parsers.put(TAG_APP_ICON, new AppShortcutParser()); |
| 310 | parsers.put(TAG_AUTO_INSTALL, new AutoInstallParser()); |
| 311 | parsers.put(TAG_FOLDER, new FolderParser()); |
| 312 | parsers.put(TAG_APPWIDGET, new AppWidgetParser()); |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 313 | parsers.put(TAG_SHORTCUT, new ShortcutParser(mSourceRes)); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 314 | return parsers; |
| 315 | } |
| 316 | |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 317 | protected interface TagParser { |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 318 | /** |
| 319 | * Parses the tag and adds to the db |
| 320 | * @return the id of the row added or -1; |
| 321 | */ |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 322 | long parseAndAdd(XmlResourceParser parser) |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 323 | throws XmlPullParserException, IOException; |
| 324 | } |
| 325 | |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 326 | /** |
| 327 | * App shortcuts: required attributes packageName and className |
| 328 | */ |
| 329 | protected class AppShortcutParser implements TagParser { |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 330 | |
| 331 | @Override |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 332 | public long parseAndAdd(XmlResourceParser parser) { |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 333 | final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME); |
| 334 | final String className = getAttributeValue(parser, ATTR_CLASS_NAME); |
| 335 | |
| 336 | if (!TextUtils.isEmpty(packageName) && !TextUtils.isEmpty(className)) { |
| 337 | ActivityInfo info; |
| 338 | try { |
| 339 | ComponentName cn; |
| 340 | try { |
| 341 | cn = new ComponentName(packageName, className); |
| 342 | info = mPackageManager.getActivityInfo(cn, 0); |
| 343 | } catch (PackageManager.NameNotFoundException nnfe) { |
| 344 | String[] packages = mPackageManager.currentToCanonicalPackageNames( |
| 345 | new String[] { packageName }); |
| 346 | cn = new ComponentName(packages[0], className); |
| 347 | info = mPackageManager.getActivityInfo(cn, 0); |
| 348 | } |
| 349 | final Intent intent = new Intent(Intent.ACTION_MAIN, null) |
| 350 | .addCategory(Intent.CATEGORY_LAUNCHER) |
| 351 | .setComponent(cn) |
| 352 | .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | |
| 353 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
| 354 | |
| 355 | return addShortcut(info.loadLabel(mPackageManager).toString(), |
| 356 | intent, Favorites.ITEM_TYPE_APPLICATION); |
| 357 | } catch (PackageManager.NameNotFoundException e) { |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 358 | if (LOGD) Log.w(TAG, "Unable to add favorite: " + packageName + "/" + className, e); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 359 | } |
| 360 | return -1; |
| 361 | } else { |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 362 | return invalidPackageOrClass(parser); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 363 | } |
| 364 | } |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 365 | |
| 366 | /** |
| 367 | * Helper method to allow extending the parser capabilities |
| 368 | */ |
| 369 | protected long invalidPackageOrClass(XmlResourceParser parser) { |
| 370 | if (LOGD) Log.d(TAG, "Skipping invalid <favorite> with no component"); |
| 371 | return -1; |
| 372 | } |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 375 | /** |
| 376 | * AutoInstall: required attributes packageName and className |
| 377 | */ |
| 378 | protected class AutoInstallParser implements TagParser { |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 379 | |
| 380 | @Override |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 381 | public long parseAndAdd(XmlResourceParser parser) { |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 382 | final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME); |
| 383 | final String className = getAttributeValue(parser, ATTR_CLASS_NAME); |
| 384 | if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(className)) { |
| 385 | if (LOGD) Log.d(TAG, "Skipping invalid <favorite> with no component"); |
| 386 | return -1; |
| 387 | } |
| 388 | |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 389 | mValues.put(Favorites.RESTORED, ShortcutInfo.FLAG_AUTOINTALL_ICON); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 390 | final Intent intent = new Intent(Intent.ACTION_MAIN, null) |
| 391 | .addCategory(Intent.CATEGORY_LAUNCHER) |
| 392 | .setComponent(new ComponentName(packageName, className)) |
| 393 | .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | |
| 394 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
| 395 | return addShortcut(mContext.getString(R.string.package_state_unknown), intent, |
| 396 | Favorites.ITEM_TYPE_APPLICATION); |
| 397 | } |
| 398 | } |
| 399 | |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 400 | /** |
| 401 | * Parses a web shortcut. Required attributes url, icon, title |
| 402 | */ |
| 403 | protected class ShortcutParser implements TagParser { |
| 404 | |
| 405 | private final Resources mIconRes; |
| 406 | |
| 407 | public ShortcutParser(Resources iconRes) { |
| 408 | mIconRes = iconRes; |
| 409 | } |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 410 | |
| 411 | @Override |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 412 | public long parseAndAdd(XmlResourceParser parser) { |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 413 | final int titleResId = getAttributeResourceValue(parser, ATTR_TITLE, 0); |
| 414 | final int iconId = getAttributeResourceValue(parser, ATTR_ICON, 0); |
| 415 | |
| 416 | if (titleResId == 0 || iconId == 0) { |
| 417 | if (LOGD) Log.d(TAG, "Ignoring shortcut"); |
| 418 | return -1; |
| 419 | } |
| 420 | |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 421 | final Intent intent = parseIntent(parser); |
| 422 | if (intent == null) { |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 423 | return -1; |
| 424 | } |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 425 | |
| 426 | Drawable icon = mIconRes.getDrawable(iconId); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 427 | if (icon == null) { |
| 428 | if (LOGD) Log.d(TAG, "Ignoring shortcut, can't load icon"); |
| 429 | return -1; |
| 430 | } |
| 431 | |
| 432 | ItemInfo.writeBitmap(mValues, Utilities.createIconBitmap(icon, mContext)); |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 433 | mValues.put(Favorites.ICON_TYPE, Favorites.ICON_TYPE_RESOURCE); |
| 434 | mValues.put(Favorites.ICON_PACKAGE, mIconRes.getResourcePackageName(iconId)); |
| 435 | mValues.put(Favorites.ICON_RESOURCE, mIconRes.getResourceName(iconId)); |
| 436 | |
| 437 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 438 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 439 | return addShortcut(mSourceRes.getString(titleResId), |
| 440 | intent, Favorites.ITEM_TYPE_SHORTCUT); |
| 441 | } |
| 442 | |
| 443 | protected Intent parseIntent(XmlResourceParser parser) { |
| 444 | final String url = getAttributeValue(parser, ATTR_URL); |
| 445 | if (TextUtils.isEmpty(url) || !Patterns.WEB_URL.matcher(url).matches()) { |
| 446 | if (LOGD) Log.d(TAG, "Ignoring shortcut, invalid url: " + url); |
| 447 | return null; |
| 448 | } |
| 449 | return new Intent(Intent.ACTION_VIEW, null).setData(Uri.parse(url)); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 453 | /** |
| 454 | * AppWidget parser: Required attributes packageName, className, spanX and spanY. |
| 455 | * Options child nodes: <extra key=... value=... /> |
| 456 | */ |
| 457 | protected class AppWidgetParser implements TagParser { |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 458 | |
| 459 | @Override |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 460 | public long parseAndAdd(XmlResourceParser parser) |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 461 | throws XmlPullParserException, IOException { |
| 462 | final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME); |
| 463 | final String className = getAttributeValue(parser, ATTR_CLASS_NAME); |
| 464 | if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(className)) { |
| 465 | if (LOGD) Log.d(TAG, "Skipping invalid <favorite> with no component"); |
| 466 | return -1; |
| 467 | } |
| 468 | |
| 469 | ComponentName cn = new ComponentName(packageName, className); |
| 470 | try { |
| 471 | mPackageManager.getReceiverInfo(cn, 0); |
| 472 | } catch (Exception e) { |
| 473 | String[] packages = mPackageManager.currentToCanonicalPackageNames( |
| 474 | new String[] { packageName }); |
| 475 | cn = new ComponentName(packages[0], className); |
| 476 | try { |
| 477 | mPackageManager.getReceiverInfo(cn, 0); |
| 478 | } catch (Exception e1) { |
| 479 | if (LOGD) Log.d(TAG, "Can't find widget provider: " + className); |
| 480 | return -1; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | mValues.put(Favorites.SPANX, getAttributeValue(parser, ATTR_SPAN_X)); |
| 485 | mValues.put(Favorites.SPANY, getAttributeValue(parser, ATTR_SPAN_Y)); |
| 486 | |
| 487 | // Read the extras |
| 488 | Bundle extras = new Bundle(); |
| 489 | int widgetDepth = parser.getDepth(); |
| 490 | int type; |
| 491 | while ((type = parser.next()) != XmlPullParser.END_TAG || |
| 492 | parser.getDepth() > widgetDepth) { |
| 493 | if (type != XmlPullParser.START_TAG) { |
| 494 | continue; |
| 495 | } |
| 496 | |
| 497 | if (TAG_EXTRA.equals(parser.getName())) { |
| 498 | String key = getAttributeValue(parser, ATTR_KEY); |
| 499 | String value = getAttributeValue(parser, ATTR_VALUE); |
| 500 | if (key != null && value != null) { |
| 501 | extras.putString(key, value); |
| 502 | } else { |
| 503 | throw new RuntimeException("Widget extras must have a key and value"); |
| 504 | } |
| 505 | } else { |
| 506 | throw new RuntimeException("Widgets can contain only extras"); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext); |
| 511 | long insertedId = -1; |
| 512 | try { |
| 513 | int appWidgetId = mAppWidgetHost.allocateAppWidgetId(); |
| 514 | |
| 515 | if (!appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, cn)) { |
| 516 | if (LOGD) Log.e(TAG, "Unable to bind app widget id " + cn); |
| 517 | return -1; |
| 518 | } |
| 519 | |
| 520 | mValues.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET); |
| 521 | mValues.put(Favorites.APPWIDGET_ID, appWidgetId); |
| 522 | mValues.put(Favorites.APPWIDGET_PROVIDER, cn.flattenToString()); |
| 523 | mValues.put(Favorites._ID, mCallback.generateNewItemId()); |
| 524 | insertedId = mCallback.insertAndCheck(mDb, mValues); |
| 525 | if (insertedId < 0) { |
| 526 | mAppWidgetHost.deleteAppWidgetId(appWidgetId); |
| 527 | return insertedId; |
| 528 | } |
| 529 | |
| 530 | // Send a broadcast to configure the widget |
| 531 | if (!extras.isEmpty()) { |
| 532 | Intent intent = new Intent(ACTION_APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE); |
| 533 | intent.setComponent(cn); |
| 534 | intent.putExtras(extras); |
| 535 | intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); |
| 536 | mContext.sendBroadcast(intent); |
| 537 | } |
| 538 | } catch (RuntimeException ex) { |
| 539 | if (LOGD) Log.e(TAG, "Problem allocating appWidgetId", ex); |
| 540 | } |
| 541 | return insertedId; |
| 542 | } |
| 543 | } |
| 544 | |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 545 | protected class FolderParser implements TagParser { |
| 546 | private final HashMap<String, TagParser> mFolderElements; |
| 547 | |
| 548 | public FolderParser() { |
| 549 | this(getFolderElementsMap()); |
| 550 | } |
| 551 | |
| 552 | public FolderParser(HashMap<String, TagParser> elements) { |
| 553 | mFolderElements = elements; |
| 554 | } |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 555 | |
| 556 | @Override |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 557 | public long parseAndAdd(XmlResourceParser parser) |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 558 | throws XmlPullParserException, IOException { |
| 559 | final String title; |
| 560 | final int titleResId = getAttributeResourceValue(parser, ATTR_TITLE, 0); |
| 561 | if (titleResId != 0) { |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 562 | title = mSourceRes.getString(titleResId); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 563 | } else { |
| 564 | title = mContext.getResources().getString(R.string.folder_name); |
| 565 | } |
| 566 | |
| 567 | mValues.put(Favorites.TITLE, title); |
| 568 | mValues.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_FOLDER); |
| 569 | mValues.put(Favorites.SPANX, 1); |
| 570 | mValues.put(Favorites.SPANY, 1); |
| 571 | mValues.put(Favorites._ID, mCallback.generateNewItemId()); |
| 572 | long folderId = mCallback.insertAndCheck(mDb, mValues); |
| 573 | if (folderId < 0) { |
| 574 | if (LOGD) Log.e(TAG, "Unable to add folder"); |
| 575 | return -1; |
| 576 | } |
| 577 | |
| 578 | final ContentValues myValues = new ContentValues(mValues); |
| 579 | ArrayList<Long> folderItems = new ArrayList<Long>(); |
| 580 | |
| 581 | int type; |
| 582 | int folderDepth = parser.getDepth(); |
| 583 | while ((type = parser.next()) != XmlPullParser.END_TAG || |
| 584 | parser.getDepth() > folderDepth) { |
| 585 | if (type != XmlPullParser.START_TAG) { |
| 586 | continue; |
| 587 | } |
| 588 | mValues.clear(); |
| 589 | mValues.put(Favorites.CONTAINER, folderId); |
| 590 | |
| 591 | TagParser tagParser = mFolderElements.get(parser.getName()); |
| 592 | if (tagParser != null) { |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 593 | final long id = tagParser.parseAndAdd(parser); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 594 | if (id >= 0) { |
| 595 | folderItems.add(id); |
| 596 | } |
| 597 | } else { |
| 598 | throw new RuntimeException("Invalid folder item " + parser.getName()); |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | long addedId = folderId; |
| 603 | |
| 604 | // We can only have folders with >= 2 items, so we need to remove the |
| 605 | // folder and clean up if less than 2 items were included, or some |
| 606 | // failed to add, and less than 2 were actually added |
| 607 | if (folderItems.size() < 2) { |
| 608 | // Delete the folder |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame^] | 609 | Uri uri = Favorites.getContentUri(folderId); |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 610 | SqlArguments args = new SqlArguments(uri, null, null); |
| 611 | mDb.delete(args.table, args.where, args.args); |
| 612 | addedId = -1; |
| 613 | |
| 614 | // If we have a single item, promote it to where the folder |
| 615 | // would have been. |
| 616 | if (folderItems.size() == 1) { |
| 617 | final ContentValues childValues = new ContentValues(); |
| 618 | copyInteger(myValues, childValues, Favorites.CONTAINER); |
| 619 | copyInteger(myValues, childValues, Favorites.SCREEN); |
| 620 | copyInteger(myValues, childValues, Favorites.CELLX); |
| 621 | copyInteger(myValues, childValues, Favorites.CELLY); |
| 622 | |
| 623 | addedId = folderItems.get(0); |
| 624 | mDb.update(LauncherProvider.TABLE_FAVORITES, childValues, |
| 625 | Favorites._ID + "=" + addedId, null); |
| 626 | } |
| 627 | } |
| 628 | return addedId; |
| 629 | } |
| 630 | } |
| 631 | |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 632 | protected static final void beginDocument(XmlPullParser parser, String firstElementName) |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 633 | throws XmlPullParserException, IOException { |
| 634 | int type; |
| 635 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 636 | && type != XmlPullParser.END_DOCUMENT); |
| 637 | |
| 638 | if (type != XmlPullParser.START_TAG) { |
| 639 | throw new XmlPullParserException("No start tag found"); |
| 640 | } |
| 641 | |
| 642 | if (!parser.getName().equals(firstElementName)) { |
| 643 | throw new XmlPullParserException("Unexpected start tag: found " + parser.getName() + |
| 644 | ", expected " + firstElementName); |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | /** |
| 649 | * Return attribute value, attempting launcher-specific namespace first |
| 650 | * before falling back to anonymous attribute. |
| 651 | */ |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 652 | protected static String getAttributeValue(XmlResourceParser parser, String attribute) { |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 653 | String value = parser.getAttributeValue( |
| 654 | "http://schemas.android.com/apk/res-auto/com.android.launcher3", attribute); |
| 655 | if (value == null) { |
| 656 | value = parser.getAttributeValue(null, attribute); |
| 657 | } |
| 658 | return value; |
| 659 | } |
| 660 | |
| 661 | /** |
| 662 | * Return attribute resource value, attempting launcher-specific namespace |
| 663 | * first before falling back to anonymous attribute. |
| 664 | */ |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 665 | protected static int getAttributeResourceValue(XmlResourceParser parser, String attribute, |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 666 | int defaultValue) { |
| 667 | int value = parser.getAttributeResourceValue( |
| 668 | "http://schemas.android.com/apk/res-auto/com.android.launcher3", attribute, |
| 669 | defaultValue); |
| 670 | if (value == defaultValue) { |
| 671 | value = parser.getAttributeResourceValue(null, attribute, defaultValue); |
| 672 | } |
| 673 | return value; |
| 674 | } |
| 675 | |
| 676 | public static interface LayoutParserCallback { |
| 677 | long generateNewItemId(); |
| 678 | |
| 679 | long insertAndCheck(SQLiteDatabase db, ContentValues values); |
| 680 | } |
| 681 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 682 | @Thunk static void copyInteger(ContentValues from, ContentValues to, String key) { |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 683 | to.put(key, from.getAsInteger(key)); |
| 684 | } |
| 685 | } |