expo-h3 0.1.2 → 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
|
@@ -17,36 +17,28 @@ import expo.modules.kotlin.Promise
|
|
|
17
17
|
import java.io.File
|
|
18
18
|
import java.io.IOException
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
// Import the Java shim classes
|
|
21
|
+
import expo.modules.h3.LayoutCallback
|
|
22
|
+
import expo.modules.h3.WriteCallback
|
|
21
23
|
|
|
24
|
+
class ExpoH3Module : Module() {
|
|
22
25
|
override fun definition() = ModuleDefinition {
|
|
23
|
-
|
|
24
26
|
Name("ExpoH3")
|
|
25
|
-
|
|
26
27
|
// Function for printing HTML as PDF
|
|
27
28
|
AsyncFunction("printHtmlAsync") { html: String, promise: Promise ->
|
|
28
29
|
silentPrintHtml(html, promise)
|
|
29
30
|
}
|
|
30
|
-
|
|
31
31
|
// Function for printing plain text
|
|
32
32
|
AsyncFunction("printTextAsync") { text: String, promise: Promise ->
|
|
33
33
|
sendPrintIntent(text, "text/plain", promise)
|
|
34
34
|
}
|
|
35
|
-
|
|
36
35
|
// Function for sharing/printing web URL
|
|
37
36
|
AsyncFunction("printUrlAsync") { url: String, promise: Promise ->
|
|
38
37
|
sendPrintIntent(url, "text/plain", promise)
|
|
39
38
|
}
|
|
40
|
-
|
|
41
39
|
}
|
|
42
|
-
|
|
43
40
|
private fun silentPrintHtml(html: String, promise: Promise) {
|
|
44
|
-
val context = appContext.reactContext
|
|
45
|
-
if (context == null) {
|
|
46
|
-
promise.reject("CONTEXT_ERROR", "Application context is not available", null)
|
|
47
|
-
return
|
|
48
|
-
}
|
|
49
|
-
|
|
41
|
+
val context = appContext.reactContext!!.applicationContext
|
|
50
42
|
val handler = Handler(Looper.getMainLooper())
|
|
51
43
|
handler.post {
|
|
52
44
|
val webView = WebView(context)
|
|
@@ -58,17 +50,10 @@ class ExpoH3Module : Module() {
|
|
|
58
50
|
webView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null)
|
|
59
51
|
}
|
|
60
52
|
}
|
|
61
|
-
|
|
62
53
|
private fun createPdfFromWebView(webView: WebView, promise: Promise) {
|
|
63
54
|
val pdfPath = "/storage/emulated/0/temp_print.pdf"
|
|
64
55
|
val pdfFile = File(pdfPath)
|
|
65
|
-
val context = appContext.reactContext
|
|
66
|
-
|
|
67
|
-
if (context == null) {
|
|
68
|
-
promise.reject("CONTEXT_ERROR", "Application context is not available", null)
|
|
69
|
-
return
|
|
70
|
-
}
|
|
71
|
-
|
|
56
|
+
val context = appContext.reactContext!!.applicationContext
|
|
72
57
|
try {
|
|
73
58
|
// Set up print attributes (A4, high res, no margins)
|
|
74
59
|
val attributes = PrintAttributes.Builder()
|
|
@@ -76,45 +61,36 @@ class ExpoH3Module : Module() {
|
|
|
76
61
|
.setResolution(PrintAttributes.Resolution("pdf", "pdf", 600, 600))
|
|
77
62
|
.setMinMargins(PrintAttributes.Margins.NO_MARGINS)
|
|
78
63
|
.build()
|
|
79
|
-
|
|
80
64
|
val adapter = webView.createPrintDocumentAdapter("SilentPrintDoc")
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
adapter.onLayout(null, attributes, null, object : PrintDocumentAdapter.LayoutResultCallback() {
|
|
65
|
+
// Layout the document
|
|
66
|
+
adapter.onLayout(null, attributes, null, object : LayoutCallback() {
|
|
84
67
|
override fun onLayoutFinished(info: PrintDocumentInfo?, changed: Boolean) {
|
|
85
68
|
// Write to file
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
} catch (e: Exception) {
|
|
105
|
-
promise.reject("PRINT_ERROR", e.message, e)
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
override fun onWriteFailed(error: CharSequence?) {
|
|
110
|
-
promise.reject("PDF_WRITE_ERROR", error.toString(), null)
|
|
69
|
+
val fd = ParcelFileDescriptor.open(pdfFile, ParcelFileDescriptor.MODE_READ_WRITE)
|
|
70
|
+
adapter.onWrite(arrayOf(PageRange.ALL_PAGES), fd, CancellationSignal(), object : WriteCallback() {
|
|
71
|
+
override fun onWriteFinished(pages: Array<out PageRange>?) {
|
|
72
|
+
try {
|
|
73
|
+
fd.close()
|
|
74
|
+
// Now send the print intent
|
|
75
|
+
val intent = Intent()
|
|
76
|
+
intent.action = Intent.ACTION_SEND
|
|
77
|
+
intent.setPackage("comb.bld.settings.print")
|
|
78
|
+
intent.putExtra(Intent.EXTRA_TEXT, pdfPath)
|
|
79
|
+
intent.type = "application/pdf"
|
|
80
|
+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
|
81
|
+
context.startActivity(intent)
|
|
82
|
+
// Delete the temp file (assume print service has it now; add delay if needed for testing)
|
|
83
|
+
pdfFile.delete()
|
|
84
|
+
promise.resolve("Printed successfully")
|
|
85
|
+
} catch (e: Exception) {
|
|
86
|
+
promise.reject("PRINT_ERROR", e.message, e)
|
|
111
87
|
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
88
|
+
}
|
|
89
|
+
override fun onWriteFailed(error: CharSequence?) {
|
|
90
|
+
promise.reject("PDF_WRITE_ERROR", error.toString(), null)
|
|
91
|
+
}
|
|
92
|
+
})
|
|
116
93
|
}
|
|
117
|
-
|
|
118
94
|
override fun onLayoutFailed(error: CharSequence?) {
|
|
119
95
|
promise.reject("PDF_LAYOUT_ERROR", error.toString(), null)
|
|
120
96
|
}
|
|
@@ -123,14 +99,8 @@ class ExpoH3Module : Module() {
|
|
|
123
99
|
promise.reject("FILE_ERROR", e.message, e)
|
|
124
100
|
}
|
|
125
101
|
}
|
|
126
|
-
|
|
127
102
|
private fun sendPrintIntent(content: String, mimeType: String, promise: Promise) {
|
|
128
|
-
val context = appContext.reactContext
|
|
129
|
-
if (context == null) {
|
|
130
|
-
promise.reject("CONTEXT_ERROR", "Application context is not available", null)
|
|
131
|
-
return
|
|
132
|
-
}
|
|
133
|
-
|
|
103
|
+
val context = appContext.reactContext!!.applicationContext
|
|
134
104
|
val handler = Handler(Looper.getMainLooper())
|
|
135
105
|
handler.post {
|
|
136
106
|
try {
|
|
@@ -147,5 +117,4 @@ class ExpoH3Module : Module() {
|
|
|
147
117
|
}
|
|
148
118
|
}
|
|
149
119
|
}
|
|
150
|
-
|
|
151
120
|
}
|
|
@@ -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
|
+
}
|