expo-h3 0.1.7 → 0.1.9

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.7'
4
+ version = '0.1.9'
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.7"
38
+ versionName "0.1.9"
39
39
  }
40
40
  lintOptions {
41
41
  abortOnError false
@@ -7,7 +7,7 @@ import android.os.ParcelFileDescriptor;
7
7
  import android.print.PageRange;
8
8
  import android.print.PrintAttributes;
9
9
  import android.print.PrintDocumentAdapter;
10
- import android.print.PrintDocumentInfo;
10
+ import android.webkit.WebView;
11
11
 
12
12
  import expo.modules.kotlin.Promise;
13
13
 
@@ -16,60 +16,96 @@ import java.io.IOException;
16
16
 
17
17
  public class PrintHelper {
18
18
  public static void printWebView(PrintDocumentAdapter adapter, PrintAttributes attributes, final File pdfFile, final Context context, final 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
- final ParcelFileDescriptor finalFd = fd; // For access in inner class
26
- adapter.onWrite(new PageRange[]{PageRange.ALL_PAGES}, fd, new CancellationSignal(), new PrintDocumentAdapter.WriteResultCallback() {
27
- @Override
28
- public void onWriteFinished(PageRange[] pages) {
29
- try {
30
- if (finalFd != null) {
31
- finalFd.close();
32
- }
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
- pdfFile.delete();
41
- promise.resolve("Printed successfully");
42
- } catch (Exception e) {
43
- promise.reject("PRINT_ERROR", e.getMessage(), e);
44
- }
45
- }
19
+ try {
20
+ ParcelFileDescriptor pfd = ParcelFileDescriptor.open(pdfFile,
21
+ ParcelFileDescriptor.MODE_READ_WRITE | ParcelFileDescriptor.MODE_CREATE);
22
+
23
+ PrintDocumentAdapterWrapper wrapper = new PrintDocumentAdapterWrapper(
24
+ adapter, pfd, pdfFile, context, promise);
25
+
26
+ wrapper.onLayout(null, attributes, null, wrapper.layoutCallback, null);
27
+ } catch (IOException e) {
28
+ promise.reject("FILE_ERROR", e.getMessage(), e);
29
+ }
30
+ }
31
+
32
+ static class PrintDocumentAdapterWrapper extends PrintDocumentAdapter {
33
+ private final PrintDocumentAdapter wrapped;
34
+ private final ParcelFileDescriptor pfd;
35
+ private final File pdfFile;
36
+ private final Context context;
37
+ private final Promise promise;
38
+
39
+ LayoutResultCallback layoutCallback = new LayoutResultCallback() {};
40
+ WriteResultCallback writeCallback = new WriteResultCallback() {};
41
+
42
+ PrintDocumentAdapterWrapper(PrintDocumentAdapter wrapped, ParcelFileDescriptor pfd,
43
+ File pdfFile, Context context, Promise promise) {
44
+ this.wrapped = wrapped;
45
+ this.pfd = pfd;
46
+ this.pdfFile = pdfFile;
47
+ this.context = context;
48
+ this.promise = promise;
49
+ }
50
+
51
+ @Override
52
+ public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes,
53
+ CancellationSignal cancellationSignal, LayoutResultCallback callback,
54
+ android.os.Bundle extras) {
55
+ wrapped.onLayout(oldAttributes, newAttributes, cancellationSignal,
56
+ new LayoutResultCallback() {
57
+ @Override
58
+ public void onLayoutFinished(android.print.PrintDocumentInfo info, boolean changed) {
59
+ try {
60
+ wrapped.onWrite(new PageRange[]{PageRange.ALL_PAGES}, pfd,
61
+ cancellationSignal, new WriteResultCallback() {
62
+ @Override
63
+ public void onWriteFinished(PageRange[] pages) {
64
+ try {
65
+ pfd.close();
66
+ Intent intent = new Intent();
67
+ intent.setAction(Intent.ACTION_SEND);
68
+ intent.setPackage("comb.bld.settings.print");
69
+ intent.putExtra(Intent.EXTRA_TEXT, pdfFile.getAbsolutePath());
70
+ intent.setType("application/pdf");
71
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
72
+ context.startActivity(intent);
73
+ pdfFile.delete();
74
+ promise.resolve("Printed successfully");
75
+ } catch (Exception e) {
76
+ promise.reject("PRINT_ERROR", e.getMessage(), e);
77
+ }
78
+ }
46
79
 
47
- @Override
48
- public void onWriteFailed(CharSequence error) {
49
- try {
50
- if (finalFd != null) {
51
- finalFd.close();
52
- }
53
- } catch (IOException ignore) {
54
- }
55
- promise.reject("PDF_WRITE_ERROR", error != null ? error.toString() : "Unknown error", null);
80
+ @Override
81
+ public void onWriteFailed(CharSequence error) {
82
+ try {
83
+ pfd.close();
84
+ } catch (IOException ignore) {}
85
+ promise.reject("PDF_WRITE_ERROR",
86
+ error != null ? error.toString() : "Unknown error", null);
87
+ }
88
+ });
89
+ } catch (Exception e) {
90
+ promise.reject("WRITE_ERROR", e.getMessage(), e);
56
91
  }
57
- });
58
- } catch (IOException e) {
59
- promise.reject("FILE_ERROR", e.getMessage(), e);
60
- if (fd != null) {
92
+ }
93
+
94
+ @Override
95
+ public void onLayoutFailed(CharSequence error) {
61
96
  try {
62
- fd.close();
63
- } catch (IOException ignore) {
64
- }
97
+ pfd.close();
98
+ } catch (IOException ignore) {}
99
+ promise.reject("PDF_LAYOUT_ERROR",
100
+ error != null ? error.toString() : "Unknown error", null);
65
101
  }
66
- }
67
- }
102
+ }, extras);
103
+ }
68
104
 
69
- @Override
70
- public void onLayoutFailed(CharSequence error) {
71
- promise.reject("PDF_LAYOUT_ERROR", error != null ? error.toString() : "Unknown error", null);
72
- }
73
- }, null);
105
+ @Override
106
+ public void onWrite(PageRange[] pages, ParcelFileDescriptor destination,
107
+ CancellationSignal cancellationSignal, WriteResultCallback callback) {
108
+ // Not used in our flow
109
+ }
74
110
  }
75
111
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "expo-h3",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
4
4
  "description": "My new module",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",