Dropbox Sync Library

aggelos

Member
Licensed User
Longtime User
Hello,

i am using b4a 2.71 . nexus s 4.1.2 and latest dropbox sdk 3.1.1. and java 1.7. phone has the dropbox installed and logged in.

i use for the test a new empty project.

i have copied the dropbox sdk file and the wrapper files to the additional libraries folder.
dropbox lib is checked in b4a libs tab
added the text to manifest and change the app key.
created the strings.xml and made it readonly.
added the manager,key,secret definitions .3 lines in process_global

i am trying to use the library and i get this error on manager.Initialize(key, secret, "manager")

B4X:
** Activity (main) Create, isFirst = true **
main_activity_create (java line: 244)
java.lang.NoClassDefFoundError: com.dropbox.sync.android.DbxAccountManager
   at anywheresoftware.b4a.dropbox.DbxAccountManagerWrapper.Initialize(DbxAccountManagerWrapper.java:53)
   at b4a.example.dropbox.main._activity_create(main.java:244)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
   at b4a.example.dropbox.main.afterFirstLayout(main.java:89)
   at b4a.example.dropbox.main.access$100(main.java:16)
   at b4a.example.dropbox.main$WaitForLayout.run(main.java:74)
   at android.os.Handler.handleCallback(Handler.java:615)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:4745)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
   at dalvik.system.NativeStart.main(Native Method)
 

aggelos

Member
Licensed User
Longtime User
Hello,

me again :)
the comment is big , i know.

I have done some tests using the sync api. everything works ok BUT.

the app consumes too much internal disk space for its cache.And the bad thing is that it seems that my app is using the space and not dropbox!
for example i added 28 MB of files to my dropbox via the app and after several syncs the cache grew to 200MB.
The documentations says that the default max file cache is 500MB and i read somewhere that every 3 days the old files from the cache are cleared.
A lot of phones cant spare 500MB of internal space just for dropbox.mine showed an alert for low disk space at 160MB cache
Since we are just uploading or downloading files there is no need for such cache.

anyway i found that unlinking and linking again clears the cache but its really annoying for the user as he has to click accept every time.

Another solution is to set the max cache using
void setMaxFileCacheSize(long maxBytes)
"The cache size can be set to zero, in which case files are removed from the cache as soon as they are no longer in-use."

can i do this via reflection or any other method?

also the dropbox sync api has some usefull apis , like:
boolean anyInProgress()
long getFileCacheSize()
boolean hasLinkedAccount()
boolean isLinked()

has linked account and islinked are really usefull because you can't call .link and present the user with dropbox authorization when he opens your app.
you must first check to see if the app is linked and if not to have a button so that IF the user wants, he can press to link it.
But if its already linked you can go ahead and do your uploads/downloads without waiting for the user to press the link button.


Any help is appreciated.

Thank you
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Dim r As Reflector
r.Target = DbxManager 'DbxAccountManager object
Dim managerJO As JavaObject = r.GetField("manager")
Log(managerJO.RunMethod("hasLinkedAccount", null))

'after AccountReadyEvent
Dim fileSystemJO As JavaObject = r.GetField("fileSystem")
Dim cacheSize As Long = 0
fileSystemJO.RunMethod("setMaxFileCacheSize", Array(cacheSize))
 

aggelos

Member
Licensed User
Longtime User
Erel thank you

I know you hear those words a lot in this forum but sometimes(like this one) users really really mean it.
I think you should add this code to your first post as an example on how someone can access the rest of the api.
 

aggelos

Member
Licensed User
Longtime User
The code for setting maxfilecache works ok!

the folowing lines gave me an error. i tried to use them alone too, without setmaxfilecache.
B4X:
Dim r As Reflector
r.Target = miscsubs.DbxManager 'DbxAccountManager object

Dim managerJO As JavaObject = r.GetField("manager")
Log("haslinked " & managerJO.RunMethod("hasLinkedAccount", Null))


B4X:
Account Ready: true
Cannot get methods of class: com.dropbox.sync.android.DbxAccountManager, disabling cache.
java.lang.NullPointerException
   at anywheresoftware.b4j.object.JavaObject$MethodCache.getMethod(JavaObject.java:323)
   at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:97)
   at eu.sitediary.client.showdbxsync._dbxmanager_accountready(showdbxsync.java:453)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
   at anywheresoftware.b4a.BA$3.run(BA.java:320)
   at android.os.Handler.handleCallback(Handler.java:615)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:4745)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
   at dalvik.system.NativeStart.main(Native Method)
java.lang.NullPointerException
 

aggelos

Member
Licensed User
Longtime User
new error. i also tried isLinked

B4X:
Account Ready: true
Cannot get methods of class: com.dropbox.sync.android.DbxAccountManager, disabling cache.
java.lang.RuntimeException: Method: hasLinkedAccount not found in: com.dropbox.sync.android.DbxAccountManager
   at anywheresoftware.b4j.object.JavaObject$MethodCache.getMethod(JavaObject.java:346)
   at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:97)
   at eu.sitediary.client.showdbxsync._dbxmanager_accountready(showdbxsync.java:453)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
   at anywheresoftware.b4a.BA$3.run(BA.java:320)
   at android.os.Handler.handleCallback(Handler.java:615)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:4745)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
   at dalvik.system.NativeStart.main(Native Method)
java.lang.RuntimeException: Method: hasLinkedAccount not found in: com.dropbox.sync.android.DbxAccountManager
 

aggelos

Member
Licensed User
Longtime User
Yes!

yes yes yes! :)

Erel is there any way to find all fields of an object for use in r.GetField?

i didnt see any r.getallfields or r.listfields
 

aggelos

Member
Licensed User
Longtime User
to anyone who might find it helpfull.
I use the folowing to check if dropbox is syncing in order to allow/disallow the user
to do a new full sync/upload while the previous is running.
this helps to keep the cache size low and prevent an app crash if the user presses the
full upload button while the previous sync is running.


B4X:
Dim r As Reflector
r.Target = DbxManager 'DbxAccountManager object
r.Target = r.GetField("fileSystem")
r.Target = r.RunMethod("getSyncStatus")
Log("Background Sync running is " & r.RunMethod("anyInProgress"))
 

Mahares

Expert
Licensed User
Longtime User
How do you keep the cache on the device set to 0 every time, so when I download a file or files from Dropbox, I would like to always download a fresh copy, even if the files in Dropbox have not changed. I do not need cache on the device.
Thank you for helping.
 

Mahares

Expert
Licensed User
Longtime User
@Erel:
I do not think the code in post #305 works with Dropbox lib 1.25. I am getting this error:
java.lang.NoSuchMethodException: hasLinkedAccount []
Does it only work with version 3?

I do have the AutoSync set to true. But, the only way I get a latest copy of the downloaded files is to run the download twice within 5 minutes of each other. The first time, it almost always downloads the cached files, then on the second download, usually 3 or 5 minutes later, it downloads the latest files. By using your code in post #305, and set the cache to 0, will I be able to get the latest files in one download.
Thank you
 
Last edited:

Mahares

Expert
Licensed User
Longtime User
I installed Dropbox SDK 3.1 and B4A Dropbox lib 3.0, but I still have the same problem: The only way I can download the most recent Dropbox files to the device is if I run the app two times in succession , around 5 minutes or so apart. I even used Erel's code shown below to delete the cache to force the latest files to download, but it does not help:
B4X:
'after AccountReadyEvent
Dim fileSystemJO As JavaObject = r.GetField("fileSystem")
Dim cacheSize As Long = 0
fileSystemJO.RunMethod("setMaxFileCacheSize", Array(cacheSize))
Can someone familiar with this library please tell me why do I have to run the app twice to get the the newest files downloaded.
Thank you
 

Mahares

Expert
Licensed User
Longtime User
When AutoSync is set to true, a check is done each time with Dropbox to make sure you get the most recent file. If you are not getting it then there is a problem somewhere else.

I issue Manager.AutoSync =True in Activity_Create after Manager.LinkAccount. Should I perhaps try to put in Activity_Resume also. Or should I try to insert it immediately before the download of files. Is it worth trying either or do you think the problem may lie somewhere else?
Thank you for giving it your best guess.
 

aggelos

Member
Licensed User
Longtime User
Mahares i ve seen your posts, i would recommend that you use the Oath2 to connect to dropbox. you will have better control of your uploads/downloads

Unfortunately sync api has a mind of its own. I use it to transfer photos, these files never change.

Some ideas for your problem...
1) are you sure the file that you change has been fully uploaded to dropbox? maybe you dont get the latest version because the other device/computer did upload the file yet.
2) you cannot fully disable cache in sync api. the dropbox will still use/grow the cache while downloading/ uploading files. nothing you can do about it.
3) use anyInProgress to see if background syncing has actually finished when you download a file.
4) before download do a .sync. wait for it to finish and then download the file.
5) see the unfiltered logs to see what dropbox is doing .

hope something helps.
 
Top