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

Example showing how to request for permissions #19

Closed
charleschege opened this issue Feb 20, 2023 · 8 comments
Closed

Example showing how to request for permissions #19

charleschege opened this issue Feb 20, 2023 · 8 comments

Comments

@charleschege
Copy link

Is there an example showing how to request for example STORAGE permissions using jni and ndk-context like:

let ctx = ndk_context::android_context();
    let vm = unsafe { jni::JavaVM::from_raw(ctx.vm().cast()) }.unwrap();
    let context = unsafe { JObject::from_raw(ctx.context().cast()) };
    let env = vm.attach_current_thread().unwrap();

    // Query the global Audio Service
    let class_ctx = env.find_class("android/content/Context").unwrap();
    let storage_service = env
        .get_static_field(class_ctx, "AUDIO_SERVICE", "Ljava/lang/String;")
        .unwrap();

    let storage_manager = env
        .call_method(
            context,
            "getSystemService",
            "(Ljava/lang/String;)Ljava/lang/Object;",
            &[storage_service],
        )
        .unwrap()
        .l()
        .unwrap();

    // Request storage permission
    let permission_read = env
        .new_string("android.permission.READ_EXTERNAL_STORAGE")
        .unwrap();
    let permission_write = env
        .new_string("android.permission.WRITE_EXTERNAL_STORAGE")
        .unwrap();

env.call_method(
        storage_manager,
        "requestPermissions",
        "([Ljava/lang/String;I)V",
        &[
            permission_read.into(),
            JValue::from(permission_request_code),
        ],
    )
    .unwrap();

With the strict Rust types enforced it can be difficult to know exactly what to do and where the errors occured like the example above results in error:

02-20 12:35:17.831 15654 15693 I RustStdoutStderr: thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: JavaException', src/lib.rs:66:6
02-20 12:35:17.831 15654 15693 I RustStdoutStderr: note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Which doesn't say much.

@MarijnS95
Copy link
Member

@charleschege That indeed doesn't say much, as we don't know which line in your example corresponds to line 66 in lib.rs :)

@charleschege
Copy link
Author

Sorry about that.

Its

permission_read.into(),

@charleschege
Copy link
Author

An example to show how to request for permissions would be more ideal though.

Like the audio example is well done

@MarijnS95
Copy link
Member

MarijnS95 commented Feb 21, 2023

Right, yes, you are calling requestPermissions on the storage_manager variable which contains https://developer.android.com/reference/android/media/AudioManager (from the string AUDIO_SERVICE), it doesn't have that method :)

EDIT: Neither does https://developer.android.com/reference/android/os/storage/StorageManager, I think that method has to be called on an Activity/AppCompatActivity?

@charleschege
Copy link
Author

Sorry I missed that while copy pasting.

Changing it to STORAGE_SERVICE, still same error

@MarijnS95
Copy link
Member

Changing it to STORAGE_SERVICE, still same error

Again, as mentioned above requestPermissions doesn't exist on StorageManager either. Perhaps you're interested in reading https://github.com/matthunz/permissions / rust-mobile/android-activity#66? Feel free to converse there, this issue has nothing to do with cargo-apk (aside from borrowing a few lines from a runtime example) so I'll close it.

@MarijnS95 MarijnS95 closed this as not planned Won't fix, can't repro, duplicate, stale Feb 21, 2023
@charleschege
Copy link
Author

Thanks for pointing me to the permissions repo

@iincity
Copy link

iincity commented Jul 3, 2024

Thanks for pointing me to the permissions repo
this code can not buid for me:

error[E0308]: mismatched types
--> src\lib.rs:217:15
|
217 | &[storage_service],
| ^^^^^^^^^^^^^^^ expected JValueGen<&JObject<'_>>, found JValueGen<JObject<'_>>
|
= note: expected enum JValueGen<&jni::objects::JObject<'_>>
found enum JValueGen<jni::objects::JObject<'_>>

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

No branches or pull requests

3 participants