use of org.matrix.androidsdk.data.Room in project matrix-android-sdk by matrix-org.
the class MXDataHandler method getDirectChatRoomIdsList.
/**
* Return the list of the direct chat room IDs for the user given in parameter.<br>
* Based on the account_data map content, the entry associated with aSearchedUserId is returned.
*
* @param aSearchedUserId user ID
* @return the list of the direct chat room Id
*/
public List<String> getDirectChatRoomIdsList(String aSearchedUserId) {
ArrayList<String> directChatRoomIdsList = new ArrayList<>();
IMXStore store = getStore();
Room room;
HashMap<String, List<String>> params;
if (null != store.getDirectChatRoomsDict()) {
params = new HashMap<>(store.getDirectChatRoomsDict());
if (params.containsKey(aSearchedUserId)) {
directChatRoomIdsList = new ArrayList<>();
for (String roomId : params.get(aSearchedUserId)) {
room = store.getRoom(roomId);
if (null != room) {
// skipp empty rooms
directChatRoomIdsList.add(roomId);
}
}
} else {
Log.w(LOG_TAG, "## getDirectChatRoomIdsList(): UserId " + aSearchedUserId + " has no entry in account_data");
}
} else {
Log.w(LOG_TAG, "## getDirectChatRoomIdsList(): failure - getDirectChatRoomsDict()=null");
}
return directChatRoomIdsList;
}
use of org.matrix.androidsdk.data.Room in project matrix-android-sdk by matrix-org.
the class MXSession method removeMediasBefore.
/**
* Remove the medias older than the provided timestamp.
*
* @param context the context
* @param timestamp the timestamp (in seconds)
*/
public void removeMediasBefore(final Context context, final long timestamp) {
// list the files to keep even if they are older than the provided timestamp
// because their upload failed
final Set<String> filesToKeep = new HashSet<>();
IMXStore store = getDataHandler().getStore();
Collection<Room> rooms = store.getRooms();
for (Room room : rooms) {
Collection<Event> events = store.getRoomMessages(room.getRoomId());
if (null != events) {
for (Event event : events) {
try {
Message message = null;
if (TextUtils.equals(Event.EVENT_TYPE_MESSAGE, event.getType())) {
message = JsonUtils.toMessage(event.getContent());
} else if (TextUtils.equals(Event.EVENT_TYPE_STICKER, event.getType())) {
message = JsonUtils.toStickerMessage(event.getContent());
}
if (null != message && message instanceof MediaMessage) {
MediaMessage mediaMessage = (MediaMessage) message;
if (mediaMessage.isThumbnailLocalContent()) {
filesToKeep.add(Uri.parse(mediaMessage.getThumbnailUrl()).getPath());
}
if (mediaMessage.isLocalContent()) {
filesToKeep.add(Uri.parse(mediaMessage.getUrl()).getPath());
}
}
} catch (Exception e) {
Log.e(LOG_TAG, "## removeMediasBefore() : failed " + e.getMessage());
}
}
}
}
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
long length = getMediasCache().removeMediasBefore(timestamp, filesToKeep);
// delete also the log files
// they might be large
File logsDir = Log.getLogDirectory();
if (null != logsDir) {
File[] logFiles = logsDir.listFiles();
if (null != logFiles) {
for (File file : logFiles) {
if (ContentUtils.getLastAccessTime(file) < timestamp) {
length += file.length();
file.delete();
}
}
}
}
if (0 != length) {
Log.d(LOG_TAG, "## removeMediasBefore() : save " + android.text.format.Formatter.formatFileSize(context, length));
} else {
Log.d(LOG_TAG, "## removeMediasBefore() : useless");
}
return null;
}
};
try {
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} catch (Exception e) {
Log.e(LOG_TAG, "## removeMediasBefore() : failed " + e.getMessage());
task.cancel(true);
}
}
use of org.matrix.androidsdk.data.Room 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.data.Room in project matrix-android-sdk by matrix-org.
the class MXCrypto method setDeviceVerification.
/**
* Update the blocked/verified state of the given device.
*
* @param verificationStatus the new verification status
* @param deviceId the unique identifier for the device.
* @param userId the owner of the device
* @param callback the asynchronous callback
*/
public void setDeviceVerification(final int verificationStatus, final String deviceId, final String userId, final ApiCallback<Void> callback) {
if (hasBeenReleased()) {
return;
}
final ArrayList<String> userRoomIds = new ArrayList<>();
Collection<Room> rooms = mSession.getDataHandler().getStore().getRooms();
for (Room room : rooms) {
if (room.isEncrypted()) {
RoomMember roomMember = room.getMember(userId);
// test if the user joins the room
if ((null != roomMember) && TextUtils.equals(roomMember.membership, RoomMember.MEMBERSHIP_JOIN)) {
userRoomIds.add(room.getRoomId());
}
}
}
getEncryptingThreadHandler().post(new Runnable() {
@Override
public void run() {
MXDeviceInfo device = mCryptoStore.getUserDevice(deviceId, userId);
// Sanity check
if (null == device) {
Log.e(LOG_TAG, "## setDeviceVerification() : Unknown device " + userId + ":" + deviceId);
if (null != callback) {
getUIHandler().post(new Runnable() {
@Override
public void run() {
callback.onSuccess(null);
}
});
}
return;
}
if (device.mVerified != verificationStatus) {
device.mVerified = verificationStatus;
mCryptoStore.storeUserDevice(userId, device);
}
if (null != callback) {
getUIHandler().post(new Runnable() {
@Override
public void run() {
callback.onSuccess(null);
}
});
}
}
});
}
use of org.matrix.androidsdk.data.Room in project matrix-android-sdk by matrix-org.
the class MXCrypto method setEncryptionInRoom.
/**
* Configure a room to use encryption.
* This method must be called in getEncryptingThreadHandler
*
* @param roomId the room id to enable encryption in.
* @param algorithm the encryption config for the room.
* @param inhibitDeviceQuery true to suppress device list query for users in the room (for now)
* @return true if the operation succeeds.
*/
private boolean setEncryptionInRoom(String roomId, String algorithm, boolean inhibitDeviceQuery) {
if (hasBeenReleased()) {
return false;
}
// If we already have encryption in this room, we should ignore this event
// (for now at least. Maybe we should alert the user somehow?)
String existingAlgorithm = mCryptoStore.getRoomAlgorithm(roomId);
if (!TextUtils.isEmpty(existingAlgorithm) && !TextUtils.equals(existingAlgorithm, algorithm)) {
Log.e(LOG_TAG, "## setEncryptionInRoom() : Ignoring m.room.encryption event which requests a change of config in " + roomId);
return false;
}
Class<IMXEncrypting> encryptingClass = MXCryptoAlgorithms.sharedAlgorithms().encryptorClassForAlgorithm(algorithm);
if (null == encryptingClass) {
Log.e(LOG_TAG, "## setEncryptionInRoom() : Unable to encrypt with " + algorithm);
return false;
}
mCryptoStore.storeRoomAlgorithm(roomId, algorithm);
IMXEncrypting alg;
try {
Constructor<?> ctor = encryptingClass.getConstructors()[0];
alg = (IMXEncrypting) ctor.newInstance();
} catch (Exception e) {
Log.e(LOG_TAG, "## setEncryptionInRoom() : fail to load the class");
return false;
}
alg.initWithMatrixSession(mSession, roomId);
synchronized (mRoomEncryptors) {
mRoomEncryptors.put(roomId, alg);
}
// we just invalidate everyone in the room.
if (null == existingAlgorithm) {
Log.d(LOG_TAG, "Enabling encryption in " + roomId + " for the first time; invalidating device lists for all users therein");
Room room = mSession.getDataHandler().getRoom(roomId);
if (null != room) {
Collection<RoomMember> members = room.getJoinedMembers();
List<String> userIds = new ArrayList<>();
for (RoomMember m : members) {
userIds.add(m.getUserId());
}
getDeviceList().startTrackingDeviceList(userIds);
if (!inhibitDeviceQuery) {
getDeviceList().refreshOutdatedDeviceLists();
}
}
}
return true;
}
Aggregations