Add helper for starting an activity on Android

This commit is contained in:
Guillaume Meunier
2025-10-23 21:39:12 +02:00
parent fb509361b6
commit 8854b1b548
4 changed files with 142 additions and 24 deletions
+71
View File
@@ -0,0 +1,71 @@
/*
* WiVRn VR streaming
* Copyright (C) 2025 Guillaume Meunier <guillaume.meunier@centraliens.net>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#include "intent.h"
#include "application.h"
#include <unordered_map>
static int next_request_code;
static std::unordered_map<int, std::function<void(int, intent &&)>> 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<jni::object<"android/content/Intent">>("setType", jni::string{type});
}
void intent::add_category(const char * category)
{
obj.call<jni::object<"android/content/Intent">>("addCategory", jni::string{category});
}
void intent::start(std::function<void(int, intent &&)> callback)
{
int request_code = next_request_code++;
callbacks.emplace(request_code, std::move(callback));
jni::object<""> activity(application::native_app()->activity->clazz);
activity.call<void>("startActivityForResult", obj, jni::Int{request_code});
}
jni::object<"android/net/Uri"> intent::get_uri()
{
return obj.call<jni::object<"android/net/Uri">>("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);
}
+38
View File
@@ -0,0 +1,38 @@
/*
* WiVRn VR streaming
* Copyright (C) 2025 Guillaume Meunier <guillaume.meunier@centraliens.net>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "jnipp.h"
#include <functional>
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<void(int, intent &&)> callback);
jni::object<"android/net/Uri"> get_uri();
};
+30 -24
View File
@@ -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<void>
using type = jni::Void;
};
template <>
struct type_map<int>
{
using type = jni::Int;
};
template <typename T>
using type_map_t = type_map<T>::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 <details::string_literal Type>
struct object
{
+3
View File
@@ -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