-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
NamIT
committed
Aug 18, 2020
1 parent
4be1f66
commit b562bf4
Showing
10 changed files
with
343 additions
and
334 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 127 additions & 9 deletions
136
android/src/main/kotlin/com/namit/presentation_displays/PresentationDisplaysPlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.