package Ġcom . nk ming . power menu Ċ Ċ import Ġandroid . annotation . Target Api Ċ import Ġandroid . app . Notification Ċ import Ġandroid . app . Notification Channel Ċ import Ġandroid . app . Notification Manager Ċ import Ġandroid . app . Pending Intent Ċ import Ġandroid . content . Context Ċ import Ġandroid . content . Intent Ċ import Ġandroid . os . Build Ċ import Ġandroid . provider . Settings Ċ import Ġandroid . widget . Toast Ċ import Ġandroidx . core . app . Notification Compat Ċ import Ġandroidx . core . content . Context Compat Ċ Ċ class ĠPersistent Service Ġ: Ġcom . nk ming . utils . widget . Persistent Service () Ċ { Ċ ĉ comp anion Ġobject Ċ ĉ { Ċĉ ĉ @ Jvm Static Ċĉ ĉ fun Ġstart ( context : ĠContext ) Ċĉ ĉ { Ċĉĉ ĉ val Ġintent Ġ= ĠIntent ( context , ĠPersistent Service :: class . java ) Ċĉĉ ĉ Context Compat . start Foreground Service ( context , Ġcreate Start ( intent )) Ċĉĉ ĉ is Starting Ġ= Ġtrue Ċĉ ĉ } ĊĊĉ ĉ @ Jvm Static Ċĉ ĉ fun Ġstop ( context : ĠContext ) Ċĉ ĉ { Ċĉĉ ĉ if Ġ( isRunning () Ġ|| Ġis Starting ) Ċĉĉ ĉ { Ċĉĉĉ ĉ val Ġintent Ġ= ĠIntent ( context , ĠPersistent Service :: class . java ) Ċĉĉĉ ĉ Context Compat . start Foreground Service ( context , Ġcreate Stop ( intent )) Ċĉĉ ĉ } Ċĉ ĉ } ĊĊĉ ĉ @ Jvm Static Ċĉ ĉ fun Ġshow View ( context : ĠContext ) Ċĉ ĉ { Ċĉĉ ĉ if Ġ( isRunning () Ġ|| Ġis Starting ) Ċĉĉ ĉ { Ċĉĉĉ ĉ val Ġintent Ġ= ĠIntent ( context , ĠPersistent Service :: class . java ) Ċĉĉĉ ĉ Context Compat . start Foreground Service ( context , Ċĉĉĉĉĉ ĉ create Show View ( intent )) Ċĉĉ ĉ } Ċĉ ĉ } ĊĊĉ ĉ @ Jvm Static Ċĉ ĉ fun Ġhide View ( context : ĠContext ) Ċĉ ĉ { Ċĉĉ ĉ if Ġ( isRunning () Ġ|| Ġis Starting ) Ċĉĉ ĉ { Ċĉĉĉ ĉ val Ġintent Ġ= ĠIntent ( context , ĠPersistent Service :: class . java ) Ċĉĉĉ ĉ Context Compat . start Foreground Service ( context , Ċĉĉĉĉĉ ĉ create Hide View ( intent )) Ċĉĉ ĉ } Ċĉ ĉ } ĊĊĉ ĉ @ Jvm Static Ċĉ ĉ fun Ġset Auto hide View ( context : ĠContext , Ġflag : ĠBoolean ) Ċĉ ĉ { Ċĉĉ ĉ if Ġ( isRunning () Ġ|| Ġis Starting ) Ċĉĉ ĉ { Ċĉĉĉ ĉ val Ġintent Ġ= ĠIntent ( context , ĠPersistent Service :: class . java ) Ċĉĉĉ ĉ Context Compat . start Foreground Service ( context , Ċĉĉĉĉĉ ĉ create Set Auto hide View ( intent , Ġflag )) Ċĉĉ ĉ } Ċĉ ĉ } ĊĊĉ ĉ @ Jvm Static Ċĉ ĉ fun Ġset Alpha ( context : ĠContext , Ġalpha : ĠFloat ) Ċĉ ĉ { Ċĉĉ ĉ if Ġ( isRunning () Ġ|| Ġis Starting ) Ċĉĉ ĉ { Ċĉĉĉ ĉ val Ġintent Ġ= ĠIntent ( context , ĠPersistent Service :: class . java ) Ċĉĉĉ ĉ Context Compat . start Foreground Service ( context , Ċĉĉĉĉĉ ĉ create Set Alpha ( intent , Ġalpha )) Ċĉĉ ĉ } Ċĉ ĉ } ĊĊĉ ĉ @ Jvm Static Ċĉ ĉ fun Ġset Enable H aptic ( context : ĠContext , Ġflag : ĠBoolean ) Ċĉ ĉ { Ċĉĉ ĉ if Ġ( isRunning () Ġ|| Ġis Starting ) Ċĉĉ ĉ { Ċĉĉĉ ĉ val Ġintent Ġ= ĠIntent ( context , ĠPersistent Service :: class . java ) Ċĉĉĉ ĉ Context Compat . start Foreground Service ( context , Ċĉĉĉĉĉ ĉ create Set Enable H aptic ( intent , Ġflag )) Ċĉĉ ĉ } Ċĉ ĉ } ĊĊĉ ĉ @ Jvm Static Ċĉ ĉ @ Target Api ( Build . VERSION _ CODES . O ) Ċĉ ĉ fun Ġinit Not if Channel ( context : ĠContext , Ġnm : ĠNotification Manager ) Ċĉ ĉ { Ċĉĉ ĉ val Ġch Ġ= ĠNotification Channel ( CHANNEL _ ID , Ċĉĉĉĉ ĉ context . getString ( R . string . notification _ channel _ name ), Ċĉĉĉĉ ĉ Notification Manager . IMPORT ANCE _ MIN ) Ċĉĉ ĉ ch . description Ġ= Ġcontext . getString ( Ċĉĉĉĉ ĉ R . string . notification _ channel _ description ) Ċĉĉ ĉ ch . lock screen Visibility Ġ= ĠNotification Compat . VIS IBILITY _ SECRET Ċĉĉ ĉ ch . set Show Badge ( false ) Ċĉĉ ĉ nm . create Notification Channel ( ch ) Ċĉ ĉ } ĊĊĉ ĉ private Ġvar Ġis Starting Ġ= Ġfalse Ċĉ ĉ private Ġconst Ġval ĠCHAN NEL _ ID Ġ= Ġ" persistent _ service " Ċ ĉ } ĊĊ ĉ override Ġfun ĠonCreate () Ċ ĉ { Ċĉ ĉ super . onCreate () Ċĉ ĉ is Starting Ġ= Ġfalse Ċ ĉ } ĊĊ ĉ override Ġfun ĠonStart Command ( intent : ĠIntent ?, Ġflags : ĠInt , Ġstart Id : ĠInt ): ĠInt Ċ ĉ { Ċĉ ĉ if Ġ(! Permission Utils . has System Alert Window ( this )) Ċĉ ĉ { Ċĉĉ ĉ // ĠPermission Ġbeing Ġrev oked Ċĉĉ ĉ stop Self () Ċĉĉ ĉ return ĠSTART _ STICK Y Ċĉ ĉ } Ċĉ ĉ else Ċĉ ĉ { Ċĉĉ ĉ return Ġsuper . onStart Command ( intent , Ġflags , Ġstart Id ) Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ override Ġfun Ġget Layout Id (): ĠInt Ċ ĉ { Ċĉ ĉ return ĠR . layout . persistent _ view Ċ ĉ } ĊĊ ĉ override Ġfun Ġget Foreground Notification Id () Ġ= ĠRes . NOT IF _ PER SIST _ SERVICE ĊĊ ĉ override Ġfun Ġget Foreground Notification (): ĠNotification ? Ċ ĉ { Ċĉ ĉ val Ġbuilder Ġ= ĠNotification Compat . Builder ( this , ĠCHAN NEL _ ID ) Ċĉ ĉ builder . setContent Title ( getString ( R . string . notification _ title )) Ċĉĉĉ ĉ . setContent Text ( getString ( R . string . notification _ text )) Ċĉĉĉ ĉ . set Local Only ( true ) Ċĉĉĉ ĉ . set Only Alert Once ( true ) Ċĉĉĉ ĉ . set Small Icon ( R . drawable . ic _ action _ shutdown ) Ċĉĉĉ ĉ . setColor ( Context Compat . getColor ( this , ĠR . color . primary _ light )) Ċĉĉĉ ĉ . set Ticker ( getString ( R . string . notification _ ticker )) ĊĊĉ ĉ if Ġ( Build . VERSION . SDK _ INT Ġ< ĠBuild . VERSION _ CODES . O ) Ċĉ ĉ { Ċĉĉ ĉ builder . priority Ġ= ĠNotification Compat . PRIORITY _ MIN Ċĉ ĉ } ĊĊĉ ĉ val Ġactivity Ġ= ĠIntent ( this , ĠPreference Activity :: class . java ) Ċĉ ĉ activity . flags Ġ= Ġ( Intent . FLAG _ ACTIVITY _ NEW _ TASK Ċĉĉĉ ĉ or ĠIntent . FLAG _ ACTIVITY _ CLEAR _ TASK ) Ċĉ ĉ val Ġpi Ġ= ĠPending Intent . getActivity ( this , Ġ 0 , Ġactivity , Ċĉĉĉ ĉ Pending Intent . FLAG _ UPDATE _ CURRENT ) Ċĉ ĉ builder . setContent Intent ( pi ) ĊĊĉ ĉ if Ġ( Build . VERSION . SDK _ INT Ġ>= ĠBuild . VERSION _ CODES . O ) Ċĉ ĉ { Ċĉĉ ĉ val Ġintent Ġ= ĠIntent ( Settings . ACTION _ CHANNEL _ NOTIFICATION _ SETTINGS ) Ċĉĉ ĉ intent . putExtra ( Settings . EXTRA _ APP _ PACKAGE , Ċĉĉĉĉ ĉ Build Config . APPLICATION _ ID ) Ċĉĉ ĉ intent . putExtra ( Settings . EXTRA _ CHANNEL _ ID , ĠCHAN NEL _ ID ) Ċĉĉ ĉ val Ġpending Intent Ġ= ĠPending Intent . getActivity ( this , Ċĉĉĉĉ ĉ Res . PENDING _ INT ENT _ REQ _ HIDE _ PER SIST _ NOT IF , Ġintent , Ċĉĉĉĉ ĉ Pending Intent . FLAG _ UPDATE _ CURRENT ) Ċĉĉ ĉ builder . addAction ( R . drawable . outline _ visibility _ off _ white _ 2 4 , Ċĉĉĉĉ ĉ getString ( R . string . notification _ action _ hide ), Ċĉĉĉĉ ĉ pending Intent ) Ċĉ ĉ } ĊĊĉ ĉ return Ġbuilder . build () Ċ ĉ } ĊĊ ĉ override Ġfun Ġon Init View () Ċ ĉ { Ċĉ ĉ super . on Init View () Ċĉ ĉ val Ġpref Ġ= Ġget SharedPreferences ( getString ( R . string . pref _ file ), Ċĉĉĉ ĉ Context . MODE _ PRIVATE ) Ċĉ ĉ view . setAuto hide ( pref . getBoolean ( getString ( Ċĉĉĉ ĉ R . string . pref _ auto hide _ persistent _ view _ key ), Ġfalse )) Ċĉ ĉ view . set Alpha ( pref . getInt ( getString ( R . string . pref _ alpha _ key ), Ċĉĉĉĉ 1 0 0 ) Ġ/ Ġ 1 0 0 . 0 f ) Ċĉ ĉ view . set Enable H aptic ( pref . getBoolean ( getString ( R . string . pref _ h aptic _ key ), Ċĉĉĉ ĉ true )) Ċ ĉ } ĊĊ ĉ override Ġfun Ġon View Click () Ċ ĉ { Ċĉ ĉ super . on View Click () Ċĉ ĉ val Ġapp Context Ġ= Ġapplication Context Ċĉ ĉ System Helper . sleep ( app Context , Ċĉ ĉ { Ċĉĉ ĉ if Ġ(! it ) Ċĉĉ ĉ { Ċĉĉĉ ĉ Toast . makeText ( app Context , ĠR . string . sleep _ fail , Ċĉĉĉĉĉ ĉ Toast . LENGTH _ LONG ). show () Ċĉĉ ĉ } Ċĉ ĉ }) Ċ ĉ } ĊĊ ĉ override Ġfun Ġon View Long Click () Ċ ĉ { Ċĉ ĉ super . on View Long Click () Ċĉ ĉ val Ġapp Context Ġ= Ġapplication Context Ċĉ ĉ System Helper . startActivity ( app Context , ĠMainActivity :: class . java , Ċĉ ĉ { Ċĉĉ ĉ if Ġ(! it ) Ċĉĉ ĉ { Ċĉĉĉ ĉ Toast . makeText ( app Context , ĠR . string . start _ activity _ fail , Ċĉĉĉĉĉ ĉ Toast . LENGTH _ LONG ). show (); Ċĉĉ ĉ } Ċĉ ĉ }) Ċ ĉ } Ċ } Ċ