This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[android_intent] Android Code Inspection and Clean up #3043
Merged
Merged
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c4d173c
androidintent
hamdikahloun b00ee23
androidintent
hamdikahloun fa06517
androidintent
hamdikahloun b715cea
androidintent
hamdikahloun 1448708
android_intent
hamdikahloun accd4e6
android_intent
hamdikahloun a8b34e6
android_intent
hamdikahloun 4019710
android_intent
hamdikahloun 71702b2
android_intent
hamdikahloun 33d8903
android_intent
hamdikahloun 15d04e1
Update pubspec.yaml
hamdikahloun 54029da
Update CHANGELOG.md
hamdikahloun 04a6254
Merge branch 'master' into warnings
hamdikahloun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| import io.flutter.plugin.common.MethodChannel.MethodCallHandler; | ||
| import io.flutter.plugin.common.MethodChannel.Result; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| /** Forwards incoming {@link MethodCall}s to {@link IntentSender#send}. */ | ||
|
|
@@ -75,19 +76,24 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { | |
| String action = convertAction((String) call.argument("action")); | ||
| Integer flags = call.argument("flags"); | ||
| String category = call.argument("category"); | ||
| Uri data = call.argument("data") != null ? Uri.parse((String) call.argument("data")) : null; | ||
| Bundle arguments = convertArguments((Map<String, ?>) call.argument("arguments")); | ||
| String stringData = call.argument("data"); | ||
| Uri data = call.argument("data") != null ? Uri.parse(stringData) : null; | ||
| Map<String, ?> stringMap = call.argument("arguments"); | ||
| Bundle arguments = convertArguments(stringMap); | ||
| String packageName = call.argument("package"); | ||
| String component = call.argument("componentName"); | ||
| ComponentName componentName = | ||
| (!TextUtils.isEmpty(packageName) | ||
| && !TextUtils.isEmpty((String) call.argument("componentName"))) | ||
| ? new ComponentName(packageName, (String) call.argument("componentName")) | ||
| : null; | ||
| (packageName != null | ||
| && component != null | ||
| && !TextUtils.isEmpty(packageName) | ||
| && !TextUtils.isEmpty(component)) | ||
| ? new ComponentName(packageName, component) | ||
| : null; | ||
| String type = call.argument("type"); | ||
|
|
||
| Intent intent = | ||
| sender.buildIntent( | ||
| action, flags, category, data, arguments, packageName, componentName, type); | ||
| sender.buildIntent( | ||
| action, flags, category, data, arguments, packageName, componentName, type); | ||
|
|
||
| if ("launch".equalsIgnoreCase(call.method)) { | ||
| sender.send(intent); | ||
|
|
@@ -146,42 +152,67 @@ private static Bundle convertArguments(Map<String, ?> arguments) { | |
| bundle.putLongArray(key, (long[]) value); | ||
| } else if (value instanceof double[]) { | ||
| bundle.putDoubleArray(key, (double[]) value); | ||
| } else if (isTypedArrayList(value, Integer.class)) { | ||
| bundle.putIntegerArrayList(key, (ArrayList<Integer>) value); | ||
| } else if (isTypedArrayList(value, String.class)) { | ||
| bundle.putStringArrayList(key, (ArrayList<String>) value); | ||
| } else if (isStringKeyedMap(value)) { | ||
| bundle.putBundle(key, convertArguments((Map<String, ?>) value)); | ||
| } else if (integers(value) != null) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I think I prefer the old name of these - I'm a bit surprised to see a function named I also wonder if we can just somehow avoid traversing the array twice and creating the object twice. |
||
| bundle.putIntegerArrayList(key, integers(value)); | ||
| } else if (strings(value) != null) { | ||
| bundle.putStringArrayList(key, strings(value)); | ||
| } else if (map(value) != null) { | ||
| bundle.putBundle(key, convertArguments((map(value)))); | ||
| } else { | ||
| throw new UnsupportedOperationException("Unsupported type " + value); | ||
| } | ||
| } | ||
| return bundle; | ||
| } | ||
|
|
||
| private static boolean isTypedArrayList(Object value, Class<?> type) { | ||
| private static ArrayList<Integer> integers(Object value) { | ||
| ArrayList<Integer> integerArrayList = new ArrayList<>(); | ||
| if (!(value instanceof ArrayList)) { | ||
| return false; | ||
| return null; | ||
| } | ||
| ArrayList<?> intList = (ArrayList<?>) value; | ||
| for (Object o : intList) { | ||
| if (!(o instanceof Integer)) { | ||
| return null; | ||
| } else { | ||
| integerArrayList.add((Integer) o); | ||
| } | ||
| } | ||
| return integerArrayList; | ||
| } | ||
|
|
||
| private static ArrayList<String> strings(Object value) { | ||
| ArrayList<String> stringArrayList = new ArrayList<>(); | ||
| if (!(value instanceof ArrayList)) { | ||
| return null; | ||
| } | ||
| ArrayList list = (ArrayList) value; | ||
| for (Object o : list) { | ||
| if (!(o == null || type.isInstance(o))) { | ||
| return false; | ||
| ArrayList<?> stringList = (ArrayList<?>) value; | ||
| for (Object o : stringList) { | ||
| if (!(o instanceof String)) { | ||
| return null; | ||
| } else { | ||
| stringArrayList.add((String) o); | ||
| } | ||
| } | ||
| return true; | ||
| return stringArrayList; | ||
| } | ||
|
|
||
| private static boolean isStringKeyedMap(Object value) { | ||
| private static Map<String, ?> map(Object value) { | ||
| Map<String, Object> stringMap = new HashMap<>(); | ||
| if (!(value instanceof Map)) { | ||
| return false; | ||
| return null; | ||
| } | ||
| Map map = (Map) value; | ||
| for (Object key : map.keySet()) { | ||
| if (!(key == null || key instanceof String)) { | ||
| return false; | ||
| Map<?, ?> mapValue = (Map<?, ?>) value; | ||
| for (Object key : mapValue.keySet()) { | ||
| if (!(key instanceof String)) { | ||
| return null; | ||
| } else { | ||
| Object o = mapValue.get(key); | ||
| if (o != null) { | ||
| stringMap.put((String) key, o); | ||
| } | ||
| } | ||
| } | ||
| return true; | ||
| return stringMap; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I would find this easier to read as a regular declaration followed by an if statement. This seems like a lot of logic for a ternary.