Sub Process_Globals
Dim MainUrl, MainUrl2 As String
MainUrl = "http://www.testtest.bugs3.com/test/"
MainUrl2= "http://www.testtest.bugs3.com/test2/"
'Dim ImageFolder As String
Dim Files As List
Dim Bitmaps As List
Dim Images As List
Dim PhoneOrient As Phone
End Sub
Sub Globals
Dim iv As ImageView
Dim btnConnect1 As Button
Dim lblComment As Label
Dim btnConnect2 As Button
End Sub
Sub Activity_Create(FirstTime As Boolean)
PhoneOrient.SetScreenOrientation(1)
Activity.LoadLayout("1")
Bitmaps.Initialize
Images.Initialize
HttpUtils.CallbackActivity = "Main"
HttpUtils.CallbackJobDoneSub = "JobDone"
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub btnConnect1_Click
lblComment.Visible = True
lblComment.Text = "Connection..."
btnConnect1.Visible = False
HttpUtils.CallbackUrlDoneSub = "" 'don't want to handle this event
HttpUtils.Download("Main page", MainUrl)
' ProgressDialogShow("Fetching data...")
lblComment.Text = "Fetching data..."
End Sub
Sub btnConnect2_Click
lblComment.Visible = True
lblComment.Text = "Connection..."
btnConnect2.Visible = False
HttpUtils.CallbackUrlDoneSub = "" 'don't want to handle this event
HttpUtils.Download("Main page", MainUrl2)
' ProgressDialogShow("Fetching data...")
lblComment.Text = "Fetching data..."
End Sub
Sub Activity_Resume
'This is important as it is possible that the activity was in the background when the job has finished.
If HttpUtils.Complete Then JobDone(HttpUtils.Job)
End Sub
'Usually we will want to have a single JobDone sub for all jobs.
'This allows us to check HttpUtils.Complete flag in Activity_Resume (as shown above).
Sub JobDone(Job As String)
Select job
Case "Main page"
HandleMainPage
Case "Images"
ImagesJobDone
End Select
HttpUtils.Complete = False
End Sub
Sub JobDone2(Job As String)
Select job
Case "Main page"
HandleMainPage2
Case "Images"
ImagesJobDone2
End Select
HttpUtils.Complete = False
End Sub
'Parse the main page and find the image links
Sub HandleMainPage
If HttpUtils.IsSuccess(Mainurl) = False Then
ToastMessageShow("Error downloading main page.", True)
Return
End If
ResetImagesBackground
start = DateTime.Now
Dim TextReader1 As TextReader
TextReader1.Initialize(HttpUtils.GetInputStream(MainUrl))
Dim pattern, class As String
class = "<td class=" & QUOTE & "thumbcell" & QUOTE & ">"
pattern = "img src=\q([^q]+)\q".Replace("q", QUOTE)
Dim links As List
links.Initialize
Dim line As String
line = TextReader1.ReadLine
Do While line <> Null
If line.IndexOf(class) > -1 Then
Dim m As Matcher
m = Regex.Matcher(pattern, line)
If m.Find Then
links.Add("http://www.testtest.bugs3.com/test/" & m.Group(1).Replace("_small","")) 'add the image link
lblComment.Text = links.Size & " images found"
Images.Add(m.Group(1))
DoEvents
End If
End If
line = TextReader1.ReadLine
Loop
TextReader1.Close
Log("done parsing main page: " & (DateTime.Now - start))
HttpUtils.CallbackUrlDoneSub = "ImageUrlDone"
HttpUtils.DownloadList("Images", links)
ImageIndex = 0
' btnConnect1.Enabled = True
ProgressDialogHide
End Sub
Sub HandleMainPage2
If HttpUtils.IsSuccess(Mainurl2) = False Then
ToastMessageShow("Error downloading main page.", True)
Return
End If
ResetImagesBackground
start = DateTime.Now
Dim TextReader2 As TextReader
TextReader2.Initialize(HttpUtils.GetInputStream(MainUrl2))
Dim pattern, class As String
class = "<td class=" & QUOTE & "thumbcell" & QUOTE & ">"
pattern = "img src=\q([^q]+)\q".Replace("q", QUOTE)
Dim links As List
links.Initialize
Dim line As String
line = TextReader2.ReadLine
Do While line <> Null
If line.IndexOf(class) > -1 Then
Dim m As Matcher
m = Regex.Matcher(pattern, line)
If m.Find Then
links.Add("http://www.testtest.bugs3.com/test2/" & m.Group(1).Replace("_small",""))'add the image link
lblComment.Text = links.Size & " images found"
Images.Add(m.Group(1))
DoEvents
End If
End If
line = TextReader2.ReadLine
Loop
TextReader2.Close
Log("done parsing main page: " & (DateTime.Now - start))
HttpUtils.CallbackUrlDoneSub = "ImageUrlDone"
HttpUtils.DownloadList("Images", links)
ImageIndex = 0
' btnConnect2.Enabled = True
ProgressDialogHide
End Sub
Sub ImageUrlDone (Url As String)
Bitmaps.Add(HttpUtils.GetBitmap(Url))
lblComment.Text = "loading images"
'The SuccessfulUrls map holds all the successful links. We are removing this link from the list
'to avoid dealing with this link again in ImagesJobDone
HttpUtils.SuccessfulUrls.Remove(Url)
End Sub
Sub ImagesJobDone
'In most cases this For loop will not be used, assuming that ImageUrlDone was called for each Url.
'However if the activity was paused during the download it is possible that we have missed some calls.
'So here we make sure that all downloaded images are visible.
' For i = 0 To HttpUtils.SuccessfulUrls.Size - 1
' iv.Bitmap = HttpUtils.GetBitmap(HttpUtils.SuccessfulUrls.GetKeyAt(i))
' iv.Gravity = Gravity.FILL
' Next
lblComment.Visible = False
btnConnect1.Visible = True
StartActivity(result)
End Sub
Sub ImagesJobDone2
'In most cases this For loop will not be used, assuming that ImageUrlDone was called for each Url.
'However if the activity was paused during the download it is possible that we have missed some calls.
'So here we make sure that all downloaded images are visible.
' For i = 0 To HttpUtils.SuccessfulUrls.Size - 1
' iv.Bitmap = HttpUtils.GetBitmap(HttpUtils.SuccessfulUrls.GetKeyAt(i))
' iv.Gravity = Gravity.FILL
' Next
lblComment.Visible = False
btnConnect2.Visible = True
StartActivity(result)
End Sub
Sub ResetImagesBackground
For i = 0 To Activity.NumberOfViews - 1
If Activity.GetView(i) Is ScrollView Then Activity.GetView(i).Color = Colors.Black
Next
End Sub