Pigeon: Integrated getSessionMessageNamespaces API
This commit is contained in:
parent
41dd086a38
commit
df223cb80d
6 changed files with 35 additions and 50 deletions
|
|
@ -57,6 +57,8 @@ class FlutterCastFrameworkPlugin : FlutterPlugin, MethodCallHandler, ActivityAwa
|
||||||
castApi = MyApi()
|
castApi = MyApi()
|
||||||
HostApis.CastApi.setup(messenger, castApi)
|
HostApis.CastApi.setup(messenger, castApi)
|
||||||
|
|
||||||
|
flutterApi = HostApis.CastFlutterApi(messenger)
|
||||||
|
|
||||||
CastContext.getSharedInstance(applicationContext).addCastStateListener { i ->
|
CastContext.getSharedInstance(applicationContext).addCastStateListener { i ->
|
||||||
Log.d(TAG, "Cast state changed: $i")
|
Log.d(TAG, "Cast state changed: $i")
|
||||||
methodChannel.invokeMethod(MethodNames.onCastStateChanged, i)
|
methodChannel.invokeMethod(MethodNames.onCastStateChanged, i)
|
||||||
|
|
@ -102,6 +104,7 @@ class FlutterCastFrameworkPlugin : FlutterPlugin, MethodCallHandler, ActivityAwa
|
||||||
|
|
||||||
private var channel: MethodChannel? = null
|
private var channel: MethodChannel? = null
|
||||||
private var castApi : HostApis.CastApi? = null
|
private var castApi : HostApis.CastApi? = null
|
||||||
|
private var flutterApi: HostApis.CastFlutterApi? = null
|
||||||
private var applicationContext: Context? = null
|
private var applicationContext: Context? = null
|
||||||
private var activity: Activity? = null
|
private var activity: Activity? = null
|
||||||
|
|
||||||
|
|
@ -112,11 +115,10 @@ class FlutterCastFrameworkPlugin : FlutterPlugin, MethodCallHandler, ActivityAwa
|
||||||
Log.d(TAG, "Updating mCastSession - castSession changed: ${field != value}")
|
Log.d(TAG, "Updating mCastSession - castSession changed: ${field != value}")
|
||||||
// if (field == value) return // Despite the instances are the same, I need to re-attach the listener to every new session instance
|
// if (field == value) return // Despite the instances are the same, I need to re-attach the listener to every new session instance
|
||||||
|
|
||||||
val result = NamespaceResult(oldSession = field, newSession = value)
|
val oldSession = field
|
||||||
|
|
||||||
field = value
|
field = value
|
||||||
|
|
||||||
channel?.invokeMethod(MethodNames.getSessionMessageNamespaces, null, result)
|
flutterApi?.getSessionMessageNamespaces(getOnNamespaceResult(oldSession, newSession = value))
|
||||||
}
|
}
|
||||||
|
|
||||||
//region LifecycleObserver
|
//region LifecycleObserver
|
||||||
|
|
@ -174,36 +176,17 @@ class FlutterCastFrameworkPlugin : FlutterPlugin, MethodCallHandler, ActivityAwa
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private inner class NamespaceResult(val oldSession: CastSession?, val newSession: CastSession?) : Result {
|
private fun getOnNamespaceResult(oldSession: CastSession?, newSession: CastSession?) = HostApis.CastFlutterApi.Reply<MutableList<String>> { namespaces ->
|
||||||
override fun notImplemented() {
|
Log.d(TAG, "Updating mCastSession - getOnNamespaceResult - param: $namespaces")
|
||||||
Log.d(TAG, "Updating mCastSession - notImplemented")
|
if (oldSession == null && newSession == null) return@Reply // nothing to do here
|
||||||
}
|
if (namespaces == null || !namespaces.any()) return@Reply // nothing to do here
|
||||||
|
|
||||||
override fun error(p0: String?, p1: String?, p2: Any?) {
|
namespaces.forEach { namespace ->
|
||||||
Log.d(TAG, "Updating mCastSession - error - $p0 $p1 $p2")
|
try {
|
||||||
}
|
oldSession?.removeMessageReceivedCallbacks(namespace)
|
||||||
|
newSession?.setMessageReceivedCallbacks(namespace, mMessageCastingChannel)
|
||||||
override fun success(args: Any?) {
|
} catch (e: java.lang.Exception) {
|
||||||
Log.d(TAG, "Updating mCastSession - success - param: $args")
|
Log.e(TAG, "Updating mCastSession - Exception while creating channel", e)
|
||||||
if (oldSession == null && newSession == null) return // nothing to do here
|
|
||||||
if (args == null) return // nothing to do here
|
|
||||||
|
|
||||||
if (args !is ArrayList<*>)
|
|
||||||
throw IllegalArgumentException("${MethodNames.getSessionMessageNamespaces} method expects an ArrayList<String>")
|
|
||||||
|
|
||||||
if (!args.any()) return // nothing to do here
|
|
||||||
|
|
||||||
if (args[0] !is String)
|
|
||||||
throw IllegalArgumentException("${MethodNames.getSessionMessageNamespaces} method expects an ArrayList<String>")
|
|
||||||
|
|
||||||
val namespaces = args as ArrayList<String>
|
|
||||||
namespaces.forEach {
|
|
||||||
try {
|
|
||||||
oldSession?.removeMessageReceivedCallbacks(it)
|
|
||||||
newSession?.setMessageReceivedCallbacks(it, mMessageCastingChannel)
|
|
||||||
} catch (e: java.lang.Exception) {
|
|
||||||
Log.e(TAG, "Updating mCastSession - Exception while creating channel", e)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,5 @@ object MethodNames {
|
||||||
const val onSessionSuspended = "SessionManager.onSessionSuspended"
|
const val onSessionSuspended = "SessionManager.onSessionSuspended"
|
||||||
// end-region
|
// end-region
|
||||||
|
|
||||||
const val getSessionMessageNamespaces = "CastSession.getSessionMessageNamespaces"
|
|
||||||
const val onMessageReceived = "CastSession.onMessageReceived"
|
const val onMessageReceived = "CastSession.onMessageReceived"
|
||||||
}
|
}
|
||||||
|
|
@ -22,6 +22,5 @@ enum MethodNames : String {
|
||||||
case onSessionSuspended = "SessionManager.onSessionSuspended"
|
case onSessionSuspended = "SessionManager.onSessionSuspended"
|
||||||
// end-region
|
// end-region
|
||||||
|
|
||||||
case getSessionMessageNamespaces = "CastSession.getSessionMessageNamespaces"
|
|
||||||
case onMessageReceived = "CastSession.onMessageReceived"
|
case onMessageReceived = "CastSession.onMessageReceived"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,11 @@ import GoogleCast
|
||||||
public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessionManagerListener, CastApi {
|
public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessionManagerListener, CastApi {
|
||||||
public static func register(with registrar: FlutterPluginRegistrar) {
|
public static func register(with registrar: FlutterPluginRegistrar) {
|
||||||
let messenger : FlutterBinaryMessenger = registrar.messenger()
|
let messenger : FlutterBinaryMessenger = registrar.messenger()
|
||||||
|
|
||||||
let channel = FlutterMethodChannel(name: "flutter_cast_framework", binaryMessenger: messenger)
|
let channel = FlutterMethodChannel(name: "flutter_cast_framework", binaryMessenger: messenger)
|
||||||
let instance = SwiftFlutterCastFrameworkPlugin(channel: channel)
|
let flutterApi = CastFlutterApi.init(binaryMessenger: messenger)
|
||||||
|
|
||||||
|
let instance = SwiftFlutterCastFrameworkPlugin(channel: channel, flutterApi: flutterApi)
|
||||||
registrar.addMethodCallDelegate(instance, channel: channel)
|
registrar.addMethodCallDelegate(instance, channel: channel)
|
||||||
|
|
||||||
let api : CastApi & NSObjectProtocol = instance
|
let api : CastApi & NSObjectProtocol = instance
|
||||||
|
|
@ -16,6 +19,7 @@ public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessio
|
||||||
private let castContext: GCKCastContext
|
private let castContext: GCKCastContext
|
||||||
private var castStateObserver: NSKeyValueObservation?
|
private var castStateObserver: NSKeyValueObservation?
|
||||||
private let channel: FlutterMethodChannel
|
private let channel: FlutterMethodChannel
|
||||||
|
private let flutterApi : CastFlutterApi
|
||||||
|
|
||||||
private let sessionManager: GCKSessionManager
|
private let sessionManager: GCKSessionManager
|
||||||
|
|
||||||
|
|
@ -32,14 +36,13 @@ public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessio
|
||||||
|
|
||||||
_castSession = newValue
|
_castSession = newValue
|
||||||
|
|
||||||
channel.invokeMethod(MethodNames.getSessionMessageNamespaces.rawValue, arguments: nil) { (args) in
|
flutterApi.getSessionMessageNamespaces { (namespaces, err) in
|
||||||
print("Updating castSession - success - param: \(args ?? "-")")
|
print("Updating castSession - getSessionMessageNamespaces success - param: \(namespaces.joined(separator: ", "))")
|
||||||
if (oldSession == nil && newSession == nil) {
|
if (oldSession == nil && newSession == nil) {
|
||||||
return // nothing to do here
|
return // nothing to do here
|
||||||
}
|
}
|
||||||
|
|
||||||
let namespaces = args as? NSArray
|
if (namespaces.count == 0) {
|
||||||
if (namespaces == nil || namespaces?.count == 0) {
|
|
||||||
return // nothing to do here
|
return // nothing to do here
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -50,12 +53,7 @@ public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessio
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
namespaces?.forEach({ (namespaceRaw) in
|
namespaces.forEach({ (namespace) in
|
||||||
if (!(namespaceRaw is String)) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
let namespace = namespaceRaw as! String
|
|
||||||
let castingChannel = MessageCastingChannel.init(namespace: namespace, channel: self.channel)
|
let castingChannel = MessageCastingChannel.init(namespace: namespace, channel: self.channel)
|
||||||
self.castingChannels[namespace] = castingChannel
|
self.castingChannels[namespace] = castingChannel
|
||||||
newSession?.add(castingChannel)
|
newSession?.add(castingChannel)
|
||||||
|
|
@ -64,11 +62,13 @@ public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessio
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
init(channel: FlutterMethodChannel) {
|
init(channel: FlutterMethodChannel, flutterApi : CastFlutterApi) {
|
||||||
self.channel = channel
|
self.channel = channel
|
||||||
self.castContext = GCKCastContext.sharedInstance()
|
self.castContext = GCKCastContext.sharedInstance()
|
||||||
self.sessionManager = GCKCastContext.sharedInstance().sessionManager
|
self.sessionManager = GCKCastContext.sharedInstance().sessionManager
|
||||||
|
|
||||||
|
self.flutterApi = flutterApi
|
||||||
|
|
||||||
super.init()
|
super.init()
|
||||||
|
|
||||||
self.castSession = GCKCastContext.sharedInstance().sessionManager.currentCastSession
|
self.castSession = GCKCastContext.sharedInstance().sessionManager.currentCastSession
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,5 @@ class PlatformMethodNames {
|
||||||
static const onSessionResumeFailed = "SessionManager.onSessionResumeFailed";
|
static const onSessionResumeFailed = "SessionManager.onSessionResumeFailed";
|
||||||
static const onSessionSuspended = "SessionManager.onSessionSuspended";
|
static const onSessionSuspended = "SessionManager.onSessionSuspended";
|
||||||
|
|
||||||
static const getSessionMessageNamespaces = "CastSession.getSessionMessageNamespaces";
|
|
||||||
static const onMessageReceived = "CastSession.onMessageReceived";
|
static const onMessageReceived = "CastSession.onMessageReceived";
|
||||||
}
|
}
|
||||||
|
|
@ -16,6 +16,7 @@ class FlutterCastFramework {
|
||||||
static bool _isInitiated = false;
|
static bool _isInitiated = false;
|
||||||
|
|
||||||
static _init() {
|
static _init() {
|
||||||
|
CastFlutterApi.setup(CastFlutterApiImpl());
|
||||||
_channel.setMethodCallHandler((MethodCall call) async {
|
_channel.setMethodCallHandler((MethodCall call) async {
|
||||||
String method = call.method;
|
String method = call.method;
|
||||||
dynamic arguments = call.arguments;
|
dynamic arguments = call.arguments;
|
||||||
|
|
@ -38,9 +39,6 @@ class FlutterCastFramework {
|
||||||
castContext.sessionManager.onSessionStateChanged(method, arguments);
|
castContext.sessionManager.onSessionStateChanged(method, arguments);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case PlatformMethodNames.getSessionMessageNamespaces:
|
|
||||||
return namespaces;
|
|
||||||
|
|
||||||
case PlatformMethodNames.onMessageReceived:
|
case PlatformMethodNames.onMessageReceived:
|
||||||
castContext.sessionManager.platformOnMessageReceived(arguments);
|
castContext.sessionManager.platformOnMessageReceived(arguments);
|
||||||
break;
|
break;
|
||||||
|
|
@ -68,3 +66,10 @@ class FlutterCastFramework {
|
||||||
return castContext;
|
return castContext;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class CastFlutterApiImpl extends CastFlutterApi {
|
||||||
|
@override
|
||||||
|
List<String?> getSessionMessageNamespaces() {
|
||||||
|
return FlutterCastFramework.namespaces;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue