Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handled opening urls #28

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions android/src/main/java/com/example/pdfviewerplugin/PdfViewer.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.example.pdfviewerplugin;

import com.github.barteksc.pdfviewer.PDFView;
import com.github.barteksc.pdfviewer.link.LinkHandler;
import com.github.barteksc.pdfviewer.model.LinkTapEvent;

import android.content.Intent;
import android.content.Context;
import android.net.Uri;
import android.view.View;

import java.io.File;
Expand All @@ -15,22 +19,25 @@
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.platform.PlatformView;

import static androidx.core.content.ContextCompat.startActivity;

public class PdfViewer implements PlatformView, MethodCallHandler {
private PDFView pdfView;
private String filePath;
LinkHandler linkHandler;

PdfViewer(Context context, BinaryMessenger messenger, int id, Map<String, Object> args) {
MethodChannel methodChannel = new MethodChannel(messenger, "pdf_viewer_plugin_" + id);
methodChannel.setMethodCallHandler(this);

pdfView = new PDFView(context, null);

linkHandler = new MyLinktHandler(context);
if (!args.containsKey("filePath")) {
return;
}

filePath = (String)args.get("filePath");
filePath = (String) args.get("filePath");
loadPdfView();

}

@Override
Expand All @@ -48,6 +55,7 @@ private void loadPdfView() {
.swipeHorizontal(false)
.enableDoubletap(true)
.defaultPage(0)
.linkHandler(linkHandler).enableAntialiasing(false)
.load();
}

Expand All @@ -57,5 +65,24 @@ public View getView() {
}

@Override
public void dispose() {}
public void dispose() {
}
}

class MyLinktHandler implements LinkHandler {
Context context;

public MyLinktHandler(Context context) {
this.context = context;
}

@Override
public void handleLinkEvent(LinkTapEvent event) {
String url = event.getLink().getUri();
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(context, i, null);
}
}
59 changes: 54 additions & 5 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
archive:
dependency: transitive
description:
name: archive
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.11"
args:
dependency: transitive
description:
name: args
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.2"
async:
dependency: transitive
description:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.3.0"
version: "2.4.0"
boolean_selector:
dependency: transitive
description:
Expand All @@ -29,6 +43,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.14.11"
convert:
dependency: transitive
description:
name: convert
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
crypto:
dependency: transitive
description:
name: crypto
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.3"
cupertino_icons:
dependency: "direct main"
description:
Expand Down Expand Up @@ -60,20 +88,27 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.3"
image:
dependency: transitive
description:
name: image
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.4"
matcher:
dependency: transitive
description:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.5"
version: "0.12.6"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.7"
version: "1.1.8"
path:
dependency: transitive
description:
Expand Down Expand Up @@ -102,6 +137,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0+1"
petitparser:
dependency: transitive
description:
name: petitparser
url: "https://pub.dartlang.org"
source: hosted
version: "2.4.0"
quiver:
dependency: transitive
description:
Expand Down Expand Up @@ -155,7 +197,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.5"
version: "0.2.11"
typed_data:
dependency: transitive
description:
Expand All @@ -170,6 +212,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.8"
xml:
dependency: transitive
description:
name: xml
url: "https://pub.dartlang.org"
source: hosted
version: "3.5.0"
sdks:
dart: ">=2.2.2 <3.0.0"
dart: ">=2.4.0 <3.0.0"
flutter: ">=0.1.4 <2.0.0"
17 changes: 10 additions & 7 deletions lib/pdf_viewer_plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ class _PdfViewerState extends State<PdfViewer> {
@override
Widget build(BuildContext context) {
if (defaultTargetPlatform == TargetPlatform.android) {
return AndroidView(
viewType: 'pdf_viewer_plugin',
creationParams: <String, dynamic>{
'filePath': widget.filePath,
},
creationParamsCodec: StandardMessageCodec(),
onPlatformViewCreated: _onPlatformViewCreated,
return Container(
color: Colors.white,
child: AndroidView(
viewType: 'pdf_viewer_plugin',
creationParams: <String, dynamic>{
'filePath': widget.filePath,
},
creationParamsCodec: StandardMessageCodec(),
onPlatformViewCreated: _onPlatformViewCreated,
),
);
} else if (defaultTargetPlatform == TargetPlatform.iOS) {
return UiKitView(
Expand Down
59 changes: 54 additions & 5 deletions pubspec.lock
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
archive:
dependency: transitive
description:
name: archive
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.11"
args:
dependency: transitive
description:
name: args
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.2"
async:
dependency: transitive
description:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.3.0"
version: "2.4.0"
boolean_selector:
dependency: transitive
description:
Expand All @@ -29,6 +43,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.14.11"
convert:
dependency: transitive
description:
name: convert
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
crypto:
dependency: transitive
description:
name: crypto
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.3"
flutter:
dependency: "direct main"
description: flutter
Expand All @@ -39,20 +67,27 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
image:
dependency: transitive
description:
name: image
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.4"
matcher:
dependency: transitive
description:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.5"
version: "0.12.6"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.7"
version: "1.1.8"
path:
dependency: transitive
description:
Expand All @@ -67,6 +102,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0+1"
petitparser:
dependency: transitive
description:
name: petitparser
url: "https://pub.dartlang.org"
source: hosted
version: "2.4.0"
quiver:
dependency: transitive
description:
Expand Down Expand Up @@ -120,7 +162,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.5"
version: "0.2.11"
typed_data:
dependency: transitive
description:
Expand All @@ -135,5 +177,12 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.8"
xml:
dependency: transitive
description:
name: xml
url: "https://pub.dartlang.org"
source: hosted
version: "3.5.0"
sdks:
dart: ">=2.2.2 <3.0.0"
dart: ">=2.4.0 <3.0.0"