Skip to content

Commit ca47aaa

Browse files
authored
Merge pull request #112 from aryaveerruno/flutter_view
added Flutter view in window manager
2 parents ab68334 + 8fadc1e commit ca47aaa

22 files changed

+416
-858
lines changed

README.md

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,69 @@ Displays as a notification in the notification center [Help Needed]
4444

4545
## Example
4646

47-
### Request overlay permission
47+
#### Show Overlay
48+
49+
#### Request overlay permission
4850
await SystemAlertWindow.requestPermissions;
4951

50-
### Show the overlay
52+
### Inside `main.dart` create an entry point for your Overlay widget;
53+
```dart
54+
// overlay entry point
55+
@pragma("vm:entry-point")
56+
void overlayMain() {
57+
runApp(const MaterialApp(
58+
debugShowCheckedModeBanner: false,
59+
home: Material(child: Text("My overlay"))
60+
));
61+
}
62+
63+
64+
//Open overLay content
65+
66+
// - Optional arguments:
67+
/// `gravity` Position of the window and default is [SystemWindowGravity.CENTER]
68+
/// `width` Width of the window and default is [Constants.MATCH_PARENT]
69+
/// `height` Height of the window and default is [Constants.WRAP_CONTENT]
70+
/// `notificationTitle` Notification title, applicable in case of bubble
71+
/// `notificationBody` Notification body, applicable in case of bubble
72+
/// `prefMode` Preference for the system window. Default is [SystemWindowPrefMode.DEFAULT]
73+
/// `isDisableClicks` Disables the clicks across the system window. Default is false. This is not applicable for bubbles.
74+
await SystemAlertWindow.showSystemWindow();
75+
76+
// closes overlay if open
77+
await SystemAlertWindow.closeSystemWindow();
78+
79+
// broadcast data to and from overlay app
80+
await SystemAlertWindow.sendMessageToOverlay("Hello from the other side");
81+
82+
//streams message shared between overlay and main app
83+
SystemAlertWindow.overlayListener.listen((event) {
84+
log("Current Event: $event");
85+
});
86+
87+
//to add custom logs in log file
88+
SystemAlertWindow.addCustomLog("add custom log")
89+
90+
91+
/// update the overlay flag while the overlay in action
92+
/// - Optional arguments:
93+
/// `gravity` Position of the window and default is [SystemWindowGravity.CENTER]
94+
/// `width` Width of the window and default is [Constants.MATCH_PARENT]
95+
/// `height` Height of the window and default is [Constants.WRAP_CONTENT]
96+
/// `notificationTitle` Notification title, applicable in case of bubble
97+
/// `notificationBody` Notification body, applicable in case of bubble
98+
/// `prefMode` Preference for the system window. Default is [SystemWindowPrefMode.DEFAULT]
99+
/// `isDisableClicks` Disables the clicks across the system window. Default is false. This is not applicable for bubbles.
100+
await FlutterOverlayWindow.updateSystemWindow();
101+
102+
```
103+
104+
### Close the overlay
105+
SystemAlertWindow.closeSystemWindow();
106+
107+
108+
109+
#### Show the Bubble
51110
52111
SystemWindowHeader header = SystemWindowHeader(
53112
title: SystemWindowText(text: "Incoming Call", fontSize: 10, textColor: Colors.black45),
@@ -111,7 +170,7 @@ Displays as a notification in the notification center [Help Needed]
111170
//Using SystemWindowPrefMode.OVERLAY forces overlay window instead of bubble in Android 11.
112171
//Using SystemWindowPrefMode.BUBBLE forces Bubble instead of overlay window in Android 10 & above
113172
114-
### Register for onClick events (button click)
173+
#### Register for onClick events (button click)
115174

116175
SystemAlertWindow.registerOnClickListener(callBackFunction);
117176

@@ -136,12 +195,9 @@ Displays as a notification in the notification center [Help Needed]
136195
}
137196
}
138197
139-
### Close the overlay
140198

141-
SystemAlertWindow.closeSystemWindow();
142-
143-
### Isolate communication
144-
##### Use this snippet, if you want the callbacks on your main thread, instead of handling them in an isolate (like mentioned above)
199+
#### Isolate communication
200+
###### Use this snippet, if you want the callbacks on your main thread, instead of handling them in an isolate (like mentioned above)
145201

146202
###### Create an isolate_manager.dart
147203
```
@@ -193,7 +249,7 @@ class IsolateManager{
193249
SystemAlertWindow.registerOnClickListener(callBackFunction);
194250
```
195251

196-
###### Now the callBackFunction should looks like
252+
###### Now the callBackFunction should looks like
197253
```
198254
bool callBackFunction(String tag) {
199255
print("Got tag " + tag);
@@ -202,3 +258,7 @@ bool callBackFunction(String tag) {
202258
return true;
203259
}
204260
```
261+
262+
263+
264+

android/src/main/java/in/jvapps/system_alert_window/SystemAlertWindowPlugin.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,36 @@
11
package in.jvapps.system_alert_window;
22

3+
import android.content.Context;
4+
35
import androidx.annotation.NonNull;
46
import androidx.annotation.Nullable;
57

8+
import in.jvapps.system_alert_window.utils.Commons;
9+
import in.jvapps.system_alert_window.utils.Constants;
610
import in.jvapps.system_alert_window.utils.ContextHolder;
711
import in.jvapps.system_alert_window.utils.LogUtils;
12+
import io.flutter.FlutterInjector;
13+
import io.flutter.embedding.engine.FlutterEngine;
14+
import io.flutter.embedding.engine.FlutterEngineCache;
15+
import io.flutter.embedding.engine.FlutterEngineGroup;
16+
import io.flutter.embedding.engine.dart.DartExecutor;
817
import io.flutter.embedding.engine.plugins.FlutterPlugin;
918
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
1019
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
20+
import io.flutter.plugin.common.BasicMessageChannel;
21+
import io.flutter.plugin.common.JSONMessageCodec;
1122

12-
public class SystemAlertWindowPlugin implements FlutterPlugin, ActivityAware {
23+
public class SystemAlertWindowPlugin implements FlutterPlugin, ActivityAware,BasicMessageChannel.MessageHandler {
1324

1425
private boolean isInitialized;
1526

1627
@Nullable
1728
private ActivityPluginBinding pluginBinding;
1829
MethodCallHandlerImpl methodCallHandler;
1930
private final String TAG = "SAW:Plugin";
31+
private Context context;
32+
private BasicMessageChannel<Object> messenger;
33+
2034

2135
public SystemAlertWindowPlugin() {
2236
LogUtils.getInstance().d(TAG, "Initializing the constructor");
@@ -60,7 +74,12 @@ private void dispose() {
6074

6175
@Override
6276
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
77+
this.context = flutterPluginBinding.getApplicationContext();
6378
initialize(flutterPluginBinding);
79+
messenger = new BasicMessageChannel<>(flutterPluginBinding.getBinaryMessenger(), Constants.MESSAGE_CHANNEL,
80+
JSONMessageCodec.INSTANCE);
81+
messenger.setMessageHandler(this);
82+
Commons.messenger = messenger;
6483
}
6584

6685
@Override
@@ -78,6 +97,13 @@ public void onAttachedToActivity(@NonNull ActivityPluginBinding activityPluginBi
7897
LogUtils.getInstance().d(TAG, "Initializing on attached to activity");
7998
if (methodCallHandler != null) {
8099
methodCallHandler.setActivity(activityPluginBinding.getActivity());
100+
FlutterEngineGroup enn = new FlutterEngineGroup(context);
101+
DartExecutor.DartEntrypoint dEntry = new DartExecutor.DartEntrypoint(
102+
FlutterInjector.instance().flutterLoader().findAppBundlePath(),
103+
"overlayMain");
104+
FlutterEngine engine = enn.createAndRunEngine(context, dEntry);
105+
FlutterEngineCache.getInstance().put(Constants.FLUTTER_CACHE_ENGINE, engine);
106+
81107
}
82108
this.pluginBinding = activityPluginBinding;
83109
registerListeners();
@@ -101,4 +127,13 @@ public void onDetachedFromActivity() {
101127
}
102128
deregisterListeners();
103129
}
130+
131+
@Override
132+
public void onMessage(@Nullable Object message, @NonNull BasicMessageChannel.Reply reply) {
133+
BasicMessageChannel overlayMessageChannel = new BasicMessageChannel(
134+
FlutterEngineCache.getInstance().get(Constants.FLUTTER_CACHE_ENGINE)
135+
.getDartExecutor(),
136+
Constants.MESSAGE_CHANNEL, JSONMessageCodec.INSTANCE);
137+
overlayMessageChannel.send(message, reply);
138+
}
104139
}

0 commit comments

Comments
 (0)