use crate::android_dex::{Class, dex};
use std::cell::Cell;
use std::ffi::{CStr, c_char, c_void};
use std::sync::atomic::{AtomicPtr, Ordering};
pub(crate) type Obj = *mut c_void;
#[repr(C)]
#[derive(Clone, Copy)]
pub(crate) union Value {
pub z: u8,
pub i: i32,
pub j: i64,
pub f: f32,
pub l: Obj,
}
impl Value {
pub(crate) fn int(i: i32) -> Value {
Value { j: i64::from(i) & 0xffff_ffff }
}
pub(crate) fn obj(l: Obj) -> Value {
Value { l }
}
pub(crate) fn bool(on: bool) -> Value {
Value { j: i64::from(on) }
}
pub(crate) fn float(f: f32) -> Value {
Value { j: i64::from(f.to_bits()) }
}
pub(crate) fn object(self) -> Obj {
unsafe { self.l }
}
pub(crate) fn as_int(self) -> i32 {
unsafe { self.i }
}
pub(crate) fn as_bool(self) -> bool {
unsafe { self.z != 0 }
}
pub(crate) fn as_float(self) -> f32 {
unsafe { self.f }
}
}
#[repr(C)]
pub(crate) struct Native {
pub name: *const c_char,
pub signature: *const c_char,
pub function: *const c_void,
}
const FIND_CLASS: usize = 6;
const EXCEPTION_DESCRIBE: usize = 16;
const EXCEPTION_CLEAR: usize = 17;
const PUSH_LOCAL_FRAME: usize = 19;
const POP_LOCAL_FRAME: usize = 20;
const NEW_GLOBAL_REF: usize = 21;
const DELETE_LOCAL_REF: usize = 23;
const NEW_OBJECT_A: usize = 30;
const GET_OBJECT_CLASS: usize = 31;
const GET_METHOD_ID: usize = 33;
const CALL_OBJECT_METHOD_A: usize = 36;
const CALL_STATIC_OBJECT_METHOD_A: usize = 116;
const TYPES: &[u8] = b"LZBCSIJFDV";
const GET_FIELD_ID: usize = 94;
const GET_INT_FIELD: usize = 100;
const GET_FLOAT_FIELD: usize = 102;
const SET_INT_FIELD: usize = 109;
const GET_STATIC_METHOD_ID: usize = 113;
const GET_STATIC_FIELD_ID: usize = 144;
const GET_STATIC_INT_FIELD: usize = 150;
const NEW_STRING: usize = 163;
const GET_STRING_LENGTH: usize = 164;
const NEW_INT_ARRAY: usize = 179;
const GET_INT_ARRAY_REGION: usize = 203;
const REGISTER_NATIVES: usize = 215;
const GET_STRING_REGION: usize = 220;
const EXCEPTION_CHECK: usize = 228;
const NEW_DIRECT_BYTE_BUFFER: usize = 229;
const ATTACH_CURRENT_THREAD: usize = 4;
const DETACH_CURRENT_THREAD: usize = 5;
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct Env(pub *mut *const *const c_void);
thread_local! {
static MAIN: Cell<Env> = const { Cell::new(Env(std::ptr::null_mut())) };
}
static VM: AtomicPtr<c_void> = AtomicPtr::new(std::ptr::null_mut());
pub(crate) fn set_main(env: Env, vm: *mut c_void) {
MAIN.set(env);
VM.store(vm, Ordering::SeqCst);
}
pub(crate) fn env() -> Option<Env> {
Some(MAIN.get()).filter(|env| !env.0.is_null())
}
impl Env {
unsafe fn slot<F: Copy>(self, n: usize) -> F {
unsafe { std::mem::transmute_copy(&*(*self.0).add(n)) }
}
pub(crate) fn threw(self) -> bool {
unsafe {
let thrown = self.slot::<extern "C" fn(Env) -> u8>(EXCEPTION_CHECK)(self) != 0;
if thrown {
self.slot::<extern "C" fn(Env)>(EXCEPTION_DESCRIBE)(self);
self.slot::<extern "C" fn(Env)>(EXCEPTION_CLEAR)(self);
}
thrown
}
}
pub(crate) fn class(self, name: &CStr) -> Obj {
let class = unsafe { self.slot::<extern "C" fn(Env, *const c_char) -> Obj>(FIND_CLASS)(self, name.as_ptr()) };
if self.threw() { std::ptr::null_mut() } else { class }
}
pub(crate) fn call(self, on: Obj, name: &CStr, signature: &CStr, args: &[Value]) -> Value {
if on.is_null() {
return Value { j: 0 };
}
let method = unsafe {
let class = self.slot::<extern "C" fn(Env, Obj) -> Obj>(GET_OBJECT_CLASS)(self, on);
let method = self.slot::<extern "C" fn(Env, Obj, *const c_char, *const c_char) -> Obj>(GET_METHOD_ID)(self, class, name.as_ptr(), signature.as_ptr());
self.drop_local(class);
method
};
if self.threw() || method.is_null() {
return Value { j: 0 };
}
self.invoke(CALL_OBJECT_METHOD_A, on, method, signature, args)
}
pub(crate) fn call_static(self, class: &CStr, name: &CStr, signature: &CStr, args: &[Value]) -> Value {
let class = self.class(class);
if class.is_null() {
return Value { j: 0 };
}
let method = unsafe { self.slot::<extern "C" fn(Env, Obj, *const c_char, *const c_char) -> Obj>(GET_STATIC_METHOD_ID)(self, class, name.as_ptr(), signature.as_ptr()) };
let value = if self.threw() || method.is_null() { Value { j: 0 } } else { self.invoke(CALL_STATIC_OBJECT_METHOD_A, class, method, signature, args) };
self.drop_local(class);
value
}
fn invoke(self, first: usize, on: Obj, method: Obj, signature: &CStr, args: &[Value]) -> Value {
let returns = signature.to_bytes().iter().rposition(|byte| *byte == b')').and_then(|at| signature.to_bytes().get(at + 1)).copied().unwrap_or(b'V');
let kind = TYPES.iter().position(|letter| *letter == if returns == b'[' { b'L' } else { returns }).unwrap_or(9);
let slot = first + kind * 3;
let value = unsafe {
let ptr = args.as_ptr();
match returns {
b'V' => (self.slot::<extern "C" fn(Env, Obj, Obj, *const Value)>(slot)(self, on, method, ptr), Value { j: 0 }).1,
b'Z' | b'B' => Value { j: i64::from(self.slot::<extern "C" fn(Env, Obj, Obj, *const Value) -> u8>(slot)(self, on, method, ptr)) },
b'C' | b'S' => Value { j: i64::from(self.slot::<extern "C" fn(Env, Obj, Obj, *const Value) -> u16>(slot)(self, on, method, ptr)) },
b'I' => Value::int(self.slot::<extern "C" fn(Env, Obj, Obj, *const Value) -> i32>(slot)(self, on, method, ptr)),
b'J' => Value { j: self.slot::<extern "C" fn(Env, Obj, Obj, *const Value) -> i64>(slot)(self, on, method, ptr) },
b'F' => Value::float(self.slot::<extern "C" fn(Env, Obj, Obj, *const Value) -> f32>(slot)(self, on, method, ptr)),
b'D' => Value::float(self.slot::<extern "C" fn(Env, Obj, Obj, *const Value) -> f64>(slot)(self, on, method, ptr) as f32),
_ => Value::obj(self.slot::<extern "C" fn(Env, Obj, Obj, *const Value) -> Obj>(slot)(self, on, method, ptr)),
}
};
if self.threw() { Value { j: 0 } } else { value }
}
pub(crate) fn new_object(self, class: Obj, signature: &CStr, args: &[Value]) -> Obj {
if class.is_null() {
return std::ptr::null_mut();
}
let object = unsafe {
let made = self.slot::<extern "C" fn(Env, Obj, *const c_char, *const c_char) -> Obj>(GET_METHOD_ID)(self, class, c"<init>".as_ptr(), signature.as_ptr());
if made.is_null() { made } else { self.slot::<extern "C" fn(Env, Obj, Obj, *const Value) -> Obj>(NEW_OBJECT_A)(self, class, made, args.as_ptr()) }
};
if self.threw() { std::ptr::null_mut() } else { object }
}
pub(crate) fn new_named(self, class: &CStr, signature: &CStr, args: &[Value]) -> Obj {
let class = self.class(class);
let object = self.new_object(class, signature, args);
self.drop_local(class);
object
}
fn field(self, on: Obj, name: &CStr, kind: &CStr) -> Obj {
let field = unsafe {
let class = self.slot::<extern "C" fn(Env, Obj) -> Obj>(GET_OBJECT_CLASS)(self, on);
let field = self.slot::<extern "C" fn(Env, Obj, *const c_char, *const c_char) -> Obj>(GET_FIELD_ID)(self, class, name.as_ptr(), kind.as_ptr());
self.drop_local(class);
field
};
if self.threw() { std::ptr::null_mut() } else { field }
}
pub(crate) fn int_field(self, on: Obj, name: &CStr) -> i32 {
let field = if on.is_null() { std::ptr::null_mut() } else { self.field(on, name, c"I") };
if field.is_null() { 0 } else { unsafe { self.slot::<extern "C" fn(Env, Obj, Obj) -> i32>(GET_INT_FIELD)(self, on, field) } }
}
pub(crate) fn float_field(self, on: Obj, name: &CStr) -> f32 {
let field = if on.is_null() { std::ptr::null_mut() } else { self.field(on, name, c"F") };
if field.is_null() { 0.0 } else { unsafe { self.slot::<extern "C" fn(Env, Obj, Obj) -> f32>(GET_FLOAT_FIELD)(self, on, field) } }
}
pub(crate) fn set_int_field(self, on: Obj, name: &CStr, value: i32) {
let field = if on.is_null() { std::ptr::null_mut() } else { self.field(on, name, c"I") };
if !field.is_null() {
unsafe { self.slot::<extern "C" fn(Env, Obj, Obj, i32)>(SET_INT_FIELD)(self, on, field, value) };
}
}
pub(crate) fn static_int(self, class: &CStr, name: &CStr) -> i32 {
let class = self.class(class);
if class.is_null() {
return 0;
}
let value = unsafe {
let field = self.slot::<extern "C" fn(Env, Obj, *const c_char, *const c_char) -> Obj>(GET_STATIC_FIELD_ID)(self, class, name.as_ptr(), c"I".as_ptr());
if field.is_null() { 0 } else { self.slot::<extern "C" fn(Env, Obj, Obj) -> i32>(GET_STATIC_INT_FIELD)(self, class, field) }
};
let value = if self.threw() { 0 } else { value };
self.drop_local(class);
value
}
pub(crate) fn string(self, text: &str) -> Obj {
let units: Vec<u16> = text.encode_utf16().collect();
let made = unsafe { self.slot::<extern "C" fn(Env, *const u16, i32) -> Obj>(NEW_STRING)(self, units.as_ptr(), units.len() as i32) };
if self.threw() { std::ptr::null_mut() } else { made }
}
pub(crate) fn text(self, of: Obj) -> Option<String> {
let string = self.call(of, c"toString", c"()Ljava/lang/String;", &[]).object();
if string.is_null() {
return None;
}
let units = unsafe {
let long = self.slot::<extern "C" fn(Env, Obj) -> i32>(GET_STRING_LENGTH)(self, string).max(0) as usize;
let mut units = vec![0u16; long];
self.slot::<extern "C" fn(Env, Obj, i32, i32, *mut u16)>(GET_STRING_REGION)(self, string, 0, long as i32, units.as_mut_ptr());
units
};
self.drop_local(string);
Some(String::from_utf16_lossy(&units))
}
pub(crate) fn global(self, of: Obj) -> Obj {
if of.is_null() { of } else { unsafe { self.slot::<extern "C" fn(Env, Obj) -> Obj>(NEW_GLOBAL_REF)(self, of) } }
}
pub(crate) fn drop_local(self, of: Obj) {
if !of.is_null() {
unsafe { self.slot::<extern "C" fn(Env, Obj)>(DELETE_LOCAL_REF)(self, of) };
}
}
pub(crate) fn frame<T>(self, work: impl FnOnce() -> T) -> T {
let pushed = unsafe { self.slot::<extern "C" fn(Env, i32) -> i32>(PUSH_LOCAL_FRAME)(self, 128) } == 0;
let answer = work();
if pushed {
unsafe { self.slot::<extern "C" fn(Env, Obj) -> Obj>(POP_LOCAL_FRAME)(self, std::ptr::null_mut()) };
} else {
self.threw();
}
answer
}
pub(crate) fn register(self, class: Obj, natives: &[Native]) -> bool {
let said = unsafe { self.slot::<extern "C" fn(Env, Obj, *const Native, i32) -> i32>(REGISTER_NATIVES)(self, class, natives.as_ptr(), natives.len() as i32) };
!self.threw() && said == 0
}
}
pub(crate) fn new_ints(env: Env, long: i32) -> Obj {
let made = unsafe { env.slot::<extern "C" fn(Env, i32) -> Obj>(NEW_INT_ARRAY)(env, long) };
if env.threw() { std::ptr::null_mut() } else { made }
}
pub(crate) fn ints(env: Env, of: Obj, into: &mut [i32]) {
if !of.is_null() {
unsafe { env.slot::<extern "C" fn(Env, Obj, i32, i32, *mut i32)>(GET_INT_ARRAY_REGION)(env, of, 0, into.len() as i32, into.as_mut_ptr()) };
env.threw();
}
}
pub(crate) const CLASSES: [Class; 3] = [
Class {
name: "taut/Host",
parent: "android/view/View",
made_with: &["Landroid/content/Context;"],
natives: &[
("getAccessibilityNodeProvider", "()Landroid/view/accessibility/AccessibilityNodeProvider;"),
("onCreateInputConnection", "(Landroid/view/inputmethod/EditorInfo;)Landroid/view/inputmethod/InputConnection;"),
("onCheckIsTextEditor", "()Z"),
("onApplyWindowInsets", "(Landroid/view/WindowInsets;)Landroid/view/WindowInsets;"),
],
},
Class {
name: "taut/Nodes",
parent: "android/view/accessibility/AccessibilityNodeProvider",
made_with: &[],
natives: &[
("createAccessibilityNodeInfo", "(I)Landroid/view/accessibility/AccessibilityNodeInfo;"),
("performAction", "(IILandroid/os/Bundle;)Z"),
("findFocus", "(I)Landroid/view/accessibility/AccessibilityNodeInfo;"),
],
},
Class {
name: "taut/Typing",
parent: "android/view/inputmethod/BaseInputConnection",
made_with: &["Landroid/view/View;", "Z"],
natives: &[
("commitText", "(Ljava/lang/CharSequence;I)Z"),
("setComposingText", "(Ljava/lang/CharSequence;I)Z"),
("finishComposingText", "()Z"),
("deleteSurroundingText", "(II)Z"),
("performEditorAction", "(I)Z"),
],
},
];
static DEX: std::sync::OnceLock<Vec<u8>> = std::sync::OnceLock::new();
pub(crate) fn load_classes(env: Env, activity: Obj) -> Option<[Obj; 3]> {
let file = DEX.get_or_init(|| dex(&CLASSES));
env.frame(|| {
let buffer = unsafe { env.slot::<extern "C" fn(Env, *mut c_void, i64) -> Obj>(NEW_DIRECT_BYTE_BUFFER)(env, file.as_ptr().cast_mut().cast(), file.len() as i64) };
let parent = env.call(activity, c"getClassLoader", c"()Ljava/lang/ClassLoader;", &[]).object();
let loader = env.new_named(c"dalvik/system/InMemoryDexClassLoader", c"(Ljava/nio/ByteBuffer;Ljava/lang/ClassLoader;)V", &[Value::obj(buffer), Value::obj(parent)]);
let load = |name: &str| env.global(env.call(loader, c"loadClass", c"(Ljava/lang/String;)Ljava/lang/Class;", &[Value::obj(env.string(name))]).object());
let classes = [load("taut.Host"), load("taut.Nodes"), load("taut.Typing")];
classes.iter().all(|class| !class.is_null()).then_some(classes)
})
}
pub(crate) fn service(env: Env, activity: Obj, name: &str) -> Obj {
env.call(activity, c"getSystemService", c"(Ljava/lang/String;)Ljava/lang/Object;", &[Value::obj(env.string(name))]).object()
}
pub(crate) fn clipboard_text(env: Env, activity: Obj) -> Option<String> {
env.frame(|| {
let clip = env.call(service(env, activity, "clipboard"), c"getPrimaryClip", c"()Landroid/content/ClipData;", &[]).object();
if env.call(clip, c"getItemCount", c"()I", &[]).as_int() < 1 {
return None;
}
let item = env.call(clip, c"getItemAt", c"(I)Landroid/content/ClipData$Item;", &[Value::int(0)]).object();
env.text(env.call(item, c"coerceToText", c"(Landroid/content/Context;)Ljava/lang/CharSequence;", &[Value::obj(activity)]).object())
})
}
pub(crate) fn set_clipboard_text(env: Env, activity: Obj, text: &str) {
env.frame(|| {
let args = [Value::obj(env.string("text")), Value::obj(env.string(text))];
let clip = env.call_static(c"android/content/ClipData", c"newPlainText", c"(Ljava/lang/CharSequence;Ljava/lang/CharSequence;)Landroid/content/ClipData;", &args).object();
env.call(service(env, activity, "clipboard"), c"setPrimaryClip", c"(Landroid/content/ClipData;)V", &[Value::obj(clip)]);
});
}
pub(crate) fn soft_keyboard(env: Env, activity: Obj, view: Obj, show: bool, again: bool) {
env.frame(|| {
let methods = service(env, activity, "input_method");
if again {
env.call(methods, c"restartInput", c"(Landroid/view/View;)V", &[Value::obj(view)]);
}
if show {
env.call(view, c"requestFocus", c"()Z", &[]);
env.call(methods, c"showSoftInput", c"(Landroid/view/View;I)Z", &[Value::obj(view), Value::int(0)]);
} else {
let token = env.call(view, c"getWindowToken", c"()Landroid/os/IBinder;", &[]).object();
env.call(methods, c"hideSoftInputFromWindow", c"(Landroid/os/IBinder;I)Z", &[Value::obj(token), Value::int(0)]);
}
});
}
pub(crate) fn insets_of(env: Env, insets: Obj) -> [i32; 4] {
env.frame(|| {
let types = [c"systemBars", c"displayCutout", c"ime"].map(|name| env.call_static(c"android/view/WindowInsets$Type", name, c"()I", &[]).as_int());
let kept = env.call(insets, c"getInsets", c"(I)Landroid/graphics/Insets;", &[Value::int(types.iter().fold(0, |all, one| all | one))]).object();
[c"left", c"top", c"right", c"bottom"].map(|side| env.int_field(kept, side))
})
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub(crate) struct Display {
pub density: f32,
pub text_scale: f32,
pub dark: bool,
pub accent: Option<u32>,
pub still: bool,
}
pub(crate) fn display(env: Env, activity: Obj) -> Display {
env.frame(|| {
let resources = env.call(activity, c"getResources", c"()Landroid/content/res/Resources;", &[]).object();
let metrics = env.call(resources, c"getDisplayMetrics", c"()Landroid/util/DisplayMetrics;", &[]).object();
let config = env.call(resources, c"getConfiguration", c"()Landroid/content/res/Configuration;", &[]).object();
let accent_id = env.static_int(c"android/R$color", c"system_accent1_500");
let accent = (accent_id != 0).then(|| env.call(activity, c"getColor", c"(I)I", &[Value::int(accent_id)]).as_int() as u32);
let resolver = env.call(activity, c"getContentResolver", c"()Landroid/content/ContentResolver;", &[]).object();
let args = [Value::obj(resolver), Value::obj(env.string("animator_duration_scale")), Value::float(1.0)];
let motion = env.call_static(c"android/provider/Settings$Global", c"getFloat", c"(Landroid/content/ContentResolver;Ljava/lang/String;F)F", &args).as_float();
Display {
density: env.float_field(metrics, c"density").max(0.5),
text_scale: env.float_field(config, c"fontScale").max(0.5),
dark: env.int_field(config, c"uiMode") & 0x30 == 0x20,
accent,
still: motion == 0.0,
}
})
}
pub(crate) fn open_link(activity: Obj, link: &str) -> bool {
let vm = VM.load(Ordering::SeqCst);
if vm.is_null() || activity.is_null() {
return false;
}
let mut raw: *mut c_void = std::ptr::null_mut();
let env = unsafe {
let table = *(vm as *mut *const *const c_void);
let attach: extern "C" fn(*mut c_void, *mut *mut c_void, *mut c_void) -> i32 = std::mem::transmute_copy(&*table.add(ATTACH_CURRENT_THREAD));
(attach(vm, &mut raw, std::ptr::null_mut()) == 0).then_some(Env(raw.cast()))
};
let Some(env) = env else { return false };
let opened = env.frame(|| {
let uri = env.call_static(c"android/net/Uri", c"parse", c"(Ljava/lang/String;)Landroid/net/Uri;", &[Value::obj(env.string(link))]).object();
let intent = env.new_named(c"android/content/Intent", c"(Ljava/lang/String;Landroid/net/Uri;)V", &[Value::obj(env.string("android.intent.action.VIEW")), Value::obj(uri)]);
env.call(intent, c"addFlags", c"(I)Landroid/content/Intent;", &[Value::int(0x1000_0000)]);
env.call(activity, c"startActivity", c"(Landroid/content/Intent;)V", &[Value::obj(intent)]);
!intent.is_null() && !env.threw()
});
unsafe {
let table = *(vm as *mut *const *const c_void);
let detach: extern "C" fn(*mut c_void) -> i32 = std::mem::transmute_copy(&*table.add(DETACH_CURRENT_THREAD));
detach(vm);
}
opened
}