receive_intent_android34/lib/receive_intent.dart

80 lines
2.4 KiB
Dart
Raw Normal View History

import 'dart:async';
2021-04-23 15:15:30 +02:00
import 'dart:convert';
import 'package:flutter/services.dart';
class ReceivedIntent {
2021-04-23 15:15:30 +02:00
final bool isNull;
final String? fromPackageName;
final List<String>? fromSignatures;
final String action;
final String? data;
final List<String>? categories;
final Map<String, dynamic>? extra;
2021-04-23 15:15:30 +02:00
bool get isNotNull => !isNull;
const ReceivedIntent({
2021-04-23 15:15:30 +02:00
this.isNull = true,
this.fromPackageName,
this.fromSignatures,
required this.action,
this.data,
this.categories,
this.extra,
});
factory ReceivedIntent.fromMap(Map? map) => ReceivedIntent(
2021-04-23 15:15:30 +02:00
isNull: map == null,
fromPackageName: map?["fromPackageName"],
2021-04-23 15:15:30 +02:00
fromSignatures: map?["fromSignatures"] != null
? List.unmodifiable(
(map!["fromSignatures"] as List).map((e) => e.toString()))
: null,
action: map?["action"],
data: map?["data"],
2021-04-23 15:15:30 +02:00
categories: map?["categories"] != null
? List.unmodifiable(
(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,
);
2021-04-23 15:15:30 +02:00
Map<String, dynamic> toMap() => {
"fromPackageName": fromPackageName,
"fromSignatures": fromSignatures,
"action": action,
"data": data,
"categories": categories,
"extra": extra,
};
}
class ReceiveIntent {
static const MethodChannel _methodChannel =
const MethodChannel('receive_intent');
static const EventChannel _eventChannel =
const EventChannel("receive_intent/event");
static Future<ReceivedIntent?> getInitialIntent() async {
final renameMap = await _methodChannel.invokeMapMethod('getInitialIntent');
2021-04-23 15:22:59 +02:00
//print("result: $renameMap");
return ReceivedIntent.fromMap(renameMap);
}
static Stream<ReceivedIntent?> receivedIntentStream = _eventChannel
.receiveBroadcastStream()
.map<ReceivedIntent?>((event) => ReceivedIntent.fromMap(event as Map?));
static Future<void> giveResult(int resultCode, {Map? data, bool shouldFinish: false}) async {
2021-04-23 15:15:30 +02:00
await _methodChannel.invokeMethod('giveResult', <String, dynamic>{
"resultCode": resultCode,
"data": json.encode(data),
"shouldFinish": shouldFinish,
2021-04-23 15:15:30 +02:00
});
}
}