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

217 lines
6.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
const Launcher = {
2017-02-22 01:35:16 +01:00
name: 'cordova',
2017-02-05 22:01:46 +01:00
version: '6.0.0',
autoTypeSupported: false,
2017-04-12 00:06:44 +02:00
thirdPartyStoragesSupported: false,
2017-02-05 22:01:46 +01:00
ready: function(callback) {
document.addEventListener('deviceready', callback, false);
2017-02-05 22:01:46 +01:00
},
platform: function() {
return 'cordova';
},
openLink: function(href) {
window.open(href, '_system');
},
2017-03-19 00:30:29 +01:00
devTools: false,
// openDevTools: function() { },
2017-03-29 00:25:16 +02:00
getSaveFileName: function(defaultPath, callback) { /* skip in cordova */ },
2017-03-19 00:30:29 +01:00
getDataPath: function() {
2017-02-05 22:01:46 +01:00
const storagePath = window.cordova.file.externalDataDirectory;
return [storagePath].concat(Array.from(arguments)).filter(s => !!s);
},
getUserDataPath: function(fileName) {
2017-03-19 00:30:29 +01:00
return this.getDataPath('userdata', fileName).join('/');
2017-02-05 22:01:46 +01:00
},
getTempPath: function(fileName) {
2017-03-19 00:30:29 +01:00
return this.getDataPath('temp', fileName).join('/');
2017-02-05 22:01:46 +01:00
},
getDocumentsPath: function(fileName) {
2017-03-19 00:30:29 +01:00
return this.getDataPath('documents', fileName).join('/');
2017-02-05 22:01:46 +01:00
},
getAppPath: function(fileName) {
2017-03-19 00:30:29 +01:00
return this.getDataPath(fileName).join('/');
2017-02-05 22:01:46 +01:00
},
getWorkDirPath: function(fileName) {
2017-03-19 00:30:29 +01:00
return this.getDataPath(fileName).join('/');
2017-02-05 22:01:46 +01:00
},
2017-04-11 01:34:59 +02:00
joinPath: function(...parts) {
return [...parts].join('/');
},
2017-02-05 22:01:46 +01:00
writeFile: function(path, data, callback) {
const writeFile = fileEntry => {
fileEntry.createWriter(fileWriter => {
fileWriter.onerror = callback;
fileWriter.onwriteend = () => callback();
fileWriter.write(data);
}, callback);
};
window.resolveLocalFileSystemURL(path, writeFile, callback, callback);
2017-02-05 22:01:46 +01:00
},
readFile: function(path, encoding, callback) {
window.resolveLocalFileSystemURL(path, fileEntry => {
fileEntry.file(file => {
const reader = new FileReader();
reader.onerror = callback;
reader.onloadend = () => {
const contents = new Uint8Array(reader.result);
callback(encoding ? String.fromCharCode.apply(null, contents) : contents);
};
reader.readAsArrayBuffer(file);
}, err => callback(undefined, err));
}, err => callback(undefined, err));
},
fileExists: function(path, callback) {
window.resolveLocalFileSystemURL(path, fileEntry => callback(true), err => callback(false)); // eslint-disable-line handle-callback-err
},
deleteFile: function(path, callback) {
window.resolveLocalFileSystemURL(path, fileEntry => {
fileEntry.remove(callback, callback, callback);
}, callback);
},
statFile: function(path, callback) {
window.resolveLocalFileSystemURL(path, fileEntry => {
fileEntry.file(file => {
callback({
ctime: new Date(file.lastModified),
mtime: new Date(file.lastModified)
});
}, err => callback(undefined, err));
}, err => callback(undefined, err));
},
2017-03-19 00:30:29 +01:00
statFileSync: function(path) {
this.req('fs').statSync(path);
},
mkdir: function(dir, callback) {
2017-03-19 00:30:29 +01:00
const basePath = this.getDataPath().join('/');
const createDir = (dirEntry, path, callback) => {
const name = path.shift();
dirEntry.getDirectory(name, { create: true }, dirEntry => {
if (path.length) { // there is more to create
createDir(dirEntry, path, callback);
} else {
callback();
}
}, callback);
};
2017-03-19 00:30:29 +01:00
const localPath = dir.replace(basePath, '').split('/').filter(s => !!s);
if (localPath.length) {
2017-03-19 00:30:29 +01:00
window.resolveLocalFileSystemURL(basePath, dirEntry => {
createDir(dirEntry, localPath, callback);
}, callback);
} else {
callback();
}
2017-02-05 22:01:46 +01:00
},
parsePath: function(fileName) {
const parts = fileName.split('/');
return {
path: fileName,
dir: parts.pop(),
file: parts.join('/')
};
},
createFsWatcher: function(path) {
return null; // not in android with content provider
},
2017-03-19 00:30:29 +01:00
// ensureRunnable: function(path) { },
2017-02-05 22:01:46 +01:00
preventExit: function(e) {
e.returnValue = false;
return false;
},
exit: function() {
2017-03-19 00:30:29 +01:00
this.hideApp();
},
2017-03-29 00:25:16 +02:00
requestExit: function() { /* skip in cordova */ },
2017-02-05 22:01:46 +01:00
requestRestart: function() {
window.location.reload();
},
2017-03-29 00:25:16 +02:00
cancelRestart: function() { /* skip in cordova */ },
2017-02-05 22:01:46 +01:00
setClipboardText: function(text) {
2017-04-10 23:48:29 +02:00
// TODO
2017-02-05 22:01:46 +01:00
},
getClipboardText: function() {
// TODO
},
clearClipboardText: function() {
// TODO
},
minimizeApp: function() {
this.hideApp();
},
canMinimize: function() {
2017-03-29 00:25:16 +02:00
return false;
2017-02-05 22:01:46 +01:00
},
updaterEnabled: function() {
return false;
},
2017-03-19 00:30:29 +01:00
// getMainWindow: function() { },
2017-03-29 00:25:16 +02:00
resolveProxy: function(url, callback) { /* skip in cordova */ },
openWindow: function(opts) { /* skip in cordova */ },
hideApp: function() { /* skip in cordova */ },
2017-02-05 22:01:46 +01:00
isAppFocused: function() {
2017-03-29 00:25:16 +02:00
return false; /* skip in cordova */
2017-02-05 22:01:46 +01:00
},
2017-03-29 00:25:16 +02:00
showMainWindow: function() { /* skip in cordova */ },
2017-03-19 00:30:29 +01:00
// spawn: function(config) { },
2017-04-12 00:06:44 +02:00
openFileChooser: function(callback) {
2017-02-05 22:36:32 +01: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
},
2017-02-05 22:01:46 +01:00
fingerprints: {
2017-02-05 22:36:32 +01:00
config: {
disableBackup: true,
clientId: 'keeweb'
},
2017-04-12 00:06:44 +02:00
register: function(fileId, password, callback) {
FingerprintAuth.isAvailable(result => {
2017-02-05 22:36:32 +01:00
if (!result.isAvailable) {
return;
}
2017-02-05 22:01:46 +01:00
2017-02-05 22:36:32 +01:00
const encryptConfig = _.extend({}, this.config, {
2017-04-12 00:06:44 +02:00
username: fileId,
password: password
2017-02-05 22:36:32 +01:00
});
2017-04-12 00:06:44 +02:00
FingerprintAuth.encrypt(encryptConfig, result => {
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
2017-02-05 22:01:46 +01:00
auth: function(fileInfo, callback) {
2017-02-05 22:36:32 +01:00
if (!fileInfo.has('fingerprint')) {
return;
}
const decryptConfig = _.extend({}, this.config, {
username: fileInfo.id,
token: fileInfo.get('fingerprint')
});
2017-02-05 22:01:46 +01:00
2017-04-12 00:06:44 +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
}
}
};
module.exports = Launcher;