import 'package:aves/services/services.dart'; import 'package:flutter/services.dart'; import 'package:flutter/widgets.dart'; abstract class WindowService { Future keepScreenOn(bool on); Future isRotationLocked(); Future requestOrientation([Orientation? orientation]); Future canSetCutoutMode(); Future setCutoutMode(bool use); } class PlatformWindowService implements WindowService { static const platform = MethodChannel('deckers.thibault/aves/window'); @override Future keepScreenOn(bool on) async { try { await platform.invokeMethod('keepScreenOn', { 'on': on, }); } on PlatformException catch (e) { await reportService.recordChannelError('keepScreenOn', e); } } @override Future isRotationLocked() async { try { final result = await platform.invokeMethod('isRotationLocked'); if (result != null) return result as bool; } on PlatformException catch (e) { await reportService.recordChannelError('isRotationLocked', e); } return false; } @override Future requestOrientation([Orientation? orientation]) async { // cf Android `ActivityInfo.ScreenOrientation` late final int orientationCode; switch (orientation) { case Orientation.landscape: // SCREEN_ORIENTATION_SENSOR_LANDSCAPE orientationCode = 6; break; case Orientation.portrait: // SCREEN_ORIENTATION_SENSOR_PORTRAIT orientationCode = 7; break; default: // SCREEN_ORIENTATION_UNSPECIFIED orientationCode = -1; break; } try { await platform.invokeMethod('requestOrientation', { 'orientation': orientationCode, }); } on PlatformException catch (e) { await reportService.recordChannelError('requestOrientation', e); } } @override Future canSetCutoutMode() async { try { final result = await platform.invokeMethod('canSetCutoutMode'); if (result != null) return result as bool; } on PlatformException catch (e) { await reportService.recordChannelError('canSetCutoutMode', e); } return false; } @override Future setCutoutMode(bool use) async { try { await platform.invokeMethod('setCutoutMode', { 'use': use, }); } on PlatformException catch (e) { await reportService.recordChannelError('setCutoutMode', e); } } }