forked from ezra-bible-app/ezra-bible-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcordova_main.js
135 lines (104 loc) · 4.23 KB
/
cordova_main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* This file is part of Ezra Bible App.
Copyright (C) 2019 - 2024 Tobias Klein <[email protected]>
Ezra Bible App is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
Ezra Bible App is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Ezra Project. See the file LICENSE.
If not, see <http://www.gnu.org/licenses/>. */
console.log('Starting nodejs backend from cordova_main.js');
const PlatformHelper = require('./app/lib/platform_helper.js');
const IPC = require('./app/backend/ipc/ipc.js');
global.ipc = null;
global.sendCrashReports = true;
class Main {
init(isDebug) {
// Require the 'cordova-bridge' to enable communications between the
// Node.js app and the Cordova app.
console.log('Loading cordova-bridge');
global.cordova = require('cordova-bridge');
this.platformHelper = new PlatformHelper();
this.isDebug = isDebug;
this.androidVersion = null;
if (!isDebug) {
console.log('Initializing Sentry');
this.initSentry();
}
console.log('Initializing app events');
this.initAppEvents();
console.log('Initializing non-persistent IPC');
global.ipc = new IPC();
global.ipc.initNonPersistentIpc();
const dataDir = cordova.app.datadir();
cordova.channel.send(`nodejs: cordova_main.js loaded / data dir: ${dataDir}`);
}
initSentry() {
var pjson = require('./package.json');
var version = pjson.version;
console.log("Configuring Sentry (node.js) with app version: " + version);
try {
// Loading Sentry in a try/catch block, because we have observed failures related to this step.
// If it fails ... startup is broken. Why did it fail previously? After a sentry upgrade the
// path to the sources had changed and the require statement did not work anymore.
global.Sentry = require('@sentry/node/dist');
Sentry.init({
dsn: 'https://[email protected]/1488321',
release: version,
beforeSend: (event) => global.sendCrashReports ? event : null
});
} catch (error) {
console.error('Sentry initialization failed with an error!');
console.log(error);
}
}
initPersistentIpc(androidVersion=undefined) {
console.log("Initializing persistent IPC!");
this.androidVersion = androidVersion;
this.initStorage(androidVersion);
global.ipc.init(this.isDebug, undefined, androidVersion);
return true;
}
async initDatabase(androidVersion=undefined, connectionType=undefined) {
console.log("Initializing database!");
console.log(`Connection type: ${connectionType}`);
if (connectionType === undefined) {
connectionType = global.connectionType;
}
let initDbResult = await global.ipc.initDatabase(this.isDebug, androidVersion, connectionType);
return initDbResult;
}
async closeDatabase() {
await global.ipc.closeDatabase();
}
initAppEvents() {
// Handle the 'pause' and 'resume' events.
// These are events raised automatically when the app switched to the
// background/foreground.
cordova.app.on('pause', async (pauseLock) => {
console.log('[node] App paused. Closing database!');
await this.closeDatabase();
console.log('[node] Database closed!');
pauseLock.release();
});
cordova.app.on('resume', () => {
console.log('[node] Resume: Re-initializing database.');
this.initDatabase(this.androidVersion);
console.log('[node] App resumed.');
cordova.channel.post('engine', 'resumed');
});
}
initStorage(androidVersion=undefined) {
const fs = require('fs');
var path = this.platformHelper.getUserDataPath(false, androidVersion);
if (!fs.existsSync(path)) {
console.log("Creating data directory for app at " + path);
fs.mkdirSync(path, { recursive: true });
}
}
}
module.exports = Main;