Search in sources :

Example 26 with MatrixError

use of org.matrix.androidsdk.rest.model.MatrixError in project matrix-android-sdk by matrix-org.

the class Room method getMemberEvent.

public void getMemberEvent(final String userId, final ApiCallback<Event> callback) {
    final Event event;
    final RoomMember member = getMember(userId);
    if ((null != member) && (null != member.getOriginalEventId())) {
        event = mMemberEventByEventId.get(member.getOriginalEventId());
        if (null == event) {
            mDataHandler.getDataRetriever().getRoomsRestClient().getEvent(getRoomId(), member.getOriginalEventId(), new ApiCallback<Event>() {

                @Override
                public void onSuccess(Event event) {
                    if (null != event) {
                        mMemberEventByEventId.put(event.eventId, event);
                    }
                    callback.onSuccess(event);
                }

                @Override
                public void onNetworkError(Exception e) {
                    callback.onNetworkError(e);
                }

                @Override
                public void onMatrixError(MatrixError e) {
                    callback.onMatrixError(e);
                }

                @Override
                public void onUnexpectedError(Exception e) {
                    callback.onUnexpectedError(e);
                }
            });
            return;
        }
    } else {
        event = null;
    }
    new Handler(Looper.getMainLooper()).post(new Runnable() {

        @Override
        public void run() {
            callback.onSuccess(event);
        }
    });
}
Also used : RoomMember(org.matrix.androidsdk.rest.model.RoomMember) Event(org.matrix.androidsdk.rest.model.Event) Handler(android.os.Handler) MXDataHandler(org.matrix.androidsdk.MXDataHandler) MatrixError(org.matrix.androidsdk.rest.model.MatrixError)

Example 27 with MatrixError

use of org.matrix.androidsdk.rest.model.MatrixError in project matrix-android-sdk by matrix-org.

the class Room method setReadMarkers.

/**
 * Send the request to update the read marker and read receipt.
 *
 * @param aReadMarkerEventId  the read marker event id
 * @param aReadReceiptEventId the read receipt event id
 * @param callback            the asynchronous callback
 */
private void setReadMarkers(final String aReadMarkerEventId, final String aReadReceiptEventId, final ApiCallback<Void> callback) {
    Log.d(LOG_TAG, "## setReadMarkers(): readMarkerEventId " + aReadMarkerEventId + " readReceiptEventId " + aReadMarkerEventId);
    // check if the message ids are valid
    final String readMarkerEventId = MXSession.isMessageId(aReadMarkerEventId) ? aReadMarkerEventId : null;
    final String readReceiptEventId = MXSession.isMessageId(aReadReceiptEventId) ? aReadReceiptEventId : null;
    // if there is nothing to do
    if (TextUtils.isEmpty(readMarkerEventId) && TextUtils.isEmpty(readReceiptEventId)) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {

            @Override
            public void run() {
                if (null != callback) {
                    callback.onSuccess(null);
                }
            }
        });
    } else {
        mDataHandler.getDataRetriever().getRoomsRestClient().sendReadMarker(getRoomId(), readMarkerEventId, readReceiptEventId, new ApiCallback<Void>() {

            @Override
            public void onSuccess(Void info) {
                if (null != callback) {
                    callback.onSuccess(info);
                }
            }

            @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);
                }
            }
        });
    }
}
Also used : Handler(android.os.Handler) MXDataHandler(org.matrix.androidsdk.MXDataHandler) MatrixError(org.matrix.androidsdk.rest.model.MatrixError)

Example 28 with MatrixError

use of org.matrix.androidsdk.rest.model.MatrixError in project matrix-android-sdk by matrix-org.

the class MXMegolmEncryption method getDevicesInRoom.

/**
 * Get the list of devices which can encrypt data to.
 * This method must be called in getDecryptingThreadHandler() thread.
 *
 * @param userIds  the user ids whose devices must be checked.
 * @param callback the asynchronous callback
 */
private void getDevicesInRoom(final List<String> userIds, final ApiCallback<MXUsersDevicesMap<MXDeviceInfo>> callback) {
    // We are happy to use a cached version here: we assume that if we already
    // have a list of the user's devices, then we already share an e2e room
    // with them, which means that they will have announced any new devices via
    // an m.new_device.
    mCrypto.getDeviceList().downloadKeys(userIds, false, new ApiCallback<MXUsersDevicesMap<MXDeviceInfo>>() {

        @Override
        public void onSuccess(final MXUsersDevicesMap<MXDeviceInfo> devices) {
            mCrypto.getEncryptingThreadHandler().post(new Runnable() {

                @Override
                public void run() {
                    boolean encryptToVerifiedDevicesOnly = mCrypto.getGlobalBlacklistUnverifiedDevices() || mCrypto.isRoomBlacklistUnverifiedDevices(mRoomId);
                    final MXUsersDevicesMap<MXDeviceInfo> devicesInRoom = new MXUsersDevicesMap<>();
                    final MXUsersDevicesMap<MXDeviceInfo> unknownDevices = new MXUsersDevicesMap<>();
                    List<String> userIds = devices.getUserIds();
                    for (String userId : userIds) {
                        List<String> deviceIds = devices.getUserDeviceIds(userId);
                        for (String deviceId : deviceIds) {
                            MXDeviceInfo deviceInfo = devices.getObject(deviceId, userId);
                            if (mCrypto.warnOnUnknownDevices() && deviceInfo.isUnknown()) {
                                // The device is not yet known by the user
                                unknownDevices.setObject(deviceInfo, userId, deviceId);
                                continue;
                            }
                            if (deviceInfo.isBlocked()) {
                                // Remove any blocked devices
                                continue;
                            }
                            if (!deviceInfo.isVerified() && encryptToVerifiedDevicesOnly) {
                                continue;
                            }
                            if (TextUtils.equals(deviceInfo.identityKey(), mCrypto.getOlmDevice().getDeviceCurve25519Key())) {
                                // Don't bother sending to ourself
                                continue;
                            }
                            devicesInRoom.setObject(deviceInfo, userId, deviceId);
                        }
                    }
                    mCrypto.getUIHandler().post(new Runnable() {

                        @Override
                        public void run() {
                            // if so, warn the user so they can verify or ignore.
                            if (0 != unknownDevices.getMap().size()) {
                                callback.onMatrixError(new MXCryptoError(MXCryptoError.UNKNOWN_DEVICES_CODE, MXCryptoError.UNABLE_TO_ENCRYPT, MXCryptoError.UNKNOWN_DEVICES_REASON, unknownDevices));
                            } else {
                                callback.onSuccess(devicesInRoom);
                            }
                        }
                    });
                }
            });
        }

        @Override
        public void onNetworkError(Exception e) {
            callback.onNetworkError(e);
        }

        @Override
        public void onMatrixError(MatrixError e) {
            callback.onMatrixError(e);
        }

        @Override
        public void onUnexpectedError(Exception e) {
            callback.onUnexpectedError(e);
        }
    });
}
Also used : MXDeviceInfo(org.matrix.androidsdk.crypto.data.MXDeviceInfo) MXUsersDevicesMap(org.matrix.androidsdk.crypto.data.MXUsersDevicesMap) MatrixError(org.matrix.androidsdk.rest.model.MatrixError) MXCryptoError(org.matrix.androidsdk.crypto.MXCryptoError)

Example 29 with MatrixError

use of org.matrix.androidsdk.rest.model.MatrixError in project matrix-android-sdk by matrix-org.

the class MXMegolmEncryption method ensureOutboundSession.

/**
 * Ensure the outbound session
 *
 * @param devicesInRoom the devices list
 * @param callback      the asynchronous callback.
 */
private void ensureOutboundSession(MXUsersDevicesMap<MXDeviceInfo> devicesInRoom, final ApiCallback<MXOutboundSessionInfo> callback) {
    MXOutboundSessionInfo session = mOutboundSession;
    if ((null == session) || // Need to make a brand new session?
    session.needsRotation(mSessionRotationPeriodMsgs, mSessionRotationPeriodMs) || // Determine if we have shared with anyone we shouldn't have
    session.sharedWithTooManyDevices(devicesInRoom)) {
        mOutboundSession = session = prepareNewSessionInRoom();
    }
    if (mShareOperationIsProgress) {
        Log.d(LOG_TAG, "## ensureOutboundSessionInRoom() : already in progress");
        // Key share already in progress
        return;
    }
    final MXOutboundSessionInfo fSession = session;
    HashMap<String, ArrayList<MXDeviceInfo>> /* userId */
    shareMap = new HashMap<>();
    List<String> userIds = devicesInRoom.getUserIds();
    for (String userId : userIds) {
        List<String> deviceIds = devicesInRoom.getUserDeviceIds(userId);
        for (String deviceId : deviceIds) {
            MXDeviceInfo deviceInfo = devicesInRoom.getObject(deviceId, userId);
            if (null == fSession.mSharedWithDevices.getObject(deviceId, userId)) {
                if (!shareMap.containsKey(userId)) {
                    shareMap.put(userId, new ArrayList<MXDeviceInfo>());
                }
                shareMap.get(userId).add(deviceInfo);
            }
        }
    }
    shareKey(fSession, shareMap, new ApiCallback<Void>() {

        @Override
        public void onSuccess(Void anything) {
            mShareOperationIsProgress = false;
            if (null != callback) {
                callback.onSuccess(fSession);
            }
        }

        @Override
        public void onNetworkError(final Exception e) {
            Log.e(LOG_TAG, "## ensureOutboundSessionInRoom() : shareKey onNetworkError " + e.getMessage());
            if (null != callback) {
                callback.onNetworkError(e);
            }
            mShareOperationIsProgress = false;
        }

        @Override
        public void onMatrixError(final MatrixError e) {
            Log.e(LOG_TAG, "## ensureOutboundSessionInRoom() : shareKey onMatrixError " + e.getMessage());
            if (null != callback) {
                callback.onMatrixError(e);
            }
            mShareOperationIsProgress = false;
        }

        @Override
        public void onUnexpectedError(final Exception e) {
            Log.e(LOG_TAG, "## ensureOutboundSessionInRoom() : shareKey onUnexpectedError " + e.getMessage());
            if (null != callback) {
                callback.onUnexpectedError(e);
            }
            mShareOperationIsProgress = false;
        }
    });
}
Also used : HashMap(java.util.HashMap) MXDeviceInfo(org.matrix.androidsdk.crypto.data.MXDeviceInfo) ArrayList(java.util.ArrayList) MatrixError(org.matrix.androidsdk.rest.model.MatrixError)

Example 30 with MatrixError

use of org.matrix.androidsdk.rest.model.MatrixError in project matrix-android-sdk by matrix-org.

the class GroupsManager method createGroup.

/**
 * Create a group.
 *
 * @param localPart the local part
 * @param groupName the group human name
 * @param callback  the asynchronous callback
 */
public void createGroup(String localPart, String groupName, final ApiCallback<String> callback) {
    final CreateGroupParams params = new CreateGroupParams();
    params.localpart = localPart;
    params.profile = new GroupProfile();
    params.profile.name = groupName;
    getGroupsRestClient().createGroup(params, new ApiCallback<String>() {

        @Override
        public void onSuccess(String groupId) {
            Group group = getGroup(groupId);
            // if the group does not exist, create it
            if (null == group) {
                group = new Group(groupId);
                group.setGroupProfile(params.profile);
                group.setMembership(RoomMember.MEMBERSHIP_JOIN);
                mStore.storeGroup(group);
            }
            callback.onSuccess(groupId);
        }

        @Override
        public void onNetworkError(Exception e) {
            callback.onNetworkError(e);
        }

        @Override
        public void onMatrixError(MatrixError e) {
            callback.onMatrixError(e);
        }

        @Override
        public void onUnexpectedError(Exception e) {
            callback.onUnexpectedError(e);
        }
    });
}
Also used : Group(org.matrix.androidsdk.rest.model.group.Group) CreateGroupParams(org.matrix.androidsdk.rest.model.group.CreateGroupParams) GroupProfile(org.matrix.androidsdk.rest.model.group.GroupProfile) MatrixError(org.matrix.androidsdk.rest.model.MatrixError)

Aggregations

MatrixError (org.matrix.androidsdk.rest.model.MatrixError)73 HashMap (java.util.HashMap)41 CountDownLatch (java.util.concurrent.CountDownLatch)39 Room (org.matrix.androidsdk.data.Room)33 JsonObject (com.google.gson.JsonObject)31 Test (org.junit.Test)30 Event (org.matrix.androidsdk.rest.model.Event)27 MXEventListener (org.matrix.androidsdk.listeners.MXEventListener)25 ArrayList (java.util.ArrayList)24 Context (android.content.Context)23 RoomState (org.matrix.androidsdk.data.RoomState)20 MXDeviceInfo (org.matrix.androidsdk.crypto.data.MXDeviceInfo)17 ApiCallback (org.matrix.androidsdk.rest.callback.ApiCallback)15 MXUsersDevicesMap (org.matrix.androidsdk.crypto.data.MXUsersDevicesMap)13 IMXStore (org.matrix.androidsdk.data.store.IMXStore)12 Credentials (org.matrix.androidsdk.rest.model.login.Credentials)12 Uri (android.net.Uri)10 MXFileStore (org.matrix.androidsdk.data.store.MXFileStore)10 EventTimeline (org.matrix.androidsdk.data.EventTimeline)9 SimpleApiCallback (org.matrix.androidsdk.rest.callback.SimpleApiCallback)7