2021-04-23 07:15:26 +02:00
|
|
|
import 'dart:async';
|
2021-04-23 15:15:30 +02:00
|
|
|
import 'dart:convert';
|
2021-04-23 07:15:26 +02:00
|
|
|
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
|
2022-07-21 08:12:34 +02:00
|
|
|
/// Result code indicating that operation succeeded.
|
|
|
|
///
|
|
|
|
/// Same as [`Activity.RESULT_OK`](https://developer.android.com/reference/android/app/Activity#RESULT_OK)
|
|
|
|
/// but for the Dart world.
|
2021-05-09 08:15:35 +02:00
|
|
|
const kActivityResultOk = -1;
|
2022-07-21 08:12:34 +02:00
|
|
|
|
|
|
|
/// Result code indicating that operation canceled.
|
|
|
|
///
|
|
|
|
/// Same as [`Activity.RESULT_CANCELED`](https://developer.android.com/reference/android/app/Activity#RESULT_CANCELED)
|
|
|
|
/// but for the Dart world.
|
2021-05-09 08:15:35 +02:00
|
|
|
const kActivityResultCanceled = 0;
|
|
|
|
|
2021-04-25 05:50:20 +02:00
|
|
|
class Intent {
|
2021-04-23 15:15:30 +02:00
|
|
|
final bool isNull;
|
2021-04-23 10:36:07 +02:00
|
|
|
final String? fromPackageName;
|
|
|
|
final List<String>? fromSignatures;
|
2022-02-04 07:49:36 +01:00
|
|
|
final String? action;
|
2021-04-23 10:36:07 +02:00
|
|
|
final String? data;
|
|
|
|
final List<String>? categories;
|
|
|
|
final Map<String, dynamic>? extra;
|
|
|
|
|
2021-04-23 15:15:30 +02:00
|
|
|
bool get isNotNull => !isNull;
|
|
|
|
|
2021-04-25 05:50:20 +02:00
|
|
|
const Intent({
|
2021-04-23 15:15:30 +02:00
|
|
|
this.isNull = true,
|
2021-04-23 10:36:07 +02:00
|
|
|
this.fromPackageName,
|
|
|
|
this.fromSignatures,
|
2022-02-04 07:49:36 +01:00
|
|
|
this.action,
|
2021-04-23 10:36:07 +02:00
|
|
|
this.data,
|
|
|
|
this.categories,
|
|
|
|
this.extra,
|
|
|
|
});
|
|
|
|
|
2021-04-25 05:50:20 +02:00
|
|
|
factory Intent.fromMap(Map? map) => Intent(
|
2021-04-23 15:15:30 +02:00
|
|
|
isNull: map == null,
|
2021-04-23 10:36:07 +02:00
|
|
|
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,
|
2021-04-23 10:36:07 +02:00
|
|
|
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 10:36:07 +02:00
|
|
|
);
|
2021-04-23 15:15:30 +02:00
|
|
|
|
|
|
|
Map<String, dynamic> toMap() => {
|
|
|
|
"fromPackageName": fromPackageName,
|
|
|
|
"fromSignatures": fromSignatures,
|
|
|
|
"action": action,
|
|
|
|
"data": data,
|
|
|
|
"categories": categories,
|
|
|
|
"extra": extra,
|
|
|
|
};
|
2021-10-23 19:38:48 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
String toString() {
|
|
|
|
if (isNull) return 'Intent(null)';
|
|
|
|
var str = 'Intent${toMap()}';
|
|
|
|
return str.replaceFirst('{', '(').replaceFirst('}', ')', str.length - 1);
|
|
|
|
}
|
2021-04-23 10:36:07 +02:00
|
|
|
}
|
|
|
|
|
2021-04-23 07:15:26 +02:00
|
|
|
class ReceiveIntent {
|
2022-07-20 08:05:17 +02:00
|
|
|
static const MethodChannel _methodChannel = MethodChannel('receive_intent');
|
2021-04-23 10:36:07 +02:00
|
|
|
static const EventChannel _eventChannel =
|
2022-07-20 08:05:17 +02:00
|
|
|
EventChannel("receive_intent/event");
|
2021-04-23 10:36:07 +02:00
|
|
|
|
2021-04-25 05:50:20 +02:00
|
|
|
static Future<Intent?> getInitialIntent() async {
|
2021-04-23 10:36:07 +02:00
|
|
|
final renameMap = await _methodChannel.invokeMapMethod('getInitialIntent');
|
2021-04-23 15:22:59 +02:00
|
|
|
//print("result: $renameMap");
|
2021-04-25 05:50:20 +02:00
|
|
|
return Intent.fromMap(renameMap);
|
2021-04-23 10:36:07 +02:00
|
|
|
}
|
|
|
|
|
2021-04-25 05:50:20 +02:00
|
|
|
static Stream<Intent?> receivedIntentStream = _eventChannel
|
2021-04-23 10:36:07 +02:00
|
|
|
.receiveBroadcastStream()
|
2021-04-25 05:50:20 +02:00
|
|
|
.map<Intent?>((event) => Intent.fromMap(event as Map?));
|
2021-04-23 07:15:26 +02:00
|
|
|
|
2021-04-25 05:50:20 +02:00
|
|
|
static Future<void> setResult(int resultCode,
|
2022-07-20 08:05:17 +02:00
|
|
|
{Map<String, Object?>? data, bool shouldFinish = false}) async {
|
2021-04-25 05:50:20 +02:00
|
|
|
await _methodChannel.invokeMethod('setResult', <String, dynamic>{
|
2021-04-23 15:15:30 +02:00
|
|
|
"resultCode": resultCode,
|
2021-04-24 20:08:30 +02:00
|
|
|
if (data != null) "data": json.encode(data),
|
2021-04-24 17:59:33 +02:00
|
|
|
"shouldFinish": shouldFinish,
|
2021-04-23 15:15:30 +02:00
|
|
|
});
|
2021-04-23 07:15:26 +02:00
|
|
|
}
|
|
|
|
}
|