Skip to content

Commit b045022

Browse files
Can not make it work on a Nativescript-Vue project #6
1 parent 53b904a commit b045022

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

insomnia.android.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
1-
var appModule = require("application");
1+
var app = require("tns-core-modules/application");
22

33
exports.keepAwake = function () {
4+
5+
var keepScreenOn = function() {
6+
var activity = app.android.foregroundActivity || app.android.startActivity;
7+
var window = activity.getWindow();
8+
window.addFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
9+
};
10+
411
return new Promise(function (resolve, reject) {
512
try {
6-
var window = appModule.android.currentContext.getWindow();
7-
window.addFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
8-
resolve();
13+
14+
if (app.android.foregroundActivity || app.android.startActivity) {
15+
keepScreenOn();
16+
resolve();
17+
} else {
18+
app.android.on("activityStarted", function (args) {
19+
keepScreenOn();
20+
resolve();
21+
});
22+
}
23+
924
} catch (ex) {
1025
console.log("Error in insomnia.keepAwake: " + ex);
1126
reject(ex);
@@ -16,12 +31,13 @@ exports.keepAwake = function () {
1631
exports.allowSleepAgain = function () {
1732
return new Promise(function (resolve, reject) {
1833
try {
19-
var window = appModule.android.currentContext.getWindow();
34+
var activity = app.android.foregroundActivity || app.android.startActivity;
35+
var window = activity.getWindow();
2036
window.clearFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
2137
resolve();
2238
} catch (ex) {
2339
console.log("Error in insomnia.allowSleepAgain: " + ex);
2440
reject(ex);
2541
}
2642
});
27-
};
43+
};

insomnia.ios.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var utils = require("utils/utils");
1+
var utils = require("tns-core-modules/utils/utils");
22

33
exports.keepAwake = function () {
44
return new Promise(function (resolve, reject) {
@@ -28,4 +28,4 @@ exports.allowSleepAgain = function () {
2828
reject(ex);
2929
}
3030
});
31-
};
31+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-insomnia",
3-
"version": "1.2.1",
3+
"version": "1.2.2",
44
"description": "Make the screen not dim (and eventually lock the device) while Insomnia is active",
55
"main": "insomnia",
66
"typings": "index.d.ts",

0 commit comments

Comments
 (0)