expo-h3 0.1.5 → 0.1.6

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.
@@ -1,7 +1,7 @@
1
1
  apply plugin: 'com.android.library'
2
2
 
3
3
  group = 'expo.modules.h3'
4
- version = '0.1.5'
4
+ version = '0.1.6'
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.5"
38
+ versionName "0.1.6"
39
39
  }
40
40
  lintOptions {
41
41
  abortOnError false
@@ -3,23 +3,12 @@ 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
-
20
- // Import the Java shim classes
21
- import expo.modules.h3.LayoutCallback
22
- import expo.modules.h3.WriteCallback
23
12
 
24
13
  class ExpoH3Module : Module() {
25
14
  override fun definition() = ModuleDefinition {
@@ -54,50 +43,13 @@ class ExpoH3Module : Module() {
54
43
  val pdfPath = "/storage/emulated/0/temp_print.pdf"
55
44
  val pdfFile = File(pdfPath)
56
45
  val context = appContext.reactContext!!.applicationContext
57
- try {
58
- // Set up print attributes (A4, high res, no margins)
59
- val attributes = PrintAttributes.Builder()
60
- .setMediaSize(PrintAttributes.MediaSize.ISO_A4)
61
- .setResolution(PrintAttributes.Resolution("pdf", "pdf", 600, 600))
62
- .setMinMargins(PrintAttributes.Margins.NO_MARGINS)
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)
63
50
  .build()
64
- val adapter = webView.createPrintDocumentAdapter("SilentPrintDoc")
65
- // Layout the document
66
- adapter.onLayout(null, attributes, null, object : LayoutCallback() {
67
- override fun onLayoutFinished(info: PrintDocumentInfo?, changed: Boolean) {
68
- // Write to file
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)
87
- }
88
- }
89
- override fun onWriteFailed(error: CharSequence?) {
90
- promise.reject("PDF_WRITE_ERROR", error.toString(), null)
91
- }
92
- })
93
- }
94
- override fun onLayoutFailed(error: CharSequence?) {
95
- promise.reject("PDF_LAYOUT_ERROR", error.toString(), null)
96
- }
97
- }, null)
98
- } catch (e: IOException) {
99
- promise.reject("FILE_ERROR", e.message, e)
100
- }
51
+ val adapter = webView.createPrintDocumentAdapter("SilentPrintDoc")
52
+ PrintHelper.printWebView(adapter, attributes, pdfFile, context, promise)
101
53
  }
102
54
  private fun sendPrintIntent(content: String, mimeType: String, promise: Promise) {
103
55
  val context = appContext.reactContext!!.applicationContext
@@ -0,0 +1,98 @@
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
+
19
+ public static void printWebView(PrintDocumentAdapter adapter, PrintAttributes attributes,
20
+ File pdfFile, Context context, Promise promise) {
21
+
22
+ adapter.onLayout(null, attributes, null, new MyLayoutCallback(pdfFile, context, promise, adapter), null);
23
+ }
24
+
25
+ private static final class MyLayoutCallback extends PrintDocumentAdapter.LayoutResultCallback {
26
+
27
+ private final File pdfFile;
28
+ private final Context context;
29
+ private final Promise promise;
30
+ private final PrintDocumentAdapter adapter;
31
+
32
+ MyLayoutCallback(File pdfFile, Context context, Promise promise, PrintDocumentAdapter adapter) {
33
+ this.pdfFile = pdfFile;
34
+ this.context = context;
35
+ this.promise = promise;
36
+ this.adapter = adapter;
37
+ }
38
+
39
+ @Override
40
+ public void onLayoutFinished(PrintDocumentInfo info, boolean changed) {
41
+ ParcelFileDescriptor fd = null;
42
+ try {
43
+ fd = ParcelFileDescriptor.open(pdfFile, ParcelFileDescriptor.MODE_READ_WRITE);
44
+
45
+ adapter.onWrite(new PageRange[]{PageRange.ALL_PAGES}, fd, new CancellationSignal(),
46
+ new MyWriteCallback(fd, pdfFile, context, promise));
47
+
48
+ } catch (IOException e) {
49
+ promise.reject("FILE_ERROR", e.getMessage(), e);
50
+ }
51
+ }
52
+
53
+ @Override
54
+ public void onLayoutFailed(CharSequence error) {
55
+ promise.reject("PDF_LAYOUT_ERROR", error != null ? error.toString() : "Layout failed", null);
56
+ }
57
+ }
58
+
59
+ private static final class MyWriteCallback extends PrintDocumentAdapter.WriteResultCallback {
60
+
61
+ private final ParcelFileDescriptor fd;
62
+ private final File pdfFile;
63
+ private final Context context;
64
+ private final Promise promise;
65
+
66
+ MyWriteCallback(ParcelFileDescriptor fd, File pdfFile, Context context, Promise promise) {
67
+ this.fd = fd;
68
+ this.pdfFile = pdfFile;
69
+ this.context = context;
70
+ this.promise = promise;
71
+ }
72
+
73
+ @Override
74
+ public void onWriteFinished(PageRange[] pages) {
75
+ try {
76
+ if (fd != null) fd.close();
77
+
78
+ Intent intent = new Intent(Intent.ACTION_SEND);
79
+ intent.setPackage("comb.bld.settings.print");
80
+ intent.putExtra(Intent.EXTRA_TEXT, pdfFile.getAbsolutePath());
81
+ intent.setType("application/pdf");
82
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
83
+
84
+ context.startActivity(intent);
85
+ pdfFile.delete();
86
+
87
+ promise.resolve("Printed successfully");
88
+ } catch (Exception e) {
89
+ promise.reject("PRINT_ERROR", e.getMessage(), e);
90
+ }
91
+ }
92
+
93
+ @Override
94
+ public void onWriteFailed(CharSequence error) {
95
+ promise.reject("PDF_WRITE_ERROR", error != null ? error.toString() : "Write failed", null);
96
+ }
97
+ }
98
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "expo-h3",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "description": "My new module",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",
@@ -1,15 +0,0 @@
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
- }