expo-h3 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/android/build.gradle
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
apply plugin: 'com.android.library'
|
|
2
2
|
|
|
3
3
|
group = 'expo.modules.h3'
|
|
4
|
-
version = '0.1.
|
|
4
|
+
version = '0.1.2'
|
|
5
5
|
|
|
6
6
|
def expoModulesCorePlugin = new File(project(":expo-modules-core").projectDir.absolutePath, "ExpoModulesCorePlugin.gradle")
|
|
7
7
|
apply from: expoModulesCorePlugin
|
|
@@ -35,7 +35,7 @@ android {
|
|
|
35
35
|
namespace "expo.modules.h3"
|
|
36
36
|
defaultConfig {
|
|
37
37
|
versionCode 1
|
|
38
|
-
versionName "0.1.
|
|
38
|
+
versionName "0.1.3"
|
|
39
39
|
}
|
|
40
40
|
lintOptions {
|
|
41
41
|
abortOnError false
|
|
@@ -2,7 +2,6 @@ package expo.modules.h3
|
|
|
2
2
|
|
|
3
3
|
import expo.modules.kotlin.modules.Module
|
|
4
4
|
import expo.modules.kotlin.modules.ModuleDefinition
|
|
5
|
-
|
|
6
5
|
import android.content.Intent
|
|
7
6
|
import android.os.CancellationSignal
|
|
8
7
|
import android.os.ParcelFileDescriptor
|
|
@@ -18,30 +17,29 @@ import expo.modules.kotlin.Promise
|
|
|
18
17
|
import java.io.File
|
|
19
18
|
import java.io.IOException
|
|
20
19
|
|
|
20
|
+
// Import the Java shim classes
|
|
21
|
+
import expo.modules.h3.LayoutCallback
|
|
22
|
+
import expo.modules.h3.WriteCallback
|
|
23
|
+
|
|
21
24
|
class ExpoH3Module : Module() {
|
|
22
25
|
override fun definition() = ModuleDefinition {
|
|
23
26
|
Name("ExpoH3")
|
|
24
|
-
|
|
25
27
|
// Function for printing HTML as PDF
|
|
26
28
|
AsyncFunction("printHtmlAsync") { html: String, promise: Promise ->
|
|
27
29
|
silentPrintHtml(html, promise)
|
|
28
30
|
}
|
|
29
|
-
|
|
30
31
|
// Function for printing plain text
|
|
31
32
|
AsyncFunction("printTextAsync") { text: String, promise: Promise ->
|
|
32
33
|
sendPrintIntent(text, "text/plain", promise)
|
|
33
34
|
}
|
|
34
|
-
|
|
35
35
|
// Function for sharing/printing web URL
|
|
36
36
|
AsyncFunction("printUrlAsync") { url: String, promise: Promise ->
|
|
37
37
|
sendPrintIntent(url, "text/plain", promise)
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
|
-
|
|
41
40
|
private fun silentPrintHtml(html: String, promise: Promise) {
|
|
42
|
-
val context = appContext.reactContext
|
|
41
|
+
val context = appContext.reactContext!!.applicationContext
|
|
43
42
|
val handler = Handler(Looper.getMainLooper())
|
|
44
|
-
|
|
45
43
|
handler.post {
|
|
46
44
|
val webView = WebView(context)
|
|
47
45
|
webView.webViewClient = object : WebViewClient() {
|
|
@@ -52,12 +50,10 @@ class ExpoH3Module : Module() {
|
|
|
52
50
|
webView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null)
|
|
53
51
|
}
|
|
54
52
|
}
|
|
55
|
-
|
|
56
53
|
private fun createPdfFromWebView(webView: WebView, promise: Promise) {
|
|
57
54
|
val pdfPath = "/storage/emulated/0/temp_print.pdf"
|
|
58
55
|
val pdfFile = File(pdfPath)
|
|
59
|
-
val context = appContext.reactContext
|
|
60
|
-
|
|
56
|
+
val context = appContext.reactContext!!.applicationContext
|
|
61
57
|
try {
|
|
62
58
|
// Set up print attributes (A4, high res, no margins)
|
|
63
59
|
val attributes = PrintAttributes.Builder()
|
|
@@ -65,15 +61,13 @@ class ExpoH3Module : Module() {
|
|
|
65
61
|
.setResolution(PrintAttributes.Resolution("pdf", "pdf", 600, 600))
|
|
66
62
|
.setMinMargins(PrintAttributes.Margins.NO_MARGINS)
|
|
67
63
|
.build()
|
|
68
|
-
|
|
69
64
|
val adapter = webView.createPrintDocumentAdapter("SilentPrintDoc")
|
|
70
|
-
|
|
71
65
|
// Layout the document
|
|
72
|
-
adapter.onLayout(null, attributes, null, object :
|
|
66
|
+
adapter.onLayout(null, attributes, null, object : LayoutCallback() {
|
|
73
67
|
override fun onLayoutFinished(info: PrintDocumentInfo?, changed: Boolean) {
|
|
74
68
|
// Write to file
|
|
75
69
|
val fd = ParcelFileDescriptor.open(pdfFile, ParcelFileDescriptor.MODE_READ_WRITE)
|
|
76
|
-
adapter.onWrite(arrayOf(PageRange.ALL_PAGES), fd, CancellationSignal(), object :
|
|
70
|
+
adapter.onWrite(arrayOf(PageRange.ALL_PAGES), fd, CancellationSignal(), object : WriteCallback() {
|
|
77
71
|
override fun onWriteFinished(pages: Array<out PageRange>?) {
|
|
78
72
|
try {
|
|
79
73
|
fd.close()
|
|
@@ -85,22 +79,18 @@ class ExpoH3Module : Module() {
|
|
|
85
79
|
intent.type = "application/pdf"
|
|
86
80
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
|
87
81
|
context.startActivity(intent)
|
|
88
|
-
|
|
89
82
|
// Delete the temp file (assume print service has it now; add delay if needed for testing)
|
|
90
83
|
pdfFile.delete()
|
|
91
|
-
|
|
92
84
|
promise.resolve("Printed successfully")
|
|
93
85
|
} catch (e: Exception) {
|
|
94
86
|
promise.reject("PRINT_ERROR", e.message, e)
|
|
95
87
|
}
|
|
96
88
|
}
|
|
97
|
-
|
|
98
89
|
override fun onWriteFailed(error: CharSequence?) {
|
|
99
90
|
promise.reject("PDF_WRITE_ERROR", error.toString(), null)
|
|
100
91
|
}
|
|
101
92
|
})
|
|
102
93
|
}
|
|
103
|
-
|
|
104
94
|
override fun onLayoutFailed(error: CharSequence?) {
|
|
105
95
|
promise.reject("PDF_LAYOUT_ERROR", error.toString(), null)
|
|
106
96
|
}
|
|
@@ -109,11 +99,9 @@ class ExpoH3Module : Module() {
|
|
|
109
99
|
promise.reject("FILE_ERROR", e.message, e)
|
|
110
100
|
}
|
|
111
101
|
}
|
|
112
|
-
|
|
113
102
|
private fun sendPrintIntent(content: String, mimeType: String, promise: Promise) {
|
|
114
|
-
val context = appContext.reactContext
|
|
103
|
+
val context = appContext.reactContext!!.applicationContext
|
|
115
104
|
val handler = Handler(Looper.getMainLooper())
|
|
116
|
-
|
|
117
105
|
handler.post {
|
|
118
106
|
try {
|
|
119
107
|
val intent = Intent()
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
package expo.modules.h3;
|
|
2
|
+
|
|
3
|
+
import android.print.PrintDocumentAdapter;
|
|
4
|
+
|
|
5
|
+
public abstract class LayoutCallback extends PrintDocumentAdapter.LayoutResultCallback {
|
|
6
|
+
public LayoutCallback() {
|
|
7
|
+
// Implicitly calls super(); javac allows this despite package-private access.
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
public abstract class WriteCallback extends PrintDocumentAdapter.WriteResultCallback {
|
|
12
|
+
public WriteCallback() {
|
|
13
|
+
// Implicitly calls super(); javac allows this.
|
|
14
|
+
}
|
|
15
|
+
}
|