Skip to content

Commit f178550

Browse files
committed
reflex event type declared
1 parent 553edf5 commit f178550

File tree

11 files changed

+151
-44
lines changed

11 files changed

+151
-44
lines changed

android/src/main/java/com/devsonflutter/reflex/EventCallHandler.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
public class EventCallHandler implements EventChannel.StreamHandler {
3333

34-
private EventChannel.EventSink mEventSink = null;
34+
private static EventChannel.EventSink mEventSink = null;
3535
private final Context context;
3636
private final NotificationPermission notificationPermission;
3737

@@ -42,6 +42,10 @@ public class EventCallHandler implements EventChannel.StreamHandler {
4242

4343
private static final String TAG = ReflexPlugin.getPluginTag();
4444

45+
static public EventChannel.EventSink getEventSink() {
46+
return mEventSink;
47+
}
48+
4549
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
4650
@Override
4751
public void onListen(Object arguments, EventChannel.EventSink events) {

android/src/main/java/com/devsonflutter/reflex/notification/NotificationListener.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@
1919
import android.service.notification.StatusBarNotification;
2020
import androidx.annotation.RequiresApi;
2121

22+
import com.devsonflutter.reflex.EventCallHandler;
2223
import com.devsonflutter.reflex.ReflexPlugin;
2324
import com.devsonflutter.reflex.notification.autoReply.AutoReply;
2425

26+
import java.util.HashMap;
2527
import java.util.List;
2628
import java.util.Map;
2729

2830
import io.flutter.Log;
31+
import io.flutter.plugin.common.EventChannel;
2932

3033
/* Notification Listener */
3134
@RequiresApi(api = VERSION_CODES.JELLY_BEAN_MR2)
@@ -68,9 +71,11 @@ public void onNotificationPosted(StatusBarNotification notification) {
6871
Map<String, Object> autoReply = ReflexPlugin.autoReply;
6972
if(autoReply != null) {
7073
List<String> autoReplyPackageNameList = (List<String>) autoReply.get("packageNameList");
74+
String message = (String) autoReply.get("message");
7175
assert autoReplyPackageNameList != null;
7276
if(autoReplyPackageNameList.contains(packageName)) {
73-
new AutoReply(ReflexPlugin.context).sendReply(notification, (String) autoReply.get("message"));
77+
// Reply to notification
78+
new AutoReply(ReflexPlugin.context).sendReply(notification, packageName, title, message);
7479
}
7580
}
7681
}

android/src/main/java/com/devsonflutter/reflex/notification/NotificationReceiver.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,6 @@ public void onReceive(Context context, Intent intent) {
3838
String title = intent.getStringExtra(NotificationUtils.NOTIFICATION_TITLE);
3939
String message = intent.getStringExtra(NotificationUtils.NOTIFICATION_MESSAGE);
4040

41-
// Sending Data from Java to Flutter
42-
HashMap<String, Object> data = new HashMap<>();
43-
data.put("packageName", packageName);
44-
data.put("title", title);
45-
data.put("message", message);
46-
4741
boolean sendData = false;
4842

4943
List<String> packageNameList = ReflexPlugin.packageNameList;
@@ -62,6 +56,13 @@ else if(packageNameList != null) {
6256
}
6357

6458
if(sendData) {
59+
// Sending Data from Java to Flutter
60+
HashMap<String, Object> data = new HashMap<>();
61+
data.put("type", "notification");
62+
data.put("packageName", packageName);
63+
data.put("title", title);
64+
data.put("message", message);
65+
6566
eventSink.success(data);
6667
}
6768
}

android/src/main/java/com/devsonflutter/reflex/notification/autoReply/AutoReply.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@
1414
import androidx.annotation.RequiresApi;
1515
import androidx.core.app.RemoteInput;
1616

17+
import com.devsonflutter.reflex.EventCallHandler;
1718
import com.devsonflutter.reflex.ReflexPlugin;
1819
import com.devsonflutter.reflex.notification.NotificationUtils;
1920
import com.devsonflutter.reflex.notification.model.NotificationWear;
2021

22+
import java.util.HashMap;
23+
24+
import io.flutter.plugin.common.EventChannel;
25+
2126
public class AutoReply {
2227

2328
public AutoReply(Context context) {
@@ -29,15 +34,16 @@ public AutoReply(Context context) {
2934
private static final String TAG = ReflexPlugin.getPluginTag();
3035

3136
@RequiresApi(api = Build.VERSION_CODES.N)
32-
public void sendReply(StatusBarNotification sbn, String message){
37+
public void sendReply(StatusBarNotification sbn, String packageName, CharSequence title,String message){
3338
if (canReply(sbn)) {
34-
reply(sbn,message);
39+
reply(sbn,packageName, title, message);
3540
}
3641
}
3742

3843
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
39-
private void reply(StatusBarNotification sbn, String message) {
44+
private void reply(StatusBarNotification sbn, String packageName, CharSequence title,String message) {
4045
NotificationWear notificationWear = NotificationUtils.extractWearNotification(sbn);
46+
4147
if (notificationWear.getRemoteInputs().isEmpty()) {
4248
return;
4349
}
@@ -61,6 +67,16 @@ private void reply(StatusBarNotification sbn, String message) {
6167
if (notificationWear.getPendingIntent() != null) {
6268
notificationWear.getPendingIntent().send(context, 0, localIntent);
6369
ReflexPlugin.debugPrint("Auto Reply Sent Successfully...");
70+
71+
// Sending Reply Data from Java to Flutter
72+
HashMap<String, Object> data = new HashMap<>();
73+
data.put("type", "reply");
74+
data.put("packageName", packageName);
75+
data.put("title", title);
76+
data.put("message", message);
77+
78+
EventChannel.EventSink eventSink = EventCallHandler.getEventSink();
79+
eventSink.success(data);
6480
}
6581
} catch (PendingIntent.CanceledException e) {
6682
Log.e(TAG, "PendingIntent.CanceledException: " + e.getMessage());

example/lib/main.dart

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ class MyApp extends StatefulWidget {
1717
}
1818

1919
class _MyAppState extends State<MyApp> {
20-
StreamSubscription<NotificationEvent>? _subscription;
21-
final List<NotificationEvent> _notificationLogs = [];
20+
StreamSubscription<ReflexEvent>? _subscription;
21+
final List<ReflexEvent> _notificationLogs = [];
22+
final List<ReflexEvent> _autoReplyLogs = [];
2223
bool isListening = false;
2324

2425
Reflex reflex = Reflex(
@@ -42,9 +43,13 @@ class _MyAppState extends State<MyApp> {
4243
startListening();
4344
}
4445

45-
void onData(NotificationEvent event) {
46+
void onData(ReflexEvent event) {
4647
setState(() {
47-
_notificationLogs.add(event);
48+
if (event.type == ReflexEventType.notification) {
49+
_notificationLogs.add(event);
50+
} else if (event.type == ReflexEventType.reply) {
51+
_autoReplyLogs.add(event);
52+
}
4853
});
4954
debugPrint(event.toString());
5055
}
@@ -72,13 +77,15 @@ class _MyAppState extends State<MyApp> {
7277
appBar: AppBar(
7378
title: const Text('Reflex Example app'),
7479
),
75-
body: Column(
76-
mainAxisAlignment: MainAxisAlignment.start,
77-
children: [
78-
notificationListener(),
79-
permissions(),
80-
autoReply(),
81-
],
80+
body: SingleChildScrollView(
81+
child: Column(
82+
mainAxisAlignment: MainAxisAlignment.start,
83+
children: [
84+
notificationListener(),
85+
permissions(),
86+
autoReply(),
87+
],
88+
),
8289
),
8390
),
8491
);
@@ -115,7 +122,7 @@ class _MyAppState extends State<MyApp> {
115122
child: ListView.builder(
116123
itemCount: _notificationLogs.length,
117124
itemBuilder: (BuildContext context, int index) {
118-
final NotificationEvent element = _notificationLogs[index];
125+
final ReflexEvent element = _notificationLogs[index];
119126
return ListTile(
120127
title: Text(element.title ?? ""),
121128
subtitle: Text(element.message ?? ""),
@@ -168,6 +175,46 @@ class _MyAppState extends State<MyApp> {
168175
}
169176

170177
Widget autoReply() {
171-
return const Text("AutoReply");
178+
return SizedBox(
179+
height: 400,
180+
child: Column(
181+
children: [
182+
SizedBox(
183+
height: 300,
184+
child: ListView.builder(
185+
itemCount: _autoReplyLogs.length,
186+
itemBuilder: (BuildContext context, int index) {
187+
final ReflexEvent element = _autoReplyLogs[index];
188+
return ListTile(
189+
title: Text("AutoReply to: ${element.title}"),
190+
subtitle: Text(element.message ?? ""),
191+
trailing: Text(
192+
element.packageName.toString().split('.').last,
193+
),
194+
);
195+
},
196+
),
197+
),
198+
if (_autoReplyLogs.isNotEmpty)
199+
ElevatedButton.icon(
200+
style: ElevatedButton.styleFrom(
201+
primary: Colors.red,
202+
),
203+
icon: const Icon(Icons.clear),
204+
label: const Text(
205+
"Clear List",
206+
style: TextStyle(
207+
color: Colors.white,
208+
),
209+
),
210+
onPressed: () {
211+
setState(() {
212+
_autoReplyLogs.clear();
213+
});
214+
},
215+
),
216+
],
217+
),
218+
);
172219
}
173220
}

lib/reflex.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ for more details.
1010

1111
import 'dart:async';
1212

13-
import 'package:reflex/src/helper/events/notification_event.dart';
13+
import 'package:reflex/src/helper/events/reflex_event.dart';
1414
import 'package:reflex/src/helper/helper.dart';
1515
import 'package:reflex/src/platform/reflex_platform.dart';
1616

@@ -46,7 +46,7 @@ class Reflex {
4646
}
4747

4848
/// [notificationStream] returns stream of notifications.
49-
Stream<NotificationEvent>? get notificationStream {
49+
Stream<ReflexEvent>? get notificationStream {
5050
return reflexPlatform.notificationStream;
5151
}
5252

lib/src/handler/reflex_handler.dart

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ for more details.
1111
import 'dart:io';
1212

1313
import 'package:flutter/services.dart';
14-
import 'package:reflex/src/helper/events/notification_event.dart';
14+
import 'package:reflex/src/helper/events/reflex_event.dart';
1515
import 'package:reflex/src/helper/exception/reflex_exception.dart';
1616
import 'package:reflex/src/helper/helper.dart';
1717
import 'package:reflex/src/helper/utils/reflex_utils.dart';
@@ -26,7 +26,7 @@ class ReflexHandler extends ReflexPlatform {
2626
MethodChannel get methodChannel => _methodChannel;
2727
EventChannel get eventChannel => _eventChannel;
2828

29-
Stream<NotificationEvent>? _notificationStream;
29+
Stream<ReflexEvent>? _notificationStream;
3030

3131
late final List<dynamic> arguments;
3232

@@ -83,13 +83,12 @@ class ReflexHandler extends ReflexPlatform {
8383
}
8484

8585
@override
86-
Stream<NotificationEvent>? get notificationStream {
86+
Stream<ReflexEvent>? get notificationStream {
8787
if (Platform.isAndroid) {
88-
_notificationStream ??= _eventChannel
89-
.receiveBroadcastStream(arguments)
90-
.map<NotificationEvent>(
91-
(event) => NotificationEvent.getNotificationEventFromMap(event),
92-
);
88+
_notificationStream ??=
89+
_eventChannel.receiveBroadcastStream(arguments).map<ReflexEvent>(
90+
(event) => ReflexEvent.getReflexEventFromMap(event),
91+
);
9392
return _notificationStream;
9493
}
9594
throw ReflexException('notificationStream is only supported on Android!');
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
3+
Copyright (c) 2022 DevsOnFlutter (Devs On Flutter)
4+
All rights reserved.
5+
6+
The plugin is governed by the BSD-3-clause License. Please see the LICENSE file
7+
for more details.
8+
9+
*/
10+
11+
/// [ReflexEventType] determines type of Reflex Event
12+
enum ReflexEventType {
13+
/// [ReflexEventType.notification] receives notification
14+
notification,
15+
16+
/// [ReflexEventType.reply] receives auto reply event
17+
reply,
18+
}

lib/src/helper/events/notification_event.dart renamed to lib/src/helper/events/reflex_event.dart

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,20 @@ for more details.
88
99
*/
1010

11-
class NotificationEvent {
12-
NotificationEvent({
11+
import 'package:reflex/src/helper/helper.dart';
12+
13+
class ReflexEvent {
14+
ReflexEvent({
15+
this.type,
1316
this.packageName,
1417
this.title,
1518
this.message,
1619
this.timeStamp,
1720
});
1821

22+
/// Event type Notification/Reply
23+
ReflexEventType? type;
24+
1925
/// The package name of the app that sent the notification.
2026
String? packageName;
2127

@@ -28,27 +34,37 @@ class NotificationEvent {
2834
/// The time stamp of the notification.
2935
DateTime? timeStamp;
3036

31-
factory NotificationEvent.fromMap(Map<dynamic, dynamic> map) {
37+
factory ReflexEvent.fromMap(Map<dynamic, dynamic> map) {
3238
DateTime time = DateTime.now();
3339
String? name = map['packageName'];
3440
String? message = map['message'];
3541
String? title = map['title'];
3642

37-
return NotificationEvent(
43+
ReflexEventType? type;
44+
45+
if (map['type'] == 'notification') {
46+
type = ReflexEventType.notification;
47+
} else if (map['type'] == 'reply') {
48+
type = ReflexEventType.reply;
49+
}
50+
51+
return ReflexEvent(
52+
type: type,
3853
packageName: name,
3954
title: title,
4055
message: message,
4156
timeStamp: time,
4257
);
4358
}
4459

45-
static NotificationEvent getNotificationEventFromMap(dynamic data) {
46-
return NotificationEvent.fromMap(data);
60+
static ReflexEvent getReflexEventFromMap(dynamic data) {
61+
return ReflexEvent.fromMap(data);
4762
}
4863

4964
@override
5065
String toString() {
5166
return '''$runtimeType
67+
type: $type,
5268
Package Name: $packageName,
5369
Title: $title,
5470
Message: $message,

lib/src/helper/helper.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
3-
Copyright (c) 2022 DevsOnFlutter (Devs On Flutter)
3+
Copyright (c) 2022 DevsOnFlutter (Devs On Flutter)
44
All rights reserved.
55
66
The plugin is governed by the BSD-3-clause License. Please see the LICENSE file
@@ -9,5 +9,6 @@ for more details.
99
*/
1010

1111
export 'exception/reflex_exception.dart';
12-
export 'events/notification_event.dart';
12+
export 'events/reflex_event.dart';
1313
export 'model/auto_reply.dart';
14+
export 'enums/reflex_event_type.dart';

0 commit comments

Comments
 (0)