mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-18 13:51:27 +08:00
refactor: circular deps part 8
This commit is contained in:
@ -1,21 +1,11 @@
|
||||
import * as textModule from '../text';
|
||||
import { Application } from '../application';
|
||||
import { getFileExtension } from '../utils';
|
||||
import { getFileExtension, android as androidUtils } from '../utils';
|
||||
import { SDK_VERSION } from '../utils/constants';
|
||||
|
||||
import type { IFileSystemAccess } from './file-system-access';
|
||||
|
||||
let applicationContext: android.content.Context;
|
||||
function getApplicationContext() {
|
||||
if (!applicationContext) {
|
||||
applicationContext = Application.android.getNativeApplication().getApplicationContext();
|
||||
}
|
||||
|
||||
return applicationContext;
|
||||
}
|
||||
|
||||
function getOrSetHelper(path: string): org.nativescript.widgets.FileHelper {
|
||||
return org.nativescript.widgets.FileHelper.fromString(getApplicationContext(), path);
|
||||
return org.nativescript.widgets.FileHelper.fromString(androidUtils.getApplicationContext(), path);
|
||||
}
|
||||
|
||||
function isContentUri(path: string): boolean {
|
||||
@ -222,30 +212,30 @@ export class FileSystemAccess implements IFileSystemAccess {
|
||||
}
|
||||
|
||||
public getDocumentsFolderPath(): string {
|
||||
const dir = getApplicationContext().getFilesDir();
|
||||
const dir = androidUtils.getApplicationContext().getFilesDir();
|
||||
|
||||
return dir.getAbsolutePath();
|
||||
}
|
||||
public getExternalDocumentsFolderPath(): string {
|
||||
const dirs = getApplicationContext().getExternalFilesDirs(null);
|
||||
const dirs = androidUtils.getApplicationContext().getExternalFilesDirs(null);
|
||||
let dir;
|
||||
if (dirs && dirs.length > 1) {
|
||||
dir = dirs[dirs.length - 1];
|
||||
}
|
||||
if (!dir) {
|
||||
dir = getApplicationContext().getExternalFilesDir(null);
|
||||
dir = androidUtils.getApplicationContext().getExternalFilesDir(null);
|
||||
}
|
||||
return dir.getAbsolutePath();
|
||||
}
|
||||
|
||||
public getLogicalRootPath(): string {
|
||||
const dir = getApplicationContext().getFilesDir();
|
||||
const dir = androidUtils.getApplicationContext().getFilesDir();
|
||||
|
||||
return dir.getCanonicalPath();
|
||||
}
|
||||
|
||||
public getTempFolderPath(): string {
|
||||
const dir = getApplicationContext().getCacheDir();
|
||||
const dir = androidUtils.getApplicationContext().getCacheDir();
|
||||
|
||||
return dir.getAbsolutePath();
|
||||
}
|
||||
@ -258,7 +248,7 @@ export class FileSystemAccess implements IFileSystemAccess {
|
||||
|
||||
public copySync(src: string, dest: string, onError?: (error: any) => any) {
|
||||
try {
|
||||
return org.nativescript.widgets.Async.File.copySync(src, dest, getApplicationContext());
|
||||
return org.nativescript.widgets.Async.File.copySync(src, dest, androidUtils.getApplicationContext());
|
||||
} catch (error) {
|
||||
if (onError) {
|
||||
onError(error);
|
||||
@ -282,7 +272,7 @@ export class FileSystemAccess implements IFileSystemAccess {
|
||||
reject(err);
|
||||
},
|
||||
}),
|
||||
getApplicationContext(),
|
||||
androidUtils.getApplicationContext(),
|
||||
);
|
||||
} catch (ex) {
|
||||
reject(ex);
|
||||
@ -926,7 +916,7 @@ export class FileSystemAccess29 extends FileSystemAccess {
|
||||
}
|
||||
fileExists(path: string): boolean {
|
||||
if (isContentUri(path)) {
|
||||
return org.nativescript.widgets.FileHelper.exists(applicationContext, path);
|
||||
return org.nativescript.widgets.FileHelper.exists(androidUtils.getApplicationContext(), path);
|
||||
}
|
||||
return super.fileExists(path);
|
||||
}
|
||||
@ -939,7 +929,7 @@ export class FileSystemAccess29 extends FileSystemAccess {
|
||||
deleteFile(path: string, onError?: (error: any) => any) {
|
||||
if (isContentUri(path)) {
|
||||
try {
|
||||
getOrSetHelper(path).delete(applicationContext);
|
||||
getOrSetHelper(path).delete(androidUtils.getApplicationContext());
|
||||
} catch (e) {
|
||||
onError?.(e);
|
||||
}
|
||||
@ -968,7 +958,7 @@ export class FileSystemAccess29 extends FileSystemAccess {
|
||||
},
|
||||
});
|
||||
}
|
||||
getOrSetHelper(path).renameSync(applicationContext, newPath, callback);
|
||||
getOrSetHelper(path).renameSync(androidUtils.getApplicationContext(), newPath, callback);
|
||||
} else {
|
||||
super.rename(path, newPath, onError);
|
||||
}
|
||||
@ -977,7 +967,7 @@ export class FileSystemAccess29 extends FileSystemAccess {
|
||||
public renameAsync(path: string, newPath: string): Promise<any> {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
getOrSetHelper(path).renameSync(
|
||||
applicationContext,
|
||||
androidUtils.getApplicationContext(),
|
||||
newPath,
|
||||
new org.nativescript.widgets.FileHelper.Callback({
|
||||
onSuccess(result) {
|
||||
@ -1010,7 +1000,7 @@ export class FileSystemAccess29 extends FileSystemAccess {
|
||||
if (isContentUri(path)) {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
getOrSetHelper(path).appendBuffer(
|
||||
applicationContext,
|
||||
androidUtils.getApplicationContext(),
|
||||
FileSystemAccess.getBuffer(content),
|
||||
new org.nativescript.widgets.FileHelper.Callback({
|
||||
onSuccess(result) {
|
||||
@ -1037,7 +1027,7 @@ export class FileSystemAccess29 extends FileSystemAccess {
|
||||
},
|
||||
});
|
||||
}
|
||||
getOrSetHelper(path).appendSync(applicationContext, FileSystemAccess.getBuffer(content), callback);
|
||||
getOrSetHelper(path).appendSync(androidUtils.getApplicationContext(), FileSystemAccess.getBuffer(content), callback);
|
||||
} else {
|
||||
super.appendSync(path, content, onError);
|
||||
}
|
||||
@ -1049,7 +1039,7 @@ export class FileSystemAccess29 extends FileSystemAccess {
|
||||
if (isContentUri(path)) {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
getOrSetHelper(path).append(
|
||||
applicationContext,
|
||||
androidUtils.getApplicationContext(),
|
||||
content,
|
||||
new org.nativescript.widgets.FileHelper.Callback({
|
||||
onSuccess(result) {
|
||||
@ -1076,7 +1066,7 @@ export class FileSystemAccess29 extends FileSystemAccess {
|
||||
},
|
||||
});
|
||||
}
|
||||
getOrSetHelper(path).appendSync(applicationContext, content, callback);
|
||||
getOrSetHelper(path).appendSync(androidUtils.getApplicationContext(), content, callback);
|
||||
} else {
|
||||
super.appendSync(path, content, onError);
|
||||
}
|
||||
@ -1088,7 +1078,7 @@ export class FileSystemAccess29 extends FileSystemAccess {
|
||||
if (isContentUri(path)) {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
getOrSetHelper(path).appendText(
|
||||
applicationContext,
|
||||
androidUtils.getApplicationContext(),
|
||||
content,
|
||||
encoding ?? null,
|
||||
new org.nativescript.widgets.FileHelper.Callback({
|
||||
@ -1116,7 +1106,7 @@ export class FileSystemAccess29 extends FileSystemAccess {
|
||||
},
|
||||
});
|
||||
}
|
||||
getOrSetHelper(path).appendTextSync(applicationContext, content, encoding ?? null, callback);
|
||||
getOrSetHelper(path).appendTextSync(androidUtils.getApplicationContext(), content, encoding ?? null, callback);
|
||||
} else {
|
||||
super.appendTextSync(path, content, onError);
|
||||
}
|
||||
@ -1128,7 +1118,7 @@ export class FileSystemAccess29 extends FileSystemAccess {
|
||||
if (isContentUri(path)) {
|
||||
return new Promise((resolve, reject) => {
|
||||
getOrSetHelper(path).readText(
|
||||
applicationContext,
|
||||
androidUtils.getApplicationContext(),
|
||||
encoding ?? null,
|
||||
new org.nativescript.widgets.FileHelper.Callback({
|
||||
onSuccess(result) {
|
||||
@ -1154,7 +1144,7 @@ export class FileSystemAccess29 extends FileSystemAccess {
|
||||
},
|
||||
});
|
||||
}
|
||||
return getOrSetHelper(path).readTextSync(applicationContext, encoding ?? null, callback);
|
||||
return getOrSetHelper(path).readTextSync(androidUtils.getApplicationContext(), encoding ?? null, callback);
|
||||
} else {
|
||||
return super.readTextSync(path, onError, encoding);
|
||||
}
|
||||
@ -1166,7 +1156,7 @@ export class FileSystemAccess29 extends FileSystemAccess {
|
||||
if (isContentUri(path)) {
|
||||
return new Promise((resolve, reject) => {
|
||||
getOrSetHelper(path).readBuffer(
|
||||
applicationContext,
|
||||
androidUtils.getApplicationContext(),
|
||||
new org.nativescript.widgets.FileHelper.Callback({
|
||||
onSuccess(result) {
|
||||
resolve(result);
|
||||
@ -1192,7 +1182,7 @@ export class FileSystemAccess29 extends FileSystemAccess {
|
||||
},
|
||||
});
|
||||
}
|
||||
const ret = getOrSetHelper(path).readBufferSync(applicationContext, callback);
|
||||
const ret = getOrSetHelper(path).readBufferSync(androidUtils.getApplicationContext(), callback);
|
||||
if (ret) {
|
||||
return null;
|
||||
}
|
||||
@ -1207,7 +1197,7 @@ export class FileSystemAccess29 extends FileSystemAccess {
|
||||
if (isContentUri(path)) {
|
||||
return new Promise((resolve, reject) => {
|
||||
getOrSetHelper(path).read(
|
||||
applicationContext,
|
||||
androidUtils.getApplicationContext(),
|
||||
new org.nativescript.widgets.FileHelper.Callback({
|
||||
onSuccess(result) {
|
||||
resolve(result);
|
||||
@ -1233,7 +1223,7 @@ export class FileSystemAccess29 extends FileSystemAccess {
|
||||
},
|
||||
});
|
||||
}
|
||||
return getOrSetHelper(path).readSync(applicationContext, callback);
|
||||
return getOrSetHelper(path).readSync(androidUtils.getApplicationContext(), callback);
|
||||
}
|
||||
return super.readSync(path, onError);
|
||||
}
|
||||
@ -1244,7 +1234,7 @@ export class FileSystemAccess29 extends FileSystemAccess {
|
||||
if (isContentUri(path)) {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
getOrSetHelper(path).writeText(
|
||||
applicationContext,
|
||||
androidUtils.getApplicationContext(),
|
||||
content,
|
||||
encoding ?? null,
|
||||
new org.nativescript.widgets.FileHelper.Callback({
|
||||
@ -1272,7 +1262,7 @@ export class FileSystemAccess29 extends FileSystemAccess {
|
||||
},
|
||||
});
|
||||
}
|
||||
getOrSetHelper(path).writeTextSync(applicationContext, content, encoding ?? null, callback);
|
||||
getOrSetHelper(path).writeTextSync(androidUtils.getApplicationContext(), content, encoding ?? null, callback);
|
||||
} else {
|
||||
super.writeTextSync(path, content, onError);
|
||||
}
|
||||
@ -1284,7 +1274,7 @@ export class FileSystemAccess29 extends FileSystemAccess {
|
||||
if (isContentUri(path)) {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
getOrSetHelper(path).writeBuffer(
|
||||
applicationContext,
|
||||
androidUtils.getApplicationContext(),
|
||||
FileSystemAccess.getBuffer(content),
|
||||
new org.nativescript.widgets.FileHelper.Callback({
|
||||
onSuccess(result) {
|
||||
@ -1311,7 +1301,7 @@ export class FileSystemAccess29 extends FileSystemAccess {
|
||||
},
|
||||
});
|
||||
}
|
||||
getOrSetHelper(path).writeSync(applicationContext, FileSystemAccess.getBuffer(content), callback);
|
||||
getOrSetHelper(path).writeSync(androidUtils.getApplicationContext(), FileSystemAccess.getBuffer(content), callback);
|
||||
} else {
|
||||
super.writeSync(path, content, onError);
|
||||
}
|
||||
@ -1323,7 +1313,7 @@ export class FileSystemAccess29 extends FileSystemAccess {
|
||||
if (isContentUri(path)) {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
getOrSetHelper(path).write(
|
||||
applicationContext,
|
||||
androidUtils.getApplicationContext(),
|
||||
content,
|
||||
new org.nativescript.widgets.FileHelper.Callback({
|
||||
onSuccess(result) {
|
||||
@ -1350,7 +1340,7 @@ export class FileSystemAccess29 extends FileSystemAccess {
|
||||
},
|
||||
});
|
||||
}
|
||||
getOrSetHelper(path).writeSync(applicationContext, content, callback);
|
||||
getOrSetHelper(path).writeSync(androidUtils.getApplicationContext(), content, callback);
|
||||
} else {
|
||||
super.writeSync(path, content, onError);
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { IFileSystemAccess, FileSystemAccess, FileSystemAccess29 } from './file-system-access';
|
||||
import { SDK_VERSION } from '../utils';
|
||||
import { Application } from '../application';
|
||||
import { SDK_VERSION } from '../utils/constants';
|
||||
import { android as androidUtils } from '../utils';
|
||||
|
||||
// The FileSystemAccess implementation, used through all the APIs.
|
||||
let fileAccess: IFileSystemAccess;
|
||||
@ -182,15 +182,6 @@ export class FileSystemEntity {
|
||||
}
|
||||
}
|
||||
|
||||
let applicationContext;
|
||||
function getApplicationContext() {
|
||||
if (!applicationContext) {
|
||||
applicationContext = Application.android.getNativeApplication().getApplicationContext();
|
||||
}
|
||||
|
||||
return applicationContext;
|
||||
}
|
||||
|
||||
export enum AndroidDirectory {
|
||||
ALARMS = 'alarms',
|
||||
AUDIOBOOKS = 'audiobooks',
|
||||
@ -279,7 +270,7 @@ class Android {
|
||||
throw new Error(`createFile is available on Android only!`);
|
||||
}
|
||||
|
||||
const context = getApplicationContext() as android.content.Context;
|
||||
const context = androidUtils.getApplicationContext() as android.content.Context;
|
||||
|
||||
const meta = new android.content.ContentValues();
|
||||
meta.put(android.provider.MediaStore.MediaColumns.DISPLAY_NAME, options.name);
|
||||
@ -332,7 +323,7 @@ export class File extends FileSystemEntity {
|
||||
// falls back to creating a temp file without a known extension.
|
||||
if (!fileInfo) {
|
||||
const tempFile = `${knownFolders.temp().path}/${java.util.UUID.randomUUID().toString()}`;
|
||||
org.nativescript.widgets.Async.File.copySync(path, tempFile, getApplicationContext());
|
||||
org.nativescript.widgets.Async.File.copySync(path, tempFile, androidUtils.getApplicationContext());
|
||||
path = tempFile;
|
||||
} else {
|
||||
const ext = fileInfo.extension;
|
||||
|
Reference in New Issue
Block a user