Sub DownloadAndSaveFile (Link As String, sFile As String)
Dialog.Title = sNomAPP & " NUEVA VERSION"
Dim rs As ResumableSub = Dialog.Show("Hay una Nueva Versión Disponoble. ¿Quieres Instalarla?", "", "", "")'SI "" NO
Dialog.GetButton(xui.DialogResponse_Positive).TextColor = xui.Color_Green
Dialog.GetButton(xui.DialogResponse_Cancel).TextColor = xui.Color_Red
Wait For (rs) Complete (pp As Int)
If pp = xui.DialogResponse_Positive Then
ProgressBarTrackDownload.Progress = 0
LblTrackDownload.Text = 0
LblTrackDownloadTotal.Text = 0
MostrarPanel2(PnlTrackDownload, PnlCaja)
Wait For (DownloadAndTrackProgress(Link, sFile)) Complete (Success As Boolean)
If Success Then
Sleep(1000)
PnlTrackDownload.Visible = False
BlurScreen(False, PnlCaja)
fx.ShowExternalDocument(File.GetUri(File.DirTemp, sFile))
ExitApplication
Else
Dialog.Title = sNomAPP
Dialog.ButtonsTextColor = xui.Color_Red
Dialog.Show("Ha Ocurrido un Error en la Descarga. Inténtelo Más Tarde", "", "", "")
End If
End If
End Sub
Private Sub DownloadAndTrackProgress (url As String, sFile As String) As ResumableSub
Dim j As HttpJob
j.Initialize("", Me)
j.Download(url)
Sleep(0)
Dim TaskToJob As Map = HttpUtils2Service.TaskIdToJob
Do While HttpUtils2Service.TaskIdToJob.IsInitialized = False
Sleep(30)
Loop
Dim TaskId As Int
For Each id As Int In TaskToJob.Keys
If TaskToJob.Get(id) = j Then
TaskId = id
Exit
End If
Next
Dim b() As Boolean = Array As Boolean(False)
TrackProgress(j, b, TaskId)
Wait For (j) JobDone (j As HttpJob)
b(0) = True
If j.Success Then
LblTrackDownload.Text = LblTrackDownloadTotal.Text
ProgressBarTrackDownload.Progress = 100
Dim out As OutputStream = File.OpenOutput(File.DirTemp, sFile, False)
File.Copy2(j.GetInputStream, out)
out.Close
End If
j.Release
Return j.Success
End Sub
Private Sub TrackProgress (j As HttpJob, Stop() As Boolean, TaskId As Int)
Do While Stop(0) = False
If j.Out.IsInitialized Then
Dim TotalLength As Long = j.Response.ContentLength
Dim size As Long = File.Size(HttpUtils2Service.TempFolder, TaskId)
ProgressBarTrackDownload.Progress = 100 * size / TotalLength
LblTrackDownload.Text = size
LblTrackDownloadTotal.Text = TotalLength
End If
Sleep(100)
Loop
End Sub
Sub MostrarPanel2 (PanelMostrar As B4XView, PanelOrigen As B4XView)
Dim a As Int = 30
PanelOrigen.Enabled = False
PanelMostrar.Visible = True
PanelMostrar.Top = (MainForm.RootPane.Top + (MainForm.RootPane.Height - PanelMostrar.Height) / 2) - a
PanelMostrar.Left = (MainForm.RootPane.Left + (MainForm.RootPane.Width - PanelMostrar.Width) / 2)
BlurScreen(True, PanelOrigen)
End Sub