Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android: implemented PlatformPrintJobController.onComplete #2216

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
}
}

public void onComplete() {
getChannel().invokeMethod("onComplete", null);
}

@Override
public void dispose() {
super.dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,19 @@ public class PrintJobController implements Disposable {
@Nullable
public PrintJobSettings settings;

public PrintJobController(@NonNull String id, @NonNull android.print.PrintJob job,
@Nullable PrintJobSettings settings, @NonNull InAppWebViewFlutterPlugin plugin) {
public PrintJobController(@NonNull String id, @Nullable PrintJobSettings settings,
@NonNull InAppWebViewFlutterPlugin plugin) {
this.id = id;
this.plugin = plugin;
this.job = job;
this.settings = settings;
final MethodChannel channel = new MethodChannel(plugin.messenger, METHOD_CHANNEL_NAME_PREFIX + id);
this.channelDelegate = new PrintJobChannelDelegate(this, channel);
}


public void setJob(@Nullable android.print.PrintJob job) {
this.job = job;
}

public void cancel() {
if (this.job != null) {
this.job.cancel();
Expand Down Expand Up @@ -93,4 +96,8 @@ public void dispose() {
}
plugin = null;
}

public void onComplete() {
if (channelDelegate != null) channelDelegate.onComplete();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.ParcelFileDescriptor;
import android.print.PageRange;
import android.print.PrintAttributes;
import android.print.PrintDocumentAdapter;
import android.print.PrintManager;
Expand Down Expand Up @@ -1403,6 +1406,37 @@ public void setDesktopMode(final boolean enabled) {
webSettings.setBuiltInZoomControls(enabled);
}

interface PrintDocumentAdapterWrapperCallback {
void onFinish();
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
class PrintDocumentAdapterWrapper extends PrintDocumentAdapter {
@NonNull PrintDocumentAdapter delegate;
@Nullable PrintDocumentAdapterWrapperCallback callback;

PrintDocumentAdapterWrapper(@NonNull PrintDocumentAdapter delegate, @Nullable PrintDocumentAdapterWrapperCallback callback) {
this.delegate = delegate;
this.callback = callback;
}

@Override
public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) {
this.delegate.onLayout(oldAttributes, newAttributes, cancellationSignal, callback, extras);
}

@Override
public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback) {
this.delegate.onWrite(pages, destination, cancellationSignal, callback);
}

@Override
public void onFinish() {
this.delegate.onFinish();
if (this.callback != null) this.callback.onFinish();
}
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Nullable
public String printCurrentPage(@Nullable PrintJobSettings settings) {
Expand Down Expand Up @@ -1458,15 +1492,21 @@ public String printCurrentPage(@Nullable PrintJobSettings settings) {
printAdapter = createPrintDocumentAdapter();
}

// Create a printCurrentPage job with name and adapter instance
android.print.PrintJob job = printManager.print(jobName, printAdapter, builder.build());
PrintJobController printJobController = null;
String id = null;

if (settings != null && settings.handledByClient && plugin.printJobManager != null) {
String id = UUID.randomUUID().toString();
PrintJobController printJobController = new PrintJobController(id, job, settings, plugin);
id = UUID.randomUUID().toString();
printJobController = new PrintJobController(id, settings, plugin);
plugin.printJobManager.jobs.put(printJobController.id, printJobController);
return id;
printAdapter = new PrintDocumentAdapterWrapper(printAdapter, printJobController::onComplete);
}

// Create a printCurrentPage job with name and adapter instance
android.print.PrintJob job = printManager.print(jobName, printAdapter, builder.build());
if (printJobController != null) printJobController.setJob(job);

return id;
} else {
Log.e(LOG_TAG, "No PrintManager available");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class AndroidPrintJobController extends PlatformPrintJobController

Future<dynamic> _handleMethod(MethodCall call) async {
switch (call.method) {
case "onComplete":
onComplete?.call(true, null);
break;
default:
throw UnimplementedError("Unimplemented ${call.method} method");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ abstract class PlatformPrintJobController extends PlatformInterface
///A completion handler used to handle the conclusion of the print job (for instance, to reset state) and to handle any errors encountered in printing.
///
///**Officially Supported Platforms/Implementations**:
///- Android ([Official API - PrintDocumentAdapter.onFinish](https://developer.android.com/reference/android/print/PrintDocumentAdapter#onFinish()))
///- iOS ([Official API - UIPrintInteractionController.CompletionHandler](https://developer.apple.com/documentation/uikit/uiprintinteractioncontroller/completionhandler))
///- MacOS ([Official API - NSPrintOperation.runModal](https://developer.apple.com/documentation/appkit/nsprintoperation/1532065-runmodal))
///{@endtemplate}
Expand Down
Loading