So I've been going back and forth with this 
in https://github.com/golang/go/issues/105808.

Syncthing on Android consists of two parts, one is the actual android 
UI/wrapper, and second is GOOS=linux GOARCH=arm Go binary that the android 
UI starts and manipulates using a rest API.

>From the Go binary, we need to call into Java and get a file descriptor to 
file on the SD card with write permissions.
Normal os.Open doesn't cut it, as permissions on the sdcard are managed via 
Storage Access Framework, so we have to go through that in Java call a few 
other functions to get a writable fd.

In Go terms I expect to do something like this

import (
   "Java/android/content/ContentProvider"   
   "Java/android/content/Intent"

)

func Open(name string) (*os.File, error) {
    //Java/Go pseudo code
    try {
        var contentProvider = ContentProvider 
<https://developer.android.com/reference/android/content/ContentProvider.html#ContentProvider()>
();
        // Get write permissions
        contentProvider.getContext().getContentResolver().
takePersistableUriPermission(name, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | 
Intent.FLAG_GRANT_READ_URI_PERMISSION);
        // At this point it probably doesn't matter if we get the FD using 
Java or Go...
        var fdFromJava = contentProvider.openFile(name).getFd();
        return os.NewFile(fdFromJava, name), nil
    } catch {
        return nil, errors.New("welp")
    }
}



Elias on the ticket suggests that forward gomobile bind should be enough, 
yet I can't comprehend on how this is the case and how this would work, as 
all examples of forward bindings I find show calling Go from Java not the 
other way around.
As in, the call is initiated from Java side, which should not be the case 
in this case, as we are running in a Go binary.

Can someone provide more clarity on how this can be achieved?

Even if we do reverse bindings, can we actually make all the calls we want 
without having an activity in the current process/JVM?





Unrelated, but I thought I'll give gomobile bind a test to see what I end 
up with and I get the following:

c:\gopath\src\github.com\syncthing\syncthing\lib\fs>gomobile bind
gomobile: go build -pkgdir=c:\gopath\pkg\gomobile/pkg_android_arm -buildmode
=c-shared -o=C:\Users\Audrius\AppData\Local\Temp\gomobile-work-804905487\
android\src\main\jniLibs\armeabi-v7a\libgojni.so C:\Users\Audrius\AppData\
Local\Temp\gomobile-work-804905487\androidlib\main.go failed: exit status 2
# 
_/C_/Users/Audrius/AppData/Local/Temp/gomobile-work-804905487/gomobile_bind
C:\Users\Audrius\AppData\Local\Temp\gomobile-work-804905487\gomobile_bind\
go_fsmain.go:243: cannot use (*proxyfs_Filesystem)(_v_ref) (type *
proxyfs_Filesystem) as type fs.Filesystem in assignment:
        *proxyfs_Filesystem does not implement fs.Filesystem (missing Chmod 
method)
C:\Users\Audrius\AppData\Local\Temp\gomobile-work-804905487\gomobile_bind\
go_fsmain.go:722: cannot use (*proxyfs_FileInfo)(res_0_ref) (type *
proxyfs_FileInfo) as type fs.FileInfo in assignment:
        *proxyfs_FileInfo does not implement fs.FileInfo (missing ModTime 
method)
C:\Users\Audrius\AppData\Local\Temp\gomobile-work-804905487\gomobile_bind\
go_fsmain.go:1170: cannot use (*proxyfs_FileInfo)(res_0_ref) (type *
proxyfs_FileInfo) as type fs.FileInfo in assignment:
        *proxyfs_FileInfo does not implement fs.FileInfo (missing ModTime 
method)
C:\Users\Audrius\AppData\Local\Temp\gomobile-work-804905487\gomobile_bind\
go_fsmain.go:1284: cannot use (*proxyfs_FileInfo)(res_0_ref) (type *
proxyfs_FileInfo) as type fs.FileInfo in assignment:
        *proxyfs_FileInfo does not implement fs.FileInfo (missing ModTime 
method)
C:\Users\Audrius\AppData\Local\Temp\gomobile-work-804905487\gomobile_bind\
go_fsmain.go:1465: cannot use (*proxyfs_Filesystem)(_param_next_ref) (type *
proxyfs_Filesystem) as type fs.Filesystem in assignment:
        *proxyfs_Filesystem does not implement fs.Filesystem (missing Chmod 
method)


-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to