Skip to content

Commit

Permalink
updated Android PlatformPrintJobController.onComplete implementation,…
Browse files Browse the repository at this point in the history
… removed unused PlatformPrintJobControllerCreationParams.onComplete param
  • Loading branch information
pichillilorenzo committed Nov 30, 2024
2 parents 0687430 + 4f59192 commit 68aa382
Show file tree
Hide file tree
Showing 13 changed files with 196 additions and 30 deletions.
1 change: 1 addition & 0 deletions flutter_inappwebview/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#### Android Platform
- Implemented `saveState`, `restoreState` InAppWebViewController methods
- Merged "Android: implemented PlatformPrintJobController.onComplete" [#2216](https://github.com/pichillilorenzo/flutter_inappwebview/pull/2216) (thanks to [Doflatango](https://github.com/Doflatango))

#### macOS and iOS Platforms
- Implemented `saveState`, `restoreState` InAppWebViewController methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,19 @@ class _InAppWebViewExampleScreenState extends State<InAppWebViewExampleScreen> {

return NavigationActionPolicy.ALLOW;
},
onLoadStop: (controller, url) {
onLoadStop: (controller, url) async {
pullToRefreshController?.endRefreshing();
setState(() {
this.url = url.toString();
urlController.text = this.url;
});

final printJobController = await controller.printCurrentPage(settings: PrintJobSettings(handledByClient: true));
printJobController?.onComplete = (completed, error) async {
print('Print job completed: $completed');
print('Print job error: $error');
printJobController.dispose();
};
},
onReceivedError: (controller, request, error) {
pullToRefreshController?.endRefreshing();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import 'package:flutter_inappwebview_platform_interface/flutter_inappwebview_pla
///{@macro flutter_inappwebview_platform_interface.PlatformPrintJobController}
class PrintJobController {
///{@macro flutter_inappwebview_platform_interface.PlatformPrintJobController}
PrintJobController({required String id, PrintJobCompletionHandler onComplete})
PrintJobController({required String id})
: this.fromPlatformCreationParams(
params: PlatformPrintJobControllerCreationParams(
id: id, onComplete: onComplete));
params: PlatformPrintJobControllerCreationParams(id: id));

/// Constructs a [PrintJobController].
///
Expand Down
1 change: 1 addition & 0 deletions flutter_inappwebview_android/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- Updated flutter_inappwebview_platform_interface version to ^1.4.0-beta.3
- Implemented `saveState`, `restoreState` InAppWebViewController methods
- Merged "Android: implemented PlatformPrintJobController.onComplete" [#2216](https://github.com/pichillilorenzo/flutter_inappwebview/pull/2216) (thanks to [Doflatango](https://github.com/Doflatango))

## 1.2.0-beta.2

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package android.print;

import android.os.Bundle;
import android.os.CancellationSignal;
import android.os.ParcelFileDescriptor;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class InAppWebViewPrintDocumentAdapter extends PrintDocumentAdapter {
@NonNull
private final PrintDocumentAdapter delegate;
@Nullable
private final PrintDocumentAdapterCallback callback;

public InAppWebViewPrintDocumentAdapter(@NonNull PrintDocumentAdapter delegate, @Nullable PrintDocumentAdapterCallback callback) {
this.delegate = delegate;
this.callback = callback;
}

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

@Override
public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback layoutResultCallback, Bundle extras) {
this.delegate.onLayout(oldAttributes, newAttributes, cancellationSignal, new LayoutResultCallback() {
@Override
public void onLayoutFinished(PrintDocumentInfo info, boolean changed) {
layoutResultCallback.onLayoutFinished(info, changed);
if (callback != null) callback.onLayoutFinished(info, changed);
}

@Override
public void onLayoutFailed(CharSequence error) {
layoutResultCallback.onLayoutFailed(error);
if (callback != null) callback.onLayoutFailed(error);
}

@Override
public void onLayoutCancelled() {
layoutResultCallback.onLayoutCancelled();
if (callback != null) callback.onLayoutCancelled();
}
}, extras);

if (callback != null) callback.onLayout(oldAttributes, newAttributes, cancellationSignal, layoutResultCallback, extras);
}

@Override
public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback writeResultCallback) {
this.delegate.onWrite(pages, destination, cancellationSignal, new WriteResultCallback() {
@Override
public void onWriteFinished(PageRange[] pages) {
writeResultCallback.onWriteFinished(pages);
if (callback != null) callback.onWriteFinished(pages);
}

@Override
public void onWriteFailed(CharSequence error) {
writeResultCallback.onWriteFailed(error);
if (callback != null) callback.onWriteFailed(error);
}

@Override
public void onWriteCancelled() {
writeResultCallback.onWriteCancelled();
if (callback != null) callback.onWriteCancelled();
}
});
if (callback != null) callback.onWrite(pages, destination, cancellationSignal, writeResultCallback);
}

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

public static class PrintDocumentAdapterCallback {
public void onStart() {
/* do nothing - stub */
}

public void onFinish() {
/* do nothing - stub */
}

public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback layoutResultCallback, Bundle extras) {
/* do nothing - stub */
}

public void onLayoutFinished(PrintDocumentInfo info, boolean changed) {
/* do nothing - stub */
}

public void onLayoutFailed(CharSequence error) {
/* do nothing - stub */
}

public void onLayoutCancelled() {
/* do nothing - stub */
}

public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback writeResultCallback) {
/* do nothing - stub */
}

public void onWriteFinished(PageRange[] pages) {
/* do nothing - stub */
}

public void onWriteFailed(CharSequence error) {
/* do nothing - stub */
}

public void onWriteCancelled() {
/* do nothing - stub */
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import com.pichillilorenzo.flutter_inappwebview_android.types.ChannelDelegateImpl;
import com.pichillilorenzo.flutter_inappwebview_android.types.PrintJobInfoExt;

import java.util.HashMap;
import java.util.Map;

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

Expand Down Expand Up @@ -62,6 +65,15 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
}
}

public void onComplete(boolean completed, @Nullable String error) {
MethodChannel channel = getChannel();
if (channel == null) return;
Map<String, Object> obj = new HashMap<>();
obj.put("completed", completed);
obj.put("error", error);
channel.invokeMethod("onComplete", obj);
}

@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(boolean completed, @Nullable String error) {
if (channelDelegate != null) channelDelegate.onComplete(completed, error);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import android.os.Looper;
import android.os.Message;
import android.os.Parcel;
import android.print.InAppWebViewPrintDocumentAdapter;
import android.print.PrintAttributes;
import android.print.PrintDocumentAdapter;
import android.print.PrintJob;
import android.print.PrintManager;
import android.text.TextUtils;
import android.util.AttributeSet;
Expand Down Expand Up @@ -115,7 +117,7 @@
import io.flutter.plugin.common.MethodChannel;

final public class InAppWebView extends InputAwareWebView implements InAppWebViewInterface {
protected static final String LOG_TAG = "InAppWebView";
private static final String LOG_TAG = "InAppWebView";
public static final String METHOD_CHANNEL_NAME_PREFIX = "com.pichillilorenzo/flutter_inappwebview_";

@Nullable
Expand Down Expand Up @@ -1445,7 +1447,6 @@ public void setDesktopMode(final boolean enabled) {
webSettings.setBuiltInZoomControls(enabled);
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Nullable
public String printCurrentPage(@Nullable PrintJobSettings settings) {
if (plugin != null && plugin.activity != null) {
Expand Down Expand Up @@ -1500,15 +1501,27 @@ 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;
final PrintJobController finalPrintJobController = printJobController;
printAdapter = new InAppWebViewPrintDocumentAdapter(printAdapter, new InAppWebViewPrintDocumentAdapter.PrintDocumentAdapterCallback() {
@Override
public void onFinish() {
finalPrintJobController.onComplete(true, null);
}
});
}

// Create a printCurrentPage job with name and adapter instance
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 @@ -12,15 +12,15 @@ class AndroidPrintJobControllerCreationParams
extends PlatformPrintJobControllerCreationParams {
/// Creates a new [AndroidPrintJobControllerCreationParams] instance.
const AndroidPrintJobControllerCreationParams(
{required super.id, super.onComplete});
{required super.id});

/// Creates a [AndroidPrintJobControllerCreationParams] instance based on [PlatformPrintJobControllerCreationParams].
factory AndroidPrintJobControllerCreationParams.fromPlatformPrintJobControllerCreationParams(
// Recommended placeholder to prevent being broken by platform interface.
// ignore: avoid_unused_constructor_parameters
PlatformPrintJobControllerCreationParams params) {
return AndroidPrintJobControllerCreationParams(
id: params.id, onComplete: params.onComplete);
id: params.id);
}
}

Expand All @@ -43,6 +43,11 @@ class AndroidPrintJobController extends PlatformPrintJobController

Future<dynamic> _handleMethod(MethodCall call) async {
switch (call.method) {
case "onComplete":
bool completed = call.arguments["completed"];
String? error = call.arguments["error"];
onComplete?.call(completed, error);
break;
default:
throw UnimplementedError("Unimplemented ${call.method} method");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ class IOSPrintJobControllerCreationParams
extends PlatformPrintJobControllerCreationParams {
/// Creates a new [IOSPrintJobControllerCreationParams] instance.
const IOSPrintJobControllerCreationParams(
{required super.id, super.onComplete});
{required super.id});

/// Creates a [IOSPrintJobControllerCreationParams] instance based on [PlatformPrintJobControllerCreationParams].
factory IOSPrintJobControllerCreationParams.fromPlatformPrintJobControllerCreationParams(
// Recommended placeholder to prevent being broken by platform interface.
// ignore: avoid_unused_constructor_parameters
PlatformPrintJobControllerCreationParams params) {
return IOSPrintJobControllerCreationParams(
id: params.id, onComplete: params.onComplete);
id: params.id);
}
}

Expand All @@ -35,7 +35,6 @@ class IOSPrintJobController extends PlatformPrintJobController
: IOSPrintJobControllerCreationParams
.fromPlatformPrintJobControllerCreationParams(params),
) {
onComplete = params.onComplete;
channel = MethodChannel(
'com.pichillilorenzo/flutter_inappwebview_printjobcontroller_${params.id}');
handler = _handleMethod;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ class MacOSPrintJobControllerCreationParams
extends PlatformPrintJobControllerCreationParams {
/// Creates a new [MacOSPrintJobControllerCreationParams] instance.
const MacOSPrintJobControllerCreationParams(
{required super.id, super.onComplete});
{required super.id});

/// Creates a [MacOSPrintJobControllerCreationParams] instance based on [PlatformPrintJobControllerCreationParams].
factory MacOSPrintJobControllerCreationParams.fromPlatformPrintJobControllerCreationParams(
// Recommended placeholder to prevent being broken by platform interface.
// ignore: avoid_unused_constructor_parameters
PlatformPrintJobControllerCreationParams params) {
return MacOSPrintJobControllerCreationParams(
id: params.id, onComplete: params.onComplete);
id: params.id);
}
}

Expand All @@ -35,7 +35,6 @@ class MacOSPrintJobController extends PlatformPrintJobController
: MacOSPrintJobControllerCreationParams
.fromPlatformPrintJobControllerCreationParams(params),
) {
onComplete = params.onComplete;
channel = MethodChannel(
'com.pichillilorenzo/flutter_inappwebview_printjobcontroller_${params.id}');
handler = _handleMethod;
Expand Down
Loading

0 comments on commit 68aa382

Please sign in to comment.