SkipAd handling pigeon, android, ios, flutter

This commit is contained in:
gianlucaparadise 2022-01-03 12:08:45 +01:00
parent b62174fba7
commit b22a3df404
9 changed files with 82 additions and 1 deletions

View file

@ -491,6 +491,7 @@ public class PlatformBridgeApis {
void pause();
void stop();
void showTracksChooserDialog();
void skipAd();
/** The codec used by CastHostApi. */
static MessageCodec<Object> getCodec() {
@ -704,6 +705,25 @@ public class PlatformBridgeApis {
channel.setMessageHandler(null);
}
}
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(binaryMessenger, "dev.flutter.pigeon.CastHostApi.skipAd", getCodec());
if (api != null) {
channel.setMessageHandler((message, reply) -> {
Map<String, Object> wrapped = new HashMap<>();
try {
api.skipAd();
wrapped.put("result", null);
}
catch (Error | RuntimeException exception) {
wrapped.put("error", wrapError(exception));
}
reply.reply(wrapped);
});
} else {
channel.setMessageHandler(null);
}
}
}
}
private static class CastFlutterApiCodec extends StandardMessageCodec {

View file

@ -336,6 +336,11 @@ class FlutterCastFrameworkPlugin : FlutterPlugin, MethodCallHandler, ActivityAwa
.show(activity.supportFragmentManager, "FlutterCastFrameworkTracksChooserDialog")
}
override fun skipAd() {
val remoteMediaClient: RemoteMediaClient = remoteMediaClient ?: return
remoteMediaClient.skipAd()
}
override fun setMute(muted: Boolean?) {
if (muted == null) return
val castSession = mCastSession ?: return

View file

@ -133,6 +133,7 @@ NSObject<FlutterMessageCodec> *CastHostApiGetCodec(void);
- (void)pauseWithError:(FlutterError *_Nullable *_Nonnull)error;
- (void)stopWithError:(FlutterError *_Nullable *_Nonnull)error;
- (void)showTracksChooserDialogWithError:(FlutterError *_Nullable *_Nonnull)error;
- (void)skipAdWithError:(FlutterError *_Nullable *_Nonnull)error;
@end
extern void CastHostApiSetup(id<FlutterBinaryMessenger> binaryMessenger, NSObject<CastHostApi> *_Nullable api);

View file

@ -540,6 +540,24 @@ void CastHostApiSetup(id<FlutterBinaryMessenger> binaryMessenger, NSObject<CastH
[channel setMessageHandler:nil];
}
}
{
FlutterBasicMessageChannel *channel =
[FlutterBasicMessageChannel
messageChannelWithName:@"dev.flutter.pigeon.CastHostApi.skipAd"
binaryMessenger:binaryMessenger
codec:CastHostApiGetCodec()];
if (api) {
NSCAssert([api respondsToSelector:@selector(skipAdWithError:)], @"CastHostApi api (%@) doesn't respond to @selector(skipAdWithError:)", api);
[channel setMessageHandler:^(id _Nullable message, FlutterReply callback) {
FlutterError *error;
[api skipAdWithError:&error];
callback(wrapResult(nil, error));
}];
}
else {
[channel setMessageHandler:nil];
}
}
}
@interface CastFlutterApiCodecReader : FlutterStandardReader
@end

View file

@ -323,6 +323,9 @@ public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessio
print("showTracksChooserDialog: unsupported feature")
}
public func skipAdWithError(_ error: AutoreleasingUnsafeMutablePointer<FlutterError?>) {
remoteMediaClient?.skipAd()
}
// MARK: - GCKSessionManagerListener
// onSessionSuspended

View file

@ -585,6 +585,29 @@ class CastHostApi {
return;
}
}
Future<void> skipAd() async {
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
'dev.flutter.pigeon.CastHostApi.skipAd', codec, binaryMessenger: _binaryMessenger);
final Map<Object?, Object?>? replyMap =
await channel.send(null) as Map<Object?, Object?>?;
if (replyMap == null) {
throw PlatformException(
code: 'channel-error',
message: 'Unable to establish connection on channel.',
details: null,
);
} else if (replyMap['error'] != null) {
final Map<Object?, Object?> error = (replyMap['error'] as Map<Object?, Object?>?)!;
throw PlatformException(
code: (error['code'] as String?)!,
message: error['message'] as String?,
details: error['details'],
);
} else {
return;
}
}
}
class _CastFlutterApiCodec extends StandardMessageCodec {

View file

@ -79,6 +79,11 @@ class RemoteMediaClient {
_hostApi.showTracksChooserDialog();
}
/// Skips the playing ad.
void skipAd() {
_hostApi.skipAd();
}
/// Internal method that shouldn't be visible
@internal
void dispatchPlayerStateUpdate(PlayerState playerState) {

View file

@ -137,6 +137,11 @@ class _ExpandedControlsState extends State<ExpandedControls> {
.updateProgress(progressMs, durationMs, whenSkippableMs);
}
void _onSkipAd() {
final sessionManager = widget.castFramework.castContext.sessionManager;
sessionManager.remoteMediaClient.skipAd();
}
Widget _getDecoratedToolbar(MediaInfo? mediaInfo) {
// Title and subtitle can't be retrieved at the moment
// final title = mediaInfo?.mediaMetadata?.strings[MediaMetadataKey.title]
@ -221,7 +226,7 @@ class _ExpandedControlsState extends State<ExpandedControls> {
controller: widget.adSkipBoxController,
skipAdButtonText: widget.skipAdButtonText,
skipAdTimerText: widget.skipAdTimerText,
onSkipPressed: () {/* TODO: skip ad */},
onSkipPressed: _onSkipAd,
),
];
}

View file

@ -267,6 +267,7 @@ abstract class CastHostApi {
void pause();
void stop();
void showTracksChooserDialog();
void skipAd();
//endregion
}