From 9b90e62d76f6e64a9d6bc029ce64c4fa82408f96 Mon Sep 17 00:00:00 2001 From: gianlucaparadise Date: Sat, 9 Nov 2019 10:01:11 +0100 Subject: [PATCH] Show dialog on click --- .../FlutterCastFrameworkPlugin.kt | 74 ++++++++++++++----- example/android/app/build.gradle | 2 + .../app/src/main/res/values/styles.xml | 2 +- example/lib/main.dart | 13 ++-- lib/cast/CastContext.dart | 17 +++-- lib/cast/widget/CastButton.dart | 12 --- lib/cast/widgets/CastButton.dart | 12 +++ lib/cast/{widget => widgets}/CastIcon.dart | 0 lib/flutter_cast_framework.dart | 23 ++++-- 9 files changed, 102 insertions(+), 53 deletions(-) delete mode 100644 lib/cast/widget/CastButton.dart create mode 100644 lib/cast/widgets/CastButton.dart rename lib/cast/{widget => widgets}/CastIcon.dart (100%) diff --git a/android/src/main/kotlin/com/gianlucaparadise/flutter_cast_framework/FlutterCastFrameworkPlugin.kt b/android/src/main/kotlin/com/gianlucaparadise/flutter_cast_framework/FlutterCastFrameworkPlugin.kt index 45964df..e0d86da 100644 --- a/android/src/main/kotlin/com/gianlucaparadise/flutter_cast_framework/FlutterCastFrameworkPlugin.kt +++ b/android/src/main/kotlin/com/gianlucaparadise/flutter_cast_framework/FlutterCastFrameworkPlugin.kt @@ -1,6 +1,8 @@ package com.gianlucaparadise.flutter_cast_framework import android.util.Log +import androidx.mediarouter.app.MediaRouteChooserDialog +import androidx.mediarouter.app.MediaRouteControllerDialog import com.google.android.gms.cast.framework.CastContext import io.flutter.plugin.common.MethodCall import io.flutter.plugin.common.MethodChannel @@ -8,27 +10,59 @@ import io.flutter.plugin.common.MethodChannel.MethodCallHandler import io.flutter.plugin.common.MethodChannel.Result import io.flutter.plugin.common.PluginRegistry.Registrar -class FlutterCastFrameworkPlugin(registrar: Registrar, private val channel: MethodChannel): MethodCallHandler { - companion object { - @JvmStatic - fun registerWith(registrar: Registrar) { - val channel = MethodChannel(registrar.messenger(), "flutter_cast_framework") - channel.setMethodCallHandler(FlutterCastFrameworkPlugin(registrar, channel)) - } - } +class FlutterCastFrameworkPlugin(private val registrar: Registrar, private val channel: MethodChannel) : MethodCallHandler { + companion object { + const val TAG = "AndroidCastPlugin" - init { - CastContext.getSharedInstance(registrar.activeContext()).addCastStateListener { i -> - Log.d("Android", "Method call on flutter: $i") - channel.invokeMethod("onCastStateChanged", i) - } - } + // @StyleRes + // var customStyleResId: Int? = null + // private val themeResId get() = customStyleResId ?: R.style.Theme_AppCompat_DayNight_Dialog_Alert - override fun onMethodCall(call: MethodCall, result: Result) { - if (call.method == "getPlatformVersion") { - result.success("Android ${android.os.Build.VERSION.RELEASE}") - } else { - result.notImplemented() + @JvmStatic + fun registerWith(registrar: Registrar) { + val channel = MethodChannel(registrar.messenger(), "flutter_cast_framework") + channel.setMethodCallHandler(FlutterCastFrameworkPlugin(registrar, channel)) + } + } + + init { + CastContext.getSharedInstance(registrar.activeContext()).addCastStateListener { i -> + Log.d(TAG, "Cast state changed: $i") + channel.invokeMethod("onCastStateChanged", i) + } + } + + override fun onMethodCall(call: MethodCall, result: Result) { + when (call.method) { + "showCastDialog" -> showCastDialog() + else -> result.notImplemented() + } + } + + private fun showCastDialog() { + val castContext = CastContext.getSharedInstance(registrar.activeContext()) + val castSession = castContext.sessionManager.currentCastSession + + val activity = this.registrar.activity() + val themeResId = activity.packageManager.getActivityInfo(activity.componentName, 0).themeResource + + try { + if (castSession != null) { + // This dialog allows the user to control or disconnect from the currently selected route. + MediaRouteControllerDialog(registrar.activeContext(), themeResId) + .show() + } else { + // This dialog allows the user to choose a route that matches a given selector. + MediaRouteChooserDialog(registrar.activeContext(), themeResId).apply { + routeSelector = castContext.mergedSelector + show() + } + } + } catch (ex: IllegalArgumentException) { + Log.d(TAG, "Exception while opening Dialog") + throw IllegalArgumentException("Error while opening MediaRouteDialog." + + " Did you use AppCompat theme on your activity?" + + " Check https://developers.google.com/cast/docs/android_sender/integrate#androidtheme", ex) + } } - } } diff --git a/example/android/app/build.gradle b/example/android/app/build.gradle index 191fcbe..888662f 100644 --- a/example/android/app/build.gradle +++ b/example/android/app/build.gradle @@ -62,6 +62,8 @@ flutter { dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" testImplementation 'junit:junit:4.12' + implementation 'androidx.appcompat:appcompat:1.1.0' + androidTestImplementation 'androidx.test:runner:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' } diff --git a/example/android/app/src/main/res/values/styles.xml b/example/android/app/src/main/res/values/styles.xml index 00fa441..8676405 100644 --- a/example/android/app/src/main/res/values/styles.xml +++ b/example/android/app/src/main/res/values/styles.xml @@ -1,6 +1,6 @@ -