Skip to content

Commit

Permalink
Android implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
dragermrb committed Aug 30, 2021
1 parent f84ae35 commit 71b015c
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 24 deletions.
36 changes: 33 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,39 @@ npx cap sync

## API

<docgen-index></docgen-index>
<docgen-index>

* [`open(...)`](#open)
* [Interfaces](#interfaces)

</docgen-index>

<docgen-api>
<!-- run docgen to generate docs from the source -->
<!-- More info: https://github.com/ionic-team/capacitor-docgen -->
<!--Update the source file JSDoc comments and rerun docgen to update the docs below-->

### open(...)

```typescript
open(options: FileOpenerOptions) => any
```

| Param | Type |
| ------------- | --------------------------------------------------------------- |
| **`options`** | <code><a href="#fileopeneroptions">FileOpenerOptions</a></code> |

**Returns:** <code>any</code>

--------------------


### Interfaces


#### FileOpenerOptions

| Prop | Type | Description |
| ---------- | ------------------- | -------------- |
| **`path`** | <code>string</code> | Path to file |
| **`mime`** | <code>string</code> | Mime to select |

</docgen-api>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,22 +1,77 @@
package com.whiteguru.capacitor.plugin.fileopener;

import com.getcapacitor.JSObject;
import android.content.Intent;
import android.net.Uri;
import android.webkit.MimeTypeMap;

import androidx.activity.result.ActivityResult;
import androidx.core.content.FileProvider;

import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.ActivityCallback;
import com.getcapacitor.annotation.CapacitorPlugin;

import java.io.File;

@CapacitorPlugin(name = "FileOpener")
public class FileOpenerPlugin extends Plugin {

private FileOpener implementation = new FileOpener();

@PluginMethod
public void echo(PluginCall call) {
String value = call.getString("value");
public void open(PluginCall call) {
String path = call.getString("path", "");
String mime = call.getString("mime", "");

File file;
Uri u = Uri.parse(path);
if (u.getScheme() == null || u.getScheme().equals("file")) {
file = new File(u.getPath());
} else {
file = new File(path);
}

Uri fileUri = FileProvider.getUriForFile(
getActivity(),
getContext().getPackageName() + ".fileprovider",
file
);

if (mime == null || mime.trim().equals("")) {
mime = getMimeType(file.getName());
}

Intent openFileIntent = new Intent(Intent.ACTION_VIEW);
openFileIntent.setDataAndType(fileUri, mime);
openFileIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

if (openFileIntent.resolveActivity(getContext().getPackageManager()) != null) {
startActivityForResult(call, openFileIntent, "openFileResult");
} else {
call.reject("No default apps for open file");
}
}

@ActivityCallback
private void openFileResult(PluginCall call, ActivityResult result) {
if (call == null) {
return;
}

call.resolve();
}

private String getMimeType(String url) {
String mimeType = "*/*";
int extensionIndex = url.lastIndexOf('.');

if (extensionIndex > 0) {
String extMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(url.substring(extensionIndex + 1));
if (extMimeType != null) {
mimeType = extMimeType;
}
}

JSObject ret = new JSObject();
ret.put("value", implementation.echo(value));
call.resolve(ret);
return mimeType;
}
}
13 changes: 12 additions & 1 deletion src/definitions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
export interface FileOpenerPlugin {
echo(options: { value: string }): Promise<{ value: string }>;
open(options: FileOpenerOptions): Promise<void>;
}

export interface FileOpenerOptions {
/**
* Path to file
*/
path: string;
/**
* Mime to select
*/
mime: string;
}
9 changes: 5 additions & 4 deletions src/web.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { WebPlugin } from '@capacitor/core';

import type { FileOpenerPlugin } from './definitions';
import type { FileOpenerOptions, FileOpenerPlugin } from './definitions';

export class FileOpenerWeb extends WebPlugin implements FileOpenerPlugin {
async echo(options: { value: string }): Promise<{ value: string }> {
console.log('ECHO', options);
return options;
open(options: FileOpenerOptions): Promise<void> {
console.log('open', options);

throw this.unimplemented('Not implemented on web.');
}
}

0 comments on commit 71b015c

Please sign in to comment.