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()
|
||||
HostApis.CastApi.setup(messenger, castApi)
|
||||
|
||||
flutterApi = HostApis.CastFlutterApi(messenger)
|
||||
|
||||
CastContext.getSharedInstance(applicationContext).addCastStateListener { i ->
|
||||
Log.d(TAG, "Cast state changed: $i")
|
||||
methodChannel.invokeMethod(MethodNames.onCastStateChanged, i)
|
||||
|
|
@ -102,6 +104,7 @@ class FlutterCastFrameworkPlugin : FlutterPlugin, MethodCallHandler, ActivityAwa
|
|||
|
||||
private var channel: MethodChannel? = null
|
||||
private var castApi : HostApis.CastApi? = null
|
||||
private var flutterApi: HostApis.CastFlutterApi? = null
|
||||
private var applicationContext: Context? = null
|
||||
private var activity: Activity? = null
|
||||
|
||||
|
|
@ -112,11 +115,10 @@ class FlutterCastFrameworkPlugin : FlutterPlugin, MethodCallHandler, ActivityAwa
|
|||
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
|
||||
|
||||
val result = NamespaceResult(oldSession = field, newSession = value)
|
||||
|
||||
val oldSession = field
|
||||
field = value
|
||||
|
||||
channel?.invokeMethod(MethodNames.getSessionMessageNamespaces, null, result)
|
||||
flutterApi?.getSessionMessageNamespaces(getOnNamespaceResult(oldSession, newSession = value))
|
||||
}
|
||||
|
||||
//region LifecycleObserver
|
||||
|
|
@ -174,36 +176,17 @@ class FlutterCastFrameworkPlugin : FlutterPlugin, MethodCallHandler, ActivityAwa
|
|||
}
|
||||
}
|
||||
|
||||
private inner class NamespaceResult(val oldSession: CastSession?, val newSession: CastSession?) : Result {
|
||||
override fun notImplemented() {
|
||||
Log.d(TAG, "Updating mCastSession - notImplemented")
|
||||
}
|
||||
private fun getOnNamespaceResult(oldSession: CastSession?, newSession: CastSession?) = HostApis.CastFlutterApi.Reply<MutableList<String>> { namespaces ->
|
||||
Log.d(TAG, "Updating mCastSession - getOnNamespaceResult - param: $namespaces")
|
||||
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?) {
|
||||
Log.d(TAG, "Updating mCastSession - error - $p0 $p1 $p2")
|
||||
}
|
||||
|
||||
override fun success(args: Any?) {
|
||||
Log.d(TAG, "Updating mCastSession - success - param: $args")
|
||||
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)
|
||||
}
|
||||
namespaces.forEach { namespace ->
|
||||
try {
|
||||
oldSession?.removeMessageReceivedCallbacks(namespace)
|
||||
newSession?.setMessageReceivedCallbacks(namespace, 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"
|
||||
// end-region
|
||||
|
||||
const val getSessionMessageNamespaces = "CastSession.getSessionMessageNamespaces"
|
||||
const val onMessageReceived = "CastSession.onMessageReceived"
|
||||
}
|
||||
|
|
@ -22,6 +22,5 @@ enum MethodNames : String {
|
|||
case onSessionSuspended = "SessionManager.onSessionSuspended"
|
||||
// end-region
|
||||
|
||||
case getSessionMessageNamespaces = "CastSession.getSessionMessageNamespaces"
|
||||
case onMessageReceived = "CastSession.onMessageReceived"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,8 +5,11 @@ import GoogleCast
|
|||
public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessionManagerListener, CastApi {
|
||||
public static func register(with registrar: FlutterPluginRegistrar) {
|
||||
let messenger : FlutterBinaryMessenger = registrar.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)
|
||||
|
||||
let api : CastApi & NSObjectProtocol = instance
|
||||
|
|
@ -16,6 +19,7 @@ public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessio
|
|||
private let castContext: GCKCastContext
|
||||
private var castStateObserver: NSKeyValueObservation?
|
||||
private let channel: FlutterMethodChannel
|
||||
private let flutterApi : CastFlutterApi
|
||||
|
||||
private let sessionManager: GCKSessionManager
|
||||
|
||||
|
|
@ -32,14 +36,13 @@ public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessio
|
|||
|
||||
_castSession = newValue
|
||||
|
||||
channel.invokeMethod(MethodNames.getSessionMessageNamespaces.rawValue, arguments: nil) { (args) in
|
||||
print("Updating castSession - success - param: \(args ?? "-")")
|
||||
flutterApi.getSessionMessageNamespaces { (namespaces, err) in
|
||||
print("Updating castSession - getSessionMessageNamespaces success - param: \(namespaces.joined(separator: ", "))")
|
||||
if (oldSession == nil && newSession == nil) {
|
||||
return // nothing to do here
|
||||
}
|
||||
|
||||
let namespaces = args as? NSArray
|
||||
if (namespaces == nil || namespaces?.count == 0) {
|
||||
if (namespaces.count == 0) {
|
||||
return // nothing to do here
|
||||
}
|
||||
|
||||
|
|
@ -50,12 +53,7 @@ public class SwiftFlutterCastFrameworkPlugin: NSObject, FlutterPlugin, GCKSessio
|
|||
}
|
||||
}
|
||||
|
||||
namespaces?.forEach({ (namespaceRaw) in
|
||||
if (!(namespaceRaw is String)) {
|
||||
return
|
||||
}
|
||||
|
||||
let namespace = namespaceRaw as! String
|
||||
namespaces.forEach({ (namespace) in
|
||||
let castingChannel = MessageCastingChannel.init(namespace: namespace, channel: self.channel)
|
||||
self.castingChannels[namespace] = 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.castContext = GCKCastContext.sharedInstance()
|
||||
self.sessionManager = GCKCastContext.sharedInstance().sessionManager
|
||||
|
||||
self.flutterApi = flutterApi
|
||||
|
||||
super.init()
|
||||
|
||||
self.castSession = GCKCastContext.sharedInstance().sessionManager.currentCastSession
|
||||
|
|
|
|||
|
|
@ -11,6 +11,5 @@ class PlatformMethodNames {
|
|||
static const onSessionResumeFailed = "SessionManager.onSessionResumeFailed";
|
||||
static const onSessionSuspended = "SessionManager.onSessionSuspended";
|
||||
|
||||
static const getSessionMessageNamespaces = "CastSession.getSessionMessageNamespaces";
|
||||
static const onMessageReceived = "CastSession.onMessageReceived";
|
||||
}
|
||||
|
|
@ -16,6 +16,7 @@ class FlutterCastFramework {
|
|||
static bool _isInitiated = false;
|
||||
|
||||
static _init() {
|
||||
CastFlutterApi.setup(CastFlutterApiImpl());
|
||||
_channel.setMethodCallHandler((MethodCall call) async {
|
||||
String method = call.method;
|
||||
dynamic arguments = call.arguments;
|
||||
|
|
@ -38,9 +39,6 @@ class FlutterCastFramework {
|
|||
castContext.sessionManager.onSessionStateChanged(method, arguments);
|
||||
break;
|
||||
|
||||
case PlatformMethodNames.getSessionMessageNamespaces:
|
||||
return namespaces;
|
||||
|
||||
case PlatformMethodNames.onMessageReceived:
|
||||
castContext.sessionManager.platformOnMessageReceived(arguments);
|
||||
break;
|
||||
|
|
@ -68,3 +66,10 @@ class FlutterCastFramework {
|
|||
return castContext;
|
||||
}
|
||||
}
|
||||
|
||||
class CastFlutterApiImpl extends CastFlutterApi {
|
||||
@override
|
||||
List<String?> getSessionMessageNamespaces() {
|
||||
return FlutterCastFramework.namespaces;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue