use of org.matrix.androidsdk.crypto.data.MXQueuedEncryption in project matrix-android-sdk by matrix-org.
the class MXMegolmEncryption method encryptEventContent.
@Override
public void encryptEventContent(final JsonElement eventContent, final String eventType, final List<String> userIds, final ApiCallback<JsonElement> callback) {
// Queue the encryption request
// It will be processed when everything is set up
MXQueuedEncryption queuedEncryption = new MXQueuedEncryption();
queuedEncryption.mEventContent = eventContent;
queuedEncryption.mEventType = eventType;
queuedEncryption.mApiCallback = callback;
synchronized (mPendingEncryptions) {
mPendingEncryptions.add(queuedEncryption);
}
final long t0 = System.currentTimeMillis();
Log.d(LOG_TAG, "## encryptEventContent () starts");
getDevicesInRoom(userIds, new ApiCallback<MXUsersDevicesMap<MXDeviceInfo>>() {
/**
* A network error has been received while encrypting
* @param e the exception
*/
private void dispatchNetworkError(Exception e) {
Log.e(LOG_TAG, "## encryptEventContent() : onNetworkError " + e.getMessage());
List<MXQueuedEncryption> queuedEncryptions = getPendingEncryptions();
for (MXQueuedEncryption queuedEncryption : queuedEncryptions) {
queuedEncryption.mApiCallback.onNetworkError(e);
}
synchronized (mPendingEncryptions) {
mPendingEncryptions.removeAll(queuedEncryptions);
}
}
/**
* A matrix error has been received while encrypting
* @param e the exception
*/
private void dispatchMatrixError(MatrixError e) {
Log.e(LOG_TAG, "## encryptEventContent() : onMatrixError " + e.getMessage());
List<MXQueuedEncryption> queuedEncryptions = getPendingEncryptions();
for (MXQueuedEncryption queuedEncryption : queuedEncryptions) {
queuedEncryption.mApiCallback.onMatrixError(e);
}
synchronized (mPendingEncryptions) {
mPendingEncryptions.removeAll(queuedEncryptions);
}
}
/**
* An unexpected error has been received while encrypting
* @param e the exception
*/
private void dispatchUnexpectedError(Exception e) {
Log.e(LOG_TAG, "## onUnexpectedError() : onMatrixError " + e.getMessage());
List<MXQueuedEncryption> queuedEncryptions = getPendingEncryptions();
for (MXQueuedEncryption queuedEncryption : queuedEncryptions) {
queuedEncryption.mApiCallback.onUnexpectedError(e);
}
synchronized (mPendingEncryptions) {
mPendingEncryptions.removeAll(queuedEncryptions);
}
}
@Override
public void onSuccess(MXUsersDevicesMap<MXDeviceInfo> devicesInRoom) {
ensureOutboundSession(devicesInRoom, new ApiCallback<MXOutboundSessionInfo>() {
@Override
public void onSuccess(final MXOutboundSessionInfo session) {
mCrypto.getEncryptingThreadHandler().post(new Runnable() {
@Override
public void run() {
Log.d(LOG_TAG, "## encryptEventContent () processPendingEncryptions after " + (System.currentTimeMillis() - t0) + "ms");
processPendingEncryptions(session);
}
});
}
@Override
public void onNetworkError(Exception e) {
dispatchNetworkError(e);
}
@Override
public void onMatrixError(MatrixError e) {
dispatchMatrixError(e);
}
@Override
public void onUnexpectedError(Exception e) {
dispatchUnexpectedError(e);
}
});
}
@Override
public void onNetworkError(Exception e) {
dispatchNetworkError(e);
}
@Override
public void onMatrixError(MatrixError e) {
dispatchMatrixError(e);
}
@Override
public void onUnexpectedError(Exception e) {
dispatchUnexpectedError(e);
}
});
}
use of org.matrix.androidsdk.crypto.data.MXQueuedEncryption in project matrix-android-sdk by matrix-org.
the class MXMegolmEncryption method processPendingEncryptions.
/**
* process the pending encryptions
*/
private void processPendingEncryptions(MXOutboundSessionInfo session) {
if (null != session) {
List<MXQueuedEncryption> queuedEncryptions = getPendingEncryptions();
// Everything is in place, encrypt all pending events
for (MXQueuedEncryption queuedEncryption : queuedEncryptions) {
HashMap<String, Object> payloadJson = new HashMap<>();
payloadJson.put("room_id", mRoomId);
payloadJson.put("type", queuedEncryption.mEventType);
payloadJson.put("content", queuedEncryption.mEventContent);
String payloadString = JsonUtils.convertToUTF8(JsonUtils.canonicalize(JsonUtils.getGson(false).toJsonTree(payloadJson)).toString());
String ciphertext = mCrypto.getOlmDevice().encryptGroupMessage(session.mSessionId, payloadString);
final HashMap<String, Object> map = new HashMap<>();
map.put("algorithm", MXCryptoAlgorithms.MXCRYPTO_ALGORITHM_MEGOLM);
map.put("sender_key", mCrypto.getOlmDevice().getDeviceCurve25519Key());
map.put("ciphertext", ciphertext);
map.put("session_id", session.mSessionId);
// Include our device ID so that recipients can send us a
// m.new_device message if they don't have our session key.
map.put("device_id", mDeviceId);
final MXQueuedEncryption fQueuedEncryption = queuedEncryption;
mCrypto.getUIHandler().post(new Runnable() {
@Override
public void run() {
fQueuedEncryption.mApiCallback.onSuccess(JsonUtils.getGson(false).toJsonTree(map));
}
});
session.mUseCount++;
}
synchronized (mPendingEncryptions) {
mPendingEncryptions.removeAll(queuedEncryptions);
}
}
}
Aggregations