use of org.matrix.androidsdk.data.store.IMXStore in project matrix-android-sdk by matrix-org.
the class MXDataHandler method setDirectChatRoomsMap.
/**
* Store and upload the provided direct chat rooms map.
*
* @param directChatRoomsMap the direct chats map
* @param callback the asynchronous callback
*/
public void setDirectChatRoomsMap(Map<String, List<String>> directChatRoomsMap, ApiCallback<Void> callback) {
Log.d(LOG_TAG, "## setDirectChatRoomsMap()");
IMXStore store = getStore();
if (null != store) {
// update the store value
// do not wait the server request echo to update the store
store.setDirectChatRoomsDict(directChatRoomsMap);
} else {
Log.e(LOG_TAG, "## setDirectChatRoomsMap() : null store");
}
mLocalDirectChatRoomIdsList = null;
// Upload the new map
mAccountDataRestClient.setAccountData(getMyUser().user_id, AccountDataRestClient.ACCOUNT_DATA_TYPE_DIRECT_MESSAGES, directChatRoomsMap, callback);
}
use of org.matrix.androidsdk.data.store.IMXStore 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.store.IMXStore 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.store.IMXStore in project matrix-android-sdk by matrix-org.
the class MXDataHandler method getMyUser.
/**
* Get the session's current user. The MyUser object provides methods for updating user properties which are not possible for other users.
*
* @return the session's MyUser object
*/
public MyUser getMyUser() {
checkIfAlive();
IMXStore store = getStore();
// which should be the case if this is called after the initial sync
if (mMyUser == null) {
mMyUser = new MyUser(store.getUser(mCredentials.userId));
mMyUser.setDataHandler(this);
// assume the profile is not yet initialized
if (null == store.displayName()) {
store.setAvatarURL(mMyUser.getAvatarUrl(), System.currentTimeMillis());
store.setDisplayName(mMyUser.displayname, System.currentTimeMillis());
} else {
// use the latest user information
// The user could have updated his profile in offline mode and kill the application.
mMyUser.displayname = store.displayName();
mMyUser.setAvatarUrl(store.avatarURL());
}
// Handle the case where the user is null by loading the user information from the server
mMyUser.user_id = mCredentials.userId;
} else if (null != store) {
// assume the profile is not yet initialized
if ((null == store.displayName()) && (null != mMyUser.displayname)) {
// setAvatarURL && setDisplayName perform a commit if it is required.
store.setAvatarURL(mMyUser.getAvatarUrl(), System.currentTimeMillis());
store.setDisplayName(mMyUser.displayname, System.currentTimeMillis());
} else if (!TextUtils.equals(mMyUser.displayname, store.displayName())) {
mMyUser.displayname = store.displayName();
mMyUser.setAvatarUrl(store.avatarURL());
}
}
// check if there is anything to refresh
mMyUser.refreshUserInfos(null);
return mMyUser;
}
use of org.matrix.androidsdk.data.store.IMXStore in project matrix-android-sdk by matrix-org.
the class MXDataHandler method getDirectChatRoomIdsList.
/**
* @return the direct chat room ids list
*/
public List<String> getDirectChatRoomIdsList() {
if (null != mLocalDirectChatRoomIdsList)
return mLocalDirectChatRoomIdsList;
IMXStore store = getStore();
List<String> directChatRoomIdsList = new ArrayList<>();
if (null == store) {
Log.e(LOG_TAG, "## getDirectChatRoomIdsList() : null store");
return directChatRoomIdsList;
}
Collection<List<String>> listOfList = null;
if (null != store.getDirectChatRoomsDict()) {
listOfList = store.getDirectChatRoomsDict().values();
}
// if the direct messages entry has been defined
if (null != listOfList) {
for (List<String> list : listOfList) {
for (String roomId : list) {
// test if the room is defined once and exists
if ((directChatRoomIdsList.indexOf(roomId) < 0) && (null != store.getRoom(roomId))) {
directChatRoomIdsList.add(roomId);
}
}
}
} else {
// background compatibility heuristic (named looksLikeDirectMessageRoom in the JS)
ArrayList<RoomIdsListRetroCompat> directChatRoomIdsListRetValue = new ArrayList<>();
getDirectChatRoomIdsListRetroCompat(store, directChatRoomIdsListRetValue);
// force direct chat room list to be updated with retro compatibility rooms values
if (0 != directChatRoomIdsListRetValue.size()) {
forceDirectChatRoomValue(directChatRoomIdsListRetValue, new ApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
Log.d(LOG_TAG, "## getDirectChatRoomIdsList(): background compatibility heuristic => account_data update succeed");
}
@Override
public void onMatrixError(MatrixError e) {
if (MatrixError.FORBIDDEN.equals(e.errcode)) {
Log.e(LOG_TAG, "## getDirectChatRoomIdsList(): onMatrixError Msg=" + e.error);
}
}
@Override
public void onNetworkError(Exception e) {
Log.e(LOG_TAG, "## getDirectChatRoomIdsList(): onNetworkError Msg=" + e.getMessage());
}
@Override
public void onUnexpectedError(Exception e) {
Log.e(LOG_TAG, "## getDirectChatRoomIdsList(): onUnexpectedError Msg=" + e.getMessage());
}
});
}
}
return mLocalDirectChatRoomIdsList = directChatRoomIdsList;
}
Aggregations