Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not keep the screen awake on Android when accessed via chrome #7

Open
odkken opened this issue Sep 6, 2023 · 7 comments
Open

Comments

@odkken
Copy link

odkken commented Sep 6, 2023

Hosting the app locally, accessing it on an android device via chrome over LAN (e.g. 192.168.1.12:port)

The future returns the updated enabled/disabled value, but the screen still turns off.

If I go to the nosleep demo page on the same phone / browser (https://richtr.github.io/NoSleep.js/example) the screen stays on.

@diegotori
Copy link
Collaborator

diegotori commented Sep 6, 2023

@odkken thank you for bringing this up.

Unfortunately, I don't have the experience in debugging mobile web apps from Chrome, so I might be hamstrung in trying to investigate this further.

If you can provide an example app that reproduces this, that would also help.

Lastly, I do take pull requests to resolve this issue. So if you have a lead on a possible fix, I'll be happy to review it.

@odkken
Copy link
Author

odkken commented Sep 6, 2023

Sure, the provided example app is all I'm running. If you just launch it with

flutter run -d web-server --web-hostname 0.0.0.0 --web-port 12345

then access that from your phone's browser, you should be able to reproduce

@odkken
Copy link
Author

odkken commented Sep 6, 2023

Huh, this seems to be an android-specific issue. I tried it on an iphone and it works fine.

@odkken
Copy link
Author

odkken commented Sep 7, 2023

Here is a very simple working widget to provide the functionality:

NOTE: the wakeLock object on the navigator is only available on https connections!

class WakeLockWidget extends StatefulWidget {
  const WakeLockWidget({super.key});

  @override
  _WakeLockWidgetState createState() => _WakeLockWidgetState();
}

class _WakeLockWidgetState extends State<WakeLockWidget> {
  bool _isSupported = false;
  var _wakeLock;

  @override
  void initState() {
    super.initState();
    _checkSupport();
  }

  _checkSupport() {
    final navigatorObj = js.JsObject.fromBrowserObject(js.context['navigator']);
    if (navigatorObj.hasProperty('wakeLock')) {
      setState(() => _isSupported = true);
    }
  }

  enable() {
    if (!_isSupported) return;

    final navigatorObj = js.JsObject.fromBrowserObject(js.context['navigator']);
    final promise = navigatorObj['wakeLock'].callMethod('request', ['screen']);

    // Handle the promise resolution
    js.JsObject.fromBrowserObject(promise).callMethod('then', [
      (val) {
        setState(() {
          _wakeLock = val;
        });
      }
    ]);
  }

  disable() {
    if (!_isSupported || _wakeLock == null) return;

    final wakeLockObj = js.JsObject.fromBrowserObject(_wakeLock);
    if (wakeLockObj.hasProperty('release')) wakeLockObj.callMethod('release');
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        ElevatedButton(onPressed: enable, child: Text('Enable WakeLock')),
        ElevatedButton(onPressed: disable, child: Text('Disable WakeLock')),
      ],
    );
  }
}

I will leave this here rather than submitting a pull request, as I am not familiar with flutter package development, and I think this would need to be added to the original wakelock package (the web implementation specifically).

@okku
Copy link

okku commented Sep 9, 2023

I can confirm there is some kind of issue with web, I tried on mobile both chrome and safari. nosleep.js testpage works fine.

@ocinon
Copy link

ocinon commented Dec 22, 2023

@diegotori any update on including/fixing this? I'd love to remove the hack, as it breaks cross-platform compatibility while js is included directly.

@ocinon
Copy link

ocinon commented Feb 5, 2024

It works in the latest Flutter version for me now. Thank you for making/maintaining the package!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants