diff --git a/apps/toolbox/src/pages/fs-helper.ts b/apps/toolbox/src/pages/fs-helper.ts index 5e100f125..8924d5852 100644 --- a/apps/toolbox/src/pages/fs-helper.ts +++ b/apps/toolbox/src/pages/fs-helper.ts @@ -1,4 +1,4 @@ -import { Page, EventData, Application, File } from '@nativescript/core'; +import { Page, EventData, Application, File, Folder, knownFolders, path, getFileAccess, Utils } from '@nativescript/core'; let page: Page; @@ -71,3 +71,108 @@ function doWork(path: string) { console.error(e); } } + +export function pickFiles() { + if (!global.isAndroid) { + return; + } + const Intent = android.content.Intent; + Application.android.on('activityResult', (args) => { + if (args.requestCode === 1000) { + const intent: android.content.Intent = args.intent; + const clip = intent.getClipData(); + if (clip) { + const count = clip.getItemCount(); + for (let i = 0; i < count; i++) { + const item = clip.getItemAt(i); + readFile(item.getUri().toString()); + } + } else { + readFile(intent.getData().toString()); + } + } + }); + const picker = new Intent(Intent.ACTION_OPEN_DOCUMENT); + picker.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION); + picker.addCategory(Intent.CATEGORY_OPENABLE); + picker.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); + picker.setType('*/*'); + Application.android.foregroundActivity.startActivityForResult(picker, 1000); +} + +export function pickFile() { + if (!global.isAndroid) { + return; + } + Application.android.on('activityResult', (args) => { + if (args.requestCode === 1000) { + const file = args.intent.getData().toString(); + //const file = File.fromPath(args.intent.getData().toString()); + //console.log(file); + readFile(file); + } + }); + const Intent = android.content.Intent; + const picker = new Intent(Intent.ACTION_OPEN_DOCUMENT); + picker.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION); + picker.addCategory(Intent.CATEGORY_OPENABLE); + picker.setType('*/*'); + Application.android.foregroundActivity.startActivityForResult(picker, 1000); +} + +function readFile(selected: string) { + let applicationContext = Utils.android.getApplicationContext(); + const getOrSetHelper: org.nativescript.widgets.FileHelper = org.nativescript.widgets.FileHelper.fromString(applicationContext, selected); + + console.log('==== CONTEXT START ========='); + console.log('applicationContext', applicationContext); + console.log('selected: ', decodeURIComponent(selected)); + console.log('getOrSetHelper: ', getOrSetHelper); + console.log('==== CONTEXT END ========='); + + console.log('==== READ START ========='); + const fileRead = getFileAccess().read(selected); + console.log('# read: ', fileRead); + + const readSync = getFileAccess().readSync(selected); + console.log('# readSync: ', readSync); + + const readText = getFileAccess().readText(selected); + console.log('# readText: ', readText); + + getFileAccess() + .readAsync(selected) + .then( + (readAsyncResolve) => { + console.log('# readAsynccResolve: ', readAsyncResolve); + }, + (readAsyncRejected) => { + console.log('# readAsyncRejected: ', readAsyncRejected); + } + ); + console.log('==== READ END ========='); + + saveFile(selected, readSync); +} + +function saveFile(selected, readSync) { + const folder: Folder = knownFolders.documents(); + + let newPath: string = path.join(folder.path, getFileNameFromContent(selected)); + + File.fromPath(newPath).writeSync(readSync, (error) => { + console.log(error); + }); + + let savedFile = folder.getFile(getFileNameFromContent(selected)); + + console.log('==== SAVE START ========='); + console.log('# saved file: ', savedFile); + console.log('# save size: ', savedFile.size); + console.log('==== SAVE END ========='); +} + +function getFileNameFromContent(content: string) { + const file = getFileAccess().getFile(content); + return decodeURIComponent(file.name).split('/').pop().toLowerCase(); +} diff --git a/apps/toolbox/src/pages/fs-helper.xml b/apps/toolbox/src/pages/fs-helper.xml index 980cae506..000d09a5d 100644 --- a/apps/toolbox/src/pages/fs-helper.xml +++ b/apps/toolbox/src/pages/fs-helper.xml @@ -2,5 +2,7 @@