diff --git a/client/android/intent.cpp b/client/android/intent.cpp new file mode 100644 index 00000000..c6f6d848 --- /dev/null +++ b/client/android/intent.cpp @@ -0,0 +1,71 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2025 Guillaume Meunier + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "intent.h" +#include "application.h" +#include + +static int next_request_code; +static std::unordered_map> callbacks; + +intent::intent(const char * intent_type) : + obj(jni::new_object<"android/content/Intent">(jni::string{intent_type})) +{ +} + +intent::intent(jobject jni_obj) : + obj(jni_obj) +{ +} + +void intent::set_type(const char * type) +{ + obj.call>("setType", jni::string{type}); +} + +void intent::add_category(const char * category) +{ + obj.call>("addCategory", jni::string{category}); +} + +void intent::start(std::function callback) +{ + int request_code = next_request_code++; + + callbacks.emplace(request_code, std::move(callback)); + + jni::object<""> activity(application::native_app()->activity->clazz); + + activity.call("startActivityForResult", obj, jni::Int{request_code}); +} + +jni::object<"android/net/Uri"> intent::get_uri() +{ + return obj.call>("getData"); +} + +extern "C" __attribute__((visibility("default"))) void Java_org_meumeu_wivrn_MainActivity_onActivityResult(JNIEnv * env, jobject instance, int request_code, int result_code, jobject data_obj) +{ + auto it = callbacks.find(request_code); + if (it == callbacks.end()) + return; + + it->second(result_code, intent{data_obj}); + + callbacks.erase(it); +} diff --git a/client/android/intent.h b/client/android/intent.h new file mode 100644 index 00000000..3bcb84d9 --- /dev/null +++ b/client/android/intent.h @@ -0,0 +1,38 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2025 Guillaume Meunier + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include "jnipp.h" +#include + +class intent +{ + jni::object<"android/content/Intent"> obj; + +public: + intent(const char * intent_type); + intent(jobject); + + void set_type(const char * type); + void add_category(const char * category); + + void start(std::function callback); + + jni::object<"android/net/Uri"> get_uri(); +}; diff --git a/client/android/jnipp.h b/client/android/jnipp.h index 3d313b03..d9fd8994 100644 --- a/client/android/jnipp.h +++ b/client/android/jnipp.h @@ -85,6 +85,30 @@ struct Void constexpr static const auto call_static_method = &_JNIEnv::CallStaticVoidMethod; }; +struct Int +{ + constexpr static auto static_field = &_JNIEnv::GetStaticIntField; + constexpr static const auto call_method = &_JNIEnv::CallIntMethod; + constexpr static const auto call_static_method = &_JNIEnv::CallStaticIntMethod; + + static std::string type() + { + return "I"; + } + + int value; + + int handle() const + { + return value; + } + + operator int() const + { + return value; + } +}; + namespace details { @@ -139,6 +163,12 @@ struct type_map using type = jni::Void; }; +template <> +struct type_map +{ + using type = jni::Int; +}; + template using type_map_t = type_map::type; } // namespace details @@ -224,30 +254,6 @@ struct Bool } }; -struct Int -{ - constexpr static auto static_field = &_JNIEnv::GetStaticIntField; - constexpr static const auto call_method = &_JNIEnv::CallIntMethod; - constexpr static const auto call_static_method = &_JNIEnv::CallStaticIntMethod; - - static std::string type() - { - return "I"; - } - - int value; - - int handle() const - { - return value; - } - - operator int() const - { - return value; - } -}; - template struct object { diff --git a/client/java/MainActivity.java b/client/java/MainActivity.java index 514ea693..b1b690dc 100644 --- a/client/java/MainActivity.java +++ b/client/java/MainActivity.java @@ -13,6 +13,9 @@ public class MainActivity extends android.app.NativeActivity @Override public native void onNewIntent(android.content.Intent intent); + @Override + public native void onActivityResult(int requestCode, int resultCode, android.content.Intent data); + private android.content.BroadcastReceiver BatteryInfoReceiver = null; @Override