use of org.matrix.androidsdk.listeners.MXEventListener in project matrix-android-sdk by matrix-org.
the class TestsHelper method createAccountAndSync.
/**
* Create an account and a dedicated session
* @param context the context
* @param userName the account username
* @param password the password
* @param startSession true to perform an initial sync
* @param callback the callback
* @throws Exception an exception if the account cannot be created
*/
public static void createAccountAndSync(Context context, String userName, String password, boolean startSession, ApiCallback<MXSession> callback) throws Exception {
RestClient.mUseMXExececutor = true;
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<>();
RegistrationParams registrationParams = new RegistrationParams();
mLock = new CountDownLatch(1);
// get the registration session id
loginRestClient.register(registrationParams, new ApiCallback<Credentials>() {
@Override
public void onSuccess(Credentials credentials) {
mLock.countDown();
}
@Override
public void onNetworkError(Exception e) {
mLock.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
// detect if a parameter is expected
RegistrationFlowResponse registrationFlowResponse = null;
// when a response is not completed the server returns an error message
if ((null != e.mStatus) && (e.mStatus == 401)) {
try {
registrationFlowResponse = JsonUtils.toRegistrationFlowResponse(e.mErrorBodyAsString);
} catch (Exception castExcept) {
}
}
// check if the server response can be casted
if (null != registrationFlowResponse) {
params.put("session", registrationFlowResponse.session);
}
mLock.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
mLock.countDown();
}
});
mLock.await(1000, TimeUnit.MILLISECONDS);
String session = (String) params.get("session");
if (null == session) {
callback.onUnexpectedError(null);
}
registrationParams.username = userName;
registrationParams.password = password;
HashMap<String, Object> authParams = new HashMap<>();
authParams.put("session", session);
authParams.put("type", LoginRestClient.LOGIN_FLOW_TYPE_DUMMY);
registrationParams.auth = authParams;
mLock = new CountDownLatch(1);
loginRestClient.register(registrationParams, 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(1000, TimeUnit.MILLISECONDS);
Credentials credentials = (Credentials) params.get("credentials");
if (null == credentials) {
callback.onMatrixError(null);
return;
}
hs.setCredentials(credentials);
IMXStore store = new MXFileStore(hs, context);
MXSession mxSession = new MXSession(hs, new MXDataHandler(store, credentials), context);
if (!startSession) {
callback.onSuccess(mxSession);
return;
}
mxSession.getDataHandler().getStore().open();
mxSession.startEventStream(null);
mLock = new CountDownLatch(1);
mxSession.getDataHandler().addListener(new MXEventListener() {
@Override
public void onInitialSyncComplete(String toToken) {
params.put("isInit", true);
mLock.countDown();
}
});
mLock.await(10000, TimeUnit.MILLISECONDS);
if (params.containsKey("isInit")) {
callback.onSuccess(mxSession);
} else {
callback.onMatrixError(null);
}
}
use of org.matrix.androidsdk.listeners.MXEventListener in project matrix-android-sdk by matrix-org.
the class User method addEventListener.
/**
* Add an event listener to this room. Only events relative to the room will come down.
*
* @param eventListener the event listener to add
*/
public void addEventListener(final IMXEventListener eventListener) {
// Create a global listener that we'll add to the data handler
IMXEventListener globalListener = new MXEventListener() {
@Override
public void onPresenceUpdate(Event event, User user) {
// Only pass event through for this user
if (user.user_id.equals(user_id)) {
eventListener.onPresenceUpdate(event, user);
}
}
};
getEventListeners().put(eventListener, globalListener);
// the handler could be set later
if (null != mDataHandler) {
mDataHandler.addListener(globalListener);
} else {
getPendingListeners().add(globalListener);
}
}
Aggregations