feat: added support for ByteArrays (#19)

This commit is contained in:
Geert-Johan Riemer 2023-05-20 18:35:15 +02:00 committed by GitHub
parent 5950356e8d
commit 6f3f0ef295
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 8 deletions

View file

@ -14,6 +14,7 @@ import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result import io.flutter.plugin.common.MethodChannel.Result
import org.json.JSONObject import org.json.JSONObject
// import android.util.Log
/** ReceiveIntentPlugin */ /** ReceiveIntentPlugin */
@ -36,6 +37,8 @@ class ReceiveIntentPlugin : FlutterPlugin, MethodCallHandler, EventChannel.Strea
private fun handleIntent(intent: Intent, fromPackageName: String?) { private fun handleIntent(intent: Intent, fromPackageName: String?) {
// Log.e("ReceiveIntentPlugin", "intent: $intent") // Log.e("ReceiveIntentPlugin", "intent: $intent")
// val decodeData = intent.extras?.get("com.symbol.datawedge.decode_data")
// Log.e("ReceiveIntentPlugin", "decodeData: $decodeData")
// Log.e("ReceiveIntentPlugin", "fromPackageName: $fromPackageName") // Log.e("ReceiveIntentPlugin", "fromPackageName: $fromPackageName")
val intentMap = mapOf<String, Any?>( val intentMap = mapOf<String, Any?>(
"fromPackageName" to fromPackageName, "fromPackageName" to fromPackageName,
@ -111,6 +114,7 @@ class ReceiveIntentPlugin : FlutterPlugin, MethodCallHandler, EventChannel.Strea
override fun onAttachedToActivity(binding: ActivityPluginBinding) { override fun onAttachedToActivity(binding: ActivityPluginBinding) {
activity = binding.activity activity = binding.activity
binding.addOnNewIntentListener(fun(intent: Intent?): Boolean { binding.addOnNewIntentListener(fun(intent: Intent?): Boolean {
// Log.e("addOnNewIntentListener", "intent: $intent")
intent?.let { handleIntent(it, binding.activity.callingActivity?.packageName) } intent?.let { handleIntent(it, binding.activity.callingActivity?.packageName) }
return false; return false;
}) })

View file

@ -7,7 +7,7 @@ import android.net.Uri
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.Parcelable import android.os.Parcelable
import android.util.Log // import android.util.Log
import org.json.JSONArray import org.json.JSONArray
import org.json.JSONException import org.json.JSONException
import org.json.JSONObject import org.json.JSONObject
@ -52,6 +52,7 @@ fun bundleToJSON(bundle: Bundle): JSONObject {
while (iterator.hasNext()) { while (iterator.hasNext()) {
val key = iterator.next() val key = iterator.next()
try { try {
// Log.e("ReceiveIntentPlugin wrapping key", "$key")
json.put(key, wrap(bundle.get(key))) json.put(key, wrap(bundle.get(key)))
} catch (e: JSONException) { } catch (e: JSONException) {
e.printStackTrace() e.printStackTrace()
@ -62,18 +63,22 @@ fun bundleToJSON(bundle: Bundle): JSONObject {
fun wrap(o: Any?): Any? { fun wrap(o: Any?): Any? {
if (o == null) { if (o == null) {
// Log.e("ReceiveIntentPlugin", "$o is null")
return JSONObject.NULL return JSONObject.NULL
} }
if (o is JSONArray || o is JSONObject) { if (o is JSONArray || o is JSONObject) {
// Log.e("ReceiveIntentPlugin", "$o is JSONArray or JSONObject")
return o return o
} }
if (o == JSONObject.NULL) { if (o == JSONObject.NULL) {
// Log.e("ReceiveIntentPlugin", "$o is JSONObject.NULL")
return o return o
} }
try { try {
if (o is Collection<*>) { if (o is Collection<*>) {
// Log.e("ReceiveIntentPlugin", "$o is Collection<*>") // Log.e("ReceiveIntentPlugin", "$o is Collection<*>")
if (o is ArrayList<*>) { if (o is ArrayList<*>) {
// Log.e("ReceiveIntentPlugin", "..And also ArrayList")
return toJSONArray(o) return toJSONArray(o)
} }
return JSONArray(o as Collection<*>?) return JSONArray(o as Collection<*>?)
@ -109,20 +114,40 @@ fun wrap(o: Any?): Any? {
fun toJSONArray(array: Any): JSONArray? { fun toJSONArray(array: Any): JSONArray? {
val result = JSONArray() val result = JSONArray()
if (!array.javaClass.isArray && array !is ArrayList<*>) { if (!array.javaClass.isArray && array !is ArrayList<*>) {
// Log.e("ReceiveIntentPlugin not a primitive array", "")
throw JSONException("Not a primitive array: " + array.javaClass) throw JSONException("Not a primitive array: " + array.javaClass)
} }
when (array) { when (array) {
is List<*> -> { is List<*> -> {
// Log.e("ReceiveIntentPlugin toJSONArray List", "")
// Log.e("ReceiveIntentPlugin toJSONArray List size", "${array.size}")
array.forEach { result.put(wrap(it)) } array.forEach { result.put(wrap(it)) }
} }
is Array<*> -> { is Array<*> -> {
// Log.e("ReceiveIntentPlugin toJSONArray Array", "")
// Log.e("ReceiveIntentPlugin toJSONArray Array size", "${array.size}")
array.forEach { result.put(wrap(it)) } array.forEach { result.put(wrap(it)) }
} }
is ArrayList<*> -> { is ArrayList<*> -> {
// Log.e("ReceiveIntentPlugin toJSONArray ArrayList", "")
array.forEach { result.put(wrap(it)) } array.forEach { result.put(wrap(it)) }
} }
is ByteArray -> {
// Log.e("ReceiveIntentPlugin toJSONArray ByteArray", "")
array.forEach { result.put(wrap(it)) }
} }
else -> {
// val typename = array.javaClass.kotlin.simpleName
// Log.e("ReceiveIntentPlugin toJSONArray else", "$typename")
val length = java.lang.reflect.Array.getLength(array)
for (i in 0 until length) {
result.put(wrap(java.lang.reflect.Array.get(array, i)))
}
}
}
// Log.e("ReceiveIntentPlugin toJSONArray result", "$result")
return result return result
} }