EddyVerbruggen / Insomnia-PhoneGap-Plugin

:sleepy: Prevent the screen of the mobile device from falling asleep

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Android: keepAwake invokes the error callback

Tawpie opened this issue · comments

and the screen won't auto-lock. Encountered on 4.4.x and Marshmallow. The plugin is able to prevent screen sleep though!

Do you see this on the most recent cordova-android version as well as an older one? Perhaps you can share your project?

The project is 1.4GB and even if it was a good idea I can’t share it publicly. Details on the platform:
Cordova 5.3.3 Installed platforms: android 4.1.1, ios 3.9.2

We have to use everything downrev because our plugin soup includes plugins that aren’t compatible or whose new versions for Cordova 6.x are too scary to incorporate. Here’s that list:
cc.fovea.plugins.inapppurchase 3.4.1 "InAppPurchase"
com.phonegap.plugins.PushPlugin 2.5.0 "PushPlugin"
com.smartmobilesoftware.inappbilling 3.0.0 "Android InAppBilling"
cordova-plugin-app-event 1.2.0 "Application Events"
cordova-plugin-background-task 0.2.0 "Background Task"
cordova-plugin-camera 2.1.0 "Camera"
cordova-plugin-console 1.0.2 "Console"
cordova-plugin-device 1.1.1 "Device"
cordova-plugin-dialogs 1.2.0 "Notification"
cordova-plugin-file 4.1.1 "File"
cordova-plugin-globalization 1.0.2 "Globalization"
cordova-plugin-inappbrowser 1.3.0 "InAppBrowser"
cordova-plugin-insomnia 4.2.0 "Insomnia (prevent screen sleep)"
cordova-plugin-media 1.0.1 "Media"
cordova-plugin-network-information 1.2.0 "Network Information"
cordova-plugin-splashscreen 3.2.0 "Splashscreen"
cordova-plugin-statusbar 2.1.1 "StatusBar"
cordova-plugin-vibration 2.1.0 "Vibration"
cordova-plugin-whitelist 1.2.2-dev "Whitelist"
de.appplant.cordova.plugin.email-composer 0.8.2 "EmailComposer"
de.appplant.cordova.plugin.local-notification 0.8.4 “LocalNotification"

Here are the methods:

// ************ support for preventing lock screen engagement **********
function tryPreventScreenLock() {
if (isDefined(window.plugins.insomnia) && isDefined(window.plugins.insomnia.keepAwake)) {
//console.log("a.tPSL - screen lock is prevented");
gbScreenLockCurrentlyPrevented = true;
window.plugins.insomnia.keepAwake(console.log("a.tPSL - screen lock is prevented"), console.log("a.tPSL - screen lock prevention FAILED for unknown reasons"));
} else {
gbScreenLockCurrentlyPrevented = null;
console.log("a.tPSL - prevention of screen lock was requested but not possible because plugin is likely missing");
}
}

function tryAllowScreenLock() {
if (isDefined(window.plugins.insomnia) && isDefined(window.plugins.insomnia.allowSleepAgain)) {
//console.log("a.tPSL - screen lock prevention has been lifted");
gbScreenLockCurrentlyPrevented = false;
window.plugins.insomnia.allowSleepAgain(console.log("a.tPSL - screen lock prevention has been lifted"), console.log("a.tPSL - lifting prevention of screen lock FAILED for unknown reasons"));
} else {
gbScreenLockCurrentlyPrevented = null;
console.log("a.tPSL - lifting prevention of screen lock was requested but not possible because plugin is likely missing");
}
}

I haven’t checked it on iOS yet.

On Mar 7, 2016, at 11:13 AM, Eddy Verbruggen notifications@github.com wrote:

Do you see this on the most recent cordova-android version as well as an older one? Perhaps you can share your project?


Reply to this email directly or view it on GitHub.

If that's actual code then those console.log callbacks aren't valid afaik.

I don't want your app of course, just a simple app reproducing this issue ;)

I abbreviated...

window.plugins.insomnia.keepAwake(onPreventScreenLockSuccess(), onPreventScreenLockError());

perhaps I misinterpreted something: the first passed parameter will be called pretty much always. The second I only expected to be called in case of a fatal error.

Switching to the enable side for a sec, when I say
window.plugins.insomnia.keepAwake(onPreventScreenLockSuccess(), onPreventScreenLockError())

I get both callbacks firing so I would assume something bad happened... but the screen doesn't lock (which is good).

On the window.plugins.insomnia.allowSleepAgain(onAllowScreenLockSuccess(), onAllowScreenLockError()) side, both callbacks also fire but the screen doesn't lock until I quit my app.

So now I'm sort of confused.

They fire because you invoke them ()

krap. my bad. DUH

kk. That cleared up the callbacks (apologize for raising that with my error in it!). But the screen won't auto-lock after

window.plugins.insomnia.allowSleepAgain(onAllowScreenLockSuccess, onAllowScreenLockError)

and only the first callback is invoked so I believe it thinks it was successful. This is a Nexus 5 on Android 6.0.1. I'll close this issue as it's my error and open another.

👍

I'd be helped if you can first reproduce it on a vanilla cordova app:
(I would do it but don't currently have a similar device to test)

cordova create insomniatest
cd insomniatest
cordova plugin add cordova-plugin-insomnia
cordova prepare android
// now add 2 buttons to index.html triggering keepAwake() and allowSleepAgain()

Never mind... not sure what was going on, but rebuild etc. and all is working just fine—the plugin doesn't have an issue. Apologies!