use of org.matrix.androidsdk.rest.model.MatrixError 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.rest.model.MatrixError in project matrix-android-sdk by matrix-org.
the class MXCall method sendHangup.
/**
* send an hang up event
*
* @param reason the reason
*/
protected void sendHangup(String reason) {
JsonObject hangupContent = new JsonObject();
hangupContent.add("version", new JsonPrimitive(0));
hangupContent.add("call_id", new JsonPrimitive(this.mCallId));
if (!TextUtils.isEmpty(reason)) {
hangupContent.add("reason", new JsonPrimitive(reason));
}
Event event = new Event(Event.EVENT_TYPE_CALL_HANGUP, hangupContent, mSession.getCredentials().userId, mCallSignalingRoom.getRoomId());
// local notification to indicate the end of call
mUIThreadHandler.post(new Runnable() {
@Override
public void run() {
dispatchOnCallEnd(END_CALL_REASON_USER_HIMSELF);
}
});
Log.d(LOG_TAG, "## sendHangup(): reason=" + reason);
// send hang up event to the server
mCallSignalingRoom.sendEvent(event, new ApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
Log.d(LOG_TAG, "## sendHangup(): onSuccess");
}
@Override
public void onNetworkError(Exception e) {
Log.d(LOG_TAG, "## sendHangup(): onNetworkError Msg=" + e.getMessage());
}
@Override
public void onMatrixError(MatrixError e) {
Log.d(LOG_TAG, "## sendHangup(): onMatrixError Msg=" + e.getMessage());
}
@Override
public void onUnexpectedError(Exception e) {
Log.d(LOG_TAG, "## sendHangup(): onUnexpectedError Msg=" + e.getMessage());
}
});
}
use of org.matrix.androidsdk.rest.model.MatrixError in project matrix-android-sdk by matrix-org.
the class MXCallsManager method checkPendingIncomingCalls.
/**
* check if there is a pending incoming call
*/
public void checkPendingIncomingCalls() {
// Log.d(LOG_TAG, "checkPendingIncomingCalls");
mUIThreadHandler.post(new Runnable() {
@Override
public void run() {
if (mxPendingIncomingCallId.size() > 0) {
for (String callId : mxPendingIncomingCallId) {
final IMXCall call = getCallWithCallId(callId);
if (null != call) {
final Room room = call.getRoom();
// If there are some unknown devices, the answer event would not be encrypted.
if ((null != room) && room.isEncrypted() && mSession.getCrypto().warnOnUnknownDevices() && (room.getJoinedMembers().size() == 2)) {
// test if the encrypted events are sent only to the verified devices (any room)
mSession.getCrypto().getGlobalBlacklistUnverifiedDevices(new SimpleApiCallback<Boolean>() {
@Override
public void onSuccess(Boolean sendToVerifiedDevicesOnly) {
if (sendToVerifiedDevicesOnly) {
dispatchOnIncomingCall(call, null);
} else {
// test if the encrypted events are sent only to the verified devices (only this room)
mSession.getCrypto().isRoomBlacklistUnverifiedDevices(room.getRoomId(), new SimpleApiCallback<Boolean>() {
@Override
public void onSuccess(Boolean sendToVerifiedDevicesOnly) {
if (sendToVerifiedDevicesOnly) {
dispatchOnIncomingCall(call, null);
} else {
List<RoomMember> members = new ArrayList<>(room.getJoinedMembers());
String userId1 = members.get(0).getUserId();
String userId2 = members.get(1).getUserId();
Log.d(LOG_TAG, "## checkPendingIncomingCalls() : check the unknown devices");
//
mSession.getCrypto().checkUnknownDevices(Arrays.asList(userId1, userId2), new ApiCallback<Void>() {
@Override
public void onSuccess(Void anything) {
Log.d(LOG_TAG, "## checkPendingIncomingCalls() : no unknown device");
dispatchOnIncomingCall(call, null);
}
@Override
public void onNetworkError(Exception e) {
Log.e(LOG_TAG, "## checkPendingIncomingCalls() : checkUnknownDevices failed " + e.getMessage());
dispatchOnIncomingCall(call, null);
}
@Override
public void onMatrixError(MatrixError e) {
MXUsersDevicesMap<MXDeviceInfo> unknownDevices = null;
if (e instanceof MXCryptoError) {
MXCryptoError cryptoError = (MXCryptoError) e;
if (MXCryptoError.UNKNOWN_DEVICES_CODE.equals(cryptoError.errcode)) {
unknownDevices = (MXUsersDevicesMap<MXDeviceInfo>) cryptoError.mExceptionData;
}
}
if (null != unknownDevices) {
Log.d(LOG_TAG, "## checkPendingIncomingCalls() : checkUnknownDevices found some unknown devices");
} else {
Log.e(LOG_TAG, "## checkPendingIncomingCalls() : checkUnknownDevices failed " + e.getMessage());
}
dispatchOnIncomingCall(call, unknownDevices);
}
@Override
public void onUnexpectedError(Exception e) {
Log.e(LOG_TAG, "## checkPendingIncomingCalls() : checkUnknownDevices failed " + e.getMessage());
dispatchOnIncomingCall(call, null);
}
});
}
}
});
}
}
});
} else {
dispatchOnIncomingCall(call, null);
}
}
}
}
mxPendingIncomingCallId.clear();
}
});
}
use of org.matrix.androidsdk.rest.model.MatrixError in project matrix-android-sdk by matrix-org.
the class MXCallsManager method createCallInRoom.
/**
* Create an IMXCall in the room defines by its room Id.
* -> for a 1:1 call, it is a standard call.
* -> for a conference call,
* ----> the conference user is invited to the room (if it was not yet invited)
* ----> the call signaling room is created (or retrieved) with the conference
* ----> and the call is started
*
* @param roomId the room roomId
* @param isVideo true to start a video call
* @param callback the async callback
*/
public void createCallInRoom(final String roomId, final boolean isVideo, final ApiCallback<IMXCall> callback) {
Log.d(LOG_TAG, "createCallInRoom in " + roomId);
final Room room = mSession.getDataHandler().getRoom(roomId);
// sanity check
if (null != room) {
if (isSupported()) {
int joinedMembers = room.getJoinedMembers().size();
Log.d(LOG_TAG, "createCallInRoom : the room has " + joinedMembers + " joined members");
if (joinedMembers > 1) {
if (joinedMembers == 2) {
// So it seems safer to reject the call creation it it will fail.
if (room.isEncrypted() && mSession.getCrypto().warnOnUnknownDevices()) {
List<RoomMember> members = new ArrayList<>(room.getJoinedMembers());
String userId1 = members.get(0).getUserId();
String userId2 = members.get(1).getUserId();
// force the refresh to ensure that the devices list is up-to-date
mSession.getCrypto().checkUnknownDevices(Arrays.asList(userId1, userId2), new ApiCallback<Void>() {
@Override
public void onSuccess(Void anything) {
final IMXCall call = getCallWithCallId(null, true);
call.setRooms(room, room);
call.setIsVideo(isVideo);
dispatchOnOutgoingCall(call);
if (null != callback) {
mUIThreadHandler.post(new Runnable() {
@Override
public void run() {
callback.onSuccess(call);
}
});
}
}
@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 {
final IMXCall call = getCallWithCallId(null, true);
call.setIsVideo(isVideo);
dispatchOnOutgoingCall(call);
call.setRooms(room, room);
if (null != callback) {
mUIThreadHandler.post(new Runnable() {
@Override
public void run() {
callback.onSuccess(call);
}
});
}
}
} else {
Log.d(LOG_TAG, "createCallInRoom : inviteConferenceUser");
inviteConferenceUser(room, new ApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
Log.d(LOG_TAG, "createCallInRoom : inviteConferenceUser succeeds");
getConferenceUserRoom(room.getRoomId(), new ApiCallback<Room>() {
@Override
public void onSuccess(Room conferenceRoom) {
Log.d(LOG_TAG, "createCallInRoom : getConferenceUserRoom succeeds");
final IMXCall call = getCallWithCallId(null, true);
call.setRooms(room, conferenceRoom);
call.setIsConference(true);
call.setIsVideo(isVideo);
dispatchOnOutgoingCall(call);
if (null != callback) {
mUIThreadHandler.post(new Runnable() {
@Override
public void run() {
callback.onSuccess(call);
}
});
}
}
@Override
public void onNetworkError(Exception e) {
Log.d(LOG_TAG, "createCallInRoom : getConferenceUserRoom failed " + e.getMessage());
if (null != callback) {
callback.onNetworkError(e);
}
}
@Override
public void onMatrixError(MatrixError e) {
Log.d(LOG_TAG, "createCallInRoom : getConferenceUserRoom failed " + e.getMessage());
if (null != callback) {
callback.onMatrixError(e);
}
}
@Override
public void onUnexpectedError(Exception e) {
Log.d(LOG_TAG, "createCallInRoom : getConferenceUserRoom failed " + e.getMessage());
if (null != callback) {
callback.onUnexpectedError(e);
}
}
});
}
@Override
public void onNetworkError(Exception e) {
Log.d(LOG_TAG, "createCallInRoom : inviteConferenceUser fails " + e.getMessage());
if (null != callback) {
callback.onNetworkError(e);
}
}
@Override
public void onMatrixError(MatrixError e) {
Log.d(LOG_TAG, "createCallInRoom : inviteConferenceUser fails " + e.getMessage());
if (null != callback) {
callback.onMatrixError(e);
}
}
@Override
public void onUnexpectedError(Exception e) {
Log.d(LOG_TAG, "createCallInRoom : inviteConferenceUser fails " + e.getMessage());
if (null != callback) {
callback.onUnexpectedError(e);
}
}
});
}
} else {
if (null != callback) {
callback.onMatrixError(new MatrixError(MatrixError.NOT_SUPPORTED, "too few users"));
}
}
} else {
if (null != callback) {
callback.onMatrixError(new MatrixError(MatrixError.NOT_SUPPORTED, "VOIP is not supported"));
}
}
} else {
if (null != callback) {
callback.onMatrixError(new MatrixError(MatrixError.NOT_FOUND, "room not found"));
}
}
}
use of org.matrix.androidsdk.rest.model.MatrixError in project matrix-android-sdk by matrix-org.
the class MXCallsManager method getConferenceUserRoom.
/**
* Get the room with the conference user dedicated for the passed room.
*
* @param roomId the room id.
* @param callback the async callback.
*/
private void getConferenceUserRoom(final String roomId, final ApiCallback<Room> callback) {
Log.d(LOG_TAG, "getConferenceUserRoom with room id " + roomId);
String conferenceUserId = getConferenceUserId(roomId);
Room conferenceRoom = null;
Collection<Room> rooms = mSession.getDataHandler().getStore().getRooms();
// Use an existing 1:1 with the conference user; else make one
for (Room room : rooms) {
if (room.isConferenceUserRoom() && (2 == room.getMembers().size()) && (null != room.getMember(conferenceUserId))) {
conferenceRoom = room;
break;
}
}
if (null != conferenceRoom) {
Log.d(LOG_TAG, "getConferenceUserRoom : the room already exists");
final Room fConferenceRoom = conferenceRoom;
mSession.getDataHandler().getStore().commit();
mUIThreadHandler.post(new Runnable() {
@Override
public void run() {
callback.onSuccess(fConferenceRoom);
}
});
} else {
Log.d(LOG_TAG, "getConferenceUserRoom : create the room");
CreateRoomParams params = new CreateRoomParams();
params.preset = CreateRoomParams.PRESET_PRIVATE_CHAT;
params.invite = Arrays.asList(conferenceUserId);
mSession.createRoom(params, new ApiCallback<String>() {
@Override
public void onSuccess(String roomId) {
Log.d(LOG_TAG, "getConferenceUserRoom : the room creation succeeds");
Room room = mSession.getDataHandler().getRoom(roomId);
if (null != room) {
room.setIsConferenceUserRoom(true);
mSession.getDataHandler().getStore().commit();
callback.onSuccess(room);
}
}
@Override
public void onNetworkError(Exception e) {
Log.d(LOG_TAG, "getConferenceUserRoom : failed " + e.getMessage());
callback.onNetworkError(e);
}
@Override
public void onMatrixError(MatrixError e) {
Log.d(LOG_TAG, "getConferenceUserRoom : failed " + e.getMessage());
callback.onMatrixError(e);
}
@Override
public void onUnexpectedError(Exception e) {
Log.d(LOG_TAG, "getConferenceUserRoom : failed " + e.getMessage());
callback.onUnexpectedError(e);
}
});
}
}
Aggregations