Skip to content

Commit

Permalink
remove DisplayManager Widget
Browse files Browse the repository at this point in the history
  • Loading branch information
NamIT committed Aug 18, 2020
1 parent 4be1f66 commit b562bf4
Show file tree
Hide file tree
Showing 10 changed files with 343 additions and 334 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.1

* remove DisplaysManager widget

## 0.1.0

* Initial Open Source release.
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import io.flutter.plugin.common.StandardMessageCodec
import io.flutter.plugin.platform.PlatformView
import io.flutter.plugin.platform.PlatformViewFactory

class PresentationDisplaysFactory(private val messenger: BinaryMessenger, private val registrar: PluginRegistry.Registrar)
: PlatformViewFactory(StandardMessageCodec.INSTANCE){

/**
* @hide
*/
override fun create(context: Context?, viewId: Int, args: Any?): PlatformView {
val methodChannel = MethodChannel(messenger, "${PresentationDisplaysPlugin.viewTypeId}_$viewId")
val displayManager = context?.getSystemService(Context.DISPLAY_SERVICE) as DisplayManager
return PresentationDisplaysView(registrar, methodChannel, viewId, displayManager)
}
}
//class PresentationDisplaysFactory(private val messenger: BinaryMessenger, private val registrar: PluginRegistry.Registrar)
// : PlatformViewFactory(StandardMessageCodec.INSTANCE){
//
// /**
// * @hide
// */
// override fun create(context: Context?, viewId: Int, args: Any?): PlatformView {
// val methodChannel = MethodChannel(messenger, "${PresentationDisplaysPlugin.viewTypeId}_$viewId")
// val displayManager = context?.getSystemService(Context.DISPLAY_SERVICE) as DisplayManager
// return PresentationDisplaysView(registrar, methodChannel, viewId, displayManager)
// }
//}
Original file line number Diff line number Diff line change
@@ -1,17 +1,135 @@
package com.namit.presentation_displays

import io.flutter.plugin.common.PluginRegistry.Registrar
import android.content.ContentValues.TAG
import android.content.Context
import android.hardware.display.DisplayManager
import android.util.Log
import android.view.Display
import androidx.annotation.NonNull
import com.google.gson.Gson
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.FlutterEngineCache
import io.flutter.embedding.engine.dart.DartExecutor
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.PluginRegistry
import org.json.JSONObject

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

const val viewTypeId = "presentation_displays_plugin"

/**
* @hide
*/
@JvmStatic
fun registerWith(registrar: Registrar) {
registrar.platformViewRegistry().registerViewFactory(viewTypeId, PresentationDisplaysFactory(registrar.messenger(),registrar))
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 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")}")

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)

} 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 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))
}
}
"transferDataToPresentation" -> {
try {
flutterEngineChannel?.invokeMethod("DataTransfer", call.arguments)
result.success(true)
} catch (e: Exception) {
result.success(false)
}
}
}
}

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)
}
return FlutterEngineCache.getInstance().get(tag)
}

override fun onDetachedFromActivity() {
}

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 onDetachedFromActivityForConfigChanges() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,99 +14,99 @@ import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.platform.PlatformView
import org.json.JSONObject

class PresentationDisplaysView internal
constructor(
private val registrar: PluginRegistry.Registrar,
private val methodChannel: MethodChannel,
private val viewId: Int,
private val displayManager: DisplayManager) :
PlatformView, MethodChannel.MethodCallHandler {

private val TAG = "${PresentationDisplaysPlugin.viewTypeId}_$viewId"
private var flutterEngineChannel: MethodChannel? = null

init {
methodChannel.setMethodCallHandler(this)
}

/**
* @hide
*/
override fun getView(): View {
return View(registrar.context())
}

/**
* @hide
*/
override fun dispose() {
}

/**
* @hide
*/
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)
val flutterEngine = createFlutterEngine(tag)

if (display != null) {
flutterEngine?.let {
flutterEngineChannel = MethodChannel(it.dartExecutor.binaryMessenger, "${TAG}_engine")
val presentation = PresentationDisplay(registrar.activity(), tag, display)
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 gson = Gson()
val category = call.arguments
val displays = displayManager.getDisplays(category as String?)
val listJson = ArrayList<DisplayJson>();
for (display: Display in displays) {
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)
}
}
}
}

private fun createFlutterEngine(tag: String): FlutterEngine? {
if (FlutterEngineCache.getInstance().get(tag) == null) {
val flutterEngine = FlutterEngine(registrar.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)
}
return FlutterEngineCache.getInstance().get(tag)
}
}
//class PresentationDisplaysView internal
//constructor(
// private val registrar: PluginRegistry.Registrar,
// private val methodChannel: MethodChannel,
// private val viewId: Int,
// private val displayManager: DisplayManager) :
// PlatformView, MethodChannel.MethodCallHandler {
//
// private val TAG = "${PresentationDisplaysPlugin.viewTypeId}_$viewId"
// private var flutterEngineChannel: MethodChannel? = null
//
// init {
// methodChannel.setMethodCallHandler(this)
// }
//
// /**
// * @hide
// */
// override fun getView(): View {
// return View(registrar.context())
// }
//
// /**
// * @hide
// */
// override fun dispose() {
// }
//
// /**
// * @hide
// */
// 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)
// val flutterEngine = createFlutterEngine(tag)
//
// if (display != null) {
// flutterEngine?.let {
// flutterEngineChannel = MethodChannel(it.dartExecutor.binaryMessenger, "${TAG}_engine")
// val presentation = PresentationDisplay(registrar.activity(), tag, display)
// 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 gson = Gson()
// val category = call.arguments
// val displays = displayManager.getDisplays(category as String?)
// val listJson = ArrayList<DisplayJson>();
// for (display: Display in displays) {
// 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)
// }
// }
// }
// }
//
// private fun createFlutterEngine(tag: String): FlutterEngine? {
// if (FlutterEngineCache.getInstance().get(tag) == null) {
// val flutterEngine = FlutterEngine(registrar.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)
// }
// return FlutterEngineCache.getInstance().get(tag)
// }
//}
Loading

0 comments on commit b562bf4

Please sign in to comment.