C++ Builder - Get the result of an Activity - android

I'm using C++Builder 10.1 Berlin to develop an Android app that scans barcodes. To do this, I'm calling another app (Zxing) when the user clicks on a button in my app, like this:
_di_JIntent intent;
if(Global->ClipService != NULL){
Global->ClipService->SetClipboard(TValue::_op_Implicit(NULL));
intent = TJIntent::Create();
intent->setAction(StringToJString("com.google.zxing.client.android.SCAN"));
SharedActivity()->startActivityForResult(intent,0);
scanCalled = true;
}
else{
ShowMessage("Presse Papier non disponible!");
}
To get the read barcode, I use the clipboard service. Until now, it was enough for me.
Now, I would like to get the type of the barcode. I need to handle Android's onActivityResult event. I found a solution in Delphi, but I'm not able to translate it to C++:
Launching activities and handling results in Delphi XE6 Android apps
In particular, the section titled "Communication from the launched activity".
Indeed, the function TMessageManager.DefaultManager.SubscribeToMessage() needs a TMessageListener in C++, but I don't know how to use it.

So, the real problem is just that you don't understand how to call TMessageManager::SubscribeToMessage() in C++. You should have started by reading the documentation, which includes C++ examples.
Sending and Receiving Messages Using the RTL.
Brian Long's Delphi example would translate to the following in C++:
#include <System.Messaging.hpp>
//...
class TMainForm : public TForm
{
//...
private:
static const int ScanRequestCode = 0;
int FMessageSubscriptionID;
void __fastcall HandleActivityMessage(TObject* const Sender, TMessageBase* const M);
bool __fastcall OnActivityResult(int RequestCode, int ResultCode, _di_JIntent Data);
//...
};
#include <FMX.Platform.Android.hpp>
#include <Androidapi.Helpers.hpp>
#include <Androidapi.JNI.App.hpp>
#include <Androidapi.JNI.Toast.hpp>
#include <LaunchActivities.hpp>
// ...
void __fastcall TMainForm::BarcodeScannerButtonClick(TObject *Sender)
{
FMessageSubscriptionID = TMessageManager::DefaultManager->SubscribeToMessage(__classid(TMessageResultNotification), &HandleActivityMessage);
LaunchQRScanner(ScanRequestCode);
}
void __fastcall TMainForm::HandleActivityMessage(TObject* const Sender, TMessageBase* const M)
{
TMessageResultNotification *msg = dynamic_cast<TMessageResultNotification*>(M);
if (msg)
OnActivityResult(msg->RequestCode, msg->ResultCode, msg->Value);
}
bool __fastcall TMainForm::OnActivityResult(int RequestCode, int ResultCode, _di_JIntent Data)
{
String ScanContent, ScanFormat;
TMessageManager::DefaultManager->Unsubscribe(__classid(TMessageResultNotification), FMessageSubscriptionID);
FMessageSubscriptionID = 0;
// For more info see https://github.com/zxing/zxing/wiki/Scanning-Via-Intent
if (RequestCode == ScanRequestCode)
{
if (ResultCode == TJActivity::JavaClass->RESULT_OK)
{
if (Data)
{
ScanContent = JStringToString(Data->getStringExtra(StringToJString("SCAN_RESULT")));
ScanFormat = JStringToString(Data.getStringExtra(StringToJString("SCAN_RESULT_FORMAT")));
Toast(Format("Found %s format barcode:\n%s", ARRAYOFCONST(( ScanFormat, ScanContent ))), LongToast);
}
}
else if (ResultCode == TJActivity::JavaClass->RESULT_CANCELED)
{
Toast("You cancelled the scan", ShortToast);
}
return true;
}
return false;
}

Related

FMX System messaging to send a pointer(?)

I'm using C++Builder 10.2.
In Android, I would like to send messages from various threads, including the main thread, to the main GUI thread. In Windows, I could post a message and assign an LPARAM or WPARAM to the address of some instance of a struct or class.
I'm trying to use System.Messaging.TMessageManager to do the same thing, similar to the example here: System.Messaging (C++). But I can only send 'simple' types, like UnicodeString or int. I haven't worked out how to send a pointer, assuming it's even possible at all.
I would like to send a struct/class instance like this:
class TSendResult
{
public:
String Message;
unsigned int Value;
int Errno;
__fastcall TSendResult(void);
__fastcall ~TSendResult();
};
If this can be done, how do I write this? I managed to get one version to compile, but got a linker error:
error: undefined reference to 'vtable for System::Messaging::TMessage__1<TSendResult>'
Form constructor:
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
TMessageManager* MessageManager = TMessageManager::DefaultManager;
TMetaClass* MessageClass = __classid(TMessage__1<TSendResult>);
TMessageListenerMethod ShowReceivedMessagePointer = &(this->MMReceiveAndCallBack);
MessageManager->SubscribeToMessage(MessageClass, ShowReceivedMessagePointer);
}
Button click handler:
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
...
TSendResult *SPtr = new TSendResult();
SPtr->Message = "All good";
SPtr->Value = 10;
SPtr->Errno = 0;
TMessageManager* MessageManager = TMessageManager::DefaultManager;
TMessage__1<TSendResult>* Message = new TMessage__1<TSendResult>(*SPtr); // <-- this doesn't look right...
MessageManager->SendMessage(Sender, Message, false);
}
Function that captures messages:
void __fastcall TForm1::MMReceiveAndCallBack(System::TObject* const Sender,
System::Messaging::TMessageBase* const M)
{
TMessage__1<TSendResult>* Message = dynamic_cast<TMessage__1<TSendResult>*>(M);
if (Message) {
ShowMessage(Message->Value.Message);
}
}
TMessage__1<T> is a C++ class implementation for the Delphi Generic TMessage<T> class. Unfortunately, there is a documented limitation when using Delphi Generic classes in C++, which is why you are getting a linker error:
How to Handle Delphi Generics in C++
Delphi generics are exposed to C++ as templates. However, it is important to realize that the instantiations occur on the Delphi side, not in C++. Therefore, you can only use these template for types that were explicitly instantiated in Delphi code.
...
If C++ code attempts to use a Delphi generic for types that were not instantiated in Delphi, you'll get errors at link time.
Which is why TMessage__1<UnicodeString> works but TMessage__1<TSendResult> does not, as there is an instantiation of TMessage<UnicodeString> present in the Delphi RTL. Whoever wrote the C++ example you are looking at was likely not aware of this limitation and was just translating the Delphi example as-is.
That being said, you have two choices:
Add a Delphi .pas unit to your C++ project, implementing TSendResult as a Delphi record, and defining an instantiation of TMessage<TSendResult> for it. Then you can use that unit in your C++ code (C++Builder will generate a C++ .hpp file for you when the .pas file is compiled), eg:
unit MyMessageTypes;
interface
uses
System.Messaging;
type
TSendResult = record
Message: String;
Value: UInt32;
Errno: Integer;
end;
TSendResultMsg = TMessage<TSendResult>;
implementation
initialization
TSendResultMsg.Create.Free;
finalization
end.
#include "MyMessageTypes.hpp"
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
TMessageManager::DefaultManager->SubscribeToMessage(__classid(TSendResultMsg), &MMReceiveAndCallBack);
}
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
...
TSendResult Res;
Res.Message = _D("All good");
Res.Value = 10;
Res.Errno = 0;
TSendResultMsg *Message = new TSendResultMsg(Res);
TMessageManager::DefaultManager->SendMessage(this, Message, true);
}
void __fastcall TForm1::MMReceiveAndCallBack(System::TObject* const Sender,
System::Messaging::TMessageBase* const M)
{
const TSendResultMsg* Message = static_cast<const TSendResultMsg*>(M);
ShowMessage(Message->Value.Message);
}
rather than using TMessage__1 at all, you can instead derive TSendResult directly from TMessageBase, eg:
class TSendResultMsg : public TMessageBase
{
public:
String Message;
unsigned int Value;
int Errno;
};
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
TMessageManager::DefaultManager->SubscribeToMessage(__classid(TSendResultMsg), &MMReceiveAndCallBack);
}
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
...
TSendResultMsg *Message = new TSendResultMsg;
Message->Message = _D("All good");
Message->Value = 10;
Message->Errno = 0;
TMessageManager::DefaultManager->SendMessage(this, Message, true);
}
void __fastcall TForm1::MMReceiveAndCallBack(System::TObject* const Sender,
System::Messaging::TMessageBase* const M)
{
const TSendResultMsg* Message = static_cast<const TSendResultMsg*>(M);
ShowMessage(Message->Message);
}

Firebase C++ Messaging Crashing on Startup

So, I'm trying to use the Firebase c++ library in my Unreal project, but I'm getting some very consistent crashes: it crashes the first time I run it after a new uninstalling it, and works fine afterwards
Here's the stack trace I've got from firebase crash logging:
E/art ( 7271): No implementation found for void com.google.firebase.messaging.cpp.RegistrationIntentService.nativeOnTokenReceived(java.lang.String) (tried Java_com_google_firebase_messaging_cpp_RegistrationIntentService_nativeOnTokenReceived and Java_com_google_firebase_messaging_cpp_RegistrationIntentService_nativeOnTokenReceived__Ljava_lang_String_2)
E/UncaughtException( 7271):
E/UncaughtException( 7271): java.lang.UnsatisfiedLinkError: No implementation found for void com.google.firebase.messaging.cpp.RegistrationIntentService.nativeOnTokenReceived(java.lang.String) (tried Java_com_google_firebase_messaging_cpp_RegistrationIntentService_nativeOnTokenReceived and Java_com_google_firebase_messaging_cpp_RegistrationIntentService_nativeOnTokenReceived__Ljava_lang_String_2)
E/UncaughtException( 7271): at com.google.firebase.messaging.cpp.RegistrationIntentService.nativeOnTokenReceived(Native Method)
E/UncaughtException( 7271): at com.google.firebase.messaging.cpp.RegistrationIntentService.onHandleIntent(RegistrationIntentService.java:31)
E/UncaughtException( 7271): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
E/UncaughtException( 7271): at android.os.Handler.dispatchMessage(Handler.java:102)
E/UncaughtException( 7271): at android.os.Looper.loop(Looper.java:145)
E/UncaughtException( 7271): at android.os.HandlerThread.run(HandlerThread.java:61)
It says there's no implementation for nativeOnTokenReceived, but it is implemented in the firebase c++ sdk library.
The crash happens when RegistrationIntentService is sent an intent from FcmInstanceIDListenerService, which happens when firebase gives a new token, which always happens on app startup after reinstalling it or clearing it's app data (I'm not sure if it's possible to make it happen at a different time than startup).
However, RegistrationIntentService has onHandleIntent activated, and calls nativeOnTokenReceived without any problems when my c++ listener class is initialized, during the course of the app. Does anybody know what might be causing this crash?
It might be relavent that Unreal's build process packages the static .a libraries from the sdk into a single .so before using ndk-build.
Here's the code for RegistrationIntentService and FcmInstanceIDListenerService extracted from the sdk's libmessaging_java.jar
FcmInstanceIDListenerService.java
package com.google.firebase.messaging.cpp;
import android.content.Intent;
import com.google.firebase.iid.FirebaseInstanceIdService;
public class FcmInstanceIDListenerService
extends FirebaseInstanceIdService
{
public void onTokenRefresh()
{
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
}
}
RegistrationIntentService.java
package com.google.firebase.messaging.cpp;
import android.app.IntentService;
import android.content.Intent;
import com.google.firebase.iid.FirebaseInstanceId;
public class RegistrationIntentService
extends IntentService
{
private static final String TAG = "FirebaseRegService";
public RegistrationIntentService()
{
super("FirebaseRegService");
}
protected void onHandleIntent(Intent intent)
{
DebugLogging.log("FirebaseRegService", String.format("onHandleIntent token=%s", new Object[] {
FirebaseInstanceId.getInstance().getToken() }));
String token = FirebaseInstanceId.getInstance().getToken();
if (token != null) {
nativeOnTokenReceived(token);
}
}
private static native void nativeOnTokenReceived(String paramString);
}
So, I guess I'm posting my solution to this problem up here..
Firstly, I don't really know why there is a problem in the first place, but I have a feeling it has to do with Unreal's build system.
(Fun fact: I've also had unexplainable errors in Google's Protobuf libraries, when trying to use them in Unreal Engine.)
So since, the JNI couldn't find the library defined function it needed, I wrote my own function to replace it.
Basically, when you're using the Firebase Messaging C++ SDK, there's two components to include in your project: the c++ static library and headers, and a java library, libmessaging_java.jar
The jar file defines a few classes, most of which are good fine, but a few need to be edited, which you can do if you decompile it
(I used this tool)
So, inside RegistrationIntentService I declared
private static native void nativeOnNewToken(String paramString);
and replaced the call to nativeOnTokenReceived with it
and in ListenerService I declared
private static native void nativeOnNewMessage(String paramString1, String paramString2, String paramString3, Map<String, String> paramMap);
and added it to writeMessageToInternalStorage()
private void writeMessageToInternalStorage(String from, String msgId, String error, Map<String, String> data)
{
//Added code
nativeOnNewMessage(from, msgId, error, data);
//I'm passing the message directly to the C++ code, rather than storing
//it in a buffer, and processing every once in a while
//like the sdk normally does; so surround this crap with if(false)
if(false){
//end added code
try
{
JSONObject json = messageToJson(from, msgId, error, data);
DebugLogging.log("FIREBASE_LISTENER", json.toString());
writeStorageFile(json.toString());
} catch (JSONException e) {
e.printStackTrace();
}
//added code
}
///end added code
}
Now, the messages and tokens are being sent to my functions, so I need to declare them:
#include "FirebaseMessageListener.h"
#include "stdio.h"
#include <string>
#include <map>
#if PLATFORM_ANDROID
//jni calls from the listener services
extern "C" void Java_com_google_firebase_messaging_cpp_ListenerService_nativeOnNewMessage(JNIEnv* jenv, jobject thiz, jstring from, jstring mssgID, jstring error, jobject data) {
UE_LOG(FirebaseLog, Log, TEXT("Entering nativeOnNewMessage *****"));
printf("Entering nativeOnNewMessage *****");
std::map<std::string, std::string> data_out;
std::string messageID;
std::string fromID;
//code iterating through map from java, based off code from here:https://android.googlesource.com/platform/frameworks/base.git/+/a3804cf77f0edd93f6247a055cdafb856b117eec/media/jni/android_media_MediaMetadataRetriever.cpp
// data is a Map<String, String>.
if (data) {
// Get the Map's entry Set.
jclass mapClass = jenv->FindClass("java/util/Map");
if (mapClass == NULL) {
return;
}
jmethodID entrySet =
jenv->GetMethodID(mapClass, "entrySet", "()Ljava/util/Set;");
if (entrySet == NULL) {
return;
}
jobject set = jenv->CallObjectMethod(data, entrySet);
if (set == NULL) {
return;
}
// Obtain an iterator over the Set
jclass setClass = jenv->FindClass("java/util/Set");
if (setClass == NULL) {
return;
}
jmethodID iterator =
jenv->GetMethodID(setClass, "iterator", "()Ljava/util/Iterator;");
if (iterator == NULL) {
return;
}
jobject iter = jenv->CallObjectMethod(set, iterator);
if (iter == NULL) {
return;
}
// Get the Iterator method IDs
jclass iteratorClass = jenv->FindClass("java/util/Iterator");
if (iteratorClass == NULL) {
return;
}
jmethodID hasNext = jenv->GetMethodID(iteratorClass, "hasNext", "()Z");
if (hasNext == NULL) {
return;
}
jmethodID next =
jenv->GetMethodID(iteratorClass, "next", "()Ljava/lang/Object;");
if (next == NULL) {
return;
}
// Get the Entry class method IDs
jclass entryClass = jenv->FindClass("java/util/Map$Entry");
if (entryClass == NULL) {
return;
}
jmethodID getKey =
jenv->GetMethodID(entryClass, "getKey", "()Ljava/lang/Object;");
if (getKey == NULL) {
return;
}
jmethodID getValue =
jenv->GetMethodID(entryClass, "getValue", "()Ljava/lang/Object;");
if (getValue == NULL) {
return;
}
// Iterate over the entry Set
while (jenv->CallBooleanMethod(iter, hasNext)) {
jobject entry = jenv->CallObjectMethod(iter, next);
jstring key = (jstring)jenv->CallObjectMethod(entry, getKey);
jstring value = (jstring)jenv->CallObjectMethod(entry, getValue);
const char* keyStr = jenv->GetStringUTFChars(key, NULL);
if (!keyStr) { // Out of memory
return;
}
const char* valueStr = jenv->GetStringUTFChars(value, NULL);
if (!valueStr) { // Out of memory
jenv->ReleaseStringUTFChars(key, keyStr);
return;
}
data_out.insert(std::pair<std::string, std::string>(std::string(keyStr), std::string(valueStr)));
jenv->DeleteLocalRef(entry);
jenv->ReleaseStringUTFChars(key, keyStr);
jenv->DeleteLocalRef(key);
jenv->ReleaseStringUTFChars(value, valueStr);
jenv->DeleteLocalRef(value);
}
}
if (from != nullptr) {
const char* valueStr = jenv->GetStringUTFChars(from, NULL);
if (!valueStr) { // Out of memory
return;
}
fromID = std::string(valueStr);
jenv->ReleaseStringUTFChars(from, valueStr);
}
if (mssgID != nullptr) {
const char* valueStr = jenv->GetStringUTFChars(mssgID, NULL);
if (!valueStr) { // Out of memory
return;
}
messageID = std::string(valueStr);
jenv->ReleaseStringUTFChars(mssgID, valueStr);
}
FirebaseMessageListener::Get()->onNewMessage(fromID, messageID, data_out);
}
extern "C" void Java_com_google_firebase_messaging_cpp_RegistrationIntentService_nativeOnNewToken(JNIEnv* jenv, jobject thiz, jstring inJNIStr) {
UE_LOG(FirebaseLog, Log, TEXT("Entering nativeOnNewToken *****"));
printf("Entering nativeOnNewToken *****");
//first, put the token into a c string
jboolean isCopy;
const char* token = jenv->GetStringUTFChars(inJNIStr, &isCopy);
FirebaseMessageListener::Get()->onNewToken(token);
}
#endif
These pass the messages on to a singleton class I made. You can do whatever you want with them here, but I pass them along to a firebase::messaging::Listener reference, to try and keep compatibility with the Firebase SDK (incase I can get it working properly)
class RIFT411_API FirebaseMessageListener
{
private:
static FirebaseMessageListener* self;
//private so that new instance can only be made through call to Get()
FirebaseMessageListener();
#if PLATFORM_ANDROID
JNIEnv * env = FAndroidApplication::GetJavaEnv();
jobject activity = FAndroidApplication::GetGameActivityThis();
#endif
//signals to return the key
void getToken();
//send the messages to the firebase sdk implemented listener, so we'll have an easier time if we ever want to move back
firebase::messaging::Listener *listener = nullptr;
public:
static FirebaseMessageListener* Get();
void onNewToken(const char* token);
void onNewMessage(std::string, std::string, std::map<std::string, std::string> &data);
void Init(firebase::messaging::Listener *listener);
~FirebaseMessageListener();
};
//
FirebaseMessageListener* FirebaseMessageListener::self = nullptr;
FirebaseMessageListener* FirebaseMessageListener::Get()
{
if (self == nullptr) {
self = new FirebaseMessageListener();
}
return self;
}
void FirebaseMessageListener::getToken() {
#if PLATFORM_ANDROID
//This has to happen in the main thread, or some of the FindClass() calls will fail
UE_LOG(FirebaseLog, Log, TEXT("Trying to grab token *****"));
printf("Trying to grab token *****");
env = FAndroidApplication::GetJavaEnv();
activity = FAndroidApplication::GetGameActivityThis();
jclass cls_intent = (env)->FindClass("android/content/Intent");
if (NULL == cls_intent) { UE_LOG(FirebaseLog, Error, TEXT("Failure getting cls_intent")); return; }
jclass cls_service = FAndroidApplication::FindJavaClass("com/google/firebase/messaging/cpp/RegistrationIntentService");
//jclass cls_service = (env)->FindClass("com/google/firebase/messaging/cpp/RegistrationIntentService");
if (NULL == cls_service) { UE_LOG(FirebaseLog, Error, TEXT("Failure getting cls_service")); }
// Get the Method ID of the constructor which takes an int
jmethodID mid_Init = (env)->GetMethodID(cls_intent, "<init>", "(Landroid/content/Context;Ljava/lang/Class;)V");
if (NULL == mid_Init) { UE_LOG(FirebaseLog, Error, TEXT("Failure getting mid_Init")); return;}
// Call back constructor to allocate a new instance, with an int argument
jobject obj_intent = (env)->NewObject(cls_intent, mid_Init, activity, cls_service);
if (NULL == obj_intent) { UE_LOG(FirebaseLog, Error, TEXT("Failure getting obj_intent")); return; }
if (NULL == activity) { UE_LOG(FirebaseLog, Error, TEXT("Failure getting activity")); return; }
jclass cls_activity = (env)->GetObjectClass(activity);
if (NULL == cls_activity) { UE_LOG(FirebaseLog, Error, TEXT("Failure getting cls_activity")); return; }
jmethodID mid_start = (env)->GetMethodID(cls_activity, "startService", "(Landroid/content/Intent;)Landroid/content/ComponentName;");
if (NULL == mid_start) { UE_LOG(FirebaseLog, Error, TEXT("Failure getting mid_start")); return; }
UE_LOG(FirebaseLog, Log, TEXT("MADE IT TO THE END!"));
(env)->CallObjectMethod(activity, mid_start, obj_intent);
#endif
}
void FirebaseMessageListener::onNewToken(const char* token) {
UE_LOG(FirebaseLog, Log, TEXT("Recieving new token in Unreal! Hooray! ***** %s"), *FString(token));
printf("Recieving new Message in Unreal! Hooray! *****\n");
if (listener != nullptr) {
listener->OnTokenReceived(token);
}
}
void FirebaseMessageListener::onNewMessage(std::string from, std::string MessageID, std::map<std::string, std::string> &data) {
UE_LOG(FirebaseLog, Log, TEXT("Recieving new Message in Unreal! Hooray! *****"));
printf("Recieving new Message in Unreal! Hooray! *****\n");
if (!data.empty()) {
UE_LOG(FirebaseLog, Log, TEXT("data:"));
typedef std::map<std::string, std::string>::const_iterator MapIter;
for (MapIter it = data.begin(); it != data.end(); ++it) {
FString s1 = FString(it->first.c_str());
FString s2 = FString(it->second.c_str());
UE_LOG(FirebaseLog, Log, TEXT(" %s: %s"), *s1, *s2);
}
}
::firebase::messaging::Message message;
message.data = data;
message.from = from;
message.message_id = MessageID;
if (listener != nullptr) {
listener->OnMessage(message);
}
}
void FirebaseMessageListener::Init(firebase::messaging::Listener *newlistener) {
this->listener = newlistener;
FGraphEventRef Task = FFunctionGraphTask::CreateAndDispatchWhenReady([=]()
{
getToken();
}, TStatId(), NULL, ENamedThreads::GameThread);
}
FirebaseMessageListener::FirebaseMessageListener()
{
self = this;
}
FirebaseMessageListener::~FirebaseMessageListener()
{
}
One thing about this is that you won't get data from notifications that are recieved while your app is closed. That data is packaged in an Intent, and I've almost got a good way to get it, to I'll probably post that once I finish it
com.google.firebase.messaging.cpp just listens for messages sent by FCM's service and forwards them to the Android C++ library (libmessaging.a). It looks like the problem is due to the firebase-messaging aar not being included in your application which had led to the com.google.firebase.messaging.cpp package not being loaded as it depends upon classes in the firebase-messaging aar that are not present.
For example, when building using gradle it's possible to include the aar using the following https://github.com/firebase/quickstart-cpp/blob/master/messaging/testapp/build.gradle#L93
Right now it's a little tricky to include aars in Unreal projects as described in https://groups.google.com/d/msg/firebase-talk/eGNr36dpB70/VXVqBfL1BAAJ . Basically you'll need to unzip (aars are just zip files) the firebase-messaging aar (look under ${ANDROID_HOME}/extras/google/firebase/firebase-messaging) and it's dependencies (you'll need to read each *.pom file to get the dependencies) and include the various .jars, AndroidManifest.xml files and resources extracted from each aar in your Unreal project. It's very important to make sure each AndroidManifest.xml gets merged into your final application's AndroidManifest.xml so that all services and content providers used by Firebase are included.

Understanding and implementing native Binders in android using C++

I want to implement a simple IPC mechanism using Binders in android. For that, I searched on the Internet and found this. I compiled it and it runs fine on my Android device. I tried to take an overall understanding of the program, by searching for each class on AOSP, but everything got more difficult and messed up. Can anyone please explain (just high level), maybe by adding more comments, so that it also helps some future visitors. Here's the code is taken from there:
#define LOG_TAG "binder_demo"
#include <stdlib.h>
#include "utils/RefBase.h"
#include "utils/Log.h"
#include "utils/TextOutput.h"
#include <binder/IInterface.h>
#include <binder/IBinder.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <binder/IPCThreadState.h>
using namespace android;
#define INFO(...) \
do { \
printf(__VA_ARGS__); \
printf("\n"); \
LOGD(__VA_ARGS__); \
} while(0)
void assert_fail(const char *file, int line, const char *func, const char *expr) {
INFO("assertion failed at file %s, line %d, function %s:",
file, line, func);
INFO("%s", expr);
abort();
}
#define ASSERT(e) \
do { \
if (!(e)) \
assert_fail(__FILE__, __LINE__, __func__, #e); \
} while(0)
// Where to print the parcel contents: aout, alog, aerr. alog doesn't seem to work.
#define PLOG aout
// Interface (our AIDL) - Shared by server and client
class IDemo : public IInterface {
public:
enum {
ALERT = IBinder::FIRST_CALL_TRANSACTION,
PUSH,
ADD
};
// Sends a user-provided value to the service
virtual void push(int32_t data) = 0;
// Sends a fixed alert string to the service
virtual void alert() = 0;
// Requests the service to perform an addition and return the result
virtual int32_t add(int32_t v1, int32_t v2) = 0;
DECLARE_META_INTERFACE(Demo); // Expands to 5 lines below:
//static const android::String16 descriptor;
//static android::sp<IDemo> asInterface(const android::sp<android::IBinder>& obj);
//virtual const android::String16& getInterfaceDescriptor() const;
//IDemo();
//virtual ~IDemo();
};
// Client
class BpDemo : public BpInterface<IDemo> {
public:
BpDemo(const sp<IBinder>& impl) : BpInterface<IDemo>(impl) {
LOGD("BpDemo::BpDemo()");
}
virtual void push(int32_t push_data) {
Parcel data, reply;
data.writeInterfaceToken(IDemo::getInterfaceDescriptor());
data.writeInt32(push_data);
aout << "BpDemo::push parcel to be sent:\n";
data.print(PLOG); endl(PLOG);
remote()->transact(PUSH, data, &reply);
aout << "BpDemo::push parcel reply:\n";
reply.print(PLOG); endl(PLOG);
LOGD("BpDemo::push(%i)", push_data);
}
virtual void alert() {
Parcel data, reply;
data.writeInterfaceToken(IDemo::getInterfaceDescriptor());
data.writeString16(String16("The alert string"));
remote()->transact(ALERT, data, &reply, IBinder::FLAG_ONEWAY); // asynchronous call
LOGD("BpDemo::alert()");
}
virtual int32_t add(int32_t v1, int32_t v2) {
Parcel data, reply;
data.writeInterfaceToken(IDemo::getInterfaceDescriptor());
data.writeInt32(v1);
data.writeInt32(v2);
aout << "BpDemo::add parcel to be sent:\n";
data.print(PLOG); endl(PLOG);
remote()->transact(ADD, data, &reply);
LOGD("BpDemo::add transact reply");
reply.print(PLOG); endl(PLOG);
int32_t res;
status_t status = reply.readInt32(&res);
LOGD("BpDemo::add(%i, %i) = %i (status: %i)", v1, v2, res, status);
return res;
}
};
//IMPLEMENT_META_INTERFACE(Demo, "Demo");
// Macro above expands to code below. Doing it by hand so we can log ctor and destructor calls.
const android::String16 IDemo::descriptor("Demo");
const android::String16& IDemo::getInterfaceDescriptor() const {
return IDemo::descriptor;
}
android::sp<IDemo> IDemo::asInterface(const android::sp<android::IBinder>& obj) {
android::sp<IDemo> intr;
if (obj != NULL) {
intr = static_cast<IDemo*>(obj->queryLocalInterface(IDemo::descriptor).get());
if (intr == NULL) {
intr = new BpDemo(obj);
}
}
return intr;
}
IDemo::IDemo() { LOGD("IDemo::IDemo()"); }
IDemo::~IDemo() { LOGD("IDemo::~IDemo()"); }
// End of macro expansion
// Server
class BnDemo : public BnInterface<IDemo> {
virtual status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags = 0);
};
status_t BnDemo::onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
LOGD("BnDemo::onTransact(%i) %i", code, flags);
data.checkInterface(this);
data.print(PLOG); endl(PLOG);
switch(code) {
case ALERT: {
alert(); // Ignoring the fixed alert string
return NO_ERROR;
} break;
case PUSH: {
int32_t inData = data.readInt32();
LOGD("BnDemo::onTransact got %i", inData);
push(inData);
ASSERT(reply != 0);
reply->print(PLOG); endl(PLOG);
return NO_ERROR;
} break;
case ADD: {
int32_t inV1 = data.readInt32();
int32_t inV2 = data.readInt32();
int32_t sum = add(inV1, inV2);
LOGD("BnDemo::onTransact add(%i, %i) = %i", inV1, inV2, sum);
ASSERT(reply != 0);
reply->print(PLOG); endl(PLOG);
reply->writeInt32(sum);
return NO_ERROR;
} break;
default:
return BBinder::onTransact(code, data, reply, flags);
}
}
class Demo : public BnDemo {
virtual void push(int32_t data) {
INFO("Demo::push(%i)", data);
}
virtual void alert() {
INFO("Demo::alert()");
}
virtual int32_t add(int32_t v1, int32_t v2) {
INFO("Demo::add(%i, %i)", v1, v2);
return v1 + v2;
}
};
// Helper function to get a hold of the "Demo" service.
sp<IDemo> getDemoServ() {
sp<IServiceManager> sm = defaultServiceManager();
ASSERT(sm != 0);
sp<IBinder> binder = sm->getService(String16("Demo"));
// TODO: If the "Demo" service is not running, getService times out and binder == 0.
ASSERT(binder != 0);
sp<IDemo> demo = interface_cast<IDemo>(binder);
ASSERT(demo != 0);
return demo;
}
int main(int argc, char **argv) {
if (argc == 1) {
LOGD("We're the service");
defaultServiceManager()->addService(String16("Demo"), new Demo());
android::ProcessState::self()->startThreadPool();
LOGD("Demo service is now ready");
IPCThreadState::self()->joinThreadPool();
LOGD("Demo service thread joined");
} else if (argc == 2) {
INFO("We're the client: %s", argv[1]);
int v = atoi(argv[1]);
sp<IDemo> demo = getDemoServ();
demo->alert();
demo->push(v);
const int32_t adder = 5;
int32_t sum = demo->add(v, adder);
LOGD("Addition result: %i + %i = %i", v, adder, sum);
}
return 0;
}
I know this is a bit late but checkout this amazing explanation by Gabriel Burca on Android IPC mechanism here. You can find a working example with a very simple C++ code from the same author here. Further it has clear instructions how to compile and add it to your AOSP source. Cheers!

Qt/Necessitas - reasonable QFileDialog replacement/skin?

I'm looking for a nice way to address porting Qt applications to Qt/Necessitas (Android).
Some of the QtGUI widgets are absolutely atrocious - unfortunately, including QFileDialog.
Do you know of any replacements with a proper look and feel?
Is making QFileDialog usable anywhere near high priority for Necessitas developers?
#include <QApplication>
#include <QFileDialog>
int main(int argc, char* argv[]) {
QApplication a(argc, argv);
QString fileName = QFileDialog::getOpenFileName(NULL,
QObject::tr("Open Image"), "/home/jana", QObject::tr("Image Files (*.png *.jpg *.bmp)"));
a.exec();
}
Android does not have own, native file dialog. We can use QtAndroidExtras to invoke external application which is able to pick a file:
I wrote wrapper, that could be used for that. Here's full code:
androidfiledialog.h
#ifndef ANDROIDFILEDIALOG_H
#define ANDROIDFILEDIALOG_H
#include <QObject>
#include <QAndroidJniObject>
#include <QtAndroid>
#include <QAndroidActivityResultReceiver>
class AndroidFileDialog : public QObject
{
Q_OBJECT
public:
explicit AndroidFileDialog(QObject *parent = 0);
virtual ~AndroidFileDialog();
bool provideExistingFileName();
private:
class ResultReceiver : public QAndroidActivityResultReceiver {
AndroidFileDialog *_dialog;
public:
ResultReceiver(AndroidFileDialog *dialog);
virtual ~ResultReceiver();
void handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject &data);
QString uriToPath(QAndroidJniObject uri);
};
static const int EXISTING_FILE_NAME_REQUEST = 1;
ResultReceiver *receiver;
void emitExistingFileNameReady(QString result);
signals:
void existingFileNameReady(QString result);
};
#endif // ANDROIDFILEDIALOG_H
androidfiledialog.cpp
#include "androidfiledialog.h"
AndroidFileDialog::ResultReceiver::ResultReceiver(AndroidFileDialog *dialog) : _dialog(dialog) {}
AndroidFileDialog::ResultReceiver::~ResultReceiver() {}
void AndroidFileDialog::ResultReceiver::handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject &data)
{
jint RESULT_OK = QAndroidJniObject::getStaticField<jint>("android/app/Activity", "RESULT_OK");
if (receiverRequestCode == EXISTING_FILE_NAME_REQUEST && resultCode == RESULT_OK) {
QAndroidJniObject uri = data.callObjectMethod("getData", "()Landroid/net/Uri;");
QString path = uriToPath(uri);
_dialog->emitExistingFileNameReady(path);
} else {
_dialog->emitExistingFileNameReady(QString());
}
}
QString AndroidFileDialog::ResultReceiver::uriToPath(QAndroidJniObject uri)
{
if (uri.toString().startsWith("file:", Qt::CaseInsensitive)) {
return uri.callObjectMethod("getPath", "()Ljava/lang/String;").toString();
} else {
QAndroidJniObject contentResolver = QtAndroid::androidActivity().callObjectMethod("getContentResolver", "()Landroid/content/ContentResolver;");
QAndroidJniObject cursor = contentResolver.callObjectMethod("query", "(Landroid/net/Uri;[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)Landroid/database/Cursor;", uri.object<jobject>(), 0, 0, 0, 0);
QAndroidJniObject DATA = QAndroidJniObject::fromString("_data");
jint columnIndex = cursor.callMethod<jint>("getColumnIndexOrThrow", "(Ljava/lang/String;)I", DATA.object<jstring>());
cursor.callMethod<jboolean>("moveToFirst", "()Z");
QAndroidJniObject result = cursor.callObjectMethod("getString", "(I)Ljava/lang/String;", columnIndex);
return result.isValid() ? result.toString() : QString();
}
}
AndroidFileDialog::AndroidFileDialog(QObject *parent) : QObject(parent)
{
receiver = new ResultReceiver(this);
}
AndroidFileDialog::~AndroidFileDialog()
{
delete receiver;
}
bool AndroidFileDialog::provideExistingFileName()
{
QAndroidJniObject ACTION_GET_CONTENT = QAndroidJniObject::fromString("android.intent.action.GET_CONTENT");
QAndroidJniObject intent("android/content/Intent");
if (ACTION_GET_CONTENT.isValid() && intent.isValid()) {
intent.callObjectMethod("setAction", "(Ljava/lang/String;)Landroid/content/Intent;", ACTION_GET_CONTENT.object<jstring>());
intent.callObjectMethod("setType", "(Ljava/lang/String;)Landroid/content/Intent;", QAndroidJniObject::fromString("file/*").object<jstring>());
QtAndroid::startActivity(intent.object<jobject>(), EXISTING_FILE_NAME_REQUEST, receiver);
return true;
} else {
return false;
}
}
void AndroidFileDialog::emitExistingFileNameReady(QString result)
{
emit existingFileNameReady(result);
}
You have to add to your *.pro file:
QT += androidextras
using example:
AndroidFileDialog *fileDialog = new AndroidFileDialog();
connect(fileDialog, SIGNAL(existingFileNameReady(QString)), this, SLOT(openFileNameReady(QString)));
bool success = fileDialog->provideExistingFileName();
if (!success) {
qDebug() << "Problem with JNI or sth like that...";
disconnect(fileDialog, SIGNAL(existingFileNameReady(QString)), this, SLOT(openFileNameReady(QString)));
//or just delete fileDialog instead of disconnect
}
slot implementation:
void MyClass::openFileNameReady(QString fileName)
{
if (!fileName.isNull()) {
qDebug() << "FileName: " << fileName;
} else {
qDebug() << "User did not choose file";
}
}
Please confirm this solution works properly on your device.
You could easily build your own file dialog either with QtWidgets or QML, by using the out-of-the-box QFileSystemModel class or the FolderListModel element.
As for whether it is priority or not, at this point it seems that Necessitas will be absorbed by Digia's efforts to support Android. I doubt there will be significant efforts to style QtWidgets appropriately, since the module is marked as DONE and all the focus for UI is on QML. So, I wouldn't hold by breath if I were you. Plus the stock Qt widgets look completely ugly on non-desktop platforms.

Segmentation fault when using dlclose(...) on android platform

I have some problems when using the dynamic loading API (<dlfcn.h>: dlopen(), dlclose(), etc) on Android.
I'm using NDK standalone toolchain (version 8) to compile the applications and libraries.
The Android version is 2.2.1 Froyo.
Here is the source code of the simple shared library.
#include <stdio.h>
int iii = 0;
int *ptr = NULL;
__attribute__((constructor))
static void init()
{
iii = 653;
}
__attribute__((destructor))
static void cleanup()
{
}
int aaa(int i)
{
printf("aaa %d\n", iii);
}
Here is the program source code which uses the mentioned library.
#include <dlfcn.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
void *handle;
typedef int (*func)(int);
func bbb;
printf("start...\n");
handle = dlopen("/data/testt/test.so", RTLD_LAZY);
if (!handle)
{
return 0;
}
bbb = (func)dlsym(handle, "aaa");
if (bbb == NULL)
{
return 0;
}
bbb(1);
dlclose(handle);
printf("exit...\n");
return 0;
}
With these sources everything is working fine, but when I try to use some STL functions or classes, the program crashes with a segmentation fault, when the main() function exits, for example when using this source code for the shared library.
#include <iostream>
using namespace std;
int iii = 0;
int *ptr = NULL;
__attribute__((constructor))
static void init()
{
iii = 653;
}
__attribute__((destructor))
static void cleanup()
{
}
int aaa(int i)
{
cout << iii << endl;
}
With this code, the program crashes with segmentation fault after or the during main() function exit.
I have tried couple of tests and found the following results.
Without using of STL everything is working fine.
When use STL and do not call dlclose() at the end, everything is working fine.
I tried to compile with various compilation flags like -fno-use-cxa-atexit or -fuse-cxa-atexit, the result is the same.
What is wrong in my code that uses the STL?
Looks like I found the reason of the bug. I have tried another example with the following source files:
Here is the source code of the simple class:
myclass.h
class MyClass
{
public:
MyClass();
~MyClass();
void Set();
void Show();
private:
int *pArray;
};
myclass.cpp
#include <stdio.h>
#include <stdlib.h>
#include "myclass.h"
MyClass::MyClass()
{
pArray = (int *)malloc(sizeof(int) * 5);
}
MyClass::~MyClass()
{
free(pArray);
pArray = NULL;
}
void MyClass::Set()
{
if (pArray != NULL)
{
pArray[0] = 0;
pArray[1] = 1;
pArray[2] = 2;
pArray[3] = 3;
pArray[4] = 4;
}
}
void MyClass::Show()
{
if (pArray != NULL)
{
for (int i = 0; i < 5; i++)
{
printf("pArray[%d] = %d\n", i, pArray[i]);
}
}
}
As you can see from the code I did not used any STL related stuff.
Here is the source files of the functions library exports.
func.h
#ifdef __cplusplus
extern "C" {
#endif
int SetBabe(int);
int ShowBabe(int);
#ifdef __cplusplus
}
#endif
func.cpp
#include <stdio.h>
#include "myclass.h"
#include "func.h"
MyClass cls;
__attribute__((constructor))
static void init()
{
}
__attribute__((destructor))
static void cleanup()
{
}
int SetBabe(int i)
{
cls.Set();
return i;
}
int ShowBabe(int i)
{
cls.Show();
return i;
}
And finally this is the source code of the programm that uses the library.
main.cpp
#include <dlfcn.h>
#include <stdlib.h>
#include <stdio.h>
#include "../simple_lib/func.h"
int main()
{
void *handle;
typedef int (*func)(int);
func bbb;
printf("start...\n");
handle = dlopen("/data/testt/test.so", RTLD_LAZY);
if (!handle)
{
printf("%s\n", dlerror());
return 0;
}
bbb = (func)dlsym(handle, "SetBabe");
if (bbb == NULL)
{
printf("%s\n", dlerror());
return 0;
}
bbb(1);
bbb = (func)dlsym(handle, "ShowBabe");
if (bbb == NULL)
{
printf("%s\n", dlerror());
return 0;
}
bbb(1);
dlclose(handle);
printf("exit...\n");
return 0;
}
Again as you can see the program using the library also does not using any STL related stuff, but after run of the program I got the same segmentation fault during main(...) function exit. So the issue is not connected to STL itself, and it is hidden in some other place. Then after some long research I found the bug.
Normally the destructors of static C++ variables are called immediately before main(...) function exit, if they are defined in main program, or if they are defined in some library and you are using it, then the destructors should be called immediately before dlclose(...).
On Android OS all destructors(defined in main program or in some library you are using) of static C++ variables are called during main(...) function exit. So what happens in our case? We have cls static C++ variable defined in library we are using. Then immediately before main(...) function exit we call dlclose(...) function, as a result library closed and cls becomes non valid. But the pointer of cls is stored somewhere and it's destructor should be called during main(...) function exit, and because at the time of call it is already invalid, we get segmentation fault. So the solution is to not call dlclose(...) and everything should be fine. Unfortunately with this solution we cannot use attribute((destructor)) for deinitializing of something we want to deinitialize, because it is called as a result of dlclose(...) call.
I have a general aversion to calling dlclose(). The problem is that you must ensure that nothing will try to execute code in the shared library after it has been unmapped, or you will get a segmentation fault.
The most common way to fail is to create an object whose destructor is defined in or calls code defined in the shared library. If the object still exists after dlclose(), your app will crash when the object is deleted.
If you look at logcat you should see a debuggerd stack trace. If you can decode that with the arm-eabi-addr2line tool you should be able to determine if it's in a destructor, and if so, for what class. Alternatively, take the crash address, strip off the high 12 bits, and use that as an offset into the library that was dlclose()d and try to figure out what code lives at that address.
I encountered the same headache on Linux. A work-around that fixes my segfault is to put these lines in the same file as main(), so that dlclose() is called after main returns:
static void* handle = 0;
void myDLClose(void) __attribute__ ((destructor));
void myDLClose(void)
{
dlclose(handle);
}
int main()
{
handle = dlopen(...);
/* ... real work ... */
return 0;
}
The root cause of dlclose-induced segfault may be that a particular implementation of dlclose() does not clean up the global variables inside the shared object.
You need to compile with -fpic as a compiler flag for the application that is using dlopen() and dlclose(). You should also try error handling via dlerror() and perhaps checking if the assignment of your function pointer is valid, even if it's not NULL the function pointer could be pointing to something invalid from the initialization, dlsym() is not guaranteed to return NULL on android if it cannot find a symbol. Refer to the android documentation opposed to the posix compliant stuff, not everything is posix compliant on android.
You should use extern "C" to declare you function aaa()

Categories

Resources