Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 1 | package com.android.launcher3; |
| 2 | |
Sunny Goyal | 86df138 | 2016-08-10 15:03:22 -0700 | [diff] [blame] | 3 | import android.appwidget.AppWidgetManager; |
| 4 | import android.content.ComponentName; |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 5 | import android.content.Context; |
| 6 | import android.content.Intent; |
| 7 | import android.content.pm.ActivityInfo; |
| 8 | import android.content.pm.ApplicationInfo; |
My Name | 2f5a1b6 | 2022-04-01 11:11:57 +0000 | [diff] [blame] | 9 | import android.content.pm.LauncherApps; |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 10 | import android.content.pm.PackageManager; |
| 11 | import android.content.pm.ResolveInfo; |
| 12 | import android.content.res.Resources; |
Sunny Goyal | 86df138 | 2016-08-10 15:03:22 -0700 | [diff] [blame] | 13 | import android.os.Bundle; |
My Name | 2f5a1b6 | 2022-04-01 11:11:57 +0000 | [diff] [blame] | 14 | import android.os.Process; |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 15 | import android.text.TextUtils; |
Rajeev Kumar | 26453a2 | 2017-06-09 16:02:25 -0700 | [diff] [blame] | 16 | import android.util.ArrayMap; |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 17 | import android.util.Log; |
Samuel Fufa | ca37b8a | 2019-08-19 17:04:36 -0700 | [diff] [blame] | 18 | |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 19 | import com.android.launcher3.LauncherSettings.Favorites; |
My Name | 2f5a1b6 | 2022-04-01 11:11:57 +0000 | [diff] [blame] | 20 | import com.android.launcher3.model.data.WorkspaceItemInfo; |
| 21 | import com.android.launcher3.shortcuts.ShortcutKey; |
Sunny Goyal | 3e58eea | 2022-11-14 14:30:07 -0800 | [diff] [blame^] | 22 | import com.android.launcher3.util.Partner; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 23 | import com.android.launcher3.util.Thunk; |
Sihua Ma | 8bbfcb6 | 2022-11-08 16:46:07 -0800 | [diff] [blame] | 24 | import com.android.launcher3.widget.LauncherWidgetHolder; |
Samuel Fufa | ca37b8a | 2019-08-19 17:04:36 -0700 | [diff] [blame] | 25 | |
| 26 | import org.xmlpull.v1.XmlPullParser; |
| 27 | import org.xmlpull.v1.XmlPullParserException; |
| 28 | |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 29 | import java.io.IOException; |
| 30 | import java.net.URISyntaxException; |
My Name | 2f5a1b6 | 2022-04-01 11:11:57 +0000 | [diff] [blame] | 31 | import java.util.Collections; |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 32 | import java.util.List; |
| 33 | |
| 34 | /** |
| 35 | * Implements the layout parser with rules for internal layouts and partner layouts. |
| 36 | */ |
| 37 | public class DefaultLayoutParser extends AutoInstallsLayout { |
| 38 | private static final String TAG = "DefaultLayoutParser"; |
| 39 | |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 40 | protected static final String TAG_RESOLVE = "resolve"; |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 41 | private static final String TAG_FAVORITES = "favorites"; |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 42 | protected static final String TAG_FAVORITE = "favorite"; |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 43 | private static final String TAG_APPWIDGET = "appwidget"; |
Sunny Goyal | a5c8a9e | 2016-07-08 08:32:44 -0700 | [diff] [blame] | 44 | protected static final String TAG_SHORTCUT = "shortcut"; |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 45 | private static final String TAG_FOLDER = "folder"; |
| 46 | private static final String TAG_PARTNER_FOLDER = "partner-folder"; |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 47 | |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 48 | protected static final String ATTR_URI = "uri"; |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 49 | private static final String ATTR_CONTAINER = "container"; |
| 50 | private static final String ATTR_SCREEN = "screen"; |
| 51 | private static final String ATTR_FOLDER_ITEMS = "folderItems"; |
My Name | 2f5a1b6 | 2022-04-01 11:11:57 +0000 | [diff] [blame] | 52 | private static final String ATTR_SHORTCUT_ID = "shortcutId"; |
| 53 | private static final String ATTR_PACKAGE_NAME = "packageName"; |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 54 | |
Sunny Goyal | 3e58eea | 2022-11-14 14:30:07 -0800 | [diff] [blame^] | 55 | public static final String RES_PARTNER_FOLDER = "partner_folder"; |
| 56 | public static final String RES_PARTNER_DEFAULT_LAYOUT = "partner_default_layout"; |
| 57 | |
Sunny Goyal | 86df138 | 2016-08-10 15:03:22 -0700 | [diff] [blame] | 58 | // TODO: Remove support for this broadcast, instead use widget options to send bind time options |
| 59 | private static final String ACTION_APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE = |
| 60 | "com.android.launcher.action.APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE"; |
| 61 | |
Sihua Ma | aa2b872 | 2022-10-25 15:17:58 -0700 | [diff] [blame] | 62 | public DefaultLayoutParser(Context context, LauncherWidgetHolder appWidgetHolder, |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 63 | LayoutParserCallback callback, Resources sourceRes, int layoutId) { |
Sihua Ma | aa2b872 | 2022-10-25 15:17:58 -0700 | [diff] [blame] | 64 | super(context, appWidgetHolder, callback, sourceRes, layoutId, TAG_FAVORITES); |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 67 | @Override |
Rajeev Kumar | 26453a2 | 2017-06-09 16:02:25 -0700 | [diff] [blame] | 68 | protected ArrayMap<String, TagParser> getFolderElementsMap() { |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 69 | return getFolderElementsMap(mSourceRes); |
| 70 | } |
| 71 | |
Samuel Fufa | ca37b8a | 2019-08-19 17:04:36 -0700 | [diff] [blame] | 72 | @Thunk |
| 73 | ArrayMap<String, TagParser> getFolderElementsMap(Resources res) { |
Rajeev Kumar | 26453a2 | 2017-06-09 16:02:25 -0700 | [diff] [blame] | 74 | ArrayMap<String, TagParser> parsers = new ArrayMap<>(); |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 75 | parsers.put(TAG_FAVORITE, new AppShortcutWithUriParser()); |
| 76 | parsers.put(TAG_SHORTCUT, new UriShortcutParser(res)); |
| 77 | return parsers; |
| 78 | } |
| 79 | |
| 80 | @Override |
Rajeev Kumar | 26453a2 | 2017-06-09 16:02:25 -0700 | [diff] [blame] | 81 | protected ArrayMap<String, TagParser> getLayoutElementsMap() { |
| 82 | ArrayMap<String, TagParser> parsers = new ArrayMap<>(); |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 83 | parsers.put(TAG_FAVORITE, new AppShortcutWithUriParser()); |
| 84 | parsers.put(TAG_APPWIDGET, new AppWidgetParser()); |
Samuel Fufa | ca37b8a | 2019-08-19 17:04:36 -0700 | [diff] [blame] | 85 | parsers.put(TAG_SEARCH_WIDGET, new SearchWidgetParser()); |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 86 | parsers.put(TAG_SHORTCUT, new UriShortcutParser(mSourceRes)); |
| 87 | parsers.put(TAG_RESOLVE, new ResolveParser()); |
| 88 | parsers.put(TAG_FOLDER, new MyFolderParser()); |
| 89 | parsers.put(TAG_PARTNER_FOLDER, new PartnerFolderParser()); |
| 90 | return parsers; |
| 91 | } |
| 92 | |
| 93 | @Override |
Sunny Goyal | 0d74231 | 2019-03-04 20:22:26 -0800 | [diff] [blame] | 94 | protected void parseContainerAndScreen(XmlPullParser parser, int[] out) { |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 95 | out[0] = LauncherSettings.Favorites.CONTAINER_DESKTOP; |
| 96 | String strContainer = getAttributeValue(parser, ATTR_CONTAINER); |
| 97 | if (strContainer != null) { |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 98 | out[0] = Integer.parseInt(strContainer); |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 99 | } |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 100 | out[1] = Integer.parseInt(getAttributeValue(parser, ATTR_SCREEN)); |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 103 | /** |
| 104 | * AppShortcutParser which also supports adding URI based intents |
| 105 | */ |
Sunny Goyal | a5c8a9e | 2016-07-08 08:32:44 -0700 | [diff] [blame] | 106 | public class AppShortcutWithUriParser extends AppShortcutParser { |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 107 | |
| 108 | @Override |
Sunny Goyal | 0d74231 | 2019-03-04 20:22:26 -0800 | [diff] [blame] | 109 | protected int invalidPackageOrClass(XmlPullParser parser) { |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 110 | final String uri = getAttributeValue(parser, ATTR_URI); |
| 111 | if (TextUtils.isEmpty(uri)) { |
| 112 | Log.e(TAG, "Skipping invalid <favorite> with no component or uri"); |
| 113 | return -1; |
| 114 | } |
| 115 | |
| 116 | final Intent metaIntent; |
| 117 | try { |
| 118 | metaIntent = Intent.parseUri(uri, 0); |
| 119 | } catch (URISyntaxException e) { |
| 120 | Log.e(TAG, "Unable to add meta-favorite: " + uri, e); |
| 121 | return -1; |
| 122 | } |
| 123 | |
| 124 | ResolveInfo resolved = mPackageManager.resolveActivity(metaIntent, |
| 125 | PackageManager.MATCH_DEFAULT_ONLY); |
| 126 | final List<ResolveInfo> appList = mPackageManager.queryIntentActivities( |
| 127 | metaIntent, PackageManager.MATCH_DEFAULT_ONLY); |
| 128 | |
| 129 | // Verify that the result is an app and not just the resolver dialog asking which |
| 130 | // app to use. |
| 131 | if (wouldLaunchResolverActivity(resolved, appList)) { |
| 132 | // If only one of the results is a system app then choose that as the default. |
| 133 | final ResolveInfo systemApp = getSingleSystemActivity(appList); |
| 134 | if (systemApp == null) { |
| 135 | // There is no logical choice for this meta-favorite, so rather than making |
| 136 | // a bad choice just add nothing. |
| 137 | Log.w(TAG, "No preference or single system activity found for " |
| 138 | + metaIntent.toString()); |
| 139 | return -1; |
| 140 | } |
| 141 | resolved = systemApp; |
| 142 | } |
| 143 | final ActivityInfo info = resolved.activityInfo; |
| 144 | final Intent intent = mPackageManager.getLaunchIntentForPackage(info.packageName); |
| 145 | if (intent == null) { |
| 146 | return -1; |
| 147 | } |
| 148 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | |
| 149 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
| 150 | |
| 151 | return addShortcut(info.loadLabel(mPackageManager).toString(), intent, |
| 152 | Favorites.ITEM_TYPE_APPLICATION); |
| 153 | } |
| 154 | |
| 155 | private ResolveInfo getSingleSystemActivity(List<ResolveInfo> appList) { |
| 156 | ResolveInfo systemResolve = null; |
| 157 | final int N = appList.size(); |
| 158 | for (int i = 0; i < N; ++i) { |
| 159 | try { |
| 160 | ApplicationInfo info = mPackageManager.getApplicationInfo( |
| 161 | appList.get(i).activityInfo.packageName, 0); |
| 162 | if ((info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 163 | if (systemResolve != null) { |
| 164 | return null; |
| 165 | } else { |
| 166 | systemResolve = appList.get(i); |
| 167 | } |
| 168 | } |
| 169 | } catch (PackageManager.NameNotFoundException e) { |
| 170 | Log.w(TAG, "Unable to get info about resolve results", e); |
| 171 | return null; |
| 172 | } |
| 173 | } |
| 174 | return systemResolve; |
| 175 | } |
| 176 | |
| 177 | private boolean wouldLaunchResolverActivity(ResolveInfo resolved, |
| 178 | List<ResolveInfo> appList) { |
| 179 | // If the list contains the above resolved activity, then it can't be |
| 180 | // ResolverActivity itself. |
| 181 | for (int i = 0; i < appList.size(); ++i) { |
| 182 | ResolveInfo tmp = appList.get(i); |
| 183 | if (tmp.activityInfo.name.equals(resolved.activityInfo.name) |
| 184 | && tmp.activityInfo.packageName.equals(resolved.activityInfo.packageName)) { |
| 185 | return false; |
| 186 | } |
| 187 | } |
| 188 | return true; |
| 189 | } |
| 190 | } |
| 191 | |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 192 | /** |
| 193 | * Shortcut parser which allows any uri and not just web urls. |
| 194 | */ |
Sunny Goyal | a5c8a9e | 2016-07-08 08:32:44 -0700 | [diff] [blame] | 195 | public class UriShortcutParser extends ShortcutParser { |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 196 | |
| 197 | public UriShortcutParser(Resources iconRes) { |
| 198 | super(iconRes); |
| 199 | } |
| 200 | |
| 201 | @Override |
My Name | 2f5a1b6 | 2022-04-01 11:11:57 +0000 | [diff] [blame] | 202 | public int parseAndAdd(XmlPullParser parser) { |
| 203 | final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME); |
| 204 | final String shortcutId = getAttributeValue(parser, ATTR_SHORTCUT_ID); |
| 205 | if (!TextUtils.isEmpty(packageName) && !TextUtils.isEmpty(shortcutId)) { |
| 206 | return parseAndAddDeepShortcut(shortcutId, packageName); |
| 207 | } |
| 208 | return super.parseAndAdd(parser); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * This method parses and adds a deep shortcut. |
| 213 | * @return item id if the shortcut is successfully added else -1 |
| 214 | */ |
My Name | 9a6dbac | 2022-04-28 05:11:01 +0000 | [diff] [blame] | 215 | private int parseAndAddDeepShortcut(String shortcutId, String packageName) { |
My Name | 2f5a1b6 | 2022-04-01 11:11:57 +0000 | [diff] [blame] | 216 | try { |
| 217 | LauncherApps launcherApps = mContext.getSystemService(LauncherApps.class); |
| 218 | launcherApps.pinShortcuts(packageName, Collections.singletonList(shortcutId), |
| 219 | Process.myUserHandle()); |
| 220 | Intent intent = ShortcutKey.makeIntent(shortcutId, packageName); |
| 221 | mValues.put(Favorites.RESTORED, WorkspaceItemInfo.FLAG_RESTORED_ICON); |
| 222 | return addShortcut(null, intent, Favorites.ITEM_TYPE_DEEP_SHORTCUT); |
| 223 | } catch (Exception e) { |
| 224 | Log.e(TAG, "Unable to pin the shortcut for shortcut id = " + shortcutId |
| 225 | + " and package name = " + packageName); |
| 226 | } |
| 227 | return -1; |
| 228 | } |
| 229 | |
| 230 | @Override |
Sunny Goyal | 0d74231 | 2019-03-04 20:22:26 -0800 | [diff] [blame] | 231 | protected Intent parseIntent(XmlPullParser parser) { |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 232 | String uri = null; |
| 233 | try { |
| 234 | uri = getAttributeValue(parser, ATTR_URI); |
| 235 | return Intent.parseUri(uri, 0); |
| 236 | } catch (URISyntaxException e) { |
| 237 | Log.w(TAG, "Shortcut has malformed uri: " + uri); |
| 238 | return null; // Oh well |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Contains a list of <favorite> nodes, and accepts the first successfully parsed node. |
| 245 | */ |
Sunny Goyal | a5c8a9e | 2016-07-08 08:32:44 -0700 | [diff] [blame] | 246 | public class ResolveParser implements TagParser { |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 247 | |
| 248 | private final AppShortcutWithUriParser mChildParser = new AppShortcutWithUriParser(); |
| 249 | |
| 250 | @Override |
Sunny Goyal | 0d74231 | 2019-03-04 20:22:26 -0800 | [diff] [blame] | 251 | public int parseAndAdd(XmlPullParser parser) throws XmlPullParserException, |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 252 | IOException { |
| 253 | final int groupDepth = parser.getDepth(); |
| 254 | int type; |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 255 | int addedId = -1; |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 256 | while ((type = parser.next()) != XmlPullParser.END_TAG || |
| 257 | parser.getDepth() > groupDepth) { |
| 258 | if (type != XmlPullParser.START_TAG || addedId > -1) { |
| 259 | continue; |
| 260 | } |
| 261 | final String fallback_item_name = parser.getName(); |
| 262 | if (TAG_FAVORITE.equals(fallback_item_name)) { |
| 263 | addedId = mChildParser.parseAndAdd(parser); |
| 264 | } else { |
| 265 | Log.e(TAG, "Fallback groups can contain only favorites, found " |
| 266 | + fallback_item_name); |
| 267 | } |
| 268 | } |
| 269 | return addedId; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * A parser which adds a folder whose contents come from partner apk. |
| 275 | */ |
Samuel Fufa | ca37b8a | 2019-08-19 17:04:36 -0700 | [diff] [blame] | 276 | @Thunk |
| 277 | class PartnerFolderParser implements TagParser { |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 278 | |
| 279 | @Override |
Sunny Goyal | 0d74231 | 2019-03-04 20:22:26 -0800 | [diff] [blame] | 280 | public int parseAndAdd(XmlPullParser parser) throws XmlPullParserException, |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 281 | IOException { |
| 282 | // Folder contents come from an external XML resource |
| 283 | final Partner partner = Partner.get(mPackageManager); |
| 284 | if (partner != null) { |
Sunny Goyal | 3e58eea | 2022-11-14 14:30:07 -0800 | [diff] [blame^] | 285 | final int resId = partner.getXmlResId(RES_PARTNER_FOLDER); |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 286 | if (resId != 0) { |
Sunny Goyal | 3e58eea | 2022-11-14 14:30:07 -0800 | [diff] [blame^] | 287 | final Resources partnerRes = partner.getResources(); |
Sunny Goyal | 0d74231 | 2019-03-04 20:22:26 -0800 | [diff] [blame] | 288 | final XmlPullParser partnerParser = partnerRes.getXml(resId); |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 289 | beginDocument(partnerParser, TAG_FOLDER); |
| 290 | |
| 291 | FolderParser folderParser = new FolderParser(getFolderElementsMap(partnerRes)); |
| 292 | return folderParser.parseAndAdd(partnerParser); |
| 293 | } |
| 294 | } |
| 295 | return -1; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * An extension of FolderParser which allows adding items from a different xml. |
| 301 | */ |
Samuel Fufa | ca37b8a | 2019-08-19 17:04:36 -0700 | [diff] [blame] | 302 | @Thunk |
| 303 | class MyFolderParser extends FolderParser { |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 304 | |
| 305 | @Override |
Sunny Goyal | 0d74231 | 2019-03-04 20:22:26 -0800 | [diff] [blame] | 306 | public int parseAndAdd(XmlPullParser parser) throws XmlPullParserException, |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 307 | IOException { |
| 308 | final int resId = getAttributeResourceValue(parser, ATTR_FOLDER_ITEMS, 0); |
| 309 | if (resId != 0) { |
| 310 | parser = mSourceRes.getXml(resId); |
| 311 | beginDocument(parser, TAG_FOLDER); |
| 312 | } |
| 313 | return super.parseAndAdd(parser); |
| 314 | } |
| 315 | } |
Sunny Goyal | 86df138 | 2016-08-10 15:03:22 -0700 | [diff] [blame] | 316 | |
| 317 | |
| 318 | /** |
| 319 | * AppWidget parser which enforces that the app is already installed when the layout is parsed. |
| 320 | */ |
| 321 | protected class AppWidgetParser extends PendingWidgetParser { |
| 322 | |
| 323 | @Override |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 324 | protected int verifyAndInsert(ComponentName cn, Bundle extras) { |
Sunny Goyal | 86df138 | 2016-08-10 15:03:22 -0700 | [diff] [blame] | 325 | try { |
| 326 | mPackageManager.getReceiverInfo(cn, 0); |
| 327 | } catch (Exception e) { |
| 328 | String[] packages = mPackageManager.currentToCanonicalPackageNames( |
Samuel Fufa | ca37b8a | 2019-08-19 17:04:36 -0700 | [diff] [blame] | 329 | new String[]{cn.getPackageName()}); |
Sunny Goyal | 86df138 | 2016-08-10 15:03:22 -0700 | [diff] [blame] | 330 | cn = new ComponentName(packages[0], cn.getClassName()); |
| 331 | try { |
| 332 | mPackageManager.getReceiverInfo(cn, 0); |
| 333 | } catch (Exception e1) { |
| 334 | Log.d(TAG, "Can't find widget provider: " + cn.getClassName()); |
| 335 | return -1; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext); |
Sunny Goyal | efb7e84 | 2018-10-04 15:11:00 -0700 | [diff] [blame] | 340 | int insertedId = -1; |
Sunny Goyal | 86df138 | 2016-08-10 15:03:22 -0700 | [diff] [blame] | 341 | try { |
Sihua Ma | aa2b872 | 2022-10-25 15:17:58 -0700 | [diff] [blame] | 342 | int appWidgetId = mAppWidgetHolder.allocateAppWidgetId(); |
Sunny Goyal | 86df138 | 2016-08-10 15:03:22 -0700 | [diff] [blame] | 343 | |
| 344 | if (!appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, cn)) { |
| 345 | Log.e(TAG, "Unable to bind app widget id " + cn); |
Sihua Ma | aa2b872 | 2022-10-25 15:17:58 -0700 | [diff] [blame] | 346 | mAppWidgetHolder.deleteAppWidgetId(appWidgetId); |
Sunny Goyal | 86df138 | 2016-08-10 15:03:22 -0700 | [diff] [blame] | 347 | return -1; |
| 348 | } |
| 349 | |
| 350 | mValues.put(Favorites.APPWIDGET_ID, appWidgetId); |
| 351 | mValues.put(Favorites.APPWIDGET_PROVIDER, cn.flattenToString()); |
| 352 | mValues.put(Favorites._ID, mCallback.generateNewItemId()); |
| 353 | insertedId = mCallback.insertAndCheck(mDb, mValues); |
| 354 | if (insertedId < 0) { |
Sihua Ma | aa2b872 | 2022-10-25 15:17:58 -0700 | [diff] [blame] | 355 | mAppWidgetHolder.deleteAppWidgetId(appWidgetId); |
Sunny Goyal | 86df138 | 2016-08-10 15:03:22 -0700 | [diff] [blame] | 356 | return insertedId; |
| 357 | } |
| 358 | |
| 359 | // Send a broadcast to configure the widget |
| 360 | if (!extras.isEmpty()) { |
| 361 | Intent intent = new Intent(ACTION_APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE); |
| 362 | intent.setComponent(cn); |
| 363 | intent.putExtras(extras); |
| 364 | intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); |
| 365 | mContext.sendBroadcast(intent); |
| 366 | } |
| 367 | } catch (RuntimeException ex) { |
| 368 | Log.e(TAG, "Problem allocating appWidgetId", ex); |
| 369 | } |
| 370 | return insertedId; |
| 371 | } |
| 372 | } |
Sunny Goyal | 3a5a9d1 | 2014-10-01 15:33:41 -0700 | [diff] [blame] | 373 | } |