NIMSDK-AOS  10.6.0
ChatRoomMessageBuilder.java
浏览该文件的文档.
1 package com.netease.nimlib.sdk.chatroom;
2 
3 import android.content.Context;
4 import android.net.Uri;
5 import android.text.TextUtils;
6 
7 import com.netease.nimlib.NimNosSceneKeyConstant;
8 import com.netease.nimlib.SDKCacheUI;
9 import com.netease.nimlib.chatroom.model.ChatRoomMessageImpl;
25 import com.netease.nimlib.util.BitmapDecoder;
26 import com.netease.nimlib.util.StringUtil;
27 import com.netease.nimlib.util.TimeUtil;
28 
29 import java.io.File;
30 
34 public class ChatRoomMessageBuilder {
35 
43  public static ChatRoomMessage createChatRoomTextMessage(String roomId, String text) {
44  ChatRoomMessageImpl msg = initSendMessage(roomId);
45  msg.setMsgType(MsgTypeEnum.text.getValue());
46  msg.setContent(text);
47 
48  return msg;
49  }
50 
58  public static ChatRoomMessage createChatRoomCustomMessage(String roomId, MsgAttachment attachment) {
59  return createChatRoomCustomMessage(roomId, attachment, NimNosSceneKeyConstant.NIM_DEFAULT_IM);
60  }
61 
70  public static ChatRoomMessage createChatRoomCustomMessage(String roomId, MsgAttachment attachment, String nosTokenScene) {
71  ChatRoomMessageImpl msg = initSendMessage(roomId);
72  msg.setMsgType(MsgTypeEnum.custom.getValue());
73  if (attachment != null && attachment instanceof FileAttachment) {
74  ((FileAttachment) attachment).setNosTokenSceneKey(nosTokenScene);
75  }
76  msg.setAttachment(attachment);
77 
78  return msg;
79  }
80 
89  public static ChatRoomMessage createChatRoomImageMessage(String roomId, File file, String displayName) {
90  return createChatRoomImageMessage(roomId, file, displayName, NimNosSceneKeyConstant.NIM_DEFAULT_IM);
91  }
92 
102  public static ChatRoomMessage createChatRoomImageMessage(Context context,String roomId, Uri uri, String displayName) {
103  return createChatRoomImageMessage(context,roomId, uri, displayName, NimNosSceneKeyConstant.NIM_DEFAULT_IM);
104  }
105 
106 
107 
117  public static ChatRoomMessage createChatRoomImageMessage(String roomId, File file, String displayName, String nosTokenSceneKey) {
118  ChatRoomMessageImpl msg = initSendMessage(roomId);
119  msg.setMsgType(MsgTypeEnum.image.getValue());
120 
121  final ImageAttachment attachment = MessageBuilder.createImageAttachment(file, displayName,nosTokenSceneKey);
122  msg.setAttachment(attachment);
123 
124  return msg;
125  }
126 
136  public static ChatRoomMessage createChatRoomImageMessage(Context context,String roomId, Uri uri, String displayName, String nosTokenSceneKey) {
137  ChatRoomMessageImpl msg = initSendMessage(roomId);
138  msg.setMsgType(MsgTypeEnum.image.getValue());
139  final ImageAttachment attachment = MessageBuilder.createImageAttachment(context,uri,displayName,nosTokenSceneKey);
140  msg.setAttachment(attachment);
141 
142  return msg;
143  }
144 
145 
154  public static ChatRoomMessage createChatRoomAudioMessage(String roomId, File file, long duration) {
155  return createChatRoomAudioMessage(roomId, file, duration, NimNosSceneKeyConstant.NIM_DEFAULT_IM);
156  }
157 
167  public static ChatRoomMessage createChatRoomAudioMessage(Context context,String roomId, Uri uri, long duration) {
168  return createChatRoomAudioMessage(context,roomId, uri, duration, NimNosSceneKeyConstant.NIM_DEFAULT_IM);
169  }
170 
180  public static ChatRoomMessage createChatRoomAudioMessage(String roomId, File file, long duration, String nosTokenSceneKey) {
181  ChatRoomMessageImpl msg = initSendMessage(roomId);
182  msg.setMsgType(MsgTypeEnum.audio.getValue());
183  if (duration > 0 && duration < 1000) {
184  duration = 1000; // 最低显示1秒
185  }
186 
187  final AudioAttachment attachment = MessageBuilder.createAudioAttachment(file,duration,nosTokenSceneKey);
188  msg.setAttachment(attachment);
189 
190  return msg;
191  }
192 
203  public static ChatRoomMessage createChatRoomAudioMessage(Context context,String roomId, Uri uri, long duration, String nosTokenSceneKey) {
204  ChatRoomMessageImpl msg = initSendMessage(roomId);
205  msg.setMsgType(MsgTypeEnum.audio.getValue());
206  if (duration > 0 && duration < 1000) {
207  duration = 1000; // 最低显示1秒
208  }
209  final AudioAttachment attachment = MessageBuilder.createAudioAttachment(context,uri,duration,nosTokenSceneKey);
210  msg.setAttachment(attachment);
211 
212  return msg;
213  }
214 
224  public static ChatRoomMessage createChatRoomLocationMessage(String roomId, double lat, double lng, String addr) {
225  ChatRoomMessageImpl msg = initSendMessage(roomId);
226  msg.setMsgType(MsgTypeEnum.location.getValue());
227 
228  final LocationAttachment location = new LocationAttachment();
229  location.setLatitude(lat);
230  location.setLongitude(lng);
231  location.setAddress(addr);
232  msg.setAttachStatus(AttachStatusEnum.transferred);
233  msg.setAttachment(location);
234 
235  return msg;
236  }
237 
249  public static ChatRoomMessage createChatRoomVideoMessage(String roomId, File file, long duration, int width, int height, String displayName) {
250  return createChatRoomVideoMessage(roomId, file, duration, width, height, displayName, NimNosSceneKeyConstant.NIM_DEFAULT_IM);
251  }
252 
265  public static ChatRoomMessage createChatRoomVideoMessage(Context context,String roomId, Uri uri, long duration, int width, int height, String displayName) {
266  return createChatRoomVideoMessage(context,roomId, uri, duration, width, height, displayName, NimNosSceneKeyConstant.NIM_DEFAULT_IM);
267  }
268 
281  public static ChatRoomMessage createChatRoomVideoMessage(String roomId, File file, long duration, int width, int height, String displayName, String nosTokenSceneKey) {
282  ChatRoomMessageImpl msg = initSendMessage(roomId);
283  msg.setMsgType(MsgTypeEnum.video.getValue());
284 
285  final VideoAttachment attachment = MessageBuilder.createVideoAttachment(file, duration, width, height, displayName,nosTokenSceneKey);
286  msg.setAttachment(attachment);
287 
288  BitmapDecoder.extractThumbnail(file.getPath(), attachment.getThumbPathForSave());
289 
290  return msg;
291  }
292 
306  @SuppressWarnings("java:S107")
307  public static ChatRoomMessage createChatRoomVideoMessage(Context context,String roomId, Uri uri, long duration, int width, int height, String displayName, String nosTokenSceneKey) {
308  ChatRoomMessageImpl msg = initSendMessage(roomId);
309  msg.setMsgType(MsgTypeEnum.video.getValue());
310 
311  final VideoAttachment attachment = MessageBuilder.createVideoAttachment(context,uri, duration, width, height, displayName,nosTokenSceneKey);
312  msg.setAttachment(attachment);
313 
314  BitmapDecoder.extractThumbnail(uri, attachment.getThumbPathForSave());
315 
316  return msg;
317  }
318 
327  public static ChatRoomMessage createChatRoomFileMessage(String roomId, File file, String displayName) {
328  return createChatRoomFileMessage(roomId, file, displayName, NimNosSceneKeyConstant.NIM_DEFAULT_IM);
329  }
330 
340  public static ChatRoomMessage createChatRoomFileMessage(Context context,String roomId, Uri uri, String displayName) {
341  ChatRoomMessageImpl msg = initSendMessage(roomId);
342  msg.setMsgType(MsgTypeEnum.file.getValue());
343 
344  final FileAttachment attachment = MessageBuilder.createFileAttachment(context, uri, displayName, NimNosSceneKeyConstant.NIM_DEFAULT_IM);
345  msg.setAttachment(attachment);
346  return msg;
347  }
348 
358  public static ChatRoomMessage createChatRoomFileMessage(String roomId, File file, String displayName, String nosTokenSceneKey) {
359  ChatRoomMessageImpl msg = initSendMessage(roomId);
360  msg.setMsgType(MsgTypeEnum.file.getValue());
361 
362  final FileAttachment attachment = MessageBuilder.createFileAttachment(file, displayName, nosTokenSceneKey);
363  msg.setAttachment(attachment);
364  return msg;
365  }
366 
377  public static ChatRoomMessage createChatRoomFileMessage(Context context,String roomId, Uri uri, String displayName, String nosTokenSceneKey) {
378  ChatRoomMessageImpl msg = initSendMessage(roomId);
379  msg.setMsgType(MsgTypeEnum.file.getValue());
380 
381  final FileAttachment attachment = MessageBuilder.createFileAttachment(context, uri, displayName, nosTokenSceneKey);
382  msg.setAttachment(attachment);
383  return msg;
384  }
385 
396  public static ChatRoomMessage createChatRoomSpatialLocationTextMessage(String roomId, String text,Double x,Double y,Double z) {
397  ChatRoomMessageImpl msg = initSendMessage(roomId);
398  msg.setMsgType(MsgTypeEnum.text.getValue());
399  msg.setContent(text);
400  msg.setLocX(x);
401  msg.setLocY(y);
402  msg.setLocZ(z);
403  return msg;
404  }
405 
412  public static ChatRoomMessage createTipMessage(String roomId) {
413  ChatRoomMessageImpl msg = initSendMessage(roomId);
414  msg.setMsgType(MsgTypeEnum.tip.getValue());
415 
416  return msg;
417  }
418 
429  public static ChatRoomMessage createRobotMessage(String roomId, String robotAccount, String text, String type, String content, String target, String params) {
430  if (TextUtils.isEmpty(type) || TextUtils.isEmpty(robotAccount)) {
431  throw new IllegalArgumentException("Invalid param, type and robot account should not be null");
432  }
433 
434  if (type.equals(RobotMsgType.TEXT) && content == null) {
435  throw new IllegalArgumentException("Invalid param, content should not be null");
436  } else if (type.equals(RobotMsgType.LINK) && TextUtils.isEmpty(target)) {
437  throw new IllegalArgumentException("Invalid param, target should not be null");
438  }
439 
440  ChatRoomMessageImpl msg = initSendMessage(roomId);
441  msg.setMsgType(MsgTypeEnum.robot.getValue());
442 
443  // build attachment
444  RobotAttachment robotAttachment = new RobotAttachment();
445  robotAttachment.initSend(robotAccount, type, content, target, params);
446  msg.setAttachment(robotAttachment);
447  msg.setContent(text); // 文案显示用,便于全文检索等
448 
449  return msg;
450  }
451 
458  public static ChatRoomMessage createEmptyChatRoomMessage(String roomId, long time) {
459  ChatRoomMessageImpl msg = new ChatRoomMessageImpl();
460  msg.setSessionId(roomId);
461  msg.setSessionType(SessionTypeEnum.ChatRoom);
462  msg.setTime(time);
463 
464  return msg;
465  }
466 
467  private final static ChatRoomMessageImpl initSendMessage(String roomId) {
468  ChatRoomMessageImpl msg = new ChatRoomMessageImpl();
469  msg.setUuid(StringUtil.get32UUID());
470  msg.setSessionId(roomId);
471  msg.setFromAccount(SDKCacheUI.getChatRoomAccount());
472  msg.setDirect(MsgDirectionEnum.Out);
473  msg.setStatus(MsgStatusEnum.sending);
474  msg.setSessionType(SessionTypeEnum.ChatRoom);
475  msg.setTime(TimeUtil.currentTimeMillis());
476 
477  return msg;
478  }
479 }
static ChatRoomMessage createChatRoomSpatialLocationTextMessage(String roomId, String text, Double x, Double y, Double z)
创建普通文本消息
static ChatRoomMessage createChatRoomTextMessage(String roomId, String text)
创建普通文本消息
static ChatRoomMessage createChatRoomVideoMessage(String roomId, File file, long duration, int width, int height, String displayName, String nosTokenSceneKey)
创建一条视频消息 并指定视频文件上传时使用的 nos scene
static ChatRoomMessage createChatRoomCustomMessage(String roomId, MsgAttachment attachment, String nosTokenScene)
创建自定义消息 另外指定指定文件上传(如果有)时使用的 nos scene
.annotation.NonNull static FileAttachment createFileAttachment(File file, String displayName, String nosTokenSceneKey)
创建文件附件。用于上传文件。
static ChatRoomMessage createChatRoomImageMessage(Context context, String roomId, Uri uri, String displayName)
创建一条图片消息
static ChatRoomMessage createChatRoomAudioMessage(String roomId, File file, long duration, String nosTokenSceneKey)
创建一条音频消息 并指定音频文件上传时使用的 nos scene
static ChatRoomMessage createChatRoomImageMessage(Context context, String roomId, Uri uri, String displayName, String nosTokenSceneKey)
创建一条图片消息 并指定图片上传时使用的 nos scene
.annotation.NonNull static VideoAttachment createVideoAttachment(File file, long duration, int width, int height, String displayName, String nosTokenSceneKey)
创建视频附件。用于上传视频文件。
云信 IM 消息构造器,提供构建各类型消息的接口。
带有文件的附件类型的基类 描述文件的相关信息
消息本身发送/接收状态,附件状态见AttachStatusEnum
static ChatRoomMessage createChatRoomImageMessage(String roomId, File file, String displayName, String nosTokenSceneKey)
创建一条图片消息 并指定图片上传时使用的 nos scene
static ChatRoomMessage createChatRoomFileMessage(String roomId, File file, String displayName)
创建一条文件消息
static ChatRoomMessage createTipMessage(String roomId)
创建一条提醒消息
static ChatRoomMessage createChatRoomVideoMessage(String roomId, File file, long duration, int width, int height, String displayName)
创建一条视频消息
.annotation.NonNull static AudioAttachment createAudioAttachment(File file, long duration, String nosTokenSceneKey)
创建音频附件。用于上传音频文件。
.annotation.NonNull static ImageAttachment createImageAttachment(File file, String displayName, String nosTokenSceneKey)
创建图片附件,用于上传图片文件。
void initSend(String robotAccount, String type, String content, String target, String params)
String getThumbPathForSave()
获取用于保存缩略图文件的位置
static ChatRoomMessage createChatRoomFileMessage(Context context, String roomId, Uri uri, String displayName, String nosTokenSceneKey)
创建一条文件消息 并指定文件上传时使用的 nos scene
static ChatRoomMessage createChatRoomVideoMessage(Context context, String roomId, Uri uri, long duration, int width, int height, String displayName)
创建一条视频消息
void setAddress(String address)
设置地理位置描述信息
static ChatRoomMessage createEmptyChatRoomMessage(String roomId, long time)
创建一条空消息,仅设置了房间ID以及时间点,用于记录查询
static ChatRoomMessage createChatRoomCustomMessage(String roomId, MsgAttachment attachment)
创建自定义消息
static ChatRoomMessage createChatRoomAudioMessage(String roomId, File file, long duration)
创建一条音频消息
static ChatRoomMessage createChatRoomLocationMessage(String roomId, double lat, double lng, String addr)
创建一条地理位置信息
static ChatRoomMessage createChatRoomFileMessage(String roomId, File file, String displayName, String nosTokenSceneKey)
创建一条文件消息 并指定文件上传时使用的 nos scene
static ChatRoomMessage createChatRoomFileMessage(Context context, String roomId, Uri uri, String displayName)
创建一条文件消息 并指定文件上传时使用的 nos scene
static ChatRoomMessage createChatRoomAudioMessage(Context context, String roomId, Uri uri, long duration, String nosTokenSceneKey)
创建一条音频消息 并指定音频文件上传时使用的 nos scene
static ChatRoomMessage createRobotMessage(String roomId, String robotAccount, String text, String type, String content, String target, String params)
static ChatRoomMessage createChatRoomImageMessage(String roomId, File file, String displayName)
创建一条图片消息
static ChatRoomMessage createChatRoomAudioMessage(Context context, String roomId, Uri uri, long duration)
创建一条音频消息