Search in sources :

Example 16 with IMXStore

use of org.matrix.androidsdk.data.store.IMXStore 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;
}
Also used : HashMap(java.util.HashMap) IMXStore(org.matrix.androidsdk.data.store.IMXStore) MXFileStore(org.matrix.androidsdk.data.store.MXFileStore) CountDownLatch(java.util.concurrent.CountDownLatch) Uri(android.net.Uri) MXEventListener(org.matrix.androidsdk.listeners.MXEventListener) LoginRestClient(org.matrix.androidsdk.rest.client.LoginRestClient) MatrixError(org.matrix.androidsdk.rest.model.MatrixError) Credentials(org.matrix.androidsdk.rest.model.login.Credentials)

Example 17 with IMXStore

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

the class MatrixMessageListFragment method onReceiptEvent.

@Override
public void onReceiptEvent(List<String> senderIds) {
    // avoid useless refresh
    boolean shouldRefresh = true;
    try {
        IMXStore store = mSession.getDataHandler().getStore();
        int firstPos = mMessageListView.getFirstVisiblePosition();
        int lastPos = mMessageListView.getLastVisiblePosition();
        ArrayList<String> senders = new ArrayList<>();
        ArrayList<String> eventIds = new ArrayList<>();
        for (int index = firstPos; index <= lastPos; index++) {
            Event event = mAdapter.getItem(index).getEvent();
            if ((null != event.getSender()) && (null != event.eventId)) {
                senders.add(event.getSender());
                eventIds.add(event.eventId);
            }
        }
        shouldRefresh = false;
        // check if the receipt will trigger a refresh
        for (String sender : senderIds) {
            if (!TextUtils.equals(sender, mSession.getMyUserId())) {
                ReceiptData receipt = store.getReceipt(mRoom.getRoomId(), sender);
                // sanity check
                if (null != receipt) {
                    // test if the event is displayed
                    int pos = eventIds.indexOf(receipt.eventId);
                    // if displayed
                    if (pos >= 0) {
                        // the sender is not displayed as a reader (makes sense...)
                        shouldRefresh = !TextUtils.equals(senders.get(pos), sender);
                        if (shouldRefresh) {
                            break;
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        Log.e(LOG_TAG, "onReceiptEvent failed with " + e.getMessage());
    }
    if (shouldRefresh) {
        mAdapter.notifyDataSetChanged();
    }
}
Also used : IMXStore(org.matrix.androidsdk.data.store.IMXStore) ArrayList(java.util.ArrayList) MotionEvent(android.view.MotionEvent) Event(org.matrix.androidsdk.rest.model.Event) ReceiptData(org.matrix.androidsdk.rest.model.ReceiptData)

Example 18 with IMXStore

use of org.matrix.androidsdk.data.store.IMXStore 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);
    }
}
Also used : HashMap(java.util.HashMap) RegistrationFlowResponse(org.matrix.androidsdk.rest.model.login.RegistrationFlowResponse) IMXStore(org.matrix.androidsdk.data.store.IMXStore) MXFileStore(org.matrix.androidsdk.data.store.MXFileStore) CountDownLatch(java.util.concurrent.CountDownLatch) Uri(android.net.Uri) MXEventListener(org.matrix.androidsdk.listeners.MXEventListener) RegistrationParams(org.matrix.androidsdk.rest.model.login.RegistrationParams) LoginRestClient(org.matrix.androidsdk.rest.client.LoginRestClient) MatrixError(org.matrix.androidsdk.rest.model.MatrixError) Credentials(org.matrix.androidsdk.rest.model.login.Credentials)

Aggregations

IMXStore (org.matrix.androidsdk.data.store.IMXStore)18 MatrixError (org.matrix.androidsdk.rest.model.MatrixError)12 Uri (android.net.Uri)10 HashMap (java.util.HashMap)10 CountDownLatch (java.util.concurrent.CountDownLatch)10 MXFileStore (org.matrix.androidsdk.data.store.MXFileStore)10 MXEventListener (org.matrix.androidsdk.listeners.MXEventListener)10 Credentials (org.matrix.androidsdk.rest.model.login.Credentials)10 Room (org.matrix.androidsdk.data.Room)8 Context (android.content.Context)7 JsonObject (com.google.gson.JsonObject)7 Test (org.junit.Test)7 Event (org.matrix.androidsdk.rest.model.Event)7 ArrayList (java.util.ArrayList)6 MXStoreListener (org.matrix.androidsdk.data.store.MXStoreListener)6 List (java.util.List)4 MXDeviceInfo (org.matrix.androidsdk.crypto.data.MXDeviceInfo)3 LoginRestClient (org.matrix.androidsdk.rest.client.LoginRestClient)3 MXDecryptionException (org.matrix.androidsdk.crypto.MXDecryptionException)2 MXUsersDevicesMap (org.matrix.androidsdk.crypto.data.MXUsersDevicesMap)2