Skip to content

Commit

Permalink
Merge pull request #25 from fbernaly/fix/index
Browse files Browse the repository at this point in the history
Fix for "show presentation" wrong index
  • Loading branch information
VNAPNIC authored Mar 29, 2022
2 parents 5868c65 + 1be226c commit 6c8c30d
Show file tree
Hide file tree
Showing 20 changed files with 381 additions and 461 deletions.
29 changes: 29 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# This file configures the analyzer, which statically analyzes Dart code to
# check for errors, warnings, and lints.
#
# The issues identified by the analyzer are surfaced in the UI of Dart-enabled
# IDEs (https://dart.dev/tools#ides-and-editors). The analyzer can also be
# invoked from the command line by running `flutter analyze`.

# The following line activates a set of recommended lints for Flutter apps,
# packages, and plugins designed to encourage good coding practices.
include: package:flutter_lints/flutter.yaml

linter:
# The lint rules applied to this project can be customized in the
# section below to disable rules from the `package:flutter_lints/flutter.yaml`
# included above or to enable additional rules. A list of all available lints
# and their documentation is published at
# https://dart-lang.github.io/linter/lints/index.html.
#
# Instead of disabling a lint rule for the entire project in the
# section below, it can also be suppressed for a single line of code
# or a specific dart file by using the `// ignore: name_of_lint` and
# `// ignore_for_file: name_of_lint` syntax on the line or in the file
# producing the lint.
rules:
# avoid_print: false # Uncomment to disable the `avoid_print` rule
# prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package com.namit.presentation_displays
import androidx.annotation.Keep
import com.google.gson.annotations.SerializedName

/**
* @hide
*/
@Keep
data class DisplayJson(
@SerializedName("displayId")
Expand All @@ -16,4 +13,4 @@ data class DisplayJson(
val rotation: Int,
@SerializedName("name")
val name: String
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ import android.widget.FrameLayout
import io.flutter.embedding.android.FlutterView
import io.flutter.embedding.engine.FlutterEngineCache

class PresentationDisplay(context: Context, private val tag: String, display: Display) : Presentation(context, display) {
class PresentationDisplay(context: Context, private val tag: String, display: Display) :
Presentation(context, display) {

/**
* @hide
*/
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val flContainer = FrameLayout(context)
val params = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
val params = FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
flContainer.layoutParams = params

setContentView(flContainer)
Expand All @@ -33,4 +34,4 @@ class PresentationDisplay(context: Context, private val tag: String, display: Di
Log.e("PresentationDisplay", "Can't find the FlutterEngine with cache name $tag")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.hardware.display.DisplayManager
import android.util.Log
import android.view.Display
import androidx.annotation.NonNull
import androidx.core.content.ContextCompat.getSystemService
import com.google.gson.Gson
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.FlutterEngineCache
Expand All @@ -19,117 +20,123 @@ import io.flutter.plugin.common.PluginRegistry
import org.json.JSONObject

/** PresentationDisplaysPlugin */
class PresentationDisplaysPlugin() : FlutterPlugin, ActivityAware, MethodChannel.MethodCallHandler {
class PresentationDisplaysPlugin : FlutterPlugin, ActivityAware, MethodChannel.MethodCallHandler {

private lateinit var channel: MethodChannel
private var flutterEngineChannel: MethodChannel? = null
private var displayManager: DisplayManager? = null
private var context: Context? = null

private lateinit var channel : MethodChannel
private var flutterEngineChannel: MethodChannel? = null
private var displayManager: DisplayManager? = null
private var context:Context?=null

override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
channel = MethodChannel(flutterPluginBinding.flutterEngine.dartExecutor, viewTypeId)
channel.setMethodCallHandler(this)
}

companion object{
private val viewTypeId = "presentation_displays_plugin"

/**
* @hide
*/
@JvmStatic
fun registerWith(registrar: PluginRegistry.Registrar) {
val channel = MethodChannel(registrar.messenger(), viewTypeId)
channel.setMethodCallHandler(PresentationDisplaysPlugin())
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
channel = MethodChannel(flutterPluginBinding.flutterEngine.dartExecutor, viewTypeId)
channel.setMethodCallHandler(this)
}
}

override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null)
}

override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
Log.i(TAG, "Channel: method: ${call.method} | arguments: ${call.arguments}")
when (call.method) {
"showPresentation" -> {
try {
val obj = JSONObject(call.arguments as String)
Log.i(TAG, "Channel: method: ${call.method} | displayId: ${obj.getInt("displayId")} | routerName: ${obj.getString("routerName")}")
companion object {
private const val viewTypeId = "presentation_displays_plugin"

val displayId: Int = obj.getInt("displayId")
val tag: String = obj.getString("routerName")
val display = displayManager?.getDisplay(displayId)
val flutterEngine = createFlutterEngine(tag)

if (display != null) {
flutterEngine?.let {
flutterEngineChannel = MethodChannel(it.dartExecutor.binaryMessenger, "${viewTypeId}_engine")
val presentation = context?.let { it1 -> PresentationDisplay(it1, tag, display) }
presentation?.show()
result.success(true)
} ?: result.error("404", "Can't find FlutterEngine", null)
@JvmStatic
fun registerWith(registrar: PluginRegistry.Registrar) {
val channel = MethodChannel(registrar.messenger(), viewTypeId)
channel.setMethodCallHandler(PresentationDisplaysPlugin())
}
}

} else {
result.error("404", "Can't find display with displayId is $displayId", null)
}
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null)
}

} catch (e: Exception) {
result.error(call.method, e.message, null)
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
Log.i(TAG, "Channel: method: ${call.method} | arguments: ${call.arguments}")
when (call.method) {
"showPresentation" -> {
try {
val obj = JSONObject(call.arguments as String)
Log.i(
TAG,
"Channel: method: ${call.method} | displayId: ${obj.getInt("displayId")} | routerName: ${
obj.getString("routerName")
}"
)
val displayId: Int = obj.getInt("displayId")
val tag: String = obj.getString("routerName")
val display = displayManager?.getDisplay(displayId)
if (display != null) {
val flutterEngine = createFlutterEngine(tag)
flutterEngine?.let {
flutterEngineChannel = MethodChannel(
it.dartExecutor.binaryMessenger,
"${viewTypeId}_engine"
)
val presentation =
context?.let { it1 -> PresentationDisplay(it1, tag, display) }
Log.i(TAG, "presentation: $presentation")
presentation?.show()
result.success(true)
} ?: result.error("404", "Can't find FlutterEngine", null)
} else {
result.error("404", "Can't find display with displayId is $displayId", null)
}
} catch (e: Exception) {
result.error(call.method, e.message, null)
}
}
"listDisplay" -> {
val listJson = ArrayList<DisplayJson>()
val category = call.arguments
val displays = displayManager?.getDisplays(category as String?)
if (displays != null) {
for (display: Display in displays) {
Log.i(TAG, "display: $display")
val d = DisplayJson(
display.displayId,
display.flags,
display.rotation,
display.name
)
listJson.add(d)
}
}
result.success(Gson().toJson(listJson))
}
"transferDataToPresentation" -> {
try {
flutterEngineChannel?.invokeMethod("DataTransfer", call.arguments)
result.success(true)
} catch (e: Exception) {
result.success(false)
}
}
}
}
"listDisplay" -> {
val gson = Gson()
val category = call.arguments
val displays = displayManager?.getDisplays(category as String?)
val listJson = ArrayList<DisplayJson>()
}

if (displays!=null) {
for (display: Display in displays) {
val d = DisplayJson(display.displayId, display.flags, display.rotation, display.name)
listJson.add(d)
}
result.success(gson.toJson(listJson))
private fun createFlutterEngine(tag: String): FlutterEngine? {
if (context == null)
return null
if (FlutterEngineCache.getInstance().get(tag) == null) {
val flutterEngine = FlutterEngine(context!!)
flutterEngine.navigationChannel.setInitialRoute(tag)
flutterEngine.dartExecutor.executeDartEntrypoint(
DartExecutor.DartEntrypoint.createDefault()
)
flutterEngine.lifecycleChannel.appIsResumed()
// Cache the FlutterEngine to be used by FlutterActivity.
FlutterEngineCache.getInstance().put(tag, flutterEngine)
}
}
"transferDataToPresentation" -> {
try {
flutterEngineChannel?.invokeMethod("DataTransfer", call.arguments)
result.success(true)
} catch (e: Exception) {
result.success(false)
}
}
return FlutterEngineCache.getInstance().get(tag)
}
}

private fun createFlutterEngine(tag: String): FlutterEngine? {
if(context == null)
return null
if (FlutterEngineCache.getInstance().get(tag) == null) {
val flutterEngine = FlutterEngine(context!!)
flutterEngine.navigationChannel.setInitialRoute(tag)
flutterEngine.dartExecutor.executeDartEntrypoint(
DartExecutor.DartEntrypoint.createDefault()
)
flutterEngine.lifecycleChannel.appIsResumed()
// Cache the FlutterEngine to be used by FlutterActivity.
FlutterEngineCache.getInstance().put(tag, flutterEngine)
override fun onDetachedFromActivity() {
}
return FlutterEngineCache.getInstance().get(tag)
}

override fun onDetachedFromActivity() {
}

override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
}
override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
}

override fun onAttachedToActivity(binding: ActivityPluginBinding) {
this.context = binding.activity
this.displayManager = context?.getSystemService(Context.DISPLAY_SERVICE) as DisplayManager
}
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
this.context = binding.activity
this.displayManager = context?.getSystemService(Context.DISPLAY_SERVICE) as DisplayManager
}

override fun onDetachedFromActivityForConfigChanges() {
}
override fun onDetachedFromActivityForConfigChanges() {
}
}
Loading

0 comments on commit 6c8c30d

Please sign in to comment.