Android Tutorial Dropbox SDK - get informed about changes in the used Dropbox

Related Libraries:
B4A Dropbox SDK V2
B4J Dropbox SDK V2

You need to know/remember the dropboxId of the account you are actually using/requesting. And the latest cursor.
Use
B4X:
dbxUsers.CurrentAccount
to get the current Account.
B4X:
Sub dbxUsers_getCurrentAccount(account As FullAccount)
    Log($"dbxUsers_getCurrentAccount(${account})"$)
    kvs.Put("AccountID",account.AccountId) ' This is the ID of the Dropbox used. We want to track them so we need to remember this ID.
    Dim acc As String = account.Name
    Dim parser As JSONParser
    parser.Initialize(acc)
    Dim root As Map = parser.NextObject
    Dim abbreviated_name As String = root.Get("abbreviated_name")
    Dim familiar_name As String = root.Get("familiar_name")
    Dim surname As String = root.Get("surname")
    Dim given_name As String = root.Get("given_name")
    Dim display_name As String = root.Get("display_name")

    Log("Name? "&display_name)
    'Log("Name? "&acc.Get("display_name"))
    kvs.Put("AccountName",display_name)
    kvs.Put("AccountType",account.AccountType)

In this Example i want to get informed about changed in my hole Dropbox.

B4X:
dbxFiles.listFolder("",False,True,False,True,True)

You may want to get notified about any other Folder. For example

B4X:
dbxFiles.listFolder("/myspecial/folder/",False,True,False,True,True)

this results in a Call to the Event dbxFiles_listFolder
Note that the "error" is the LatestCursor value if the String is 750 Bytes long.

B4X:
Sub dbxFiles_listFolder(success As Boolean, content As List, error As String)
    Log($"dbxFiles_listFolders(${success}, ${content.Size}, ${error.Length} ->  ${error})"$)
    If error.Length = 750 Then
        Log("Cursor to KVS")
        kvs.Put("LatestCursor_"&loggedUserID,error)
    End If
    If content.Size > 0 Then
        For i = 0 To content.Size-1
            'Dim meta As Metadata = content.Get(i)
            'Log(meta.toString)
        Next
    End If
End Sub

At appstart (or when authenticated to Dropbox) you now can use the latest stored Cursor and get the latest content (new Method in v0.42 of the Library (B4A and B4J))

B4X:
        If kvs.ContainsKey("LatestCursor_"&loggedUserID) Then
            Dim latest As String = kvs.Get("LatestCursor_"&loggedUserID)
            Log("Latest Cursor = "&latest)
            dbxFiles.listFolderContinue(latest)
        End If

This results in a Event

B4X:
Sub dbxFiles_ListFolderContinue(content As List, cursor As String)
    Log($"dbxFiles_ListFolderContinue(${content.Size}, ${cursor})"$)
    If content.Size > 0 Then
        For i = 0 To content.Size-1
            Dim meta As Object = content.Get(i)
            Log(GetType(meta))
            If GetType(meta) = "com.dropbox.core.v2.files.FileMetadata" Then
                Dim filemeta As FileMetadata = meta
                Log($"FileMeta ${filemeta.Name}, ${filemeta.PathDisplay}"$)
            else if GetType(meta) = "com.dropbox.core.v2.files.FolderMetadata" Then
                Dim foldermeta As FolderMetadata = meta
                Log($"FolderMeta ${foldermeta.Name}, ${foldermeta.PathDisplay}"$)
            else if GetType(meta) = "com.dropbox.core.v2.files.DeletedMetadata" Then
                Dim deletedmeta As DeletedMetadata = meta
                Log($"DeletedMeta ${deletedmeta.Name}, ${deletedmeta.PathDisplay}"$)
            End If
            Log(meta)
        Next
    End If
    Log("LatestCursor to KVS")
    kvs.Put("LatestCursor_"&loggedUserID,cursor) ' Store the latest Cursor in KVS
 
End Sub

As you can see here the result is parsed into different Objects as there may be different ones in the Result.
Note that there may be come values which i did not handle.

At the end the given Cursor (it is a new latest cursor) is updated in the kvs.

This Event tells you the latest changes.
filemeta is a new/changed File,
foldermeta is a new/changed Folder and
deletedmeta informs about a deletion.
 
Last edited:
Top