diff --git a/packages/webview_flutter/webview_flutter_ohos/lib/src/ohos_webview.dart b/packages/webview_flutter/webview_flutter_ohos/lib/src/ohos_webview.dart index 551b5b545d59a85455a1ce9484eb81434057a446..df31d4fc760fef096ab77d940d229a7b79f2ecd4 100644 --- a/packages/webview_flutter/webview_flutter_ohos/lib/src/ohos_webview.dart +++ b/packages/webview_flutter/webview_flutter_ohos/lib/src/ohos_webview.dart @@ -717,6 +717,11 @@ class WebSettings extends OhosObject { return api.getUserAgentStringFromInstance(this); } + /// Enables or disables full screen rotate within WebView. + Future setAllowFullScreenRotate(bool enabled) { + return api.setAllowFullScreenRotateInstance(this, enabled); + } + @override WebSettings copy() { return WebSettings.detached( diff --git a/packages/webview_flutter/webview_flutter_ohos/lib/src/ohos_webview.g.dart b/packages/webview_flutter/webview_flutter_ohos/lib/src/ohos_webview.g.dart index fbae7a0dc7c50a1bf5ba4babf7d1b22a569898b9..9b410164585bcb99b9fa02ae789bad8daefe69eb 100644 --- a/packages/webview_flutter/webview_flutter_ohos/lib/src/ohos_webview.g.dart +++ b/packages/webview_flutter/webview_flutter_ohos/lib/src/ohos_webview.g.dart @@ -1552,6 +1552,29 @@ class WebSettingsHostApi { return (replyList[0] as String?)!; } } + + Future setAllowFullScreenRotate(int arg_instanceId, bool arg_enabled) async { + final BasicMessageChannel channel = BasicMessageChannel( + 'dev.flutter.pigeon.webview_flutter_ohos.WebSettingsHostApi.setAllowFullScreenRotate', + codec, + binaryMessenger: _binaryMessenger); + final List? replyList = await channel + .send([arg_instanceId, arg_enabled]) as List?; + if (replyList == null) { + throw PlatformException( + code: 'channel-error', + message: 'Unable to establish connection on channel.', + ); + } else if (replyList.length > 1) { + throw PlatformException( + code: replyList[0]! as String, + message: replyList[1] as String?, + details: replyList[2], + ); + } else { + return; + } + } } class JavaScriptChannelHostApi { diff --git a/packages/webview_flutter/webview_flutter_ohos/lib/src/ohos_webview_api_impls.dart b/packages/webview_flutter/webview_flutter_ohos/lib/src/ohos_webview_api_impls.dart index 93b896ab79fb293348b6f7e5091fc8a15685ff96..0a0935dc42dbb3f2db0bef0f1d95b5cf2b251cf6 100644 --- a/packages/webview_flutter/webview_flutter_ohos/lib/src/ohos_webview_api_impls.dart +++ b/packages/webview_flutter/webview_flutter_ohos/lib/src/ohos_webview_api_impls.dart @@ -588,6 +588,17 @@ class WebSettingsHostApiImpl extends WebSettingsHostApi { Future getUserAgentStringFromInstance(WebSettings instance) { return getUserAgentString(instanceManager.getIdentifier(instance)!); } + + /// Helper method to convert instances ids to objects. + Future setAllowFullScreenRotateInstance( + WebSettings instance, + bool enabled, + ) { + return setAllowFullScreenRotate( + instanceManager.getIdentifier(instance)!, + enabled, + ); + } } /// Host api implementation for [JavaScriptChannel]. diff --git a/packages/webview_flutter/webview_flutter_ohos/lib/src/ohos_webview_controller.dart b/packages/webview_flutter/webview_flutter_ohos/lib/src/ohos_webview_controller.dart index e33a54b475aa4b6171bec1b4f38705471ead2bf3..79ddbe0a88724ba3534bd0eed42d6588153f42e0 100644 --- a/packages/webview_flutter/webview_flutter_ohos/lib/src/ohos_webview_controller.dart +++ b/packages/webview_flutter/webview_flutter_ohos/lib/src/ohos_webview_controller.dart @@ -39,6 +39,7 @@ class OhosWebViewControllerCreationParams extends PlatformWebViewControllerCreationParams { /// Creates a new [OhosWebViewControllerCreationParams] instance. OhosWebViewControllerCreationParams({ + bool? this.isAllowFullScreenRotate = false, @visibleForTesting this.ohosWebViewProxy = const OhosWebViewProxy(), @visibleForTesting ohos_webview.WebStorage? ohosWebStorage, }) : ohosWebStorage = @@ -50,17 +51,22 @@ class OhosWebViewControllerCreationParams // Recommended placeholder to prevent being broken by platform interface. // ignore: avoid_unused_constructor_parameters PlatformWebViewControllerCreationParams params, { + bool? isAllowFullScreenRotate = false, @visibleForTesting OhosWebViewProxy ohosWebViewProxy = const OhosWebViewProxy(), @visibleForTesting ohos_webview.WebStorage? ohosWebStorage, }) { return OhosWebViewControllerCreationParams( + isAllowFullScreenRotate: isAllowFullScreenRotate, ohosWebViewProxy: ohosWebViewProxy, ohosWebStorage: ohosWebStorage ?? ohos_webview.WebStorage.instance, ); } + /// Enables or disables full screen rotate within WebView. + bool? isAllowFullScreenRotate; + /// Handles constructing objects and calling static methods for the Ohos WebView /// native library. @visibleForTesting @@ -94,6 +100,7 @@ class OhosWebViewController extends PlatformWebViewController { ? params : OhosWebViewControllerCreationParams .fromPlatformWebViewControllerCreationParams(params)) { + _webView.settings.setAllowFullScreenRotate(params is OhosWebViewControllerCreationParams ? params.isAllowFullScreenRotate ?? false : false); _webView.settings.setDomStorageEnabled(true); _webView.settings.setJavaScriptCanOpenWindowsAutomatically(true); _webView.settings.setSupportMultipleWindows(true); diff --git a/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/GeneratedOhosWebView.ets b/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/GeneratedOhosWebView.ets index afe1c6843046862df31cc504e3159fcad75102fd..11720b2ffaf1235a547aff3fe244e976ea54e0a4 100644 --- a/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/GeneratedOhosWebView.ets +++ b/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/GeneratedOhosWebView.ets @@ -2188,6 +2188,8 @@ export abstract class WebSettingsHostApi { abstract getUserAgentString(instanceId: number): Promise; + abstract setAllowFullScreenRotate(instanceId: number, enabled: boolean): void; + static getCodec(): MessageCodec { return new StandardMessageCodec(); } @@ -2699,6 +2701,38 @@ export abstract class WebSettingsHostApi { channel.setMessageHandler(null); } } + { + const channel: BasicMessageChannel = + new BasicMessageChannel( + binaryMessenger, + "dev.flutter.pigeon.webview_flutter_ohos.WebSettingsHostApi.setAllowFullScreenRotate", + WebSettingsHostApi.getCodec() + ); + if (api != null) { + channel.setMessageHandler( + { + onMessage: (message: ESObject, reply: Reply) => { + let wrapped: ArrayList = new ArrayList(); + const args: Array = message; + const instanceIdArg: number = args[0]; + const enabledArg: boolean = args[1]; + try { + api.setAllowFullScreenRotate( + instanceIdArg, enabledArg + ); + wrapped.insert(null, 0); + } catch (err) { + const wrappedError: ArrayList = GeneratedOhosWebView.wrapError(err); + wrapped = wrappedError; + } + reply.reply(wrapped.convertToArray()); + } + } + ); + } else { + channel.setMessageHandler(null); + } + } } } diff --git a/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/OhosWebView.ets b/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/OhosWebView.ets index bab0850e763ca6ba8e127e3b10d642ec9e3fb7e0..834b83e8bf06ea088b46a824a384c51ee49b3c4a 100644 --- a/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/OhosWebView.ets +++ b/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/OhosWebView.ets @@ -94,6 +94,8 @@ export struct OhosWebView { } }) ) + .onFullScreenEnter(this.webView.onFullScreenEnter) + .onFullScreenExit(this.webView.onFullScreenExit) } } } diff --git a/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/WebSettingsHostApiImpl.ets b/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/WebSettingsHostApiImpl.ets index cc6f5912e1cfc79912eeea789c78e39ee2820d35..3719baa31d31ecd5aa5628d7112449da374e88a8 100644 --- a/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/WebSettingsHostApiImpl.ets +++ b/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/WebSettingsHostApiImpl.ets @@ -68,10 +68,13 @@ export interface WebSettings { getTextZoom(): number; - getUserAgentString(): Promise; onTextZoomChanged(callback: (ratio: number) => void): void; + + setAllowFullScreenRotate(enabled: boolean): void; + + getAllowFullScreenRotate(): boolean; } export class WebSettingsCreator { @@ -170,4 +173,9 @@ export class WebSettingsHostApiImpl extends WebSettingsHostApi { const webSettings:WebSettings = this.instanceManager.getInstance(instanceId); return webSettings.getUserAgentString(); } + + setAllowFullScreenRotate(instanceId: number, enabled: boolean) { + const webSettings:WebSettings = this.instanceManager.getInstance(instanceId); + webSettings.setAllowFullScreenRotate(enabled); + } } \ No newline at end of file diff --git a/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/WebViewHostApiImpl.ets b/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/WebViewHostApiImpl.ets index 86c941e04c3629e2a5803265e0aa04fef44d8e9d..b179e03c5a3d43b51bd8122599e8782d301f56d2 100644 --- a/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/WebViewHostApiImpl.ets +++ b/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/WebViewHostApiImpl.ets @@ -32,6 +32,8 @@ import { DisplayListenerProxy } from './DisplayListenerProxy' import { JavaScriptChannel } from './JavaScriptChannel' import Log from '@ohos/flutter_ohos/src/main/ets/util/Log'; import { WebViewFlutterApiImpl } from './WebViewFlutterApiImpl.ets' +import window from '@ohos.window' +import display from '@ohos.display' const TAG: string = "WebViewHostApiImpl"; @@ -299,6 +301,8 @@ export class WebViewPlatformView extends PlatformView { private onDisposeCallback?: () => void; + private rotation: number = 0; + onDownloadStart = (event: ESObject) => { if (this.downloadListener && this.downloadListener.onDownloadStart) { this.downloadListener.onDownloadStart( @@ -436,6 +440,25 @@ export class WebViewPlatformView extends PlatformView { return true; } + onFullScreenEnter = (event: ESObject) => { + let context = getContext(this) as common.UIAbilityContext; + let windowClass = context.windowStage.getMainWindowSync(); + windowClass.setWindowSystemBarEnable([]); + this.rotation = windowClass.getPreferredOrientation(); + let orientation: number = display.getDefaultDisplaySync().orientation; + let isAllowFullScreenRotate = this.getWebSettings()?.getAllowFullScreenRotate(); + if (isAllowFullScreenRotate && (orientation == display.Orientation.PORTRAIT || orientation == display.Orientation.PORTRAIT_INVERTED)) { + windowClass.setPreferredOrientation(window.Orientation.LANDSCAPE); + } + } + + onFullScreenExit = () => { + let context = getContext(this) as common.UIAbilityContext; + let windowClass = context.windowStage.getMainWindowSync(); + windowClass.setWindowSystemBarEnable(['status', 'navigation']); + windowClass.setPreferredOrientation(this.rotation); + } + getType(): string { return 'web'; } @@ -552,6 +575,7 @@ class WebSettingsImpl implements WebSettings { fileAccess : boolean = true; textZoomRatio : number = 0; textZoomRatioCallback?: (ratio: number) => void; + isAllowFullScreenRotate: boolean = false; constructor(webView: WebViewPlatformView) { this.webView = webView; @@ -671,4 +695,11 @@ class WebSettingsImpl implements WebSettings { this.textZoomRatioCallback = callback; } + setAllowFullScreenRotate(enabled: boolean) { + this.isAllowFullScreenRotate = enabled; + } + + getAllowFullScreenRotate() : boolean { + return this.isAllowFullScreenRotate; + } } \ No newline at end of file