Skip to content

Commit

Permalink
fix: not working on Android 11
Browse files Browse the repository at this point in the history
- update dependencies
- Closes #4
  • Loading branch information
sc85 committed Apr 24, 2021
1 parent 961295f commit 1dfe0f8
Show file tree
Hide file tree
Showing 32 changed files with 6,716 additions and 1,685 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ Install the plugin in your app.
npm install @finanzritter/nativescript-share-file
~~~

### Android FileProvider Setup

On Android, you must add a FileProvider definition and specify available files, which is documented [here](https://developer.android.com/reference/androidx/core/content/FileProvider#ProviderDefinition) or have a look at the demo app (AndroidManifest.xml and file_paths.xml).

## Usage

Info: Shared files should be in the `documents` path.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@
android:label="@string/app_name"
android:theme="@style/AppTheme">

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>

<activity
android:name="com.tns.NativeScriptActivity"
android:label="@string/title_activity_kimera"
Expand Down
18 changes: 18 additions & 0 deletions demo/app/App_Resources/Android/src/main/res/xml/file_paths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="external"
path="." />
<external-files-path
name="external_files"
path="." />
<cache-path
name="cache"
path="." />
<external-cache-path
name="external_cache"
path="." />
<files-path
name="files"
path="." />
</paths>
7,586 changes: 6,325 additions & 1,261 deletions demo/package-lock.json

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
"description": "Demo application for nativescript-share-file",
"dependencies": {
"@finanzritter/nativescript-share-file": "file:../src",
"@nativescript/core": "^7.0.0",
"@nativescript/theme": "^3.0.0",
"@nativescript/webpack": "^3.0.0",
"@nativescript/core": "^7.3.0",
"@nativescript/theme": "^3.0.1",
"@nativescript/webpack": "^3.0.9",
"nativescript-unit-test-runner": "^0.7.0"
},
"devDependencies": {
"@nativescript/android": "7.0.1",
"@nativescript/ios": "7.0.4",
"@nativescript/android": "8.0.0",
"@nativescript/ios": "7.2.0",
"babel-traverse": "6.24.1",
"babel-types": "6.24.1",
"babylon": "6.17.1",
"filewalker": "0.1.3",
"jasmine-core": "^2.6.1",
"karma": "4.1.0",
"karma": "^6.3.2",
"karma-jasmine": "2.0.1",
"karma-nativescript-launcher": "^0.4.0",
"karma-webpack": "3.0.5",
Expand Down
655 changes: 338 additions & 317 deletions src/package-lock.json

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@finanzritter/nativescript-share-file",
"version": "2.0.0",
"version": "2.0.1",
"description": "Send and share files to other apps in NativeScript using native OS APIs.",
"main": "share-file",
"typings": "index.d.ts",
Expand Down Expand Up @@ -57,13 +57,13 @@
"homepage": "https://github.com/finanzritter/nativescript-share-file",
"readmeFilename": "README.md",
"devDependencies": {
"@nativescript/core": "^7.0.0",
"@nativescript/types": "^7.0.0",
"@nativescript/webpack": "^3.0.8",
"prompt": "^1.0.0",
"@nativescript/core": "^7.3.0",
"@nativescript/types": "^7.3.0",
"@nativescript/webpack": "^3.0.9",
"prompt": "^1.1.0",
"rimraf": "^3.0.0",
"tslint": "^6.1.3",
"typescript": "~3.9.7"
"typescript": "^3.9.9"
},
"dependencies": {},
"bootstrapper": "nativescript-plugin-seed"
Expand Down
104 changes: 9 additions & 95 deletions src/share-file.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class ShareFile {
intent.addFlags(android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION);

const uris = new java.util.ArrayList();
const uri = this._getUriForPath(args.path, '/' + this.fileName(args.path), Application.android.context);
const uri = this._getUriForPath(args.path);
uris.add(uri);
const builder = new android.os.StrictMode.VmPolicy.Builder();
android.os.StrictMode.setVmPolicy(builder.build());
Expand All @@ -37,99 +37,13 @@ export class ShareFile {
return filename.split('/').pop();
}

_getUriForPath(path, fileName, ctx) {
if (path.indexOf('file:///') === 0) {
return this._getUriForAbsolutePath(path);
} else if (path.indexOf('file://') === 0) {
return this._getUriForAssetPath(path, fileName, ctx);
} else if (path.indexOf('base64:') === 0) {
return this._getUriForBase64Content(path, fileName, ctx);
} else {
if (path.indexOf(ctx.getPackageName()) > -1) {
return this._getUriForAssetPath(path, fileName, ctx);
} else {
return this._getUriForAbsolutePath(path);
}
}
}

_getUriForAbsolutePath(path) {
const absPath = path.replace('file://', '');
const file = new java.io.File(absPath);
if (!file.exists()) {
console.log('File not found: ' + file.getAbsolutePath());
return null;
} else {
return android.net.Uri.fromFile(file);
}
}

_getUriForAssetPath(path, fileName, ctx) {
path = path.replace('file://', '/');
if (!File.exists(path)) {
console.log('File does not exist: ' + path);
return null;
}
const localFile = File.fromPath(path);
const localFileContents = localFile.readSync(function(e) { console.log(e); });
let cacheFileName = this._writeBytesToFile(ctx, fileName, localFileContents);
if (cacheFileName.indexOf('file://') === -1) {
cacheFileName = 'file://' + cacheFileName;
}
return android.net.Uri.parse(cacheFileName);
}

_getUriForBase64Content(path, fileName, ctx) {
const resData = path.substring(path.indexOf('://') + 3);
let bytes;

try {
bytes = android.util.Base64.decode(resData, 0);
} catch (ex) {
console.log('Invalid Base64 string: ' + resData);
return android.net.Uri.EMPTY;
}

const cacheFileName = this._writeBytesToFile(ctx, fileName, bytes);
return android.net.Uri.parse(cacheFileName);
}

_writeBytesToFile(ctx, fileName, contents) {
const dir = ctx.getExternalCacheDir();

if (dir === null) {
console.log('Missing external cache dir');
return null;
}

const storage = dir.toString() + '/filecomposer';
let cacheFileName = storage + '/' + fileName;
const toFile = File.fromPath(cacheFileName);
toFile.writeSync(contents, function(e) { console.log(e); });

if (cacheFileName.indexOf('file://') === -1) {
cacheFileName = 'file://' + cacheFileName;
}

return cacheFileName;
}

_cleanAttachmentFolder() {
if (Application.android.context) {
const dir = Application.android.context.getExternalCacheDir();
const storage = dir.toString() + '/filecomposer';
const cacheFolder = Folder.fromPath(storage);
cacheFolder.clear();
}
}

toStringArray(arg) {
const arr = java.lang.reflect.Array.newInstance(java.lang.String.class, arg.length);

for (let i = 0; i < arg.length; i++) {
arr[i] = arg[i];
}

return arr;
_getUriForPath(path) {
var file = new java.io.File(path);
return androidx.core.content.FileProvider.getUriForFile(
Application.android.foregroundActivity ||
Application.android.startActivity,
Application.android.packageName + ".fileprovider",
file,
);
}
}

0 comments on commit 1dfe0f8

Please sign in to comment.