Skip to content
Snippets Groups Projects
Commit 2246a834 authored by Adrien Béraud's avatar Adrien Béraud
Browse files

swig/jni: convert text messages from/to UTF-8

Tuleap: #191
Change-Id: Ie121b78e82a4212b62dd9d9662e9a0c47db22163
parent 482cc5be
No related branches found
No related tags found
No related merge requests found
...@@ -74,7 +74,7 @@ public class CallManagerCallBack extends Callback { ...@@ -74,7 +74,7 @@ public class CallManagerCallBack extends Callback {
public void incomingMessage(String call_id, String from, StringMap messages) { public void incomingMessage(String call_id, String from, StringMap messages) {
String msg = null; String msg = null;
try { try {
msg = messages.get("text/plain"); msg = messages.getRaw("text/plain").toJavaString();
} catch (Exception e) { } catch (Exception e) {
if (messages.size() > 0) if (messages.size() > 0)
Log.w(TAG, "on_incoming_message: unsupported MIME type: " + messages.keys().get(0)); Log.w(TAG, "on_incoming_message: unsupported MIME type: " + messages.keys().get(0));
......
...@@ -70,7 +70,7 @@ public class ConfigurationManagerCallback extends ConfigurationCallback { ...@@ -70,7 +70,7 @@ public class ConfigurationManagerCallback extends ConfigurationCallback {
public void incomingAccountMessage(String accountID, String from, StringMap messages) { public void incomingAccountMessage(String accountID, String from, StringMap messages) {
String msg = null; String msg = null;
try { try {
msg = messages.get("text/plain"); msg = messages.getRaw("text/plain").toJavaString();
} catch (Exception e) { } catch (Exception e) {
if (messages.size() > 0) if (messages.size() > 0)
Log.w(TAG, "incomingAccountMessage: unsupported MIME type: " + messages.keys().get(0)); Log.w(TAG, "incomingAccountMessage: unsupported MIME type: " + messages.keys().get(0));
......
...@@ -882,7 +882,7 @@ public class DRingService extends Service { ...@@ -882,7 +882,7 @@ public class DRingService extends Service {
protected void doRun() throws SameThreadException, RemoteException { protected void doRun() throws SameThreadException, RemoteException {
Log.i(TAG, "DRingService.sendTextMessage() thread running..."); Log.i(TAG, "DRingService.sendTextMessage() thread running...");
StringMap messages = new StringMap(); StringMap messages = new StringMap();
messages.set("text/plain", msg); messages.setRaw("text/plain", Blob.fromString(msg));
Ringservice.sendTextMessage(callID, messages, "", false); Ringservice.sendTextMessage(callID, messages, "", false);
} }
}); });
...@@ -895,7 +895,7 @@ public class DRingService extends Service { ...@@ -895,7 +895,7 @@ public class DRingService extends Service {
protected void doRun() throws SameThreadException, RemoteException { protected void doRun() throws SameThreadException, RemoteException {
Log.i(TAG, "DRingService.sendAccountTextMessage() thread running... " + accountID + " " + to + " " + msg); Log.i(TAG, "DRingService.sendAccountTextMessage() thread running... " + accountID + " " + to + " " + msg);
StringMap msgs = new StringMap(); StringMap msgs = new StringMap();
msgs.set("text/plain", msg); msgs.setRaw("text/plain", Blob.fromString(msg));
Ringservice.sendAccountTextMessage(accountID, to, msgs); Ringservice.sendAccountTextMessage(accountID, to, msgs);
} }
}); });
......
...@@ -69,6 +69,13 @@ namespace std { ...@@ -69,6 +69,13 @@ namespace std {
out.put(s, get(s)); out.put(s, get(s));
return out; return out;
} }
public java.util.HashMap<String,String> toNativeFromUtf8() {
java.util.HashMap<String,String> out = new java.util.HashMap<>((int)size());
StringVect keys = keys();
for (String s : keys)
out.put(s, getRaw(s).toJavaString());
return out;
}
%} %}
%extend map<string, string> { %extend map<string, string> {
std::vector<std::string> keys() const { std::vector<std::string> keys() const {
...@@ -78,6 +85,13 @@ namespace std { ...@@ -78,6 +85,13 @@ namespace std {
k.push_back(i.first); k.push_back(i.first);
return k; return k;
} }
void setRaw(std::string key, const vector<uint8_t>& value) {
(*$self)[key] = std::string(value.data(), value.data()+value.size());
}
std::vector<uint8_t> getRaw(std::string key) {
auto& v = $self->at(key);
return {v.begin(), v.end()};
}
} }
%template(StringMap) map<string, string>; %template(StringMap) map<string, string>;
...@@ -111,6 +125,31 @@ namespace std { ...@@ -111,6 +125,31 @@ namespace std {
%template(IntegerMap) map<string,int>; %template(IntegerMap) map<string,int>;
%template(IntVect) vector<int32_t>; %template(IntVect) vector<int32_t>;
%template(UintVect) vector<uint32_t>; %template(UintVect) vector<uint32_t>;
%typemap(javacode) vector<uint8_t> %{
public static Blob fromString(String in) {
byte[] dat;
try {
dat = in.getBytes("UTF-8");
} catch (java.io.UnsupportedEncodingException e) {
dat = in.getBytes();
}
Blob n = new Blob(dat.length);
for (int i=0; i<dat.length; i++)
n.set(i, dat[i]);
return n;
}
public String toJavaString() {
byte[] dat = new byte[(int)size()];
for (int i=0; i<dat.length; i++)
dat[i] = (byte)get(i);
try {
return new String(dat, "utf-8");
} catch (java.io.UnsupportedEncodingException e) {
return "";
}
}
%}
%template(Blob) vector<uint8_t>; %template(Blob) vector<uint8_t>;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment