diff --git a/android/src/main/kotlin/com/bhikadia/receive_intent/ReceiveIntentPlugin.kt b/android/src/main/kotlin/com/bhikadia/receive_intent/ReceiveIntentPlugin.kt index 9cd3ca6..eb971d3 100644 --- a/android/src/main/kotlin/com/bhikadia/receive_intent/ReceiveIntentPlugin.kt +++ b/android/src/main/kotlin/com/bhikadia/receive_intent/ReceiveIntentPlugin.kt @@ -34,8 +34,10 @@ class ReceiveIntentPlugin : FlutterPlugin, MethodCallHandler, EventChannel.Strea private var latestIntentMap: Map? = null private var initialIntent = true - private fun intentToMap(intent: Intent, fromPackageName: String?): Map { - return mapOf( + private fun handleIntent(intent: Intent, fromPackageName: String?) { + //Log.e("ReceiveIntentPlugin", "intent: $intent") + //Log.e("ReceiveIntentPlugin", "fromPackageName: $fromPackageName") + val intentMap = mapOf( "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) @@ -65,10 +62,10 @@ class ReceiveIntentPlugin : FlutterPlugin, MethodCallHandler, EventChannel.Strea val json = JSONObject(data) activity?.setResult(resultCode, jsonToIntent(json)) } - if(shouldFinish ?: false){ + 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() diff --git a/example/lib/main.dart b/example/lib/main.dart index c95b63c..6c55a1e 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -13,7 +13,7 @@ class MyApp extends StatefulWidget { } class _MyAppState extends State { - ReceivedIntent _initialIntent; + Intent _initialIntent; @override void initState() { @@ -31,7 +31,7 @@ class _MyAppState extends State { }); } - 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 { mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ _buildFromIntent("INITIAL", _initialIntent), - StreamBuilder( + StreamBuilder( stream: ReceiveIntent.receivedIntentStream, builder: (context, snapshot) => _buildFromIntent("STREAMED", snapshot.data), diff --git a/lib/receive_intent.dart b/lib/receive_intent.dart index c9faffc..5887f75 100644 --- a/lib/receive_intent.dart +++ b/lib/receive_intent.dart @@ -3,7 +3,7 @@ import 'dart:convert'; import 'package:flutter/services.dart'; -class ReceivedIntent { +class Intent { final bool isNull; final String? fromPackageName; final List? 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 getInitialIntent() async { + static Future getInitialIntent() async { final renameMap = await _methodChannel.invokeMapMethod('getInitialIntent'); //print("result: $renameMap"); - return ReceivedIntent.fromMap(renameMap); + return Intent.fromMap(renameMap); } - static Stream receivedIntentStream = _eventChannel + static Stream receivedIntentStream = _eventChannel .receiveBroadcastStream() - .map((event) => ReceivedIntent.fromMap(event as Map?)); + .map((event) => Intent.fromMap(event as Map?)); - static Future giveResult(int resultCode, + static Future setResult(int resultCode, {Map? data, bool shouldFinish: false}) async { - await _methodChannel.invokeMethod('giveResult', { + await _methodChannel.invokeMethod('setResult', { "resultCode": resultCode, if (data != null) "data": json.encode(data), "shouldFinish": shouldFinish,