package Ġdev . craft soft . keep screen on Ċ Ċ import Ġandroid . app . Activity Ċ import Ġandroid . view . WindowManager Ċ import Ġandroidx . annotation . NonNull ; Ċ Ċ import Ġio . flutter . embedding . engine . plugins . Flutter Plugin Ċ import Ġio . flutter . embedding . engine . plugins . activity . Activity Aware Ċ import Ġio . flutter . embedding . engine . plugins . activity . Activity Plugin Binding Ċ import Ġio . flutter . plugin . common . MethodCall Ċ import Ġio . flutter . plugin . common . Method Channel Ċ import Ġio . flutter . plugin . common . Method Channel . MethodCall Handler Ċ import Ġio . flutter . plugin . common . Method Channel . Result Ċ import Ġio . flutter . plugin . common . Plugin Registry . Registrar Ċ Ċ /** ĠKeep Screen On Plugin Ġ*/ Ċ public Ġclass ĠKeep Screen On Plugin : ĠFlutter Plugin , ĠMethod Call Handler , ĠActivity Aware Ġ{ ĊĠ Ġ/// ĠThe ĠMethod Channel Ġthat Ġwill Ġthe Ġcommunication Ġbetween ĠFlutter Ġand Ġnative ĠAndroid ĊĠ Ġ/// ĊĠ Ġ/// ĠThis Ġlocal Ġreference Ġserves Ġto Ġregister Ġthe Ġplugin Ġwith Ġthe ĠFlutter ĠEngine Ġand Ġunregister Ġit ĊĠ Ġ/// Ġwhen Ġthe ĠFlutter ĠEngine Ġis Ġdet ached Ġfrom Ġthe ĠActivity ĊĠ Ġprivate Ġlateinit Ġvar Ġchannel Ġ: ĠMethod Channel ĊĊĠ Ġprivate Ġvar Ġactivity : ĠActivity ? Ġ= Ġnull ĊĊĠ Ġoverride Ġfun Ġon Attached To Engine (@ NonNull Ġflutter Plugin Binding : ĠFlutter Plugin . Flutter Plugin Binding ) Ġ{ ĊĠĠĠ Ġchannel Ġ= ĠMethod Channel ( flutter Plugin Binding . binary Messenger , ĠCHAN NEL _ NAME ) ĊĠĠĠ Ġchannel . set MethodCall Handler ( this ); ĊĠ Ġ} ĊĊĠ Ġoverride Ġfun Ġon MethodCall (@ NonNull Ġcall : ĠMethod Call , Ġ@ NonNull Ġresult : ĠResult ) Ġ{ ĊĊĠĠĠ Ġwhen Ġ( call . method ) Ġ{ ĊĠĠĠĠĠ Ġ" is On " ĠĠ Ġ-> Ġon MethodCall Is On ( call , Ġresult ) ĊĠĠĠĠĠ Ġ" turn On " Ġ-> Ġon MethodCall Turn On ( call , Ġresult ) ĊĠĠĠĠĠ Ġelse ĠĠĠĠ Ġ-> Ġ{ ĊĠĠĠĠĠĠĠ Ġresult . not Implemented (); ĊĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġoverride Ġfun Ġon Detached From Engine (@ NonNull Ġbinding : ĠFlutter Plugin . Flutter Plugin Binding ) Ġ{ ĊĠĠĠ Ġchannel . set MethodCall Handler ( null ) ĊĠ Ġ} ĊĊĠ Ġ// ĊĠ Ġ// ĠImplement ĠActivity Aware ĊĠ Ġ// ĊĠ Ġoverride Ġfun Ġon Attached To Activity ( binding : ĠActivity Plugin Binding ) Ġ{ ĊĠĠĠ Ġactivity Ġ= Ġbinding . activity ĊĠ Ġ} ĊĊĠ Ġoverride Ġfun Ġon Detached From Activity For Config Changes () Ġ{ ĊĠĠĠ Ġactivity Ġ= Ġnull ĊĠ Ġ} ĊĊĠ Ġoverride Ġfun Ġon Re attached To Activity For Config Changes ( binding : ĠActivity Plugin Binding ) Ġ{ ĊĠĠĠ Ġactivity Ġ= Ġbinding . activity ĊĠ Ġ} ĊĊĠ Ġoverride Ġfun Ġon Detached From Activity () Ġ{ ĊĠĠĠ Ġactivity Ġ= Ġnull ĊĠ Ġ} ĊĊĠ Ġ// ĊĠ Ġ// ĠImplement Ġa Ġprivate Ġmethod Ġcalled Ġfrom Ġon MethodCall ĊĠ Ġ// ĊĠ Ġprivate Ġfun Ġon MethodCall Is On (@ NonNull Ġ@ Suppress (" UNUSED _ PARAMETER ") Ġcall : ĠMethod Call , Ġ@ NonNull Ġresult : ĠResult ) Ġ{ ĊĠĠĠ Ġval Ġwindow Ġ= Ġactivity ?. window ĊĊĠĠĠ Ġif Ġ( window Ġ== Ġnull ) Ġ{ ĊĠĠĠĠĠ Ġresult . error (" not - found - activity ", Ġ" Not Ġfound Ġ' activity '. ", Ġnull ) ĊĠĠĠĠĠ Ġreturn ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġval Ġhas Keep Screen On Ġ= Ġ( window . attributes . flags Ġand ĠWindow Manager . LayoutParams . FLAG _ KEEP _ SCREEN _ ON ) Ġ!= Ġ 0 ; ĊĠĠĠ Ġresult . success ( has Keep Screen On ); ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġon MethodCall Turn On (@ NonNull Ġcall : ĠMethod Call , Ġ@ NonNull Ġresult : ĠResult ) Ġ{ ĊĠĠĠ Ġval Ġwindow Ġ= Ġactivity ?. window ĊĊĠĠĠ Ġif Ġ( window Ġ== Ġnull ) Ġ{ ĊĠĠĠĠĠ Ġresult . error (" not - found - activity ", Ġ" Not Ġfound Ġ' activity '. ", Ġnull ) ĊĠĠĠĠĠ Ġreturn ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġval Ġturn On Ġ= Ġcall . argument < Boolean >(" turn On "); ĊĠĠĠ Ġif Ġ( turn On Ġ== Ġtrue ) Ġ{ ĊĠĠĠĠĠ Ġwindow . add Flags ( WindowManager . LayoutParams . FLAG _ KEEP _ SCREEN _ ON ) ĊĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠ Ġwindow . clear Flags ( WindowManager . LayoutParams . FLAG _ KEEP _ SCREEN _ ON ); ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġresult . success ( true ); ĊĠ Ġ} ĊĊĊĠ Ġ// ĠThis Ġstatic Ġfunction Ġis Ġoptional Ġand Ġequivalent Ġto Ġon Attached To Engine . ĠIt Ġsupports Ġthe Ġold ĊĠ Ġ// Ġpre - Flutter - 1 . 1 2 ĠAndroid Ġprojects . ĠYou Ġare Ġencour aged Ġto Ġcontinue Ġsupporting ĊĠ Ġ// Ġplugin Ġregistration Ġvia Ġthis Ġfunction Ġwhile Ġapps Ġmigrate Ġto Ġuse Ġthe Ġnew ĠAndroid ĠAPIs ĊĠ Ġ// Ġpost - flutter - 1 . 1 2 Ġvia Ġhttps :// flutter . dev / go / android - project - migration . ĊĠ Ġ// ĊĠ Ġ// ĠIt Ġis Ġencour aged Ġto Ġshare Ġlogic Ġbetween Ġon Attached To Engine Ġand Ġregister With Ġto Ġkeep ĊĠ Ġ// Ġthem Ġfunction ally Ġequivalent . ĠOnly Ġone Ġof Ġon Attached To Engine Ġor Ġregister With Ġwill Ġbe Ġcalled ĊĠ Ġ// Ġdepending Ġon Ġthe Ġuser 's Ġproject . Ġon Attached To Engine Ġor Ġregister With Ġmust Ġboth Ġbe Ġdefined ĊĠ Ġ// Ġin Ġthe Ġsame Ġclass . ĊĠ Ġcompanion Ġobject Ġ{ ĊĠĠĠ Ġconst Ġval ĠTAG Ġ= Ġ" Keep Screen On Plugin "; ĊĠĠĠ Ġconst Ġval ĠCHAN NEL _ NAME Ġ= Ġ" dev . craft soft / keep _ screen _ on "; ĊĊĠĠĠ Ġ@ Jvm Static ĊĠĠĠ Ġfun Ġregister With ( regist rar : ĠRegist rar ) Ġ{ ĊĠĠĠĠĠ Ġval Ġchannel Ġ= ĠMethod Channel ( regist rar . messenger (), ĠCHAN NEL _ NAME ) ĊĠĠĠĠĠ Ġchannel . set MethodCall Handler ( Keep Screen On Plugin ()) ĊĠĠĠ Ġ} ĊĠ Ġ} Ċ } Ċ