From 37f50cfe8aa6021ad98d2db69f88c5cbed0b2f4d Mon Sep 17 00:00:00 2001 From: Vadim <44135514+vodemn@users.noreply.github.com> Date: Sat, 11 Feb 2023 01:28:33 +0300 Subject: [PATCH] platform-specific code --- .../com/vodemn/lightmeter/MainActivity.kt | 26 +++++++++++++- ios/Runner/AppDelegate.swift | 34 +++++++++++++++---- 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/android/app/src/main/kotlin/com/vodemn/lightmeter/MainActivity.kt b/android/app/src/main/kotlin/com/vodemn/lightmeter/MainActivity.kt index a234f56..61af74c 100644 --- a/android/app/src/main/kotlin/com/vodemn/lightmeter/MainActivity.kt +++ b/android/app/src/main/kotlin/com/vodemn/lightmeter/MainActivity.kt @@ -1,6 +1,30 @@ package com.vodemn.lightmeter +import android.view.WindowManager import io.flutter.embedding.android.FlutterActivity +import io.flutter.embedding.engine.FlutterEngine +import io.flutter.plugin.common.MethodChannel -class MainActivity: FlutterActivity() { +class MainActivity : FlutterActivity() { + override fun configureFlutterEngine(flutterEngine: FlutterEngine) { + super.configureFlutterEngine(flutterEngine) + MethodChannel( + flutterEngine.dartExecutor.binaryMessenger, + "com.vodemn.lightmeter/keepScreenOn" + ).setMethodCallHandler { call, result -> + when (call.method) { + "isKeepScreenOn" -> result.success((window.attributes.flags and WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) != 0) + "setKeepScreenOn" -> { + if (call.arguments !is Boolean) { + result.error("invalid args", "Argument should be of type Bool for 'setKeepScreenOn' call", null) + } else { + if (call.arguments as Boolean) window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) + else window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) + result.success(true) + } + } + else -> result.notImplemented() + } + } + } } diff --git a/ios/Runner/AppDelegate.swift b/ios/Runner/AppDelegate.swift index 70693e4..5dc5452 100644 --- a/ios/Runner/AppDelegate.swift +++ b/ios/Runner/AppDelegate.swift @@ -3,11 +3,31 @@ import Flutter @UIApplicationMain @objc class AppDelegate: FlutterAppDelegate { - override func application( - _ application: UIApplication, - didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? - ) -> Bool { - GeneratedPluginRegistrant.register(with: self) - return super.application(application, didFinishLaunchingWithOptions: launchOptions) - } + override func application( + _ application: UIApplication, + didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? + ) -> Bool { + let controller : FlutterViewController = window?.rootViewController as! FlutterViewController + let keepScreenOnChannel = FlutterMethodChannel(name: "com.vodemn.lightmeter/keepScreenOn", + binaryMessenger: controller.binaryMessenger) + keepScreenOnChannel.setMethodCallHandler({ + (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in + switch call.method { + case "isKeepScreenOn": + result(UIApplication.shared.isIdleTimerDisabled) + case "setKeepScreenOn": + guard let keepOn = call.arguments as? Bool else { + result(FlutterError(code: "invalid arguments", message: "Argument should be of type Bool for 'setKeepScreenOn' call", details: nil)) + return + } + UIApplication.shared.isIdleTimerDisabled = keepOn + result(true) + default: + result(FlutterMethodNotImplemented) + } + }) + + GeneratedPluginRegistrant.register(with: self) + return super.application(application, didFinishLaunchingWithOptions: launchOptions) + } }