expo-h3 0.1.2 → 0.1.4
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.4'
|
|
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.4"
|
|
39
39
|
}
|
|
40
40
|
lintOptions {
|
|
41
41
|
abortOnError false
|
|
@@ -3,50 +3,31 @@ package expo.modules.h3
|
|
|
3
3
|
import expo.modules.kotlin.modules.Module
|
|
4
4
|
import expo.modules.kotlin.modules.ModuleDefinition
|
|
5
5
|
import android.content.Intent
|
|
6
|
-
import android.os.CancellationSignal
|
|
7
|
-
import android.os.ParcelFileDescriptor
|
|
8
|
-
import android.print.PageRange
|
|
9
|
-
import android.print.PrintAttributes
|
|
10
|
-
import android.print.PrintDocumentAdapter
|
|
11
|
-
import android.print.PrintDocumentInfo
|
|
12
|
-
import android.webkit.WebView
|
|
13
|
-
import android.webkit.WebViewClient
|
|
14
6
|
import android.os.Handler
|
|
15
7
|
import android.os.Looper
|
|
8
|
+
import android.webkit.WebView
|
|
9
|
+
import android.webkit.WebViewClient
|
|
16
10
|
import expo.modules.kotlin.Promise
|
|
17
11
|
import java.io.File
|
|
18
|
-
import java.io.IOException
|
|
19
12
|
|
|
20
13
|
class ExpoH3Module : Module() {
|
|
21
|
-
|
|
22
14
|
override fun definition() = ModuleDefinition {
|
|
23
|
-
|
|
24
15
|
Name("ExpoH3")
|
|
25
|
-
|
|
26
16
|
// Function for printing HTML as PDF
|
|
27
17
|
AsyncFunction("printHtmlAsync") { html: String, promise: Promise ->
|
|
28
18
|
silentPrintHtml(html, promise)
|
|
29
19
|
}
|
|
30
|
-
|
|
31
20
|
// Function for printing plain text
|
|
32
21
|
AsyncFunction("printTextAsync") { text: String, promise: Promise ->
|
|
33
22
|
sendPrintIntent(text, "text/plain", promise)
|
|
34
23
|
}
|
|
35
|
-
|
|
36
24
|
// Function for sharing/printing web URL
|
|
37
25
|
AsyncFunction("printUrlAsync") { url: String, promise: Promise ->
|
|
38
26
|
sendPrintIntent(url, "text/plain", promise)
|
|
39
27
|
}
|
|
40
|
-
|
|
41
28
|
}
|
|
42
|
-
|
|
43
29
|
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
|
-
|
|
30
|
+
val context = appContext.reactContext!!.applicationContext
|
|
50
31
|
val handler = Handler(Looper.getMainLooper())
|
|
51
32
|
handler.post {
|
|
52
33
|
val webView = WebView(context)
|
|
@@ -58,79 +39,20 @@ class ExpoH3Module : Module() {
|
|
|
58
39
|
webView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null)
|
|
59
40
|
}
|
|
60
41
|
}
|
|
61
|
-
|
|
62
42
|
private fun createPdfFromWebView(webView: WebView, promise: Promise) {
|
|
63
43
|
val pdfPath = "/storage/emulated/0/temp_print.pdf"
|
|
64
44
|
val pdfFile = File(pdfPath)
|
|
65
|
-
val context = appContext.reactContext
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
try {
|
|
73
|
-
// Set up print attributes (A4, high res, no margins)
|
|
74
|
-
val attributes = PrintAttributes.Builder()
|
|
75
|
-
.setMediaSize(PrintAttributes.MediaSize.ISO_A4)
|
|
76
|
-
.setResolution(PrintAttributes.Resolution("pdf", "pdf", 600, 600))
|
|
77
|
-
.setMinMargins(PrintAttributes.Margins.NO_MARGINS)
|
|
45
|
+
val context = appContext.reactContext!!.applicationContext
|
|
46
|
+
val attributes = android.print.PrintAttributes.Builder()
|
|
47
|
+
.setMediaSize(android.print.PrintAttributes.MediaSize.ISO_A4)
|
|
48
|
+
.setResolution(android.print.PrintAttributes.Resolution("pdf", "pdf", 600, 600))
|
|
49
|
+
.setMinMargins(android.print.PrintAttributes.Margins.NO_MARGINS)
|
|
78
50
|
.build()
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
// Layout the document - use anonymous object instead of constructor
|
|
83
|
-
adapter.onLayout(null, attributes, null, object : PrintDocumentAdapter.LayoutResultCallback() {
|
|
84
|
-
override fun onLayoutFinished(info: PrintDocumentInfo?, changed: Boolean) {
|
|
85
|
-
// Write to file
|
|
86
|
-
try {
|
|
87
|
-
val fd = ParcelFileDescriptor.open(pdfFile, ParcelFileDescriptor.MODE_READ_WRITE or ParcelFileDescriptor.MODE_CREATE)
|
|
88
|
-
|
|
89
|
-
adapter.onWrite(arrayOf(PageRange.ALL_PAGES), fd, CancellationSignal(), object : PrintDocumentAdapter.WriteResultCallback() {
|
|
90
|
-
override fun onWriteFinished(pages: Array<out PageRange>?) {
|
|
91
|
-
try {
|
|
92
|
-
fd.close()
|
|
93
|
-
// Now send the print intent
|
|
94
|
-
val intent = Intent()
|
|
95
|
-
intent.action = Intent.ACTION_SEND
|
|
96
|
-
intent.setPackage("comb.bld.settings.print")
|
|
97
|
-
intent.putExtra(Intent.EXTRA_TEXT, pdfPath)
|
|
98
|
-
intent.type = "application/pdf"
|
|
99
|
-
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
|
100
|
-
context.startActivity(intent)
|
|
101
|
-
// Delete the temp file
|
|
102
|
-
pdfFile.delete()
|
|
103
|
-
promise.resolve("Printed successfully")
|
|
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)
|
|
111
|
-
}
|
|
112
|
-
})
|
|
113
|
-
} catch (e: IOException) {
|
|
114
|
-
promise.reject("FILE_ERROR", e.message, e)
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
override fun onLayoutFailed(error: CharSequence?) {
|
|
119
|
-
promise.reject("PDF_LAYOUT_ERROR", error.toString(), null)
|
|
120
|
-
}
|
|
121
|
-
}, null)
|
|
122
|
-
} catch (e: IOException) {
|
|
123
|
-
promise.reject("FILE_ERROR", e.message, e)
|
|
124
|
-
}
|
|
51
|
+
val adapter = webView.createPrintDocumentAdapter("SilentPrintDoc")
|
|
52
|
+
PrintHelper.printWebView(adapter, attributes, pdfFile, context, promise)
|
|
125
53
|
}
|
|
126
|
-
|
|
127
54
|
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
|
-
|
|
55
|
+
val context = appContext.reactContext!!.applicationContext
|
|
134
56
|
val handler = Handler(Looper.getMainLooper())
|
|
135
57
|
handler.post {
|
|
136
58
|
try {
|
|
@@ -147,5 +69,4 @@ class ExpoH3Module : Module() {
|
|
|
147
69
|
}
|
|
148
70
|
}
|
|
149
71
|
}
|
|
150
|
-
|
|
151
72
|
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
package expo.modules.h3;
|
|
2
|
+
|
|
3
|
+
import android.content.Context;
|
|
4
|
+
import android.content.Intent;
|
|
5
|
+
import android.os.CancellationSignal;
|
|
6
|
+
import android.os.ParcelFileDescriptor;
|
|
7
|
+
import android.print.PageRange;
|
|
8
|
+
import android.print.PrintAttributes;
|
|
9
|
+
import android.print.PrintDocumentAdapter;
|
|
10
|
+
import android.print.PrintDocumentInfo;
|
|
11
|
+
|
|
12
|
+
import expo.modules.kotlin.Promise;
|
|
13
|
+
|
|
14
|
+
import java.io.File;
|
|
15
|
+
import java.io.IOException;
|
|
16
|
+
|
|
17
|
+
public class PrintHelper {
|
|
18
|
+
public static void printWebView(PrintDocumentAdapter adapter, PrintAttributes attributes, File pdfFile, Context context, Promise promise) {
|
|
19
|
+
adapter.onLayout(null, attributes, null, new PrintDocumentAdapter.LayoutResultCallback() {
|
|
20
|
+
@Override
|
|
21
|
+
public void onLayoutFinished(PrintDocumentInfo info, boolean changed) {
|
|
22
|
+
ParcelFileDescriptor fd = null;
|
|
23
|
+
try {
|
|
24
|
+
fd = ParcelFileDescriptor.open(pdfFile, ParcelFileDescriptor.MODE_READ_WRITE);
|
|
25
|
+
adapter.onWrite(new PageRange[]{PageRange.ALL_PAGES}, fd, new CancellationSignal(), new PrintDocumentAdapter.WriteResultCallback() {
|
|
26
|
+
@Override
|
|
27
|
+
public void onWriteFinished(PageRange[] pages) {
|
|
28
|
+
try {
|
|
29
|
+
if (fd != null) {
|
|
30
|
+
fd.close();
|
|
31
|
+
}
|
|
32
|
+
// Now send the print intent
|
|
33
|
+
Intent intent = new Intent();
|
|
34
|
+
intent.setAction(Intent.ACTION_SEND);
|
|
35
|
+
intent.setPackage("comb.bld.settings.print");
|
|
36
|
+
intent.putExtra(Intent.EXTRA_TEXT, pdfFile.getAbsolutePath());
|
|
37
|
+
intent.setType("application/pdf");
|
|
38
|
+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
39
|
+
context.startActivity(intent);
|
|
40
|
+
// Delete the temp file (assume print service has it now; add delay if needed for testing)
|
|
41
|
+
pdfFile.delete();
|
|
42
|
+
promise.resolve("Printed successfully");
|
|
43
|
+
} catch (Exception e) {
|
|
44
|
+
promise.reject("PRINT_ERROR", e.getMessage(), e);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
@Override
|
|
49
|
+
public void onWriteFailed(CharSequence error) {
|
|
50
|
+
promise.reject("PDF_WRITE_ERROR", error != null ? error.toString() : "Unknown error", null);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
} catch (IOException e) {
|
|
54
|
+
promise.reject("FILE_ERROR", e.getMessage(), e);
|
|
55
|
+
} finally {
|
|
56
|
+
if (fd != null) {
|
|
57
|
+
try {
|
|
58
|
+
fd.close();
|
|
59
|
+
} catch (IOException e) {
|
|
60
|
+
// Ignore
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
@Override
|
|
67
|
+
public void onLayoutFailed(CharSequence error) {
|
|
68
|
+
promise.reject("PDF_LAYOUT_ERROR", error != null ? error.toString() : "Unknown error", null);
|
|
69
|
+
}
|
|
70
|
+
}, null);
|
|
71
|
+
}
|
|
72
|
+
}
|