Search in sources :

Example 26 with RoomState

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());
}
Also used : HashMap(java.util.HashMap) CountDownLatch(java.util.concurrent.CountDownLatch) MXEventListener(org.matrix.androidsdk.listeners.MXEventListener) Event(org.matrix.androidsdk.rest.model.Event) JsonObject(com.google.gson.JsonObject) MatrixError(org.matrix.androidsdk.rest.model.MatrixError) Room(org.matrix.androidsdk.data.Room) RoomState(org.matrix.androidsdk.data.RoomState) Test(org.junit.Test)

Example 27 with RoomState

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;
}
Also used : Room(org.matrix.androidsdk.data.Room) File(java.io.File) RoomState(org.matrix.androidsdk.data.RoomState)

Example 28 with RoomState

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));
    }
}
Also used : RoomState(org.matrix.androidsdk.data.RoomState)

Example 29 with RoomState

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));
    }
}
Also used : RoomState(org.matrix.androidsdk.data.RoomState)

Example 30 with RoomState

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));
    }
}
Also used : RoomState(org.matrix.androidsdk.data.RoomState)

Aggregations

RoomState (org.matrix.androidsdk.data.RoomState)30 Room (org.matrix.androidsdk.data.Room)22 Event (org.matrix.androidsdk.rest.model.Event)21 JsonObject (com.google.gson.JsonObject)20 HashMap (java.util.HashMap)20 CountDownLatch (java.util.concurrent.CountDownLatch)20 MatrixError (org.matrix.androidsdk.rest.model.MatrixError)20 Test (org.junit.Test)18 MXEventListener (org.matrix.androidsdk.listeners.MXEventListener)16 ArrayList (java.util.ArrayList)13 Context (android.content.Context)12 EventTimeline (org.matrix.androidsdk.data.EventTimeline)9 Credentials (org.matrix.androidsdk.rest.model.login.Credentials)4 MXDeviceInfo (org.matrix.androidsdk.crypto.data.MXDeviceInfo)3 Uri (android.net.Uri)2 MXCryptoError (org.matrix.androidsdk.crypto.MXCryptoError)2 MXUsersDevicesMap (org.matrix.androidsdk.crypto.data.MXUsersDevicesMap)2 IMXStore (org.matrix.androidsdk.data.store.IMXStore)2 MXFileStore (org.matrix.androidsdk.data.store.MXFileStore)2 ApiCallback (org.matrix.androidsdk.rest.callback.ApiCallback)2