-
Notifications
You must be signed in to change notification settings - Fork 17
[DRAFT] explorer: [android] open directory #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
inkeliz
wants to merge
1
commit into
gioui:main
Choose a base branch
from
inkeliz:opendir
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package explorer | ||
|
||
import ( | ||
"gioui.org/app" | ||
"git.wow.st/gmp/jni" | ||
"os" | ||
) | ||
|
||
//go:generate javac -source 8 -target 8 -bootclasspath $ANDROID_HOME/platforms/android-30/android.jar -d $TEMP/explorer_file_android/classes dir_android.java | ||
//go:generate jar cf dir_android.jar -C $TEMP/explorer_file_android/classes . | ||
|
||
type Directory struct { | ||
createFile jni.MethodID | ||
libClass jni.Class | ||
libObject jni.Object | ||
|
||
url jni.Object | ||
openFile jni.MethodID | ||
} | ||
|
||
func newDirectory(env jni.Env, url jni.Object) (*Directory, error) { | ||
f := &Directory{url: url} | ||
|
||
class, err := jni.LoadClass(env, jni.ClassLoaderFor(env, jni.Object(app.AppContext())), "org/gioui/x/explorer/dir_android") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
obj, err := jni.NewObject(env, class, jni.GetMethodID(env, class, "<init>", `()V`)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// For some reason, using `f.stream` as argument for a constructor (`public file_android(Object j) {}`) doesn't work. | ||
if err := jni.CallVoidMethod(env, obj, jni.GetMethodID(env, class, "setHandle", `(Ljava/lang/Object;)V`), jni.Value(f.url)); err != nil { | ||
return nil, err | ||
} | ||
|
||
f.libObject = jni.NewGlobalRef(env, obj) | ||
f.libClass = jni.Class(jni.NewGlobalRef(env, jni.Object(class))) | ||
f.openFile = jni.GetMethodID(env, f.libClass, "readFile", "(Landroid/view/View;Ljava/lang/String;)Ljava/lang/Object;") | ||
f.createFile = jni.GetMethodID(env, f.libClass, "writeFile", "(Landroid/view/View;Ljava/lang/String;)Ljava/lang/Object;") | ||
|
||
return f, nil | ||
|
||
} | ||
|
||
func (d *Directory) ReadFile(name string) (file *File, err error) { | ||
if d == nil || d.libObject == 0 { | ||
return nil, os.ErrClosed | ||
} | ||
|
||
err = jni.Do(jni.JVMFor(app.JavaVM()), func(env jni.Env) error { | ||
nameJava := jni.JavaString(env, name) | ||
stream, err := jni.CallObjectMethod(env, d.libObject, d.openFile, jni.Value(_View), jni.Value(nameJava)) | ||
if err != nil { | ||
return err | ||
} | ||
if stream == 0 { | ||
return os.ErrNotExist | ||
} | ||
|
||
file, err = newFile(env, stream) | ||
return nil | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return file, nil | ||
} | ||
|
||
/* | ||
func (d *Directory) OpenDirectory(name string) (Directory, error) { | ||
// Implementation for opening a subdirectory in the directory | ||
return nil, nil | ||
} | ||
|
||
*/ | ||
|
||
func (d *Directory) WriteFile(name string) (file *File, err error) { | ||
if d == nil || d.libObject == 0 { | ||
return nil, os.ErrClosed | ||
} | ||
|
||
err = jni.Do(jni.JVMFor(app.JavaVM()), func(env jni.Env) error { | ||
nameJava := jni.JavaString(env, name) | ||
stream, err := jni.CallObjectMethod(env, d.libObject, d.createFile, jni.Value(_View), jni.Value(nameJava)) | ||
if err != nil { | ||
return err | ||
} | ||
if stream == 0 { | ||
return os.ErrExist | ||
} | ||
|
||
file, err = newFile(env, stream) | ||
return nil | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return file, nil | ||
} | ||
|
||
/* | ||
func (d *Directory) CreateDirectory(name string) (Directory, error) { | ||
// Implementation for creating a subdirectory in the directory | ||
return nil, nil | ||
} | ||
|
||
func (d *Directory) ListFiles() ([]File, error) { | ||
// Implementation for listing files in the directory | ||
return nil, nil | ||
} | ||
|
||
func (d *Directory) ListDirectories() ([]Directory, error) { | ||
// Implementation for listing subdirectories in the directory | ||
return nil, nil | ||
} | ||
|
||
*/ |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.gioui.x.explorer; | ||
|
||
import android.content.Context; | ||
import android.net.Uri; | ||
import android.provider.DocumentsContract; | ||
import android.database.Cursor; | ||
|
||
import android.view.View; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
|
||
public class dir_android { | ||
public Object handler; | ||
|
||
public void setHandle(Object f) { | ||
this.handler = f; | ||
} | ||
|
||
public Object readFile(View view, String filename) { | ||
try { | ||
Context ctx = view.getContext(); | ||
Uri folderUri = (Uri) handler; | ||
Uri fileUri = findFile(ctx, folderUri, filename); | ||
if (fileUri == null) { | ||
return null; | ||
} | ||
|
||
return ctx.getContentResolver().openInputStream(fileUri); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
|
||
public Object writeFile(View view, String filename) { | ||
try { | ||
Context ctx = view.getContext(); | ||
Uri folderUri = (Uri) handler; | ||
Uri fileUri = findFile(ctx, folderUri, filename); | ||
|
||
if (fileUri == null) { | ||
fileUri = DocumentsContract.createDocument(ctx.getContentResolver(), folderUri, "application/octet-stream", filename); | ||
} | ||
|
||
return ctx.getContentResolver().openOutputStream(fileUri, "wt"); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
|
||
private Uri findFile(Context ctx, Uri folderUri, String filename) { | ||
String folderDocId = DocumentsContract.getDocumentId(folderUri); | ||
Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(folderUri, folderDocId); | ||
|
||
try (Cursor cursor = ctx.getContentResolver().query(childrenUri, new String[] {DocumentsContract.Document.COLUMN_DOCUMENT_ID, DocumentsContract.Document.COLUMN_DISPLAY_NAME},null, null, null)) { | ||
if (cursor != null) { | ||
while (cursor.moveToNext()) { | ||
String docId = cursor.getString(0); | ||
String name = cursor.getString(1); | ||
if (filename.equals(name)) { | ||
return DocumentsContract.buildDocumentUriUsingTree(folderUri, docId); | ||
} | ||
} | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where and how are these global references cleaned up? I don't see any mechanism to do that, and I don't think the JVM is able to do it automatically.