Sub Process_Globals
Dim developerKey, developerSecret, token, tokenSecret As String
developerKey = "640skmyb******"
developerSecret = "pbjh25uky******"
Dim infoLink, metadataLink, downloadFileLink, uploadLink As String
infoLink = "https://api.dropbox.com/1/account/info"
metadataLink = "https://api.dropbox.com/1/metadata/dropbox"
downloadFileLink = "https://api-content.dropbox.com/1/files/dropbox"
uploadLink = "https://api-content.dropbox.com/1/files/dropbox"
Dim currentPath As String
currentPath = "/"
Type FileEntry(FilePath As String, IsDir As Boolean)
Dim downloadedFile As FileEntry
Dim targetPath As String
Dim FilesCache As Map
End Sub
Sub Globals
Dim ListView1 As ListView
Dim lblPath As Label
Dim FileDialog As FileDialog
End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
If developerKey = "" OR developerSecret = "" Then
Msgbox("You must first set developerKey and developerSecret!", "")
Activity.Finish
End If
FilesCache.Initialize
FileDialog.FilePath = File.DirRootExternal
End If
Activity.LoadLayout("Main")
ListView1.Width = 100%x
ListView1.Height = 100%y - ListView1.Top
lblPath.Width = 100%x
End Sub
Sub Activity_Resume
If HttpUtils.Working = True Then ProgressDialogShow2("Waiting for operation to complete...", False)
If HttpUtils.Complete = True Then JobDone(HttpUtils.Job)
If token = "" Then LoadTokenKey
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub LoadTokenKey
If File.Exists(File.DirInternal, "token.txt") Then
Dim l As List
l = File.ReadList(File.DirInternal, "token.txt")
tokenKey = l.Get(0)
tokenSecret = l.Get(1)
If HttpUtilsService.OAuth.IsInitialized = False Then
HttpUtilsService.OAuth.Initialize(developerKey, developerSecret)
End If
HttpUtilsService.OAuth.SetTokenWithSecret(tokenKey, tokenSecret)
HttpUtils.CallbackActivity = "Main"
HttpUtils.CallbackJobDoneSub = "JobDone"
ChangePath(currentPath, True)
Else
StartActivity(UserForm)
End If
End Sub
Sub ChangePath(p As String, AllowFromCache As Boolean)
targetPath = p
If AllowFromCache AND FilesCache.ContainsKey(p) Then
HandleChangePathResult(FilesCache.Get(p))
Else
ProgressDialogShow2("Connecting to Dropbox server...", False)
HttpUtils.Download("changepath", HttpUtils.EncodeUrl(metadataLink & p & "?"))
End If
End Sub
Sub HandleChangePathResult(Response As Map)
ListView1.Clear
Dim files As List
files = Response.Get("contents")
currentPath = targetPath
FilesCache.Put(currentPath, Response)
lblPath.Text = "Path: " & currentPath
Dim m As Map
If currentPath <> "/" Then
Dim fe As FileEntry
fe.IsDir = True
fe.FilePath = currentPath.SubString2(0, currentPath.LastIndexOf2("/", currentPath.Length - 2) + 1)
ListView1.AddTwoLines2("Up", "Folder", fe)
End If
For i = 0 To files.Size - 1
m = files.Get(i)
If m.Get("is_dir") = True Then
Dim fe As FileEntry
fe.IsDir = True
fe.FilePath = m.Get("path")
fe.FilePath = fe.FilePath.SubString(currentPath.Length)
ListView1.AddTwoLines2(fe.FilePath, "Folder", fe)
End If
Next
For i = 0 To files.Size - 1
m = files.Get(i)
If m.Get("is_dir") = False Then
Dim fe As FileEntry
fe.IsDir = False
fe.FilePath = m.Get("path")
fe.FilePath = fe.FilePath.SubString(currentPath.Length)
ListView1.AddTwoLines2(fe.FilePath, m.Get("size"), fe)
End If
Next
End Sub
Sub DownloadFile(FE As FileEntry)
downloadedFile = FE
ProgressDialogShow2("Downloading " & FE.FilePath & "...", False)
HttpUtils.Download("download", HttpUtils.EncodeUrl(downloadFileLink & currentPath & FE.FilePath & "?"))
End Sub
Sub HandleDownloadFileResult(in As InputStream)
Dim out As OutputStream
out = File.OpenOutput(File.DirRootExternal, downloadedFile.FilePath, False)
File.Copy2(in, out)
out.Close
ToastMessageShow(downloadedFile.FilePath & " downloaded successfully.", True)
End Sub
Sub UploadFile
If FileDialog.Show("Choose file to upload", "Ok", "Cancel", "", Null) = DialogResponse.POSITIVE Then
ProgressDialogShow2("Uploading " & FileDialog.ChosenName _
& " (" & (Ceil(File.Size(FileDialog.FilePath, FileDialog.ChosenName) / 1000)) & " Kb)", False)
HttpUtils.PostFile("upload", HttpUtils.EncodeUrl(uploadLink & currentPath), _
FileDialog.FilePath, FileDialog.ChosenName)
End If
End Sub
Sub HandleUploadFileResult
ToastMessageShow("File uploaded successfully.", True)
ChangePath(currentPath, False)
End Sub
Sub JobDone(Job As String)
ProgressDialogHide
If HttpUtils.IsSuccess(HttpUtils.Tasks.Get(0)) Then
Select Job
Case "download"
HandleDownloadFileResult(HttpUtils.GetInputStream(HttpUtils.Tasks.Get(0)))
Case "upload"
HandleUploadFileResult
Case "changepath"
Dim j As JSONParser
j.Initialize(HttpUtils.GetString(HttpUtils.Tasks.Get(0)))
Dim Response As Map
Response = j.NextObject
HandleChangePathResult(Response)
End Select
End If
HttpUtils.Complete = False
End Sub
Sub ListView1_ItemClick (Position As Int, Value As Object)
Dim fe As FileEntry
fe = Value
If fe.IsDir Then
Dim p As String
If fe.FilePath.StartsWith("/") = False Then
p = currentPath & fe.FilePath & "/"
Else
p = fe.FilePath
End If
ChangePath(p, True)
Else
ToastMessageShow("Long click to download file.", True)
End If
End Sub
Sub ListView1_ItemLongClick (Position As Int, Value As Object)
Dim fe As FileEntry
fe = Value
If fe.IsDir = False Then
DownloadFile(fe)
Else
ListView1_ItemClick(Position, Value)
End If
End Sub
Sub btnRefresh_Click
ChangePath(currentPath, False)
End Sub
Sub btnUpload_Click
UploadFile
End Sub