polyhorn_android_sys/
activity.rs

1use jni::objects::{JObject, JValue};
2use jni::sys::{jobject, JNIEnv};
3
4use super::{Context, Env, Object, Rect, Reference, View};
5
6#[derive(Clone)]
7pub struct Activity {
8    reference: Reference,
9}
10
11impl Activity {
12    pub unsafe fn with_env(env: *mut JNIEnv, object: jobject) -> Activity {
13        Activity {
14            reference: Env::new(env).retain(JObject::from(object)),
15        }
16    }
17
18    pub fn bounds(&self, env: &Env) -> Rect {
19        unsafe {
20            match env.call_method(
21                self.reference.as_object(),
22                "getBounds",
23                "()Lcom/glacyr/polyhorn/Rect;",
24                &[],
25            ) {
26                JValue::Object(value) => Rect::from_reference(env.retain(value)),
27                _ => unreachable!(),
28            }
29        }
30    }
31
32    pub fn set_content_view(&mut self, env: &Env, view: &View) {
33        unsafe {
34            env.call_method(
35                self.reference.as_object(),
36                "setContentView",
37                "(Landroid/view/View;)V",
38                &[JValue::Object(view.as_reference().as_object())],
39            );
40        }
41    }
42}
43
44impl Object for Activity {
45    fn from_reference(reference: Reference) -> Self {
46        Activity { reference }
47    }
48
49    fn as_reference(&self) -> &Reference {
50        &self.reference
51    }
52}
53
54impl Into<Context> for &Activity {
55    fn into(self) -> Context {
56        Context::from_reference(self.reference.clone())
57    }
58}