Search in sources :

Example 1 with MXQueuedEncryption

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);
        }
    });
}
Also used : ApiCallback(org.matrix.androidsdk.rest.callback.ApiCallback) MXDeviceInfo(org.matrix.androidsdk.crypto.data.MXDeviceInfo) MXQueuedEncryption(org.matrix.androidsdk.crypto.data.MXQueuedEncryption) MXUsersDevicesMap(org.matrix.androidsdk.crypto.data.MXUsersDevicesMap) ArrayList(java.util.ArrayList) List(java.util.List) MatrixError(org.matrix.androidsdk.rest.model.MatrixError)

Example 2 with MXQueuedEncryption

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);
        }
    }
}
Also used : HashMap(java.util.HashMap) MXQueuedEncryption(org.matrix.androidsdk.crypto.data.MXQueuedEncryption)

Aggregations

MXQueuedEncryption (org.matrix.androidsdk.crypto.data.MXQueuedEncryption)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 MXDeviceInfo (org.matrix.androidsdk.crypto.data.MXDeviceInfo)1 MXUsersDevicesMap (org.matrix.androidsdk.crypto.data.MXUsersDevicesMap)1 ApiCallback (org.matrix.androidsdk.rest.callback.ApiCallback)1 MatrixError (org.matrix.androidsdk.rest.model.MatrixError)1