#Region Service Attributes
#StartAtBoot: False
#End Region
Sub Process_Globals
Public progressValue As Int
Public progressText, lblFile As String
Public SendingFile As Boolean
Public ReceivingFile As Boolean
Private timer1 As Timer
Private astream As AsyncStreams
Private countingStream As CountingInputStream
Private totalSizeForSending As Long
Private currentFile As String
End Sub
Sub Service_Create
timer1.Initialize("timer1", 1000)
End Sub
Sub Service_Start (StartingIntent As Intent)
End Sub
Private Sub UpdateUI
CallSub(Main, "UpdateUI")
End Sub
Private Sub UpdateProgress
CallSub(Main, "UpdateProgress")
End Sub
Public Sub SendFile(Dir As String, FileName As String)
Dim totalSizeForSending As Long = File.Size(Dir, FileName)
Dim In As InputStream = File.OpenInput(Dir, FileName)
countingStream.Initialize(In)
currentFile = FileName.SubString(FileName.LastIndexOf("/") + 1)
astream.Write(currentFile.GetBytes("UTF8")) 'write the file name
astream.WriteStream(countingStream, totalSizeForSending)
lblFile = "Sending: " & currentFile
timer1.Enabled = True
SendingFile = True
UpdateProgress
End Sub
Private Sub StartAStream (In As InputStream, out As OutputStream)
Log("StartAStream")
astream.InitializePrefix(In, True, out, "astream")
If File.ExternalWritable Then
astream.StreamFolder = File.DirDefaultExternal
Else
astream.StreamFolder = File.DirInternalCache
End If
End Sub
Sub Astream_Error
Log("Error: " & LastException)
astream.Close
AStream_Terminated 'manually call this method as it will not be called
'when we explicitly close the connection.
End Sub
Sub AStream_Terminated
timer1.Enabled = False
ReceivingFile = False
SendingFile = False
UpdateUI
End Sub
Sub AStream_NewStream (Dir As String, FileName As String)
'this event is raised when a file was received successfully
Timer1_Tick
timer1.Enabled = False
lblFile = currentFile & " completed"
ReceivingFile = False
UpdateProgress
File.Copy(Dir, FileName, Dir, currentFile)
File.Delete(Dir, FileName)
End Sub
Sub AStream_NewData (Buffer() As Byte)
'get the file name
timer1.Enabled = True
currentFile = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
lblFile = "Receiving file: " & currentFile
ReceivingFile = True
End Sub
Sub Timer1_Tick
Dim count, total As Long
If SendingFile Then
count = countingStream.count
total = totalSizeForSending
If count = total Then
lblFile = currentFile & " completed"
'stop the timer.
'when a file is received the NewStream event will be raised
SendingFile = False
timer1.Enabled = False
End If
Else If ReceivingFile Then
count = astream.StreamReceived
total = astream.StreamTotal
End If
progressValue = 100 * count / total
progressText = NumberFormat2(count / 1000, 0, 0, 0, True) & _
"kb / " & NumberFormat2(total / 1000, 0, 0, 0, True) & "kb"
UpdateProgress
End Sub
Sub Service_Destroy
End Sub