Skip to content

Commit 9fe0b16

Browse files
committed
get icon properties
1 parent 50ab94d commit 9fe0b16

File tree

6 files changed

+332
-182
lines changed

6 files changed

+332
-182
lines changed

android/src/main/java/com/divyanshushekhar/flutter_shortcuts/FlutterShortcutsPlugin.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313

1414
public class FlutterShortcutsPlugin implements FlutterPlugin, ActivityAware {
1515
private static final String CHANNEL_ID = "com.divyanshushekhar.flutter_shortcuts";
16+
private static final String TAG = "[Flutter Shortcuts]";
17+
18+
public static String getTAG() {
19+
return TAG;
20+
}
1621

1722
public static String getChannelId() {
1823
return CHANNEL_ID;

android/src/main/java/com/divyanshushekhar/flutter_shortcuts/MethodCallImplementation.java

Lines changed: 75 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import android.content.res.Resources;
99
import android.graphics.drawable.Icon;
1010
import android.os.Build;
11+
import android.util.Log;
1112
import android.widget.Toast;
1213

1314
import androidx.annotation.NonNull;
@@ -23,6 +24,7 @@
2324

2425
public class MethodCallImplementation implements MethodChannel.MethodCallHandler {
2526
private static final String EXTRA_ACTION = "flutter_shortcuts";
27+
private static final String TAG = FlutterShortcutsPlugin.getTAG();
2628

2729
private final Context context;
2830
private Activity activity;
@@ -50,6 +52,10 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
5052
final int maxLimit = getMaxShortcutLimit();
5153
result.success(maxLimit);
5254
break;
55+
case "getIconProperties":
56+
Map<String,Integer> properties = getIconProperties();
57+
result.success(properties);
58+
break;
5359
case "setShortcutItems":
5460
setShortcutItems(call,shortcutManager);
5561
break;
@@ -59,8 +65,8 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
5965
case "addShortcutItems":
6066
addShortcutItems(call,shortcutManager);
6167
break;
62-
case "updateAllShortcutItems":
63-
updateAllShortcutItems(call,shortcutManager);
68+
case "updateShortcutItems":
69+
updateShortcutItems(call,shortcutManager);
6470
break;
6571
case "updateShortcutItem":
6672
updateShortcutItem(call,shortcutManager);
@@ -89,9 +95,8 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
8995
break;
9096
default:
9197
result.notImplemented();
92-
return;
98+
break;
9399
}
94-
result.success(null);
95100
}
96101

97102
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
@@ -101,34 +106,68 @@ private int getMaxShortcutLimit() {
101106
return shortcutManager.getMaxShortcutCountPerActivity();
102107
}
103108

109+
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
110+
private Map<String, Integer> getIconProperties() {
111+
ShortcutManager shortcutManager =
112+
(ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE);
113+
Map<String, Integer> properties = new HashMap<String, Integer>();
114+
properties.put("maxHeight", shortcutManager.getIconMaxHeight());
115+
properties.put("maxWidth", shortcutManager.getIconMaxWidth());
116+
return properties;
117+
}
118+
104119
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
105120
private void setShortcutItems(MethodCall call,ShortcutManager shortcutManager) {
106121
List<Map<String, String>> setShortcutItemsArgs = call.arguments();
107-
List<ShortcutInfo> shortcuts = processShortcuts(setShortcutItemsArgs);
108-
shortcutManager.setDynamicShortcuts(shortcuts);
122+
List<ShortcutInfo> shortcuts;
123+
try {
124+
shortcuts = processShortcuts(setShortcutItemsArgs);
125+
shortcutManager.setDynamicShortcuts(shortcuts);
126+
} catch (Exception e) {
127+
Log.e(TAG,e.toString());
128+
}
109129
Toast.makeText(context, "Shortcut Created", Toast.LENGTH_SHORT).show();
110130
}
111131

112132
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
113133
private void pushShortcutItem(MethodCall call, ShortcutManager shortcutManager) {
114134
final List<Map<String, String>> args = call.arguments();
115-
List<ShortcutInfo> shortcutInfo = processShortcuts(args);
116-
shortcutManager.addDynamicShortcuts(shortcutInfo);
135+
List<ShortcutInfo> shortcuts;
136+
try {
137+
shortcuts = processShortcuts(args);
138+
shortcutManager.addDynamicShortcuts(shortcuts);
139+
} catch (Exception e) {
140+
Log.e(TAG,e.toString());
141+
}
117142
}
118143

119144
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
120145
private void addShortcutItems(MethodCall call, ShortcutManager shortcutManager) {
121146
final List<Map<String, String>> args = call.arguments();
122-
List<ShortcutInfo> shortcutInfo = processShortcuts(args);
123-
shortcutManager.addDynamicShortcuts(shortcutInfo);
147+
List<ShortcutInfo> shortcuts;
148+
try {
149+
shortcuts = processShortcuts(args);
150+
shortcutManager.addDynamicShortcuts(shortcuts);
151+
} catch (Exception e) {
152+
Log.e(TAG,e.toString());
153+
}
124154
}
125155

126156
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
127-
private void updateAllShortcutItems(MethodCall call, ShortcutManager shortcutManager) {
157+
private void updateShortcutItems(MethodCall call, ShortcutManager shortcutManager) {
128158
List<Map<String, String>> updateAllShortcutArgs = call.arguments();
129-
List<ShortcutInfo> updateShortcuts = processShortcuts(updateAllShortcutArgs);
130-
boolean updated = shortcutManager.updateShortcuts(updateShortcuts);
131-
Toast.makeText(context, "Shortcut Updated: " + updated, Toast.LENGTH_SHORT).show();
159+
boolean updated = false;
160+
try {
161+
List<ShortcutInfo> updateShortcuts = processShortcuts(updateAllShortcutArgs);
162+
updated = shortcutManager.updateShortcuts(updateShortcuts);
163+
} catch(Exception e) {
164+
Log.e(TAG, e.toString());
165+
}
166+
if(updated) {
167+
Log.d(TAG,"All Shortcuts updated");
168+
} else {
169+
Log.d(TAG,"Unable to update all shortcuts");
170+
}
132171
}
133172

134173
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
@@ -138,15 +177,25 @@ private void updateShortcutItem(MethodCall call, ShortcutManager shortcutManager
138177
final String refId = info.get("id");
139178
List<ShortcutInfo> dynamicShortcuts = shortcutManager.getDynamicShortcuts();
140179
final List<ShortcutInfo> shortcutList = new ArrayList<>();
180+
int flag = 1;
141181
for(ShortcutInfo si : dynamicShortcuts) {
142182
if(si.getId().equalsIgnoreCase(refId)) {
143183
ShortcutInfo shortcutInfo = createShortcutInfo(info);
144184
shortcutList.add(shortcutInfo);
185+
flag = 0;
145186
continue;
146187
}
147188
shortcutList.add(si);
148189
}
149-
shortcutManager.updateShortcuts(shortcutList);
190+
if (flag == 1) {
191+
Log.e(TAG, "ID did not match any shortcut");
192+
return;
193+
}
194+
try {
195+
shortcutManager.updateShortcuts(shortcutList);
196+
} catch(Exception e) {
197+
Log.e(TAG,e.toString());
198+
}
150199
}
151200

152201
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
@@ -158,14 +207,24 @@ private void changeShortcutItemIcon(MethodCall call, ShortcutManager shortcutMan
158207
ShortcutInfo shortcutInfo = createShortcutInfo(items);
159208
List<ShortcutInfo> dynamicShortcuts = shortcutManager.getDynamicShortcuts();
160209
final List<ShortcutInfo> shortcutList = new ArrayList<>();
210+
int flag = 1;
161211
for(ShortcutInfo si : dynamicShortcuts) {
162212
if(si.getId().equalsIgnoreCase(refId)) {
163213
shortcutList.add(shortcutInfo);
214+
flag = 0;
164215
continue;
165216
}
166217
shortcutList.add(si);
167218
}
168-
shortcutManager.updateShortcuts(shortcutList);
219+
if (flag == 1) {
220+
Log.e(TAG, "ID did not match any shortcut");
221+
return;
222+
}
223+
try {
224+
shortcutManager.updateShortcuts(shortcutList);
225+
} catch(Exception e) {
226+
Log.e(TAG,e.toString());
227+
}
169228
}
170229

171230
@RequiresApi(api = Build.VERSION_CODES.N_MR1)

0 commit comments

Comments
 (0)