mapping fixes
This commit is contained in:
parent
ed77c100f0
commit
5e109bed8f
3 changed files with 127 additions and 23 deletions
|
@ -3,6 +3,7 @@ package com.bhikadia.receive_intent
|
||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
import android.util.Log
|
||||||
import androidx.annotation.NonNull
|
import androidx.annotation.NonNull
|
||||||
import io.flutter.embedding.engine.plugins.FlutterPlugin
|
import io.flutter.embedding.engine.plugins.FlutterPlugin
|
||||||
import io.flutter.embedding.engine.plugins.activity.ActivityAware
|
import io.flutter.embedding.engine.plugins.activity.ActivityAware
|
||||||
|
@ -13,6 +14,7 @@ import io.flutter.plugin.common.MethodCall
|
||||||
import io.flutter.plugin.common.MethodChannel
|
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
|
||||||
|
|
||||||
|
|
||||||
/** ReceiveIntentPlugin */
|
/** ReceiveIntentPlugin */
|
||||||
|
@ -40,11 +42,14 @@ class ReceiveIntentPlugin : FlutterPlugin, MethodCallHandler, EventChannel.Strea
|
||||||
"action" to intent.action,
|
"action" to intent.action,
|
||||||
"data" to intent.dataString,
|
"data" to intent.dataString,
|
||||||
"categories" to intent.categories,
|
"categories" to intent.categories,
|
||||||
"extra" to intent.extras?.let { bundleToMap(it) }
|
"extra" to intent.extras?.let { bundleToJSON(it).toString() }
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun handleIntent(intent: Intent, fromPackageName: String?) {
|
private fun handleIntent(intent: Intent, fromPackageName: String?) {
|
||||||
|
Log.e("ReceiveIntentPlugin", "intent: $intent")
|
||||||
|
Log.e("ReceiveIntentPlugin", "fromPackageName: $fromPackageName")
|
||||||
|
Log.e("ReceiveIntentPlugin", "intentMap: " + intentToMap(intent, fromPackageName))
|
||||||
if (initialIntent) {
|
if (initialIntent) {
|
||||||
initialIntentMap = intentToMap(intent, fromPackageName)
|
initialIntentMap = intentToMap(intent, fromPackageName)
|
||||||
initialIntent = false
|
initialIntent = false
|
||||||
|
@ -53,12 +58,15 @@ class ReceiveIntentPlugin : FlutterPlugin, MethodCallHandler, EventChannel.Strea
|
||||||
eventSink?.success(latestIntentMap)
|
eventSink?.success(latestIntentMap)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun giveResult(result: Result, resultCode: Int?, data: Map<String, Any?>?) {
|
private fun giveResult(result: Result, resultCode: Int?, data: String?) {
|
||||||
if (resultCode != null) {
|
if (resultCode != null) {
|
||||||
if (data == null)
|
if (data == null) {
|
||||||
activity?.setResult(resultCode)
|
activity?.setResult(resultCode)
|
||||||
else
|
}
|
||||||
activity?.setResult(resultCode, mapToIntent(data))
|
else {
|
||||||
|
val json = JSONObject(data)
|
||||||
|
activity?.setResult(resultCode, jsonToIntent(json))
|
||||||
|
}
|
||||||
result.success(null)
|
result.success(null)
|
||||||
}
|
}
|
||||||
result.error("InvalidArg", "resultCode can not be null", null)
|
result.error("InvalidArg", "resultCode can not be null", null)
|
||||||
|
|
|
@ -6,13 +6,18 @@ import android.content.pm.PackageManager
|
||||||
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 org.json.JSONArray
|
||||||
|
import org.json.JSONException
|
||||||
|
import org.json.JSONObject
|
||||||
import java.security.MessageDigest
|
import java.security.MessageDigest
|
||||||
|
|
||||||
fun mapToBundle(map: Map<String, Any?>): Bundle {
|
|
||||||
val bundle = Bundle();
|
fun jsonToBundle(json: JSONObject): Bundle {
|
||||||
map.forEach {
|
val bundle = Bundle()
|
||||||
val k = it.key
|
json.keys().forEach {
|
||||||
val v = it.value
|
val k = it
|
||||||
|
val v = json.get(k)
|
||||||
when (v) {
|
when (v) {
|
||||||
is Byte -> bundle.putByte(k, v)
|
is Byte -> bundle.putByte(k, v)
|
||||||
is ByteArray -> bundle.putByteArray(k, v)
|
is ByteArray -> bundle.putByteArray(k, v)
|
||||||
|
@ -30,20 +35,86 @@ fun mapToBundle(map: Map<String, Any?>): Bundle {
|
||||||
return bundle;
|
return bundle;
|
||||||
}
|
}
|
||||||
|
|
||||||
fun mapToIntent(map: Map<String, Any?>): Intent = Intent().apply {
|
fun jsonToIntent(json: JSONObject): Intent = Intent().apply {
|
||||||
putExtras(mapToBundle(map))
|
putExtras(jsonToBundle(json))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fun bundleToMap(extras: Bundle): Map<String, Any?> {
|
fun bundleToJSON(bundle: Bundle): JSONObject {
|
||||||
val map: MutableMap<String, Any?> = HashMap()
|
val json = JSONObject()
|
||||||
val ks = extras.keySet()
|
val ks = bundle.keySet()
|
||||||
val iterator: Iterator<String> = ks.iterator()
|
val iterator: Iterator<String> = ks.iterator()
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
val key = iterator.next()
|
val key = iterator.next()
|
||||||
map[key] = extras.get(key)
|
try {
|
||||||
|
json.put(key, wrap(bundle.get(key)))
|
||||||
|
} catch (e: JSONException) {
|
||||||
|
e.printStackTrace()
|
||||||
}
|
}
|
||||||
return map
|
}
|
||||||
|
return json
|
||||||
|
}
|
||||||
|
|
||||||
|
fun wrap(o: Any?): Any? {
|
||||||
|
if (o == null) {
|
||||||
|
return JSONObject.NULL
|
||||||
|
}
|
||||||
|
if (o is JSONArray || o is JSONObject) {
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
if (o == JSONObject.NULL) {
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (o is Collection<*>) {
|
||||||
|
Log.e("ReceiveIntentPlugin", "$o is Collection<*>")
|
||||||
|
return JSONArray(o as Collection<*>?)
|
||||||
|
} else if (o.javaClass.isArray) {
|
||||||
|
Log.e("ReceiveIntentPlugin", "$o is isArray")
|
||||||
|
return toJSONArray(o)
|
||||||
|
}
|
||||||
|
if (o is Map<*, *>) {
|
||||||
|
Log.e("ReceiveIntentPlugin", "$o is Map<*, *>")
|
||||||
|
return JSONObject(o as Map<*, *>?)
|
||||||
|
}
|
||||||
|
if (o is Boolean ||
|
||||||
|
o is Byte ||
|
||||||
|
o is Char ||
|
||||||
|
o is Double ||
|
||||||
|
o is Float ||
|
||||||
|
o is Int ||
|
||||||
|
o is Long ||
|
||||||
|
o is Short ||
|
||||||
|
o is String) {
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
if (o.javaClass.getPackage().name.startsWith("java.")) {
|
||||||
|
return o.toString()
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.e("ReceiveIntentPlugin", e.message, e)
|
||||||
|
e.printStackTrace()
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
@Throws(JSONException::class)
|
||||||
|
fun toJSONArray(array: Any): JSONArray? {
|
||||||
|
val result = JSONArray()
|
||||||
|
if (!array.javaClass.isArray) {
|
||||||
|
throw JSONException("Not a primitive array: " + array.javaClass)
|
||||||
|
}
|
||||||
|
|
||||||
|
when (array) {
|
||||||
|
is List<*> -> {
|
||||||
|
array.forEach { result.put(wrap(it)) }
|
||||||
|
}
|
||||||
|
is Array<*> -> {
|
||||||
|
array.forEach { result.put(wrap(it)) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getApplicationSignature(context: Context, packageName: String): List<String> {
|
fun getApplicationSignature(context: Context, packageName: String): List<String> {
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
|
|
||||||
class ReceivedIntent {
|
class ReceivedIntent {
|
||||||
|
final bool isNull;
|
||||||
final String? fromPackageName;
|
final String? fromPackageName;
|
||||||
final List<String>? fromSignatures;
|
final List<String>? fromSignatures;
|
||||||
final String action;
|
final String action;
|
||||||
|
@ -10,7 +12,10 @@ class ReceivedIntent {
|
||||||
final List<String>? categories;
|
final List<String>? categories;
|
||||||
final Map<String, dynamic>? extra;
|
final Map<String, dynamic>? extra;
|
||||||
|
|
||||||
|
bool get isNotNull => !isNull;
|
||||||
|
|
||||||
const ReceivedIntent({
|
const ReceivedIntent({
|
||||||
|
this.isNull = true,
|
||||||
this.fromPackageName,
|
this.fromPackageName,
|
||||||
this.fromSignatures,
|
this.fromSignatures,
|
||||||
required this.action,
|
required this.action,
|
||||||
|
@ -20,14 +25,32 @@ class ReceivedIntent {
|
||||||
});
|
});
|
||||||
|
|
||||||
factory ReceivedIntent.fromMap(Map? map) => ReceivedIntent(
|
factory ReceivedIntent.fromMap(Map? map) => ReceivedIntent(
|
||||||
|
isNull: map == null,
|
||||||
fromPackageName: map?["fromPackageName"],
|
fromPackageName: map?["fromPackageName"],
|
||||||
fromSignatures: map?["fromSignatures"],
|
fromSignatures: map?["fromSignatures"] != null
|
||||||
|
? List.unmodifiable(
|
||||||
|
(map!["fromSignatures"] as List).map((e) => e.toString()))
|
||||||
|
: null,
|
||||||
action: map?["action"],
|
action: map?["action"],
|
||||||
data: map?["data"],
|
data: map?["data"],
|
||||||
categories: map?["categories"],
|
categories: map?["categories"] != null
|
||||||
extra: (map?["extra"] as Map?)?.map<String, dynamic>(
|
? List.unmodifiable(
|
||||||
(key, value) => MapEntry(key.toString(), value)),
|
(map!["categories"] as List).map((e) => e.toString()))
|
||||||
|
: null,
|
||||||
|
extra: map?["extra"] != null
|
||||||
|
? (json.decode(map!["extra"]) as Map)
|
||||||
|
.map((key, value) => MapEntry(key.toString(), value))
|
||||||
|
: null,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Map<String, dynamic> toMap() => {
|
||||||
|
"fromPackageName": fromPackageName,
|
||||||
|
"fromSignatures": fromSignatures,
|
||||||
|
"action": action,
|
||||||
|
"data": data,
|
||||||
|
"categories": categories,
|
||||||
|
"extra": extra,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
class ReceiveIntent {
|
class ReceiveIntent {
|
||||||
|
@ -47,7 +70,9 @@ class ReceiveIntent {
|
||||||
.map<ReceivedIntent?>((event) => ReceivedIntent.fromMap(event as Map?));
|
.map<ReceivedIntent?>((event) => ReceivedIntent.fromMap(event as Map?));
|
||||||
|
|
||||||
static Future<void> giveResult(int resultCode, {Map? data}) async {
|
static Future<void> giveResult(int resultCode, {Map? data}) async {
|
||||||
await _methodChannel.invokeMethod('giveResult',
|
await _methodChannel.invokeMethod('giveResult', <String, dynamic>{
|
||||||
<String, dynamic>{"resultCode": resultCode, "data": data});
|
"resultCode": resultCode,
|
||||||
|
"data": json.encode(data),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue