-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Complete saving after Cordova Activity relaunch
- Loading branch information
Showing
6 changed files
with
129 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
let moduleMapper = require("cordova/modulemapper"); | ||
let indexedDB = moduleMapper.getOriginalSymbol(window, "indexedDB") || window.indexedDB; | ||
|
||
function openDB() { | ||
if (!indexedDB) { | ||
return Promise.reject("indexedDB is not supported"); | ||
} | ||
if (openDB._db) { | ||
return Promise.resolve(openDB._db); | ||
} | ||
return new Promise((resolve, reject) => { | ||
let openRequest = indexedDB.open("CordovaPluginSaveDialog"); | ||
openRequest.onupgradeneeded = () => { | ||
openRequest.result.createObjectStore("blobs"); | ||
}; | ||
openRequest.onsuccess = () => { | ||
openDB._db = openRequest.result; | ||
resolve(openDB._db); | ||
}; | ||
openRequest.onerror = () => { | ||
openDB._db = null; | ||
reject(openRequest.error); | ||
}; | ||
}); | ||
} | ||
|
||
function writeBlob(blob) { | ||
return openDB().then(db => new Promise((resolve, reject) => { | ||
let objectStore = db.transaction(["blobs"], "readwrite").objectStore("blobs"); | ||
let putRequest = blob ? objectStore.put(blob, 0) : objectStore.clear(); | ||
putRequest.onsuccess = () => { | ||
resolve(); | ||
}; | ||
putRequest.onerror = () => { | ||
reject(putRequest.error); | ||
}; | ||
})); | ||
} | ||
|
||
function getBlob() { | ||
return openDB().then(db => new Promise((resolve, reject) => { | ||
let objectStore = db.transaction(["blobs"], "readonly").objectStore("blobs"); | ||
let getRequest = objectStore.get(0); | ||
getRequest.onsuccess = () => { | ||
resolve(getRequest.result); | ||
}; | ||
getRequest.onerror = () => { | ||
reject(getRequest.error); | ||
}; | ||
})); | ||
} | ||
|
||
function warn(reason) { | ||
console.warn("[SaveDialog]", reason); | ||
} | ||
|
||
module.exports = { | ||
keep: blob => writeBlob(blob).catch(warn), | ||
get: () => getBlob().catch(warn), | ||
clear: () => writeBlob(null).catch(warn) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
let exec = require("cordova/exec"); | ||
let {keep: keepBlob, get: getBlob, clear: clearBlob} = require("./BlobKeeper"); | ||
let moduleMapper = require("cordova/modulemapper"); | ||
let FileReader = moduleMapper.getOriginalSymbol(window, "FileReader") || window.FileReader; | ||
|
||
let locateFile = (type, name) => new Promise((resolve, reject) => { | ||
exec(resolve, reject, "SaveDialog", "locateFile", [type || "application/octet-stream", name]); | ||
}); | ||
|
||
let saveFile = (uri, blob) => new Promise((resolve, reject) => { | ||
let reader = new FileReader(); | ||
reader.onload = () => { | ||
exec(resolve, reject, "SaveDialog", "saveFile", [uri, reader.result]); | ||
}; | ||
reader.onerror = () => { | ||
reject(reader.error); | ||
}; | ||
reader.onabort = () => { | ||
reject("Blob reading has been aborted"); | ||
}; | ||
reader.readAsArrayBuffer(blob); | ||
}); | ||
|
||
module.exports = { | ||
saveFile(blob, name = "") { | ||
if (window.cordova.platformId !== "android") { | ||
return Promise.reject("Unsupported platform"); | ||
} | ||
return keepBlob(blob) // see the “resume” event handler below | ||
.then(() => locateFile(blob.type, name)) | ||
.then(uri => saveFile(uri, blob)) | ||
.then(() => { | ||
clearBlob(); | ||
}) | ||
.catch(reason => { | ||
clearBlob(); | ||
return Promise.reject(reason); | ||
}); | ||
} | ||
}; | ||
|
||
// If Android OS has destroyed the Cordova Activity in background, try to complete the Save operation | ||
// using the URI passed in the payload of the “resume” event and the blob stored by the BlobKeeper. | ||
// https://cordova.apache.org/docs/en/10.x/guide/platforms/android/plugin.html#launching-other-activities | ||
document.addEventListener("resume", ({pendingResult = {}}) => { | ||
if (pendingResult.pluginServiceName !== "SaveDialog") { | ||
return; | ||
} | ||
if (pendingResult.pluginStatus !== "OK" || !pendingResult.result) { | ||
clearBlob(); | ||
return; | ||
} | ||
getBlob().then(blob => { | ||
if (blob instanceof Blob) { | ||
saveFile(pendingResult.result, blob).catch(reason => { | ||
console.warn("[SaveDialog]", reason); | ||
}); | ||
} | ||
clearBlob(); | ||
}); | ||
}, false); |
This file was deleted.
Oops, something went wrong.