package Ġyet . update Ċ Ċ import Ġandroid . app . Download Manager Ċ import Ġandroid . content . Broadcast Receiver Ċ import Ġandroid . content . Context Ċ import Ġandroid . content . Intent Ċ import Ġandroid . net . Uri Ċ import Ġandroid . os . Environment Ċ import Ġyet . database . Map Table Ċ import Ġyet . util . app . App Ċ import Ġyet . util . log . log d Ċ Ċ class ĠA pk Down Receiver Ġ: ĠBroadcast Receiver () Ġ{ ĊĊ ĉ override Ġfun Ġon Receive ( context : ĠContext , Ġintent : ĠIntent ) Ġ{ Ċĉ ĉ val Ġid Ġ= Ġintent . getLong Extra ( Download Manager . EXTRA _ DOWNLOAD _ ID , Ġ- 1 ) Ċĉ ĉ log d (" Down ĠReceiver ĠID : Ġ", Ġid ) Ċĉ ĉ if Ġ( id Ġ== Ġ- 1 L ) Ġ{ Ċĉĉ ĉ return Ċĉ ĉ } Ċĉ ĉ down Map . getString ( id . toString ()) Ġ?: Ġreturn Ċĉ ĉ val Ġfile Uri Ġ= ĠApp . download Manager . get Uri For Downloaded File ( id ) Ġ?: Ġreturn Ċĉ ĉ log d ( file Uri ) Ċĉ ĉ val Ġi Ġ= ĠIntent ( Intent . ACTION _ VIEW ) Ċĉ ĉ i . add Flags ( Intent . FLAG _ ACTIVITY _ NEW _ TASK ) Ċĉ ĉ i . setData And Type ( file Uri , Ġ" application / vnd . android . package - archive ") Ċĉ ĉ context . startActivity ( i ) Ċ ĉ } ĊĊĊ ĉ comp anion Ġobject Ġ{ Ċĉ ĉ val Ġdown Map Ġ= ĠMap Table (" download Tasks ") ĊĊĉ ĉ fun Ġdownload And Install ( url : ĠString , Ġtitle : ĠString , Ġdesc : ĠString Ġ= Ġ"") Ġ{ Ċĉĉ ĉ val Ġdm Ġ= ĠApp . download Manager Ċĉĉ ĉ val Ġreq Ġ= ĠDownload Manager . Request ( Uri . parse ( url )) Ċĉĉ ĉ if Ġ( title . isNotEmpty ()) Ġ{ Ċĉĉĉ ĉ req . setTitle ( title ) Ċĉĉ ĉ } Ċĉĉ ĉ req . set Destination In External Public Dir ( Environment . DIRECTORY _ DOWNLOAD S , Ġ"${ App . app _ name }. apk ") Ċĉĉ ĉ if Ġ( desc . isNotEmpty ()) Ġ{ Ċĉĉĉ ĉ req . setDescription ( desc ) Ċĉĉ ĉ } Ċĉĉ ĉ val Ġdown Id Ġ= Ġdm . enqueue ( req ) Ċĉĉ ĉ log d (" Download Task Ġenqueue : Ġ", Ġdown Id ) Ċĉĉ ĉ down Map . put ( down Id . toString (), Ġurl ) ĊĊĉ ĉ } ĊĊ ĉ } Ċ } Ċ