use of org.matrix.androidsdk.rest.callback.ApiCallback in project matrix-android-sdk by matrix-org.
the class CryptoTest method doE2ETestWithAliceAndBobInARoomWithCryptedMessages.
private void doE2ETestWithAliceAndBobInARoomWithCryptedMessages(boolean cryptedBob) throws Exception {
doE2ETestWithAliceAndBobInARoom(cryptedBob);
if (null != mBobSession.getCrypto()) {
mBobSession.getCrypto().setWarnOnUnknownDevices(false);
}
if (null != mAliceSession.getCrypto()) {
mAliceSession.getCrypto().setWarnOnUnknownDevices(false);
}
final Room roomFromBobPOV = mBobSession.getDataHandler().getRoom(mRoomId);
final Room roomFromAlicePOV = mAliceSession.getDataHandler().getRoom(mRoomId);
mMessagesCount = 0;
final ArrayList<CountDownLatch> list = new ArrayList<>();
MXEventListener bobEventsListener = new MXEventListener() {
@Override
public void onLiveEvent(Event event, RoomState roomState) {
if (TextUtils.equals(event.getType(), Event.EVENT_TYPE_MESSAGE) && !TextUtils.equals(event.getSender(), mBobSession.getMyUserId())) {
mMessagesCount++;
list.get(0).countDown();
}
}
};
roomFromBobPOV.addEventListener(bobEventsListener);
ApiCallback<Void> callback = new ApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
list.get(0).countDown();
}
@Override
public void onNetworkError(Exception e) {
}
@Override
public void onMatrixError(MatrixError e) {
}
@Override
public void onUnexpectedError(Exception e) {
}
};
final HashMap<String, Object> results = new HashMap<>();
CountDownLatch lock = new CountDownLatch(3);
list.clear();
list.add(lock);
mBobSession.getDataHandler().addListener(new MXEventListener() {
@Override
public void onToDeviceEvent(Event event) {
results.put("onToDeviceEvent", event);
list.get(0).countDown();
}
});
roomFromAlicePOV.sendEvent(buildTextEvent(messagesFromAlice.get(0), mAliceSession), callback);
lock.await(1000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("onToDeviceEvent"));
assertTrue(mMessagesCount == 1);
lock = new CountDownLatch(1);
list.clear();
list.add(lock);
roomFromBobPOV.sendEvent(buildTextEvent(messagesFromBob.get(0), mBobSession), callback);
// android does not echo the messages sent from itself
mMessagesCount++;
lock.await(1000, TimeUnit.MILLISECONDS);
assertTrue(mMessagesCount == 2);
lock = new CountDownLatch(1);
list.clear();
list.add(lock);
roomFromBobPOV.sendEvent(buildTextEvent(messagesFromBob.get(1), mBobSession), callback);
// android does not echo the messages sent from itself
mMessagesCount++;
lock.await(1000, TimeUnit.MILLISECONDS);
assertTrue(mMessagesCount == 3);
lock = new CountDownLatch(1);
list.clear();
list.add(lock);
roomFromBobPOV.sendEvent(buildTextEvent(messagesFromBob.get(2), mBobSession), callback);
// android does not echo the messages sent from itself
mMessagesCount++;
lock.await(1000, TimeUnit.MILLISECONDS);
assertTrue(mMessagesCount == 4);
lock = new CountDownLatch(2);
list.clear();
list.add(lock);
roomFromAlicePOV.sendEvent(buildTextEvent(messagesFromAlice.get(1), mAliceSession), callback);
lock.await(1000, TimeUnit.MILLISECONDS);
assertTrue(mMessagesCount == 5);
}
use of org.matrix.androidsdk.rest.callback.ApiCallback in project matrix-android-sdk by matrix-org.
the class MXSession method markRoomsAsRead.
/**
* Send the read receipts to the latest room messages.
*
* @param roomsIterator the rooms list iterator
* @param callback the asynchronous callback
*/
private void markRoomsAsRead(final Iterator roomsIterator, final ApiCallback<Void> callback) {
if (roomsIterator.hasNext()) {
Room room = (Room) roomsIterator.next();
boolean isRequestSent = false;
if (mNetworkConnectivityReceiver.isConnected()) {
isRequestSent = room.markAllAsRead(new ApiCallback<Void>() {
@Override
public void onSuccess(Void anything) {
markRoomsAsRead(roomsIterator, callback);
}
@Override
public void onNetworkError(Exception e) {
if (null != callback) {
callback.onNetworkError(e);
}
}
@Override
public void onMatrixError(MatrixError e) {
if (null != callback) {
callback.onMatrixError(e);
}
}
@Override
public void onUnexpectedError(Exception e) {
if (null != callback) {
callback.onUnexpectedError(e);
}
}
});
} else {
// update the local data
room.sendReadReceipt();
}
if (!isRequestSent) {
markRoomsAsRead(roomsIterator, callback);
}
} else {
if (null != callback) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
callback.onSuccess(null);
}
});
}
}
}
use of org.matrix.androidsdk.rest.callback.ApiCallback in project matrix-android-sdk by matrix-org.
the class MXCallsManager method refreshTurnServer.
/**
* Refresh the turn servers.
*/
private void refreshTurnServer() {
if (mSuspendTurnServerRefresh) {
return;
}
Log.d(LOG_TAG, "## refreshTurnServer () starts");
mUIThreadHandler.post(new Runnable() {
@Override
public void run() {
mCallResClient.getTurnServer(new ApiCallback<JsonObject>() {
private void restartAfter(int msDelay) {
// "ttl" seems invalid
if (msDelay <= 0) {
Log.e(LOG_TAG, "## refreshTurnServer() : invalid delay " + msDelay);
} else {
if (null != mTurnServerTimer) {
mTurnServerTimer.cancel();
}
try {
mTurnServerTimer = new Timer();
mTurnServerTimer.schedule(new TimerTask() {
@Override
public void run() {
if (mTurnServerTimer != null) {
mTurnServerTimer.cancel();
mTurnServerTimer = null;
}
refreshTurnServer();
}
}, msDelay);
} catch (Throwable e) {
Log.e(LOG_TAG, "## refreshTurnServer() failed to start the timer");
if (null != mTurnServerTimer) {
mTurnServerTimer.cancel();
mTurnServerTimer = null;
}
refreshTurnServer();
}
}
}
@Override
public void onSuccess(JsonObject info) {
// privacy
Log.d(LOG_TAG, "## refreshTurnServer () : onSuccess");
if (null != info) {
if (info.has("uris")) {
synchronized (LOG_TAG) {
mTurnServer = info;
}
}
if (info.has("ttl")) {
int ttl = 60000;
try {
ttl = info.get("ttl").getAsInt();
// restart a 90 % before ttl expires
ttl = ttl * 9 / 10;
} catch (Exception e) {
Log.e(LOG_TAG, "Fail to retrieve ttl " + e.getMessage());
}
Log.d(LOG_TAG, "## refreshTurnServer () : onSuccess : retry after " + ttl + " seconds");
restartAfter(ttl * 1000);
}
}
}
@Override
public void onNetworkError(Exception e) {
Log.e(LOG_TAG, "## refreshTurnServer () : onNetworkError " + e);
restartAfter(60000);
}
@Override
public void onMatrixError(MatrixError e) {
Log.e(LOG_TAG, "## refreshTurnServer () : onMatrixError() : " + e.errcode);
if (TextUtils.equals(e.errcode, MatrixError.LIMIT_EXCEEDED) && (null != e.retry_after_ms)) {
Log.e(LOG_TAG, "## refreshTurnServer () : onMatrixError() : retry after " + e.retry_after_ms + " ms");
restartAfter(e.retry_after_ms);
}
}
@Override
public void onUnexpectedError(Exception e) {
// should never happen
}
});
}
});
}
use of org.matrix.androidsdk.rest.callback.ApiCallback in project matrix-android-sdk by matrix-org.
the class EventsRestClient method searchUsers.
/**
* Search users with a patter,
*
* @param text the text to search for.
* @param limit the maximum nbr of users in the response
* @param userIdsFilter the userIds to exclude from the result
* @param callback the request callback
*/
public void searchUsers(final String text, final Integer limit, final Set<String> userIdsFilter, final ApiCallback<SearchUsersResponse> callback) {
SearchUsersParams searchParams = new SearchUsersParams();
searchParams.search_term = text;
searchParams.limit = limit + ((null != userIdsFilter) ? userIdsFilter.size() : 0);
final String uid = mSearchUsersPatternIdentifier = System.currentTimeMillis() + " " + text + " " + limit;
final String description = "searchUsers";
try {
// don't retry to send the request
// if the search fails, stop it
mApi.searchUsers(searchParams, new RestAdapterCallback<SearchUsersRequestResponse>(description, null, new ApiCallback<SearchUsersRequestResponse>() {
/**
* Tells if the current response for the latest request.
*
* @return true if it is the response of the latest request.
*/
private boolean isActiveRequest() {
return TextUtils.equals(mSearchUsersPatternIdentifier, uid);
}
@Override
public void onSuccess(SearchUsersRequestResponse aResponse) {
if (isActiveRequest()) {
SearchUsersResponse response = new SearchUsersResponse();
response.limited = aResponse.limited;
response.results = new ArrayList<>();
Set<String> filter = (null != userIdsFilter) ? userIdsFilter : new HashSet<String>();
if (null != aResponse.results) {
for (SearchUsersRequestResponse.User user : aResponse.results) {
if ((null != user.user_id) && !filter.contains(user.user_id)) {
User addedUser = new User();
addedUser.user_id = user.user_id;
addedUser.avatar_url = user.avatar_url;
addedUser.displayname = user.display_name;
response.results.add(addedUser);
}
}
}
callback.onSuccess(response);
mSearchUsersPatternIdentifier = null;
}
}
@Override
public void onNetworkError(Exception e) {
if (isActiveRequest()) {
callback.onNetworkError(e);
mSearchUsersPatternIdentifier = null;
}
}
@Override
public void onMatrixError(MatrixError e) {
if (isActiveRequest()) {
callback.onMatrixError(e);
mSearchUsersPatternIdentifier = null;
}
}
@Override
public void onUnexpectedError(Exception e) {
if (isActiveRequest()) {
callback.onUnexpectedError(e);
mSearchUsersPatternIdentifier = null;
}
}
}, new RestAdapterCallback.RequestRetryCallBack() {
@Override
public void onRetry() {
searchUsers(text, limit, userIdsFilter, callback);
}
}));
} catch (Throwable t) {
callback.onUnexpectedError(new Exception(t));
}
}
use of org.matrix.androidsdk.rest.callback.ApiCallback in project matrix-android-sdk by matrix-org.
the class EventsRestClient method searchMessagesByText.
/**
* Search a text in room messages.
*
* @param text the text to search for.
* @param rooms a list of rooms to search in. nil means all rooms the user is in.
* @param beforeLimit the number of events to get before the matching results.
* @param afterLimit the number of events to get after the matching results.
* @param nextBatch the token to pass for doing pagination from a previous response.
* @param callback the request callback
*/
public void searchMessagesByText(final String text, final List<String> rooms, final int beforeLimit, final int afterLimit, final String nextBatch, final ApiCallback<SearchResponse> callback) {
SearchParams searchParams = new SearchParams();
SearchRoomEventCategoryParams searchEventParams = new SearchRoomEventCategoryParams();
searchEventParams.search_term = text;
searchEventParams.order_by = "recent";
searchEventParams.event_context = new HashMap<>();
searchEventParams.event_context.put("before_limit", beforeLimit);
searchEventParams.event_context.put("after_limit", afterLimit);
searchEventParams.event_context.put("include_profile", true);
if (null != rooms) {
searchEventParams.filter = new HashMap<>();
searchEventParams.filter.put("rooms", rooms);
}
searchParams.search_categories = new HashMap<>();
searchParams.search_categories.put("room_events", searchEventParams);
final String description = "searchMessageText";
final String uid = System.currentTimeMillis() + "";
mSearchEventsPatternIdentifier = uid + text;
try {
// don't retry to send the request
// if the search fails, stop it
mApi.searchEvents(searchParams, nextBatch, new RestAdapterCallback<SearchResponse>(description, null, new ApiCallback<SearchResponse>() {
/**
* Tells if the current response for the latest request.
*
* @return true if it is the response of the latest request.
*/
private boolean isActiveRequest() {
return TextUtils.equals(mSearchEventsPatternIdentifier, uid + text);
}
@Override
public void onSuccess(SearchResponse response) {
if (isActiveRequest()) {
if (null != callback) {
callback.onSuccess(response);
}
mSearchEventsPatternIdentifier = null;
}
}
@Override
public void onNetworkError(Exception e) {
if (isActiveRequest()) {
if (null != callback) {
callback.onNetworkError(e);
}
mSearchEventsPatternIdentifier = null;
}
}
@Override
public void onMatrixError(MatrixError e) {
if (isActiveRequest()) {
if (null != callback) {
callback.onMatrixError(e);
}
mSearchEventsPatternIdentifier = null;
}
}
@Override
public void onUnexpectedError(Exception e) {
if (isActiveRequest()) {
if (null != callback) {
callback.onUnexpectedError(e);
}
mSearchEventsPatternIdentifier = null;
}
}
}, new RestAdapterCallback.RequestRetryCallBack() {
@Override
public void onRetry() {
searchMessagesByText(text, rooms, beforeLimit, afterLimit, nextBatch, callback);
}
}));
} catch (Throwable t) {
callback.onUnexpectedError(new Exception(t));
}
}
Aggregations