Browse Source

添加接口支持获取远程新消息

heavyrian2012 4 weeks ago
parent
commit
6e2bd6454e

+ 1 - 0
client/src/main/aidl/cn/wildfirechat/client/IRemoteClient.aidl

@@ -135,6 +135,7 @@ interface IRemoteClient {
 
     oneway void getRemoteMessages(in Conversation conversation, in int[] contentTypes, in long beforeMessageUid, in int count, in IGetRemoteMessagesCallback callback);
     oneway void getRemoteMessage(in long messageUid, in IGetRemoteMessagesCallback callback);
+    oneway void getRemoteMessagesEx(in Conversation conversation, in int[] contentTypes, in long beforeMessageUid, in int count, in boolean before, in boolean saveToDb, in IGetRemoteMessagesCallback callback);
     oneway void getConversationFileRecords(in Conversation conversation, in String fromUser, in long beforeMessageUid, in int order, in int count, in IGetFileRecordCallback callback);
     oneway void getMyFileRecords(in long beforeMessageUid, in int order, in int count, in IGetFileRecordCallback callback);
     oneway void deleteFileRecord(in long messageUid, in IGeneralCallback callback);

+ 34 - 0
client/src/main/java/cn/wildfirechat/client/ClientService.java

@@ -1078,6 +1078,40 @@ public class ClientService extends Service implements SdtLogic.ICallBack,
             });
         }
 
+        @Override
+        public void getRemoteMessagesEx(Conversation conversation, int[] contentTypes, long beforeMessageUid, int count, boolean before, boolean saveToDb, IGetRemoteMessagesCallback callback) throws RemoteException {
+            if (contentTypes == null) {
+                contentTypes = new int[0];
+            }
+
+            ProtoLogic.getRemoteMessagesEx(conversation.type.ordinal(), conversation.target, conversation.line, beforeMessageUid, count, contentTypes, before, saveToDb, new ProtoLogic.ILoadRemoteMessagesCallback() {
+                @Override
+                public void onSuccess(ProtoMessage[] list) {
+                    Message[] messages = convertProtoMessages(list);
+                    try {
+                        SafeIPCEntry<Message> entry;
+                        int startIndex = 0;
+                        do {
+                            entry = buildSafeIPCEntry(messages, startIndex);
+                            callback.onSuccess(entry.entries, entry.entries.size() > 0 && entry.index > 0 && entry.index < messages.length - 1);
+                            startIndex = entry.index + 1;
+                        } while (entry.index > 0 && entry.index < messages.length - 1);
+                    } catch (RemoteException e) {
+                        e.printStackTrace();
+                    }
+                }
+
+                @Override
+                public void onFailure(int i) {
+                    try {
+                        callback.onFailure(i);
+                    } catch (RemoteException e) {
+                        e.printStackTrace();
+                    }
+                }
+            });
+        }
+
         private List<FileRecord> convertProtoFileRecord(ProtoFileRecord[] protoFileRecords) {
             List<FileRecord> records = new ArrayList<>();
             for (ProtoFileRecord pfr : protoFileRecords) {

+ 51 - 0
client/src/main/java/cn/wildfirechat/remote/ChatManager.java

@@ -3902,6 +3902,57 @@ public class ChatManager {
         }
     }
 
+    /**
+     * 获取服务器消息,可以获取对应messageUid之前或者之后的消息。**如果消息是不连续的,saveToDb要为false,避免存储在本地,需要特别注意这一点。**
+     *
+     * @param conversation     会话
+     * @param conversation 会话
+     * @param messageUid 起始消息的ID
+     * @param count 总数
+     * @param before 是获取对应messageUid之前或者时候的消息
+     * @param saveToDb 是否保存到本地DB中,如果是不连续的消息,千万要为false。
+     * @param contentTypes 指定获取的类型。
+     * @discussion 获取得到的消息数目有可能少于指定的count数,如果count不为0就意味着还有更多的消息可以获取,只有获取到的消息数为0才表示没有更多的消息了。
+     */
+    public void getRemoteMessages(Conversation conversation, List<Integer> contentTypes, long messageUid, int count, boolean before, boolean saveToDb, GetRemoteMessageCallback callback) {
+        if (!checkRemoteService()) {
+            return;
+        }
+
+        try {
+            int[] intypes = null;
+            if (contentTypes != null && !contentTypes.isEmpty()) {
+                intypes = new int[contentTypes.size()];
+                for (int i = 0; i < contentTypes.size(); i++) {
+                    intypes[i] = contentTypes.get(i);
+                }
+            }
+            List<Message> outMsgs = new ArrayList<>();
+            mClient.getRemoteMessagesEx(conversation, intypes, messageUid, count, before, saveToDb, new IGetRemoteMessagesCallback.Stub() {
+                @Override
+                public void onSuccess(List<Message> messages, boolean hasMore) throws RemoteException {
+                    if (callback != null) {
+                        outMsgs.addAll(messages);
+                        if (!hasMore) {
+                            mainHandler.post(() -> callback.onSuccess(outMsgs));
+                        }
+                    }
+                }
+
+                @Override
+                public void onFailure(int errorCode) throws RemoteException {
+                    if (callback != null) {
+                        mainHandler.post(() -> {
+                            callback.onFail(errorCode);
+                        });
+                    }
+                }
+            });
+        } catch (RemoteException e) {
+            e.printStackTrace();
+        }
+    }
+
     /**
      * 获取远程文件记录
      *

BIN
mars-core-release/mars-core-release.aar