Skip to content

Commit 62486ae

Browse files
committed
Fixed broken home launcher shortcuts on Oreo
1 parent 6610564 commit 62486ae

1 file changed

Lines changed: 40 additions & 13 deletions

File tree

omniNotes/src/main/java/it/feio/android/omninotes/utils/ShortcutHelper.java

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,19 @@
1717

1818
package it.feio.android.omninotes.utils;
1919

20+
import android.app.PendingIntent;
2021
import android.content.Context;
2122
import android.content.Intent;
23+
import android.content.pm.ShortcutInfo;
24+
import android.content.pm.ShortcutManager;
25+
import android.graphics.drawable.Icon;
26+
import android.os.Build;
27+
2228
import it.feio.android.omninotes.MainActivity;
2329
import it.feio.android.omninotes.OmniNotes;
2430
import it.feio.android.omninotes.R;
2531
import it.feio.android.omninotes.helpers.date.DateHelper;
2632
import it.feio.android.omninotes.models.Note;
27-
import it.feio.android.omninotes.utils.date.DateUtils;
2833

2934

3035
public class ShortcutHelper {
@@ -34,21 +39,43 @@ public class ShortcutHelper {
3439
* Adding shortcut on Home screen
3540
*/
3641
public static void addShortcut(Context context, Note note) {
37-
Intent shortcutIntent = new Intent(context, MainActivity.class);
38-
shortcutIntent.putExtra(Constants.INTENT_KEY, note.get_id());
39-
shortcutIntent.setAction(Constants.ACTION_SHORTCUT);
4042

41-
Intent addIntent = new Intent();
42-
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
4343
String shortcutTitle = note.getTitle().length() > 0 ? note.getTitle() : DateHelper.getFormattedDate(note
44-
.getCreation(), OmniNotes.getSharedPreferences().getBoolean(Constants
45-
.PREF_PRETTIFIED_DATES, true));
46-
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutTitle);
47-
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
48-
Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_shortcut));
49-
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
44+
.getCreation(), OmniNotes.getSharedPreferences().getBoolean(Constants
45+
.PREF_PRETTIFIED_DATES, true));
5046

51-
context.sendBroadcast(addIntent);
47+
if(Build.VERSION.SDK_INT < 26) {
48+
Intent shortcutIntent = new Intent(context, MainActivity.class);
49+
shortcutIntent.putExtra(Constants.INTENT_KEY, note.get_id());
50+
shortcutIntent.setAction(Constants.ACTION_SHORTCUT);
51+
52+
Intent addIntent = new Intent();
53+
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
54+
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutTitle);
55+
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
56+
Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_shortcut));
57+
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
58+
59+
context.sendBroadcast(addIntent);
60+
}
61+
else {
62+
ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
63+
if (shortcutManager.isRequestPinShortcutSupported()) {
64+
Intent intent = new Intent(context.getApplicationContext(), context.getClass());
65+
intent.setAction(Intent.ACTION_MAIN);
66+
ShortcutInfo pinShortcutInfo = new ShortcutInfo
67+
.Builder(context,"pinned-shortcut")
68+
.setIcon(Icon.createWithResource(context, R.drawable.ic_shortcut))
69+
.setIntent(intent)
70+
.setShortLabel(shortcutTitle)
71+
.build();
72+
Intent pinnedShortcutCallbackIntent = shortcutManager.createShortcutResultIntent(pinShortcutInfo);
73+
//Get notified when a shortcut is pinned successfully//
74+
PendingIntent successCallback = PendingIntent.getBroadcast(context, 0, pinnedShortcutCallbackIntent, 0);
75+
shortcutManager.requestPinShortcut(pinShortcutInfo, successCallback.getIntentSender()
76+
);
77+
}
78+
}
5279
}
5380

5481
/**

0 commit comments

Comments
 (0)