|
18 | 18 | import android.util.Log; |
19 | 19 |
|
20 | 20 | import androidx.annotation.NonNull; |
| 21 | +import androidx.annotation.Nullable; |
21 | 22 | import androidx.core.app.ActivityCompat; |
22 | 23 | import androidx.core.content.ContextCompat; |
23 | 24 |
|
24 | 25 | import com.pspdfkit.PSPDFKit; |
| 26 | +import com.pspdfkit.document.PdfDocument; |
| 27 | +import com.pspdfkit.document.formatters.DocumentJsonFormatter; |
| 28 | +import com.pspdfkit.document.providers.InputStreamDataProvider; |
25 | 29 | import com.pspdfkit.forms.ChoiceFormElement; |
26 | 30 | import com.pspdfkit.forms.EditableButtonFormElement; |
27 | 31 | import com.pspdfkit.forms.SignatureFormElement; |
28 | 32 | import com.pspdfkit.forms.TextFormElement; |
29 | 33 | import com.pspdfkit.ui.PdfActivityIntentBuilder; |
30 | 34 |
|
| 35 | +import java.io.ByteArrayInputStream; |
| 36 | +import java.io.ByteArrayOutputStream; |
| 37 | +import java.io.InputStream; |
| 38 | +import java.nio.charset.StandardCharsets; |
31 | 39 | import java.util.ArrayList; |
32 | 40 | import java.util.HashMap; |
33 | 41 | import java.util.Iterator; |
34 | 42 | import java.util.List; |
| 43 | +import java.util.UUID; |
35 | 44 | import java.util.concurrent.atomic.AtomicReference; |
36 | 45 |
|
37 | 46 | import io.flutter.plugin.common.MethodCall; |
|
43 | 52 | import io.reactivex.android.schedulers.AndroidSchedulers; |
44 | 53 | import io.reactivex.schedulers.Schedulers; |
45 | 54 |
|
| 55 | +import static com.pspdfkit.flutter.pspdfkit.util.Preconditions.requireDocumentNotNull; |
46 | 56 | import static com.pspdfkit.flutter.pspdfkit.util.Preconditions.requireNotNullNotEmpty; |
47 | 57 |
|
48 | 58 | /** |
@@ -76,6 +86,7 @@ public static void registerWith(Registrar registrar) { |
76 | 86 | public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { |
77 | 87 | String fullyQualifiedName; |
78 | 88 | FlutterPdfActivity flutterPdfActivity; |
| 89 | + PdfDocument document; |
79 | 90 |
|
80 | 91 | switch (call.method) { |
81 | 92 | case "frameworkVersion": |
@@ -128,20 +139,47 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { |
128 | 139 | openSettings(); |
129 | 140 | result.success(true); |
130 | 141 | break; |
| 142 | + case "applyInstantJson": |
| 143 | + String annotationsJson = call.argument("annotationsJson"); |
| 144 | + |
| 145 | + requireNotNullNotEmpty(annotationsJson, "annotationsJson"); |
| 146 | + document = requireDocumentNotNull(FlutterPdfActivity.getCurrentActivity(), "Pspdfkit.applyInstantJson(String)"); |
| 147 | + |
| 148 | + DocumentJsonDataProvider documentJsonDataProvider = new DocumentJsonDataProvider(annotationsJson); |
| 149 | + //noinspection ResultOfMethodCallIgnored |
| 150 | + DocumentJsonFormatter.importDocumentJsonAsync(document, documentJsonDataProvider) |
| 151 | + .subscribeOn(Schedulers.io()) |
| 152 | + .observeOn(AndroidSchedulers.mainThread()) |
| 153 | + .subscribe( |
| 154 | + () -> result.success(true), |
| 155 | + throwable -> result.error(LOG_TAG, |
| 156 | + "Error while importing document Instant JSON", |
| 157 | + throwable.getMessage())); |
| 158 | + break; |
| 159 | + case "exportInstantJson": |
| 160 | + document = requireDocumentNotNull(FlutterPdfActivity.getCurrentActivity(), "Pspdfkit.exportInstantJson()"); |
| 161 | + |
| 162 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 163 | + //noinspection ResultOfMethodCallIgnored |
| 164 | + DocumentJsonFormatter.exportDocumentJsonAsync(document, outputStream) |
| 165 | + .subscribeOn(Schedulers.io()) |
| 166 | + .observeOn(AndroidSchedulers.mainThread()) |
| 167 | + .subscribe( |
| 168 | + () -> result.success(outputStream.toString(StandardCharsets.UTF_8.name())), |
| 169 | + throwable -> result.error(LOG_TAG, |
| 170 | + "Error while exporting document Instant JSON", |
| 171 | + throwable.getMessage())); |
| 172 | + break; |
131 | 173 | case "setFormFieldValue": |
132 | 174 | String value = call.argument("value"); |
133 | 175 | fullyQualifiedName = call.argument("fullyQualifiedName"); |
134 | 176 |
|
135 | 177 | requireNotNullNotEmpty(value, "Value"); |
136 | 178 | requireNotNullNotEmpty(fullyQualifiedName, "Fully qualified name"); |
137 | | - flutterPdfActivity = FlutterPdfActivity.getCurrentActivity(); |
138 | | - if (flutterPdfActivity == null) { |
139 | | - throw new IllegalStateException("Before using \"Pspdfkit.setFormFieldValue()\" " + |
140 | | - "the document needs to be presented by calling \"Pspdfkit.present()\"."); |
141 | | - } |
| 179 | + document = requireDocumentNotNull(FlutterPdfActivity.getCurrentActivity(), "Pspdfkit.setFormFieldValue(String)"); |
142 | 180 |
|
143 | | - //noinspection ResultOfMethodCallIgnored,ConstantConditions |
144 | | - flutterPdfActivity.getPdfFragment().getDocument().getFormProvider() |
| 181 | + //noinspection ResultOfMethodCallIgnored |
| 182 | + document.getFormProvider() |
145 | 183 | .getFormElementWithNameAsync(fullyQualifiedName) |
146 | 184 | .subscribeOn(Schedulers.computation()) |
147 | 185 | .observeOn(AndroidSchedulers.mainThread()) |
@@ -187,14 +225,10 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { |
187 | 225 | fullyQualifiedName = call.argument("fullyQualifiedName"); |
188 | 226 |
|
189 | 227 | requireNotNullNotEmpty(fullyQualifiedName, "Fully qualified name"); |
190 | | - flutterPdfActivity = FlutterPdfActivity.getCurrentActivity(); |
191 | | - if (flutterPdfActivity == null) { |
192 | | - throw new IllegalStateException("Before using \"Pspdfkit.setFormFieldValue()\" " + |
193 | | - "the document needs to be presented by calling \"Pspdfkit.present()\"."); |
194 | | - } |
| 228 | + document = requireDocumentNotNull(FlutterPdfActivity.getCurrentActivity(), "Pspdfkit.getFormFieldValue()"); |
195 | 229 |
|
196 | | - //noinspection ResultOfMethodCallIgnored,ConstantConditions |
197 | | - flutterPdfActivity.getPdfFragment().getDocument().getFormProvider() |
| 230 | + //noinspection ResultOfMethodCallIgnored |
| 231 | + document.getFormProvider() |
198 | 232 | .getFormElementWithNameAsync(fullyQualifiedName) |
199 | 233 | .subscribeOn(Schedulers.computation()) |
200 | 234 | .observeOn(AndroidSchedulers.mainThread()) |
@@ -233,13 +267,10 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { |
233 | 267 | ); |
234 | 268 | break; |
235 | 269 | case "save": |
236 | | - flutterPdfActivity = FlutterPdfActivity.getCurrentActivity(); |
237 | | - if (flutterPdfActivity == null) { |
238 | | - throw new IllegalStateException("Before using \"Pspdfkit.save()\" " + |
239 | | - "the document needs to be presented by calling \"Pspdfkit.present()\"."); |
240 | | - } |
241 | | - //noinspection ResultOfMethodCallIgnored,ConstantConditions |
242 | | - flutterPdfActivity.getPdfFragment().getDocument().saveIfModifiedAsync() |
| 270 | + document = requireDocumentNotNull(FlutterPdfActivity.getCurrentActivity(), "Pspdfkit.save()"); |
| 271 | + |
| 272 | + //noinspection ResultOfMethodCallIgnored |
| 273 | + document.saveIfModifiedAsync() |
243 | 274 | .subscribeOn(Schedulers.computation()) |
244 | 275 | .observeOn(AndroidSchedulers.mainThread()) |
245 | 276 | .subscribe(result::success); |
@@ -353,4 +384,37 @@ public boolean onRequestPermissionsResult(int requestCode, String[] permissions, |
353 | 384 | } |
354 | 385 | return status == 2; |
355 | 386 | } |
| 387 | + |
| 388 | + /** A small in-memory data provider for loading the Document instant JSON from a string. */ |
| 389 | + private static class DocumentJsonDataProvider extends InputStreamDataProvider { |
| 390 | + @NonNull private final byte[] annotationsJsonBytes; |
| 391 | + @NonNull private final UUID uuid; |
| 392 | + |
| 393 | + DocumentJsonDataProvider(@NonNull final String annotationsJson) { |
| 394 | + this.annotationsJsonBytes = annotationsJson.getBytes(StandardCharsets.UTF_8); |
| 395 | + this.uuid = UUID.nameUUIDFromBytes(annotationsJsonBytes); |
| 396 | + } |
| 397 | + @NonNull |
| 398 | + @Override |
| 399 | + protected InputStream openInputStream() { |
| 400 | + return new ByteArrayInputStream(annotationsJsonBytes); |
| 401 | + } |
| 402 | + |
| 403 | + @Override |
| 404 | + public long getSize() { |
| 405 | + return annotationsJsonBytes.length; |
| 406 | + } |
| 407 | + |
| 408 | + @NonNull |
| 409 | + @Override |
| 410 | + public String getUid() { |
| 411 | + return String.format("document-instant-json-%s", uuid.toString()); |
| 412 | + } |
| 413 | + |
| 414 | + @Nullable |
| 415 | + @Override |
| 416 | + public String getTitle() { |
| 417 | + return null; |
| 418 | + } |
| 419 | + } |
356 | 420 | } |
0 commit comments