From d741c012f303f7d430af013be456d5221f1d29c0 Mon Sep 17 00:00:00 2001 From: ylecollen Date: Wed, 21 Dec 2016 12:58:00 +0100 Subject: identityKeys and oneTimeKeys return Map instead of JSON. --- .../src/main/java/org/matrix/olm/OlmAccount.java | 80 +++++++++++++++++++--- .../OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp | 2 +- 2 files changed, 73 insertions(+), 9 deletions(-) (limited to 'java/android/OlmLibSdk/olm-sdk/src/main') diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmAccount.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmAccount.java index aeeaebc..4863a06 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmAccount.java +++ b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmAccount.java @@ -26,6 +26,9 @@ import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; /** * Account class used to create Olm sessions in conjunction with {@link OlmSession} class.
@@ -226,16 +229,16 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable { private native long createNewAccountJni(); /** - * Return the identity keys (identity and fingerprint keys) in a JSON array.
+ * Return the identity keys (identity and fingerprint keys) in a dictionary.
* Public API for {@link #identityKeysJni()}.
* Ex: * { * "curve25519":"Vam++zZPMqDQM6ANKpO/uAl5ViJSHxV9hd+b0/fwRAg", * "ed25519":"+v8SOlOASFTMrX3MCKBM4iVnYoZ+JIjpNt1fi8Z9O2I" * } - * @return identity keys in JSON array if operation succeed, null otherwise + * @return identity keys dictionary if operation succeeds, null otherwise */ - public JSONObject identityKeys() { + public Map identityKeys() { JSONObject identityKeysJsonObj = null; byte identityKeysBuffer[]; @@ -251,7 +254,7 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable { Log.e(LOG_TAG, "## identityKeys(): Failure - identityKeysJni()=null"); } - return identityKeysJsonObj; + return toStringMap(identityKeysJsonObj); } /** * Get the public identity keys (Ed25519 fingerprint key and Curve25519 identity key).
@@ -283,7 +286,7 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable { private native int generateOneTimeKeysJni(int aNumberOfKeys); /** - * Return the "one time keys" in a JSON array.
+ * Return the "one time keys" in a dictionary.
* The number of "one time keys", is specified by {@link #generateOneTimeKeys(int)}
* Ex: * { "curve25519": @@ -295,9 +298,9 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable { * }
* Public API for {@link #oneTimeKeysJni()}.
* Note: these keys are to be published on the server. - * @return one time keys in JSON array format if operation succeed, null otherwise + * @return one time keys in string dictionary if operation succeed, null otherwise */ - public JSONObject oneTimeKeys() { + public Map> oneTimeKeys() { byte identityKeysBuffer[]; JSONObject oneTimeKeysJsonObj = null; @@ -313,7 +316,7 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable { Log.e(LOG_TAG, "## oneTimeKeys(): Failure - identityKeysJni()=null"); } - return oneTimeKeysJsonObj; + return toStringMapMap(oneTimeKeysJsonObj); } /** * Get the public parts of the unpublished "one time keys" for the account.
@@ -373,4 +376,65 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable { public int getUnreleasedCount() { return mUnreleasedCount; } + + /** + * Build a string-string dictionary from a jsonObject.
+ * @param jsonObject the object to parse + * @return the map + */ + private static Map toStringMap(JSONObject jsonObject) { + if (null != jsonObject) { + HashMap map = new HashMap<>(); + Iterator keysItr = jsonObject.keys(); + while(keysItr.hasNext()) { + String key = keysItr.next(); + try { + Object value = jsonObject.get(key); + + if (value instanceof String) { + map.put(key, (String) value); + } else { + Log.e(LOG_TAG, "## toStringMap(): unexpected type " + value.getClass()); + } + } catch (Exception e) { + Log.e(LOG_TAG, "## toStringMap(): failed " + e.getMessage()); + } + } + + return map; + } + + return null; + } + + /** + * Build a string-string dictionary of string dictionary from a jsonObject.
+ * @param jsonObject the object to parse + * @return the map + */ + private static Map> toStringMapMap(JSONObject jsonObject) { + if (null != jsonObject) { + HashMap> map = new HashMap<>(); + + Iterator keysItr = jsonObject.keys(); + while(keysItr.hasNext()) { + String key = keysItr.next(); + try { + Object value = jsonObject.get(key); + + if (value instanceof JSONObject) { + map.put(key, toStringMap((JSONObject) value)); + } else { + Log.e(LOG_TAG, "## toStringMapMap(): unexpected type " + value.getClass()); + } + } catch (Exception e) { + Log.e(LOG_TAG, "## toStringMapMap(): failed " + e.getMessage()); + } + } + + return map; + } + + return null; + } } diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp index c8c6e8a..00c8a8e 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp @@ -468,7 +468,7 @@ JNIEXPORT jstring OLM_MANAGER_FUNC_DEF(getOlmLibVersionJni)(JNIEnv* env, jobject olm_get_library_version(&majorVer, &minorVer, &patchVer); LOGD("## getOlmLibVersionJni(): Major=%d Minor=%d Patch=%d", majorVer, minorVer, patchVer); - snprintf(buff, sizeof(buff), " V%d.%d.%d", majorVer, minorVer, patchVer); + snprintf(buff, sizeof(buff), "%d.%d.%d", majorVer, minorVer, patchVer); returnValueStr = env->NewStringUTF((const char*)buff); return returnValueStr; -- cgit v1.2.3