下面列出了怎么用android.content.pm.ShortcutManager的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String action = getIntent().getAction();
if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
setResult(Activity.RESULT_OK, new Intent()
.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(this, QuickToggleShortcut.class))
.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.quick_toggle))
.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(this, R.mipmap.ic_launcher)));
finish();
} else {
mServiceBoundContext.attachService();
if (Build.VERSION.SDK_INT >= 25) {
ShortcutManager service = getSystemService(ShortcutManager.class);
if (service != null) {
service.reportShortcutUsed("toggle");
}
}
}
}
/**
* Adds a "New incognito tab" dynamic launcher shortcut.
* @param context The context used to retrieve the system {@link ShortcutManager}.
* @return True if addint the shortcut has succeeded. False if the call fails due to rate
* limiting. See {@link ShortcutManager#addDynamicShortcuts}.
*/
@TargetApi(Build.VERSION_CODES.N_MR1)
private static boolean addIncognitoLauncherShortcut(Context context) {
Intent intent = new Intent(LauncherShortcutActivity.ACTION_OPEN_NEW_INCOGNITO_TAB);
intent.setPackage(context.getPackageName());
intent.setClass(context, LauncherShortcutActivity.class);
ShortcutInfo shortcut =
new ShortcutInfo.Builder(context, DYNAMIC_OPEN_NEW_INCOGNITO_TAB_ID)
.setShortLabel(context.getResources().getString(
R.string.accessibility_tabstrip_incognito_identifier))
.setLongLabel(
context.getResources().getString(R.string.menu_new_incognito_tab))
.setIcon(Icon.createWithResource(context, R.drawable.shortcut_incognito))
.setIntent(intent)
.build();
ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
return shortcutManager.addDynamicShortcuts(Arrays.asList(shortcut));
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String action = getIntent().getAction();
if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
setResult(Activity.RESULT_OK, new Intent()
.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(this, QuickToggleShortcut.class))
.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.quick_toggle))
.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(this, R.mipmap.ic_launcher)));
finish();
} else {
mServiceBoundContext.attachService();
if (Build.VERSION.SDK_INT >= 25) {
ShortcutManager service = getSystemService(ShortcutManager.class);
if (service != null) {
service.reportShortcutUsed("toggle");
}
}
}
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_scanner);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(getTitle());
toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_material);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
navigateUp();
}
});
scannerView = (ZXingScannerView) findViewById(R.id.scanner);
if (Build.VERSION.SDK_INT >= 25) {
ShortcutManager service = getSystemService(ShortcutManager.class);
if (service != null) {
service.reportShortcutUsed("scan");
}
}
}
public static void reportAppShortcutUsage(Context context, String remoteName) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.N_MR1) {
return;
}
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
Set<String> appShortcutIds = sharedPreferences.getStringSet(context.getString(R.string.shared_preferences_app_shortcuts), new HashSet<String>());
String id = getUniqueIdFromString(remoteName);
if (appShortcutIds.contains(id)) {
ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
if (shortcutManager == null) {
return;
}
shortcutManager.reportShortcutUsed(id);
}
}
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
public static void addRemoteToAppShortcuts(Context context, RemoteItem remoteItem, String id) {
ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
if (shortcutManager == null) {
return;
}
Intent intent = new Intent(Intent.ACTION_MAIN, Uri.EMPTY, context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra(APP_SHORTCUT_REMOTE_NAME, remoteItem.getName());
ShortcutInfo shortcut = new ShortcutInfo.Builder(context, id)
.setShortLabel(remoteItem.getName())
.setIcon(Icon.createWithResource(context, AppShortcutsHelper.getRemoteIcon(remoteItem.getType(), remoteItem.isCrypt())))
.setIntent(intent)
.build();
shortcutManager.addDynamicShortcuts(Collections.singletonList(shortcut));
}
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
private void listing19_28(String destination) {
// Listing 19-28: Creating and adding dynamic App Shortcuts
ShortcutManager shortcutManager = (ShortcutManager) getSystemService(Context.SHORTCUT_SERVICE);
Intent navIntent = new Intent(this, MainActivity.class);
navIntent.setAction(Intent.ACTION_VIEW);
navIntent.putExtra(DESTINATION_EXTRA, destination);
String id = "dynamicDest" + destination;
ShortcutInfo shortcut =
new ShortcutInfo.Builder(this, id)
.setShortLabel(destination)
.setLongLabel("Navigate to " + destination)
.setDisabledMessage("Navigation Shortcut Disabled")
.setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
.setIntent(navIntent)
.build();
shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));
}
/**
* Register an app shortcut to launch the Gander UI directly from the launcher on Android 7.0 and above.
*
* @param context A valid {@link Context}
* @return The id of the added shortcut (<code>null</code> if this feature is not supported on the device).
* It can be used if you want to remove this shortcut later on.
*/
@TargetApi(Build.VERSION_CODES.N_MR1)
@SuppressWarnings("WeakerAccess")
@Nullable
public static String addAppShortcut(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
final String id = context.getPackageName() + ".gander_ui";
final ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
final ShortcutInfo shortcut = new ShortcutInfo.Builder(context, id).setShortLabel("Gander")
.setLongLabel("Open Gander")
.setIcon(Icon.createWithResource(context, R.drawable.gander_ic_shortcut_primary_24dp))
.setIntent(getLaunchIntent(context).setAction(Intent.ACTION_VIEW))
.build();
shortcutManager.addDynamicShortcuts(Collections.singletonList(shortcut));
return id;
} else {
return null;
}
}
public ShortcutHelper createShortcutList(@NonNull List<Shortcut> shortcuts) {
if (Build.VERSION.SDK_INT < 25) {
return this;
}
mShortcutManager = mActivity.getSystemService(ShortcutManager.class);
for (int i=0; i<shortcuts.size(); i++) {
if (i < mShortcutManager.getMaxShortcutCountPerActivity()) {
String shortcutId = shortcuts.get(i).getShortLabel().replaceAll("\\s+","").toLowerCase() + "_shortcut";
ShortcutInfo shortcut = new ShortcutInfo.Builder(mActivity, shortcutId)
.setShortLabel(shortcuts.get(i).getShortLabel())
.setLongLabel(shortcuts.get(i).getLongLabel())
.setIcon(Icon.createWithResource(mActivity, shortcuts.get(i).getIconResource()))
.setIntent(shortcuts.get(i).getIntent())
.build();
mShortcutInfos.add(shortcut);
}
}
return this;
}
public static int getNextRailNumber(ShortcutManager shortcutManager) {
List<ShortcutInfo> shortcuts = shortcutManager.getPinnedShortcuts();
int newRailId = -1;
for (ShortcutInfo shortcutInfo : shortcuts) {
String id = shortcutInfo.getId();
if (isRailShortcut(id)) {
int railId = getRailNumber(id);
if (railId > newRailId) {
newRailId = railId;
}
}
}
newRailId++;
return newRailId;
}
public static List<RailInfo> getRails(ShortcutManager shortcutManager) {
List<ShortcutInfo> shortcuts = shortcutManager.getPinnedShortcuts();
List<RailInfo> rails = new ArrayList<>();
for (ShortcutInfo shortcutInfo : shortcuts) {
String id = shortcutInfo.getId();
if (isRailShortcut(id)) {
PersistableBundle extras = shortcutInfo.getExtras();
if (extras != null) {
int[] posArray = extras.getIntArray(RailActionActivity.RAIL_RECT_KEY);
int rotation = extras.getInt(RailActionActivity.RAIL_ROTATION_KEY);
assert posArray != null;
rails.add(new RailInfo(posArray[0], posArray[1], rotation));
}
}
}
return rails;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wechat_contacts);
//Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
WechatFriendArrayList = MyApplication.getInstance().getWechatFriendArrayList();
WechatFriendGroups = MyApplication.getInstance().getWechatFriendGroups();
init();
// Android 7.1 用于提升Shortcut启动性能
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
ShortcutManager mShortcutManager = getSystemService(ShortcutManager.class);
mShortcutManager.reportShortcutUsed("static_wechat_contacts");
}
}
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
private void initAppShortcuts() {
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
if (shortcutManager == null) {
return;
}
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("action", "scan");
intent.setAction(Intent.ACTION_MAIN);
ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "shortcut_new")
.setShortLabel(getString(R.string.new_entry))
.setLongLabel(getString(R.string.add_new_entry))
.setIcon(Icon.createWithResource(this, R.drawable.ic_qr_code))
.setIntent(intent)
.build();
shortcutManager.setDynamicShortcuts(Collections.singletonList(shortcut));
}
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
private static void updataShortcuts(Context context, List<AppInfo> items) {
ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
List<ShortcutInfo> shortcutInfoList = new ArrayList<>();
int max = shortcutManager.getMaxShortcutCountPerActivity();
for (int i = 0; i < max && i < items.size(); i++) {
AppInfo appInfo = items.get(i);
ShortcutInfo.Builder shortcut = new ShortcutInfo.Builder(context, appInfo.packageName);
shortcut.setShortLabel(appInfo.appName);
shortcut.setLongLabel(appInfo.appName);
shortcut.setIcon(
Icon.createWithBitmap(drawableToBitmap(LocalImageLoader.getDrawable(context, appInfo))));
Intent intent = new Intent(context, AppPermissionActivity.class);
intent.putExtra(AppPermissionActivity.EXTRA_APP_PKGNAME, appInfo.packageName);
intent.putExtra(AppPermissionActivity.EXTRA_APP_NAME, appInfo.appName);
intent.setAction(Intent.ACTION_DEFAULT);
shortcut.setIntent(intent);
shortcutInfoList.add(shortcut.build());
}
shortcutManager.setDynamicShortcuts(shortcutInfoList);
}
public static void pinAppShortcut(Context context) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
ShortcutManager mShortcutManager = context.getSystemService(ShortcutManager.class);
if(mShortcutManager.isRequestPinShortcutSupported()) {
ShortcutInfo pinShortcutInfo = new ShortcutInfo.Builder(context, "freeform_mode").build();
mShortcutManager.requestPinShortcut(pinShortcutInfo, null);
} else
showToastLong(context, R.string.tb_pin_shortcut_not_supported);
} else {
Intent intent = ShortcutUtils.getShortcutIntent(context);
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
intent.putExtra("duplicate", false);
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);
ResolveInfo defaultLauncher = context.getPackageManager().resolveActivity(homeIntent, PackageManager.MATCH_DEFAULT_ONLY);
intent.setPackage(defaultLauncher.activityInfo.packageName);
context.sendBroadcast(intent);
showToast(context, R.string.tb_shortcut_created);
}
}
/**
* 启动器添加快捷方式
*
* @param context 上下文
* @param titleRes 标题资源id
* @param iconRes 图标资源id
* @param shortcutAct Shortcut额外
* @param isForce 是否是root强制模式
* @see com.ryuunoakaihitomi.rebootmenu.activity.Shortcut
*/
public static void addLauncherShortcut(@NonNull Context context, @StringRes int titleRes, @DrawableRes int iconRes, int shortcutAct, boolean isForce) {
new DebugLog("addLauncherShortcut", DebugLog.LogLevel.V);
String forceToken = isForce ? "*" : "";
String title = forceToken + context.getString(titleRes);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
context.sendBroadcast(new Intent("com.android.launcher.action.INSTALL_SHORTCUT")
.putExtra("duplicate", false)
.putExtra(Intent.EXTRA_SHORTCUT_NAME, title)
.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, iconRes))
.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(context, Shortcut.class)
.putExtra(Shortcut.extraTag, shortcutAct)));
else {
ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(context, "o_launcher_shortcut:" + shortcutAct)
.setShortLabel(title)
.setIcon(Icon.createWithResource(context, iconRes))
.setIntent(new Intent(context, Shortcut.class)
.putExtra(Shortcut.extraTag, shortcutAct)
.setAction(Intent.ACTION_VIEW))
.build();
new DebugLog("addLauncherShortcut: requestPinShortcut:"
+ context.getSystemService(ShortcutManager.class).requestPinShortcut(shortcutInfo, null));
}
}
/**
* Adds the given channel as shortcut to the launcher icon.
* Only call on api level >= 25.
*
* @param context to access system services
* @param channel channel to create a shortcut for
* @return true if the channel could be added
*/
@TargetApi(25)
public static boolean addShortcutForChannel(Context context, ChannelModel channel) {
ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
if (shortcutManager == null || shortcutManager.getDynamicShortcuts().size() >= 4) {
return false;
}
ShortcutInfo shortcut = new ShortcutInfo.Builder(context, channel.getId())
.setShortLabel(channel.getName())
.setLongLabel(channel.getName())
.setIcon(Icon.createWithResource(context, channel.getDrawableId()))
.setIntent(ChannelDetailActivity.getStartIntent(context, channel.getId()))
.build();
try {
return shortcutManager.addDynamicShortcuts(Collections.singletonList(shortcut));
} catch (IllegalArgumentException e) {
// too many shortcuts
return false;
}
}
public static void setShortcuts(Activity activity, SoundProfile[] soundProfiles) {
final List<ShortcutInfo> shortcutInfos = new ArrayList<>();
for (SoundProfile soundProfile : soundProfiles) {
if (!soundProfile.name.isEmpty()) {
shortcutInfos.add(createShortcutInfo(activity, soundProfile));
}
}
ShortcutManager shortcutManager = activity.getSystemService(ShortcutManager.class);
if (shortcutManager.getMaxShortcutCountPerActivity() < shortcutInfos.size()) {
int last = shortcutInfos.size() - 1;
int first = last - shortcutManager.getMaxShortcutCountPerActivity();
shortcutManager.setDynamicShortcuts(shortcutInfos.subList(first, last));
} else {
shortcutManager.setDynamicShortcuts(shortcutInfos);
}
}
private void setLauncherShortcuts() {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
if(shortcutManager.getDynamicShortcuts().size() == 0) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName(BuildConfig.APPLICATION_ID, TaskerQuickActionsActivity.class.getName());
intent.putExtra("launched-from-app", true);
ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "quick_actions")
.setShortLabel(getString(R.string.label_quick_actions))
.setIcon(Icon.createWithResource(this, R.drawable.shortcut_icon))
.setIntent(intent)
.build();
shortcutManager.setDynamicShortcuts(Collections.singletonList(shortcut));
}
}
}
private boolean isCreatedShortcut() {
if (DEBUG) {
Log.d(TAG, "DemoPageSetting: isCreatedShortcut");
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
ShortcutManager shortcutManager = mShortcutManager;
List<ShortcutInfo> infoList = shortcutManager.getPinnedShortcuts();
if (DEBUG) {
Log.d(TAG, "DemoPageSetting: isCreatedShortcut: PinnedShortcuts=" + infoList.size());
Log.d(TAG, "DemoPageSetting: isCreatedShortcut: DynamicShortcuts=" + shortcutManager.getDynamicShortcuts());
}
for (ShortcutInfo info : infoList) {
if (DEBUG) {
Log.d(TAG, "DemoPageSetting: isCreatedShortcut: info=" + info.getPackage());
}
if (info.getId().equals(CAMERA_DEMO_SHORTCUT_ID)) {
return true;
}
}
return false;
} else {
return false;
}
}
private void setShortcuts() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) return;
PriorityQueue<Class<? extends Fragment>> queue = new PriorityQueue<>(
(o1, o2) -> {
int opened1 = AppSettings.getFragmentOpened(o1, this);
int opened2 = AppSettings.getFragmentOpened(o2, this);
return opened2 - opened1;
});
for (Map.Entry<Integer, Class<? extends Fragment>> entry : mActualFragments.entrySet()) {
Class<? extends Fragment> fragmentClass = entry.getValue();
if (fragmentClass == null || fragmentClass == SettingsFragment.class) continue;
queue.offer(fragmentClass);
}
List<ShortcutInfo> shortcutInfos = new ArrayList<>();
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
shortcutManager.removeAllDynamicShortcuts();
for (int i = 0; i < 4; i++) {
NavigationFragment fragment = findNavigationFragmentByClass(queue.poll());
if (fragment == null || fragment.mFragmentClass == null) continue;
Intent intent = new Intent(this, MainActivity.class);
intent.setAction(Intent.ACTION_VIEW);
intent.putExtra(INTENT_SECTION, fragment.mFragmentClass.getCanonicalName());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
ShortcutInfo shortcut = new ShortcutInfo.Builder(this,
fragment.mFragmentClass.getSimpleName())
.setShortLabel(getString(fragment.mId))
.setLongLabel(Utils.strFormat(getString(R.string.open), getString(fragment.mId)))
.setIcon(Icon.createWithResource(this, fragment.mDrawable == 0 ?
R.drawable.ic_blank : fragment.mDrawable))
.setIntent(intent)
.build();
shortcutInfos.add(shortcut);
}
shortcutManager.setDynamicShortcuts(shortcutInfos);
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_quick_switch);
profilesAdapter = new ProfilesAdapter();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(R.string.quick_switch);
RecyclerView profilesList = (RecyclerView) findViewById(R.id.profilesList);
LinearLayoutManager lm = new LinearLayoutManager(this);
profilesList.setLayoutManager(lm);
profilesList.setItemAnimator(new DefaultItemAnimator());
profilesList.setAdapter(profilesAdapter);
if (app.profileId() >= 0) {
int position = 0;
List<Profile> profiles = profilesAdapter.profiles;
for (int i = 0; i < profiles.size(); i++) {
Profile profile = profiles.get(i);
if (profile.id == app.profileId()) {
position = i + 1;
break;
}
}
lm.scrollToPosition(position);
}
if (Build.VERSION.SDK_INT >= 25) {
ShortcutManager service = getSystemService(ShortcutManager.class);
if (service != null) {
service.reportShortcutUsed("switch");
}
}
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_quick_switch);
profilesAdapter = new ProfilesAdapter();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(R.string.quick_switch);
RecyclerView profilesList = (RecyclerView) findViewById(R.id.profilesList);
LinearLayoutManager lm = new LinearLayoutManager(this);
profilesList.setLayoutManager(lm);
profilesList.setItemAnimator(new DefaultItemAnimator());
profilesList.setAdapter(profilesAdapter);
if (ShadowsocksApplication.app.profileId() >= 0) {
int position = 0;
List<Profile> profiles = profilesAdapter.profiles;
for (int i = 0; i < profiles.size(); i++) {
Profile profile = profiles.get(i);
if (profile.id == ShadowsocksApplication.app.profileId()) {
position = i + 1;
break;
}
}
lm.scrollToPosition(position);
}
if (Build.VERSION.SDK_INT >= 25) {
ShortcutManager service = getSystemService(ShortcutManager.class);
if (service != null) {
service.reportShortcutUsed("switch");
}
}
}
@TargetApi(25)
public void report(Contact contact) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
ShortcutManager shortcutManager = xmppConnectionService.getSystemService(ShortcutManager.class);
shortcutManager.reportShortcutUsed(getShortcutId(contact));
}
}
private void createShortcuts() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
if (shortcutManager != null) {
shortcutManager.setDynamicShortcuts(
Arrays.asList(
getShortcutForTextType(),
getShortcutForFileType()
)
);
}
}
}
private void updateLauncherShortcut() {
// We will have one dynamic shortcut - to whichever stream was played last
new Thread(new Runnable() {
@Override
public void run() {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
JsonChannel jsonChannel = ChannelDatabase.getInstance(getApplicationContext())
.findChannelByMediaUrl(urlStream);
if (jsonChannel == null) {
// Don't create a shortcut because we don't have metadata
return;
}
Log.d(TAG, "Adding dynamic shortcut to " + jsonChannel.getName());
String logo = ChannelDatabase.getNonNullChannelLogo(jsonChannel);
try {
Bitmap logoBitmap = Glide.with(getApplicationContext())
.load(logo)
.asBitmap()
.into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
.get();
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
shortcutManager.removeAllDynamicShortcuts();
Intent playVideo = new Intent(getApplicationContext(), CumulusVideoPlayback.class);
playVideo.setAction("play");
playVideo.putExtra(KEY_VIDEO_URL, urlStream);
ShortcutInfo shortcut = new ShortcutInfo.Builder(CumulusVideoPlayback.this, "id1")
.setShortLabel(jsonChannel.getName()+"")
.setLongLabel(jsonChannel.getNumber() + " - " + jsonChannel.getName())
.setIcon(Icon.createWithBitmap(logoBitmap))
.setIntent(playVideo)
.build();
shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
}).start();
}
public static void removeAppShortcut(Context context, String remoteName) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.N_MR1) {
return;
}
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
Set<String> appShortcutIds = sharedPreferences.getStringSet(context.getString(R.string.shared_preferences_app_shortcuts), new HashSet<String>());
String id = getUniqueIdFromString(remoteName);
if (!appShortcutIds.contains(id)) {
return;
}
ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
if (shortcutManager == null) {
return;
}
List<ShortcutInfo> shortcutInfoList = shortcutManager.getDynamicShortcuts();
for (ShortcutInfo shortcutInfo : shortcutInfoList) {
if (shortcutInfo.getId().equals(id)) {
shortcutManager.removeDynamicShortcuts(Collections.singletonList(shortcutInfo.getId()));
return;
}
}
SharedPreferences.Editor editor = sharedPreferences.edit();
appShortcutIds.remove(id);
Set<String> updateAppShortcutIds = new HashSet<>(appShortcutIds);
editor.putStringSet(context.getString(R.string.shared_preferences_app_shortcuts), updateAppShortcutIds);
editor.apply();
}
public static void removeAppShortcutIds(Context context, List<String> ids) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.N_MR1) {
return;
}
ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
if (shortcutManager == null) {
return;
}
shortcutManager.removeDynamicShortcuts(ids);
}
public static void setApplicationShortcuts(Context context) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
SQRLStorage sqrlStorage = SQRLStorage.getInstance(context);
if (getCurrentId(context) > 0) {
ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
if (sqrlStorage.hasQuickPass()) {
shortcutManager.setDynamicShortcuts(Arrays.asList(scanShortcut, clearQuickPassShortcut));
} else {
shortcutManager.setDynamicShortcuts(Arrays.asList(scanShortcut, logonShortcut));
}
}
}
}
public static void createLauncherShortcut(Context context, Intent launchIntent, Icon icon, String id, String label) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
if (shortcutManager.isRequestPinShortcutSupported()) {
ShortcutInfo info = new ShortcutInfo.Builder(context, id)
.setIntent(launchIntent)
.setIcon(icon)
.setShortLabel(label)
.setLongLabel(label)
.build();
Intent addIntent = shortcutManager.createShortcutResultIntent(info);
shortcutManager.requestPinShortcut(info,
PendingIntent.getBroadcast(context, 0, addIntent, 0).getIntentSender());
} else {
// TODO: Maybe implement this for launchers without pin shortcut support?
// TODO: Should be the same with the fallback for Android < O
// for now just show unsupported
Toast.makeText(context, context.getString(R.string.unsupported_launcher), Toast.LENGTH_LONG).show();
}
} else {
Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, label);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, drawableToBitmap(icon.loadDrawable(context)));
context.sendBroadcast(shortcutIntent);
Toast.makeText(context, R.string.shortcut_create_success, Toast.LENGTH_SHORT).show();
}
}