use of org.matrix.androidsdk.data.RoomState 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.data.RoomState in project matrix-android-sdk by matrix-org.
the class MXFileStore method loadRoomState.
/**
* Load a room state from the file system.
*
* @param roomId the room id.
* @return true if the operation succeeds.
*/
private boolean loadRoomState(final String roomId) {
boolean succeed = true;
Room room = getRoom(roomId);
// should always be true
if (null != room) {
RoomState liveState = null;
try {
// the room state is not zipped
File roomStateFile = new File(mGzStoreRoomsStateFolderFile, roomId);
// new format
if (roomStateFile.exists()) {
Object roomStateAsObject = readObject("loadRoomState " + roomId, roomStateFile);
if (null == roomStateAsObject) {
succeed = false;
} else {
liveState = (RoomState) roomStateAsObject;
}
}
} catch (Exception e) {
succeed = false;
Log.e(LOG_TAG, "loadRoomState failed : " + e.getMessage());
}
if (null != liveState) {
room.getLiveTimeLine().setState(liveState);
} else {
deleteRoom(roomId);
}
} else {
try {
File messagesListFile = new File(mGzStoreRoomsStateFolderFile, roomId);
messagesListFile.delete();
} catch (Exception e) {
Log.e(LOG_TAG, "loadRoomState failed to delete a file : " + e.getMessage());
}
}
return succeed;
}
use of org.matrix.androidsdk.data.RoomState in project matrix-android-sdk by matrix-org.
the class RoomsRestClient method updateJoinRules.
/**
* Update the join rule of the room.
* To make the room private, the aJoinRule must be set to {@link RoomState#JOIN_RULE_INVITE}.
*
* @param aRoomId the room id
* @param aJoinRule the join rule: {@link RoomState#JOIN_RULE_PUBLIC} or {@link RoomState#JOIN_RULE_INVITE}
* @param callback the async callback response
*/
public void updateJoinRules(final String aRoomId, final String aJoinRule, final ApiCallback<Void> callback) {
final String description = "updateJoinRules : roomId=" + aRoomId + " rule=" + aJoinRule;
// build RoomState as input parameter
RoomState roomStateParam = new RoomState();
roomStateParam.join_rule = aJoinRule;
try {
mApi.setJoinRules(aRoomId, roomStateParam, new RestAdapterCallback<Void>(description, mUnsentEventsManager, callback, new RestAdapterCallback.RequestRetryCallBack() {
@Override
public void onRetry() {
updateJoinRules(aRoomId, aJoinRule, callback);
}
}));
} catch (Throwable t) {
callback.onUnexpectedError(new Exception(t));
}
}
use of org.matrix.androidsdk.data.RoomState in project matrix-android-sdk by matrix-org.
the class RoomsRestClient method updateHistoryVisibility.
/**
* Update the room name.
*
* @param roomId the room id
* @param aVisibility the visibility
* @param callback the async callback
*/
public void updateHistoryVisibility(final String roomId, final String aVisibility, final ApiCallback<Void> callback) {
final String description = "updateHistoryVisibility : roomId " + roomId + " visibility " + aVisibility;
RoomState roomState = new RoomState();
roomState.history_visibility = aVisibility;
try {
mApi.setHistoryVisibility(roomId, roomState, new RestAdapterCallback<Void>(description, mUnsentEventsManager, callback, new RestAdapterCallback.RequestRetryCallBack() {
@Override
public void onRetry() {
updateHistoryVisibility(roomId, aVisibility, callback);
}
}));
} catch (Throwable t) {
callback.onUnexpectedError(new Exception(t));
}
}
use of org.matrix.androidsdk.data.RoomState in project matrix-android-sdk by matrix-org.
the class RoomsRestClient method updateDirectoryVisibility.
/**
* Update the directory visibility of the room.
*
* @param aRoomId the room id
* @param aDirectoryVisibility the visibility of the room in the directory list
* @param callback the async callback response
*/
public void updateDirectoryVisibility(final String aRoomId, final String aDirectoryVisibility, final ApiCallback<Void> callback) {
final String description = "updateRoomDirectoryVisibility : roomId=" + aRoomId + " visibility=" + aDirectoryVisibility;
RoomState roomState = new RoomState();
roomState.visibility = aDirectoryVisibility;
try {
mApi.setRoomDirectoryVisibility(aRoomId, roomState, new RestAdapterCallback<Void>(description, mUnsentEventsManager, callback, new RestAdapterCallback.RequestRetryCallBack() {
@Override
public void onRetry() {
updateDirectoryVisibility(aRoomId, aDirectoryVisibility, callback);
}
}));
} catch (Throwable t) {
callback.onUnexpectedError(new Exception(t));
}
}
Aggregations