Adding support to parse deep-shortcuts.
Actual problem space is to prebundle YouTube Shorts shortcut to the
JioPhone Next device. The parsers don't support parsing the deep shortcut. In this CL we have added support for parsing deep-shortcuts.
Test: Tested manually on device.
Bug: 227290255
Change-Id: I7ea240a29639b88254fa679733541ff98ddfaa48
diff --git a/src/com/android/launcher3/DefaultLayoutParser.java b/src/com/android/launcher3/DefaultLayoutParser.java
index af85594..d8fba8b 100644
--- a/src/com/android/launcher3/DefaultLayoutParser.java
+++ b/src/com/android/launcher3/DefaultLayoutParser.java
@@ -7,15 +7,19 @@
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
+import android.content.pm.LauncherApps;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.os.Bundle;
+import android.os.Process;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Log;
import com.android.launcher3.LauncherSettings.Favorites;
+import com.android.launcher3.model.data.WorkspaceItemInfo;
+import com.android.launcher3.shortcuts.ShortcutKey;
import com.android.launcher3.util.Thunk;
import org.xmlpull.v1.XmlPullParser;
@@ -23,6 +27,7 @@
import java.io.IOException;
import java.net.URISyntaxException;
+import java.util.Collections;
import java.util.List;
/**
@@ -43,6 +48,8 @@
private static final String ATTR_CONTAINER = "container";
private static final String ATTR_SCREEN = "screen";
private static final String ATTR_FOLDER_ITEMS = "folderItems";
+ private static final String ATTR_SHORTCUT_ID = "shortcutId";
+ private static final String ATTR_PACKAGE_NAME = "packageName";
// TODO: Remove support for this broadcast, instead use widget options to send bind time options
private static final String ACTION_APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE =
@@ -178,7 +185,6 @@
}
}
-
/**
* Shortcut parser which allows any uri and not just web urls.
*/
@@ -189,6 +195,35 @@
}
@Override
+ public int parseAndAdd(XmlPullParser parser) {
+ final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
+ final String shortcutId = getAttributeValue(parser, ATTR_SHORTCUT_ID);
+ if (!TextUtils.isEmpty(packageName) && !TextUtils.isEmpty(shortcutId)) {
+ return parseAndAddDeepShortcut(shortcutId, packageName);
+ }
+ return super.parseAndAdd(parser);
+ }
+
+ /**
+ * This method parses and adds a deep shortcut.
+ * @return item id if the shortcut is successfully added else -1
+ */
+ public int parseAndAddDeepShortcut(String shortcutId, String packageName) {
+ try {
+ LauncherApps launcherApps = mContext.getSystemService(LauncherApps.class);
+ launcherApps.pinShortcuts(packageName, Collections.singletonList(shortcutId),
+ Process.myUserHandle());
+ Intent intent = ShortcutKey.makeIntent(shortcutId, packageName);
+ mValues.put(Favorites.RESTORED, WorkspaceItemInfo.FLAG_RESTORED_ICON);
+ return addShortcut(null, intent, Favorites.ITEM_TYPE_DEEP_SHORTCUT);
+ } catch (Exception e) {
+ Log.e(TAG, "Unable to pin the shortcut for shortcut id = " + shortcutId
+ + " and package name = " + packageName);
+ }
+ return -1;
+ }
+
+ @Override
protected Intent parseIntent(XmlPullParser parser) {
String uri = null;
try {
diff --git a/src/com/android/launcher3/shortcuts/ShortcutKey.java b/src/com/android/launcher3/shortcuts/ShortcutKey.java
index 0c6d675..9af68c0 100644
--- a/src/com/android/launcher3/shortcuts/ShortcutKey.java
+++ b/src/com/android/launcher3/shortcuts/ShortcutKey.java
@@ -57,11 +57,17 @@
}
public static Intent makeIntent(ShortcutInfo si) {
+ return makeIntent(si.getId(), si.getPackage()).setComponent(si.getActivity());
+ }
+
+ /**
+ * Creates an intent for shortcut id and package name.
+ */
+ public static Intent makeIntent(String shortcutId, String packageName) {
return new Intent(Intent.ACTION_MAIN)
.addCategory(INTENT_CATEGORY)
- .setComponent(si.getActivity())
- .setPackage(si.getPackage())
+ .setPackage(packageName)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED)
- .putExtra(EXTRA_SHORTCUT_ID, si.getId());
+ .putExtra(EXTRA_SHORTCUT_ID, shortcutId);
}
}