Skip to content

Commit

Permalink
fix(android): new param forceSAF to always use SAF (and remove the …
Browse files Browse the repository at this point in the history
…need for storage permission)
  • Loading branch information
farfromrefug committed Jun 14, 2024
1 parent b94ef54 commit 9a23f04
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
25 changes: 16 additions & 9 deletions src/ui-document-picker/index.android.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { request } from '@nativescript-community/perms';
import { AndroidActivityResultEventData, AndroidApplication, Application, File } from '@nativescript/core';
import { AndroidActivityResultEventData, Application, File, Utils } from '@nativescript/core';
import { SDK_VERSION } from '@nativescript/core/utils';
import { CommonPickerOptions, CommonPickerPermissionsOptions, FilePickerOptions, FolderPickerOptions, SaveFileOptions } from './index.common';
import {
CommonPickerOptions,
CommonPickerPermissionsOptions,
FilePickerOptions,
FolderPickerOptions,
SaveFileOptions
} from './index.common';

export { FilePickerOptions };
let Intent: typeof android.content.Intent;
Expand Down Expand Up @@ -60,7 +66,7 @@ function prepareIntent(intent: android.content.Intent, options: CommonPickerOpti
}

export function openFilePicker(params: FilePickerOptions = {}) {
const context = Application.android.startActivity;
const context = Utils.android.getApplicationContext();
const FILE_CODE = 1231;

if (!Intent) {
Expand Down Expand Up @@ -104,7 +110,8 @@ export function openFilePicker(params: FilePickerOptions = {}) {
const uri: android.net.Uri = result.intent.getData();
if (uri) {
return {
files: [SDK_VERSION >= 30 ? uri.toString() : FilePath.getPath(context, uri)],
files: [SDK_VERSION >= 30 || params.forceSAF ? uri.toString() : FilePath.getPath(context, uri)],
// files: [uri.toString()],
android: uri
};
}
Expand All @@ -118,7 +125,7 @@ export function openFilePicker(params: FilePickerOptions = {}) {
if (item) {
const uri: android.net.Uri = item.getUri();
if (uri) {
if (SDK_VERSION >= 30) {
if (SDK_VERSION >= 30 || params.forceSAF) {
files.push(uri.toString());
} else {
files.push(FilePath.getPath(context, uri));
Expand Down Expand Up @@ -170,7 +177,7 @@ function updatePersistableUris(
}
}
export function pickFolder(params: FolderPickerOptions = {}) {
const context = Application.android.startActivity;
const context = Utils.android.getApplicationContext();
const FOLDER_CODE = 1232;
if (!Intent) {
Intent = android.content.Intent;
Expand Down Expand Up @@ -205,7 +212,7 @@ export function pickFolder(params: FolderPickerOptions = {}) {
if (item) {
const uri: android.net.Uri = item.getUri();
if (uri) {
if (SDK_VERSION >= 30) {
if (SDK_VERSION >= 30 || params.forceSAF) {
folders.push(uri.toString());
} else {
folders.push(FilePath.getPath(context, uri));
Expand Down Expand Up @@ -237,7 +244,7 @@ export async function saveFile(params: SaveFileOptions) {
// } else {
// await tempFile.write(params.data);
// }
const context = Application.android.startActivity;
const context = Utils.android.getApplicationContext();
const FILE_CODE = 1233;

if (!Intent) {
Expand All @@ -259,7 +266,7 @@ export async function saveFile(params: SaveFileOptions) {
if (result.resultCode === android.app.Activity.RESULT_OK) {
if (result.intent != null) {
const uri: android.net.Uri = result.intent.getData();
const filePath = SDK_VERSION >= 30 ? uri.toString() : FilePath.getPath(context, uri);
const filePath = SDK_VERSION >= 30 || params.forceSAF ? uri.toString() : FilePath.getPath(context, uri);
if (typeof params.data === 'string') {
return File.fromPath(filePath)
.writeText(params.data)
Expand Down
3 changes: 2 additions & 1 deletion src/ui-document-picker/index.common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface CommonPickerOptions {
multipleSelection?: boolean;
permissions?: CommonPickerPermissionsOptions;
cloud?: boolean;
forceSAF?: boolean;
}
export interface FilePickerOptions extends CommonPickerOptions {
extensions?: string[]; // will be transformed to mimeTypes on android
Expand All @@ -17,7 +18,7 @@ export interface FilePickerOptions extends CommonPickerOptions {
}
export interface FolderPickerOptions extends CommonPickerOptions {}

export interface SaveFileOptions extends Pick<CommonPickerOptions, 'permissions'> {
export interface SaveFileOptions extends Pick<CommonPickerOptions, 'forceSAF'>, Pick<CommonPickerOptions, 'permissions'> {
name: string;
data;
mimeType?: string;
Expand Down

0 comments on commit 9a23f04

Please sign in to comment.