SharePlugin: Configure receiving directory in plugin permission mechanism
Summary
Fixes 422330
On Android 10 receiving files fail when we use the default storage location as returned by Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
. This happens to be the cause of this failure.
2020-04-25 12:58:53.713 18328-18602/org.kde.kdeconnect_tp E/Shareplugin: Error receiving file
java.lang.RuntimeException: Cannot create file 01 The Nights (Avicii By Avicii).mp3
at org.kde.kdeconnect.Plugins.SharePlugin.CompositeReceiveFileJob.getDocumentFileFor(CompositeReceiveFileJob.java:285)
at org.kde.kdeconnect.Plugins.SharePlugin.CompositeReceiveFileJob.run(CompositeReceiveFileJob.java:152)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)
and destinationFolderDocument.createFile(mimeType, displayName)
always returns null
which leads to the exception. This might be because Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
is deprecated in API 29 or Android 10 and paths returned by it are not writable by the app.
Receiving files start working when the custom directory is configured from Share and receive settings which uses a recommended approach to get a writable path Intent.ACTION_OPEN_DOCUMENT_TREE
as per Android documentation https://developer.android.com/reference/android/os/Environment#getExternalStoragePublicDirectory(java.lang.String
To avoid this, we should set the writable directory when the user is allowing plugin permissions on Android 10. When Storage permission is granted during plugin setup, this patch opens a directory selector and saves it as a custom location which avoids null
when calling destinationFolderDocument.createFile(mimeType, displayName)
because the path is now writable.
Test Plan
Before:
Send a file/files from a desktop. A notification appears on the phone showing failure.
After:
Receiving files now work.