Skip to content

Commit

Permalink
Merge pull request #39 from jpush/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
KenChoi1992 authored Jan 17, 2018
2 parents 379e403 + e7e6e33 commit 3d252f0
Show file tree
Hide file tree
Showing 30 changed files with 1,687 additions and 1,308 deletions.
Binary file removed android/libs/jmessage-android_v2.3.0.jar
Binary file not shown.
Binary file added android/libs/jmessage-android_v2.4.1.jar
Binary file not shown.
22 changes: 22 additions & 0 deletions android/src/io/jchat/android/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ public class Constant {
public static final String OLD_PWD = "oldPwd";
public static final String NEW_PWD = "newPwd";
public static final String APP_KEY = "appKey";

/**
* UserInfo
*/
public static final String NICKNAME = "nickname";
public static final String GENDER = "gender";
public static final String AVATAR_THUMB_PATH = "avatarThumbPath";
Expand All @@ -20,9 +24,14 @@ public class Constant {
public static final String IS_NO_DISTURB = "isNoDisturb";
public static final String IS_IN_BLACKLIST = "isInBlackList";
public static final String IS_FRIEND = "isFriend";

/**
* Conversation
*/
public static final String TYPE_USER = "user";
public static final String TYPE_GROUP = "group";
public static final String TYPE_SINGLE = "single";
public static final String TYPE_CHAT_ROOM = "chatroom";
public static final String ID = "id";
public static final String SERVER_ID = "serverMessageId";
public static final String MESSAGE_ID = "messageId";
Expand All @@ -38,6 +47,10 @@ public class Constant {
public static final String LATEST_MESSAGE = "latestMessage";
public static final String UNREAD_COUNT = "unreadCount";
public static final String CONVERSATION_TYPE = "conversationType";

/**
* Message
*/
public static final String TARGET = "target";
public static final String TYPE = "type";
static final String MESSAGE_TYPE = "messageType";
Expand All @@ -52,6 +65,7 @@ public class Constant {
public static final String CUSTOM = "custom";
public static final String EXTRAS = "extras";
public static final String THUMB_PATH = "thumbPath";
public static final String LOCAL_PATH = "localPath";
public static final String PATH = "path";
public static final String DURATION = "duration";
public static final String LONGITUDE = "longitude";
Expand All @@ -78,4 +92,12 @@ public class Constant {
static final String RETRACT_MESSAGE = "retractedMessage";
static final String MESSAGE_ARRAY = "messageArray";
static final String PROGRESS = "progress";
public static final String UNRECEIPT_COUNT = "unreceiptCount";
/**
* ChatRoom
*/
public static final String ROOM_ID = "roomId";
public static final String ROOM_IDS = "roomIds";
public static final String ROOM_NAME = "roomName";
public static final String MEMBER_COUNT = "memberCount";
}
195 changes: 190 additions & 5 deletions android/src/io/jchat/android/JMessageModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


import cn.jpush.im.android.api.ChatRoomManager;
import cn.jpush.im.android.api.ContactManager;
import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.CreateGroupCallback;
Expand All @@ -43,6 +46,7 @@
import cn.jpush.im.android.api.callback.GetUserInfoListCallback;
import cn.jpush.im.android.api.callback.IntegerCallback;
import cn.jpush.im.android.api.callback.ProgressUpdateCallback;
import cn.jpush.im.android.api.callback.RequestCallback;
import cn.jpush.im.android.api.content.CustomContent;
import cn.jpush.im.android.api.content.FileContent;
import cn.jpush.im.android.api.content.ImageContent;
Expand All @@ -51,13 +55,15 @@
import cn.jpush.im.android.api.content.TextContent;
import cn.jpush.im.android.api.content.VoiceContent;
import cn.jpush.im.android.api.enums.ContentType;
import cn.jpush.im.android.api.event.ChatRoomMessageEvent;
import cn.jpush.im.android.api.event.ContactNotifyEvent;
import cn.jpush.im.android.api.event.ConversationRefreshEvent;
import cn.jpush.im.android.api.event.LoginStateChangeEvent;
import cn.jpush.im.android.api.event.MessageEvent;
import cn.jpush.im.android.api.event.MessageRetractEvent;
import cn.jpush.im.android.api.event.NotificationClickEvent;
import cn.jpush.im.android.api.event.OfflineMessageEvent;
import cn.jpush.im.android.api.model.ChatRoomInfo;
import cn.jpush.im.android.api.model.Conversation;
import cn.jpush.im.android.api.model.GroupInfo;
import cn.jpush.im.android.api.model.Message;
Expand All @@ -78,6 +84,7 @@ public class JMessageModule extends ReactContextBaseJavaModule {
private static final String RETRACT_MESSAGE_EVENT = "JMessage.MessageRetract"; // 消息撤回事件
private static final String CONTACT_NOTIFY_EVENT = "JMessage.ContactNotify"; // 收到好友请求消息事件
private static final String UPLOAD_PROGRESS_EVENT = "JMessage.UploadProgress"; // 上传(图片,文件等)进度事件
private static final String RECEIVE_CHAT_ROOM_MSG_EVENT = "JMessage.ReceiveChatRoomMsgEvent"; // 收到聊天室消息事件
private static final int ERR_CODE_PARAMETER = 1;
private static final int ERR_CODE_CONVERSATION = 2;
private static final int ERR_CODE_MESSAGE = 3;
Expand Down Expand Up @@ -301,10 +308,11 @@ public void createSendMessage(ReadableMap map, Callback callback) {
content = new ImageContent(new File(path));
} else if (type.equals(Constant.VOICE)) {
String path = map.getString(Constant.PATH);
File file = new File(map.getString(path));
File file = new File(path);
MediaPlayer mediaPlayer = MediaPlayer.create(mContext, Uri.parse(path));
int duration = mediaPlayer.getDuration() / 1000; // Millisecond to second.
content = new VoiceContent(file, duration);
content = new VoiceContent(file,
duration);
mediaPlayer.release();
} else if (type.equals(Constant.LOCATION)) {
double latitude = map.getDouble(Constant.LATITUDE);
Expand All @@ -315,6 +323,9 @@ public void createSendMessage(ReadableMap map, Callback callback) {
} else {
content = new CustomContent();
}
if (map.hasKey(Constant.EXTRAS)) {
content.setExtras(ResultUtils.fromMap(map.getMap(Constant.EXTRAS)));
}
if (type.equals(Constant.CUSTOM)) {
CustomContent customContent = new CustomContent();
customContent.setAllValues(ResultUtils.fromMap(map.getMap(Constant.CUSTOM_OBJECT)));
Expand All @@ -334,7 +345,7 @@ public void createSendMessage(ReadableMap map, Callback callback) {
public void sendMessage(ReadableMap map, final Callback success, final Callback fail) {
try {
Conversation conversation = mJMessageUtils.getConversation(map);
final Message message = conversation.getMessage(map.getInt(Constant.ID));
final Message message = conversation.getMessage(Integer.parseInt(map.getString(Constant.ID)));
if (map.hasKey(Constant.SENDING_OPTIONS)) {
MessageSendingOptions options = new MessageSendingOptions();
ReadableMap optionMap = map.getMap(Constant.SENDING_OPTIONS);
Expand Down Expand Up @@ -1119,9 +1130,12 @@ public void deleteConversation(ReadableMap map, final Callback success, final Ca
String username = map.getString(Constant.USERNAME);
String appKey = map.hasKey(Constant.APP_KEY) ? map.getString(Constant.APP_KEY) : "";
JMessageClient.deleteSingleConversation(username, appKey);
} else {
} else if (type.equals(Constant.TYPE_GROUP)) {
long groupId = Long.parseLong(map.getString(Constant.GROUP_ID));
JMessageClient.deleteGroupConversation(groupId);
} else {
String roomId = map.getString(Constant.ROOM_ID);
JMessageClient.deleteChatRoomConversation(Long.parseLong(roomId));
}
mJMessageUtils.handleCallback(0, "", success, fail);
} catch (Exception e) {
Expand Down Expand Up @@ -1382,7 +1396,7 @@ public void setConversationExtras(ReadableMap map, Callback success, Callback fa
ReadableMap extraMap = map.getMap(Constant.EXTRAS);
ReadableMapKeySetIterator iterator = extraMap.keySetIterator();
JsonObject jsonObject = new JsonObject();
while(iterator.hasNextKey()) {
while (iterator.hasNextKey()) {
String key = iterator.nextKey();
jsonObject.addProperty(key, extraMap.getString(key));
}
Expand Down Expand Up @@ -1447,6 +1461,166 @@ public void gotResult(int status, String desc) {
}
}

/**
* 查询当前应用 AppKey 下的聊天室信息
*
* @param param 包含起始位置,获取个数
* @param success 成功回调
* @param fail 失败回调
*/
@ReactMethod
public void getChatRoomListByApp(ReadableMap param, final Callback success, final Callback fail) {
try {
int start = param.getInt("start");
int count = param.getInt("count");
ChatRoomManager.getChatRoomListByApp(start, count, new RequestCallback<List<ChatRoomInfo>>() {
@Override
public void gotResult(int status, String desc, List<ChatRoomInfo> chatRoomInfos) {
mJMessageUtils.handleCallbackWithArray(status, desc, success, fail,
ResultUtils.toJSArray(chatRoomInfos));
}
});
} catch (Exception e) {
e.printStackTrace();
mJMessageUtils.handleError(fail, ERR_CODE_PARAMETER, ERR_MSG_PARAMETER);
}
}

/**
* 获取当前用户所加入的所有聊天室信息
*
* @param success 成功回调
* @param fail 失败回调
*/
@ReactMethod
public void getChatRoomListByUser(final Callback success, final Callback fail) {
ChatRoomManager.getChatRoomListByUser(new RequestCallback<List<ChatRoomInfo>>() {
@Override
public void gotResult(int status, String desc, List<ChatRoomInfo> list) {
mJMessageUtils.handleCallbackWithArray(status, desc, success, fail, ResultUtils.toJSArray(list));
}
});
}

/**
* 查询指定 roomId 聊天室信息
* @param map 包含待查询 roomId
* @param success 成功回调
* @param fail 失败回调
*/
@ReactMethod
public void getChatRoomInfos(ReadableMap map, final Callback success, final Callback fail) {
try {
ReadableArray array = map.getArray(Constant.ROOM_IDS);
Set<Long> idSet = new HashSet<>();
for (int i=0; i < array.size() -1; i++) {
long id = Long.parseLong(array.getString(i));
idSet.add(id);
}
ChatRoomManager.getChatRoomInfos(idSet, new RequestCallback<List<ChatRoomInfo>>() {
@Override
public void gotResult(int status, String desc, List<ChatRoomInfo> list) {
mJMessageUtils.handleCallbackWithArray(status, desc, success, fail, ResultUtils.toJSArray(list));
}
});
} catch (Exception e) {
e.printStackTrace();
mJMessageUtils.handleError(fail, ERR_CODE_PARAMETER, ERR_MSG_PARAMETER);
}
}

/**
* 获取聊天室拥有者 UserInfo
* @param map 包含聊天室 id
* @param success 成功回调
* @param fail 失败回调
*/
@ReactMethod
public void getChatRoomOwner(ReadableMap map, final Callback success, final Callback fail) {
try {
long id = Long.parseLong(map.getString(Constant.ROOM_ID));
Set<Long> set = new HashSet<>();
set.add(id);
ChatRoomManager.getChatRoomInfos(set, new RequestCallback<List<ChatRoomInfo>>() {
@Override
public void gotResult(int status, String desc, List<ChatRoomInfo> list) {
list.get(0).getOwnerInfo(new GetUserInfoCallback() {
@Override
public void gotResult(int status, String desc, UserInfo userInfo) {
mJMessageUtils.handleCallbackWithObject(status, desc, success, fail, ResultUtils.toJSObject(userInfo));
}
});
}
});
} catch (Exception e) {
e.printStackTrace();
mJMessageUtils.handleError(fail, ERR_CODE_PARAMETER, ERR_MSG_PARAMETER);
}
}

/**
* 进入聊天室,进入后才能收到聊天室信息及发言
* @param map 包含聊天室 id
* @param success 成功回调
* @param fail 失败回调
*/
@ReactMethod
public void enterChatRoom(ReadableMap map, final Callback success, final Callback fail) {
ChatRoomManager.enterChatRoom(Long.parseLong(map.getString(Constant.ROOM_ID)), new RequestCallback<Conversation>() {
@Override
public void gotResult(int status, String desc, Conversation conversation) {
mJMessageUtils.handleCallbackWithObject(status, desc, success, fail, ResultUtils.toJSObject(conversation));
}
});
}

/**
* 离开聊天室
* @param map 包含聊天室 id
* @param success 成功回调
* @param fail 失败回调
*/
@ReactMethod
public void leaveChatRoom(ReadableMap map, final Callback success, final Callback fail) {
ChatRoomManager.leaveChatRoom(Long.parseLong(map.getString(Constant.ROOM_ID)), new BasicCallback() {
@Override
public void gotResult(int i, String s) {
mJMessageUtils.handleCallback(i, s, success, fail);
}
});
}

/**
* 从本地获取用户的聊天室会话列表,没有则返回为空的列表
* @param success 成功回调
*/
@ReactMethod
public void getChatRoomConversationList(Callback success) {
List<Conversation> list = JMessageClient.getChatRoomConversationList();
success.invoke(ResultUtils.toJSArray(list));
}

/**
* 删除聊天室会话,同时删除本地相关缓存文件。成功返回 true,失败返回 false
* @param roomId 聊天室 id
*/
@ReactMethod
public void deleteChatRoomConversation(String roomId, Callback success) {
success.invoke(JMessageClient.deleteChatRoomConversation(Long.parseLong(roomId)));
}

/**
* 创建聊天室会话,如果本地已存在,则不会重新创建,直接返回该会话
* @param roomId 聊天室 id
*/
@ReactMethod
public void createChatRoomConversation(String roomId, Callback success) {
Conversation conversation = Conversation.createChatRoomConversation(Long.parseLong(roomId));
success.invoke(ResultUtils.toJSObject(conversation));
}



public void onEvent(LoginStateChangeEvent event) {
Log.d(TAG, "登录状态改变事件:event = " + event.toString());
WritableMap map = Arguments.createMap();
Expand Down Expand Up @@ -1576,4 +1750,15 @@ public void onComplete(int status, String desc, File file) {
}
}
}

/**
* 聊天室消息事件
* @param event {@link ChatRoomMessageEvent}
*/
public void onEventMainThread(ChatRoomMessageEvent event) {
List<Message> list = event.getMessages();
Log.d(TAG, "收到聊天室消息");
getReactApplicationContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(RECEIVE_MSG_EVENT, ResultUtils.toJSArray(list));
}
}
11 changes: 8 additions & 3 deletions android/src/io/jchat/android/utils/JMessageUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,22 @@ public Conversation getConversation(ReadableMap map) {
String appKey = "";
if (map.hasKey(Constant.APP_KEY)) {
appKey = map.getString(Constant.APP_KEY);
conversation = Conversation.createSingleConversation(username, appKey);
} else {
conversation = Conversation.createSingleConversation(username);
}
conversation = Conversation.createSingleConversation(username, appKey);
} else {
} else if (type.equals(Constant.TYPE_GROUP)) {
String groupId = map.getString(Constant.GROUP_ID);
conversation = Conversation.createGroupConversation(Long.parseLong(groupId));
} else {
String roomId = map.getString(Constant.ROOM_ID);
conversation = Conversation.createChatRoomConversation(Long.parseLong(roomId));
}
return conversation;
}

public void sendMessage(ReadableMap map, MessageContent content,
final Callback success, final Callback fail) {
final Callback success, final Callback fail) {
if (map.hasKey(Constant.EXTRAS)) {
content.setExtras(ResultUtils.fromMap(map.getMap(Constant.EXTRAS)));
}
Expand Down
Loading

0 comments on commit 3d252f0

Please sign in to comment.