Search in sources :

Example 6 with IMXStore

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);
}
Also used : IMXStore(org.matrix.androidsdk.data.store.IMXStore)

Example 7 with IMXStore

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;
}
Also used : IMXStore(org.matrix.androidsdk.data.store.IMXStore) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Room(org.matrix.androidsdk.data.Room)

Example 8 with IMXStore

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);
    }
}
Also used : MediaMessage(org.matrix.androidsdk.rest.model.message.MediaMessage) StickerMessage(org.matrix.androidsdk.rest.model.message.StickerMessage) Message(org.matrix.androidsdk.rest.model.message.Message) MediaMessage(org.matrix.androidsdk.rest.model.message.MediaMessage) IMXStore(org.matrix.androidsdk.data.store.IMXStore) AsyncTask(android.os.AsyncTask) Event(org.matrix.androidsdk.rest.model.Event) Room(org.matrix.androidsdk.data.Room) File(java.io.File) HashSet(java.util.HashSet)

Example 9 with IMXStore

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;
}
Also used : MyUser(org.matrix.androidsdk.data.MyUser) IMXStore(org.matrix.androidsdk.data.store.IMXStore)

Example 10 with IMXStore

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;
}
Also used : IMXStore(org.matrix.androidsdk.data.store.IMXStore) ArrayList(java.util.ArrayList) MXDecryptionException(org.matrix.androidsdk.crypto.MXDecryptionException) UnrecognizedCertificateException(org.matrix.androidsdk.ssl.UnrecognizedCertificateException) List(java.util.List) ArrayList(java.util.ArrayList) MatrixError(org.matrix.androidsdk.rest.model.MatrixError)

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