package Ġcom . program mers box . help ful utils Ċ Ċ import Ġandroid . content . Broadcast Receiver Ċ import Ġandroid . content . Context Ċ import Ġandroid . content . Intent Ċ import Ġandroid . content . Intent Filter Ċ import Ġandroid . os . Battery Manager Ċ import Ġandroid . os . Build Ċ import Ġandroidx . annotation . Requires Api Ċ Ċ /** Ċ Ġ* Ġ@ see ĠIntent . ACTION _ TIME _ TICK Ċ Ġ*/ Ċ fun ĠContext . time Tick ( received : Ġ( context : ĠContext , Ġintent : ĠIntent ) Ġ-> ĠUnit ) Ġ= Ġobject Ġ: ĠBroadcast Receiver () Ġ{ ĊĠĠĠ Ġoverride Ġfun Ġon Receive ( context : ĠContext , Ġintent : ĠIntent ) Ġ= Ġreceived ( context , Ġintent ) Ċ }. also Ġ{ Ġregister Receiver ( it , ĠIntent Filter ( Intent . ACTION _ TIME _ TICK )) Ġ} Ċ Ċ // ---------------- ---------- Ċ /** Ċ Ġ* ĠThis Ġwill Ġgive Ġupdates Ġwhenever Ġthe Ġbattery Ġchanges Ċ Ġ* Ġ@ param Ġregister Ġif Ġtrue , Ġit Ġwill Ġautomatically Ġregister Ġthe ĠBroadcast Receiver Ġwith Ġthe Ġintent Ġfrom Ġ[ battery Intent Filter ] Ċ Ġ* Ġ@ see ĠIntent . ACTION _ B ATTER Y _ CHANGED Ċ Ġ*/ Ċ @ Requires Api ( Build . VERSION _ CODES . LO L LI POP ) Ċ fun ĠContext . battery ( register : ĠBoolean Ġ= Ġtrue , Ġbattery Info : Ġ( info : ĠB attery ) Ġ-> ĠUnit ) Ġ= Ġobject Ġ: ĠBroadcast Receiver () Ġ{ ĊĠĠĠ Ġoverride Ġfun Ġon Receive ( context : ĠContext , Ġintent : ĠIntent ) Ġ= Ġbattery Info ( battery Information ( context , Ġintent )) Ċ }. also Ġ{ Ġif Ġ( register ) Ġregister Receiver ( it , Ġbattery Intent Filter ()) Ġ} Ċ Ċ /** Ċ Ġ* ĠThis Ġwill Ġgive Ġthe Ġcurrent Ġstatus Ċ Ġ* Ġ@ see ĠIntent . ACTION _ B ATTER Y _ CHANGED Ċ Ġ*/ Ċ val ĠContext . battery Info : ĠB attery ĊĠĠĠ Ġ@ Requires Api ( Build . VERSION _ CODES . LO L LI POP ) ĊĠĠĠ Ġget () Ġ= Ġbattery Information ( this , Ġregister Receiver ( null , Ġbattery Intent Filter ())) Ċ Ċ data Ġclass ĠB attery ( ĊĠĠĠ Ġval Ġpercent : ĠFloat , ĊĠĠĠ Ġval Ġis Ch arging : ĠBoolean , ĊĠĠĠ Ġval Ġcharge Type : ĠCharge Type , ĊĠĠĠ Ġval Ġhealth : ĠB attery Health , ĊĠĠĠ Ġval Ġtechnology : ĠString ?, ĊĠĠĠ Ġval Ġtemperature : ĠFloat , ĊĠĠĠ Ġval Ġvoltage : ĠInt , ĊĠĠĠ Ġval Ġcapacity : ĠLong Ċ ) Ċ Ċ enum Ġclass ĠCharge Type Ġ{ ĠUSB , ĠAC , ĠW I RE LESS , ĠNONE Ġ} Ċ enum Ġclass ĠB attery Health Ġ{ ĠCOL D , ĠDE AD , ĠGO OD , ĠOVER _ VOL T AGE , ĠOVER HE AT , ĠUNS PE CI FIED _ FAILURE , ĠUNKNOWN Ġ} Ċ Ċ @ Requires Api ( Build . VERSION _ CODES . LO L LI POP ) Ċ fun Ġbattery Information ( context : ĠContext , Ġintent : ĠIntent ?): ĠB attery Ġ{ ĊĠĠĠ Ġ// percentage ĊĠĠĠ Ġval Ġlevel : ĠInt Ġ= Ġintent ?. getInt Extra ( Battery Manager . EXTRA _ LEVEL , Ġ- 1 ) Ġ?: Ġ- 1 ĊĠĠĠ Ġval Ġscale : ĠInt Ġ= Ġintent ?. getInt Extra ( Battery Manager . EXTRA _ SCALE , Ġ- 1 ) Ġ?: Ġ- 1 ĊĠĠĠ Ġval Ġbattery Pct : ĠFloat Ġ= Ġlevel Ġ* Ġ 1 0 0 Ġ/ Ġscale . toFloat () ĊĊĠĠĠ Ġ// ch arging Ġstatus ĊĠĠĠ Ġval Ġstatus : ĠInt Ġ= Ġintent ?. getInt Extra ( Battery Manager . EXTRA _ STATUS , Ġ- 1 ) Ġ?: Ġ- 1 ĊĠĠĠ Ġval Ġis Ch arging : ĠBoolean Ġ= Ġstatus Ġ== ĠB attery Manager . B ATTER Y _ STATUS _ CHAR GING Ġ|| Ġstatus Ġ== ĠB attery Manager . B ATTER Y _ STATUS _ FULL ĊĊĠĠĠ Ġ// ĠHow Ġare Ġwe Ġch arging ? ĊĠĠĠ Ġval Ġcharge Plug : ĠInt Ġ= Ġintent ?. getInt Extra ( Battery Manager . EXTRA _ PLU GG ED , Ġ- 1 ) Ġ?: Ġ- 1 ĊĠĠĠ Ġval Ġusb Charge : ĠBoolean Ġ= Ġcharge Plug Ġ== ĠB attery Manager . B ATTER Y _ PLU GG ED _ USB ĊĠĠĠ Ġval Ġac Charge : ĠBoolean Ġ= Ġcharge Plug Ġ== ĠB attery Manager . B ATTER Y _ PLU GG ED _ AC ĊĠĠĠ Ġval Ġwireless Charge : ĠBoolean Ġ= Ġcharge Plug Ġ== ĠB attery Manager . B ATTER Y _ PLU GG ED _ WI RE LESS ĊĠĠĠ Ġval Ġtype Ġ= Ġwhen Ġ{ ĊĠĠĠĠĠĠĠ Ġusb Charge Ġ-> ĠCharge Type . USB ĊĠĠĠĠĠĠĠ Ġac Charge Ġ-> ĠCharge Type . AC ĊĠĠĠĠĠĠĠ Ġwireless Charge Ġ-> ĠCharge Type . WI RE LESS ĊĠĠĠĠĠĠĠ Ġelse Ġ-> ĠCharge Type . NONE ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ// Battery ĠHealth ĊĠĠĠ Ġval Ġpresent Ġ= Ġintent ?. getBoolean Extra ( Battery Manager . EXTRA _ PRESENT , Ġfalse ) ĊĠĠĠ Ġval Ġhealth Ġ= Ġif Ġ( present Ġ== Ġtrue ) Ġ{ ĊĠĠĠĠĠĠĠ Ġwhen Ġ( intent . getInt Extra ( Battery Manager . EXTRA _ HE ALTH , Ġ 0 )) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠB attery Manager . B ATTER Y _ HE ALTH _ COL D Ġ-> ĠB attery Health . COL D ĊĠĠĠĠĠĠĠĠĠĠĠ ĠB attery Manager . B ATTER Y _ HE ALTH _ DEAD Ġ-> ĠB attery Health . DEAD ĊĠĠĠĠĠĠĠĠĠĠĠ ĠB attery Manager . B ATTER Y _ HE ALTH _ GO OD Ġ-> ĠB attery Health . GO OD ĊĠĠĠĠĠĠĠĠĠĠĠ ĠB attery Manager . B ATTER Y _ HE ALTH _ OVER _ VOL T AGE Ġ-> ĠB attery Health . OVER _ VOL T AGE ĊĠĠĠĠĠĠĠĠĠĠĠ ĠB attery Manager . B ATTER Y _ HE ALTH _ OVER HE AT Ġ-> ĠB attery Health . OVER HE AT ĊĠĠĠĠĠĠĠĠĠĠĠ ĠB attery Manager . B ATTER Y _ HE ALTH _ UNSPECIFIED _ FAILURE Ġ-> ĠB attery Health . UNSPECIFIED _ FAILURE ĊĠĠĠĠĠĠĠĠĠĠĠ ĠB attery Manager . B ATTER Y _ HE ALTH _ UNKNOWN Ġ-> ĠB attery Health . UNKNOWN ĊĠĠĠĠĠĠĠĠĠĠĠ Ġelse Ġ-> ĠB attery Health . UNKNOWN ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠ ĠB attery Health . UNKNOWN ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ// technology ĊĠĠĠ Ġval Ġtechnology Ġ= Ġintent ?. extras ?. getString ( Battery Manager . EXTRA _ TE CH NO LOG Y ) ĊĊĠĠĠ Ġ// temperature ĊĠĠĠ Ġval Ġtemperature Ġ= Ġintent ?. getInt Extra ( Battery Manager . EXTRA _ TEMP ER ATURE , Ġ 0 ) Ġ?: Ġ 0 ĊĊĠĠĠ Ġval Ġtemp Ġ= Ġif Ġ( temperature Ġ> Ġ 0 ) Ġ( temperature . toFloat () Ġ/ Ġ 1 0 f ) Ġelse Ġtemperature . toFloat () ĊĊĠĠĠ Ġ// voltage ĊĠĠĠ Ġval Ġvoltage Ġ= Ġintent ?. getInt Extra ( Battery Manager . EXTRA _ VOL T AGE , Ġ 0 ) Ġ?: Ġ 0 ĊĊĠĠĠ Ġ// capacity ĊĠĠĠ Ġval Ġm Battery Manager Ġ= Ġcontext . battery Manager ĊĠĠĠ Ġval Ġcharge Counter Ġ= Ġm Battery Manager . getLong Property ( Battery Manager . B ATTER Y _ PROPERTY _ CHAR GE _ COUNTER ) ĊĠĠĠ Ġval Ġcapacity Ġ= Ġm Battery Manager . getLong Property ( Battery Manager . B ATTER Y _ PROPERTY _ CAP ACITY ) ĊĠĠĠ Ġval Ġcap Ġ= Ġ( charge Counter . toFloat () Ġ/ Ġcapacity . toFloat () Ġ* Ġ 1 0 0 f ). to Long () ĊĊĠĠĠ Ġreturn ĠB attery ( battery Pct , Ġis Ch arging , Ġtype , Ġhealth , Ġtechnology , Ġtemp , Ġvoltage , Ġcap ) Ċ } Ċ Ċ fun Ġbattery Intent Filter () Ġ= ĠIntent Filter (). apply Ġ{ ĊĠĠĠ Ġadd Action ( Intent . ACTION _ POWER _ CONNECTED ) ĊĠĠĠ Ġadd Action ( Intent . ACTION _ POWER _ DIS CONNECTED ) ĊĠĠĠ Ġadd Action ( Intent . ACTION _ B ATTER Y _ CHANGED ) Ċ } Ċ Ċ // ---------------- ---------- Ċ /** Ċ Ġ* Ġ@ see ĠIntent . ACTION _ SCREEN _ OFF Ċ Ġ*/ Ċ fun ĠContext . screen Off ( received : Ġ( context : ĠContext , Ġintent : ĠIntent ) Ġ-> ĠUnit ) Ġ= Ġobject Ġ: ĠBroadcast Receiver () Ġ{ ĊĠĠĠ Ġoverride Ġfun Ġon Receive ( context : ĠContext , Ġintent : ĠIntent ) Ġ= Ġreceived ( context , Ġintent ) Ċ }. also Ġ{ Ġregister Receiver ( it , ĠIntent Filter ( Intent . ACTION _ SCREEN _ OFF )) Ġ} Ċ Ċ /** Ċ Ġ* Ġ@ see ĠIntent . ACTION _ SCREEN _ ON Ċ Ġ*/ Ċ fun ĠContext . screen On ( received : Ġ( context : ĠContext , Ġintent : ĠIntent ) Ġ-> ĠUnit ) Ġ= Ġobject Ġ: ĠBroadcast Receiver () Ġ{ ĊĠĠĠ Ġoverride Ġfun Ġon Receive ( context : ĠContext , Ġintent : ĠIntent ) Ġ= Ġreceived ( context , Ġintent ) Ċ }. also Ġ{ Ġregister Receiver ( it , ĠIntent Filter ( Intent . ACTION _ SCREEN _ ON )) Ġ} Ċ Ċ enum Ġclass ĠScreen State Ġ{ ĠON , ĠOFF , ĠUNKNOWN Ġ} Ċ Ċ /** Ċ Ġ* Ġ@ see ĠIntent . ACTION _ SCREEN _ ON Ċ Ġ* Ġ@ see ĠIntent . ACTION _ SCREEN _ OFF Ċ Ġ*/ Ċ fun ĠContext . screen State ( received : Ġ( Screen State ) Ġ-> ĠUnit ) Ġ= Ġobject Ġ: ĠBroadcast Receiver () Ġ{ ĊĠĠĠ Ġoverride Ġfun Ġon Receive ( context : ĠContext , Ġintent : ĠIntent ) Ġ= Ġreceived ( ĊĠĠĠĠĠĠĠ Ġwhen Ġ( intent . action ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠIntent . ACTION _ SCREEN _ OFF Ġ-> ĠScreen State . OFF ĊĠĠĠĠĠĠĠĠĠĠĠ ĠIntent . ACTION _ SCREEN _ ON Ġ-> ĠScreen State . ON ĊĠĠĠĠĠĠĠĠĠĠĠ Ġelse Ġ-> ĠScreen State . UNKNOWN ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ) Ċ }. also Ġ{ Ġregister Receiver ( it , ĠIntent Filter ( Intent . ACTION _ SCREEN _ ON ). apply Ġ{ Ġadd Action ( Intent . ACTION _ SCREEN _ OFF ) Ġ}) Ġ} Ċ // ---------------- ---------- Ċ