Skip to content

Commit 553edf5

Browse files
committed
exception added
1 parent 4f2f147 commit 553edf5

File tree

9 files changed

+78
-23
lines changed

9 files changed

+78
-23
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
@@ -20,6 +20,7 @@
2020
import com.devsonflutter.reflex.notification.NotificationListener;
2121
import com.devsonflutter.reflex.notification.NotificationReceiver;
2222
import com.devsonflutter.reflex.notification.NotificationUtils;
23+
import com.devsonflutter.reflex.notification.autoReply.AutoReply;
2324
import com.devsonflutter.reflex.permission.NotificationPermission;
2425

2526
import java.util.List;
@@ -53,11 +54,14 @@ public void onListen(Object arguments, EventChannel.EventSink events) {
5354
boolean debug = Boolean.parseBoolean(String.valueOf(args.get("debug")));
5455
List<String> packageNameList = (List<String>) args.get("packageNameList");
5556
List<String> packageNameExceptionList = (List<String>) args.get("packageNameExceptionList");
56-
Map<String, String> autoReply = (Map<String,String>) args.get("autoReply");
57+
Map<String, Object> autoReply = (Map<String,Object>) args.get("autoReply");
5758

5859
ReflexPlugin.debug = debug;
5960
ReflexPlugin.packageNameList = packageNameList;
6061
ReflexPlugin.packageNameExceptionList = packageNameExceptionList;
62+
if(autoReply != null) {
63+
ReflexPlugin.autoReply = autoReply;
64+
}
6165

6266
// Start listening notification
6367
listenNotification(mEventSink);

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
2727
notificationPermission.requestPermission();
2828
break;
2929
case "sendReply":
30-
Log.d(TAG,"Send Reply Invoked");
3130
break;
3231
default:
3332
result.notImplemented();

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import androidx.annotation.NonNull;
1818

1919
import java.util.List;
20+
import java.util.Map;
2021

2122
import io.flutter.embedding.engine.plugins.FlutterPlugin;
2223
import io.flutter.plugin.common.BinaryMessenger;
@@ -69,6 +70,7 @@ public static String getPluginTag() {
6970
public static boolean debug;
7071
public static List<String> packageNameList;
7172
public static List<String> packageNameExceptionList;
73+
public static Map<String,Object> autoReply = null;
7274
/* ------------- Flutter Variables -------------- */
7375

7476
/* ------------- Utility Functions -------------- */

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import com.devsonflutter.reflex.ReflexPlugin;
2323
import com.devsonflutter.reflex.notification.autoReply.AutoReply;
2424

25+
import java.util.List;
26+
import java.util.Map;
27+
2528
import io.flutter.Log;
2629

2730
/* Notification Listener */
@@ -61,7 +64,15 @@ public void onNotificationPosted(StatusBarNotification notification) {
6164
// Notification Receiver listens to this broadcast
6265
sendBroadcast(intent);
6366

64-
new AutoReply(ReflexPlugin.context).sendReply(notification);
67+
// Sending AutoReply
68+
Map<String, Object> autoReply = ReflexPlugin.autoReply;
69+
if(autoReply != null) {
70+
List<String> autoReplyPackageNameList = (List<String>) autoReply.get("packageNameList");
71+
assert autoReplyPackageNameList != null;
72+
if(autoReplyPackageNameList.contains(packageName)) {
73+
new AutoReply(ReflexPlugin.context).sendReply(notification, (String) autoReply.get("message"));
74+
}
75+
}
6576
}
6677

6778
@Override

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ public AutoReply(Context context) {
2929
private static final String TAG = ReflexPlugin.getPluginTag();
3030

3131
@RequiresApi(api = Build.VERSION_CODES.N)
32-
public void sendReply(StatusBarNotification sbn){
32+
public void sendReply(StatusBarNotification sbn, String message){
3333
if (canReply(sbn)) {
34-
reply(sbn);
34+
reply(sbn,message);
3535
}
3636
}
3737

3838
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
39-
private void reply(StatusBarNotification sbn) {
39+
private void reply(StatusBarNotification sbn, String message) {
4040
NotificationWear notificationWear = NotificationUtils.extractWearNotification(sbn);
4141
if (notificationWear.getRemoteInputs().isEmpty()) {
4242
return;
@@ -51,8 +51,7 @@ private void reply(StatusBarNotification sbn) {
5151
int i = 0;
5252
for (RemoteInput remoteIn : notificationWear.getRemoteInputs()) {
5353
remoteInputs[i] = remoteIn;
54-
// This works. Might need additional parameter to make it for Hangouts? (notification_tag?)
55-
localBundle.putCharSequence(remoteInputs[i].getResultKey(), "[Reflex] Auto Reply");
54+
localBundle.putCharSequence(remoteInputs[i].getResultKey(), message);
5655
i++;
5756
}
5857

example/lib/main.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class _MyAppState extends State<MyApp> {
2626
packageNameList: ["com.whatsapp", "com.tyup"],
2727
packageNameExceptionList: ["com.miui.securitycenter"],
2828
autoReply: AutoReply(
29-
packageName: "com.whatsapp",
30-
message: "Hello",
29+
packageNameList: ["com.whatsapp"],
30+
message: "[Reflex] This is an automated reply.",
3131
),
3232
);
3333

lib/src/handler/reflex_handler.dart

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,39 @@ class ReflexHandler extends ReflexPlatform {
3737
List<String>? packageNameExceptionList,
3838
AutoReply? autoReply,
3939
}) {
40-
// throw exception if package
40+
// Check conflicting package name list and exception list
4141
if (packageNameList != null && packageNameExceptionList != null) {
42-
ReflexUtils.checkConflictingPackageNames(
42+
String? packageName = ReflexUtils.checkConflictingPackageNames(
4343
packageNameList, packageNameExceptionList);
44+
45+
if (packageName != null) {
46+
throw ReflexException(
47+
"Found $packageName both in packageNameList and packageNameExceptionList of Reflex!");
48+
}
49+
}
50+
51+
if (autoReply != null && autoReply.packageNameList != null) {
52+
// Check if package in autoReply.packageNameList is present in packageNameList
53+
if (packageNameList != null) {
54+
String? packageName = ReflexUtils.checkPackagePresence(
55+
packageNameList, autoReply.packageNameList ?? []);
56+
57+
if (packageName != null) {
58+
throw ReflexException(
59+
"You are not listening for $packageName, but you are using it for AutoReply!");
60+
}
61+
}
62+
63+
// Check conflicting auto reply packageNameList and packageNameExceptionList
64+
if (packageNameList == null && packageNameExceptionList != null) {
65+
String? packageName = ReflexUtils.checkConflictingPackageNames(
66+
packageNameExceptionList, autoReply.packageNameList ?? []);
67+
68+
if (packageName != null) {
69+
throw ReflexException(
70+
"Found $packageName both in packageNameExceptionList and autoReply.packageNameList!");
71+
}
72+
}
4473
}
4574

4675
Map<String, dynamic> map = {

lib/src/helper/model/auto_reply.dart

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

1111
class AutoReply {
1212
AutoReply({
13-
required this.packageName,
13+
this.packageNameList,
1414
required this.message,
1515
});
1616

17-
/// Package Name for [AutoReply]
18-
String packageName;
17+
/// Package Name List for [AutoReply]
18+
List<String>? packageNameList;
1919

2020
/// Notification Message
2121
String message;
2222

2323
factory AutoReply.fromMap(Map<dynamic, dynamic> map) {
2424
// Error Handling if map does not contain package name or message
25-
if (!map.containsKey('packageName') || !map.containsKey('message')) {
26-
throw Exception('Map does not contain package name or message');
25+
if (!map.containsKey('message')) {
26+
// TODO: Create own exception
27+
throw Exception('AutoReply do not contain message!');
2728
}
2829

29-
String packageName = map['packageName'];
30+
List<String>? packageNameList =
31+
map.containsKey('packageNameList') ? map['packageNameList'] : null;
3032
String message = map['message'];
3133

3234
return AutoReply(
33-
packageName: packageName,
35+
packageNameList: packageNameList,
3436
message: message,
3537
);
3638
}
3739

3840
Map<String, dynamic> deserialize() {
3941
return <String, dynamic>{
40-
'packageName': packageName,
42+
'packageNameList': packageNameList,
4143
'message': message,
4244
};
4345
}

lib/src/helper/utils/reflex_utils.dart

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,23 @@ import 'package:reflex/src/helper/exception/reflex_exception.dart';
33
class ReflexUtils {
44
ReflexUtils._();
55

6-
static void checkConflictingPackageNames(
6+
static String? checkConflictingPackageNames(
77
List<String> packageNameList,
88
List<String> packageNameExceptionList,
99
) {
1010
for (String packageName in packageNameList) {
1111
if (packageNameExceptionList.contains(packageName)) {
12-
throw ReflexException(
13-
"Found $packageName both in packageName and packageNameExceptionList!");
12+
return packageName;
13+
}
14+
}
15+
}
16+
17+
static String? checkPackagePresence(List<String> list1, List<String> list2) {
18+
// Chekcing if presence of list 2 in list 1
19+
for (String packageName in list2) {
20+
if (!list1.contains(packageName)) {
21+
return packageName;
22+
1423
}
1524
}
1625
}

0 commit comments

Comments
 (0)