package Ġpak utom a . ii jm io coupon widget . widget Ċ Ċ import Ġandroid . annotation . Target Api Ċ import Ġandroid . app . Pending Intent Ċ import Ġandroid . app . Pending Intent . FLAG _ CANCEL _ CURRENT Ċ import Ġandroid . app widget . App Widget Manager Ċ import Ġandroid . app widget . App Widget Provider Ċ import Ġandroid . content . Component Name Ċ import Ġandroid . content . Context Ċ import Ġandroid . content . Intent Ċ import Ġandroid . view . View Ċ import Ġandroid . widget . Remote Views Ċ Ċ import Ġjava . util . Locale Ċ Ċ import Ġpak utom a . ii jm io coupon widget . R Ċ import Ġpak utom a . ii jm io coupon widget . service . Fetch Rem ains JobService Ċ import Ġpak utom a . ii jm io coupon widget . service . Switch Coupon Service Ċ import Ġandroid . app . job . Job Info Ċ import Ġandroid . app . job . Job Scheduler Ċ import Ġandroid . app . Notification Manager Ċ import Ġandroid . app . Notification Channel Ċ import Ġandroid . os . Build Ċ import Ġpak utom a . ii jm io coupon widget . service . Fetch Rem ains Service Ċ import Ġjava . util . concurrent . TimeUnit ĊĊ Ċ /** Ċ Ġ* ĠImplementation Ġof ĠApp ĠWidget Ġfunctionality . Ċ Ġ*/ Ċ Ċ class ĠSwitch Widget Ġ: ĠApp Widget Provider () Ġ{ ĊĠĠĠ Ġcompanion Ġobject Ġ{ ĊĠĠĠĠĠĠĠ Ġprivate Ġconst Ġval ĠNOT IFICATION _ CHANNEL _ ID Ġ= Ġ" switch _ service " ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġon Receive ( context : ĠContext , Ġintent : ĠIntent ) Ġ{ ĊĠĠĠĠĠĠĠ Ġsuper . on Receive ( context , Ġintent ) ĊĠĠĠĠĠĠĠ Ġif Ġ( intent . action Ġ== ĠIntent . ACTION _ BOOT _ COMPLETED ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġstart Fetch Rem ains Service ( context ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġif ( intent . action Ġ== ĠIntent . ACTION _ MY _ PACKAGE _ REPL AC ED ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġregister Fetch Rem ains JobService ( context ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġregister Notification Channel ( context ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġstart Fetch Rem ains Service ( context ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġon Enabled ( context : ĠContext ) Ġ{ ĊĠĠĠĠĠĠĠ Ġsuper . on Enabled ( context ) ĊĠĠĠĠĠĠĠ Ġregister Fetch Rem ains JobService ( context ) ĊĠĠĠĠĠĠĠ Ġregister Notification Channel ( context ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun ĠonUpdate ( context : ĠContext , Ġapp Widget Manager : ĠApp Widget Manager , Ġapp Widget Ids : ĠInt Array ) Ġ{ ĊĠĠĠĠĠĠĠ Ġsuper . on Update ( context , Ġapp Widget Manager , Ġapp Widget Ids ) ĊĠĠĠĠĠĠĠ Ġstart Fetch Rem ains Service ( context ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġon Disabled ( context : ĠContext ) Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġjob Scheduler Ġ= Ġcontext . getSystem Service ( Context . JOB _ SCHEDUL ER _ SERVICE ) Ġas ĠJob Scheduler ĊĠĠĠĠĠĠĠ Ġjob Scheduler . cancel ( context . resources . getInteger ( R . integer . periodic _ fetch _ remain s )) ĊĠĠĠĠĠĠĠ Ġsuper . on Disabled ( context ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġregister Notification Channel ( context : ĠContext ) Ġ{ ĊĠĠĠĠĠĠĠ Ġ@ Target Api ( Build . VERSION _ CODES . O ) ĊĠĠĠĠĠĠĠ Ġif Ġ( Build . VERSION . SDK _ INT Ġ>= ĠBuild . VERSION _ CODES . O ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġmanager Ġ= Ġcontext . getSystem Service ( Context . NOTIFICATION _ SERVICE ) Ġas ĠNotification Manager ĊĠĠĠĠĠĠĠĠĠĠĠ Ġif ( manager . get Notification Channel ( NOTIFICATION _ CHANNEL _ ID ) Ġ== Ġnull ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġchannel Ġ= ĠNotification Channel ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠNOT IFICATION _ CHANNEL _ ID , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġcontext . getString ( R . string . switch _ service ), ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠNotification Manager . IMPORT ANCE _ MIN ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġmanager . create Notification Channel ( channel ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġregister Fetch Rem ains JobService ( context : ĠContext ) Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġjob Scheduler Ġ= Ġcontext . getSystem Service ( Context . JOB _ SCHEDUL ER _ SERVICE ) Ġas ĠJob Scheduler ĊĠĠĠĠĠĠĠ Ġval Ġjobs Ġ= Ġjob Scheduler . all Pending Jobs ĊĠĠĠĠĠĠĠ Ġval Ġjob Id Ġ= Ġcontext . resources . getInteger ( R . integer . periodic _ fetch _ remain s ) ĊĠĠĠĠĠĠĠ Ġif ( jobs ?. any Ġ{ Ġit . id Ġ== Ġjob Id Ġ} Ġ!= Ġtrue ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġjob Info Ġ= ĠJob Info . Builder ( jobId , ĠComponent Name ( context , ĠFetch Rem ains JobService :: class . java )) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. set Required Network Type ( Job Info . NETWORK _ TYPE _ ANY ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. set Periodic ( TimeUnit . MIN UTES . to Millis ( 1 5 )) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. set Pers isted ( true ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. build () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġjob Scheduler . schedule ( job Info ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġstart Fetch Rem ains Service ( context : ĠContext ) Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġintent Ġ= ĠIntent ( context , ĠFetch Rem ains Service :: class . java ) ĊĠĠĠĠĠĠĠ Ġ@ Target Api ( Build . VERSION _ CODES . O ) ĊĠĠĠĠĠĠĠ Ġif Ġ( Build . VERSION . SDK _ INT Ġ>= ĠBuild . VERSION _ CODES . O ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġcontext . start Foreground Service ( intent ) ĊĠĠĠĠĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġcontext . start Service ( intent ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} Ċ } Ċ Ċ fun Ġchange To Fetching Mode ( context : ĠContext ) Ġ{ ĊĠĠĠ Ġval Ġremote Views Ġ= ĠRemote Views ( context . packageName , ĠR . layout . switch _ widget ) ĊĠĠĠ Ġremote Views . setText View Text ( R . id . data _ traffic , Ġ" åıĸå¾Ĺ ä¸Ń ") ĊĠĠĠ Ġval Ġthis Widget Ġ= ĠComponent Name ( context , ĠSwitch Widget :: class . java ) ĊĠĠĠ Ġval Ġmanager Ġ= ĠApp Widget Manager . getInstance ( context ) ĊĠĠĠ Ġmanager . update App Widget ( this Widget , Ġremote Views ) Ċ } Ċ Ċ fun Ġchange To Wait Mode ( context : ĠContext , Ġis Coupon Enabled : ĠBoolean ) Ġ{ ĊĠĠĠ Ġval Ġremote Views Ġ= ĠRemote Views ( context . packageName , ĠR . layout . switch _ widget ) ĊĠĠĠ Ġremote Views . setView Visibility ( R . id . coupon _ switch _ top _ on _ end , ĠView . IN VISIBLE ) ĊĠĠĠ Ġremote Views . setView Visibility ( R . id . coupon _ switch _ top _ start , ĠView . IN VISIBLE ) ĊĠĠĠ Ġremote Views . setView Visibility ( R . id . coupon _ switch _ top _ on _ start , Ġif Ġ( is Coupon Enabled ) ĠView . VISIBLE Ġelse ĠView . IN VISIBLE ) ĊĠĠĠ Ġremote Views . setView Visibility ( R . id . coupon _ switch _ top _ end , Ġif Ġ( is Coupon Enabled ) ĠView . IN VISIBLE Ġelse ĠView . VISIBLE ) ĊĠĠĠ Ġval Ġthis Widget Ġ= ĠComponent Name ( context , ĠSwitch Widget :: class . java ) ĊĠĠĠ Ġval Ġmanager Ġ= ĠApp Widget Manager . getInstance ( context ) ĊĠĠĠ Ġmanager . update App Widget ( this Widget , Ġremote Views ) Ċ } Ċ Ċ fun Ġupdate Switch Status ( context : ĠContext , Ġhas Token : ĠBoolean , Ġcould Get : ĠBoolean , Ġremains : ĠInt Ġ= Ġ- 1 , Ġis Coupon Enabled : ĠBoolean Ġ= Ġfalse ) Ġ{ ĊĠĠĠ Ġval Ġremote Views Ġ= ĠRemote Views ( context . packageName , ĠR . layout . switch _ widget ) ĊĠĠĠ Ġremote Views . setView Visibility ( R . id . coupon _ switch _ base _ on , Ġif Ġ( is Coupon Enabled ) ĠView . VISIBLE Ġelse ĠView . IN VISIBLE ) ĊĠĠĠ Ġremote Views . setView Visibility ( R . id . coupon _ switch _ base , Ġif Ġ( is Coupon Enabled ) ĠView . IN VISIBLE Ġelse ĠView . VISIBLE ) ĊĠĠĠ Ġremote Views . setView Visibility ( R . id . coupon _ switch _ top _ on _ end , Ġif Ġ( is Coupon Enabled ) ĠView . VISIBLE Ġelse ĠView . IN VISIBLE ) ĊĠĠĠ Ġremote Views . setView Visibility ( R . id . coupon _ switch _ top _ start , Ġif Ġ( is Coupon Enabled ) ĠView . IN VISIBLE Ġelse ĠView . VISIBLE ) ĊĠĠĠ Ġremote Views . setView Visibility ( R . id . coupon _ switch _ top _ on _ start , ĠView . IN VISIBLE ) ĊĠĠĠ Ġremote Views . setView Visibility ( R . id . coupon _ switch _ top _ end , ĠView . IN VISIBLE ) ĊĠĠĠ Ġif Ġ(! has Token ) Ġ{ ĊĠĠĠĠĠĠĠ Ġremote Views . setText View Text ( R . id . data _ traffic , Ġ" æľª èªį è¨¼ ") ĊĠĠĠ Ġ} Ġelse Ġif Ġ(! could Get ) Ġ{ ĊĠĠĠĠĠĠĠ Ġremote Views . setText View Text ( R . id . data _ traffic , Ġ" ãĤ¨ãĥ© ãĥ¼ ") ĊĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠ Ġremote Views . setText View Text ( R . id . data _ traffic , Ġconvert Prefix String ( remain s )) ĊĠĠĠ Ġ} ĊĠĠĠ Ġval Ġclick Intent Ġ= ĠIntent ( context , ĠSwitch Coupon Service :: class . java ) ĊĠĠĠ Ġval Ġpending Intent Ġ= Ġif Ġ( Build . VERSION . SDK _ INT Ġ>= ĠBuild . VERSION _ CODES . O ) Ġ{ ĊĠĠĠĠĠĠĠ ĠPending Intent . get Foreground Service ( context , Ġ 0 , Ġclick Intent , ĠFLAG _ CANCEL _ CURRENT ) ĊĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠ ĠPending Intent . getService ( context , Ġ 0 , Ġclick Intent , ĠFLAG _ CANCEL _ CURRENT ) ĊĠĠĠ Ġ} ĊĠĠĠ Ġremote Views . setOn Click Pending Intent ( R . id . transparent _ button , Ġpending Intent ) ĊĠĠĠ Ġval Ġthis Widget Ġ= ĠComponent Name ( context , ĠSwitch Widget :: class . java ) ĊĠĠĠ Ġval Ġmanager Ġ= ĠApp Widget Manager . getInstance ( context ) ĊĠĠĠ Ġmanager . update App Widget ( this Widget , Ġremote Views ) Ċ } Ċ Ċ private Ġfun Ġconvert Prefix String ( traffic : ĠInt ): ĠString Ġ{ ĊĠĠĠ Ġreturn Ġwhen Ġ( traffic ) Ġ{ ĊĠĠĠĠĠĠĠ Ġin Ġ 0 .. 9 9 9 Ġ-> ĠString . format ( Locale . US , Ġ"% d MB ", Ġtraffic ) ĊĠĠĠĠĠĠĠ Ġin Ġ 1 0 0 0 .. 1 0 0 0 0 Ġ-> ĠString . format ( Locale . US , Ġ"% 1 $. 2 f GB ", Ġtraffic Ġ/ Ġ 1 0 0 0 . 0 ) ĊĠĠĠĠĠĠĠ Ġelse Ġ-> ĠString . format ( Locale . US , Ġ"% 1 $. 1 f GB ", Ġtraffic Ġ/ Ġ 1 0 0 0 . 0 ) ĊĠĠĠ Ġ} Ċ } Ċ Ċ fun Ġset Switch Pending Intent ( context : ĠContext ) Ġ{ ĊĠĠĠ Ġval Ġremote Views Ġ= ĠRemote Views ( context . packageName , ĠR . layout . switch _ widget ) ĊĠĠĠ Ġval Ġclick Intent Ġ= ĠIntent ( context , ĠSwitch Coupon Service :: class . java ) ĊĠĠĠ Ġval Ġpending Intent Ġ= Ġif Ġ( Build . VERSION . SDK _ INT Ġ>= ĠBuild . VERSION _ CODES . O ) Ġ{ ĊĠĠĠĠĠĠĠ ĠPending Intent . get Foreground Service ( context , Ġ 0 , Ġclick Intent , ĠFLAG _ CANCEL _ CURRENT ) ĊĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠ ĠPending Intent . getService ( context , Ġ 0 , Ġclick Intent , ĠFLAG _ CANCEL _ CURRENT ) ĊĠĠĠ Ġ} ĊĠĠĠ Ġremote Views . setOn Click Pending Intent ( R . id . transparent _ button , Ġpending Intent ) ĊĠĠĠ Ġval Ġthis Widget Ġ= ĠComponent Name ( context , ĠSwitch Widget :: class . java ) ĊĠĠĠ Ġval Ġmanager Ġ= ĠApp Widget Manager . getInstance ( context ) ĊĠĠĠ Ġmanager . update App Widget ( this Widget , Ġremote Views ) Ċ }