Search in sources :

Example 1 with EventTimeline

use of org.matrix.androidsdk.data.EventTimeline in project matrix-android-sdk by matrix-org.

the class CryptoTest method test12_testAliceAndBobInACryptedRoomBackPaginationFromHomeServer.

@Test
public void test12_testAliceAndBobInACryptedRoomBackPaginationFromHomeServer() throws Exception {
    Log.e(LOG_TAG, "test12_testAliceAndBobInACryptedRoomBackPaginationFromHomeServer");
    Context context = InstrumentationRegistry.getContext();
    final HashMap<String, Object> results = new HashMap();
    doE2ETestWithAliceAndBobInARoomWithCryptedMessages(true);
    String eventId = mBobSession.getDataHandler().getStore().getLatestEvent(mRoomId).eventId;
    EventTimeline timeline = new EventTimeline(mBobSession.getDataHandler(), mRoomId, eventId);
    final CountDownLatch lock2 = new CountDownLatch(6);
    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);
                lock2.countDown();
            }
        }
    };
    timeline.addEventTimelineListener(eventTimelineListener);
    timeline.backPaginate(new ApiCallback<Integer>() {

        @Override
        public void onSuccess(Integer info) {
            results.put("backPaginate", "backPaginate");
            lock2.countDown();
        }

        @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("backPaginate"));
    assertTrue(5 == receivedEvents.size());
    checkEncryptedEvent(receivedEvents.get(0), mRoomId, messagesFromAlice.get(1), mAliceSession);
    checkEncryptedEvent(receivedEvents.get(1), mRoomId, messagesFromBob.get(2), mBobSession);
    checkEncryptedEvent(receivedEvents.get(2), mRoomId, messagesFromBob.get(1), mBobSession);
    checkEncryptedEvent(receivedEvents.get(3), mRoomId, messagesFromBob.get(0), mBobSession);
    checkEncryptedEvent(receivedEvents.get(4), mRoomId, messagesFromAlice.get(0), mAliceSession);
    mBobSession.clear(context);
    mAliceSession.clear(context);
}
Also used : Context(android.content.Context) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) EventTimeline(org.matrix.androidsdk.data.EventTimeline) CountDownLatch(java.util.concurrent.CountDownLatch) Event(org.matrix.androidsdk.rest.model.Event) JsonObject(com.google.gson.JsonObject) MatrixError(org.matrix.androidsdk.rest.model.MatrixError) RoomState(org.matrix.androidsdk.data.RoomState) Test(org.junit.Test)

Example 2 with EventTimeline

use of org.matrix.androidsdk.data.EventTimeline in project matrix-android-sdk by matrix-org.

the class MatrixMessageListFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Log.d(LOG_TAG, "onCreateView");
    View defaultView = super.onCreateView(inflater, container, savedInstanceState);
    Bundle args = getArguments();
    // for dispatching data to add to the adapter we need to be on the main thread
    mUiHandler = new Handler(Looper.getMainLooper());
    mMatrixId = args.getString(ARG_MATRIX_ID);
    mSession = getSession(mMatrixId);
    if (null == mSession) {
        if (null != getActivity()) {
            Log.e(LOG_TAG, "Must have valid default MXSession.");
            getActivity().finish();
            return defaultView;
        }
        throw new RuntimeException("Must have valid default MXSession.");
    }
    if (null == getMXMediasCache()) {
        if (null != getActivity()) {
            Log.e(LOG_TAG, "Must have valid default MediasCache.");
            getActivity().finish();
            return defaultView;
        }
        throw new RuntimeException("Must have valid default MediasCache.");
    }
    String roomId = args.getString(ARG_ROOM_ID);
    View v = inflater.inflate(args.getInt(ARG_LAYOUT_ID), container, false);
    mMessageListView = v.findViewById(R.id.listView_messages);
    mIsScrollListenerSet = false;
    if (mAdapter == null) {
        // only init the adapter if it wasn't before, so we can preserve messages/position.
        mAdapter = createMessagesAdapter();
        if (null == getMXMediasCache()) {
            throw new RuntimeException("Must have valid default MessagesAdapter.");
        }
    } else if (null != savedInstanceState) {
        mFirstVisibleRow = savedInstanceState.getInt("FIRST_VISIBLE_ROW", -1);
    }
    mAdapter.setIsPreviewMode(false);
    if (null == mEventTimeLine) {
        mEventId = args.getString(ARG_EVENT_ID);
        final String previewMode = args.getString(ARG_PREVIEW_MODE_ID);
        // the fragment displays the history around a message
        if (!TextUtils.isEmpty(mEventId)) {
            mEventTimeLine = new EventTimeline(mSession.getDataHandler(), roomId, mEventId);
            mRoom = mEventTimeLine.getRoom();
            if (PREVIEW_MODE_UNREAD_MESSAGE.equals(previewMode)) {
                mAdapter.setIsUnreadViewMode(true);
            }
        } else // display a room preview
        if (PREVIEW_MODE_READ_ONLY.equals(previewMode)) {
            mAdapter.setIsPreviewMode(true);
            mEventTimeLine = new EventTimeline(mSession.getDataHandler(), roomId);
            mRoom = mEventTimeLine.getRoom();
        } else // standard case
        {
            if (!TextUtils.isEmpty(roomId)) {
                mRoom = mSession.getDataHandler().getRoom(roomId);
                mEventTimeLine = mRoom.getLiveTimeLine();
            }
        }
    }
    // GA reported some weird room content
    // so ensure that the room fields are properly initialized
    mSession.getDataHandler().checkRoom(mRoom);
    mMessageListView.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            onListTouch(event);
            return false;
        }
    });
    mDisplayAllEvents = isDisplayAllEvents();
    return v;
}
Also used : Bundle(android.os.Bundle) Handler(android.os.Handler) EventTimeline(org.matrix.androidsdk.data.EventTimeline) View(android.view.View) AbsListView(android.widget.AbsListView) AutoScrollDownListView(org.matrix.androidsdk.view.AutoScrollDownListView) MotionEvent(android.view.MotionEvent)

Aggregations

EventTimeline (org.matrix.androidsdk.data.EventTimeline)2 Context (android.content.Context)1 Bundle (android.os.Bundle)1 Handler (android.os.Handler)1 MotionEvent (android.view.MotionEvent)1 View (android.view.View)1 AbsListView (android.widget.AbsListView)1 JsonObject (com.google.gson.JsonObject)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.Test)1 RoomState (org.matrix.androidsdk.data.RoomState)1 Event (org.matrix.androidsdk.rest.model.Event)1 MatrixError (org.matrix.androidsdk.rest.model.MatrixError)1 AutoScrollDownListView (org.matrix.androidsdk.view.AutoScrollDownListView)1