Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim allimages, todownload As List
Private FilesToDownload As ListView
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("Layout1")
allimages.Initialize
todownload.Initialize
FilesToDownload.Clear
Dim php As HttpJob
php.Initialize("getimagesizes",Me)
php.Download("http://snapshots.basic4android.de/imagesizes.php")
' Go ahead in JobDone to get the list
End Sub
Sub JobDone(Job As HttpJob)
ProgressDialogHide
If Job.Success = False Then
Log(Job.ErrorMessage)
else If Job.Success Then
Dim res As String
res = Job.GetString
Log("JobName: "&Job.JobName)
If Job.JobName = "getimagesizes" Then
'Log("result is: "&res)
Dim parser As JSONParser
parser.Initialize(res)
Dim root As List = parser.NextArray
allimages = root
Log("Count images: "&allimages.Size)
'
' We now get 50 Images from the list randomly and put them into a downloadqueue
For i = 1 To 50
Dim rand As Int
rand = Rnd(0,allimages.Size)
Dim m As Map = allimages.Get(rand)
todownload.Add(m)
Next
Log("List of files to download contains "&todownload.Size&" items...")
For Each colroot As Map In todownload
Dim height As Int = colroot.Get("height")
Dim width As Int = colroot.Get("width")
Dim filename As String = colroot.Get("filename")
Dim ext As String = colroot.Get("ext")
Log(filename&" width="&width&", height="&height)
FilesToDownload.AddTwoLines2(filename,"width="&width&", height="&height,colroot)
Next
If todownload.Size > 0 Then
Dim m As Map = todownload.Get(0)
Log("Download: "&m.Get("filename"))
Dim j As HttpJob
j.Initialize("DownloadImage",Me)
j.Tag = m
j.Download("http://snapshots.basic4android.de/"&m.Get("filename"))
End If
End If
If Job.JobName = "DownloadImage" Then
Dim m As Map = Job.Tag
Dim OutStream As OutputStream
Log("DownloadReady: "&Job.Tag)
OutStream = File.OpenOutput(File.DirRootExternal, m.Get("filename"), False)
File.Copy2(Job.GetInputStream,OutStream) ' save the file
OutStream.Close
Log(m.Get("filename")&" is written to "&File.DirRootExternal)
' Remove the first job from queue
If todownload.Size > 0 Then
todownload.RemoveAt(0)
End If
' Refill Listview
FilesToDownload.Clear
For Each colroot As Map In todownload
Dim height As Int = colroot.Get("height")
Dim width As Int = colroot.Get("width")
Dim filename As String = colroot.Get("filename")
Dim ext As String = colroot.Get("ext")
Log(filename&" width="&width&", height="&height)
FilesToDownload.AddTwoLines2(filename,"width="&width&", height="&height,colroot)
Next
' Check if there are more files to download. If yes. Download them
If todownload.Size > 0 Then
Dim m As Map = todownload.Get(0)
Dim j As HttpJob
j.Initialize("DownloadImage",Me)
j.Tag = m
j.Download("http://snapshots.basic4android.de/"&m.Get("filename"))
Else
LogColor("======================================",Colors.Green)
LogColor("===== ALL FILES FROM QUERE =======",Colors.Green)
LogColor("===== ARE FINISHED DOWNLOADING =======",Colors.Green)
LogColor("======================================",Colors.Green)
End If
End If
Else
ToastMessageShow("Error: " & Job.ErrorMessage, True)
End If
Job.Release
End Sub