Call an Android API
Import a package from lucent:android/* and call it with Java's names, from the app's Context.
import { PLATFORM } from "lucent:platform";import { BatteryManager } from "lucent:android/android.os";import { appContext } from "lucent:android";import { error } from "lucent:core";
export async function isCharging(): Promise<boolean> { if (PLATFORM === "android") { const manager = appContext().getSystemService(BatteryManager); if (!manager) throw error("E_NO_BATTERY", "BatteryManager isn't available"); return manager.charging; } else { throw error("E_UNSUPPORTED", "Only Android is implemented"); }}See the C++
// Generated by Lucent from battery.lucent.ts. Do not edit.#include "m_battery.h"
#pragma push_macro("isCharging")#undef isCharging
namespace lucent_app {
lucent::Promise<bool> m_battery::isCharging() {#line 7 "battery.lucent.ts" {#line 12 "battery.lucent.ts" lucent::Error v2_ = lucent::withSite(lucent::errorWithCode(LUCENT_STR("E_UNSUPPORTED"), LUCENT_STR("Only Android is implemented")), __FILE__, __LINE__, "isCharging"); lucent::throwError(v2_); } co_return []() -> bool { lucent::unreachable(); }();}
void m_battery::init() {}
} // namespace lucent_app
#pragma pop_macro("isCharging")// Generated by Lucent from battery.lucent.ts. Do not edit.#include <lucent/platform/android.h>#include "m_battery.h"
#pragma push_macro("isCharging")#undef isCharging#pragma push_macro("manager")#undef manager
namespace lucent_app {
lucent::Promise<bool> m_battery::isCharging() {#line 7 "battery.lucent.ts" {#line 8 "battery.lucent.ts" lucent::NativeRef v0_ = lucent::jni::appContext(); lucent::Opt<lucent::NativeRef> manager = [&]() -> lucent::Opt<lucent::NativeRef> { JNIEnv* env = lucent::jni::env(); auto recv_ = v0_; lucent::jni::LocalFrame frame_(env); static jclass cls_ = lucent::jni::findClass("android/content/Context"); static auto id_ = lucent::jni::method(cls_, "getSystemService", "(Ljava/lang/Class;)Ljava/lang/Object;"); auto r_ = env->CallObjectMethod(lucent::jni::unwrap(recv_), id_, lucent::jni::findClass("android/os/BatteryManager")); lucent::jni::check(env); return lucent::jni::wrapOpt(env, r_); }();#line 9 "battery.lucent.ts" bool v4_ = !lucent::truthy(manager); if (v4_) { lucent::Error v7_ = lucent::withSite(lucent::errorWithCode(LUCENT_STR("E_NO_BATTERY"), LUCENT_STR("BatteryManager isn't available")), __FILE__, __LINE__, "isCharging"); lucent::throwError(v7_); }#line 10 "battery.lucent.ts" lucent::NativeRef v9_ = manager.value(); co_return [&]() -> bool { JNIEnv* env = lucent::jni::env(); auto recv_ = v9_; lucent::jni::LocalFrame frame_(env); static jclass cls_ = lucent::jni::findClass("android/os/BatteryManager"); static auto id_ = lucent::jni::method(cls_, "isCharging", "()Z"); auto r_ = env->CallBooleanMethod(lucent::jni::unwrap(recv_), id_); lucent::jni::check(env); return r_ == JNI_TRUE; }(); }}
void m_battery::init() {}
} // namespace lucent_app
#pragma pop_macro("manager")#pragma pop_macro("isCharging")- Import from
lucent:android/<package>: any package ofandroid.jar, or of the app's Gradle dependencies. appContext()fromlucent:androidis the app'sContext.currentActivity()is the Activity in front, or null: use it now, insidemain(), rather than keep it.startActivityForResult()andrequestPermissions()ask from the Activity in front and resolve with the answer.onActivityEvent()reports the Activities' lifecycle.- A
Class<T>parameter takes the class itself:getSystemService(BatteryManager)returns aBatteryManager | null. isCharging()is also the propertycharging, as Kotlin makes properties of getters.- References the SDK doesn't mark
@NonNullareT | null, so check them.
A Java exception becomes a thrown error whose code is the exception's class. Nested classes join with _: Build.VERSION is Build_VERSION. SDK types has every rule.