8
8
import android .app .Activity ;
9
9
import android .content .Context ;
10
10
import android .content .Intent ;
11
- import android .content .pm .ShortcutInfo ;
12
- import android .content .pm .ShortcutManager ;
13
11
import android .content .res .Resources ;
14
- import android .graphics .drawable .Icon ;
15
12
import android .os .Build ;
16
13
import android .os .Handler ;
17
14
import android .os .Looper ;
18
15
import androidx .annotation .ChecksSdkIntAtLeast ;
19
16
import androidx .annotation .NonNull ;
20
17
import androidx .annotation .Nullable ;
18
+ import androidx .core .content .pm .ShortcutInfoCompat ;
19
+ import androidx .core .content .pm .ShortcutManagerCompat ;
20
+ import androidx .core .graphics .drawable .IconCompat ;
21
21
import io .flutter .plugins .quickactions .Messages .AndroidQuickActionsApi ;
22
22
import io .flutter .plugins .quickactions .Messages .FlutterError ;
23
23
import io .flutter .plugins .quickactions .Messages .Result ;
@@ -61,9 +61,7 @@ public void setShortcutItems(
61
61
result .success (null );
62
62
return ;
63
63
}
64
- ShortcutManager shortcutManager =
65
- (ShortcutManager ) context .getSystemService (Context .SHORTCUT_SERVICE );
66
- List <ShortcutInfo > shortcuts = shortcutItemMessageToShortcutInfo (itemsList );
64
+ List <ShortcutInfoCompat > shortcuts = shortcutItemMessageToShortcutInfo (itemsList );
67
65
Executor uiThreadExecutor = new UiThreadExecutor ();
68
66
ThreadPoolExecutor executor =
69
67
new ThreadPoolExecutor (0 , 1 , 1 , TimeUnit .SECONDS , new LinkedBlockingQueue <>());
@@ -72,7 +70,7 @@ public void setShortcutItems(
72
70
() -> {
73
71
boolean dynamicShortcutsSet = false ;
74
72
try {
75
- shortcutManager .setDynamicShortcuts (shortcuts );
73
+ ShortcutManagerCompat .setDynamicShortcuts (context , shortcuts );
76
74
dynamicShortcutsSet = true ;
77
75
} catch (Exception e ) {
78
76
// Leave dynamicShortcutsSet as false
@@ -101,18 +99,14 @@ public void clearShortcutItems() {
101
99
if (!isVersionAllowed ()) {
102
100
return ;
103
101
}
104
- ShortcutManager shortcutManager =
105
- (ShortcutManager ) context .getSystemService (Context .SHORTCUT_SERVICE );
106
- shortcutManager .removeAllDynamicShortcuts ();
102
+ ShortcutManagerCompat .removeAllDynamicShortcuts (context );
107
103
}
108
104
109
105
@ Override
110
106
public @ Nullable String getLaunchAction () {
111
107
if (!isVersionAllowed ()) {
112
108
return null ;
113
109
}
114
- ShortcutManager shortcutManager =
115
- (ShortcutManager ) context .getSystemService (Context .SHORTCUT_SERVICE );
116
110
if (activity == null ) {
117
111
throw new FlutterError (
118
112
"quick_action_getlaunchaction_no_activity" ,
@@ -122,31 +116,32 @@ public void clearShortcutItems() {
122
116
final Intent intent = activity .getIntent ();
123
117
final String launchAction = intent .getStringExtra (EXTRA_ACTION );
124
118
if (launchAction != null && !launchAction .isEmpty ()) {
125
- shortcutManager .reportShortcutUsed (launchAction );
119
+ ShortcutManagerCompat .reportShortcutUsed (context , launchAction );
126
120
intent .removeExtra (EXTRA_ACTION );
127
121
}
128
122
return launchAction ;
129
123
}
130
124
131
125
@ TargetApi (Build .VERSION_CODES .N_MR1 )
132
- private List <ShortcutInfo > shortcutItemMessageToShortcutInfo (
126
+ private List <ShortcutInfoCompat > shortcutItemMessageToShortcutInfo (
133
127
@ NonNull List <ShortcutItemMessage > shortcuts ) {
134
- final List <ShortcutInfo > shortcutInfos = new ArrayList <>();
128
+ final List <ShortcutInfoCompat > shortcutInfos = new ArrayList <>();
135
129
136
130
for (ShortcutItemMessage shortcut : shortcuts ) {
137
131
final String icon = shortcut .getIcon ();
138
132
final String type = shortcut .getType ();
139
133
final String title = shortcut .getLocalizedTitle ();
140
- final ShortcutInfo .Builder shortcutBuilder = new ShortcutInfo .Builder (context , type );
134
+ final ShortcutInfoCompat .Builder shortcutBuilder =
135
+ new ShortcutInfoCompat .Builder (context , type );
141
136
142
137
final int resourceId = loadResourceId (context , icon );
143
138
final Intent intent = getIntentToOpenMainActivity (type );
144
139
145
140
if (resourceId > 0 ) {
146
- shortcutBuilder .setIcon (Icon .createWithResource (context , resourceId ));
141
+ shortcutBuilder .setIcon (IconCompat .createWithResource (context , resourceId ));
147
142
}
148
143
149
- final ShortcutInfo shortcutInfo =
144
+ final ShortcutInfoCompat shortcutInfo =
150
145
shortcutBuilder .setLongLabel (title ).setShortLabel (title ).setIntent (intent ).build ();
151
146
shortcutInfos .add (shortcutInfo );
152
147
}
0 commit comments