package Ġml . ars en iy 8 9 9 . bank account test . viewModel Ċ Ċ import Ġandroid . content . Broadcast Receiver Ċ import Ġandroid . content . Context Ċ import Ġandroid . content . Intent Ċ import Ġandroid . net . Connectivity Manager Ċ import Ġandroid . net . Network Capabilities Ċ import Ġandroid . os . Build ĊĊ Ċ class ĠNetwork State Receiver Ġ: ĠBroadcast Receiver () Ċ { ĊĉĊ ĉ protected Ġvar Ġlisteners : ĠMutable Set < Network State Receiver Listener > Ċ ĉ protected Ġvar Ġconnected : ĠBoolean ? Ġ= Ġnull ĊĉĊ ĉ init Ċ ĉ { Ċĉ ĉ listeners Ġ= ĠHashSet () Ċĉ ĉ connected Ġ= Ġnull Ċ ĉ } ĊĉĊ ĉ override Ġfun Ġon Receive ( context : ĠContext , Ġintent : ĠIntent ?) Ċ ĉ { Ċĉ ĉ if Ġ( intent Ġ== Ġnull Ġ|| Ġintent . extras Ġ== Ġnull ) Ġreturn ĊĉĉĊĉ ĉ val Ġmanager Ġ= Ġcontext . getSystem Service ( Context . CONNECT IVITY _ SERVICE ) Ġas ĠConnect ivity Manager Ċĉ ĉ connected Ġ= Ġis Internet Available ( context ) ĊĉĉĊĉĉĊĉ ĉ notify State To All () Ċ ĉ } Ċ ĉ private Ġfun Ġis Internet Available ( context : ĠContext ): ĠBoolean Ġ{ Ċĉ ĉ var Ġresult Ġ= Ġfalse Ċĉ ĉ val Ġconnectivity Manager Ġ= Ċĉĉ ĉ context . getSystem Service ( Context . CONNECT IVITY _ SERVICE ) Ġas ĠConnect ivity Manager Ċĉ ĉ if Ġ( Build . VERSION . SDK _ INT Ġ>= ĠBuild . VERSION _ CODES . M ) Ġ{ Ċĉĉ ĉ val Ġnetwork Capabilities Ġ= Ġconnectivity Manager . active Network Ġ?: Ġreturn Ġfalse Ċĉĉ ĉ val Ġact Nw Ġ= Ċĉĉĉ ĉ connectivity Manager . get Network Capabilities ( network Capabilities ) Ġ?: Ġreturn Ġfalse Ċĉĉ ĉ result Ġ= Ġwhen Ġ{ Ċĉĉĉ ĉ act Nw . has Transport ( Network Capabilities . TRANSPORT _ WIFI ) Ġ-> Ġtrue Ċĉĉĉ ĉ act Nw . has Transport ( Network Capabilities . TRANSPORT _ CELL ULAR ) Ġ-> Ġtrue Ċĉĉĉ ĉ act Nw . has Transport ( Network Capabilities . TRANSPORT _ ETHER NET ) Ġ-> Ġtrue Ċĉĉĉ ĉ else Ġ-> Ġfalse Ċĉĉ ĉ } Ċĉ ĉ } Ġelse Ġ{ Ċĉĉ ĉ connectivity Manager . run Ġ{ Ċĉĉĉ ĉ connectivity Manager . active Network Info ?. run Ġ{ Ċĉĉĉĉ ĉ result Ġ= Ġwhen Ġ( type ) Ġ{ Ċĉĉĉĉĉ ĉ Connectivity Manager . TYPE _ WIFI Ġ-> Ġtrue Ċĉĉĉĉĉ ĉ Connectivity Manager . TYPE _ MOB ILE Ġ-> Ġtrue Ċĉĉĉĉĉ ĉ Connectivity Manager . TYPE _ ETHER NET Ġ-> Ġtrue Ċĉĉĉĉĉ ĉ else Ġ-> Ġfalse Ċĉĉĉĉ ĉ } ĊĉĉĉĉĉĊĉĉĉ ĉ } Ċĉĉ ĉ } Ċĉ ĉ } ĊĉĉĊĉ ĉ return Ġresult Ċ ĉ } Ċ ĉ private Ġfun Ġnotify State To All () Ċ ĉ { Ċĉ ĉ for Ġ( listener Ġin Ġlisteners ) Ġnotify State ( listener ) Ċ ĉ } ĊĉĊ ĉ private Ġfun Ġnotify State ( listener : ĠNetwork State Receiver Listener ?) Ċ ĉ { Ċĉ ĉ if Ġ( connected Ġ== Ġnull Ġ|| Ġlistener Ġ== Ġnull ) Ġreturn ĊĉĉĊĉ ĉ if Ġ( connected Ġ== Ġtrue ) Ġlistener . network Available () Ċĉ ĉ else Ġlistener . network Unavailable () Ċ ĉ } ĊĉĊ ĉ fun Ġadd Listener ( l : ĠNetwork State Receiver Listener ) Ċ ĉ { Ċĉ ĉ listeners . add ( l ) Ċĉ ĉ notify State ( l ) Ċ ĉ } ĊĉĊ ĉ fun Ġremove Listener ( l : ĠNetwork State Receiver Listener ) Ċ ĉ { Ċĉ ĉ listeners . remove ( l ) Ċ ĉ } ĊĉĊ ĉ interface ĠNetwork State Receiver Listener Ċ ĉ { Ċĉ ĉ fun Ġnetwork Available () Ċĉ ĉ fun Ġnetwork Unavailable () Ċ ĉ } Ċ }