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