- [Intent] renamed ReceivedIntent
-> Intent
- [ReceiveIntent] renamed `giveResult` -> `setResult` - [ReceiveIntentPlugin] removed `intentToMap`, in favour of creating it inside `handleIntent`
This commit is contained in:
parent
07f81ea602
commit
a5541aa248
3 changed files with 24 additions and 27 deletions
|
@ -34,8 +34,10 @@ class ReceiveIntentPlugin : FlutterPlugin, MethodCallHandler, EventChannel.Strea
|
|||
private var latestIntentMap: Map<String, Any?>? = null
|
||||
private var initialIntent = true
|
||||
|
||||
private fun intentToMap(intent: Intent, fromPackageName: String?): Map<String, Any?> {
|
||||
return mapOf(
|
||||
private fun handleIntent(intent: Intent, fromPackageName: String?) {
|
||||
//Log.e("ReceiveIntentPlugin", "intent: $intent")
|
||||
//Log.e("ReceiveIntentPlugin", "fromPackageName: $fromPackageName")
|
||||
val intentMap = mapOf<String, Any?>(
|
||||
"fromPackageName" to fromPackageName,
|
||||
"fromSignatures" to fromPackageName?.let { getApplicationSignature(context, it) },
|
||||
"action" to intent.action,
|
||||
|
@ -43,21 +45,16 @@ class ReceiveIntentPlugin : FlutterPlugin, MethodCallHandler, EventChannel.Strea
|
|||
"categories" to intent.categories,
|
||||
"extra" to intent.extras?.let { bundleToJSON(it).toString() }
|
||||
)
|
||||
}
|
||||
|
||||
private fun handleIntent(intent: Intent, fromPackageName: String?) {
|
||||
//Log.e("ReceiveIntentPlugin", "intent: $intent")
|
||||
//Log.e("ReceiveIntentPlugin", "fromPackageName: $fromPackageName")
|
||||
//Log.e("ReceiveIntentPlugin", "intentMap: " + intentToMap(intent, fromPackageName))
|
||||
//Log.e("ReceiveIntentPlugin", "intentMap: $intentMap")
|
||||
if (initialIntent) {
|
||||
initialIntentMap = intentToMap(intent, fromPackageName)
|
||||
initialIntentMap = intentMap
|
||||
initialIntent = false
|
||||
}
|
||||
latestIntentMap = intentToMap(intent, fromPackageName)
|
||||
latestIntentMap = intentMap
|
||||
eventSink?.success(latestIntentMap)
|
||||
}
|
||||
|
||||
private fun giveResult(result: Result, resultCode: Int?, data: String?, shouldFinish: Boolean?) {
|
||||
private fun setResult(result: Result, resultCode: Int?, data: String?, shouldFinish: Boolean?) {
|
||||
if (resultCode != null) {
|
||||
if (data == null) {
|
||||
activity?.setResult(resultCode)
|
||||
|
@ -68,7 +65,7 @@ class ReceiveIntentPlugin : FlutterPlugin, MethodCallHandler, EventChannel.Strea
|
|||
if (shouldFinish ?: false) {
|
||||
activity?.finish()
|
||||
}
|
||||
result.success(null)
|
||||
return result.success(null)
|
||||
}
|
||||
result.error("InvalidArg", "resultCode can not be null", null)
|
||||
}
|
||||
|
@ -89,8 +86,8 @@ class ReceiveIntentPlugin : FlutterPlugin, MethodCallHandler, EventChannel.Strea
|
|||
"getInitialIntent" -> {
|
||||
result.success(initialIntentMap)
|
||||
}
|
||||
"giveResult" -> {
|
||||
giveResult(result, call.argument("resultCode"), call.argument("data"), call.argument("shouldFinish"))
|
||||
"setResult" -> {
|
||||
setResult(result, call.argument("resultCode"), call.argument("data"), call.argument("shouldFinish"))
|
||||
}
|
||||
else -> {
|
||||
result.notImplemented()
|
||||
|
|
|
@ -13,7 +13,7 @@ class MyApp extends StatefulWidget {
|
|||
}
|
||||
|
||||
class _MyAppState extends State<MyApp> {
|
||||
ReceivedIntent _initialIntent;
|
||||
Intent _initialIntent;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
|
@ -31,7 +31,7 @@ class _MyAppState extends State<MyApp> {
|
|||
});
|
||||
}
|
||||
|
||||
Widget _buildFromIntent(String label, ReceivedIntent intent) {
|
||||
Widget _buildFromIntent(String label, Intent intent) {
|
||||
return Center(
|
||||
child: Column(
|
||||
children: [
|
||||
|
@ -59,7 +59,7 @@ class _MyAppState extends State<MyApp> {
|
|||
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||
children: [
|
||||
_buildFromIntent("INITIAL", _initialIntent),
|
||||
StreamBuilder<ReceivedIntent>(
|
||||
StreamBuilder<Intent>(
|
||||
stream: ReceiveIntent.receivedIntentStream,
|
||||
builder: (context, snapshot) =>
|
||||
_buildFromIntent("STREAMED", snapshot.data),
|
||||
|
|
|
@ -3,7 +3,7 @@ import 'dart:convert';
|
|||
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
class ReceivedIntent {
|
||||
class Intent {
|
||||
final bool isNull;
|
||||
final String? fromPackageName;
|
||||
final List<String>? fromSignatures;
|
||||
|
@ -14,7 +14,7 @@ class ReceivedIntent {
|
|||
|
||||
bool get isNotNull => !isNull;
|
||||
|
||||
const ReceivedIntent({
|
||||
const Intent({
|
||||
this.isNull = true,
|
||||
this.fromPackageName,
|
||||
this.fromSignatures,
|
||||
|
@ -24,7 +24,7 @@ class ReceivedIntent {
|
|||
this.extra,
|
||||
});
|
||||
|
||||
factory ReceivedIntent.fromMap(Map? map) => ReceivedIntent(
|
||||
factory Intent.fromMap(Map? map) => Intent(
|
||||
isNull: map == null,
|
||||
fromPackageName: map?["fromPackageName"],
|
||||
fromSignatures: map?["fromSignatures"] != null
|
||||
|
@ -59,19 +59,19 @@ class ReceiveIntent {
|
|||
static const EventChannel _eventChannel =
|
||||
const EventChannel("receive_intent/event");
|
||||
|
||||
static Future<ReceivedIntent?> getInitialIntent() async {
|
||||
static Future<Intent?> getInitialIntent() async {
|
||||
final renameMap = await _methodChannel.invokeMapMethod('getInitialIntent');
|
||||
//print("result: $renameMap");
|
||||
return ReceivedIntent.fromMap(renameMap);
|
||||
return Intent.fromMap(renameMap);
|
||||
}
|
||||
|
||||
static Stream<ReceivedIntent?> receivedIntentStream = _eventChannel
|
||||
static Stream<Intent?> receivedIntentStream = _eventChannel
|
||||
.receiveBroadcastStream()
|
||||
.map<ReceivedIntent?>((event) => ReceivedIntent.fromMap(event as Map?));
|
||||
.map<Intent?>((event) => Intent.fromMap(event as Map?));
|
||||
|
||||
static Future<void> giveResult(int resultCode,
|
||||
static Future<void> setResult(int resultCode,
|
||||
{Map<String, dynamic?>? data, bool shouldFinish: false}) async {
|
||||
await _methodChannel.invokeMethod('giveResult', <String, dynamic>{
|
||||
await _methodChannel.invokeMethod('setResult', <String, dynamic>{
|
||||
"resultCode": resultCode,
|
||||
if (data != null) "data": json.encode(data),
|
||||
"shouldFinish": shouldFinish,
|
||||
|
|
Loading…
Reference in a new issue