jiusanzhou / flutter_floatwing

A Flutter plugin that makes it easier to make floating/overlay window for Android with pure Flutter. 一个可以用纯粹的Flutter来开发Android浮动窗口的插件。
Other
169 stars 28 forks source link

Can't run example app #3

Open gcostaapps opened 2 years ago

gcostaapps commented 2 years ago

Hi, great package!

I tried to run the example app and couldn't run it. So I've decided to create a minimal app based on the docs but couldn't run either.

I'm using the views for example folder and this is the code that I'm using for the main app

import 'package:floating_app/views/night.dart';
import 'package:floating_app/views/normal.dart';
import 'package:flutter/material.dart';
import 'package:flutter_floatwing/flutter_floatwing.dart';

import 'views/assistive_touch.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      routes: {
        '/': (_) => const MyHomePage(title: 'Flutter Demo Home Page'),
        '/normal': (_) => NormalView(),
        '/night': (_) => NightView(),
        '/assistive': (_) => AssistiveTouch(),
      },
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
    FloatwingPlugin().checkPermission().then((v) {
      if (!v) FloatwingPlugin().openPermissionSetting();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          FloatingActionButton(
            onPressed: () async => initializeSerices(),
            tooltip: 'Increment',
            child: const Icon(Icons.play_arrow),
          ),
          FloatingActionButton(
            onPressed: () async => _createWindow(),
            tooltip: 'Increment',
            child: const Icon(Icons.add),
          ),
          FloatingActionButton(
            onPressed: () async => _createWindow2(),
            tooltip: 'Increment',
            child: const Icon(Icons.minimize),
          ),
        ],
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  Future<void> initializeSerices() async {
    await FloatwingPlugin().initialize();

    await FloatwingPlugin().isServiceRunning().then((v) async {
      await FloatwingPlugin().startService().then((_) {
        print("start the backgroud service success.");
      });
    });
  }

  Future<void> _createWindow() async {
    final window = await WindowConfig(id: 'test2', route: '/normal')
        .to()
        .on(EventType.WindowCreated, (w, _) {
      print('TEST');
    }).create(start: true);
  }

  Future<void> _createWindow2() async {
    final windows = FloatwingPlugin().windows;
    final window = await WindowConfig(
      id: 'test2',
      entry: null,
      callback: myOverlayMain,
    ).create(start: true);
  }
}

void myOverlayMain() {
  runApp(((_) => NormalView()).floatwing(app: true).make());
}

If I click in the play button, then plus, then minimize, these are the logs that I'm getting:

D/FloatwingPlugin( 9657): [plugin] pixel radio already exits
D/FloatwingPlugin( 9657): [plugin] system config already exits: {"pixelRadio":2,"screen":{"width":1080,"height":1794}}
[log] [plugin] initialize result: {permission_grated: true, pixel_radio_updated: false, system_config_updated: false, service_running: false, windows: null}
[log] [plugin] there are 0 windows already started
D/FloatwingService( 9657): [service] wait for service created
I/flutter ( 9657): start the backgroud service success.
I/FloatwingService( 9657): [service] create a window: test2 {entry=main, route=/normal}
D/FloatwingService( 9657): [service] wait for service created
[log] [event] register listener window.created for Window[test2]@834626997, EventManager@193599441, config: {"entry":"main","route":"/normal"}

What am I doing wrong? The permission is added in the AndroidManifest and granted in the initState dialog.

I'm using Flutter 3.0 and created a new project.

jiusanzhou commented 2 years ago

Hi @gcostaapps thank you for your attention.

What happened when you run the example?

gcostaapps commented 2 years ago

@jiusanzhou , The example app had the buttons to start the floating items disabled, only the debug was enabled but it didn't work.

When I try to run the code that I posted, I can run but when I click to create and start the windows nothing happens.

jiusanzhou commented 2 years ago

The flutter engine for window needs a little bit time to get started. In the example, I registered a handle to set the button available while the created event emitted. If it's always unavailable, maybe there's something wrong.

I tested the example in flutter 2, maybe the plugin doesn't work well in flutter 3. I will test it with flutter 3 on this weekend.

jiusanzhou commented 2 years ago

@gcostaapps I found the issue, in the example forget to check and grant the permission.

I have fixed it, you can try it.

Thank you again.

gcostaapps commented 2 years ago

Hi @jiusanzhou , I've updated the code but the 'Open' button is still disabled. Even after I grant permission and tap the start button. Below are the logs

Connecting to VM Service at ws://127.0.0.1:59112/ziwE6MEyGbM=/ws
D/FloatwingPlugin(30781): [plugin] pixel radio need to be saved
D/eglCodecCommon(30781): setVertexArrayObject: set vao to 0 (0) 1 0
D/EGL_emulation(30781): eglMakeCurrent: 0xe021a240: ver 3 0 (tinfo 0xe020f220)
D/eglCodecCommon(30781): setVertexArrayObject: set vao to 0 (0) 1 0
[log] [plugin] initialize result: {permission_grated: false, pixel_radio_updated: true, system_config_updated: true, service_running: false, windows: null}
[log] [plugin] there are 0 windows already started
D/EGL_emulation(30781): eglMakeCurrent: 0xe021a240: ver 3 0 (tinfo 0xe020f220)
D/EGL_emulation(30781): eglMakeCurrent: 0xf34534a0: ver 3 0 (tinfo 0xf34892e0)
D/EGL_emulation(30781): eglMakeCurrent: 0xf34534a0: ver 3 0 (tinfo 0xf34892e0)
D/EGL_emulation(30781): eglMakeCurrent: 0xe021a240: ver 3 0 (tinfo 0xe020f220)
D/FloatwingService(30781): [service] wait for service created
D/FloatwingService(30781): [service] wait for service created
I/flutter (30781): start the backgroud service success.
I/FloatwingService(30781): [service] create a window: normal {entry=main, route=/normal, draggable=true}
D/FloatwingService(30781): [service] wait for service created
[log] [event] register listener window.created for Window[normal]@228681343, EventManager@1006652032, config: {"entry":"main","route":"/normal","draggable":true}
[log] [event] register listener window.created for Window[assitive_touch]@636263486, EventManager@1006652032, config: {"entry":"main","route":"/assitive_touch","draggable":true}
[log] [event] register listener window.created for Window[night]@219634324, EventManager@1006652032, config: {"entry":"main","route":"/night","width":-1,"height":-1,"clickable":false}
I/FloatwingService(30781): [service] create a window: assitive_touch {entry=main, route=/assitive_touch, draggable=true}
D/FloatwingService(30781): [service] wait for service created
I/FloatwingService(30781): [service] create a window: night {clickable=false, entry=main, route=/night, width=-1, height=-1}
D/FloatwingService(30781): [service] wait for service created
jiusanzhou commented 2 years ago

Hi @gcostaapps, According to the information in the log, it should be that the service failed to start. What version of android are you using?

The normal log should be,

D/FloatwingService(19841): [service] wait for service created
I/FloatwingService(19841): [service] the background service onCreate
D/FloatwingService(19841): [service] load the pixel radio: 2.0
D/FloatwingService(19841): [service] install the service handler for main engine
D/FloatwingService(19841): [service] set service as handler im.zoe.labs/flutter_floatwing/window for io.flutter.embedding.engine.FlutterEngine@4879e2
I/flutter (19841): start the backgroud service success.
I/FloatwingService(19841): [service] create a window: normal {entry=main, route=/normal, draggable=true}
D/FloatwingService(19841): [service] miss from cache need to create a new flutter engine
I/FloatwingService(19841): [service] start flutter engine, id: floatwing_flutter_engine_normal entrypoint: main, route: /normal
D/FloatwingPlugin(19841): [plugin] on attached to window engine
D/FloatwingService(19841): [service] set window as handler im.zoe.labs/flutter_floatwing/window for io.flutter.embedding.engine.FlutterEngine@68a2bab
D/FloatwingService(19841): [service] created window: normal {focusable=false, entry=main, route=/normal, draggable=true}
I/FloatwingService(19841): [service] create a window: assitive_touch {entry=main, route=/assitive_touch, draggable=true}
D/FloatwingService(19841): [service] miss from cache need to create a new flutter engine
I/FloatwingService(19841): [service] start flutter engine, id: floatwing_flutter_engine_assitive_touch entrypoint: main, route: /assitive_touch
D/FloatwingPlugin(19841): [plugin] on attached to window engine
D/FloatwingService(19841): [service] set window as handler im.zoe.labs/flutter_floatwing/window for io.flutter.embedding.engine.FlutterEngine@1459bc6
D/FloatwingService(19841): [service] created window: assitive_touch {focusable=false, entry=main, route=/assitive_touch, draggable=true}
I/FloatwingService(19841): [service] create a window: night {clickable=false, entry=main, route=/night, width=-1, height=-1}
D/FloatwingService(19841): [service] miss from cache need to create a new flutter engine
I/FloatwingService(19841): [service] start flutter engine, id: floatwing_flutter_engine_night entrypoint: main, route: /night
D/FloatwingPlugin(19841): [plugin] on attached to window engine
D/FloatwingService(19841): [service] set window as handler im.zoe.labs/flutter_floatwing/window for io.flutter.embedding.engine.FlutterEngine@b4eb2dd
D/FloatwingService(19841): [service] created window: night {clickable=false, focusable=false, entry=main, route=/night, width=-1, height=-1}
gcostaapps commented 2 years ago

Hi @jiusanzhou ,

I've downloaded the entire library an ran the example app from that and it worked! I don't know if master version is different from pub (as I was using pub version) but since it's working here I can find what I need to do. Thanks for the support and this great package!

oavasquez commented 2 years ago

Hi @jiusanzhou ,

I've downloaded the entire library an ran the example app from that and it worked! I don't know if master version is different from pub (as I was using pub version) but since it's working here I can find what I need to do. Thanks for the support and this great package!

Add the following line in your AndroidManifest.xml

<service android:name="im.zoe.labs.flutter_floatwing.FloatwingService" android:label="Floatwing Service"/>

psovit commented 1 year ago

Adding this <service android:name="im.zoe.labs.flutter_floatwing.FloatwingService" android:label="Floatwing Service"/> enables normal but assistive panel is still disabled.

@oavasquez @jiusanzhou

jiusanzhou commented 1 year ago

@psovit Hi, did you get any errors?

psovit commented 1 year ago

Hi @jiusanzhou , Yes, I am getting this error:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(error, java.lang.Integer cannot be cast to java.lang.Long, null, java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
E/flutter (31126):      at im.zoe.labs.flutter_floatwing.FloatWindow$Config$Companion.from(FloatWindow.kt:501)
E/flutter (31126):      at im.zoe.labs.flutter_floatwing.FlutterFloatwingPlugin.onMethodCall(FlutterFloatwingPlugin.kt:151)
E/flutter (31126):      at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:261)
E/flutter (31126):      at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
E/flutter (31126):      at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:321)
E/flutter (31126):      at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
E/flutter (31126):      at android.os.Handler.handleCallback(Handler.java:938)
E/flutter (31126):      at android.os.Handler.dispatchMessage(Handler.java:99)
E/flutter (31126):      at android.os.Looper.loop(Looper.java:223)
E/flutter (31126):      at android.app.ActivityThread.main(ActivityThread.java:7656)
E/flutter (31126):      at java.lang.reflect.Method.invoke(Native Method)
E/flutter (31126):      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
E/flutter (31126):      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
E/flutter (31126): )
E/flutter (31126): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:653:7)
E/flutter (31126): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:296:18)
E/flutter (31126): <asynchronous suspension>
E/flutter (31126): #2      MethodChannel.invokeMapMethod (package:flutter/src/services/platform_channel.dart:499:43)
E/flutter (31126): <asynchronous suspension>
E/flutter (31126): #3      FloatwingPlugin.internalCreateWindow (package:floating/package/src/plugin.dart:187:19)
E/flutter (31126): <asynchronous suspension>
E/flutter (31126): #4      FloatwingPlugin.createWindow (package:floating/package/src/plugin.dart:157:11)
E/flutter (31126): <asynchronous suspension>
E/flutter (31126): #5      Window.create (package:floating/package/src/window.dart:93:12)
E/flutter (31126): <asynchronous suspension>
E/flutter (31126): 
psovit commented 1 year ago

Ok, found the issue which has a simple fix. In your FloatWindow.kt class, you have cfg.callback = data["callback"] as Long?. This is throwing the java.lang.ClassCastException in Flutter environment : sdk: ">=2.12.0 <3.0.0".

Converting Int? seems to solve this issue:

val int_callback = data["callback"] as Int?
cfg.callback = int_callback?.toLong()

@jiusanzhou

yongjieYuan commented 1 year ago

Hi, I downloaded your entire project from your repository, I run it in Flutter 2.5.3,buit it show these errors:

image

I think the erro was from here:

image

But I can run it well in Flutter 3.3.3. @jiusanzhou

zhangyakai commented 9 months ago

Example Window not displayed: error log: Unable to match the desired swap behavior.

What is the reason for this?

rorystephenson commented 5 months ago

For people having trouble running the example app in the latest Flutter, it is working for me after upgrading Gradle config. See PR #23