feat: added support for ByteArrays (#19)
This commit is contained in:
parent
5950356e8d
commit
6f3f0ef295
2 changed files with 37 additions and 8 deletions
|
@ -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 */
|
||||||
|
@ -35,8 +36,10 @@ class ReceiveIntentPlugin : FlutterPlugin, MethodCallHandler, EventChannel.Strea
|
||||||
private var initialIntent = true
|
private var initialIntent = true
|
||||||
|
|
||||||
private fun handleIntent(intent: Intent, fromPackageName: String?) {
|
private fun handleIntent(intent: Intent, fromPackageName: String?) {
|
||||||
//Log.e("ReceiveIntentPlugin", "intent: $intent")
|
// Log.e("ReceiveIntentPlugin", "intent: $intent")
|
||||||
//Log.e("ReceiveIntentPlugin", "fromPackageName: $fromPackageName")
|
// val decodeData = intent.extras?.get("com.symbol.datawedge.decode_data")
|
||||||
|
// Log.e("ReceiveIntentPlugin", "decodeData: $decodeData")
|
||||||
|
// Log.e("ReceiveIntentPlugin", "fromPackageName: $fromPackageName")
|
||||||
val intentMap = mapOf<String, Any?>(
|
val intentMap = mapOf<String, Any?>(
|
||||||
"fromPackageName" to fromPackageName,
|
"fromPackageName" to fromPackageName,
|
||||||
"fromSignatures" to fromPackageName?.let { getApplicationSignature(context, it) },
|
"fromSignatures" to fromPackageName?.let { getApplicationSignature(context, it) },
|
||||||
|
@ -45,7 +48,7 @@ class ReceiveIntentPlugin : FlutterPlugin, MethodCallHandler, EventChannel.Strea
|
||||||
"categories" to intent.categories?.toList(),
|
"categories" to intent.categories?.toList(),
|
||||||
"extra" to intent.extras?.let { bundleToJSON(it).toString() }
|
"extra" to intent.extras?.let { bundleToJSON(it).toString() }
|
||||||
)
|
)
|
||||||
//Log.e("ReceiveIntentPlugin", "intentMap: $intentMap")
|
// Log.e("ReceiveIntentPlugin", "intentMap: $intentMap")
|
||||||
if (initialIntent) {
|
if (initialIntent) {
|
||||||
initialIntentMap = intentMap
|
initialIntentMap = intentMap
|
||||||
initialIntent = false
|
initialIntent = false
|
||||||
|
@ -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;
|
||||||
})
|
})
|
||||||
|
|
|
@ -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,27 +63,31 @@ 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<*>?)
|
||||||
} else if (o.javaClass.isArray) {
|
} else if (o.javaClass.isArray) {
|
||||||
//Log.e("ReceiveIntentPlugin", "$o is isArray")
|
// Log.e("ReceiveIntentPlugin", "$o is isArray")
|
||||||
return toJSONArray(o)
|
return toJSONArray(o)
|
||||||
}
|
}
|
||||||
if (o is Map<*, *>) {
|
if (o is Map<*, *>) {
|
||||||
//Log.e("ReceiveIntentPlugin", "$o is Map<*, *>")
|
// Log.e("ReceiveIntentPlugin", "$o is Map<*, *>")
|
||||||
return JSONObject(o as Map<*, *>?)
|
return JSONObject(o as Map<*, *>?)
|
||||||
}
|
}
|
||||||
if (o is Boolean ||
|
if (o is Boolean ||
|
||||||
|
@ -100,7 +105,7 @@ fun wrap(o: Any?): Any? {
|
||||||
return o.toString()
|
return o.toString()
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
//Log.e("ReceiveIntentPlugin", e.message, e)
|
// Log.e("ReceiveIntentPlugin", e.message, e)
|
||||||
}
|
}
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
@ -109,21 +114,41 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue