use of org.matrix.androidsdk.listeners.MXEventListener in project matrix-android-sdk by matrix-org.
the class CryptoTest method test02_testCryptoPersistenceInStore.
@Test
public void test02_testCryptoPersistenceInStore() throws Exception {
Log.e(LOG_TAG, "test02_testCryptoPersistenceInStore");
Context context = InstrumentationRegistry.getContext();
final HashMap<String, Object> results = new HashMap<>();
createBobAccount();
mBobSession.getCredentials().deviceId = "BobDevice";
assertTrue(null == mBobSession.getCrypto());
final CountDownLatch lock0 = new CountDownLatch(1);
mBobSession.enableCrypto(true, new ApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
results.put("enableCrypto", "enableCrypto");
lock0.countDown();
}
@Override
public void onNetworkError(Exception e) {
lock0.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
lock0.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
lock0.countDown();
}
});
lock0.await(1000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("enableCrypto"));
assertTrue(null != mBobSession.getCrypto());
SystemClock.sleep(1000);
final String deviceCurve25519Key = mBobSession.getCrypto().getOlmDevice().getDeviceCurve25519Key();
final String deviceEd25519Key = mBobSession.getCrypto().getOlmDevice().getDeviceEd25519Key();
final List<MXDeviceInfo> myUserDevices = mBobSession.getCrypto().getUserDevices(mBobSession.getMyUserId());
assertTrue(null != myUserDevices);
assertTrue(1 == myUserDevices.size());
final Credentials bobCredentials = mBobSession.getCredentials();
Uri uri = Uri.parse(CryptoTestHelper.TESTS_HOME_SERVER_URL);
HomeServerConnectionConfig hs = new HomeServerConnectionConfig(uri);
hs.setCredentials(bobCredentials);
IMXStore store = new MXFileStore(hs, context);
MXSession bobSession2 = new MXSession(hs, new MXDataHandler(store, bobCredentials), context);
final CountDownLatch lock1 = new CountDownLatch(1);
MXStoreListener listener = new MXStoreListener() {
@Override
public void postProcess(String accountId) {
}
@Override
public void onStoreReady(String accountId) {
results.put("onStoreReady", "onStoreReady");
lock1.countDown();
}
@Override
public void onStoreCorrupted(String accountId, String description) {
lock1.countDown();
}
@Override
public void onStoreOOM(String accountId, String description) {
lock1.countDown();
}
};
bobSession2.getDataHandler().getStore().addMXStoreListener(listener);
bobSession2.getDataHandler().getStore().open();
lock1.await(1000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("onStoreReady"));
assertTrue(bobSession2.isCryptoEnabled());
final CountDownLatch lock2 = new CountDownLatch(2);
MXEventListener eventsListener = new MXEventListener() {
@Override
public void onInitialSyncComplete(String toToken) {
results.put("onInitialSyncComplete", "onInitialSyncComplete");
lock2.countDown();
}
@Override
public void onCryptoSyncComplete() {
results.put("onCryptoSyncComplete", "onCryptoSyncComplete");
lock2.countDown();
}
};
bobSession2.getDataHandler().addListener(eventsListener);
bobSession2.startEventStream(null);
lock2.await(1000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("onInitialSyncComplete"));
assertTrue(results.containsKey("onCryptoSyncComplete"));
MXCrypto crypto = bobSession2.getCrypto();
assertNotNull(crypto);
assertTrue(TextUtils.equals(deviceCurve25519Key, crypto.getOlmDevice().getDeviceCurve25519Key()));
assertTrue(TextUtils.equals(deviceEd25519Key, crypto.getOlmDevice().getDeviceEd25519Key()));
List<MXDeviceInfo> myUserDevices2 = bobSession2.getCrypto().getUserDevices(bobSession2.getMyUserId());
assertTrue(1 == myUserDevices2.size());
assertTrue(TextUtils.equals(myUserDevices2.get(0).deviceId, myUserDevices.get(0).deviceId));
mBobSession.clear(context);
bobSession2.clear(context);
}
use of org.matrix.androidsdk.listeners.MXEventListener in project matrix-android-sdk by matrix-org.
the class CryptoTest method test16_testRoomKeyReshare.
@Test
public void test16_testRoomKeyReshare() throws Exception {
Log.e(LOG_TAG, "test16_testRoomKeyReshare");
final HashMap<String, Object> results = new HashMap<>();
doE2ETestWithAliceAndBobInARoom(true);
mBobSession.getCrypto().setWarnOnUnknownDevices(false);
mAliceSession.getCrypto().setWarnOnUnknownDevices(false);
String messageFromAlice = "Hello I'm Alice!";
final Room roomFromBobPOV = mBobSession.getDataHandler().getRoom(mRoomId);
final Room roomFromAlicePOV = mAliceSession.getDataHandler().getRoom(mRoomId);
assertTrue(roomFromBobPOV.isEncrypted());
assertTrue(roomFromAlicePOV.isEncrypted());
final CountDownLatch lock1 = new CountDownLatch(2);
MXEventListener bobEventListener = new MXEventListener() {
@Override
public void onToDeviceEvent(Event event) {
if (!results.containsKey("onToDeviceEvent")) {
results.put("onToDeviceEvent", event);
lock1.countDown();
}
}
};
mBobSession.getDataHandler().addListener(bobEventListener);
final ArrayList<Event> receivedEvents = new ArrayList<>();
EventTimeline.EventTimelineListener eventTimelineListener = new EventTimeline.EventTimelineListener() {
public void onEvent(Event event, EventTimeline.Direction direction, RoomState roomState) {
if (TextUtils.equals(event.getType(), Event.EVENT_TYPE_MESSAGE)) {
receivedEvents.add(event);
lock1.countDown();
}
}
};
roomFromBobPOV.getLiveTimeLine().addEventTimelineListener(eventTimelineListener);
roomFromAlicePOV.sendEvent(buildTextEvent(messageFromAlice, mAliceSession), new ApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
}
@Override
public void onNetworkError(Exception e) {
}
@Override
public void onMatrixError(MatrixError e) {
}
@Override
public void onUnexpectedError(Exception e) {
}
});
lock1.await(2000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("onToDeviceEvent"));
assertTrue(1 == receivedEvents.size());
Event event = receivedEvents.get(0);
assertTrue(checkEncryptedEvent(event, mRoomId, messageFromAlice, mAliceSession));
// Reinject a modified version of the received room_key event from Alice.
// From Bob pov, that mimics Alice resharing her keys but with an advanced outbound group session.
Event toDeviceEvent = (Event) results.get("onToDeviceEvent");
String sessionId = toDeviceEvent.getContentAsJsonObject().get("session_id").getAsString();
String newSessionKey = mAliceSession.getCrypto().getOlmDevice().getSessionKey(sessionId);
JsonObject content = toDeviceEvent.getClearEvent().getContentAsJsonObject();
content.add("session_key", new JsonPrimitive(newSessionKey));
mBobSession.getDataHandler().onToDeviceEvent(toDeviceEvent);
// We still must be able to decrypt the event
// ie, the implementation must have ignored the new room key with the advanced outbound group
// session key
event.setClearData(null);
mBobSession.getDataHandler().decryptEvent(event, null);
assertTrue(checkEncryptedEvent(event, mRoomId, messageFromAlice, mAliceSession));
}
use of org.matrix.androidsdk.listeners.MXEventListener in project matrix-android-sdk by matrix-org.
the class CryptoTest method test13_testAliceAndNotCryptedBobInACryptedRoom.
@Test
public void test13_testAliceAndNotCryptedBobInACryptedRoom() throws Exception {
Log.e(LOG_TAG, "test13_testAliceAndNotCryptedBobInACryptedRoom");
final HashMap<String, Object> results = new HashMap();
doE2ETestWithAliceAndBobInARoom(false);
mAliceSession.getCrypto().setWarnOnUnknownDevices(false);
Room roomFromBobPOV = mBobSession.getDataHandler().getRoom(mRoomId);
Room roomFromAlicePOV = mAliceSession.getDataHandler().getRoom(mRoomId);
assertTrue(roomFromBobPOV.isEncrypted());
assertTrue(roomFromAlicePOV.isEncrypted());
final String messageFromAlice = "Hello I'm Alice!";
final CountDownLatch lock1 = new CountDownLatch(1);
MXEventListener bobEventListener = new MXEventListener() {
@Override
public void onLiveEvent(Event event, RoomState roomState) {
if (TextUtils.equals(event.getType(), Event.EVENT_TYPE_MESSAGE_ENCRYPTED) && !TextUtils.equals(event.getSender(), mBobSession.getMyUserId())) {
results.put("bobEcho", event);
lock1.countDown();
}
}
};
roomFromBobPOV.addEventListener(bobEventListener);
roomFromAlicePOV.sendEvent(buildTextEvent(messageFromAlice, mAliceSession), new ApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
}
@Override
public void onNetworkError(Exception e) {
}
@Override
public void onMatrixError(MatrixError e) {
}
@Override
public void onUnexpectedError(Exception e) {
}
});
lock1.await(1000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("bobEcho"));
Event event = (Event) results.get("bobEcho");
assertTrue(event.isEncrypted());
assertTrue(TextUtils.equals(event.getType(), Event.EVENT_TYPE_MESSAGE_ENCRYPTED));
assertTrue(null != event.getContentAsJsonObject());
assertTrue(!event.getContentAsJsonObject().has("body"));
assertTrue(null != event.getCryptoError());
assertTrue(TextUtils.equals(event.getCryptoError().errcode, MXCryptoError.ENCRYPTING_NOT_ENABLED_ERROR_CODE));
final CountDownLatch lock2 = new CountDownLatch(1);
MXEventListener aliceEventListener = new MXEventListener() {
@Override
public void onLiveEvent(Event event, RoomState roomState) {
if (TextUtils.equals(event.getType(), Event.EVENT_TYPE_MESSAGE) && !TextUtils.equals(event.getSender(), mAliceSession.getMyUserId())) {
results.put("aliceEcho", event);
lock2.countDown();
}
}
};
roomFromAlicePOV.addEventListener(aliceEventListener);
roomFromBobPOV.sendEvent(buildTextEvent("Hello I'm Bob!", mBobSession), new ApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
}
@Override
public void onNetworkError(Exception e) {
}
@Override
public void onMatrixError(MatrixError e) {
}
@Override
public void onUnexpectedError(Exception e) {
}
});
lock2.await(1000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("aliceEcho"));
event = (Event) results.get("aliceEcho");
assertTrue(!event.isEncrypted());
}
use of org.matrix.androidsdk.listeners.MXEventListener in project matrix-android-sdk by matrix-org.
the class CryptoTestHelper method logAccountAndSync.
/**
* Start an account login
* @param context the context
* @param userName the account username
* @param password the password
* @throws Exception an exception if the account cannot be synced
*/
public static MXSession logAccountAndSync(Context context, String userName, String password) throws Exception {
Uri uri = Uri.parse(TESTS_HOME_SERVER_URL);
HomeServerConnectionConfig hs = new HomeServerConnectionConfig(uri);
LoginRestClient loginRestClient = new LoginRestClient(hs);
final HashMap<String, Object> params = new HashMap<>();
mLock = new CountDownLatch(1);
// get the registration session id
loginRestClient.loginWithUser(userName, password, new ApiCallback<Credentials>() {
@Override
public void onSuccess(Credentials credentials) {
params.put("credentials", credentials);
mLock.countDown();
}
@Override
public void onNetworkError(Exception e) {
mLock.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
mLock.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
mLock.countDown();
}
});
mLock.await(10000, TimeUnit.MILLISECONDS);
Credentials credentials = (Credentials) params.get("credentials");
assertTrue(null != credentials);
hs.setCredentials(credentials);
IMXStore store = new MXFileStore(hs, context);
MXSession mxSession = new MXSession(hs, new MXDataHandler(store, credentials), context);
mxSession.enableCryptoWhenStarting();
mLock = new CountDownLatch(2);
mxSession.getDataHandler().addListener(new MXEventListener() {
@Override
public void onInitialSyncComplete(String toToken) {
params.put("isInit", true);
mLock.countDown();
}
@Override
public void onCryptoSyncComplete() {
params.put("onCryptoSyncComplete", true);
mLock.countDown();
}
});
mxSession.getDataHandler().getStore().open();
mxSession.startEventStream(null);
mLock.await(10000, TimeUnit.MILLISECONDS);
assertTrue(params.containsKey("isInit"));
assertTrue(params.containsKey("onCryptoSyncComplete"));
return mxSession;
}
use of org.matrix.androidsdk.listeners.MXEventListener in project matrix-android-sdk by matrix-org.
the class Room method addEventListener.
// ==============================================================================================================
// Room events dispatcher
// ==============================================================================================================
/**
* Add an event listener to this room. Only events relative to the room will come down.
*
* @param eventListener the event listener to add
*/
public void addEventListener(final IMXEventListener eventListener) {
// sanity check
if (null == eventListener) {
Log.e(LOG_TAG, "addEventListener : eventListener is null");
return;
}
// GA crash : should never happen but got it.
if (null == mDataHandler) {
Log.e(LOG_TAG, "addEventListener : mDataHandler is null");
return;
}
// Create a global listener that we'll add to the data handler
IMXEventListener globalListener = new MXEventListener() {
@Override
public void onPresenceUpdate(Event event, User user) {
// Only pass event through if the user is a member of the room
if (getMember(user.user_id) != null) {
try {
eventListener.onPresenceUpdate(event, user);
} catch (Exception e) {
Log.e(LOG_TAG, "onPresenceUpdate exception " + e.getMessage());
}
}
}
@Override
public void onLiveEvent(Event event, RoomState roomState) {
// Filter out events for other rooms and events while we are joining (before the room is ready)
if (TextUtils.equals(getRoomId(), event.roomId) && mIsReady) {
try {
eventListener.onLiveEvent(event, roomState);
} catch (Exception e) {
Log.e(LOG_TAG, "onLiveEvent exception " + e.getMessage());
}
}
}
@Override
public void onLiveEventsChunkProcessed(String fromToken, String toToken) {
try {
eventListener.onLiveEventsChunkProcessed(fromToken, toToken);
} catch (Exception e) {
Log.e(LOG_TAG, "onLiveEventsChunkProcessed exception " + e.getMessage());
}
}
@Override
public void onEventSentStateUpdated(Event event) {
// Filter out events for other rooms
if (TextUtils.equals(getRoomId(), event.roomId)) {
try {
eventListener.onEventSentStateUpdated(event);
} catch (Exception e) {
Log.e(LOG_TAG, "onEventSentStateUpdated exception " + e.getMessage());
}
}
}
@Override
public void onEventDecrypted(Event event) {
// Filter out events for other rooms
if (TextUtils.equals(getRoomId(), event.roomId)) {
try {
eventListener.onEventDecrypted(event);
} catch (Exception e) {
Log.e(LOG_TAG, "onDecryptedEvent exception " + e.getMessage());
}
}
}
@Override
public void onEventSent(final Event event, final String prevEventId) {
// Filter out events for other rooms
if (TextUtils.equals(getRoomId(), event.roomId)) {
try {
eventListener.onEventSent(event, prevEventId);
} catch (Exception e) {
Log.e(LOG_TAG, "onEventSent exception " + e.getMessage());
}
}
}
@Override
public void onRoomInitialSyncComplete(String roomId) {
// Filter out events for other rooms
if (TextUtils.equals(getRoomId(), roomId)) {
try {
eventListener.onRoomInitialSyncComplete(roomId);
} catch (Exception e) {
Log.e(LOG_TAG, "onRoomInitialSyncComplete exception " + e.getMessage());
}
}
}
@Override
public void onRoomInternalUpdate(String roomId) {
// Filter out events for other rooms
if (TextUtils.equals(getRoomId(), roomId)) {
try {
eventListener.onRoomInternalUpdate(roomId);
} catch (Exception e) {
Log.e(LOG_TAG, "onRoomInternalUpdate exception " + e.getMessage());
}
}
}
@Override
public void onNotificationCountUpdate(String roomId) {
// Filter out events for other rooms
if (TextUtils.equals(getRoomId(), roomId)) {
try {
eventListener.onNotificationCountUpdate(roomId);
} catch (Exception e) {
Log.e(LOG_TAG, "onNotificationCountUpdate exception " + e.getMessage());
}
}
}
@Override
public void onNewRoom(String roomId) {
// Filter out events for other rooms
if (TextUtils.equals(getRoomId(), roomId)) {
try {
eventListener.onNewRoom(roomId);
} catch (Exception e) {
Log.e(LOG_TAG, "onNewRoom exception " + e.getMessage());
}
}
}
@Override
public void onJoinRoom(String roomId) {
// Filter out events for other rooms
if (TextUtils.equals(getRoomId(), roomId)) {
try {
eventListener.onJoinRoom(roomId);
} catch (Exception e) {
Log.e(LOG_TAG, "onJoinRoom exception " + e.getMessage());
}
}
}
@Override
public void onReceiptEvent(String roomId, List<String> senderIds) {
// Filter out events for other rooms
if (TextUtils.equals(getRoomId(), roomId)) {
try {
eventListener.onReceiptEvent(roomId, senderIds);
} catch (Exception e) {
Log.e(LOG_TAG, "onReceiptEvent exception " + e.getMessage());
}
}
}
@Override
public void onRoomTagEvent(String roomId) {
// Filter out events for other rooms
if (TextUtils.equals(getRoomId(), roomId)) {
try {
eventListener.onRoomTagEvent(roomId);
} catch (Exception e) {
Log.e(LOG_TAG, "onRoomTagEvent exception " + e.getMessage());
}
}
}
@Override
public void onReadMarkerEvent(String roomId) {
// Filter out events for other rooms
if (TextUtils.equals(getRoomId(), roomId)) {
try {
eventListener.onReadMarkerEvent(roomId);
} catch (Exception e) {
Log.e(LOG_TAG, "onReadMarkerEvent exception " + e.getMessage());
}
}
}
@Override
public void onRoomFlush(String roomId) {
// Filter out events for other rooms
if (TextUtils.equals(getRoomId(), roomId)) {
try {
eventListener.onRoomFlush(roomId);
} catch (Exception e) {
Log.e(LOG_TAG, "onRoomFlush exception " + e.getMessage());
}
}
}
@Override
public void onLeaveRoom(String roomId) {
// Filter out events for other rooms
if (TextUtils.equals(getRoomId(), roomId)) {
try {
eventListener.onLeaveRoom(roomId);
} catch (Exception e) {
Log.e(LOG_TAG, "onLeaveRoom exception " + e.getMessage());
}
}
}
@Override
public void onRoomKick(String roomId) {
// Filter out events for other rooms
if (TextUtils.equals(getRoomId(), roomId)) {
try {
eventListener.onRoomKick(roomId);
} catch (Exception e) {
Log.e(LOG_TAG, "onRoomKick exception " + e.getMessage());
}
}
}
};
mEventListeners.put(eventListener, globalListener);
// GA crash
if (null != mDataHandler) {
mDataHandler.addListener(globalListener);
}
}
Aggregations