This repository has been archived by the owner on Jan 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDynamicShortcutHelper.java
72 lines (56 loc) · 2.34 KB
/
DynamicShortcutHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package xyz.klinker.nougatplayground;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Icon;
import android.net.Uri;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import xyz.klinker.nougatplayground.util.DensityUtil;
import xyz.klinker.nougatplayground.util.ImageUtil;
public class DynamicShortcutHelper {
private Context context;
private ShortcutManager manager;
@SuppressWarnings("WrongConstant")
public DynamicShortcutHelper(Context context) {
this.context = context;
manager = (ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE);
}
public void buildDynamicShortcuts(String[] titles) {
buildDynamicShortcuts(Arrays.asList(titles));
}
public void buildDynamicShortcuts(List<String> titles) {
List<ShortcutInfo> infos = new ArrayList<>();
for (String title : titles) {
Intent messenger = new Intent(context, MainActivity.class);
messenger.setAction(Intent.ACTION_VIEW);
// example of a web link that you can open in your app
// should probably be some kind of ID or something representing the shortcut you clicked
messenger.setData(Uri.parse("https://klinkerapps.com/" + title.toLowerCase(Locale.US).replace(" ", "_")));
Set<String> category = new HashSet<>();
category.add("android.shortcut.conversation");
ShortcutInfo info = new ShortcutInfo.Builder(context, title)
.setIntent(messenger)
.setRank(infos.size())
.setShortLabel(title)
.setCategories(category)
.setIcon(getIcon())
.build();
infos.add(info);
}
manager.setDynamicShortcuts(infos);
}
private Icon getIcon() {
Bitmap color = Bitmap.createBitmap(DensityUtil.toDp(context, 148), DensityUtil.toDp(context, 148), Bitmap.Config.RGB_565);
color.eraseColor(Color.BLUE);
color = ImageUtil.clipToCircle(color);
return Icon.createWithBitmap(color);
}
}