keeweb/app/scripts/comp/launcher/launcher-cordova.js

284 lines
7.9 KiB
JavaScript
Raw Normal View History

2017-04-12 00:06:44 +02:00
/* global FingerprintAuth */
2017-02-05 22:01:46 +01:00
2019-09-16 22:57:56 +02:00
import { Events } from 'framework/events';
2018-06-01 17:06:54 +02:00
2017-02-05 22:01:46 +01:00
const Launcher = {
2017-02-22 01:35:16 +01:00
name: 'cordova',
version: window.cordova.platformVersion,
2017-02-05 22:01:46 +01:00
autoTypeSupported: false,
2017-04-12 00:06:44 +02:00
thirdPartyStoragesSupported: false,
clipboardSupported: false,
2019-08-18 10:17:09 +02:00
ready(callback) {
2018-06-01 17:06:54 +02:00
document.addEventListener('deviceready', callback, false);
2019-08-16 23:05:39 +02:00
document.addEventListener(
'pause',
() => {
2019-09-16 22:57:56 +02:00
Events.emit('app-minimized');
2019-08-16 23:05:39 +02:00
},
false
);
2017-02-05 22:01:46 +01:00
},
2019-08-18 10:17:09 +02:00
platform() {
2017-02-05 22:01:46 +01:00
return 'cordova';
},
2019-08-18 10:17:09 +02:00
openLink(href) {
2017-02-05 22:01:46 +01:00
window.open(href, '_system');
},
2017-03-19 00:30:29 +01:00
devTools: false,
openDevTools() {},
2019-08-18 10:17:09 +02:00
getSaveFileName(defaultPath, callback) {
2019-08-16 23:05:39 +02:00
/* skip in cordova */
},
2019-08-18 10:17:09 +02:00
getDataPath(...args) {
2017-02-05 22:01:46 +01:00
const storagePath = window.cordova.file.externalDataDirectory;
2020-06-01 16:53:51 +02:00
return [storagePath].concat(Array.from(args)).filter((s) => !!s);
2017-02-05 22:01:46 +01:00
},
2019-08-18 10:17:09 +02:00
getUserDataPath(fileName) {
2017-03-19 00:30:29 +01:00
return this.getDataPath('userdata', fileName).join('/');
2017-02-05 22:01:46 +01:00
},
2019-08-18 10:17:09 +02:00
getTempPath(fileName) {
2017-03-19 00:30:29 +01:00
return this.getDataPath('temp', fileName).join('/');
2017-02-05 22:01:46 +01:00
},
2019-08-18 10:17:09 +02:00
getDocumentsPath(fileName) {
2017-03-19 00:30:29 +01:00
return this.getDataPath('documents', fileName).join('/');
2017-02-05 22:01:46 +01:00
},
2019-08-18 10:17:09 +02:00
getAppPath(fileName) {
2017-03-19 00:30:29 +01:00
return this.getDataPath(fileName).join('/');
2017-02-05 22:01:46 +01:00
},
2019-08-18 10:17:09 +02:00
getWorkDirPath(fileName) {
2017-03-19 00:30:29 +01:00
return this.getDataPath(fileName).join('/');
2017-02-05 22:01:46 +01:00
},
2019-08-18 10:17:09 +02:00
joinPath(...parts) {
2017-04-11 01:34:59 +02:00
return [...parts].join('/');
},
2019-08-18 10:17:09 +02:00
writeFile(path, data, callback) {
2020-06-01 16:53:51 +02:00
const createFile = (filePath) => {
2019-08-16 23:05:39 +02:00
window.resolveLocalFileSystemURL(
filePath.dir,
2020-06-01 16:53:51 +02:00
(dir) => {
2019-08-16 23:05:39 +02:00
dir.getFile(filePath.file, { create: true }, writeFile);
},
callback,
callback
);
};
2020-06-01 16:53:51 +02:00
const writeFile = (fileEntry) => {
fileEntry.createWriter((fileWriter) => {
2017-02-05 22:01:46 +01:00
fileWriter.onerror = callback;
fileWriter.onwriteend = () => callback();
fileWriter.write(data);
}, callback);
};
2019-08-16 23:05:39 +02:00
if (path.startsWith('cdvfile://')) {
// then file exists
window.resolveLocalFileSystemURL(path, writeFile, callback, callback);
2019-08-16 23:05:39 +02:00
} else {
// create file on sd card
const filePath = this.parsePath(path);
this.mkdir(filePath.dir, () => {
createFile(filePath);
});
}
2017-02-05 22:01:46 +01:00
},
2019-08-18 10:17:09 +02:00
readFile(path, encoding, callback) {
2019-08-16 23:05:39 +02:00
window.resolveLocalFileSystemURL(
path,
2020-06-01 16:53:51 +02:00
(fileEntry) => {
2019-08-16 23:05:39 +02:00
fileEntry.file(
2020-06-01 16:53:51 +02:00
(file) => {
2019-08-16 23:05:39 +02:00
const reader = new FileReader();
reader.onerror = callback;
reader.onloadend = () => {
const contents = new Uint8Array(reader.result);
2019-08-18 08:05:38 +02:00
callback(
encoding ? String.fromCharCode.apply(null, contents) : contents
);
2019-08-16 23:05:39 +02:00
};
reader.readAsArrayBuffer(file);
},
2020-06-01 16:53:51 +02:00
(err) => callback(undefined, err)
2019-08-16 23:05:39 +02:00
);
},
2020-06-01 16:53:51 +02:00
(err) => callback(undefined, err)
2019-08-16 23:05:39 +02:00
);
2017-02-05 22:01:46 +01:00
},
2019-08-18 10:17:09 +02:00
fileExists(path, callback) {
2020-03-14 20:29:55 +01:00
window.resolveLocalFileSystemURL(
path,
2020-06-01 16:53:51 +02:00
(fileEntry) => callback(true),
2020-03-14 20:29:55 +01:00
() => callback(false)
);
2017-02-05 22:01:46 +01:00
},
2019-08-18 10:17:09 +02:00
deleteFile(path, callback) {
2019-08-16 23:05:39 +02:00
window.resolveLocalFileSystemURL(
path,
2020-06-01 16:53:51 +02:00
(fileEntry) => {
2019-08-16 23:05:39 +02:00
fileEntry.remove(callback, callback, callback);
},
callback
);
2017-02-05 22:01:46 +01:00
},
2019-08-18 10:17:09 +02:00
statFile(path, callback) {
2019-08-16 23:05:39 +02:00
window.resolveLocalFileSystemURL(
path,
2020-06-01 16:53:51 +02:00
(fileEntry) => {
2019-08-16 23:05:39 +02:00
fileEntry.file(
2020-06-01 16:53:51 +02:00
(file) => {
2019-08-16 23:05:39 +02:00
callback({
ctime: new Date(file.lastModified),
mtime: new Date(file.lastModified)
});
},
2020-06-01 16:53:51 +02:00
(err) => callback(undefined, err)
2019-08-16 23:05:39 +02:00
);
},
2020-06-01 16:53:51 +02:00
(err) => callback(undefined, err)
2019-08-16 23:05:39 +02:00
);
2017-02-05 22:01:46 +01:00
},
2019-08-18 10:17:09 +02:00
mkdir(dir, callback) {
2017-03-19 00:30:29 +01:00
const basePath = this.getDataPath().join('/');
const createDir = (dirEntry, path, callback) => {
const name = path.shift();
2019-08-16 23:05:39 +02:00
dirEntry.getDirectory(
name,
{ create: true },
2020-06-01 16:53:51 +02:00
(dirEntry) => {
2019-08-16 23:05:39 +02:00
if (path.length) {
// there is more to create
createDir(dirEntry, path, callback);
} else {
callback();
}
},
callback
);
};
2019-08-16 23:05:39 +02:00
const localPath = dir
.replace(basePath, '')
.split('/')
2020-06-01 16:53:51 +02:00
.filter((s) => !!s);
if (localPath.length) {
2019-08-16 23:05:39 +02:00
window.resolveLocalFileSystemURL(
basePath,
2020-06-01 16:53:51 +02:00
(dirEntry) => {
2019-08-16 23:05:39 +02:00
createDir(dirEntry, localPath, callback);
},
callback
);
} else {
callback();
}
2017-02-05 22:01:46 +01:00
},
2019-08-18 10:17:09 +02:00
parsePath(fileName) {
2017-02-05 22:01:46 +01:00
const parts = fileName.split('/');
return {
path: fileName,
2017-04-25 22:52:07 +02:00
file: parts.pop(),
dir: parts.join('/')
2017-02-05 22:01:46 +01:00
};
},
2019-08-18 10:17:09 +02:00
createFsWatcher(path) {
2017-02-05 22:01:46 +01:00
return null; // not in android with content provider
},
ensureRunnable(path) {},
2019-08-18 10:17:09 +02:00
preventExit(e) {
2017-02-05 22:01:46 +01:00
e.returnValue = false;
return false;
},
2019-08-18 10:17:09 +02:00
exit() {
2017-03-19 00:30:29 +01:00
this.hideApp();
},
2019-08-18 10:17:09 +02:00
requestExit() {
2019-08-16 23:05:39 +02:00
/* skip in cordova */
},
2019-08-18 10:17:09 +02:00
requestRestart() {
2017-02-05 22:01:46 +01:00
window.location.reload();
},
2019-08-18 10:17:09 +02:00
cancelRestart() {
2019-08-16 23:05:39 +02:00
/* skip in cordova */
},
2019-08-18 10:17:09 +02:00
setClipboardText(text) {},
getClipboardText() {},
clearClipboardText() {},
minimizeApp() {
2017-02-05 22:01:46 +01:00
this.hideApp();
},
2019-08-18 10:17:09 +02:00
canDetectOsSleep() {
return false;
},
2019-08-18 10:17:09 +02:00
updaterEnabled() {
2017-02-05 22:01:46 +01:00
return false;
},
// getMainWindow() {},
2019-08-18 10:17:09 +02:00
resolveProxy(url, callback) {
2019-08-16 23:05:39 +02:00
/* skip in cordova */
},
2019-08-18 10:17:09 +02:00
hideApp() {
2019-08-16 23:05:39 +02:00
/* skip in cordova */
},
2019-08-18 10:17:09 +02:00
isAppFocused() {
2017-03-29 00:25:16 +02:00
return false; /* skip in cordova */
2017-02-05 22:01:46 +01:00
},
2019-08-18 10:17:09 +02:00
showMainWindow() {
2019-08-16 23:05:39 +02:00
/* skip in cordova */
},
// spawn(config) {},
2019-08-18 10:17:09 +02:00
openFileChooser(callback) {
2020-06-01 16:53:51 +02:00
const onFileSelected = function (selected) {
window.resolveLocalFileSystemURL(selected.uri, (fileEntry) => {
fileEntry.file((file) => {
file.path = file.localURL;
file.name = selected.name;
2017-03-19 00:30:29 +01:00
callback(null, file);
});
});
2017-02-05 22:36:32 +01:00
};
2017-03-19 00:30:29 +01:00
window.cordova.exec(onFileSelected, callback, 'FileChooser', 'choose');
2017-02-05 22:36:32 +01:00
},
setGlobalShortcuts(appSettings) {},
2017-02-05 22:01:46 +01:00
fingerprints: {
2017-02-05 22:36:32 +01:00
config: {
disableBackup: true,
clientId: 'keeweb'
},
2019-08-18 10:17:09 +02:00
register(fileId, password, callback) {
2020-06-01 16:53:51 +02:00
FingerprintAuth.isAvailable((result) => {
2017-02-05 22:36:32 +01:00
if (!result.isAvailable) {
return;
}
2017-02-05 22:01:46 +01:00
2019-09-17 23:44:17 +02:00
const encryptConfig = {
...this.config,
2017-04-12 00:06:44 +02:00
username: fileId,
password: password.getText()
2019-09-17 23:44:17 +02:00
};
2017-02-05 22:36:32 +01:00
2020-06-01 16:53:51 +02:00
FingerprintAuth.encrypt(encryptConfig, (result) => {
2017-04-12 00:06:44 +02:00
callback(result.token);
2017-02-05 22:36:32 +01:00
});
});
2017-02-05 22:01:46 +01:00
},
2017-02-05 22:36:32 +01:00
2019-08-18 10:17:09 +02:00
auth(fileId, token, callback) {
if (!token) {
return callback();
2017-02-05 22:36:32 +01:00
}
2019-09-17 23:44:17 +02:00
const decryptConfig = { ...this.config, username: fileId, token };
2017-02-05 22:01:46 +01:00
2020-06-01 16:53:51 +02:00
FingerprintAuth.decrypt(decryptConfig, (result) => {
2017-02-05 22:36:32 +01:00
callback(result.password);
});
2017-02-05 22:01:46 +01:00
}
}
};
2019-09-15 14:16:32 +02:00
export { Launcher };