Skip to content
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

Added android support #40

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

Conversation

Fancyflame
Copy link

No description provided.

@Fancyflame
Copy link
Author

Fancyflame commented May 2, 2022

@chrisduerr Here it is. But it still needs further checking.
I have tested it on my phone.

@chrisduerr
Copy link
Member

@Fancyflame Do you plan on finishing this up or are you looking for feedback on specific issues? I usually don't do reviews unless the PR is finished and CI goes through.

@Fancyflame
Copy link
Author

Fancyflame commented May 20, 2022

@chrisduerr Actually I didn't edit anything about Linux, also don't have Linux development experience... Sure, I need some feedback.
I hope someone could tell me what's going wrong.

EDIT: I repeatedly checked my code, everything changed only happens on Android platform. As you can see, any changes is behind #[cfg(target_os="android")]. How about checking the dependencies or the checking program?

@chrisduerr
Copy link
Member

@Fancyflame Check out why the build failed. You need to format the code.

@Fancyflame

This comment was marked as outdated.

@Fancyflame
Copy link
Author

Well, you helped me formatted the code, but I wonder what the configuration is, maybe I'll update these files in the future for optimization. (Wierd that it's not the same as rust playground)

Cargo.toml Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
let cb = env.get_static_field(class_ctx, "CLIPBOARD_SERVICE", "Ljava/lang/String;")?;
let cb_manager = env
.call_method(
ctx.context() as jni::sys::jobject,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a bunch of instances here where you have specified multiple path objects. For structs and other types the import should always go up to the last path element, so a use jni::sys::jobject should be used here.

This applies to other areas too like jni::JavaVM for example.

Copy link
Author

@Fancyflame Fancyflame May 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JObject must live with env, storage JObject is not easy due to the limitation of API form.

src/android_clipboard.rs Outdated Show resolved Hide resolved
src/android_clipboard.rs Outdated Show resolved Hide resolved
Comment on lines 55 to 68
let ctx = ndk_context::android_context();
let vm = unsafe { jni::JavaVM::from_raw(ctx.vm().cast()) }?;
let env = vm.attach_current_thread()?;
let class_ctx = env.find_class("android/content/Context")?;
let cb = env.get_static_field(class_ctx, "CLIPBOARD_SERVICE", "Ljava/lang/String;")?;
let cb_manager = env
.call_method(
ctx.context() as jni::sys::jobject,
"getSystemService",
"(Ljava/lang/String;)Ljava/lang/Object;",
&[cb],
)?
.l()
.unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of this seems duplicated between the two methods. It also seems to repeatedly access system stuff again and again. Can this not be persisted on the AndroidClipboardContext struct?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About this part, I have considered storage the duplicated part into a struct, but cb_manager here borrows vm, which is related to self-reference. I have made a crate to support self-reference: https://github.com/Fancyflame/Any-ref-rs. Its stability should be fine (code reviewed by rust forum) but it requires a high rustc version. Alternatively, we can merge the duplicate part into a function, which can re-use the code but it will not increase any performance, that is to say it still will access the system repeatedly. I prefer the latter, What do you think?

src/android_clipboard.rs Outdated Show resolved Hide resolved
src/android_clipboard.rs Outdated Show resolved Hide resolved
src/android_clipboard.rs Outdated Show resolved Hide resolved
src/android_clipboard.rs Outdated Show resolved Hide resolved
@chrisduerr
Copy link
Member

Well, you helped me formatted the code, but I wonder what the configuration is, maybe I'll update these files in the future for optimization. (Wierd that it's not the same as rust playground)

All you have to do is run cargo fmt locally. Of course running it in the playground won't work since that doesn't use the rustfmt configuration at the root of the project.

@Fancyflame
Copy link
Author

I saw your annotation. But I'm afraid I can only correct them at night (about after 8 hours) because I have classes through out the day.

@chrisduerr
Copy link
Member

I saw your annotation. But I'm afraid I can only correct them at night (about after 8 hours) because I have classes through out the day.

Absolutely no worries, take as much time as you want. Just make sure you re-request a review once you're looking for feedback so I know you're waiting for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants