Skip to content

Commit

Permalink
Merge pull request #82 from hisaichi5518/handle-download
Browse files Browse the repository at this point in the history
set DownloadListener
  • Loading branch information
hisaichi5518 committed Sep 20, 2020
2 parents 44ee711 + c42644c commit 0bce7c7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
Expand Up @@ -2,10 +2,13 @@ package com.hisaichi5518.native_webview

import android.annotation.SuppressLint
import android.content.Context
import android.os.IBinder
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.*
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat.startActivity
import io.flutter.plugin.common.MethodChannel

class NativeWebView(context: Context, channel: MethodChannel, options: WebViewOptions) : InputAwareWebView(context, null as View?) {
Expand All @@ -22,6 +25,17 @@ class NativeWebView(context: Context, channel: MethodChannel, options: WebViewOp
settings.useWideViewPort = true
settings.loadWithOverviewMode = true
addJavascriptInterface(JavascriptHandler(channel), NativeWebChromeClient.JAVASCRIPT_BRIDGE_NAME)

setDownloadListener { url, _, _, mimetype, _ ->
val intent = Intent(Intent.ACTION_VIEW)
intent.type = mimetype
intent.data = Uri.parse(url)

val activity = Locator.activity ?: return@setDownloadListener
if (intent.resolveActivity(activity.packageManager) != null) {
startActivity(activity, intent, Bundle())
}
}
}

fun load(initialData: Map<String, String>?, initialFile: String?, initialURL: String, initialHeaders: Map<String, String>?) {
Expand Down
2 changes: 2 additions & 0 deletions example/lib/main.dart
Expand Up @@ -6,6 +6,7 @@ import 'package:native_webview_example/screens/initial_url_screen.dart';
import 'package:native_webview_example/screens/multiple_webviews_screen.dart';
import 'package:native_webview_example/screens/on_js_prompt_screen.dart';
import 'package:native_webview_example/screens/open_dropdown_screen.dart';
import 'package:native_webview_example/screens/pdf_screen.dart';
import 'package:native_webview_example/screens/target_blank_screen.dart';

void main() {
Expand All @@ -27,6 +28,7 @@ class _MyAppState extends State<MyApp> {
TargetBlankScreen(),
MultipleWebViewsScreen(),
BasicAuthScreen(),
PdfScreen(),
];

@override
Expand Down
28 changes: 28 additions & 0 deletions example/lib/screens/pdf_screen.dart
@@ -0,0 +1,28 @@
import 'package:flutter/material.dart';
import 'package:native_webview/native_webview.dart';

class PdfScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Initial Data with baseURL"),
),
body: WebView(
initialData: WebViewData("""
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
</head>
<body>
<a href="https://porchmusicstore.com/wp-content/uploads/2014/07/alphabet-song-canjo.pdf">Alphabet Song(PDF)</a>
</body>
</html>
"""),
),
);
}
}

0 comments on commit 0bce7c7

Please sign in to comment.