-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from andriyoganp/fix/webcontent-converter-plat…
…form-channel fix: content to image native code
- Loading branch information
Showing
16 changed files
with
510 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
## 0.0.8 | ||
|
||
* Add native code to convert content image | ||
|
||
## 0.0.7 | ||
|
||
* Add parameter option to generate image as raster | ||
|
175 changes: 150 additions & 25 deletions
175
android/src/main/kotlin/com/ayeee/blue_print_pos/BluePrintPosPlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,161 @@ | ||
package com.ayeee.blue_print_pos | ||
|
||
import android.app.Activity | ||
import android.content.Context | ||
import android.graphics.Bitmap | ||
import android.graphics.Canvas | ||
import android.os.Build | ||
import android.os.Handler | ||
import android.os.Looper | ||
import android.view.View | ||
import android.view.WindowInsets | ||
import android.webkit.WebView | ||
import android.webkit.WebViewClient | ||
import androidx.annotation.NonNull | ||
|
||
import io.flutter.embedding.engine.plugins.FlutterPlugin | ||
import io.flutter.embedding.engine.plugins.activity.ActivityAware | ||
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding | ||
import io.flutter.plugin.common.MethodCall | ||
import io.flutter.plugin.common.MethodChannel | ||
import io.flutter.plugin.common.MethodChannel.MethodCallHandler | ||
import io.flutter.plugin.common.MethodChannel.Result | ||
import io.flutter.plugin.common.PluginRegistry.Registrar | ||
import java.io.ByteArrayOutputStream | ||
import kotlin.math.absoluteValue | ||
|
||
/** BluePrintPosPlugin */ | ||
class BluePrintPosPlugin: FlutterPlugin, MethodCallHandler { | ||
/// The MethodChannel that will the communication between Flutter and native Android | ||
/// | ||
/// This local reference serves to register the plugin with the Flutter Engine and unregister it | ||
/// when the Flutter Engine is detached from the Activity | ||
private lateinit var channel : MethodChannel | ||
|
||
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { | ||
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "blue_print_pos") | ||
channel.setMethodCallHandler(this) | ||
} | ||
|
||
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) { | ||
if (call.method == "getPlatformVersion") { | ||
result.success("Android ${android.os.Build.VERSION.RELEASE}") | ||
} else { | ||
result.notImplemented() | ||
} | ||
} | ||
|
||
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) { | ||
channel.setMethodCallHandler(null) | ||
} | ||
class BluePrintPosPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { | ||
/// The MethodChannel that will the communication between Flutter and native Android | ||
/// | ||
/// This local reference serves to register the plugin with the Flutter Engine and unregister it | ||
/// when the Flutter Engine is detached from the Activity | ||
private lateinit var channel: MethodChannel | ||
private lateinit var activity: Activity | ||
private lateinit var context: Context | ||
private lateinit var webView: WebView | ||
|
||
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { | ||
val viewID = "webview-view-type" | ||
flutterPluginBinding.platformViewRegistry.registerViewFactory(viewID, FLNativeViewFactory()) | ||
|
||
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "blue_print_pos") | ||
channel.setMethodCallHandler(this) | ||
context = flutterPluginBinding.applicationContext | ||
} | ||
|
||
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) { | ||
val arguments = call.arguments as Map<*, *> | ||
val content = arguments["content"] as String | ||
val duration = arguments["duration"] as Double? | ||
|
||
if (call.method == "contentToImage") { | ||
webView = WebView(this.context) | ||
val dWidth: Int | ||
val dHeight: Int | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { | ||
val windowMetrics = activity.windowManager.currentWindowMetrics | ||
val insets = windowMetrics.windowInsets.getInsetsIgnoringVisibility(WindowInsets.Type.systemBars()) | ||
dWidth = windowMetrics.bounds.width() - insets.left - insets.right | ||
dHeight = windowMetrics.bounds.height() - insets.bottom - insets.top | ||
} else { | ||
dWidth = this.activity.window.windowManager.defaultDisplay.width | ||
dHeight = this.activity.window.windowManager.defaultDisplay.height | ||
} | ||
print("\ndwidth : $dWidth") | ||
print("\ndheight : $dHeight") | ||
webView.layout(0, 0, dWidth, dHeight) | ||
webView.loadDataWithBaseURL(null, content, "text/HTML", "UTF-8", null) | ||
webView.setInitialScale(1) | ||
webView.settings.javaScriptEnabled = true | ||
webView.settings.useWideViewPort = true | ||
webView.settings.javaScriptCanOpenWindowsAutomatically = true | ||
webView.settings.loadWithOverviewMode = true | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | ||
print("\n=======> enabled scrolled <=========") | ||
WebView.enableSlowWholeDocumentDraw() | ||
} | ||
|
||
print("\n ///////////////// webview setted /////////////////") | ||
|
||
webView.webViewClient = object : WebViewClient() { | ||
override fun onPageFinished(view: WebView, url: String) { | ||
super.onPageFinished(view, url) | ||
|
||
Handler(Looper.getMainLooper()).postDelayed({ | ||
print("\nOS Version: ${Build.VERSION.SDK_INT}") | ||
print("\n ================ webview completed ==============") | ||
print("\n scroll delayed ${webView.scrollBarFadeDuration}") | ||
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { | ||
webView.evaluateJavascript("document.body.offsetWidth") { offsetWidth -> | ||
webView.evaluateJavascript("document.body.offsetHeight") { offsetHeight -> | ||
print("\noffsetWidth : $offsetWidth") | ||
print("\noffsetHeight : $offsetHeight") | ||
val data = webView.toBitmap( | ||
offsetWidth!!.toDouble(), | ||
offsetHeight!!.toDouble() | ||
) | ||
if (data != null) { | ||
val bytes = data.toByteArray() | ||
result.success(bytes) | ||
println("\n Got snapshot") | ||
} | ||
} | ||
} | ||
} | ||
}, duration!!.toLong()) | ||
} | ||
} | ||
} else { | ||
result.notImplemented() | ||
} | ||
} | ||
|
||
override fun onAttachedToActivity(binding: ActivityPluginBinding) { | ||
print("onAttachedToActivity") | ||
activity = binding.activity | ||
webView = WebView(activity.applicationContext) | ||
webView.minimumHeight = 1 | ||
webView.minimumWidth = 1 | ||
} | ||
|
||
override fun onDetachedFromActivityForConfigChanges() { | ||
// TODO: the Activity your plugin was attached to was destroyed to change configuration. | ||
// This call will be followed by onReattachedToActivityForConfigChanges(). | ||
print("onDetachedFromActivityForConfigChanges") | ||
} | ||
|
||
override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) { | ||
// TODO: your plugin is now attached to a new Activity after a configuration change. | ||
print("onAttachedToActivity") | ||
onAttachedToActivity(binding) | ||
} | ||
|
||
override fun onDetachedFromActivity() { | ||
// TODO: your plugin is no longer associated with an Activity. Clean up references. | ||
print("onDetachedFromActivity") | ||
} | ||
|
||
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) { | ||
channel.setMethodCallHandler(null) | ||
} | ||
} | ||
|
||
fun WebView.toBitmap(offsetWidth: Double, offsetHeight: Double): Bitmap? { | ||
if (offsetHeight > 0 && offsetWidth > 0) { | ||
val width = (offsetWidth * this.resources.displayMetrics.density).absoluteValue.toInt() | ||
val height = (offsetHeight * this.resources.displayMetrics.density).absoluteValue.toInt() | ||
this.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)) | ||
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) | ||
val canvas = Canvas(bitmap) | ||
this.draw(canvas) | ||
return bitmap | ||
} | ||
return null | ||
} | ||
|
||
fun Bitmap.toByteArray(): ByteArray { | ||
ByteArrayOutputStream().apply { | ||
compress(Bitmap.CompressFormat.PNG, 100, this) | ||
return toByteArray() | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
android/src/main/kotlin/com/ayeee/blue_print_pos/FLNativeViewFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.ayeee.blue_print_pos | ||
|
||
import android.content.Context | ||
import android.view.View | ||
import android.webkit.WebView | ||
import io.flutter.plugin.common.StandardMessageCodec | ||
import io.flutter.plugin.platform.PlatformView | ||
import io.flutter.plugin.platform.PlatformViewFactory | ||
|
||
class FLNativeViewFactory : PlatformViewFactory(StandardMessageCodec.INSTANCE) { | ||
override fun create(context: Context, viewId: Int, args: Any?): PlatformView { | ||
val creationParams = args as Map<String?, Any?>? | ||
return FLNativeView(context, viewId, creationParams) | ||
} | ||
} | ||
|
||
|
||
internal class FLNativeView(context: Context, id: Int, creationParams: Map<String?, Any?>?) : PlatformView { | ||
private val webView: WebView = WebView(context) | ||
private var arguments: Map<String?, Any?>? = creationParams | ||
|
||
override fun getView(): View { | ||
return webView | ||
} | ||
|
||
override fun dispose() {} | ||
|
||
init { | ||
var width = (arguments!!["width"]!! as Number).toInt() | ||
var height = (arguments!!["height"]!! as Number).toInt() | ||
var content = arguments!!["content"] as String | ||
webView.layout(0, 0, width, height) | ||
webView.loadDataWithBaseURL(null, content, "text/HTML", "UTF-8", null) | ||
webView.setInitialScale(1) | ||
webView.settings.javaScriptEnabled = true | ||
webView.settings.useWideViewPort = true | ||
webView.settings.javaScriptCanOpenWindowsAutomatically = true | ||
webView.settings.loadWithOverviewMode = true | ||
|
||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig" | ||
#include "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig" | ||
#include "Generated.xcconfig" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig" | ||
#include "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig" | ||
#include "Generated.xcconfig" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.