1717
1818package it .feio .android .omninotes .utils ;
1919
20+ import android .app .PendingIntent ;
2021import android .content .Context ;
2122import 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+
2228import it .feio .android .omninotes .MainActivity ;
2329import it .feio .android .omninotes .OmniNotes ;
2430import it .feio .android .omninotes .R ;
2531import it .feio .android .omninotes .helpers .date .DateHelper ;
2632import it .feio .android .omninotes .models .Note ;
27- import it .feio .android .omninotes .utils .date .DateUtils ;
2833
2934
3035public 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