Commit 9a127eb37547592c327191bab906c1599f04751b

Authored by 谷俊
1 parent a37e8345

区分旧版和3.0版本demo

app/src/main/AndroidManifest.xml
@@ -48,6 +48,14 @@ @@ -48,6 +48,14 @@
48 <activity android:name=".ui.PrivateActivity" 48 <activity android:name=".ui.PrivateActivity"
49 android:screenOrientation="portrait"/> 49 android:screenOrientation="portrait"/>
50 50
  51 + <activity
  52 + android:name=".ui.ChatRoom3Activity"
  53 + android:theme="@style/Theme.AppCompat.Light.NoActionBar"
  54 + android:windowSoftInputMode="adjustResize" />
  55 +
  56 + <activity android:name=".ui.Private3Activity"
  57 + android:screenOrientation="portrait"/>
  58 +
51 <activity android:name=".ui.VideoActivity" 59 <activity android:name=".ui.VideoActivity"
52 android:screenOrientation="portrait" 60 android:screenOrientation="portrait"
53 android:launchMode="singleTop"/> 61 android:launchMode="singleTop"/>
@@ -91,7 +99,6 @@ @@ -91,7 +99,6 @@
91 <action android:name="android.intent.action.TIMEZONE_CHANGED" /> 99 <action android:name="android.intent.action.TIMEZONE_CHANGED" />
92 </intent-filter> 100 </intent-filter>
93 101
94 - <!-- ImSDK 3.0.2 后添加 -->  
95 <intent-filter> 102 <intent-filter>
96 <action android:name="com.tencent.qalsdk.service.TASK_REMOVED" /> 103 <action android:name="com.tencent.qalsdk.service.TASK_REMOVED" />
97 </intent-filter> 104 </intent-filter>
app/src/main/java/com/cnlive/im/adapter/ChatRoom3Adpter.java 0 → 100644
  1 +package com.cnlive.im.adapter;
  2 +
  3 +import android.content.Context;
  4 +import android.graphics.Color;
  5 +import android.graphics.drawable.AnimationDrawable;
  6 +import android.support.v7.widget.RecyclerView;
  7 +import android.view.LayoutInflater;
  8 +import android.view.View;
  9 +import android.view.ViewGroup;
  10 +import android.widget.ImageView;
  11 +import android.widget.TextView;
  12 +
  13 +import com.bumptech.glide.Glide;
  14 +import com.cnlive.im.R;
  15 +import com.cnlive.im.adapter.base.BaseRecyclerAdapter;
  16 +import com.cnlive.im.adapter.base.ItemClickListener;
  17 +import com.cnlive.im.mode.ImagMessage;
  18 +import com.cnlive.im.mode.StringMessage;
  19 +import com.cnlive.im.mode.UgcVideoMessage;
  20 +import com.cnlive.im.mode.VideoMessage;
  21 +import com.cnlive.im.mode.VoiceMessage;
  22 +import com.cnlive.im.util.FileUtil;
  23 +import com.cnlive.libs.util.chat.base.IChat;
  24 +
  25 +import org.json.JSONException;
  26 +import org.json.JSONObject;
  27 +
  28 +/**
  29 + * Created by gujun on 2017/12/19.
  30 + */
  31 +public class ChatRoom3Adpter extends BaseRecyclerAdapter {
  32 +
  33 + private int stringUser = 1;
  34 + private int stringOppsite = 2;
  35 + private int imaUser = 3;
  36 + private int imaOppsite = 4;
  37 + private int voiceUser = 5;
  38 + private int voiceOppsite = 6;
  39 + private int videoUser = 7;
  40 + private int videoOppsite = 8;
  41 + private int ugcVideoUser = 9;
  42 + private int ugcVideoOppsite = 10;
  43 + private Context mContext;
  44 + private ItemClickListener mItemClickListener;
  45 +
  46 + private boolean isPrivate;
  47 +
  48 + public ChatRoom3Adpter(Context context, boolean isPrivate) {
  49 + super(context);
  50 + this.mContext = context;
  51 + this.isPrivate = isPrivate;
  52 + }
  53 +
  54 +
  55 + public void setonVoiceItemClickListener(ItemClickListener itemClickListener) {
  56 + this.mItemClickListener = itemClickListener;
  57 + }
  58 +
  59 + @Override
  60 + public int getItemViewType(int position) {
  61 + Object o = mItems.get(position);
  62 + if (o instanceof StringMessage) {
  63 + StringMessage o1 = (StringMessage) o;
  64 + boolean userSend = o1.isUserSend();
  65 + if (userSend) return stringUser;
  66 + else return stringOppsite;
  67 + } else if (o instanceof ImagMessage) {
  68 + ImagMessage o1 = (ImagMessage) o;
  69 + if (o1.isUserSend()) return imaUser;
  70 + else return imaOppsite;
  71 + } else if (o instanceof VoiceMessage) {
  72 + VoiceMessage o1 = (VoiceMessage) o;
  73 + if (o1.isUserSend())
  74 + return voiceUser;
  75 + else return voiceOppsite;
  76 +
  77 + } else if (o instanceof VideoMessage) {
  78 + VideoMessage o1 = (VideoMessage) o;
  79 + if (o1.isUserSend())
  80 + return videoUser;
  81 + else return videoOppsite;
  82 +
  83 + } else if (o instanceof UgcVideoMessage) {
  84 + UgcVideoMessage o1 = (UgcVideoMessage) o;
  85 + if (o1.isUserSend())
  86 + return ugcVideoUser;
  87 + else return ugcVideoOppsite;
  88 +
  89 + }
  90 + return 0;
  91 + }
  92 +
  93 + @Override
  94 + public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  95 + if (viewType == imaUser) {
  96 + return new ImaMessageHolder(LayoutInflater.from(mContext).inflate(R.layout.imag_layout_item_user, null));
  97 + } else if (viewType == imaOppsite) {
  98 + return new ImaMessageHolder(LayoutInflater.from(mContext).inflate(R.layout.imag_layout_item_oppsite, null));
  99 + } else if (viewType == stringUser) {
  100 + return new StringMessageHolder(LayoutInflater.from(mContext).inflate(R.layout.string_layout_item_user, null));
  101 + } else if (viewType == stringOppsite) {
  102 + return new StringMessageHolder(LayoutInflater.from(mContext).inflate(R.layout.string_layout_item_oppsite, null));
  103 + } else if (viewType == voiceUser) {
  104 + return new VoiceMessageHolder(LayoutInflater.from(mContext).inflate(R.layout.voice_layout_item_user, null));
  105 + } else if (viewType == voiceOppsite) {
  106 + return new VoiceMessageHolder(LayoutInflater.from(mContext).inflate(R.layout.voice_layout_item_oppsite, null));
  107 + } else if (viewType == videoUser) {
  108 + return new VideoMessageHolder(LayoutInflater.from(mContext).inflate(R.layout.video_layout_item_user, null));
  109 + } else if (viewType == videoOppsite) {
  110 + return new VideoMessageHolder(LayoutInflater.from(mContext).inflate(R.layout.video_layout_item_oppsite, null));
  111 + } else if (viewType == ugcVideoUser) {
  112 + return new VideoMessageHolder(LayoutInflater.from(mContext).inflate(R.layout.video_layout_item_user, null));
  113 + } else if (viewType == ugcVideoOppsite) {
  114 + return new VideoMessageHolder(LayoutInflater.from(mContext).inflate(R.layout.video_layout_item_oppsite, null));
  115 + }
  116 + return null;
  117 + }
  118 +
  119 + @Override
  120 + public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
  121 + if (holder instanceof StringMessageHolder) {
  122 + StringMessageHolder holder1 = (StringMessageHolder) holder;
  123 + Object o = mItems.get(position);
  124 + StringMessage o1 = (StringMessage) o;
  125 + holder1.showMessage(o1);
  126 + } else if (holder instanceof ImaMessageHolder) {
  127 + ImaMessageHolder holder1 = (ImaMessageHolder) holder;
  128 + Object o = mItems.get(position);
  129 + ImagMessage o1 = (ImagMessage) o;
  130 + holder1.showMessage(o1);
  131 + } else if (holder instanceof VoiceMessageHolder) {
  132 + final VoiceMessageHolder holder1 = (VoiceMessageHolder) holder;
  133 + final Object o = mItems.get(position);
  134 + final VoiceMessage o1 = (VoiceMessage) o;
  135 + holder1.showMessage(o1);
  136 + } else if (holder instanceof VideoMessageHolder) {
  137 + final VideoMessageHolder holder1 = (VideoMessageHolder) holder;
  138 + final Object o = mItems.get(position);
  139 + final VideoMessage o1 = (VideoMessage) o;
  140 + holder1.showMessage(o1);
  141 + } else if (holder instanceof UgcVideoMessageHolder) {
  142 + final UgcVideoMessageHolder holder1 = (UgcVideoMessageHolder) holder;
  143 + final Object o = mItems.get(position);
  144 + final UgcVideoMessage o1 = (UgcVideoMessage) o;
  145 + holder1.showMessage(o1);
  146 + }
  147 +
  148 + }
  149 +
  150 + public class StringMessageHolder extends RecyclerView.ViewHolder {
  151 + private TextView user_name;
  152 + private TextView msg_text;
  153 + private TextView status;
  154 +
  155 + public StringMessageHolder(View itemView) {
  156 + super(itemView);
  157 + user_name = (TextView) itemView.findViewById(R.id.user_name);
  158 + msg_text = (TextView) itemView.findViewById(R.id.msg_text);
  159 + status = (TextView) itemView.findViewById(R.id.status);
  160 + }
  161 +
  162 + public void showMessage(StringMessage item) {
  163 + user_name.setText(item.getCnMessage().getUserInfo().getUserName());
  164 +
  165 + switch (item.getCnMessage().getStatus()) {
  166 + case IChat.CN_MSG_STATUS_SENDING:
  167 + if (status != null)
  168 + status.setText("发送中");
  169 + sendMsg(item);
  170 + break;
  171 + case IChat.CN_MSG_STATUS_SEND_FAIL:
  172 + if (status != null)
  173 + status.setText("发送失败");
  174 + sendMsg(item);
  175 + break;
  176 + case IChat.CN_MSG_STATUS_SEND_SUCCESS:
  177 + if (status != null)
  178 + status.setText("发送成功");
  179 + sendMsg(item);
  180 + break;
  181 + }
  182 + }
  183 +
  184 + private void sendMsg(StringMessage item) {
  185 + if (isPrivate) {
  186 + try {
  187 + msg_text.setText((String) (new JSONObject(item.getCnMessage().getContent()).get("message")));
  188 + } catch (JSONException e) {
  189 + e.printStackTrace();
  190 + }
  191 + } else {
  192 + msg_text.setText(item.getCnMessage().getContent());
  193 + }
  194 + msg_text.setTextColor(Color.BLACK);
  195 + }
  196 + }
  197 +
  198 + public class ImaMessageHolder extends RecyclerView.ViewHolder {
  199 +
  200 + private TextView ima_user_name;
  201 + private ImageView img_content;
  202 + private TextView status;
  203 + private TextView uploadProgress;
  204 +
  205 + public ImaMessageHolder(View itemView) {
  206 + super(itemView);
  207 + ima_user_name = (TextView) itemView.findViewById(R.id.img_user_name);
  208 + img_content = (ImageView) itemView.findViewById(R.id.img_content);
  209 + status = (TextView) itemView.findViewById(R.id.status);
  210 + uploadProgress = (TextView) itemView.findViewById(R.id.uploadProgress);
  211 + }
  212 +
  213 + public void showMessage(final ImagMessage imagMessage) {
  214 + ima_user_name.setText(imagMessage.getCnImgMessage().getUserInfo().getUserName());
  215 + img_content.getLayoutParams().height = 200;
  216 + img_content.getLayoutParams().width = 200;
  217 + img_content.setLayoutParams(img_content.getLayoutParams());
  218 + switch (imagMessage.getCnImgMessage().getStatus()) {
  219 + case IChat.CN_MSG_STATUS_SENDING:
  220 + if (status != null)
  221 + status.setText("发送中");
  222 + if (uploadProgress != null) {
  223 + uploadProgress.setVisibility(View.VISIBLE);
  224 + uploadProgress.setText("上传进度:" + imagMessage.getProgress() + "%");
  225 + }
  226 + Glide.with(mContext).load(imagMessage.getCnImgMessage().getImagePath()).into(img_content);
  227 + break;
  228 + case IChat.CN_MSG_STATUS_SEND_FAIL:
  229 + if (status != null)
  230 + status.setText("发送失败");
  231 + if (uploadProgress != null)
  232 + uploadProgress.setVisibility(View.GONE);
  233 + Glide.with(mContext).load(imagMessage.getCnImgMessage().getImagePath()).into(img_content);
  234 + break;
  235 + case IChat.CN_MSG_STATUS_SEND_SUCCESS:
  236 + if (status != null)
  237 + status.setText("发送成功");
  238 + if (uploadProgress != null)
  239 + uploadProgress.setVisibility(View.GONE);
  240 + sendSuccess(imagMessage);
  241 + break;
  242 + }
  243 + }
  244 +
  245 + private void sendSuccess(final ImagMessage imagMessage) {
  246 + if (imagMessage.getCnImgMessage().isSelf()) {
  247 + Glide.with(mContext).load(imagMessage.getCnImgMessage().getImagePath()).into(img_content);
  248 + } else {
  249 + String thumbUuid = imagMessage.getCnImgMessage().getThumbUuid();
  250 + final String imagePath = FileUtil.getCacheFilePath(thumbUuid);
  251 + if (FileUtil.isCacheFileExist(thumbUuid)) {
  252 + if (img_content != null && mContext != null)
  253 + Glide.with(mContext).load(imagePath).into(img_content);
  254 + } else {
  255 + if (!imagMessage.isDownloading()) {
  256 + imagMessage.setDownloading(true);
  257 + imagMessage.getCnImgMessage().getThumbImage(imagePath, new IChat.OnDownloadListener() {
  258 + @Override
  259 + public void onSuccess() {
  260 + imagMessage.setDownloading(false);
  261 + if (img_content != null && mContext != null)
  262 + Glide.with(mContext).load(imagePath).into(img_content);
  263 + }
  264 +
  265 + @Override
  266 + public void onError(int i, String s) {
  267 + imagMessage.setDownloading(false);
  268 + }
  269 + });
  270 + }
  271 + }
  272 + }
  273 + img_content.setOnClickListener(new View.OnClickListener() {
  274 + @Override
  275 + public void onClick(View v) {
  276 + if (mItemClickListener != null)
  277 + mItemClickListener.onImageClick(imagMessage);
  278 + }
  279 + });
  280 + }
  281 + }
  282 +
  283 + public class VoiceMessageHolder extends RecyclerView.ViewHolder {
  284 +
  285 +
  286 + private TextView voice_user_name;
  287 + private ImageView voiceAnim;
  288 + private TextView voice_duration;
  289 + private View voice_none_read;
  290 + private TextView status;
  291 + private TextView uploadProgress;
  292 +
  293 + public VoiceMessageHolder(View itemView) {
  294 + super(itemView);
  295 + voice_user_name = (TextView) itemView.findViewById(R.id.voice_user_name);
  296 + voiceAnim = (ImageView) itemView.findViewById(R.id.voiceAnim);
  297 + voice_duration = (TextView) itemView.findViewById(R.id.voice_duration);
  298 + voice_none_read = itemView.findViewById(R.id.noneRead);
  299 + status = (TextView) itemView.findViewById(R.id.status);
  300 + uploadProgress = (TextView) itemView.findViewById(R.id.uploadProgress);
  301 + }
  302 +
  303 + public void showMessage(final VoiceMessage voiceMessage) {
  304 + voice_user_name.setText(voiceMessage.getCnVoiceMessage().getUserInfo().getUserName());
  305 + int duration = voiceMessage.getCnVoiceMessage().getVoiceTime();
  306 + voice_duration.setText(String.valueOf(duration).concat("\""));
  307 + switch (voiceMessage.getCnVoiceMessage().getStatus()) {
  308 + case IChat.CN_MSG_STATUS_SENDING:
  309 + if (status != null)
  310 + status.setText("发送中");
  311 + if (uploadProgress != null) {
  312 + uploadProgress.setVisibility(View.VISIBLE);
  313 + uploadProgress.setText("上传进度:" + voiceMessage.getProgress() + "%");
  314 + }
  315 + break;
  316 + case IChat.CN_MSG_STATUS_SEND_FAIL:
  317 + if (status != null)
  318 + status.setText("发送失败");
  319 + if (uploadProgress != null)
  320 + uploadProgress.setVisibility(View.GONE);
  321 + break;
  322 + case IChat.CN_MSG_STATUS_SEND_SUCCESS:
  323 + if (status != null)
  324 + status.setText("发送成功");
  325 + if (uploadProgress != null)
  326 + uploadProgress.setVisibility(View.GONE);
  327 + sendSuccess(voiceMessage);
  328 + break;
  329 + }
  330 + }
  331 +
  332 + private void sendSuccess(final VoiceMessage voiceMessage) {
  333 + if (voice_none_read != null)
  334 + voice_none_read.setVisibility(voiceMessage.getCnVoiceMessage().isRead() ? View.GONE : View.VISIBLE);
  335 + itemView.setOnClickListener(new View.OnClickListener() {
  336 + @Override
  337 + public void onClick(View v) {
  338 + if (mItemClickListener != null)
  339 + mItemClickListener.onItemClick(voiceMessage, (AnimationDrawable) voiceAnim.getBackground());
  340 + if (voice_none_read != null)
  341 + voice_none_read.setVisibility(View.GONE);
  342 + }
  343 + });
  344 + }
  345 + }
  346 +
  347 + public class VideoMessageHolder extends RecyclerView.ViewHolder {
  348 +
  349 + private TextView video_user_name;
  350 + private ImageView video_snapshot;
  351 + private View videoLayout;
  352 + private TextView status;
  353 + private TextView uploadProgress;
  354 +
  355 + public VideoMessageHolder(View itemView) {
  356 + super(itemView);
  357 + video_user_name = (TextView) itemView.findViewById(R.id.user_name);
  358 + video_snapshot = (ImageView) itemView.findViewById(R.id.img_content);
  359 + videoLayout = itemView.findViewById(R.id.videoLayout);
  360 + status = (TextView) itemView.findViewById(R.id.status);
  361 + uploadProgress = (TextView) itemView.findViewById(R.id.uploadProgress);
  362 + }
  363 +
  364 + public void showMessage(final VideoMessage videoMessage) {
  365 + video_user_name.setText(videoMessage.getCnVideoMessage().getUserInfo().getUserName());
  366 + switch (videoMessage.getCnVideoMessage().getStatus()) {
  367 + case IChat.CN_MSG_STATUS_SENDING:
  368 + if (status != null)
  369 + status.setText("发送中");
  370 + if (uploadProgress != null) {
  371 + uploadProgress.setVisibility(View.VISIBLE);
  372 + uploadProgress.setText("上传进度:" + videoMessage.getProgress() + "%");
  373 + }
  374 + Glide.with(mContext).load(videoMessage.getCnVideoMessage().getSnapshotPath()).into(video_snapshot);
  375 + break;
  376 + case IChat.CN_MSG_STATUS_SEND_FAIL:
  377 + if (status != null)
  378 + status.setText("发送失败");
  379 + if (uploadProgress != null)
  380 + uploadProgress.setVisibility(View.GONE);
  381 + Glide.with(mContext).load(videoMessage.getCnVideoMessage().getSnapshotPath()).into(video_snapshot);
  382 + break;
  383 + case IChat.CN_MSG_STATUS_SEND_SUCCESS:
  384 + if (status != null)
  385 + status.setText("发送成功");
  386 + if (uploadProgress != null)
  387 + uploadProgress.setVisibility(View.GONE);
  388 + sendSuccess(videoMessage);
  389 + break;
  390 + }
  391 + }
  392 +
  393 + private void sendSuccess(final VideoMessage videoMessage) {
  394 + videoLayout.setOnClickListener(new View.OnClickListener() {
  395 + @Override
  396 + public void onClick(View v) {
  397 + if (mItemClickListener != null)
  398 + mItemClickListener.onVideoClick(videoMessage);
  399 + }
  400 + });
  401 + if (videoMessage.getCnVideoMessage().isSelf()) {
  402 + Glide.with(mContext).load(videoMessage.getCnVideoMessage().getSnapshotPath()).into(video_snapshot);
  403 + } else {
  404 + String snapshotUuid = videoMessage.getCnVideoMessage().getSnapshot().getUuid();
  405 + final String imagePath = FileUtil.getCacheFilePath(snapshotUuid);
  406 + if (FileUtil.isCacheFileExist(snapshotUuid)) {
  407 + if (video_snapshot != null && mContext != null)
  408 + Glide.with(mContext).load(imagePath).into(video_snapshot);
  409 + } else {
  410 + if (!videoMessage.isDownloading()) {
  411 + videoMessage.setDownloading(true);
  412 + videoMessage.getCnVideoMessage().getSnapshot().getImage(imagePath, new IChat.OnDownloadListener() {
  413 + @Override
  414 + public void onSuccess() {
  415 + videoMessage.setDownloading(false);
  416 + if (video_snapshot != null && mContext != null)
  417 + Glide.with(mContext).load(imagePath).into(video_snapshot);
  418 + }
  419 +
  420 + @Override
  421 + public void onError(int i, String s) {
  422 + videoMessage.setDownloading(false);
  423 + }
  424 + });
  425 + }
  426 + }
  427 + }
  428 + }
  429 + }
  430 +
  431 + public class UgcVideoMessageHolder extends RecyclerView.ViewHolder {
  432 +
  433 + private TextView video_user_name;
  434 + private ImageView video_cover;
  435 + private View videoLayout;
  436 + private TextView status;
  437 + private TextView uploadProgress;
  438 +
  439 + public UgcVideoMessageHolder(View itemView) {
  440 + super(itemView);
  441 + video_user_name = (TextView) itemView.findViewById(R.id.user_name);
  442 + video_cover = (ImageView) itemView.findViewById(R.id.img_content);
  443 + videoLayout = itemView.findViewById(R.id.videoLayout);
  444 + status = (TextView) itemView.findViewById(R.id.status);
  445 + uploadProgress = (TextView) itemView.findViewById(R.id.uploadProgress);
  446 + }
  447 +
  448 + public void showMessage(final UgcVideoMessage videoMessage) {
  449 + video_user_name.setText(videoMessage.getCnUgcMessage().getUserInfo().getUserName());
  450 + switch (videoMessage.getCnUgcMessage().getStatus()) {
  451 + case IChat.CN_MSG_STATUS_SENDING:
  452 + if (status != null)
  453 + status.setText("发送中");
  454 + if (uploadProgress != null) {
  455 + uploadProgress.setVisibility(View.VISIBLE);
  456 + uploadProgress.setText("上传进度:" + videoMessage.getProgress() + "%");
  457 + }
  458 + Glide.with(mContext).load(videoMessage.getCnUgcMessage().getCoverPath()).into(video_cover);
  459 + break;
  460 + case IChat.CN_MSG_STATUS_SEND_FAIL:
  461 + if (status != null)
  462 + status.setText("发送失败");
  463 + if (uploadProgress != null)
  464 + uploadProgress.setVisibility(View.GONE);
  465 + Glide.with(mContext).load(videoMessage.getCnUgcMessage().getCoverPath()).into(video_cover);
  466 + break;
  467 + case IChat.CN_MSG_STATUS_SEND_SUCCESS:
  468 + if (status != null)
  469 + status.setText("发送成功");
  470 + if (uploadProgress != null)
  471 + uploadProgress.setVisibility(View.GONE);
  472 + sendSuccess(videoMessage);
  473 + break;
  474 + }
  475 + }
  476 +
  477 + private void sendSuccess(final UgcVideoMessage videoMessage) {
  478 + videoLayout.setOnClickListener(new View.OnClickListener() {
  479 + @Override
  480 + public void onClick(View v) {
  481 + if (mItemClickListener != null)
  482 + mItemClickListener.onUgcVideoClick(videoMessage);
  483 + }
  484 + });
  485 + if (videoMessage.getCnUgcMessage().isSelf()) {
  486 + Glide.with(mContext).load(videoMessage.getCnUgcMessage().getCoverPath()).into(video_cover);
  487 + } else {
  488 + String coverUuid = videoMessage.getCnUgcMessage().getFileId() + "_image";
  489 + final String imagePath = FileUtil.getCacheFilePath(coverUuid);
  490 + if (FileUtil.isCacheFileExist(coverUuid)) {
  491 + if (video_cover != null && mContext != null)
  492 + Glide.with(mContext).load(imagePath).into(video_cover);
  493 + } else {
  494 + if (!videoMessage.isDownloading()) {
  495 + videoMessage.setDownloading(true);
  496 + videoMessage.getCnUgcMessage().getCover().getImage(imagePath, new IChat.OnDownloadListener() {
  497 + @Override
  498 + public void onSuccess() {
  499 + videoMessage.setDownloading(false);
  500 + if (video_cover != null && mContext != null)
  501 + Glide.with(mContext).load(imagePath).into(video_cover);
  502 + }
  503 +
  504 + @Override
  505 + public void onError(int i, String s) {
  506 + videoMessage.setDownloading(false);
  507 + }
  508 + });
  509 + }
  510 + }
  511 + }
  512 + }
  513 + }
  514 +
  515 +}
app/src/main/java/com/cnlive/im/adapter/ChatRoomAdpter.java
1 package com.cnlive.im.adapter; 1 package com.cnlive.im.adapter;
2 2
3 import android.content.Context; 3 import android.content.Context;
  4 +import android.database.Cursor;
4 import android.graphics.Color; 5 import android.graphics.Color;
5 -import android.graphics.drawable.AnimationDrawable; 6 +import android.net.Uri;
  7 +import android.provider.ContactsContract;
  8 +import android.provider.MediaStore;
6 import android.support.v7.widget.RecyclerView; 9 import android.support.v7.widget.RecyclerView;
  10 +import android.util.Log;
7 import android.view.LayoutInflater; 11 import android.view.LayoutInflater;
8 import android.view.View; 12 import android.view.View;
9 import android.view.ViewGroup; 13 import android.view.ViewGroup;
  14 +import android.view.WindowManager;
10 import android.widget.ImageView; 15 import android.widget.ImageView;
  16 +import android.widget.LinearLayout;
11 import android.widget.TextView; 17 import android.widget.TextView;
12 18
13 import com.bumptech.glide.Glide; 19 import com.bumptech.glide.Glide;
14 import com.cnlive.im.R; 20 import com.cnlive.im.R;
15 import com.cnlive.im.adapter.base.BaseRecyclerAdapter; 21 import com.cnlive.im.adapter.base.BaseRecyclerAdapter;
16 -import com.cnlive.im.adapter.base.ItemClickListener; 22 +import com.cnlive.im.adapter.base.VoiceItemClickListener;
17 import com.cnlive.im.mode.ImagMessage; 23 import com.cnlive.im.mode.ImagMessage;
18 import com.cnlive.im.mode.StringMessage; 24 import com.cnlive.im.mode.StringMessage;
19 -import com.cnlive.im.mode.UgcVideoMessage;  
20 -import com.cnlive.im.mode.VideoMessage;  
21 import com.cnlive.im.mode.VoiceMessage; 25 import com.cnlive.im.mode.VoiceMessage;
22 -import com.cnlive.im.util.FileUtil;  
23 -import com.cnlive.libs.util.chat.base.IChat;  
24 26
25 import org.json.JSONException; 27 import org.json.JSONException;
26 import org.json.JSONObject; 28 import org.json.JSONObject;
27 29
  30 +import java.text.SimpleDateFormat;
  31 +import java.util.Calendar;
  32 +import java.util.Date;
  33 +import java.util.HashMap;
  34 +import java.util.Locale;
  35 +
28 /** 36 /**
29 * @author 陈硕 37 * @author 陈硕
30 * @time 2017/7/14 14:35 38 * @time 2017/7/14 14:35
@@ -38,12 +46,8 @@ public class ChatRoomAdpter extends BaseRecyclerAdapter { @@ -38,12 +46,8 @@ public class ChatRoomAdpter extends BaseRecyclerAdapter {
38 private int imaOppsite = 4; 46 private int imaOppsite = 4;
39 private int voiceUser = 5; 47 private int voiceUser = 5;
40 private int voiceOppsite = 6; 48 private int voiceOppsite = 6;
41 - private int videoUser = 7;  
42 - private int videoOppsite = 8;  
43 - private int ugcVideoUser = 9;  
44 - private int ugcVideoOppsite = 10;  
45 private Context mContext; 49 private Context mContext;
46 - private ItemClickListener mItemClickListener; 50 + private VoiceItemClickListener mVoiceItemClickListener;
47 51
48 private boolean isPrivate; 52 private boolean isPrivate;
49 53
@@ -54,8 +58,8 @@ public class ChatRoomAdpter extends BaseRecyclerAdapter { @@ -54,8 +58,8 @@ public class ChatRoomAdpter extends BaseRecyclerAdapter {
54 } 58 }
55 59
56 60
57 - public void setonVoiceItemClickListener(ItemClickListener itemClickListener) {  
58 - this.mItemClickListener = itemClickListener; 61 + public void setonVoiceItemClickListener(VoiceItemClickListener voiveItemClickListener) {
  62 + this.mVoiceItemClickListener = voiveItemClickListener;
59 } 63 }
60 64
61 @Override 65 @Override
@@ -76,18 +80,6 @@ public class ChatRoomAdpter extends BaseRecyclerAdapter { @@ -76,18 +80,6 @@ public class ChatRoomAdpter extends BaseRecyclerAdapter {
76 return voiceUser; 80 return voiceUser;
77 else return voiceOppsite; 81 else return voiceOppsite;
78 82
79 - } else if (o instanceof VideoMessage) {  
80 - VideoMessage o1 = (VideoMessage) o;  
81 - if (o1.isUserSend())  
82 - return videoUser;  
83 - else return videoOppsite;  
84 -  
85 - } else if (o instanceof UgcVideoMessage) {  
86 - UgcVideoMessage o1 = (UgcVideoMessage) o;  
87 - if (o1.isUserSend())  
88 - return ugcVideoUser;  
89 - else return ugcVideoOppsite;  
90 -  
91 } 83 }
92 return 0; 84 return 0;
93 } 85 }
@@ -106,14 +98,6 @@ public class ChatRoomAdpter extends BaseRecyclerAdapter { @@ -106,14 +98,6 @@ public class ChatRoomAdpter extends BaseRecyclerAdapter {
106 return new VoiceMessageHolder(LayoutInflater.from(mContext).inflate(R.layout.voice_layout_item_user, null)); 98 return new VoiceMessageHolder(LayoutInflater.from(mContext).inflate(R.layout.voice_layout_item_user, null));
107 } else if (viewType == voiceOppsite) { 99 } else if (viewType == voiceOppsite) {
108 return new VoiceMessageHolder(LayoutInflater.from(mContext).inflate(R.layout.voice_layout_item_oppsite, null)); 100 return new VoiceMessageHolder(LayoutInflater.from(mContext).inflate(R.layout.voice_layout_item_oppsite, null));
109 - } else if (viewType == videoUser) {  
110 - return new VideoMessageHolder(LayoutInflater.from(mContext).inflate(R.layout.video_layout_item_user, null));  
111 - } else if (viewType == videoOppsite) {  
112 - return new VideoMessageHolder(LayoutInflater.from(mContext).inflate(R.layout.video_layout_item_oppsite, null));  
113 - } else if (viewType == ugcVideoUser) {  
114 - return new VideoMessageHolder(LayoutInflater.from(mContext).inflate(R.layout.video_layout_item_user, null));  
115 - } else if (viewType == ugcVideoOppsite) {  
116 - return new VideoMessageHolder(LayoutInflater.from(mContext).inflate(R.layout.video_layout_item_oppsite, null));  
117 } 101 }
118 return null; 102 return null;
119 } 103 }
@@ -124,27 +108,42 @@ public class ChatRoomAdpter extends BaseRecyclerAdapter { @@ -124,27 +108,42 @@ public class ChatRoomAdpter extends BaseRecyclerAdapter {
124 StringMessageHolder holder1 = (StringMessageHolder) holder; 108 StringMessageHolder holder1 = (StringMessageHolder) holder;
125 Object o = mItems.get(position); 109 Object o = mItems.get(position);
126 StringMessage o1 = (StringMessage) o; 110 StringMessage o1 = (StringMessage) o;
127 - holder1.showMessage(o1); 111 + if (isPrivate) {
  112 + try {
  113 + holder1.msg_text.setText((String) (new JSONObject(o1.getCnMessage().getContent()).get("message")));
  114 + } catch (JSONException e) {
  115 + e.printStackTrace();
  116 + }
  117 + } else {
  118 + holder1.msg_text.setText(o1.getCnMessage().getContent());
  119 + }
  120 + holder1.user_name.setText(o1.getCnMessage().getUserInfo().getUserName());
  121 + holder1.user_name.setTextColor(Color.parseColor("#d61d0c"));
  122 + holder1.msg_text.setTextColor(Color.BLACK);
128 } else if (holder instanceof ImaMessageHolder) { 123 } else if (holder instanceof ImaMessageHolder) {
129 ImaMessageHolder holder1 = (ImaMessageHolder) holder; 124 ImaMessageHolder holder1 = (ImaMessageHolder) holder;
130 Object o = mItems.get(position); 125 Object o = mItems.get(position);
131 ImagMessage o1 = (ImagMessage) o; 126 ImagMessage o1 = (ImagMessage) o;
132 - holder1.showMessage(o1); 127 + holder1.ima_user_name.setTextColor(Color.parseColor("#d61d0c"));
  128 + holder1.img_content.getLayoutParams().height = 200;
  129 + holder1.img_content.getLayoutParams().width = 200;
  130 + holder1.img_content.setLayoutParams(holder1.img_content.getLayoutParams());
  131 + Glide.with(mContext).load(o1.getCnImgMessage().getRemoteUri()).into(holder1.img_content);
  132 + holder1.ima_user_name.setText(o1.getCnImgMessage().getUserInfo().getUserName());
133 } else if (holder instanceof VoiceMessageHolder) { 133 } else if (holder instanceof VoiceMessageHolder) {
134 - final VoiceMessageHolder holder1 = (VoiceMessageHolder) holder;  
135 - final Object o = mItems.get(position); 134 + VoiceMessageHolder holder1 = (VoiceMessageHolder) holder;
  135 + Object o = mItems.get(position);
136 final VoiceMessage o1 = (VoiceMessage) o; 136 final VoiceMessage o1 = (VoiceMessage) o;
137 - holder1.showMessage(o1);  
138 - } else if (holder instanceof VideoMessageHolder) {  
139 - final VideoMessageHolder holder1 = (VideoMessageHolder) holder;  
140 - final Object o = mItems.get(position);  
141 - final VideoMessage o1 = (VideoMessage) o;  
142 - holder1.showMessage(o1);  
143 - } else if (holder instanceof UgcVideoMessageHolder) {  
144 - final UgcVideoMessageHolder holder1 = (UgcVideoMessageHolder) holder;  
145 - final Object o = mItems.get(position);  
146 - final UgcVideoMessage o1 = (UgcVideoMessage) o;  
147 - holder1.showMessage(o1); 137 + holder1.voice_user_name.setText(o1.getCnVoiceMessage().getUserInfo().getUserName());
  138 + final Uri voiceUri = o1.getCnVoiceMessage().getVoiceUri();
  139 + int duration = o1.getCnVoiceMessage().getVoiceTime();
  140 + holder1.voice_duration.setText(String.valueOf(duration).concat("\""));
  141 + holder1.itemView.setOnClickListener(new View.OnClickListener() {
  142 + @Override
  143 + public void onClick(View v) {
  144 + mVoiceItemClickListener.onItemClick(o1);
  145 + }
  146 + });
148 } 147 }
149 148
150 } 149 }
@@ -152,48 +151,12 @@ public class ChatRoomAdpter extends BaseRecyclerAdapter { @@ -152,48 +151,12 @@ public class ChatRoomAdpter extends BaseRecyclerAdapter {
152 public class StringMessageHolder extends RecyclerView.ViewHolder { 151 public class StringMessageHolder extends RecyclerView.ViewHolder {
153 private TextView user_name; 152 private TextView user_name;
154 private TextView msg_text; 153 private TextView msg_text;
155 - private TextView status;  
156 154
157 public StringMessageHolder(View itemView) { 155 public StringMessageHolder(View itemView) {
158 super(itemView); 156 super(itemView);
159 user_name = (TextView) itemView.findViewById(R.id.user_name); 157 user_name = (TextView) itemView.findViewById(R.id.user_name);
160 msg_text = (TextView) itemView.findViewById(R.id.msg_text); 158 msg_text = (TextView) itemView.findViewById(R.id.msg_text);
161 - status = (TextView) itemView.findViewById(R.id.status);  
162 - }  
163 159
164 - public void showMessage(StringMessage item) {  
165 - user_name.setText(item.getCnMessage().getUserInfo().getUserName());  
166 -  
167 - switch (item.getCnMessage().getStatus()) {  
168 - case IChat.CN_MSG_STATUS_SENDING:  
169 - if (status != null)  
170 - status.setText("发送中");  
171 - sendMsg(item);  
172 - break;  
173 - case IChat.CN_MSG_STATUS_SEND_FAIL:  
174 - if (status != null)  
175 - status.setText("发送失败");  
176 - sendMsg(item);  
177 - break;  
178 - case IChat.CN_MSG_STATUS_SEND_SUCCESS:  
179 - if (status != null)  
180 - status.setText("发送成功");  
181 - sendMsg(item);  
182 - break;  
183 - }  
184 - }  
185 -  
186 - private void sendMsg(StringMessage item) {  
187 - if (isPrivate) {  
188 - try {  
189 - msg_text.setText((String) (new JSONObject(item.getCnMessage().getContent()).get("message")));  
190 - } catch (JSONException e) {  
191 - e.printStackTrace();  
192 - }  
193 - } else {  
194 - msg_text.setText(item.getCnMessage().getContent());  
195 - }  
196 - msg_text.setTextColor(Color.BLACK);  
197 } 160 }
198 } 161 }
199 162
@@ -201,84 +164,11 @@ public class ChatRoomAdpter extends BaseRecyclerAdapter { @@ -201,84 +164,11 @@ public class ChatRoomAdpter extends BaseRecyclerAdapter {
201 164
202 private TextView ima_user_name; 165 private TextView ima_user_name;
203 private ImageView img_content; 166 private ImageView img_content;
204 - private TextView status;  
205 - private TextView uploadProgress;  
206 167
207 public ImaMessageHolder(View itemView) { 168 public ImaMessageHolder(View itemView) {
208 super(itemView); 169 super(itemView);
209 ima_user_name = (TextView) itemView.findViewById(R.id.img_user_name); 170 ima_user_name = (TextView) itemView.findViewById(R.id.img_user_name);
210 img_content = (ImageView) itemView.findViewById(R.id.img_content); 171 img_content = (ImageView) itemView.findViewById(R.id.img_content);
211 - status = (TextView) itemView.findViewById(R.id.status);  
212 - uploadProgress = (TextView) itemView.findViewById(R.id.uploadProgress);  
213 - }  
214 -  
215 - public void showMessage(final ImagMessage imagMessage) {  
216 - ima_user_name.setText(imagMessage.getCnImgMessage().getUserInfo().getUserName());  
217 - img_content.getLayoutParams().height = 200;  
218 - img_content.getLayoutParams().width = 200;  
219 - img_content.setLayoutParams(img_content.getLayoutParams());  
220 - switch (imagMessage.getCnImgMessage().getStatus()) {  
221 - case IChat.CN_MSG_STATUS_SENDING:  
222 - if (status != null)  
223 - status.setText("发送中");  
224 - if (uploadProgress != null) {  
225 - uploadProgress.setVisibility(View.VISIBLE);  
226 - uploadProgress.setText("上传进度:" + imagMessage.getProgress() + "%");  
227 - }  
228 - Glide.with(mContext).load(imagMessage.getCnImgMessage().getImagePath()).into(img_content);  
229 - break;  
230 - case IChat.CN_MSG_STATUS_SEND_FAIL:  
231 - if (status != null)  
232 - status.setText("发送失败");  
233 - if (uploadProgress != null)  
234 - uploadProgress.setVisibility(View.GONE);  
235 - Glide.with(mContext).load(imagMessage.getCnImgMessage().getImagePath()).into(img_content);  
236 - break;  
237 - case IChat.CN_MSG_STATUS_SEND_SUCCESS:  
238 - if (status != null)  
239 - status.setText("发送成功");  
240 - if (uploadProgress != null)  
241 - uploadProgress.setVisibility(View.GONE);  
242 - sendSuccess(imagMessage);  
243 - break;  
244 - }  
245 - }  
246 -  
247 - private void sendSuccess(final ImagMessage imagMessage) {  
248 - if (imagMessage.getCnImgMessage().isSelf()) {  
249 - Glide.with(mContext).load(imagMessage.getCnImgMessage().getImagePath()).into(img_content);  
250 - } else {  
251 - String thumbUuid = imagMessage.getCnImgMessage().getThumbUuid();  
252 - final String imagePath = FileUtil.getCacheFilePath(thumbUuid);  
253 - if (FileUtil.isCacheFileExist(thumbUuid)) {  
254 - if (img_content != null && mContext != null)  
255 - Glide.with(mContext).load(imagePath).into(img_content);  
256 - } else {  
257 - if (!imagMessage.isDownloading()) {  
258 - imagMessage.setDownloading(true);  
259 - imagMessage.getCnImgMessage().getThumbImage(imagePath, new IChat.OnDownloadListener() {  
260 - @Override  
261 - public void onSuccess() {  
262 - imagMessage.setDownloading(false);  
263 - if (img_content != null && mContext != null)  
264 - Glide.with(mContext).load(imagePath).into(img_content);  
265 - }  
266 -  
267 - @Override  
268 - public void onError(int i, String s) {  
269 - imagMessage.setDownloading(false);  
270 - }  
271 - });  
272 - }  
273 - }  
274 - }  
275 - img_content.setOnClickListener(new View.OnClickListener() {  
276 - @Override  
277 - public void onClick(View v) {  
278 - if (mItemClickListener != null)  
279 - mItemClickListener.onImageClick(imagMessage);  
280 - }  
281 - });  
282 } 172 }
283 } 173 }
284 174
@@ -286,231 +176,12 @@ public class ChatRoomAdpter extends BaseRecyclerAdapter { @@ -286,231 +176,12 @@ public class ChatRoomAdpter extends BaseRecyclerAdapter {
286 176
287 177
288 private TextView voice_user_name; 178 private TextView voice_user_name;
289 - private ImageView voiceAnim;  
290 private TextView voice_duration; 179 private TextView voice_duration;
291 - private View voice_none_read;  
292 - private TextView status;  
293 - private TextView uploadProgress;  
294 180
295 public VoiceMessageHolder(View itemView) { 181 public VoiceMessageHolder(View itemView) {
296 super(itemView); 182 super(itemView);
297 voice_user_name = (TextView) itemView.findViewById(R.id.voice_user_name); 183 voice_user_name = (TextView) itemView.findViewById(R.id.voice_user_name);
298 - voiceAnim = (ImageView) itemView.findViewById(R.id.voiceAnim);  
299 voice_duration = (TextView) itemView.findViewById(R.id.voice_duration); 184 voice_duration = (TextView) itemView.findViewById(R.id.voice_duration);
300 - voice_none_read = itemView.findViewById(R.id.noneRead);  
301 - status = (TextView) itemView.findViewById(R.id.status);  
302 - uploadProgress = (TextView) itemView.findViewById(R.id.uploadProgress);  
303 - }  
304 -  
305 - public void showMessage(final VoiceMessage voiceMessage) {  
306 - voice_user_name.setText(voiceMessage.getCnVoiceMessage().getUserInfo().getUserName());  
307 - int duration = voiceMessage.getCnVoiceMessage().getVoiceTime();  
308 - voice_duration.setText(String.valueOf(duration).concat("\""));  
309 - switch (voiceMessage.getCnVoiceMessage().getStatus()) {  
310 - case IChat.CN_MSG_STATUS_SENDING:  
311 - if (status != null)  
312 - status.setText("发送中");  
313 - if (uploadProgress != null) {  
314 - uploadProgress.setVisibility(View.VISIBLE);  
315 - uploadProgress.setText("上传进度:" + voiceMessage.getProgress() + "%");  
316 - }  
317 - break;  
318 - case IChat.CN_MSG_STATUS_SEND_FAIL:  
319 - if (status != null)  
320 - status.setText("发送失败");  
321 - if (uploadProgress != null)  
322 - uploadProgress.setVisibility(View.GONE);  
323 - break;  
324 - case IChat.CN_MSG_STATUS_SEND_SUCCESS:  
325 - if (status != null)  
326 - status.setText("发送成功");  
327 - if (uploadProgress != null)  
328 - uploadProgress.setVisibility(View.GONE);  
329 - sendSuccess(voiceMessage);  
330 - break;  
331 - }  
332 - }  
333 -  
334 - private void sendSuccess(final VoiceMessage voiceMessage) {  
335 - if (voice_none_read != null)  
336 - voice_none_read.setVisibility(voiceMessage.getCnVoiceMessage().isRead() ? View.GONE : View.VISIBLE);  
337 - itemView.setOnClickListener(new View.OnClickListener() {  
338 - @Override  
339 - public void onClick(View v) {  
340 - if (mItemClickListener != null)  
341 - mItemClickListener.onItemClick(voiceMessage, (AnimationDrawable) voiceAnim.getBackground());  
342 - if (voice_none_read != null)  
343 - voice_none_read.setVisibility(View.GONE);  
344 - }  
345 - });  
346 - }  
347 - }  
348 -  
349 - public class VideoMessageHolder extends RecyclerView.ViewHolder {  
350 -  
351 - private TextView video_user_name;  
352 - private ImageView video_snapshot;  
353 - private View videoLayout;  
354 - private TextView status;  
355 - private TextView uploadProgress;  
356 -  
357 - public VideoMessageHolder(View itemView) {  
358 - super(itemView);  
359 - video_user_name = (TextView) itemView.findViewById(R.id.user_name);  
360 - video_snapshot = (ImageView) itemView.findViewById(R.id.img_content);  
361 - videoLayout = itemView.findViewById(R.id.videoLayout);  
362 - status = (TextView) itemView.findViewById(R.id.status);  
363 - uploadProgress = (TextView) itemView.findViewById(R.id.uploadProgress);  
364 - }  
365 -  
366 - public void showMessage(final VideoMessage videoMessage) {  
367 - video_user_name.setText(videoMessage.getCnVideoMessage().getUserInfo().getUserName());  
368 - switch (videoMessage.getCnVideoMessage().getStatus()) {  
369 - case IChat.CN_MSG_STATUS_SENDING:  
370 - if (status != null)  
371 - status.setText("发送中");  
372 - if (uploadProgress != null) {  
373 - uploadProgress.setVisibility(View.VISIBLE);  
374 - uploadProgress.setText("上传进度:" + videoMessage.getProgress() + "%");  
375 - }  
376 - Glide.with(mContext).load(videoMessage.getCnVideoMessage().getSnapshotPath()).into(video_snapshot);  
377 - break;  
378 - case IChat.CN_MSG_STATUS_SEND_FAIL:  
379 - if (status != null)  
380 - status.setText("发送失败");  
381 - if (uploadProgress != null)  
382 - uploadProgress.setVisibility(View.GONE);  
383 - Glide.with(mContext).load(videoMessage.getCnVideoMessage().getSnapshotPath()).into(video_snapshot);  
384 - break;  
385 - case IChat.CN_MSG_STATUS_SEND_SUCCESS:  
386 - if (status != null)  
387 - status.setText("发送成功");  
388 - if (uploadProgress != null)  
389 - uploadProgress.setVisibility(View.GONE);  
390 - sendSuccess(videoMessage);  
391 - break;  
392 - }  
393 - }  
394 -  
395 - private void sendSuccess(final VideoMessage videoMessage) {  
396 - videoLayout.setOnClickListener(new View.OnClickListener() {  
397 - @Override  
398 - public void onClick(View v) {  
399 - if (mItemClickListener != null)  
400 - mItemClickListener.onVideoClick(videoMessage);  
401 - }  
402 - });  
403 - if (videoMessage.getCnVideoMessage().isSelf()) {  
404 - Glide.with(mContext).load(videoMessage.getCnVideoMessage().getSnapshotPath()).into(video_snapshot);  
405 - } else {  
406 - String snapshotUuid = videoMessage.getCnVideoMessage().getSnapshot().getUuid();  
407 - final String imagePath = FileUtil.getCacheFilePath(snapshotUuid);  
408 - if (FileUtil.isCacheFileExist(snapshotUuid)) {  
409 - if (video_snapshot != null && mContext != null)  
410 - Glide.with(mContext).load(imagePath).into(video_snapshot);  
411 - } else {  
412 - if (!videoMessage.isDownloading()) {  
413 - videoMessage.setDownloading(true);  
414 - videoMessage.getCnVideoMessage().getSnapshot().getImage(imagePath, new IChat.OnDownloadListener() {  
415 - @Override  
416 - public void onSuccess() {  
417 - videoMessage.setDownloading(false);  
418 - if (video_snapshot != null && mContext != null)  
419 - Glide.with(mContext).load(imagePath).into(video_snapshot);  
420 - }  
421 -  
422 - @Override  
423 - public void onError(int i, String s) {  
424 - videoMessage.setDownloading(false);  
425 - }  
426 - });  
427 - }  
428 - }  
429 - }  
430 - }  
431 - }  
432 -  
433 - public class UgcVideoMessageHolder extends RecyclerView.ViewHolder {  
434 -  
435 - private TextView video_user_name;  
436 - private ImageView video_cover;  
437 - private View videoLayout;  
438 - private TextView status;  
439 - private TextView uploadProgress;  
440 -  
441 - public UgcVideoMessageHolder(View itemView) {  
442 - super(itemView);  
443 - video_user_name = (TextView) itemView.findViewById(R.id.user_name);  
444 - video_cover = (ImageView) itemView.findViewById(R.id.img_content);  
445 - videoLayout = itemView.findViewById(R.id.videoLayout);  
446 - status = (TextView) itemView.findViewById(R.id.status);  
447 - uploadProgress = (TextView) itemView.findViewById(R.id.uploadProgress);  
448 - }  
449 -  
450 - public void showMessage(final UgcVideoMessage videoMessage) {  
451 - video_user_name.setText(videoMessage.getCnUgcMessage().getUserInfo().getUserName());  
452 - switch (videoMessage.getCnUgcMessage().getStatus()) {  
453 - case IChat.CN_MSG_STATUS_SENDING:  
454 - if (status != null)  
455 - status.setText("发送中");  
456 - if (uploadProgress != null) {  
457 - uploadProgress.setVisibility(View.VISIBLE);  
458 - uploadProgress.setText("上传进度:" + videoMessage.getProgress() + "%");  
459 - }  
460 - Glide.with(mContext).load(videoMessage.getCnUgcMessage().getCoverPath()).into(video_cover);  
461 - break;  
462 - case IChat.CN_MSG_STATUS_SEND_FAIL:  
463 - if (status != null)  
464 - status.setText("发送失败");  
465 - if (uploadProgress != null)  
466 - uploadProgress.setVisibility(View.GONE);  
467 - Glide.with(mContext).load(videoMessage.getCnUgcMessage().getCoverPath()).into(video_cover);  
468 - break;  
469 - case IChat.CN_MSG_STATUS_SEND_SUCCESS:  
470 - if (status != null)  
471 - status.setText("发送成功");  
472 - if (uploadProgress != null)  
473 - uploadProgress.setVisibility(View.GONE);  
474 - sendSuccess(videoMessage);  
475 - break;  
476 - }  
477 - }  
478 -  
479 - private void sendSuccess(final UgcVideoMessage videoMessage) {  
480 - videoLayout.setOnClickListener(new View.OnClickListener() {  
481 - @Override  
482 - public void onClick(View v) {  
483 - if (mItemClickListener != null)  
484 - mItemClickListener.onUgcVideoClick(videoMessage);  
485 - }  
486 - });  
487 - if (videoMessage.getCnUgcMessage().isSelf()) {  
488 - Glide.with(mContext).load(videoMessage.getCnUgcMessage().getCoverPath()).into(video_cover);  
489 - } else {  
490 - String coverUuid = videoMessage.getCnUgcMessage().getFileId() + "_image";  
491 - final String imagePath = FileUtil.getCacheFilePath(coverUuid);  
492 - if (FileUtil.isCacheFileExist(coverUuid)) {  
493 - if (video_cover != null && mContext != null)  
494 - Glide.with(mContext).load(imagePath).into(video_cover);  
495 - } else {  
496 - if (!videoMessage.isDownloading()) {  
497 - videoMessage.setDownloading(true);  
498 - videoMessage.getCnUgcMessage().getCover().getImage(imagePath, new IChat.OnDownloadListener() {  
499 - @Override  
500 - public void onSuccess() {  
501 - videoMessage.setDownloading(false);  
502 - if (video_cover != null && mContext != null)  
503 - Glide.with(mContext).load(imagePath).into(video_cover);  
504 - }  
505 -  
506 - @Override  
507 - public void onError(int i, String s) {  
508 - videoMessage.setDownloading(false);  
509 - }  
510 - });  
511 - }  
512 - }  
513 - }  
514 } 185 }
515 } 186 }
516 187
app/src/main/java/com/cnlive/im/adapter/base/ItemClickListener.java
@@ -8,9 +8,7 @@ import com.cnlive.im.mode.VideoMessage; @@ -8,9 +8,7 @@ import com.cnlive.im.mode.VideoMessage;
8 import com.cnlive.im.mode.VoiceMessage; 8 import com.cnlive.im.mode.VoiceMessage;
9 9
10 /** 10 /**
11 - * @author 陈硕  
12 - * @time 2017/7/14 17:50  
13 - * @desc ${TODD} 11 + * Created by gujun on 2017/12/19.
14 */ 12 */
15 public interface ItemClickListener { 13 public interface ItemClickListener {
16 void onItemClick(VoiceMessage voiceMessage, AnimationDrawable animationDrawable); 14 void onItemClick(VoiceMessage voiceMessage, AnimationDrawable animationDrawable);
app/src/main/java/com/cnlive/im/adapter/base/VoiceItemClickListener.java 0 → 100644
  1 +package com.cnlive.im.adapter.base;
  2 +
  3 +import android.net.Uri;
  4 +
  5 +import com.cnlive.im.mode.VoiceMessage;
  6 +
  7 +/**
  8 + * @author 陈硕
  9 + * @time 2017/7/14 17:50
  10 + * @desc ${TODD}
  11 + */
  12 +public interface VoiceItemClickListener {
  13 + void onItemClick(VoiceMessage voiceMessage);
  14 +}
app/src/main/java/com/cnlive/im/ui/ChatRoom3Activity.java 0 → 100644
  1 +package com.cnlive.im.ui;
  2 +
  3 +import android.Manifest;
  4 +import android.app.Activity;
  5 +import android.content.Context;
  6 +import android.content.Intent;
  7 +import android.content.pm.PackageManager;
  8 +import android.graphics.Bitmap;
  9 +import android.graphics.drawable.AnimationDrawable;
  10 +import android.media.ThumbnailUtils;
  11 +import android.net.Uri;
  12 +import android.os.Build;
  13 +import android.os.Bundle;
  14 +import android.os.Handler;
  15 +import android.provider.MediaStore;
  16 +import android.support.annotation.NonNull;
  17 +import android.support.v4.app.ActivityCompat;
  18 +import android.support.v4.content.ContextCompat;
  19 +import android.support.v7.app.AppCompatActivity;
  20 +import android.support.v7.widget.LinearLayoutManager;
  21 +import android.support.v7.widget.RecyclerView;
  22 +import android.text.Editable;
  23 +import android.text.TextUtils;
  24 +import android.text.TextWatcher;
  25 +import android.util.Log;
  26 +import android.view.Gravity;
  27 +import android.view.MotionEvent;
  28 +import android.view.View;
  29 +import android.view.inputmethod.InputMethodManager;
  30 +import android.widget.Button;
  31 +import android.widget.EditText;
  32 +import android.widget.ImageView;
  33 +import android.widget.LinearLayout;
  34 +import android.widget.RelativeLayout;
  35 +import android.widget.TextView;
  36 +import android.widget.Toast;
  37 +
  38 +import com.cnlive.im.R;
  39 +import com.cnlive.im.adapter.ChatRoom3Adpter;
  40 +import com.cnlive.im.adapter.ChatRoomAdpter;
  41 +import com.cnlive.im.adapter.base.ItemClickListener;
  42 +import com.cnlive.im.mode.ImagMessage;
  43 +import com.cnlive.im.mode.StringMessage;
  44 +import com.cnlive.im.mode.UgcVideoMessage;
  45 +import com.cnlive.im.mode.VideoMessage;
  46 +import com.cnlive.im.mode.VoiceMessage;
  47 +import com.cnlive.im.util.AudioRecordUtil;
  48 +import com.cnlive.im.util.FileUtil;
  49 +import com.cnlive.im.util.MediaUtil;
  50 +import com.cnlive.im.util.MessageType;
  51 +import com.cnlive.im.util.TimeUtils;
  52 +import com.cnlive.im.util.UploadImageUtils;
  53 +import com.cnlive.libs.util.chat.ChatUtil;
  54 +import com.cnlive.libs.util.chat.base.IChat;
  55 +import com.cnlive.libs.util.chat.model.CNBaseMessage;
  56 +import com.cnlive.libs.util.chat.model.CNImgMessage;
  57 +import com.cnlive.libs.util.chat.model.CNMessage;
  58 +import com.cnlive.libs.util.chat.model.CNUgcMessage;
  59 +import com.cnlive.libs.util.chat.model.CNUserInfo;
  60 +import com.cnlive.libs.util.chat.model.CNVideoMessage;
  61 +import com.cnlive.libs.util.chat.model.CNVoiceMessage;
  62 +
  63 +import java.io.File;
  64 +import java.io.FileInputStream;
  65 +import java.util.ArrayList;
  66 +
  67 +
  68 +/**
  69 + * Created by gujun on 2017/12/19.
  70 + */
  71 +public class ChatRoom3Activity extends AppCompatActivity implements View.OnClickListener, ItemClickListener {
  72 + private static final String TAG = "ChatRoomActivity";
  73 +
  74 + private final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 0x2001;
  75 +
  76 + private ArrayList<String> msgIds = new ArrayList<>();
  77 + private String roomIdOne = "ChatRoom10";
  78 + private EditText editText;
  79 + private Button send_messageOneBtn;
  80 + private RecyclerView listViewOne;
  81 + private ChatRoom3Adpter chatListAdapterOne;
  82 + private Button foundChatBtn;
  83 + private Handler mHandler;
  84 + private RelativeLayout re_chat;
  85 + private Button exit_chat1;
  86 + public static String user_name_string;
  87 + private ImageView ima_send;
  88 + private Button sendVoice;
  89 +
  90 + private Context context;
  91 + private AudioRecordUtil mAudioRecordUtil;
  92 + private PopupWindowFactory mPop;
  93 + private ImageView mImageView;
  94 + private TextView mTextView;
  95 + private ImageView keybordBtn;
  96 +
  97 + private LinearLayout sendMsgLayout;
  98 + private LinearLayout cancelSendLayout;
  99 +
  100 + private boolean isVoiceBtnVisible = false;
  101 +
  102 +
  103 + public static final int IMG_FROM_PICTURE = 0x1001;
  104 + public static final int PICTURE_REQUEST_CODE = 0x1003;
  105 + public static final int VOICE_REQUEST_CODE = 0x1004;
  106 + int duration;
  107 +
  108 + private String mFilePath;
  109 +
  110 + // 所需的全部权限
  111 + static final String[] PERMISSIONS = new String[]{
  112 + Manifest.permission.RECORD_AUDIO,
  113 + Manifest.permission.WRITE_EXTERNAL_STORAGE
  114 + };
  115 +
  116 + @Override
  117 + protected void onCreate(Bundle savedInstanceState) {
  118 + super.onCreate(savedInstanceState);
  119 + //取消状态栏
  120 + setContentView(R.layout.activity_groupchat_3);
  121 + context = this;
  122 + initUI();
  123 + mAudioRecordUtil = new AudioRecordUtil();
  124 + mAudioRecordUtil.setOnAudioStatusUpdateListener(onAudioStatusUpdateListener);
  125 + //设置监听
  126 + mHandler = new Handler();
  127 + ChatUtil.setOnConnectStatusListener(connectStatusListener);
  128 + ChatUtil.setOnChatRoomActionListener(chatRoomActionListener);
  129 + ChatUtil.setOnReceiveChatroomMessageListener(receiveMessageListener);
  130 + ChatUtil.setUploadProgressListener(uploadProgressListener);
  131 + ChatUtil.setKickedOfflineListener(onKickedOfflineListener);
  132 + Intent intent = getIntent();
  133 + user_name_string = intent.getStringExtra("user_name_string");
  134 + String user_id_string = intent.getStringExtra("user_id_string");
  135 + if (user_name_string != null)
  136 + ChatUtil.login(new CNUserInfo(user_id_string, user_name_string, ""), connectListener);
  137 + else ChatUtil.logOut(connectListener);
  138 + requestPermission();
  139 +
  140 + }
  141 +
  142 +
  143 + private void initUI() {
  144 +
  145 + final View view = View.inflate(this, R.layout.layout_microphone, null);
  146 + mPop = new PopupWindowFactory(this, view);
  147 + mImageView = (ImageView) view.findViewById(R.id.iv_recording_icon);
  148 + mTextView = (TextView) view.findViewById(R.id.tv_recording_time);
  149 + sendMsgLayout = (LinearLayout) view.findViewById(R.id.send_layout);
  150 + cancelSendLayout = (LinearLayout) view.findViewById(R.id.cancel_send_layout);
  151 + ima_send = (ImageView) findViewById(R.id.ima_send);
  152 + exit_chat1 = (Button) findViewById(R.id.exit_chat1);
  153 +// user_id = (EditText) findViewById(R.id.user_ID);
  154 +// user_name = (EditText) findViewById(R.id.user_name);
  155 +// lin_user = (LinearLayout) findViewById(R.id.lin_user);
  156 + re_chat = (RelativeLayout) findViewById(R.id.re_chat);
  157 + foundChatBtn = (Button) findViewById(R.id.creatChat);
  158 + listViewOne = (RecyclerView) findViewById(R.id.chat_list_viewOne);
  159 + editText = (EditText) findViewById(R.id.message_content);
  160 + editText.addTextChangedListener(textWatcher);
  161 + send_messageOneBtn = (Button) findViewById(R.id.send_messageOne);
  162 + ima_send.setOnClickListener(this);
  163 + exit_chat1.setOnClickListener(this);
  164 + foundChatBtn.setOnClickListener(this);
  165 + send_messageOneBtn.setOnClickListener(this);
  166 + chatListAdapterOne = new ChatRoom3Adpter(ChatRoom3Activity.this, false);
  167 + chatListAdapterOne.setonVoiceItemClickListener(this);
  168 + listViewOne.setLayoutManager(new LinearLayoutManager(ChatRoom3Activity.this));
  169 + listViewOne.setAdapter(chatListAdapterOne);
  170 +
  171 + sendVoice = (Button) findViewById(R.id.send_voice);
  172 + re_chat = (RelativeLayout) findViewById(R.id.activity_chat_room);
  173 + keybordBtn = (ImageView) findViewById(R.id.keybord);
  174 + keybordBtn.setOnClickListener(this);
  175 +
  176 + findViewById(R.id.videoBtn).setOnClickListener(this);
  177 + findViewById(R.id.ugcVideoBtn).setOnClickListener(this);
  178 +
  179 +
  180 + StartListener();
  181 +
  182 +
  183 + }
  184 +
  185 +
  186 + /**
  187 + * 发送消息监听
  188 + */
  189 + private IChat.OnSendMessageListener sendMessageListener = new IChat.OnSendMessageListener() {
  190 + @Override
  191 + public void onAttached(CNBaseMessage o) {
  192 + showMessage(o);
  193 + }
  194 +
  195 + @Override
  196 + public void onSuccess(final CNBaseMessage o) {
  197 + updateItem(o, -1);
  198 + }
  199 +
  200 + @Override
  201 + public void onError(CNBaseMessage o, int i, String s) {
  202 + updateItem(o, -1);
  203 + }
  204 + };
  205 +
  206 + //连接状态监听
  207 + private IChat.OnConnectStatusListener connectStatusListener = new IChat.OnConnectStatusListener() {
  208 + @Override
  209 + public void onConnectStatus(int i, String s) {
  210 + Log.i(TAG, "连接状态监听" + i + " " + s);
  211 + }
  212 + };
  213 +
  214 +
  215 + private IChat.OnReceiveMessageListener receiveMessageListener = new IChat.OnReceiveMessageListener() {
  216 + @Override
  217 + public void processMessage(final CNBaseMessage o) {
  218 + if (!o.getTargetId().equals(roomIdOne)) return;
  219 + showMessage(o);
  220 + }
  221 + };
  222 +
  223 + private IChat.OnUploadProgressListener uploadProgressListener = new IChat.OnUploadProgressListener() {
  224 + @Override
  225 + public void onMessagesUpdate(CNBaseMessage message, int elemIdx, int taskId, int progress) {
  226 + if (!message.getTargetId().equals(roomIdOne)) return;
  227 + updateItem(message, progress);
  228 + }
  229 + };
  230 +
  231 +
  232 + /**
  233 + * 更新消息
  234 + */
  235 + public void updateItem(CNBaseMessage o, int progress) {
  236 + int updateIndex = msgIds.indexOf(o.getMessageId());
  237 + if (updateIndex < 0 || chatListAdapterOne.getItemCount() <= 0 || updateIndex > chatListAdapterOne.getItemCount() - 1)
  238 + return;
  239 + if (o.getMsgType() == IChat.CN_MSG_TYPE_TEXT) {
  240 + CNMessage o1 = (CNMessage) o;
  241 + chatListAdapterOne.updateItem(new StringMessage(MessageType.stringMessage, o1, o1.isSelf()), updateIndex);
  242 + } else if (o.getMsgType() == IChat.CN_MSG_TYPE_IMAGE) {
  243 + CNImgMessage o1 = (CNImgMessage) o;
  244 + chatListAdapterOne.updateItem(new ImagMessage(MessageType.imaMessage, o1, o1.isSelf(), progress < 0 ? 0 : progress), updateIndex);
  245 + } else if (o.getMsgType() == IChat.CN_MSG_TYPE_VOICE) {
  246 + CNVoiceMessage o1 = (CNVoiceMessage) o;
  247 + VoiceMessage voiceMessage = new VoiceMessage(MessageType.voiceMessage, o1, o1.isSelf(), progress < 0 ? 0 : progress);
  248 + chatListAdapterOne.updateItem(voiceMessage, updateIndex);
  249 + } else if (o.getMsgType() == IChat.CN_MSG_TYPE_VIDEO) {
  250 + CNVideoMessage ol = (CNVideoMessage) o;
  251 + VideoMessage videoMessage = new VideoMessage(MessageType.videoMessage, ol, ol.isSelf(), progress < 0 ? 0 : progress);
  252 + chatListAdapterOne.updateItem(videoMessage, updateIndex);
  253 + } else if (o.getMsgType() == IChat.CN_MSG_TYPE_UGC) {
  254 + CNUgcMessage ol = (CNUgcMessage) o;
  255 + UgcVideoMessage ugcVideoMessage = new UgcVideoMessage(MessageType.ugcVideoMessage, ol, ol.isSelf(), progress < 0 ? 0 : progress);
  256 + chatListAdapterOne.updateItem(ugcVideoMessage, updateIndex);
  257 + }
  258 + }
  259 +
  260 + /**
  261 + * 展示消息
  262 + *
  263 + * @param o
  264 + */
  265 + private void showMessage(CNBaseMessage o) {
  266 + msgIds.add(o.getMessageId());
  267 + if (o.getMsgType() == IChat.CN_MSG_TYPE_TEXT) {
  268 + CNMessage o1 = (CNMessage) o;
  269 + chatListAdapterOne.addItem(new StringMessage(MessageType.stringMessage, o1, o1.isSelf()));
  270 + } else if (o.getMsgType() == IChat.CN_MSG_TYPE_IMAGE) {
  271 + CNImgMessage o1 = (CNImgMessage) o;
  272 + chatListAdapterOne.addItem(new ImagMessage(MessageType.imaMessage, o1, o1.isSelf()));
  273 + } else if (o.getMsgType() == IChat.CN_MSG_TYPE_VOICE) {
  274 + CNVoiceMessage o1 = (CNVoiceMessage) o;
  275 + VoiceMessage voiceMessage = new VoiceMessage(MessageType.voiceMessage, o1, o1.isSelf());
  276 + chatListAdapterOne.addItem(voiceMessage);
  277 + } else if (o.getMsgType() == IChat.CN_MSG_TYPE_VIDEO) {
  278 + CNVideoMessage o1 = (CNVideoMessage) o;
  279 + VideoMessage videoMessage = new VideoMessage(MessageType.videoMessage, o1, o1.isSelf());
  280 + chatListAdapterOne.addItem(videoMessage);
  281 + } else if (o.getMsgType() == IChat.CN_MSG_TYPE_UGC) {
  282 + CNUgcMessage o1 = (CNUgcMessage) o;
  283 + UgcVideoMessage ugcVideoMessage = new UgcVideoMessage(MessageType.ugcVideoMessage, o1, o1.isSelf());
  284 + chatListAdapterOne.addItem(ugcVideoMessage);
  285 + }
  286 + if (chatListAdapterOne.getItemCount() > 0)
  287 + listViewOne.smoothScrollToPosition(chatListAdapterOne.getItemCount() - 1);
  288 + }
  289 +
  290 +
  291 + //聊天室操作
  292 + private IChat.OnChatRoomActionListener chatRoomActionListener = new IChat.OnChatRoomActionListener() {
  293 + @Override
  294 + public void onJoining(String s) {
  295 + Log.i(TAG, "chatRoomActionListener" + " onJoining" + s);
  296 + }
  297 +
  298 + @Override
  299 + public void onJoined(String s) {
  300 + Log.i(TAG, "chatRoomActionListener " + "onJoined" + s);
  301 + }
  302 +
  303 + @Override
  304 + public void onQuited(String s) {
  305 + Log.i(TAG, "chatRoomActionListener " + "onQuited" + s);
  306 + }
  307 +
  308 + @Override
  309 + public void onError(String s, int i, String s1) {
  310 + Log.i(TAG, "chatRoomActionListener " + "onError" + s);
  311 +
  312 + }
  313 + };
  314 +
  315 + //被踢下线状态
  316 + private IChat.OnKickedOfflineListener onKickedOfflineListener = new IChat.OnKickedOfflineListener() {
  317 + @Override
  318 + public void disconnected(int what, final String extra) {
  319 + Log.e(TAG, what + " : " + extra);
  320 + mHandler.post(new Runnable() {
  321 + @Override
  322 + public void run() {
  323 + Toast.makeText(ChatRoom3Activity.this, extra, Toast.LENGTH_SHORT).show();
  324 + }
  325 + });
  326 + }
  327 +
  328 + @Override
  329 + public void conByDevId(final int what, final String extra) {
  330 + Log.e(TAG, what + " : " + extra);
  331 + mHandler.post(new Runnable() {
  332 + @Override
  333 + public void run() {
  334 + Toast.makeText(ChatRoom3Activity.this, extra, Toast.LENGTH_SHORT).show();
  335 +
  336 + }
  337 + });
  338 + }
  339 + };
  340 +
  341 +
  342 + // 加入聊天室监听
  343 + private IChat.OnOperationListener onOperationListener = new IChat.OnOperationListener() {
  344 + @Override
  345 + public void onSuccess(int what, final String extra) {
  346 + Log.v(TAG, +what + extra);
  347 +
  348 +
  349 + }
  350 +
  351 + @Override
  352 + public void onError(int what, String extra) {
  353 + Log.v(TAG, "加入聊天室" + what + extra);
  354 +
  355 + }
  356 + };
  357 +
  358 + @Override
  359 + public void onClick(View view) {
  360 + switch (view.getId()) {
  361 + //向聊天室1发送消息
  362 + case R.id.send_messageOne:
  363 + //发送聊天室消息
  364 + if (editText.getText().toString().isEmpty()) {
  365 + Toast.makeText(this, "不能发送空消息呦~", Toast.LENGTH_SHORT).show();
  366 + } else {
  367 + ChatUtil.sendMessage(IChat.CHATROOM, roomIdOne, editText.getText().toString(), sendMessageListener);
  368 + editText.setText("");
  369 + }
  370 + break;
  371 + case R.id.creatChat:
  372 + ChatUtil.joinChatRoom(roomIdOne, -1, onOperationListener);
  373 + break;
  374 + case R.id.exit_chat1:
  375 + ChatUtil.quitChatRoom(roomIdOne, onOperationListener);
  376 + break;
  377 + case R.id.ima_send:
  378 + fromPicture();
  379 + break;
  380 + case R.id.keybord:
  381 + if (!isVoiceBtnVisible) {
  382 + editText.setVisibility(View.GONE);
  383 + sendVoice.setVisibility(View.VISIBLE);
  384 + keybordBtn.setImageResource(R.drawable.keybord);
  385 + InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
  386 + imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
  387 + isVoiceBtnVisible = true;
  388 + } else {
  389 + sendVoice.setVisibility(View.GONE);
  390 + editText.setVisibility(View.VISIBLE);
  391 + keybordBtn.setImageResource(R.drawable.voice);
  392 + isVoiceBtnVisible = false;
  393 + }
  394 + break;
  395 + case R.id.videoBtn:
  396 + openSystemRecorder();
  397 + break;
  398 + case R.id.ugcVideoBtn:
  399 + openSystemRecorder();
  400 + break;
  401 + }
  402 + }
  403 +
  404 + private void openSystemRecorder() {
  405 + // 创建拍照Intent并将控制权返回给调用的程序
  406 + Intent intent = new Intent();
  407 + intent.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
  408 + intent.addCategory(Intent.CATEGORY_DEFAULT);
  409 +
  410 + // 保存录像到指定的路径
  411 + File file = new File("/sdcard/video.mp4");
  412 + Uri uri = Uri.fromFile(file);
  413 + intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
  414 +
  415 + // 启动图像捕获Intent
  416 + startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);
  417 + }
  418 +
  419 +
  420 + public void fromPicture() {
  421 + if (Build.VERSION.SDK_INT >= 23) {
  422 + int checkCallPhonePermission = ContextCompat.checkSelfPermission(ChatRoom3Activity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
  423 + if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {
  424 + requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PICTURE_REQUEST_CODE);
  425 + return;
  426 + } else {
  427 + toIntentPicture();
  428 + }
  429 + } else {
  430 + toIntentPicture();
  431 + }
  432 + }
  433 +
  434 +
  435 + private void toIntentPicture() {
  436 + Intent intent = new Intent();
  437 + intent.setType("image/*");
  438 + intent.setAction(Intent.ACTION_PICK);
  439 + startActivityForResult(Intent.createChooser(intent, "Complete action using"), IMG_FROM_PICTURE);
  440 + }
  441 +
  442 +
  443 + @Override
  444 + public void onActivityResult(int requestCode, int resultCode, Intent data) {
  445 + super.onActivityResult(requestCode, resultCode, data);
  446 + if (resultCode == Activity.RESULT_OK) {
  447 + switch (requestCode) {
  448 + case IMG_FROM_PICTURE:
  449 + Uri selectedImage = data.getData();
  450 + String picturePath = UploadImageUtils.getPath(this, selectedImage);
  451 + Uri uri = Uri.parse("file://" + picturePath);
  452 + ChatUtil.sendImaMessage(IChat.CHATROOM, roomIdOne, uri, uri, sendMessageListener);
  453 + break;
  454 + case CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE:
  455 + String videoPath = data.getData().getPath();
  456 + Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, MediaStore.Images.Thumbnails.FULL_SCREEN_KIND);
  457 + String imgPath = FileUtil.createFile(bitmap, System.currentTimeMillis() + "");
  458 + ChatUtil.sendVideoMessage(IChat.CHATROOM, roomIdOne, videoPath, imgPath, MediaUtil.getInstance().getDuration(videoPath), sendMessageListener);
  459 + break;
  460 + default:
  461 + break;
  462 + }
  463 + }
  464 + }
  465 +
  466 +
  467 + private IChat.OnConnectListener connectListener = new IChat.OnConnectListener() {
  468 +
  469 +
  470 + @Override
  471 + public void onTokenIncorrect(String s) {
  472 + Log.e(TAG, "ConnectListener TokenIncorrect");
  473 +
  474 + }
  475 +
  476 + @Override
  477 + public void onSuccess(final String s) {
  478 + Log.e(TAG, "ConnectListener 连接监听" + s);
  479 +
  480 + }
  481 +
  482 + @Override
  483 + public void onError(int i, final String s) {
  484 + Log.e(TAG, "ConnectListener errorCode : " + i + " : errorMessage : " + s);
  485 +
  486 +
  487 + }
  488 + };
  489 +
  490 + /**
  491 + * 播放语音文件
  492 + *
  493 + * @param voicePath
  494 + * @param animationDrawable
  495 + */
  496 + private void startVoice(String voicePath, final AnimationDrawable animationDrawable) {
  497 + try {
  498 + FileInputStream fis = new FileInputStream(voicePath);
  499 + MediaUtil.getInstance().play(fis);
  500 + animationDrawable.start();
  501 + MediaUtil.getInstance().setEventListener(new MediaUtil.EventListener() {
  502 + @Override
  503 + public void onStop() {
  504 + animationDrawable.stop();
  505 + animationDrawable.selectDrawable(0);
  506 + }
  507 + });
  508 + } catch (Exception e) {
  509 + }
  510 + }
  511 +
  512 + @Override
  513 + public void onItemClick(final VoiceMessage voiceMessage, final AnimationDrawable animationDrawable) {
  514 + if (voiceMessage == null) {
  515 + Toast.makeText(this, "数据异常", Toast.LENGTH_LONG).show();
  516 + return;
  517 + }
  518 + if (voiceMessage.getCnVoiceMessage().isSelf()) {
  519 + startVoice(voiceMessage.getCnVoiceMessage().getVoiceUri().getPath(), animationDrawable);
  520 + } else {
  521 + //设置本地已读
  522 + voiceMessage.getCnVoiceMessage().setRead(true);
  523 + String voiceUuid = voiceMessage.getCnVoiceMessage().getUuid();
  524 + final String voicePath = FileUtil.getCacheFilePath(voiceUuid);
  525 + if (FileUtil.isCacheFileExist(voiceUuid)) {
  526 + File file = new File(voicePath);
  527 + if (file.length() < voiceMessage.getCnVoiceMessage().getDataSize()) {
  528 + Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
  529 + return;
  530 + }
  531 + startVoice(voicePath, animationDrawable);
  532 + } else {
  533 + if (voiceMessage.isDownloading()) {
  534 + Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
  535 + } else {
  536 + voiceMessage.setDownloading(true);
  537 + voiceMessage.getCnVoiceMessage().getVoice(voicePath, new IChat.OnDownloadListener() {
  538 + @Override
  539 + public void onSuccess() {
  540 + voiceMessage.setDownloading(false);
  541 + startVoice(voicePath, animationDrawable);
  542 + }
  543 +
  544 + @Override
  545 + public void onError(int i, String s) {
  546 + voiceMessage.setDownloading(false);
  547 + }
  548 + });
  549 + }
  550 + }
  551 + }
  552 + }
  553 +
  554 + @Override
  555 + public void onVideoClick(final VideoMessage videoMessage) {
  556 + if (videoMessage.getCnVideoMessage().isSelf()) {
  557 + Intent intent = new Intent(ChatRoom3Activity.this, VideoActivity.class);
  558 + intent.putExtra("videoPath", videoMessage.getCnVideoMessage().getVideoPath());
  559 + startActivity(intent);
  560 + } else {
  561 + String videoUuid = videoMessage.getCnVideoMessage().getVideo().getUuid();
  562 + final String videoPath = FileUtil.getCacheFilePath(videoUuid);
  563 + if (FileUtil.isCacheFileExist(videoUuid)) {
  564 + File file = new File(videoPath);
  565 + if (file.length() < videoMessage.getCnVideoMessage().getVideo().getSize()) {
  566 + Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
  567 + return;
  568 + }
  569 + Intent intent = new Intent(ChatRoom3Activity.this, VideoActivity.class);
  570 + intent.putExtra("videoPath", videoPath);
  571 + startActivity(intent);
  572 + } else {
  573 + if (videoMessage.isDownloading()) {
  574 + Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
  575 + } else {
  576 + videoMessage.setDownloading(true);
  577 + videoMessage.getCnVideoMessage().getVideo().getVideo(videoPath, new IChat.OnDownloadListener() {
  578 + @Override
  579 + public void onSuccess() {
  580 + videoMessage.setDownloading(false);
  581 + Intent intent = new Intent(ChatRoom3Activity.this, VideoActivity.class);
  582 + intent.putExtra("videoPath", videoPath);
  583 + startActivity(intent);
  584 + }
  585 +
  586 + @Override
  587 + public void onError(int i, String s) {
  588 + videoMessage.setDownloading(false);
  589 + }
  590 + });
  591 + }
  592 + }
  593 + }
  594 +
  595 + }
  596 +
  597 + @Override
  598 + public void onUgcVideoClick(final UgcVideoMessage ugcVideoMessage) {
  599 + if (ugcVideoMessage.getCnUgcMessage().isSelf()) {
  600 + Intent intent = new Intent(ChatRoom3Activity.this, VideoActivity.class);
  601 + intent.putExtra("videoPath", ugcVideoMessage.getCnUgcMessage().getVideoPath());
  602 + startActivity(intent);
  603 + } else {
  604 + String videoUuid = ugcVideoMessage.getCnUgcMessage().getFileId() + "_video";
  605 + final String videoPath = FileUtil.getCacheFilePath(videoUuid);
  606 + if (FileUtil.isCacheFileExist(videoUuid)) {
  607 + File file = new File(videoPath);
  608 + if (file.length() < ugcVideoMessage.getCnUgcMessage().getVideo().getSize()) {
  609 + Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
  610 + return;
  611 + }
  612 + Intent intent = new Intent(ChatRoom3Activity.this, VideoActivity.class);
  613 + intent.putExtra("videoPath", videoPath);
  614 + startActivity(intent);
  615 + } else {
  616 + if (ugcVideoMessage.isDownloading()) {
  617 + Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
  618 + } else {
  619 + ugcVideoMessage.setDownloading(true);
  620 + ugcVideoMessage.getCnUgcMessage().getVideo().getVideo(videoPath, new IChat.OnDownloadListener() {
  621 + @Override
  622 + public void onSuccess() {
  623 + ugcVideoMessage.setDownloading(false);
  624 + Intent intent = new Intent(ChatRoom3Activity.this, VideoActivity.class);
  625 + intent.putExtra("videoPath", videoPath);
  626 + startActivity(intent);
  627 + }
  628 +
  629 + @Override
  630 + public void onError(int i, String s) {
  631 + ugcVideoMessage.setDownloading(false);
  632 + }
  633 + });
  634 + }
  635 + }
  636 + }
  637 + }
  638 +
  639 + @Override
  640 + public void onImageClick(final ImagMessage imagMessage) {
  641 + if (imagMessage.getCnImgMessage().isSelf()) {
  642 + Intent intent = new Intent(ChatRoom3Activity.this, ImageViewActivity.class);
  643 + intent.putExtra("path", imagMessage.getCnImgMessage().getImagePath());
  644 + startActivity(intent);
  645 + } else {
  646 + String remoteUuid = imagMessage.getCnImgMessage().getRemoteUuid();
  647 + final String imagePath = FileUtil.getCacheFilePath(remoteUuid);
  648 + if (FileUtil.isCacheFileExist(remoteUuid)) {
  649 + File file = new File(imagePath);
  650 + if (file.length() < imagMessage.getCnImgMessage().getRemoteSize()) {
  651 + Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
  652 + return;
  653 + }
  654 + Intent intent = new Intent(ChatRoom3Activity.this, ImageViewActivity.class);
  655 + intent.putExtra("path", imagePath);
  656 + startActivity(intent);
  657 + } else {
  658 + if (imagMessage.isDownloading()) {
  659 + Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
  660 + } else {
  661 + imagMessage.setDownloading(true);
  662 + imagMessage.getCnImgMessage().getRemoteImage(imagePath, new IChat.OnDownloadListener() {
  663 + @Override
  664 + public void onSuccess() {
  665 + imagMessage.setDownloading(false);
  666 + Intent intent = new Intent(ChatRoom3Activity.this, ImageViewActivity.class);
  667 + intent.putExtra("path", imagePath);
  668 + startActivity(intent);
  669 + }
  670 +
  671 + @Override
  672 + public void onError(int i, String s) {
  673 + imagMessage.setDownloading(false);
  674 + Toast.makeText(context, "下载失败", Toast.LENGTH_SHORT).show();
  675 + }
  676 + });
  677 + }
  678 + }
  679 + }
  680 + }
  681 +
  682 +
  683 + /**************************************************************************/
  684 +
  685 + /**
  686 + * 开启扫描之前判断权限是否打开
  687 + */
  688 + private void requestPermission() {
  689 + //判断是否开启摄像头权限
  690 + if ((ContextCompat.checkSelfPermission(context,
  691 + Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) &&
  692 + (ContextCompat.checkSelfPermission(context,
  693 + Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED)
  694 + && (ContextCompat.checkSelfPermission(context,
  695 + Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)) {
  696 + StartListener();
  697 +
  698 + //判断是否开启语音权限
  699 + } else {
  700 + //请求获取摄像头权限
  701 + ActivityCompat.requestPermissions((Activity) context,
  702 + new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA}, VOICE_REQUEST_CODE);
  703 + }
  704 + }
  705 +
  706 + @Override
  707 + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  708 + super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  709 + if (requestCode == VOICE_REQUEST_CODE) {
  710 + if ((grantResults[0] == PackageManager.PERMISSION_GRANTED) && (grantResults[1] == PackageManager.PERMISSION_GRANTED)) {
  711 + StartListener();
  712 + } else {
  713 + Toast.makeText(context, "已拒绝权限!", Toast.LENGTH_SHORT).show();
  714 + }
  715 + }
  716 + }
  717 +
  718 + float downX;
  719 + float downY;
  720 +
  721 + public void StartListener() {
  722 +
  723 + //Button的touch监听
  724 + sendVoice.setOnTouchListener(new View.OnTouchListener() {
  725 + @Override
  726 + public boolean onTouch(View v, MotionEvent event) {
  727 + if (lacksPermissions(PERMISSIONS)) {
  728 + if (lacksPermission(PERMISSIONS[0])) {
  729 + Toast.makeText(ChatRoom3Activity.this, "没有麦克风权限,请前往“设置 - 应用 - 权限”中开启权限", Toast.LENGTH_SHORT).show();
  730 +
  731 + } else if (lacksPermission(PERMISSIONS[1])) {
  732 + Toast.makeText(ChatRoom3Activity.this, "没有文件读写权限,请前往“设置 - 应用 - 权限”中开启权限", Toast.LENGTH_SHORT).show();
  733 +
  734 + }
  735 + } else {
  736 +
  737 + switch (event.getAction()) {
  738 +
  739 + case MotionEvent.ACTION_DOWN:
  740 + mPop.showAtLocation(re_chat, Gravity.CENTER, 0, 0);
  741 +
  742 + sendVoice.setText("松开发送");
  743 + mAudioRecordUtil.startRecord();
  744 + downX = event.getX();
  745 + downY = event.getY();
  746 +
  747 + break;
  748 +
  749 + case MotionEvent.ACTION_UP:
  750 +
  751 + if (cancelSendLayout.getVisibility() == View.VISIBLE) {
  752 + cancelSendLayout.setVisibility(View.GONE);
  753 + sendMsgLayout.setVisibility(View.VISIBLE);
  754 + }
  755 + float moveX = Math.abs(event.getX() - downX);
  756 + float moveY = Math.abs(event.getY() - downY);
  757 + if (moveY > 100) {
  758 +// Toast.makeText(ChatRoomActivity.this, "cancel", Toast.LENGTH_SHORT).show();
  759 + mAudioRecordUtil.cancelRecord();
  760 + } else {
  761 + mAudioRecordUtil.stopRecord(); //结束录音(保存录音文件)
  762 + }
  763 + mPop.dismiss();
  764 + mTextView.setText("00:00");
  765 + sendVoice.setText("按住说话");
  766 + break;
  767 + case MotionEvent.ACTION_MOVE:
  768 + float moveXm = Math.abs(event.getX() - downX);
  769 + float moveYm = Math.abs(event.getY() - downY);
  770 + if (moveYm > 100) {
  771 +// Toast.makeText(ChatRoomActivity.this, "cancel", Toast.LENGTH_SHORT).show();
  772 + if (sendMsgLayout.getVisibility() == View.VISIBLE) {
  773 + sendMsgLayout.setVisibility(View.GONE);
  774 + cancelSendLayout.setVisibility(View.VISIBLE);
  775 + }
  776 + } else {
  777 + if (cancelSendLayout.getVisibility() == View.VISIBLE) {
  778 + cancelSendLayout.setVisibility(View.GONE);
  779 + sendMsgLayout.setVisibility(View.VISIBLE);
  780 + }
  781 + }
  782 + break;
  783 + }
  784 + }
  785 + return true;
  786 + }
  787 + });
  788 +
  789 + }
  790 +
  791 + // 判断权限集合
  792 + public boolean lacksPermissions(String... permissions) {
  793 + for (String permission : permissions) {
  794 + if (lacksPermission(permission)) {
  795 + return true;
  796 + }
  797 + }
  798 + return false;
  799 + }
  800 +
  801 + // 判断是否缺少权限
  802 + private boolean lacksPermission(String permission) {
  803 + return ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_DENIED;
  804 + }
  805 +
  806 + private AudioRecordUtil.OnAudioStatusUpdateListener onAudioStatusUpdateListener = new AudioRecordUtil.OnAudioStatusUpdateListener() {
  807 + @Override
  808 + public void onUpdate(double db, long time) {
  809 + mImageView.getDrawable().setLevel((int) (3000 + 6000 * db / 100));
  810 + mTextView.setText(TimeUtils.long2String(time));
  811 + }
  812 +
  813 + @Override
  814 + public void onStop(String filePath) {
  815 + duration = (int) mAudioRecordUtil.getDuration();
  816 + mFilePath = filePath;
  817 + ChatUtil.sendVoiceMessage(IChat.CHATROOM, roomIdOne, Uri.parse(mFilePath), duration, sendMessageListener);
  818 + }
  819 + };
  820 +
  821 + private TextWatcher textWatcher = new TextWatcher() {
  822 + @Override
  823 + public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  824 + }
  825 +
  826 + @Override
  827 + public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  828 +
  829 + }
  830 +
  831 + @Override
  832 + public void afterTextChanged(Editable editable) {
  833 + if (!TextUtils.isEmpty(editable)) {
  834 + ima_send.setVisibility(View.GONE);
  835 + send_messageOneBtn.setVisibility(View.VISIBLE);
  836 + } else {
  837 + send_messageOneBtn.setVisibility(View.GONE);
  838 + ima_send.setVisibility(View.VISIBLE);
  839 + }
  840 + }
  841 + };
  842 +
  843 + @Override
  844 + protected void onPause() {
  845 + super.onPause();
  846 + MediaUtil.getInstance().stop();
  847 + }
  848 +
  849 + @Override
  850 + protected void onDestroy() {
  851 + super.onDestroy();
  852 + ChatUtil.removeUploadProgressListener(uploadProgressListener);
  853 + ChatUtil.removeReceiveMessageListener(receiveMessageListener);
  854 + ChatUtil.removeConnectListener(connectListener);
  855 + ChatUtil.removeConnectStatusListener(connectStatusListener);
  856 + }
  857 +
  858 +}
app/src/main/java/com/cnlive/im/ui/ChatRoomActivity.java
@@ -5,14 +5,10 @@ import android.app.Activity; @@ -5,14 +5,10 @@ import android.app.Activity;
5 import android.content.Context; 5 import android.content.Context;
6 import android.content.Intent; 6 import android.content.Intent;
7 import android.content.pm.PackageManager; 7 import android.content.pm.PackageManager;
8 -import android.graphics.Bitmap;  
9 -import android.graphics.drawable.AnimationDrawable;  
10 -import android.media.ThumbnailUtils;  
11 import android.net.Uri; 8 import android.net.Uri;
12 import android.os.Build; 9 import android.os.Build;
13 import android.os.Bundle; 10 import android.os.Bundle;
14 import android.os.Handler; 11 import android.os.Handler;
15 -import android.provider.MediaStore;  
16 import android.support.annotation.NonNull; 12 import android.support.annotation.NonNull;
17 import android.support.v4.app.ActivityCompat; 13 import android.support.v4.app.ActivityCompat;
18 import android.support.v4.content.ContextCompat; 14 import android.support.v4.content.ContextCompat;
@@ -37,16 +33,14 @@ import android.widget.Toast; @@ -37,16 +33,14 @@ import android.widget.Toast;
37 33
38 import com.cnlive.im.R; 34 import com.cnlive.im.R;
39 import com.cnlive.im.adapter.ChatRoomAdpter; 35 import com.cnlive.im.adapter.ChatRoomAdpter;
40 -import com.cnlive.im.adapter.base.ItemClickListener; 36 +import com.cnlive.im.adapter.base.VoiceItemClickListener;
41 import com.cnlive.im.mode.ImagMessage; 37 import com.cnlive.im.mode.ImagMessage;
42 import com.cnlive.im.mode.StringMessage; 38 import com.cnlive.im.mode.StringMessage;
43 -import com.cnlive.im.mode.UgcVideoMessage;  
44 -import com.cnlive.im.mode.VideoMessage;  
45 import com.cnlive.im.mode.VoiceMessage; 39 import com.cnlive.im.mode.VoiceMessage;
46 import com.cnlive.im.util.AudioRecordUtil; 40 import com.cnlive.im.util.AudioRecordUtil;
47 import com.cnlive.im.util.FileUtil; 41 import com.cnlive.im.util.FileUtil;
48 -import com.cnlive.im.util.MediaUtil;  
49 import com.cnlive.im.util.MessageType; 42 import com.cnlive.im.util.MessageType;
  43 +import com.cnlive.im.util.PlayerUtil;
50 import com.cnlive.im.util.TimeUtils; 44 import com.cnlive.im.util.TimeUtils;
51 import com.cnlive.im.util.UploadImageUtils; 45 import com.cnlive.im.util.UploadImageUtils;
52 import com.cnlive.libs.util.chat.ChatUtil; 46 import com.cnlive.libs.util.chat.ChatUtil;
@@ -54,13 +48,9 @@ import com.cnlive.libs.util.chat.base.IChat; @@ -54,13 +48,9 @@ import com.cnlive.libs.util.chat.base.IChat;
54 import com.cnlive.libs.util.chat.model.CNBaseMessage; 48 import com.cnlive.libs.util.chat.model.CNBaseMessage;
55 import com.cnlive.libs.util.chat.model.CNImgMessage; 49 import com.cnlive.libs.util.chat.model.CNImgMessage;
56 import com.cnlive.libs.util.chat.model.CNMessage; 50 import com.cnlive.libs.util.chat.model.CNMessage;
57 -import com.cnlive.libs.util.chat.model.CNUgcMessage;  
58 import com.cnlive.libs.util.chat.model.CNUserInfo; 51 import com.cnlive.libs.util.chat.model.CNUserInfo;
59 -import com.cnlive.libs.util.chat.model.CNVideoMessage;  
60 import com.cnlive.libs.util.chat.model.CNVoiceMessage; 52 import com.cnlive.libs.util.chat.model.CNVoiceMessage;
61 53
62 -import java.io.File;  
63 -import java.io.FileInputStream;  
64 import java.util.ArrayList; 54 import java.util.ArrayList;
65 55
66 56
@@ -69,13 +59,10 @@ import java.util.ArrayList; @@ -69,13 +59,10 @@ import java.util.ArrayList;
69 * @time 2017/3/7 14:36 59 * @time 2017/3/7 14:36
70 * @desc ${TODD} 60 * @desc ${TODD}
71 */ 61 */
72 -public class ChatRoomActivity extends AppCompatActivity implements View.OnClickListener, ItemClickListener { 62 +public class ChatRoomActivity extends AppCompatActivity implements View.OnClickListener, VoiceItemClickListener {
73 private static final String TAG = "ChatRoomActivity"; 63 private static final String TAG = "ChatRoomActivity";
74 -  
75 - private final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 0x2001;  
76 -  
77 - private ArrayList<String> msgIds = new ArrayList<>();  
78 - private String roomIdOne = "ChatRoom10"; 64 + private ArrayList<Object> groupChatOneListMy;
  65 + private String roomIdOne = "ChatRoom05";
79 private EditText editText; 66 private EditText editText;
80 private Button send_messageOneBtn; 67 private Button send_messageOneBtn;
81 private RecyclerView listViewOne; 68 private RecyclerView listViewOne;
@@ -113,6 +100,8 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL @@ -113,6 +100,8 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
113 100
114 private String mFilePath; 101 private String mFilePath;
115 102
  103 + private PlayerUtil playerUtil;
  104 +
116 // 所需的全部权限 105 // 所需的全部权限
117 static final String[] PERMISSIONS = new String[]{ 106 static final String[] PERMISSIONS = new String[]{
118 Manifest.permission.RECORD_AUDIO, 107 Manifest.permission.RECORD_AUDIO,
@@ -132,8 +121,7 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL @@ -132,8 +121,7 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
132 mHandler = new Handler(); 121 mHandler = new Handler();
133 ChatUtil.setOnConnectStatusListener(connectStatusListener); 122 ChatUtil.setOnConnectStatusListener(connectStatusListener);
134 ChatUtil.setOnChatRoomActionListener(chatRoomActionListener); 123 ChatUtil.setOnChatRoomActionListener(chatRoomActionListener);
135 - ChatUtil.setOnReceiveChatroomMessageListener(receiveMessageListener);  
136 - ChatUtil.setUploadProgressListener(uploadProgressListener); 124 + ChatUtil.setOnReceiveMessageListener(receiveMessageListener);
137 ChatUtil.setKickedOfflineListener(onKickedOfflineListener); 125 ChatUtil.setKickedOfflineListener(onKickedOfflineListener);
138 Intent intent = getIntent(); 126 Intent intent = getIntent();
139 user_name_string = intent.getStringExtra("user_name_string"); 127 user_name_string = intent.getStringExtra("user_name_string");
@@ -142,6 +130,7 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL @@ -142,6 +130,7 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
142 ChatUtil.login(new CNUserInfo(user_id_string, user_name_string, ""), connectListener); 130 ChatUtil.login(new CNUserInfo(user_id_string, user_name_string, ""), connectListener);
143 else ChatUtil.logOut(connectListener); 131 else ChatUtil.logOut(connectListener);
144 requestPermission(); 132 requestPermission();
  133 + playerUtil = new PlayerUtil(this);
145 134
146 } 135 }
147 136
@@ -169,6 +158,7 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL @@ -169,6 +158,7 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
169 exit_chat1.setOnClickListener(this); 158 exit_chat1.setOnClickListener(this);
170 foundChatBtn.setOnClickListener(this); 159 foundChatBtn.setOnClickListener(this);
171 send_messageOneBtn.setOnClickListener(this); 160 send_messageOneBtn.setOnClickListener(this);
  161 + groupChatOneListMy = new ArrayList<>();
172 chatListAdapterOne = new ChatRoomAdpter(ChatRoomActivity.this, false); 162 chatListAdapterOne = new ChatRoomAdpter(ChatRoomActivity.this, false);
173 chatListAdapterOne.setonVoiceItemClickListener(this); 163 chatListAdapterOne.setonVoiceItemClickListener(this);
174 listViewOne.setLayoutManager(new LinearLayoutManager(ChatRoomActivity.this)); 164 listViewOne.setLayoutManager(new LinearLayoutManager(ChatRoomActivity.this));
@@ -179,9 +169,6 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL @@ -179,9 +169,6 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
179 keybordBtn = (ImageView) findViewById(R.id.keybord); 169 keybordBtn = (ImageView) findViewById(R.id.keybord);
180 keybordBtn.setOnClickListener(this); 170 keybordBtn.setOnClickListener(this);
181 171
182 - findViewById(R.id.videoBtn).setOnClickListener(this);  
183 - findViewById(R.id.ugcVideoBtn).setOnClickListener(this);  
184 -  
185 172
186 StartListener(); 173 StartListener();
187 174
@@ -195,20 +182,42 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL @@ -195,20 +182,42 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
195 private IChat.OnSendMessageListener sendMessageListener = new IChat.OnSendMessageListener() { 182 private IChat.OnSendMessageListener sendMessageListener = new IChat.OnSendMessageListener() {
196 @Override 183 @Override
197 public void onAttached(CNBaseMessage o) { 184 public void onAttached(CNBaseMessage o) {
198 - showMessage(o); 185 +
199 } 186 }
200 187
201 @Override 188 @Override
202 public void onSuccess(final CNBaseMessage o) { 189 public void onSuccess(final CNBaseMessage o) {
203 - updateItem(o, -1); 190 + mHandler.post(new Runnable() {
  191 + @Override
  192 + public void run() {
  193 + if (o instanceof CNMessage) {
  194 + CNMessage o1 = (CNMessage) o;
  195 + chatListAdapterOne.addItem(new StringMessage(MessageType.stringMessage, o1, true));
  196 + } else if (o instanceof CNImgMessage) {
  197 + CNImgMessage o1 = (CNImgMessage) o;
  198 + chatListAdapterOne.addItem(new ImagMessage(MessageType.imaMessage, o1, true));
  199 + } else if (o instanceof CNVoiceMessage) {
  200 + CNVoiceMessage o1 = (CNVoiceMessage) o;
  201 + VoiceMessage voiceMessage = new VoiceMessage(MessageType.voiceMessage, o1, true);
  202 + chatListAdapterOne.addItem(voiceMessage);
  203 + //发送成功后删除保存在本地的录制文件
  204 + mAudioRecordUtil.deleteRecordFile(mFilePath);
  205 +
  206 + }
  207 + listViewOne.smoothScrollToPosition(chatListAdapterOne.getItemCount() - 1);
  208 + }
  209 + });
204 } 210 }
205 211
206 @Override 212 @Override
207 public void onError(CNBaseMessage o, int i, String s) { 213 public void onError(CNBaseMessage o, int i, String s) {
208 - updateItem(o, -1); 214 + if (o instanceof CNImgMessage) {
  215 +
  216 + }
209 } 217 }
210 }; 218 };
211 219
  220 +
212 //连接状态监听 221 //连接状态监听
213 private IChat.OnConnectStatusListener connectStatusListener = new IChat.OnConnectStatusListener() { 222 private IChat.OnConnectStatusListener connectStatusListener = new IChat.OnConnectStatusListener() {
214 @Override 223 @Override
@@ -221,79 +230,28 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL @@ -221,79 +230,28 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
221 private IChat.OnReceiveMessageListener receiveMessageListener = new IChat.OnReceiveMessageListener() { 230 private IChat.OnReceiveMessageListener receiveMessageListener = new IChat.OnReceiveMessageListener() {
222 @Override 231 @Override
223 public void processMessage(final CNBaseMessage o) { 232 public void processMessage(final CNBaseMessage o) {
224 - if (!o.getTargetId().equals(roomIdOne)) return;  
225 - showMessage(o);  
226 - }  
227 - }; 233 + mHandler.post(new Runnable() {
  234 + @Override
  235 + public void run() {
  236 + if (o instanceof CNMessage) {
  237 + CNMessage o1 = (CNMessage) o;
  238 + chatListAdapterOne.addItem(new StringMessage(MessageType.stringMessage, o1, false));
  239 + } else if (o instanceof CNImgMessage) {
  240 + CNImgMessage o1 = (CNImgMessage) o;
  241 + chatListAdapterOne.addItem(new ImagMessage(MessageType.imaMessage, o1, false));
  242 + } else if (o instanceof CNVoiceMessage) {
  243 + CNVoiceMessage o1 = (CNVoiceMessage) o;
  244 + VoiceMessage voiceMessage = new VoiceMessage(MessageType.voiceMessage, o1, false);
  245 + chatListAdapterOne.addItem(voiceMessage);
  246 + }
  247 + listViewOne.smoothScrollToPosition(chatListAdapterOne.getItemCount() - 1);
  248 + }
  249 + });
228 250
229 - private IChat.OnUploadProgressListener uploadProgressListener = new IChat.OnUploadProgressListener() {  
230 - @Override  
231 - public void onMessagesUpdate(CNBaseMessage message, int elemIdx, int taskId, int progress) {  
232 - if (!message.getTargetId().equals(roomIdOne)) return;  
233 - updateItem(message, progress);  
234 } 251 }
235 }; 252 };
236 253
237 254
238 - /**  
239 - * 更新消息  
240 - */  
241 - public void updateItem(CNBaseMessage o, int progress) {  
242 - int updateIndex = msgIds.indexOf(o.getMessageId());  
243 - if (updateIndex < 0 || chatListAdapterOne.getItemCount() <= 0 || updateIndex > chatListAdapterOne.getItemCount() - 1)  
244 - return;  
245 - if (o.getMsgType() == IChat.CN_MSG_TYPE_TEXT) {  
246 - CNMessage o1 = (CNMessage) o;  
247 - chatListAdapterOne.updateItem(new StringMessage(MessageType.stringMessage, o1, o1.isSelf()), updateIndex);  
248 - } else if (o.getMsgType() == IChat.CN_MSG_TYPE_IMAGE) {  
249 - CNImgMessage o1 = (CNImgMessage) o;  
250 - chatListAdapterOne.updateItem(new ImagMessage(MessageType.imaMessage, o1, o1.isSelf(), progress < 0 ? 0 : progress), updateIndex);  
251 - } else if (o.getMsgType() == IChat.CN_MSG_TYPE_VOICE) {  
252 - CNVoiceMessage o1 = (CNVoiceMessage) o;  
253 - VoiceMessage voiceMessage = new VoiceMessage(MessageType.voiceMessage, o1, o1.isSelf(), progress < 0 ? 0 : progress);  
254 - chatListAdapterOne.updateItem(voiceMessage, updateIndex);  
255 - } else if (o.getMsgType() == IChat.CN_MSG_TYPE_VIDEO) {  
256 - CNVideoMessage ol = (CNVideoMessage) o;  
257 - VideoMessage videoMessage = new VideoMessage(MessageType.videoMessage, ol, ol.isSelf(), progress < 0 ? 0 : progress);  
258 - chatListAdapterOne.updateItem(videoMessage, updateIndex);  
259 - } else if (o.getMsgType() == IChat.CN_MSG_TYPE_UGC) {  
260 - CNUgcMessage ol = (CNUgcMessage) o;  
261 - UgcVideoMessage ugcVideoMessage = new UgcVideoMessage(MessageType.ugcVideoMessage, ol, ol.isSelf(), progress < 0 ? 0 : progress);  
262 - chatListAdapterOne.updateItem(ugcVideoMessage, updateIndex);  
263 - }  
264 - }  
265 -  
266 - /**  
267 - * 展示消息  
268 - *  
269 - * @param o  
270 - */  
271 - private void showMessage(CNBaseMessage o) {  
272 - msgIds.add(o.getMessageId());  
273 - if (o.getMsgType() == IChat.CN_MSG_TYPE_TEXT) {  
274 - CNMessage o1 = (CNMessage) o;  
275 - chatListAdapterOne.addItem(new StringMessage(MessageType.stringMessage, o1, o1.isSelf()));  
276 - } else if (o.getMsgType() == IChat.CN_MSG_TYPE_IMAGE) {  
277 - CNImgMessage o1 = (CNImgMessage) o;  
278 - chatListAdapterOne.addItem(new ImagMessage(MessageType.imaMessage, o1, o1.isSelf()));  
279 - } else if (o.getMsgType() == IChat.CN_MSG_TYPE_VOICE) {  
280 - CNVoiceMessage o1 = (CNVoiceMessage) o;  
281 - VoiceMessage voiceMessage = new VoiceMessage(MessageType.voiceMessage, o1, o1.isSelf());  
282 - chatListAdapterOne.addItem(voiceMessage);  
283 - } else if (o.getMsgType() == IChat.CN_MSG_TYPE_VIDEO) {  
284 - CNVideoMessage o1 = (CNVideoMessage) o;  
285 - VideoMessage videoMessage = new VideoMessage(MessageType.videoMessage, o1, o1.isSelf());  
286 - chatListAdapterOne.addItem(videoMessage);  
287 - } else if (o.getMsgType() == IChat.CN_MSG_TYPE_UGC) {  
288 - CNUgcMessage o1 = (CNUgcMessage) o;  
289 - UgcVideoMessage ugcVideoMessage = new UgcVideoMessage(MessageType.ugcVideoMessage, o1, o1.isSelf());  
290 - chatListAdapterOne.addItem(ugcVideoMessage);  
291 - }  
292 - if (chatListAdapterOne.getItemCount() > 0)  
293 - listViewOne.smoothScrollToPosition(chatListAdapterOne.getItemCount() - 1);  
294 - }  
295 -  
296 -  
297 //聊天室操作 255 //聊天室操作
298 private IChat.OnChatRoomActionListener chatRoomActionListener = new IChat.OnChatRoomActionListener() { 256 private IChat.OnChatRoomActionListener chatRoomActionListener = new IChat.OnChatRoomActionListener() {
299 @Override 257 @Override
@@ -398,30 +356,9 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL @@ -398,30 +356,9 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
398 isVoiceBtnVisible = false; 356 isVoiceBtnVisible = false;
399 } 357 }
400 break; 358 break;
401 - case R.id.videoBtn:  
402 - openSystemRecorder();  
403 - break;  
404 - case R.id.ugcVideoBtn:  
405 - openSystemRecorder();  
406 - break;  
407 } 359 }
408 } 360 }
409 361
410 - private void openSystemRecorder() {  
411 - // 创建拍照Intent并将控制权返回给调用的程序  
412 - Intent intent = new Intent();  
413 - intent.setAction(MediaStore.ACTION_VIDEO_CAPTURE);  
414 - intent.addCategory(Intent.CATEGORY_DEFAULT);  
415 -  
416 - // 保存录像到指定的路径  
417 - File file = new File("/sdcard/video.mp4");  
418 - Uri uri = Uri.fromFile(file);  
419 - intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);  
420 -  
421 - // 启动图像捕获Intent  
422 - startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);  
423 - }  
424 -  
425 362
426 public void fromPicture() { 363 public void fromPicture() {
427 if (Build.VERSION.SDK_INT >= 23) { 364 if (Build.VERSION.SDK_INT >= 23) {
@@ -457,12 +394,7 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL @@ -457,12 +394,7 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
457 Uri uri = Uri.parse("file://" + picturePath); 394 Uri uri = Uri.parse("file://" + picturePath);
458 ChatUtil.sendImaMessage(IChat.CHATROOM, roomIdOne, uri, uri, sendMessageListener); 395 ChatUtil.sendImaMessage(IChat.CHATROOM, roomIdOne, uri, uri, sendMessageListener);
459 break; 396 break;
460 - case CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE:  
461 - String videoPath = data.getData().getPath();  
462 - Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, MediaStore.Images.Thumbnails.FULL_SCREEN_KIND);  
463 - String imgPath = FileUtil.createFile(bitmap, System.currentTimeMillis() + "");  
464 - ChatUtil.sendVideoMessage(IChat.CHATROOM, roomIdOne, videoPath, imgPath, MediaUtil.getInstance().getDuration(videoPath), sendMessageListener);  
465 - break; 397 +
466 default: 398 default:
467 break; 399 break;
468 } 400 }
@@ -493,195 +425,25 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL @@ -493,195 +425,25 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
493 } 425 }
494 }; 426 };
495 427
496 - /**  
497 - * 播放语音文件  
498 - *  
499 - * @param voicePath  
500 - * @param animationDrawable  
501 - */  
502 - private void startVoice(String voicePath, final AnimationDrawable animationDrawable) {  
503 - try {  
504 - FileInputStream fis = new FileInputStream(voicePath);  
505 - MediaUtil.getInstance().play(fis);  
506 - animationDrawable.start();  
507 - MediaUtil.getInstance().setEventListener(new MediaUtil.EventListener() {  
508 - @Override  
509 - public void onStop() {  
510 - animationDrawable.stop();  
511 - animationDrawable.selectDrawable(0);  
512 - }  
513 - });  
514 - } catch (Exception e) {  
515 - }  
516 - }  
517 428
518 @Override 429 @Override
519 - public void onItemClick(final VoiceMessage voiceMessage, final AnimationDrawable animationDrawable) {  
520 - if (voiceMessage == null) {  
521 - Toast.makeText(this, "数据异常", Toast.LENGTH_LONG).show();  
522 - return;  
523 - }  
524 - if (voiceMessage.getCnVoiceMessage().isSelf()) {  
525 - startVoice(voiceMessage.getCnVoiceMessage().getVoiceUri().getPath(), animationDrawable); 430 + public void onItemClick(VoiceMessage voiceMessage) {
  431 + if (voiceMessage.isUserSend()) {
  432 + playerUtil.start(voiceMessage.getCnVoiceMessage().getVoiceUri());
526 } else { 433 } else {
527 - //设置本地已读  
528 - voiceMessage.getCnVoiceMessage().setRead(true);  
529 - String voiceUuid = voiceMessage.getCnVoiceMessage().getUuid();  
530 - final String voicePath = FileUtil.getCacheFilePath(voiceUuid);  
531 - if (FileUtil.isCacheFileExist(voiceUuid)) {  
532 - File file = new File(voicePath);  
533 - if (file.length() < voiceMessage.getCnVoiceMessage().getDataSize()) {  
534 - Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();  
535 - return;  
536 - }  
537 - startVoice(voicePath, animationDrawable);  
538 - } else {  
539 - if (voiceMessage.isDownloading()) {  
540 - Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();  
541 - } else {  
542 - voiceMessage.setDownloading(true);  
543 - voiceMessage.getCnVoiceMessage().getVoice(voicePath, new IChat.OnDownloadListener() {  
544 - @Override  
545 - public void onSuccess() {  
546 - voiceMessage.setDownloading(false);  
547 - startVoice(voicePath, animationDrawable);  
548 - }  
549 -  
550 - @Override  
551 - public void onError(int i, String s) {  
552 - voiceMessage.setDownloading(false);  
553 - }  
554 - });  
555 - }  
556 - }  
557 - }  
558 - }  
559 -  
560 - @Override  
561 - public void onVideoClick(final VideoMessage videoMessage) {  
562 - if (videoMessage.getCnVideoMessage().isSelf()) {  
563 - Intent intent = new Intent(ChatRoomActivity.this, VideoActivity.class);  
564 - intent.putExtra("videoPath", videoMessage.getCnVideoMessage().getVideoPath());  
565 - startActivity(intent);  
566 - } else {  
567 - String videoUuid = videoMessage.getCnVideoMessage().getVideo().getUuid();  
568 - final String videoPath = FileUtil.getCacheFilePath(videoUuid);  
569 - if (FileUtil.isCacheFileExist(videoUuid)) {  
570 - File file = new File(videoPath);  
571 - if (file.length() < videoMessage.getCnVideoMessage().getVideo().getSize()) {  
572 - Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();  
573 - return;  
574 - }  
575 - Intent intent = new Intent(ChatRoomActivity.this, VideoActivity.class);  
576 - intent.putExtra("videoPath", videoPath);  
577 - startActivity(intent);  
578 - } else {  
579 - if (videoMessage.isDownloading()) {  
580 - Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();  
581 - } else {  
582 - videoMessage.setDownloading(true);  
583 - videoMessage.getCnVideoMessage().getVideo().getVideo(videoPath, new IChat.OnDownloadListener() {  
584 - @Override  
585 - public void onSuccess() {  
586 - videoMessage.setDownloading(false);  
587 - Intent intent = new Intent(ChatRoomActivity.this, VideoActivity.class);  
588 - intent.putExtra("videoPath", videoPath);  
589 - startActivity(intent);  
590 - }  
591 -  
592 - @Override  
593 - public void onError(int i, String s) {  
594 - videoMessage.setDownloading(false);  
595 - }  
596 - }); 434 + final String voicePath = FileUtil.getTempFile(FileUtil.FileType.AUDIO).getAbsolutePath();
  435 + voiceMessage.getCnVoiceMessage().getVoice(voicePath, new IChat.OnDownloadListener() {
  436 + @Override
  437 + public void onSuccess() {
  438 + if (playerUtil != null)
  439 + playerUtil.start(Uri.parse(voicePath));
597 } 440 }
598 - }  
599 - }  
600 -  
601 - }  
602 441
603 - @Override  
604 - public void onUgcVideoClick(final UgcVideoMessage ugcVideoMessage) {  
605 - if (ugcVideoMessage.getCnUgcMessage().isSelf()) {  
606 - Intent intent = new Intent(ChatRoomActivity.this, VideoActivity.class);  
607 - intent.putExtra("videoPath", ugcVideoMessage.getCnUgcMessage().getVideoPath());  
608 - startActivity(intent);  
609 - } else {  
610 - String videoUuid = ugcVideoMessage.getCnUgcMessage().getFileId() + "_video";  
611 - final String videoPath = FileUtil.getCacheFilePath(videoUuid);  
612 - if (FileUtil.isCacheFileExist(videoUuid)) {  
613 - File file = new File(videoPath);  
614 - if (file.length() < ugcVideoMessage.getCnUgcMessage().getVideo().getSize()) {  
615 - Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();  
616 - return;  
617 - }  
618 - Intent intent = new Intent(ChatRoomActivity.this, VideoActivity.class);  
619 - intent.putExtra("videoPath", videoPath);  
620 - startActivity(intent);  
621 - } else {  
622 - if (ugcVideoMessage.isDownloading()) {  
623 - Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();  
624 - } else {  
625 - ugcVideoMessage.setDownloading(true);  
626 - ugcVideoMessage.getCnUgcMessage().getVideo().getVideo(videoPath, new IChat.OnDownloadListener() {  
627 - @Override  
628 - public void onSuccess() {  
629 - ugcVideoMessage.setDownloading(false);  
630 - Intent intent = new Intent(ChatRoomActivity.this, VideoActivity.class);  
631 - intent.putExtra("videoPath", videoPath);  
632 - startActivity(intent);  
633 - }  
634 -  
635 - @Override  
636 - public void onError(int i, String s) {  
637 - ugcVideoMessage.setDownloading(false);  
638 - }  
639 - });  
640 - }  
641 - }  
642 - }  
643 - } 442 + @Override
  443 + public void onError(int i, String s) {
644 444
645 - @Override  
646 - public void onImageClick(final ImagMessage imagMessage) {  
647 - if (imagMessage.getCnImgMessage().isSelf()) {  
648 - Intent intent = new Intent(ChatRoomActivity.this, ImageViewActivity.class);  
649 - intent.putExtra("path", imagMessage.getCnImgMessage().getImagePath());  
650 - startActivity(intent);  
651 - } else {  
652 - String remoteUuid = imagMessage.getCnImgMessage().getRemoteUuid();  
653 - final String imagePath = FileUtil.getCacheFilePath(remoteUuid);  
654 - if (FileUtil.isCacheFileExist(remoteUuid)) {  
655 - File file = new File(imagePath);  
656 - if (file.length() < imagMessage.getCnImgMessage().getRemoteSize()) {  
657 - Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();  
658 - return;  
659 } 445 }
660 - Intent intent = new Intent(ChatRoomActivity.this, ImageViewActivity.class);  
661 - intent.putExtra("path", imagePath);  
662 - startActivity(intent);  
663 - } else {  
664 - if (imagMessage.isDownloading()) {  
665 - Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();  
666 - } else {  
667 - imagMessage.setDownloading(true);  
668 - imagMessage.getCnImgMessage().getRemoteImage(imagePath, new IChat.OnDownloadListener() {  
669 - @Override  
670 - public void onSuccess() {  
671 - imagMessage.setDownloading(false);  
672 - Intent intent = new Intent(ChatRoomActivity.this, ImageViewActivity.class);  
673 - intent.putExtra("path", imagePath);  
674 - startActivity(intent);  
675 - }  
676 -  
677 - @Override  
678 - public void onError(int i, String s) {  
679 - imagMessage.setDownloading(false);  
680 - Toast.makeText(context, "下载失败", Toast.LENGTH_SHORT).show();  
681 - }  
682 - });  
683 - }  
684 - } 446 + });
685 } 447 }
686 } 448 }
687 449
@@ -697,15 +459,14 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL @@ -697,15 +459,14 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
697 Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) && 459 Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) &&
698 (ContextCompat.checkSelfPermission(context, 460 (ContextCompat.checkSelfPermission(context,
699 Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED) 461 Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED)
700 - && (ContextCompat.checkSelfPermission(context,  
701 - Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)) { 462 + ) {
702 StartListener(); 463 StartListener();
703 464
704 //判断是否开启语音权限 465 //判断是否开启语音权限
705 } else { 466 } else {
706 //请求获取摄像头权限 467 //请求获取摄像头权限
707 ActivityCompat.requestPermissions((Activity) context, 468 ActivityCompat.requestPermissions((Activity) context,
708 - new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA}, VOICE_REQUEST_CODE); 469 + new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO}, VOICE_REQUEST_CODE);
709 } 470 }
710 } 471 }
711 472
@@ -846,19 +607,4 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL @@ -846,19 +607,4 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
846 } 607 }
847 }; 608 };
848 609
849 - @Override  
850 - protected void onPause() {  
851 - super.onPause();  
852 - MediaUtil.getInstance().stop();  
853 - }  
854 -  
855 - @Override  
856 - protected void onDestroy() {  
857 - super.onDestroy();  
858 - ChatUtil.removeUploadProgressListener(uploadProgressListener);  
859 - ChatUtil.removeReceiveMessageListener(receiveMessageListener);  
860 - ChatUtil.removeConnectListener(connectListener);  
861 - ChatUtil.removeConnectStatusListener(connectStatusListener);  
862 - }  
863 -  
864 } 610 }
app/src/main/java/com/cnlive/im/ui/Private3Activity.java 0 → 100644
  1 +package com.cnlive.im.ui;
  2 +
  3 +import android.Manifest;
  4 +import android.app.Activity;
  5 +import android.content.Context;
  6 +import android.content.Intent;
  7 +import android.content.pm.PackageManager;
  8 +import android.database.Cursor;
  9 +import android.graphics.Bitmap;
  10 +import android.graphics.drawable.AnimationDrawable;
  11 +import android.media.ThumbnailUtils;
  12 +import android.net.Uri;
  13 +import android.os.Build;
  14 +import android.os.Bundle;
  15 +import android.os.Handler;
  16 +import android.provider.MediaStore;
  17 +import android.support.annotation.NonNull;
  18 +import android.support.v4.app.ActivityCompat;
  19 +import android.support.v4.content.ContextCompat;
  20 +import android.support.v7.app.AppCompatActivity;
  21 +import android.support.v7.widget.LinearLayoutManager;
  22 +import android.support.v7.widget.RecyclerView;
  23 +import android.text.Editable;
  24 +import android.text.TextUtils;
  25 +import android.text.TextWatcher;
  26 +import android.util.Log;
  27 +import android.view.Gravity;
  28 +import android.view.MotionEvent;
  29 +import android.view.View;
  30 +import android.view.inputmethod.InputMethodManager;
  31 +import android.widget.Button;
  32 +import android.widget.EditText;
  33 +import android.widget.ImageView;
  34 +import android.widget.LinearLayout;
  35 +import android.widget.RelativeLayout;
  36 +import android.widget.TextView;
  37 +import android.widget.Toast;
  38 +
  39 +import com.cnlive.im.R;
  40 +import com.cnlive.im.adapter.ChatRoom3Adpter;
  41 +import com.cnlive.im.adapter.ChatRoomAdpter;
  42 +import com.cnlive.im.adapter.base.ItemClickListener;
  43 +import com.cnlive.im.mode.CNMessageModole;
  44 +import com.cnlive.im.mode.ImagMessage;
  45 +import com.cnlive.im.mode.StringMessage;
  46 +import com.cnlive.im.mode.UgcVideoMessage;
  47 +import com.cnlive.im.mode.VideoMessage;
  48 +import com.cnlive.im.mode.VoiceMessage;
  49 +import com.cnlive.im.util.AudioRecordUtil;
  50 +import com.cnlive.im.util.FileUtil;
  51 +import com.cnlive.im.util.MediaUtil;
  52 +import com.cnlive.im.util.MessageType;
  53 +import com.cnlive.im.util.TimeUtils;
  54 +import com.cnlive.im.util.ToStringUtils;
  55 +import com.cnlive.libs.util.chat.ChatUtil;
  56 +import com.cnlive.libs.util.chat.base.IChat;
  57 +import com.cnlive.libs.util.chat.model.CNBaseMessage;
  58 +import com.cnlive.libs.util.chat.model.CNImgMessage;
  59 +import com.cnlive.libs.util.chat.model.CNMessage;
  60 +import com.cnlive.libs.util.chat.model.CNUgcMessage;
  61 +import com.cnlive.libs.util.chat.model.CNUserInfo;
  62 +import com.cnlive.libs.util.chat.model.CNVideoMessage;
  63 +import com.cnlive.libs.util.chat.model.CNVoiceMessage;
  64 +
  65 +import org.json.JSONException;
  66 +import org.json.JSONObject;
  67 +
  68 +import java.io.File;
  69 +import java.io.FileInputStream;
  70 +import java.util.ArrayList;
  71 +
  72 +import static com.cnlive.im.R.id.opposite_id_edit;
  73 +
  74 +/**
  75 + * Created by gujun on 2017/12/19.
  76 + */
  77 +
  78 +public class Private3Activity extends AppCompatActivity implements View.OnClickListener, ItemClickListener {
  79 +
  80 + private static final String TAG = "PrivateChatActivity";
  81 +
  82 + private final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 0x2001;
  83 +
  84 + // 所需的全部权限
  85 + static final String[] PERMISSIONS = new String[]{
  86 + Manifest.permission.RECORD_AUDIO,
  87 + Manifest.permission.WRITE_EXTERNAL_STORAGE
  88 + };
  89 +
  90 + public static final int IMG_FROM_PICTURE = 0x1001;
  91 + public static final int VOICE = 0x1002;
  92 + public static final int PICTURE_REQUEST_CODE = 0x1003;
  93 + public static final int VOICE_REQUEST_CODE = 0x1004;
  94 +
  95 + public static final String IMAGE_MESSAGE = "image_message";
  96 + public static final String STRING_MESSAGE = "string_message";
  97 + public static final String VOICE_MESSAGE = "voice_message";
  98 +
  99 + private RecyclerView chatListView;
  100 + private EditText messageContent;
  101 + private Button sendMessageBtn;
  102 + private ChatRoom3Adpter chatListAdapter;
  103 + private ArrayList<String> msgIds = new ArrayList<>();
  104 + // private CNMessage cnMessage;
  105 + private LinearLayout linearLayoutUseID;
  106 + private EditText user_id_edit;
  107 + private Button userId_btn_top;
  108 + private String oppositeID;
  109 + private LinearLayout edit_layout;
  110 + private Handler mHandler;
  111 + private EditText opposite_name_edit;
  112 + private String user_name_string;
  113 + private String user_id_string;
  114 + private int sendInfo = 3;
  115 + private Button agree_btn;
  116 + private Button reject_btn;
  117 + private LinearLayout agree_reject_lin;
  118 + boolean isCanSendMeaasge = false;
  119 + private Button vistor_btn;
  120 +
  121 + private ImageView keybordBtn;
  122 + private Button sendVoice;
  123 + private AudioRecordUtil mAudioRecordUtil;
  124 + private String mFilePath;
  125 + private PopupWindowFactory mPop;
  126 + private RelativeLayout privateLayout;
  127 + private LinearLayout sendMsgLayout;
  128 + private LinearLayout cancelSendLayout;
  129 + private TextView mTextView;
  130 + private ImageView mImageView;
  131 + private ImageView ima_send;
  132 +
  133 + private boolean isVoiceBtnVisible = false;
  134 + private int duration;
  135 + private Context context;
  136 +
  137 + @Override
  138 + protected void onCreate(Bundle savedInstanceState) {
  139 + super.onCreate(savedInstanceState);
  140 + setContentView(R.layout.activity_private_chat_3);
  141 + context = this;
  142 + initView();
  143 + mAudioRecordUtil = new AudioRecordUtil();
  144 + mAudioRecordUtil.setOnAudioStatusUpdateListener(onAudioStatusUpdateListener);
  145 + mHandler = new Handler();
  146 + ChatUtil.setOnConnectStatusListener(connectStatusListener);
  147 + ChatUtil.setOnReceivePrivateMessageListener(receiveMessageListener);
  148 + ChatUtil.setUploadProgressListener(uploadProgressListener);
  149 + requestPermission();
  150 + }
  151 +
  152 + private void initView() {
  153 +
  154 + final View view = View.inflate(this, R.layout.layout_microphone, null);
  155 + mPop = new PopupWindowFactory(this, view);
  156 + sendMsgLayout = (LinearLayout) view.findViewById(R.id.send_layout);
  157 + cancelSendLayout = (LinearLayout) view.findViewById(R.id.cancel_send_layout);
  158 + mTextView = (TextView) view.findViewById(R.id.tv_recording_time);
  159 + mImageView = (ImageView) view.findViewById(R.id.iv_recording_icon);
  160 +
  161 + opposite_name_edit = (EditText) findViewById(R.id.opposite_name_edit);
  162 + chatListView = (RecyclerView) findViewById(R.id.private_list_view);
  163 +
  164 + privateLayout = (RelativeLayout) findViewById(R.id.activity_private_chat);
  165 +
  166 + messageContent = (EditText) findViewById(R.id.message_content);
  167 + messageContent.addTextChangedListener(textWatcher);
  168 + sendMessageBtn = (Button) findViewById(R.id.send_message);
  169 + sendMessageBtn.setOnClickListener(this);
  170 + linearLayoutUseID = (LinearLayout) findViewById(R.id.opposite_liner_useID);
  171 + userId_btn_top = (Button) findViewById(R.id.userId_btn_top);
  172 + userId_btn_top.setOnClickListener(this);
  173 + edit_layout = (LinearLayout) findViewById(R.id.edit_layout);
  174 + user_id_edit = (EditText) findViewById(opposite_id_edit);
  175 + Intent intent = getIntent();
  176 + user_name_string = intent.getStringExtra("user_name_string");
  177 + user_id_string = intent.getStringExtra("user_id_string");
  178 + agree_btn = (Button) findViewById(R.id.agree_btn);
  179 + reject_btn = (Button) findViewById(R.id.reject_btn);
  180 + agree_btn.setOnClickListener(this);
  181 + reject_btn.setOnClickListener(this);
  182 + agree_reject_lin = (LinearLayout) findViewById(R.id.agree_reject_lin);
  183 + vistor_btn = (Button) findViewById(R.id.vistor_Btn);
  184 + vistor_btn.setOnClickListener(this);
  185 +
  186 + keybordBtn = (ImageView) findViewById(R.id.keybord);
  187 + keybordBtn.setOnClickListener(this);
  188 +
  189 + ima_send = (ImageView) findViewById(R.id.ima_send);
  190 + ima_send.setOnClickListener(this);
  191 +
  192 + sendVoice = (Button) findViewById(R.id.send_voice);
  193 +
  194 + chatListAdapter = new ChatRoom3Adpter(this, true);
  195 + chatListAdapter.setonVoiceItemClickListener(this);
  196 + chatListView.setLayoutManager(new LinearLayoutManager(Private3Activity.this));
  197 + chatListView.setAdapter(chatListAdapter);
  198 +
  199 + findViewById(R.id.videoBtn).setOnClickListener(this);
  200 + findViewById(R.id.ugcVideoBtn).setOnClickListener(this);
  201 +
  202 + StartListener();
  203 + }
  204 +
  205 + @Override
  206 + public void onClick(View v) {
  207 +
  208 + switch (v.getId()) {
  209 + case R.id.send_message:
  210 + sendMessage(STRING_MESSAGE);
  211 +
  212 + break;
  213 +
  214 + case R.id.userId_btn_top:
  215 + //发送私聊消息,需要传入目标ID与发送的消息内容
  216 + if (user_id_edit.getText().toString().isEmpty()) {
  217 + Toast.makeText(this, "对方ID不能为空", Toast.LENGTH_SHORT).show();
  218 + } else if (opposite_name_edit.getText().toString().isEmpty()) {
  219 + Toast.makeText(this, "对方name不能为空", Toast.LENGTH_SHORT).show();
  220 + } else {
  221 +
  222 + oppositeID = user_id_edit.getText().toString();
  223 + linearLayoutUseID.setVisibility(View.INVISIBLE);
  224 + edit_layout.setVisibility(View.VISIBLE);
  225 + chatListView.setVisibility(View.VISIBLE);
  226 + CNUserInfo cnName = new CNUserInfo(user_id_string, user_name_string, "");
  227 + ChatUtil.login(cnName, connectListener);
  228 +
  229 + }
  230 +
  231 + break;
  232 +
  233 + case R.id.agree_btn:
  234 + String objToJsonString = ToStringUtils.objToJsonString(new CNMessageModole("同意", "3"));
  235 + ChatUtil.sendMessage(IChat.PRIVATE, oppositeID, objToJsonString, sendMessageListener);
  236 + agree_reject_lin.setVisibility(View.GONE);
  237 + break;
  238 +
  239 + case R.id.reject_btn:
  240 + String objToJsonStringDis = ToStringUtils.objToJsonString(new CNMessageModole("拒绝", "3"));//私聊
  241 +
  242 + ChatUtil.sendMessage(IChat.PRIVATE, oppositeID, objToJsonStringDis, sendMessageListener);
  243 + agree_reject_lin.setVisibility(View.GONE);
  244 + break;
  245 + case R.id.vistor_Btn:
  246 + String objToJsonStringInvt = ToStringUtils.objToJsonString(new CNMessageModole("你好我们可以进行私聊吗", "3"));//私聊
  247 + ChatUtil.sendMessage(IChat.PRIVATE, oppositeID, objToJsonStringInvt, sendMessageListener);
  248 +
  249 + break;
  250 +
  251 + case R.id.keybord:
  252 + if (!isVoiceBtnVisible) {
  253 + messageContent.setVisibility(View.GONE);
  254 + sendVoice.setVisibility(View.VISIBLE);
  255 + keybordBtn.setImageResource(R.drawable.keybord);
  256 + InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
  257 + imm.hideSoftInputFromWindow(messageContent.getWindowToken(), 0);
  258 + isVoiceBtnVisible = true;
  259 + } else {
  260 + sendVoice.setVisibility(View.GONE);
  261 + messageContent.setVisibility(View.VISIBLE);
  262 + keybordBtn.setImageResource(R.drawable.voice);
  263 + isVoiceBtnVisible = false;
  264 + }
  265 + break;
  266 + case R.id.ima_send:
  267 + sendMessage(IMAGE_MESSAGE);
  268 + break;
  269 + case R.id.videoBtn:
  270 + openSystemRecorder();
  271 + break;
  272 + case R.id.ugcVideoBtn:
  273 + openSystemRecorder();
  274 + break;
  275 + }
  276 + }
  277 +
  278 + private void openSystemRecorder() {
  279 + // 创建拍照Intent并将控制权返回给调用的程序
  280 + Intent intent = new Intent();
  281 + intent.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
  282 + intent.addCategory(Intent.CATEGORY_DEFAULT);
  283 +
  284 + // 保存录像到指定的路径
  285 + File file = new File("/sdcard/video.mp4");
  286 + Uri uri = Uri.fromFile(file);
  287 + intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
  288 +
  289 + // 启动图像捕获Intent
  290 + startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);
  291 + }
  292 +
  293 +
  294 + private void sendMessage(String type) {
  295 + if (sendInfo == 2) {
  296 + Toast.makeText(this, "等待对方接受邀请", Toast.LENGTH_SHORT).show();
  297 +
  298 + } else if (sendInfo == 4) {
  299 + Toast.makeText(this, "对方已经拒绝不能发送", Toast.LENGTH_SHORT).show();
  300 +
  301 + } else if (sendInfo == 3) {
  302 + Toast.makeText(this, "还没有邀请对方呦~", Toast.LENGTH_SHORT).show();
  303 + } else if (sendInfo == 5) {
  304 + Toast.makeText(this, "你已经拒绝对方不能发送消息", Toast.LENGTH_SHORT).show();
  305 + } else {
  306 + if (type.equals(STRING_MESSAGE)) {
  307 + if (messageContent.getText().toString().isEmpty()) {
  308 + Toast.makeText(this, "不能发送空消息呦~", Toast.LENGTH_SHORT).show();
  309 + } else {
  310 + String objToJsonString = ToStringUtils.objToJsonString(new CNMessageModole(messageContent.getText().toString(), "4"));//私聊消息
  311 + ChatUtil.sendMessage(IChat.PRIVATE, oppositeID, objToJsonString, sendMessageListener);
  312 + messageContent.setText("");
  313 + }
  314 + } else if (type.equals(IMAGE_MESSAGE)) {
  315 + fromPicture();
  316 + } else if (type.equals(VOICE_MESSAGE)) {
  317 + mPop.showAtLocation(privateLayout, Gravity.CENTER, 0, 0);
  318 +
  319 + sendVoice.setText("松开发送");
  320 + mAudioRecordUtil.startRecord();
  321 + }
  322 + }
  323 + }
  324 +
  325 + public void fromPicture() {
  326 + if (Build.VERSION.SDK_INT >= 23) {
  327 + int checkCallPhonePermission = ContextCompat.checkSelfPermission(Private3Activity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
  328 + if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {
  329 + requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PICTURE_REQUEST_CODE);
  330 + return;
  331 + } else {
  332 + toIntentPicture();
  333 + }
  334 + } else {
  335 + toIntentPicture();
  336 + }
  337 + }
  338 +
  339 +
  340 + private void toIntentPicture() {
  341 + Intent intent = new Intent();
  342 + intent.setType("image/*");
  343 + intent.setAction(Intent.ACTION_PICK);
  344 + startActivityForResult(Intent.createChooser(intent, "Complete action using"), IMG_FROM_PICTURE);
  345 + }
  346 +
  347 +
  348 + @Override
  349 + public void onActivityResult(int requestCode, int resultCode, Intent data) {
  350 + super.onActivityResult(requestCode, resultCode, data);
  351 + if (resultCode == Activity.RESULT_OK) {
  352 + switch (requestCode) {
  353 + case IMG_FROM_PICTURE:
  354 +// String localImagePath = "/sdcard/test.png";
  355 +// Uri selectedImage = Uri.parse("file://" + localImagePath);
  356 + Uri selectedImage = data.getData();
  357 + String[] filePathColumn = {MediaStore.Images.Media.DATA};
  358 +
  359 + Cursor cursor = getContentResolver().query(selectedImage,
  360 + filePathColumn, null, null, null);
  361 + cursor.moveToFirst();
  362 +
  363 + int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
  364 + final String picturePath = cursor.getString(columnIndex);
  365 +
  366 + Uri uri = Uri.parse("file://" + picturePath);
  367 + ChatUtil.sendImaMessage(IChat.PRIVATE, oppositeID, uri, uri, sendMessageListener);
  368 + break;
  369 + case CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE:
  370 + String videoPath = data.getData().getPath();
  371 + Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, MediaStore.Images.Thumbnails.FULL_SCREEN_KIND);
  372 + String imgPath = FileUtil.createFile(bitmap, System.currentTimeMillis() + "");
  373 + ChatUtil.sendVideoMessage(IChat.PRIVATE, oppositeID, videoPath, imgPath, MediaUtil.getInstance().getDuration(videoPath), sendMessageListener);
  374 + break;
  375 +
  376 + default:
  377 + break;
  378 + }
  379 + }
  380 + }
  381 +
  382 + //连接监听
  383 + private IChat.OnConnectListener connectListener = new IChat.OnConnectListener() {
  384 +
  385 +
  386 + @Override
  387 + public void onTokenIncorrect(String s) {
  388 + Log.e(TAG, "ConnectListener TokenIncorrect");
  389 + Toast.makeText(Private3Activity.this, "TokenIncorrect", Toast.LENGTH_SHORT).show();
  390 + }
  391 +
  392 + @Override
  393 + public void onSuccess(String s) {
  394 + Log.e(TAG, "ConnectListener 连接监听" + s);
  395 + Toast.makeText(Private3Activity.this, s, Toast.LENGTH_SHORT).show();
  396 +
  397 + }
  398 +
  399 + @Override
  400 + public void onError(int i, String s) {
  401 + Log.e(TAG, "ConnectListener errorCode : " + i + " : errorMessage : " + s);
  402 + Toast.makeText(Private3Activity.this, s, Toast.LENGTH_SHORT).show();
  403 +
  404 + }
  405 + };
  406 +
  407 +
  408 + /**
  409 + * 发送消息监听
  410 + */
  411 + private IChat.OnSendMessageListener sendMessageListener = new IChat.OnSendMessageListener() {
  412 + @Override
  413 + public void onAttached(CNBaseMessage o) {
  414 + showMessage(o, false);
  415 + }
  416 +
  417 + @Override
  418 + public void onSuccess(final CNBaseMessage o) {
  419 + updateItem(o, -1);
  420 + }
  421 +
  422 + @Override
  423 + public void onError(CNBaseMessage o, int i, String s) {
  424 + updateItem(o, -1);
  425 + }
  426 + };
  427 +
  428 + //连接状态监听
  429 + private IChat.OnConnectStatusListener connectStatusListener = new IChat.OnConnectStatusListener() {
  430 + @Override
  431 + public void onConnectStatus(int i, String s) {
  432 + Log.i(TAG, "连接状态监听" + i + " " + s);
  433 + }
  434 + };
  435 +
  436 +
  437 + //接收消息监听
  438 + private IChat.OnReceiveMessageListener receiveMessageListener = new IChat.OnReceiveMessageListener() {
  439 + @Override
  440 + public void processMessage(final CNBaseMessage o) {
  441 + if (!o.getTargetId().equals(oppositeID)) return;
  442 + showMessage(o, true);
  443 + }
  444 + };
  445 +
  446 + private IChat.OnUploadProgressListener uploadProgressListener = new IChat.OnUploadProgressListener() {
  447 + @Override
  448 + public void onMessagesUpdate(CNBaseMessage message, int elemIdx, int taskId, int progress) {
  449 + if (!message.getTargetId().equals(oppositeID)) return;
  450 + updateItem(message, progress);
  451 + }
  452 + };
  453 +
  454 + /**
  455 + * 更新消息
  456 + */
  457 + public void updateItem(CNBaseMessage o, int progress) {
  458 + int updateIndex = msgIds.indexOf(o.getMessageId());
  459 + if (updateIndex < 0 || chatListAdapter.getItemCount() <= 0 || updateIndex > chatListAdapter.getItemCount() - 1)
  460 + return;
  461 + if (o.getMsgType() == IChat.CN_MSG_TYPE_TEXT) {
  462 + CNMessage o1 = (CNMessage) o;
  463 + chatListAdapter.updateItem(new StringMessage(MessageType.stringMessage, o1, o1.isSelf()), updateIndex);
  464 + } else if (o.getMsgType() == IChat.CN_MSG_TYPE_IMAGE) {
  465 + CNImgMessage o1 = (CNImgMessage) o;
  466 + chatListAdapter.updateItem(new ImagMessage(MessageType.imaMessage, o1, o1.isSelf(), progress < 0 ? 0 : progress), updateIndex);
  467 + } else if (o.getMsgType() == IChat.CN_MSG_TYPE_VOICE) {
  468 + CNVoiceMessage o1 = (CNVoiceMessage) o;
  469 + VoiceMessage voiceMessage = new VoiceMessage(MessageType.voiceMessage, o1, o1.isSelf(), progress < 0 ? 0 : progress);
  470 + chatListAdapter.updateItem(voiceMessage, updateIndex);
  471 + } else if (o.getMsgType() == IChat.CN_MSG_TYPE_VIDEO) {
  472 + CNVideoMessage ol = (CNVideoMessage) o;
  473 + VideoMessage videoMessage = new VideoMessage(MessageType.videoMessage, ol, ol.isSelf(), progress < 0 ? 0 : progress);
  474 + chatListAdapter.updateItem(videoMessage, updateIndex);
  475 + } else if (o.getMsgType() == IChat.CN_MSG_TYPE_UGC) {
  476 + CNUgcMessage ol = (CNUgcMessage) o;
  477 + UgcVideoMessage ugcVideoMessage = new UgcVideoMessage(MessageType.ugcVideoMessage, ol, ol.isSelf(), progress < 0 ? 0 : progress);
  478 + chatListAdapter.updateItem(ugcVideoMessage, updateIndex);
  479 + }
  480 + }
  481 +
  482 + /**
  483 + * 展示消息
  484 + *
  485 + * @param o
  486 + */
  487 + private void showMessage(CNBaseMessage o, boolean isReceivedMsg) {
  488 + if (o.getMsgType() == IChat.CN_MSG_TYPE_TEXT) {
  489 + if (isReceivedMsg) {
  490 + CNMessage o1 = (CNMessage) o;
  491 + JSONObject jsonObject = null;
  492 + try {
  493 + jsonObject = new JSONObject(o1.getContent().toString());
  494 + String type = (String) jsonObject.get("type");
  495 + if ("3".equals(type) && "你好我们可以进行私聊吗".equals(jsonObject.get("message"))) {
  496 + vistor_btn.setVisibility(View.GONE);
  497 + agree_reject_lin.setVisibility(View.VISIBLE);
  498 + } else if ("3".equals(type) && "同意".equals(jsonObject.get("message"))) {
  499 + sendInfo = 1;
  500 + } else if ("3".equals(type) && "拒绝".equals(jsonObject.get("message"))) {
  501 + sendInfo = 4;
  502 + } else if ("4".equals(type)) {
  503 + Log.v(TAG, o1.getContent().toString());
  504 + msgIds.add(o.getMessageId());
  505 + chatListAdapter.addItem(new StringMessage(MessageType.stringMessage, o1, false));
  506 + }
  507 + } catch (JSONException e) {
  508 + e.printStackTrace();
  509 + }
  510 + } else {
  511 + CNMessage o1 = (CNMessage) o;
  512 + JSONObject jsonObject = null;
  513 + try {
  514 + jsonObject = new JSONObject(o1.getContent().toString());
  515 + String type = (String) jsonObject.get("type");
  516 + String message = (String) jsonObject.get("message");
  517 + Toast.makeText(Private3Activity.this, "发送消息成功", Toast.LENGTH_SHORT).show();
  518 + if ("3".equals(type) && message.equals("你好我们可以进行私聊吗")) {
  519 + sendInfo = 2;
  520 + Toast.makeText(Private3Activity.this, "申请已发送等待对方验证", Toast.LENGTH_SHORT).show();
  521 + } else if ("3".equals(type) && message.equals("拒绝")) {
  522 + sendInfo = 5;
  523 + } else if ("3".equals(type) && message.equals("同意")) {
  524 + sendInfo = 1;
  525 + } else if ("4".equals(type)) {
  526 + msgIds.add(o.getMessageId());
  527 + chatListAdapter.addItem(new StringMessage(MessageType.stringMessage, o1, true));
  528 + }
  529 + } catch (JSONException e) {
  530 + }
  531 + }
  532 + } else if (o.getMsgType() == IChat.CN_MSG_TYPE_IMAGE) {
  533 + msgIds.add(o.getMessageId());
  534 + CNImgMessage o1 = (CNImgMessage) o;
  535 + chatListAdapter.addItem(new ImagMessage(MessageType.imaMessage, o1, o1.isSelf()));
  536 + } else if (o.getMsgType() == IChat.CN_MSG_TYPE_VOICE) {
  537 + msgIds.add(o.getMessageId());
  538 + CNVoiceMessage o1 = (CNVoiceMessage) o;
  539 + VoiceMessage voiceMessage = new VoiceMessage(MessageType.voiceMessage, o1, o1.isSelf());
  540 + chatListAdapter.addItem(voiceMessage);
  541 + } else if (o.getMsgType() == IChat.CN_MSG_TYPE_VIDEO) {
  542 + msgIds.add(o.getMessageId());
  543 + CNVideoMessage ol = (CNVideoMessage) o;
  544 + VideoMessage videoMessage = new VideoMessage(MessageType.videoMessage, ol, ol.isSelf());
  545 + chatListAdapter.addItem(videoMessage);
  546 + } else if (o.getMsgType() == IChat.CN_MSG_TYPE_UGC) {
  547 + msgIds.add(o.getMessageId());
  548 + CNUgcMessage ol = (CNUgcMessage) o;
  549 + UgcVideoMessage ugcVideoMessage = new UgcVideoMessage(MessageType.ugcVideoMessage, ol, ol.isSelf());
  550 + chatListAdapter.addItem(ugcVideoMessage);
  551 + }
  552 + if (chatListAdapter.getItemCount() > 0)
  553 + chatListView.smoothScrollToPosition(chatListAdapter.getItemCount() - 1);
  554 + }
  555 +
  556 + /**
  557 + * 开启扫描之前判断权限是否打开
  558 + */
  559 + private void requestPermission() {
  560 + //判断是否开启摄像头权限
  561 + if ((ContextCompat.checkSelfPermission(context,
  562 + Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) &&
  563 + (ContextCompat.checkSelfPermission(context,
  564 + Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED)
  565 + && (ContextCompat.checkSelfPermission(context,
  566 + Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)) {
  567 + StartListener();
  568 +
  569 + //判断是否开启语音权限
  570 + } else {
  571 + //请求获取摄像头权限
  572 + ActivityCompat.requestPermissions((Activity) context,
  573 + new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA}, VOICE_REQUEST_CODE);
  574 + }
  575 + }
  576 +
  577 + @Override
  578 + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  579 + super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  580 + if (requestCode == VOICE_REQUEST_CODE) {
  581 + if ((grantResults[0] == PackageManager.PERMISSION_GRANTED) && (grantResults[1] == PackageManager.PERMISSION_GRANTED)) {
  582 + StartListener();
  583 + } else {
  584 + Toast.makeText(context, "已拒绝权限!", Toast.LENGTH_SHORT).show();
  585 + }
  586 + }
  587 + }
  588 +
  589 + float downX;
  590 + float downY;
  591 +
  592 + public void StartListener() {
  593 +
  594 + //Button的touch监听
  595 + sendVoice.setOnTouchListener(new View.OnTouchListener() {
  596 + @Override
  597 + public boolean onTouch(View v, MotionEvent event) {
  598 + if (lacksPermissions(PERMISSIONS)) {
  599 + if (lacksPermission(PERMISSIONS[0])) {
  600 + Toast.makeText(Private3Activity.this, "没有麦克风权限,请前往“设置 - 应用 - 权限”中开启权限", Toast.LENGTH_SHORT).show();
  601 +
  602 + } else if (lacksPermission(PERMISSIONS[1])) {
  603 + Toast.makeText(Private3Activity.this, "没有文件读写权限,请前往“设置 - 应用 - 权限”中开启权限", Toast.LENGTH_SHORT).show();
  604 +
  605 + }
  606 + } else {
  607 +
  608 + switch (event.getAction()) {
  609 +
  610 + case MotionEvent.ACTION_DOWN:
  611 + sendMessage(VOICE_MESSAGE);
  612 +// mPop.showAtLocation(privateLayout, Gravity.CENTER, 0, 0);
  613 +//
  614 +// sendVoice.setText("松开发送");
  615 +// mAudioRecordUtil.startRecord();
  616 + downX = event.getX();
  617 + downY = event.getY();
  618 +
  619 + break;
  620 +
  621 + case MotionEvent.ACTION_UP:
  622 +
  623 + if (cancelSendLayout.getVisibility() == View.VISIBLE) {
  624 + cancelSendLayout.setVisibility(View.GONE);
  625 + sendMsgLayout.setVisibility(View.VISIBLE);
  626 + }
  627 + float moveX = Math.abs(event.getX() - downX);
  628 + float moveY = Math.abs(event.getY() - downY);
  629 + if (moveY > 100) {
  630 +// Toast.makeText(PrivateActivity.this, "cancel", Toast.LENGTH_SHORT).show();
  631 + mAudioRecordUtil.cancelRecord();
  632 + } else {
  633 + mAudioRecordUtil.stopRecord(); //结束录音(保存录音文件)
  634 + }
  635 + mPop.dismiss();
  636 + mTextView.setText("00:00");
  637 + sendVoice.setText("按住说话");
  638 + break;
  639 + case MotionEvent.ACTION_MOVE:
  640 + float moveXm = Math.abs(event.getX() - downX);
  641 + float moveYm = Math.abs(event.getY() - downY);
  642 + if (moveYm > 100) {
  643 +// Toast.makeText(PrivateActivity.this, "cancel", Toast.LENGTH_SHORT).show();
  644 + if (sendMsgLayout.getVisibility() == View.VISIBLE) {
  645 + sendMsgLayout.setVisibility(View.GONE);
  646 + cancelSendLayout.setVisibility(View.VISIBLE);
  647 + }
  648 + } else {
  649 + if (cancelSendLayout.getVisibility() == View.VISIBLE) {
  650 + cancelSendLayout.setVisibility(View.GONE);
  651 + sendMsgLayout.setVisibility(View.VISIBLE);
  652 + }
  653 + }
  654 + break;
  655 + }
  656 + }
  657 + return true;
  658 + }
  659 + });
  660 +
  661 + }
  662 +
  663 + // 判断权限集合
  664 + public boolean lacksPermissions(String... permissions) {
  665 + for (String permission : permissions) {
  666 + if (lacksPermission(permission)) {
  667 + return true;
  668 + }
  669 + }
  670 + return false;
  671 + }
  672 +
  673 + // 判断是否缺少权限
  674 + private boolean lacksPermission(String permission) {
  675 + return ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_DENIED;
  676 + }
  677 +
  678 + private AudioRecordUtil.OnAudioStatusUpdateListener onAudioStatusUpdateListener = new AudioRecordUtil.OnAudioStatusUpdateListener() {
  679 + @Override
  680 + public void onUpdate(double db, long time) {
  681 + mImageView.getDrawable().setLevel((int) (3000 + 6000 * db / 100));
  682 + mTextView.setText(TimeUtils.long2String(time));
  683 + }
  684 +
  685 + @Override
  686 + public void onStop(String filePath) {
  687 + duration = (int) mAudioRecordUtil.getDuration();
  688 + mFilePath = filePath;
  689 + ChatUtil.sendVoiceMessage(IChat.PRIVATE, oppositeID, Uri.parse(mFilePath), duration, sendMessageListener);
  690 + }
  691 + };
  692 +
  693 + private TextWatcher textWatcher = new TextWatcher() {
  694 + @Override
  695 + public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  696 + }
  697 +
  698 + @Override
  699 + public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  700 +
  701 + }
  702 +
  703 + @Override
  704 + public void afterTextChanged(Editable editable) {
  705 + if (!TextUtils.isEmpty(editable)) {
  706 + ima_send.setVisibility(View.GONE);
  707 + sendMessageBtn.setVisibility(View.VISIBLE);
  708 + } else {
  709 + sendMessageBtn.setVisibility(View.GONE);
  710 + ima_send.setVisibility(View.VISIBLE);
  711 + }
  712 + }
  713 + };
  714 +
  715 + /**
  716 + * 播放语音文件
  717 + *
  718 + * @param voicePath
  719 + * @param animationDrawable
  720 + */
  721 + private void startVoice(String voicePath, final AnimationDrawable animationDrawable) {
  722 + try {
  723 + FileInputStream fis = new FileInputStream(voicePath);
  724 + MediaUtil.getInstance().play(fis);
  725 + animationDrawable.start();
  726 + MediaUtil.getInstance().setEventListener(new MediaUtil.EventListener() {
  727 + @Override
  728 + public void onStop() {
  729 + animationDrawable.stop();
  730 + animationDrawable.selectDrawable(0);
  731 + }
  732 + });
  733 + } catch (Exception e) {
  734 + }
  735 + }
  736 +
  737 + @Override
  738 + public void onItemClick(final VoiceMessage voiceMessage, final AnimationDrawable animationDrawable) {
  739 + if (voiceMessage == null) {
  740 + Toast.makeText(this, "数据异常", Toast.LENGTH_LONG).show();
  741 + return;
  742 + }
  743 + if (voiceMessage.getCnVoiceMessage().isSelf()) {
  744 + startVoice(voiceMessage.getCnVoiceMessage().getVoiceUri().getPath(), animationDrawable);
  745 + } else {
  746 + //设置本地已读
  747 + voiceMessage.getCnVoiceMessage().setRead(true);
  748 + String voiceUuid = voiceMessage.getCnVoiceMessage().getUuid();
  749 + final String voicePath = FileUtil.getCacheFilePath(voiceUuid);
  750 + if (FileUtil.isCacheFileExist(voiceUuid)) {
  751 + File file = new File(voicePath);
  752 + if (file.length() < voiceMessage.getCnVoiceMessage().getDataSize()) {
  753 + Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
  754 + return;
  755 + }
  756 + startVoice(voicePath, animationDrawable);
  757 + } else {
  758 + if (voiceMessage.isDownloading()) {
  759 + Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
  760 + } else {
  761 + voiceMessage.setDownloading(true);
  762 + voiceMessage.getCnVoiceMessage().getVoice(voicePath, new IChat.OnDownloadListener() {
  763 + @Override
  764 + public void onSuccess() {
  765 + voiceMessage.setDownloading(false);
  766 + startVoice(voicePath, animationDrawable);
  767 + }
  768 +
  769 + @Override
  770 + public void onError(int i, String s) {
  771 + voiceMessage.setDownloading(false);
  772 + }
  773 + });
  774 + }
  775 + }
  776 + }
  777 + }
  778 +
  779 + @Override
  780 + public void onVideoClick(final VideoMessage videoMessage) {
  781 + if (videoMessage.getCnVideoMessage().isSelf()) {
  782 + Intent intent = new Intent(Private3Activity.this, VideoActivity.class);
  783 + intent.putExtra("videoPath", videoMessage.getCnVideoMessage().getVideoPath());
  784 + startActivity(intent);
  785 + } else {
  786 + String videoUuid = videoMessage.getCnVideoMessage().getVideo().getUuid();
  787 + final String videoPath = FileUtil.getCacheFilePath(videoUuid);
  788 + if (FileUtil.isCacheFileExist(videoUuid)) {
  789 + File file = new File(videoPath);
  790 + if (file.length() < videoMessage.getCnVideoMessage().getVideo().getSize()) {
  791 + Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
  792 + return;
  793 + }
  794 + Intent intent = new Intent(Private3Activity.this, VideoActivity.class);
  795 + intent.putExtra("videoPath", videoPath);
  796 + startActivity(intent);
  797 + } else {
  798 + if (videoMessage.isDownloading()) {
  799 + Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
  800 + } else {
  801 + videoMessage.setDownloading(true);
  802 + videoMessage.getCnVideoMessage().getVideo().getVideo(videoPath, new IChat.OnDownloadListener() {
  803 + @Override
  804 + public void onSuccess() {
  805 + videoMessage.setDownloading(false);
  806 + Intent intent = new Intent(Private3Activity.this, VideoActivity.class);
  807 + intent.putExtra("videoPath", videoPath);
  808 + startActivity(intent);
  809 + }
  810 +
  811 + @Override
  812 + public void onError(int i, String s) {
  813 + videoMessage.setDownloading(false);
  814 + }
  815 + });
  816 + }
  817 + }
  818 + }
  819 +
  820 + }
  821 +
  822 + @Override
  823 + public void onUgcVideoClick(final UgcVideoMessage ugcVideoMessage) {
  824 + if (ugcVideoMessage.getCnUgcMessage().isSelf()) {
  825 + Intent intent = new Intent(Private3Activity.this, VideoActivity.class);
  826 + intent.putExtra("videoPath", ugcVideoMessage.getCnUgcMessage().getVideoPath());
  827 + startActivity(intent);
  828 + } else {
  829 + String videoUuid = ugcVideoMessage.getCnUgcMessage().getFileId() + "_video";
  830 + final String videoPath = FileUtil.getCacheFilePath(videoUuid);
  831 + if (FileUtil.isCacheFileExist(videoUuid)) {
  832 + File file = new File(videoPath);
  833 + if (file.length() < ugcVideoMessage.getCnUgcMessage().getVideo().getSize()) {
  834 + Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
  835 + return;
  836 + }
  837 + Intent intent = new Intent(Private3Activity.this, VideoActivity.class);
  838 + intent.putExtra("videoPath", videoPath);
  839 + startActivity(intent);
  840 + } else {
  841 + if (ugcVideoMessage.isDownloading()) {
  842 + Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
  843 + } else {
  844 + ugcVideoMessage.setDownloading(true);
  845 + ugcVideoMessage.getCnUgcMessage().getVideo().getVideo(videoPath, new IChat.OnDownloadListener() {
  846 + @Override
  847 + public void onSuccess() {
  848 + ugcVideoMessage.setDownloading(false);
  849 + Intent intent = new Intent(Private3Activity.this, VideoActivity.class);
  850 + intent.putExtra("videoPath", videoPath);
  851 + startActivity(intent);
  852 + }
  853 +
  854 + @Override
  855 + public void onError(int i, String s) {
  856 + ugcVideoMessage.setDownloading(false);
  857 + }
  858 + });
  859 + }
  860 + }
  861 + }
  862 + }
  863 +
  864 + @Override
  865 + public void onImageClick(final ImagMessage imagMessage) {
  866 + if (imagMessage.getCnImgMessage().isSelf()) {
  867 + Intent intent = new Intent(Private3Activity.this, ImageViewActivity.class);
  868 + intent.putExtra("path", imagMessage.getCnImgMessage().getImagePath());
  869 + startActivity(intent);
  870 + } else {
  871 + String remoteUuid = imagMessage.getCnImgMessage().getRemoteUuid();
  872 + final String imagePath = FileUtil.getCacheFilePath(remoteUuid);
  873 + if (FileUtil.isCacheFileExist(remoteUuid)) {
  874 + File file = new File(imagePath);
  875 + if (file.length() < imagMessage.getCnImgMessage().getRemoteSize()) {
  876 + Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
  877 + return;
  878 + }
  879 + Intent intent = new Intent(Private3Activity.this, ImageViewActivity.class);
  880 + intent.putExtra("path", imagePath);
  881 + startActivity(intent);
  882 + } else {
  883 + if (imagMessage.isDownloading()) {
  884 + Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
  885 + } else {
  886 + imagMessage.setDownloading(true);
  887 + imagMessage.getCnImgMessage().getRemoteImage(imagePath, new IChat.OnDownloadListener() {
  888 + @Override
  889 + public void onSuccess() {
  890 + imagMessage.setDownloading(false);
  891 + Intent intent = new Intent(Private3Activity.this, ImageViewActivity.class);
  892 + intent.putExtra("path", imagePath);
  893 + startActivity(intent);
  894 + }
  895 +
  896 + @Override
  897 + public void onError(int i, String s) {
  898 + imagMessage.setDownloading(false);
  899 + Toast.makeText(context, "下载失败", Toast.LENGTH_SHORT).show();
  900 + }
  901 + });
  902 + }
  903 + }
  904 + }
  905 + }
  906 +
  907 + @Override
  908 + protected void onPause() {
  909 + super.onPause();
  910 + MediaUtil.getInstance().stop();
  911 + }
  912 +
  913 + @Override
  914 + protected void onDestroy() {
  915 + super.onDestroy();
  916 + ChatUtil.removeUploadProgressListener(uploadProgressListener);
  917 + ChatUtil.removeReceiveMessageListener(receiveMessageListener);
  918 + ChatUtil.removeConnectListener(connectListener);
  919 + ChatUtil.removeConnectStatusListener(connectStatusListener);
  920 + }
  921 +}
app/src/main/java/com/cnlive/im/ui/PrivateActivity.java
@@ -6,9 +6,6 @@ import android.content.Context; @@ -6,9 +6,6 @@ import android.content.Context;
6 import android.content.Intent; 6 import android.content.Intent;
7 import android.content.pm.PackageManager; 7 import android.content.pm.PackageManager;
8 import android.database.Cursor; 8 import android.database.Cursor;
9 -import android.graphics.Bitmap;  
10 -import android.graphics.drawable.AnimationDrawable;  
11 -import android.media.ThumbnailUtils;  
12 import android.net.Uri; 9 import android.net.Uri;
13 import android.os.Build; 10 import android.os.Build;
14 import android.os.Bundle; 11 import android.os.Bundle;
@@ -38,17 +35,15 @@ import android.widget.Toast; @@ -38,17 +35,15 @@ import android.widget.Toast;
38 35
39 import com.cnlive.im.R; 36 import com.cnlive.im.R;
40 import com.cnlive.im.adapter.ChatRoomAdpter; 37 import com.cnlive.im.adapter.ChatRoomAdpter;
41 -import com.cnlive.im.adapter.base.ItemClickListener; 38 +import com.cnlive.im.adapter.base.VoiceItemClickListener;
42 import com.cnlive.im.mode.CNMessageModole; 39 import com.cnlive.im.mode.CNMessageModole;
43 import com.cnlive.im.mode.ImagMessage; 40 import com.cnlive.im.mode.ImagMessage;
44 import com.cnlive.im.mode.StringMessage; 41 import com.cnlive.im.mode.StringMessage;
45 -import com.cnlive.im.mode.UgcVideoMessage;  
46 -import com.cnlive.im.mode.VideoMessage;  
47 import com.cnlive.im.mode.VoiceMessage; 42 import com.cnlive.im.mode.VoiceMessage;
48 import com.cnlive.im.util.AudioRecordUtil; 43 import com.cnlive.im.util.AudioRecordUtil;
49 import com.cnlive.im.util.FileUtil; 44 import com.cnlive.im.util.FileUtil;
50 -import com.cnlive.im.util.MediaUtil;  
51 import com.cnlive.im.util.MessageType; 45 import com.cnlive.im.util.MessageType;
  46 +import com.cnlive.im.util.PlayerUtil;
52 import com.cnlive.im.util.TimeUtils; 47 import com.cnlive.im.util.TimeUtils;
53 import com.cnlive.im.util.ToStringUtils; 48 import com.cnlive.im.util.ToStringUtils;
54 import com.cnlive.libs.util.chat.ChatUtil; 49 import com.cnlive.libs.util.chat.ChatUtil;
@@ -56,16 +51,12 @@ import com.cnlive.libs.util.chat.base.IChat; @@ -56,16 +51,12 @@ import com.cnlive.libs.util.chat.base.IChat;
56 import com.cnlive.libs.util.chat.model.CNBaseMessage; 51 import com.cnlive.libs.util.chat.model.CNBaseMessage;
57 import com.cnlive.libs.util.chat.model.CNImgMessage; 52 import com.cnlive.libs.util.chat.model.CNImgMessage;
58 import com.cnlive.libs.util.chat.model.CNMessage; 53 import com.cnlive.libs.util.chat.model.CNMessage;
59 -import com.cnlive.libs.util.chat.model.CNUgcMessage;  
60 import com.cnlive.libs.util.chat.model.CNUserInfo; 54 import com.cnlive.libs.util.chat.model.CNUserInfo;
61 -import com.cnlive.libs.util.chat.model.CNVideoMessage;  
62 import com.cnlive.libs.util.chat.model.CNVoiceMessage; 55 import com.cnlive.libs.util.chat.model.CNVoiceMessage;
63 56
64 import org.json.JSONException; 57 import org.json.JSONException;
65 import org.json.JSONObject; 58 import org.json.JSONObject;
66 59
67 -import java.io.File;  
68 -import java.io.FileInputStream;  
69 import java.util.ArrayList; 60 import java.util.ArrayList;
70 61
71 import static com.cnlive.im.R.id.opposite_id_edit; 62 import static com.cnlive.im.R.id.opposite_id_edit;
@@ -74,12 +65,10 @@ import static com.cnlive.im.R.id.opposite_id_edit; @@ -74,12 +65,10 @@ import static com.cnlive.im.R.id.opposite_id_edit;
74 * Created by Lynn on 2017/7/24. 65 * Created by Lynn on 2017/7/24.
75 */ 66 */
76 67
77 -public class PrivateActivity extends AppCompatActivity implements View.OnClickListener, ItemClickListener { 68 +public class PrivateActivity extends AppCompatActivity implements View.OnClickListener, VoiceItemClickListener {
78 69
79 private static final String TAG = "PrivateChatActivity"; 70 private static final String TAG = "PrivateChatActivity";
80 71
81 - private final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 0x2001;  
82 -  
83 // 所需的全部权限 72 // 所需的全部权限
84 static final String[] PERMISSIONS = new String[]{ 73 static final String[] PERMISSIONS = new String[]{
85 Manifest.permission.RECORD_AUDIO, 74 Manifest.permission.RECORD_AUDIO,
@@ -99,7 +88,7 @@ public class PrivateActivity extends AppCompatActivity implements View.OnClickLi @@ -99,7 +88,7 @@ public class PrivateActivity extends AppCompatActivity implements View.OnClickLi
99 private EditText messageContent; 88 private EditText messageContent;
100 private Button sendMessageBtn; 89 private Button sendMessageBtn;
101 private ChatRoomAdpter chatListAdapter; 90 private ChatRoomAdpter chatListAdapter;
102 - private ArrayList<String> msgIds = new ArrayList<>(); 91 + private ArrayList<Object> messageList = new ArrayList<>();
103 // private CNMessage cnMessage; 92 // private CNMessage cnMessage;
104 private LinearLayout linearLayoutUseID; 93 private LinearLayout linearLayoutUseID;
105 private EditText user_id_edit; 94 private EditText user_id_edit;
@@ -120,6 +109,7 @@ public class PrivateActivity extends AppCompatActivity implements View.OnClickLi @@ -120,6 +109,7 @@ public class PrivateActivity extends AppCompatActivity implements View.OnClickLi
120 private ImageView keybordBtn; 109 private ImageView keybordBtn;
121 private Button sendVoice; 110 private Button sendVoice;
122 private AudioRecordUtil mAudioRecordUtil; 111 private AudioRecordUtil mAudioRecordUtil;
  112 + private PlayerUtil playerUtil;
123 private String mFilePath; 113 private String mFilePath;
124 private PopupWindowFactory mPop; 114 private PopupWindowFactory mPop;
125 private RelativeLayout privateLayout; 115 private RelativeLayout privateLayout;
@@ -144,8 +134,8 @@ public class PrivateActivity extends AppCompatActivity implements View.OnClickLi @@ -144,8 +134,8 @@ public class PrivateActivity extends AppCompatActivity implements View.OnClickLi
144 mHandler = new Handler(); 134 mHandler = new Handler();
145 ChatUtil.setOnConnectStatusListener(connectStatusListener); 135 ChatUtil.setOnConnectStatusListener(connectStatusListener);
146 ChatUtil.setOnReceivePrivateMessageListener(receiveMessageListener); 136 ChatUtil.setOnReceivePrivateMessageListener(receiveMessageListener);
147 - ChatUtil.setUploadProgressListener(uploadProgressListener);  
148 requestPermission(); 137 requestPermission();
  138 + playerUtil = new PlayerUtil(this);
149 } 139 }
150 140
151 private void initView() { 141 private void initView() {
@@ -195,9 +185,6 @@ public class PrivateActivity extends AppCompatActivity implements View.OnClickLi @@ -195,9 +185,6 @@ public class PrivateActivity extends AppCompatActivity implements View.OnClickLi
195 chatListView.setLayoutManager(new LinearLayoutManager(PrivateActivity.this)); 185 chatListView.setLayoutManager(new LinearLayoutManager(PrivateActivity.this));
196 chatListView.setAdapter(chatListAdapter); 186 chatListView.setAdapter(chatListAdapter);
197 187
198 - findViewById(R.id.videoBtn).setOnClickListener(this);  
199 - findViewById(R.id.ugcVideoBtn).setOnClickListener(this);  
200 -  
201 StartListener(); 188 StartListener();
202 } 189 }
203 190
@@ -265,31 +252,9 @@ public class PrivateActivity extends AppCompatActivity implements View.OnClickLi @@ -265,31 +252,9 @@ public class PrivateActivity extends AppCompatActivity implements View.OnClickLi
265 case R.id.ima_send: 252 case R.id.ima_send:
266 sendMessage(IMAGE_MESSAGE); 253 sendMessage(IMAGE_MESSAGE);
267 break; 254 break;
268 - case R.id.videoBtn:  
269 - openSystemRecorder();  
270 - break;  
271 - case R.id.ugcVideoBtn:  
272 - openSystemRecorder();  
273 - break;  
274 } 255 }
275 } 256 }
276 257
277 - private void openSystemRecorder() {  
278 - // 创建拍照Intent并将控制权返回给调用的程序  
279 - Intent intent = new Intent();  
280 - intent.setAction(MediaStore.ACTION_VIDEO_CAPTURE);  
281 - intent.addCategory(Intent.CATEGORY_DEFAULT);  
282 -  
283 - // 保存录像到指定的路径  
284 - File file = new File("/sdcard/video.mp4");  
285 - Uri uri = Uri.fromFile(file);  
286 - intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);  
287 -  
288 - // 启动图像捕获Intent  
289 - startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);  
290 - }  
291 -  
292 -  
293 private void sendMessage(String type) { 258 private void sendMessage(String type) {
294 if (sendInfo == 2) { 259 if (sendInfo == 2) {
295 Toast.makeText(this, "等待对方接受邀请", Toast.LENGTH_SHORT).show(); 260 Toast.makeText(this, "等待对方接受邀请", Toast.LENGTH_SHORT).show();
@@ -365,12 +330,6 @@ public class PrivateActivity extends AppCompatActivity implements View.OnClickLi @@ -365,12 +330,6 @@ public class PrivateActivity extends AppCompatActivity implements View.OnClickLi
365 Uri uri = Uri.parse("file://" + picturePath); 330 Uri uri = Uri.parse("file://" + picturePath);
366 ChatUtil.sendImaMessage(IChat.PRIVATE, oppositeID, uri, uri, sendMessageListener); 331 ChatUtil.sendImaMessage(IChat.PRIVATE, oppositeID, uri, uri, sendMessageListener);
367 break; 332 break;
368 - case CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE:  
369 - String videoPath = data.getData().getPath();  
370 - Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, MediaStore.Images.Thumbnails.FULL_SCREEN_KIND);  
371 - String imgPath = FileUtil.createFile(bitmap, System.currentTimeMillis() + "");  
372 - ChatUtil.sendVideoMessage(IChat.PRIVATE, oppositeID, videoPath, imgPath, MediaUtil.getInstance().getDuration(videoPath), sendMessageListener);  
373 - break;  
374 333
375 default: 334 default:
376 break; 335 break;
@@ -407,150 +366,120 @@ public class PrivateActivity extends AppCompatActivity implements View.OnClickLi @@ -407,150 +366,120 @@ public class PrivateActivity extends AppCompatActivity implements View.OnClickLi
407 /** 366 /**
408 * 发送消息监听 367 * 发送消息监听
409 */ 368 */
  369 +
410 private IChat.OnSendMessageListener sendMessageListener = new IChat.OnSendMessageListener() { 370 private IChat.OnSendMessageListener sendMessageListener = new IChat.OnSendMessageListener() {
411 @Override 371 @Override
412 public void onAttached(CNBaseMessage o) { 372 public void onAttached(CNBaseMessage o) {
413 - showMessage(o, false);  
414 } 373 }
415 374
416 @Override 375 @Override
417 public void onSuccess(final CNBaseMessage o) { 376 public void onSuccess(final CNBaseMessage o) {
418 - updateItem(o, -1); 377 + if (View.VISIBLE == vistor_btn.getVisibility()) {
  378 + vistor_btn.setVisibility(View.GONE);
  379 + }
  380 +
  381 + mHandler.post(new Runnable() {
  382 + @Override
  383 + public void run() {
  384 + if (o instanceof CNMessage) {
  385 + CNMessage o1 = (CNMessage) o;
  386 + JSONObject jsonObject = null;
  387 + try {
  388 + jsonObject = new JSONObject(o1.getContent().toString());
  389 + String type = (String) jsonObject.get("type");
  390 + String message = (String) jsonObject.get("message");
  391 + Toast.makeText(PrivateActivity.this, "发送消息成功", Toast.LENGTH_SHORT).show();
  392 + if ("3".equals(type) && message.equals("你好我们可以进行私聊吗")) {
  393 + sendInfo = 2;
  394 + Toast.makeText(PrivateActivity.this, "申请已发送等待对方验证", Toast.LENGTH_SHORT).show();
  395 + } else if ("3".equals(type) && message.equals("拒绝")) {
  396 + sendInfo = 5;
  397 + } else if ("3".equals(type) && message.equals("同意")) {
  398 + sendInfo = 1;
  399 + } else if ("4".equals(type)) {
  400 + chatListAdapter.addItem(new StringMessage(MessageType.stringMessage, o1, true));
  401 + chatListView.smoothScrollToPosition(chatListAdapter.getItemCount() - 1);
  402 +
  403 + }
  404 + } catch (JSONException e) {
  405 +
  406 +
  407 + }
  408 + } else if (o instanceof CNImgMessage) {
  409 + CNImgMessage o1 = (CNImgMessage) o;
  410 + chatListAdapter.addItem(new ImagMessage(MessageType.imaMessage, o1, true));
  411 + chatListView.smoothScrollToPosition(chatListAdapter.getItemCount() - 1);
  412 + } else if (o instanceof CNVoiceMessage) {
  413 + CNVoiceMessage o1 = (CNVoiceMessage) o;
  414 + VoiceMessage voiceMessage = new VoiceMessage(MessageType.voiceMessage, o1, true);
  415 + chatListAdapter.addItem(voiceMessage);
  416 + //发送成功后删除保存在本地的录制文件
  417 + mAudioRecordUtil.deleteRecordFile(mFilePath);
  418 + chatListView.smoothScrollToPosition(chatListAdapter.getItemCount() - 1);
  419 + }
  420 + }
  421 + });
419 } 422 }
420 423
421 @Override 424 @Override
422 public void onError(CNBaseMessage o, int i, String s) { 425 public void onError(CNBaseMessage o, int i, String s) {
423 - updateItem(o, -1);  
424 } 426 }
425 }; 427 };
426 428
  429 +
427 //连接状态监听 430 //连接状态监听
428 private IChat.OnConnectStatusListener connectStatusListener = new IChat.OnConnectStatusListener() { 431 private IChat.OnConnectStatusListener connectStatusListener = new IChat.OnConnectStatusListener() {
429 @Override 432 @Override
430 public void onConnectStatus(int i, String s) { 433 public void onConnectStatus(int i, String s) {
431 - Log.i(TAG, "连接状态监听" + i + " " + s);  
432 - }  
433 - }; 434 + Log.i(TAG, "私聊连接状态监听私聊" + i + " " + s);
434 435
435 436
  437 + }
  438 + };
436 //接收消息监听 439 //接收消息监听
437 private IChat.OnReceiveMessageListener receiveMessageListener = new IChat.OnReceiveMessageListener() { 440 private IChat.OnReceiveMessageListener receiveMessageListener = new IChat.OnReceiveMessageListener() {
438 @Override 441 @Override
439 public void processMessage(final CNBaseMessage o) { 442 public void processMessage(final CNBaseMessage o) {
440 - if (!o.getTargetId().equals(oppositeID)) return;  
441 - showMessage(o, true);  
442 - }  
443 - };  
444 -  
445 - private IChat.OnUploadProgressListener uploadProgressListener = new IChat.OnUploadProgressListener() {  
446 - @Override  
447 - public void onMessagesUpdate(CNBaseMessage message, int elemIdx, int taskId, int progress) {  
448 - if (!message.getTargetId().equals(oppositeID)) return;  
449 - updateItem(message, progress);  
450 - }  
451 - };  
452 -  
453 - /**  
454 - * 更新消息  
455 - */  
456 - public void updateItem(CNBaseMessage o, int progress) {  
457 - int updateIndex = msgIds.indexOf(o.getMessageId());  
458 - if (updateIndex < 0 || chatListAdapter.getItemCount() <= 0 || updateIndex > chatListAdapter.getItemCount() - 1)  
459 - return;  
460 - if (o.getMsgType() == IChat.CN_MSG_TYPE_TEXT) {  
461 - CNMessage o1 = (CNMessage) o;  
462 - chatListAdapter.updateItem(new StringMessage(MessageType.stringMessage, o1, o1.isSelf()), updateIndex);  
463 - } else if (o.getMsgType() == IChat.CN_MSG_TYPE_IMAGE) {  
464 - CNImgMessage o1 = (CNImgMessage) o;  
465 - chatListAdapter.updateItem(new ImagMessage(MessageType.imaMessage, o1, o1.isSelf(), progress < 0 ? 0 : progress), updateIndex);  
466 - } else if (o.getMsgType() == IChat.CN_MSG_TYPE_VOICE) {  
467 - CNVoiceMessage o1 = (CNVoiceMessage) o;  
468 - VoiceMessage voiceMessage = new VoiceMessage(MessageType.voiceMessage, o1, o1.isSelf(), progress < 0 ? 0 : progress);  
469 - chatListAdapter.updateItem(voiceMessage, updateIndex);  
470 - } else if (o.getMsgType() == IChat.CN_MSG_TYPE_VIDEO) {  
471 - CNVideoMessage ol = (CNVideoMessage) o;  
472 - VideoMessage videoMessage = new VideoMessage(MessageType.videoMessage, ol, ol.isSelf(), progress < 0 ? 0 : progress);  
473 - chatListAdapter.updateItem(videoMessage, updateIndex);  
474 - } else if (o.getMsgType() == IChat.CN_MSG_TYPE_UGC) {  
475 - CNUgcMessage ol = (CNUgcMessage) o;  
476 - UgcVideoMessage ugcVideoMessage = new UgcVideoMessage(MessageType.ugcVideoMessage, ol, ol.isSelf(), progress < 0 ? 0 : progress);  
477 - chatListAdapter.updateItem(ugcVideoMessage, updateIndex);  
478 - }  
479 - } 443 + mHandler.post(new Runnable() {
  444 + @Override
  445 + public void run() {
  446 + if (o instanceof CNMessage) {
  447 + CNMessage o1 = (CNMessage) o;
  448 + JSONObject jsonObject = null;
  449 + try {
  450 + jsonObject = new JSONObject(o1.getContent().toString());
  451 + String type = (String) jsonObject.get("type");
  452 + if ("3".equals(type) && "你好我们可以进行私聊吗".equals(jsonObject.get("message"))) {
  453 + vistor_btn.setVisibility(View.GONE);
  454 + agree_reject_lin.setVisibility(View.VISIBLE);
  455 + } else if ("3".equals(type) && "同意".equals(jsonObject.get("message"))) {
  456 + sendInfo = 1;
  457 + } else if ("3".equals(type) && "拒绝".equals(jsonObject.get("message"))) {
  458 + sendInfo = 4;
  459 + } else if ("4".equals(type)) {
  460 + Log.v(TAG, o1.getContent().toString());
  461 + chatListAdapter.addItem(new StringMessage(MessageType.stringMessage, o1, false));
  462 + chatListView.smoothScrollToPosition(chatListAdapter.getItemCount() - 1);
  463 + }
480 464
481 - /**  
482 - * 展示消息  
483 - *  
484 - * @param o  
485 - */  
486 - private void showMessage(CNBaseMessage o, boolean isReceivedMsg) {  
487 - if (o.getMsgType() == IChat.CN_MSG_TYPE_TEXT) {  
488 - if (isReceivedMsg) {  
489 - CNMessage o1 = (CNMessage) o;  
490 - JSONObject jsonObject = null;  
491 - try {  
492 - jsonObject = new JSONObject(o1.getContent().toString());  
493 - String type = (String) jsonObject.get("type");  
494 - if ("3".equals(type) && "你好我们可以进行私聊吗".equals(jsonObject.get("message"))) {  
495 - vistor_btn.setVisibility(View.GONE);  
496 - agree_reject_lin.setVisibility(View.VISIBLE);  
497 - } else if ("3".equals(type) && "同意".equals(jsonObject.get("message"))) {  
498 - sendInfo = 1;  
499 - } else if ("3".equals(type) && "拒绝".equals(jsonObject.get("message"))) {  
500 - sendInfo = 4;  
501 - } else if ("4".equals(type)) {  
502 - Log.v(TAG, o1.getContent().toString());  
503 - msgIds.add(o.getMessageId());  
504 - chatListAdapter.addItem(new StringMessage(MessageType.stringMessage, o1, false));  
505 - }  
506 - } catch (JSONException e) {  
507 - e.printStackTrace();  
508 - }  
509 - } else {  
510 - CNMessage o1 = (CNMessage) o;  
511 - JSONObject jsonObject = null;  
512 - try {  
513 - jsonObject = new JSONObject(o1.getContent().toString());  
514 - String type = (String) jsonObject.get("type");  
515 - String message = (String) jsonObject.get("message");  
516 - Toast.makeText(PrivateActivity.this, "发送消息成功", Toast.LENGTH_SHORT).show();  
517 - if ("3".equals(type) && message.equals("你好我们可以进行私聊吗")) {  
518 - sendInfo = 2;  
519 - Toast.makeText(PrivateActivity.this, "申请已发送等待对方验证", Toast.LENGTH_SHORT).show();  
520 - } else if ("3".equals(type) && message.equals("拒绝")) {  
521 - sendInfo = 5;  
522 - } else if ("3".equals(type) && message.equals("同意")) {  
523 - sendInfo = 1;  
524 - } else if ("4".equals(type)) {  
525 - msgIds.add(o.getMessageId());  
526 - chatListAdapter.addItem(new StringMessage(MessageType.stringMessage, o1, true)); 465 + } catch (JSONException e) {
  466 + e.printStackTrace();
  467 + }
  468 + } else if (o instanceof CNImgMessage) {
  469 + CNImgMessage o1 = (CNImgMessage) o;
  470 + chatListAdapter.addItem(new ImagMessage(MessageType.imaMessage, o1, false));
  471 + chatListView.smoothScrollToPosition(chatListAdapter.getItemCount() - 1);
  472 + } else if (o instanceof CNVoiceMessage) {
  473 + CNVoiceMessage o1 = (CNVoiceMessage) o;
  474 + VoiceMessage voiceMessage = new VoiceMessage(MessageType.voiceMessage, o1, false);
  475 + chatListAdapter.addItem(voiceMessage);
  476 + chatListView.smoothScrollToPosition(chatListAdapter.getItemCount() - 1);
527 } 477 }
528 - } catch (JSONException e) {  
529 } 478 }
530 - }  
531 - } else if (o.getMsgType() == IChat.CN_MSG_TYPE_IMAGE) {  
532 - msgIds.add(o.getMessageId());  
533 - CNImgMessage o1 = (CNImgMessage) o;  
534 - chatListAdapter.addItem(new ImagMessage(MessageType.imaMessage, o1, o1.isSelf()));  
535 - } else if (o.getMsgType() == IChat.CN_MSG_TYPE_VOICE) {  
536 - msgIds.add(o.getMessageId());  
537 - CNVoiceMessage o1 = (CNVoiceMessage) o;  
538 - VoiceMessage voiceMessage = new VoiceMessage(MessageType.voiceMessage, o1, o1.isSelf());  
539 - chatListAdapter.addItem(voiceMessage);  
540 - } else if (o.getMsgType() == IChat.CN_MSG_TYPE_VIDEO) {  
541 - msgIds.add(o.getMessageId());  
542 - CNVideoMessage ol = (CNVideoMessage) o;  
543 - VideoMessage videoMessage = new VideoMessage(MessageType.videoMessage, ol, ol.isSelf());  
544 - chatListAdapter.addItem(videoMessage);  
545 - } else if (o.getMsgType() == IChat.CN_MSG_TYPE_UGC) {  
546 - msgIds.add(o.getMessageId());  
547 - CNUgcMessage ol = (CNUgcMessage) o;  
548 - UgcVideoMessage ugcVideoMessage = new UgcVideoMessage(MessageType.ugcVideoMessage, ol, ol.isSelf());  
549 - chatListAdapter.addItem(ugcVideoMessage); 479 + });
  480 +
550 } 481 }
551 - if (chatListAdapter.getItemCount() > 0)  
552 - chatListView.smoothScrollToPosition(chatListAdapter.getItemCount() - 1);  
553 - } 482 + };
554 483
555 /** 484 /**
556 * 开启扫描之前判断权限是否打开 485 * 开启扫描之前判断权限是否打开
@@ -561,15 +490,14 @@ public class PrivateActivity extends AppCompatActivity implements View.OnClickLi @@ -561,15 +490,14 @@ public class PrivateActivity extends AppCompatActivity implements View.OnClickLi
561 Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) && 490 Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) &&
562 (ContextCompat.checkSelfPermission(context, 491 (ContextCompat.checkSelfPermission(context,
563 Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED) 492 Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED)
564 - && (ContextCompat.checkSelfPermission(context,  
565 - Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)) { 493 + ) {
566 StartListener(); 494 StartListener();
567 495
568 //判断是否开启语音权限 496 //判断是否开启语音权限
569 } else { 497 } else {
570 //请求获取摄像头权限 498 //请求获取摄像头权限
571 ActivityCompat.requestPermissions((Activity) context, 499 ActivityCompat.requestPermissions((Activity) context,
572 - new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA}, VOICE_REQUEST_CODE); 500 + new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO}, VOICE_REQUEST_CODE);
573 } 501 }
574 } 502 }
575 503
@@ -626,7 +554,7 @@ public class PrivateActivity extends AppCompatActivity implements View.OnClickLi @@ -626,7 +554,7 @@ public class PrivateActivity extends AppCompatActivity implements View.OnClickLi
626 float moveX = Math.abs(event.getX() - downX); 554 float moveX = Math.abs(event.getX() - downX);
627 float moveY = Math.abs(event.getY() - downY); 555 float moveY = Math.abs(event.getY() - downY);
628 if (moveY > 100) { 556 if (moveY > 100) {
629 -// Toast.makeText(PrivateActivity.this, "cancel", Toast.LENGTH_SHORT).show(); 557 +// Toast.makeText(ChatRoomActivity.this, "cancel", Toast.LENGTH_SHORT).show();
630 mAudioRecordUtil.cancelRecord(); 558 mAudioRecordUtil.cancelRecord();
631 } else { 559 } else {
632 mAudioRecordUtil.stopRecord(); //结束录音(保存录音文件) 560 mAudioRecordUtil.stopRecord(); //结束录音(保存录音文件)
@@ -639,7 +567,7 @@ public class PrivateActivity extends AppCompatActivity implements View.OnClickLi @@ -639,7 +567,7 @@ public class PrivateActivity extends AppCompatActivity implements View.OnClickLi
639 float moveXm = Math.abs(event.getX() - downX); 567 float moveXm = Math.abs(event.getX() - downX);
640 float moveYm = Math.abs(event.getY() - downY); 568 float moveYm = Math.abs(event.getY() - downY);
641 if (moveYm > 100) { 569 if (moveYm > 100) {
642 -// Toast.makeText(PrivateActivity.this, "cancel", Toast.LENGTH_SHORT).show(); 570 +// Toast.makeText(ChatRoomActivity.this, "cancel", Toast.LENGTH_SHORT).show();
643 if (sendMsgLayout.getVisibility() == View.VISIBLE) { 571 if (sendMsgLayout.getVisibility() == View.VISIBLE) {
644 sendMsgLayout.setVisibility(View.GONE); 572 sendMsgLayout.setVisibility(View.GONE);
645 cancelSendLayout.setVisibility(View.VISIBLE); 573 cancelSendLayout.setVisibility(View.VISIBLE);
@@ -711,210 +639,24 @@ public class PrivateActivity extends AppCompatActivity implements View.OnClickLi @@ -711,210 +639,24 @@ public class PrivateActivity extends AppCompatActivity implements View.OnClickLi
711 } 639 }
712 }; 640 };
713 641
714 - /**  
715 - * 播放语音文件  
716 - *  
717 - * @param voicePath  
718 - * @param animationDrawable  
719 - */  
720 - private void startVoice(String voicePath, final AnimationDrawable animationDrawable) {  
721 - try {  
722 - FileInputStream fis = new FileInputStream(voicePath);  
723 - MediaUtil.getInstance().play(fis);  
724 - animationDrawable.start();  
725 - MediaUtil.getInstance().setEventListener(new MediaUtil.EventListener() {  
726 - @Override  
727 - public void onStop() {  
728 - animationDrawable.stop();  
729 - animationDrawable.selectDrawable(0);  
730 - }  
731 - });  
732 - } catch (Exception e) {  
733 - }  
734 - }  
735 -  
736 @Override 642 @Override
737 - public void onItemClick(final VoiceMessage voiceMessage, final AnimationDrawable animationDrawable) {  
738 - if (voiceMessage == null) {  
739 - Toast.makeText(this, "数据异常", Toast.LENGTH_LONG).show();  
740 - return;  
741 - }  
742 - if (voiceMessage.getCnVoiceMessage().isSelf()) {  
743 - startVoice(voiceMessage.getCnVoiceMessage().getVoiceUri().getPath(), animationDrawable); 643 + public void onItemClick(VoiceMessage voiceMessage) {
  644 + if (voiceMessage.isUserSend()) {
  645 + playerUtil.start(voiceMessage.getCnVoiceMessage().getVoiceUri());
744 } else { 646 } else {
745 - //设置本地已读  
746 - voiceMessage.getCnVoiceMessage().setRead(true);  
747 - String voiceUuid = voiceMessage.getCnVoiceMessage().getUuid();  
748 - final String voicePath = FileUtil.getCacheFilePath(voiceUuid);  
749 - if (FileUtil.isCacheFileExist(voiceUuid)) {  
750 - File file = new File(voicePath);  
751 - if (file.length() < voiceMessage.getCnVoiceMessage().getDataSize()) {  
752 - Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();  
753 - return;  
754 - }  
755 - startVoice(voicePath, animationDrawable);  
756 - } else {  
757 - if (voiceMessage.isDownloading()) {  
758 - Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();  
759 - } else {  
760 - voiceMessage.setDownloading(true);  
761 - voiceMessage.getCnVoiceMessage().getVoice(voicePath, new IChat.OnDownloadListener() {  
762 - @Override  
763 - public void onSuccess() {  
764 - voiceMessage.setDownloading(false);  
765 - startVoice(voicePath, animationDrawable);  
766 - }  
767 -  
768 - @Override  
769 - public void onError(int i, String s) {  
770 - voiceMessage.setDownloading(false);  
771 - }  
772 - });  
773 - }  
774 - }  
775 - }  
776 - }  
777 -  
778 - @Override  
779 - public void onVideoClick(final VideoMessage videoMessage) {  
780 - if (videoMessage.getCnVideoMessage().isSelf()) {  
781 - Intent intent = new Intent(PrivateActivity.this, VideoActivity.class);  
782 - intent.putExtra("videoPath", videoMessage.getCnVideoMessage().getVideoPath());  
783 - startActivity(intent);  
784 - } else {  
785 - String videoUuid = videoMessage.getCnVideoMessage().getVideo().getUuid();  
786 - final String videoPath = FileUtil.getCacheFilePath(videoUuid);  
787 - if (FileUtil.isCacheFileExist(videoUuid)) {  
788 - File file = new File(videoPath);  
789 - if (file.length() < videoMessage.getCnVideoMessage().getVideo().getSize()) {  
790 - Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();  
791 - return;  
792 - }  
793 - Intent intent = new Intent(PrivateActivity.this, VideoActivity.class);  
794 - intent.putExtra("videoPath", videoPath);  
795 - startActivity(intent);  
796 - } else {  
797 - if (videoMessage.isDownloading()) {  
798 - Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();  
799 - } else {  
800 - videoMessage.setDownloading(true);  
801 - videoMessage.getCnVideoMessage().getVideo().getVideo(videoPath, new IChat.OnDownloadListener() {  
802 - @Override  
803 - public void onSuccess() {  
804 - videoMessage.setDownloading(false);  
805 - Intent intent = new Intent(PrivateActivity.this, VideoActivity.class);  
806 - intent.putExtra("videoPath", videoPath);  
807 - startActivity(intent);  
808 - }  
809 -  
810 - @Override  
811 - public void onError(int i, String s) {  
812 - videoMessage.setDownloading(false);  
813 - }  
814 - });  
815 - }  
816 - }  
817 - }  
818 -  
819 - }  
820 -  
821 - @Override  
822 - public void onUgcVideoClick(final UgcVideoMessage ugcVideoMessage) {  
823 - if (ugcVideoMessage.getCnUgcMessage().isSelf()) {  
824 - Intent intent = new Intent(PrivateActivity.this, VideoActivity.class);  
825 - intent.putExtra("videoPath", ugcVideoMessage.getCnUgcMessage().getVideoPath());  
826 - startActivity(intent);  
827 - } else {  
828 - String videoUuid = ugcVideoMessage.getCnUgcMessage().getFileId() + "_video";  
829 - final String videoPath = FileUtil.getCacheFilePath(videoUuid);  
830 - if (FileUtil.isCacheFileExist(videoUuid)) {  
831 - File file = new File(videoPath);  
832 - if (file.length() < ugcVideoMessage.getCnUgcMessage().getVideo().getSize()) {  
833 - Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();  
834 - return;  
835 - }  
836 - Intent intent = new Intent(PrivateActivity.this, VideoActivity.class);  
837 - intent.putExtra("videoPath", videoPath);  
838 - startActivity(intent);  
839 - } else {  
840 - if (ugcVideoMessage.isDownloading()) {  
841 - Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();  
842 - } else {  
843 - ugcVideoMessage.setDownloading(true);  
844 - ugcVideoMessage.getCnUgcMessage().getVideo().getVideo(videoPath, new IChat.OnDownloadListener() {  
845 - @Override  
846 - public void onSuccess() {  
847 - ugcVideoMessage.setDownloading(false);  
848 - Intent intent = new Intent(PrivateActivity.this, VideoActivity.class);  
849 - intent.putExtra("videoPath", videoPath);  
850 - startActivity(intent);  
851 - }  
852 -  
853 - @Override  
854 - public void onError(int i, String s) {  
855 - ugcVideoMessage.setDownloading(false);  
856 - }  
857 - }); 647 + final String voicePath = FileUtil.getTempFile(FileUtil.FileType.AUDIO).getAbsolutePath();
  648 + voiceMessage.getCnVoiceMessage().getVoice(voicePath, new IChat.OnDownloadListener() {
  649 + @Override
  650 + public void onSuccess() {
  651 + if (playerUtil != null)
  652 + playerUtil.start(Uri.parse(voicePath));
858 } 653 }
859 - }  
860 - }  
861 - }  
862 654
863 - @Override  
864 - public void onImageClick(final ImagMessage imagMessage) {  
865 - if (imagMessage.getCnImgMessage().isSelf()) {  
866 - Intent intent = new Intent(PrivateActivity.this, ImageViewActivity.class);  
867 - intent.putExtra("path", imagMessage.getCnImgMessage().getImagePath());  
868 - startActivity(intent);  
869 - } else {  
870 - String remoteUuid = imagMessage.getCnImgMessage().getRemoteUuid();  
871 - final String imagePath = FileUtil.getCacheFilePath(remoteUuid);  
872 - if (FileUtil.isCacheFileExist(remoteUuid)) {  
873 - File file = new File(imagePath);  
874 - if (file.length() < imagMessage.getCnImgMessage().getRemoteSize()) {  
875 - Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();  
876 - return;  
877 - }  
878 - Intent intent = new Intent(PrivateActivity.this, ImageViewActivity.class);  
879 - intent.putExtra("path", imagePath);  
880 - startActivity(intent);  
881 - } else {  
882 - if (imagMessage.isDownloading()) {  
883 - Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();  
884 - } else {  
885 - imagMessage.setDownloading(true);  
886 - imagMessage.getCnImgMessage().getRemoteImage(imagePath, new IChat.OnDownloadListener() {  
887 - @Override  
888 - public void onSuccess() {  
889 - imagMessage.setDownloading(false);  
890 - Intent intent = new Intent(PrivateActivity.this, ImageViewActivity.class);  
891 - intent.putExtra("path", imagePath);  
892 - startActivity(intent);  
893 - } 655 + @Override
  656 + public void onError(int i, String s) {
894 657
895 - @Override  
896 - public void onError(int i, String s) {  
897 - imagMessage.setDownloading(false);  
898 - Toast.makeText(context, "下载失败", Toast.LENGTH_SHORT).show();  
899 - }  
900 - });  
901 } 658 }
902 - } 659 + });
903 } 660 }
904 } 661 }
905 -  
906 - @Override  
907 - protected void onPause() {  
908 - super.onPause();  
909 - MediaUtil.getInstance().stop();  
910 - }  
911 -  
912 - @Override  
913 - protected void onDestroy() {  
914 - super.onDestroy();  
915 - ChatUtil.removeUploadProgressListener(uploadProgressListener);  
916 - ChatUtil.removeReceiveMessageListener(receiveMessageListener);  
917 - ChatUtil.removeConnectListener(connectListener);  
918 - ChatUtil.removeConnectStatusListener(connectStatusListener);  
919 - }  
920 } 662 }
app/src/main/java/com/cnlive/im/ui/UserLoginActivity.java
@@ -22,6 +22,7 @@ public class UserLoginActivity extends AppCompatActivity implements View.OnClick @@ -22,6 +22,7 @@ public class UserLoginActivity extends AppCompatActivity implements View.OnClick
22 private EditText user_name; 22 private EditText user_name;
23 private EditText user_id; 23 private EditText user_id;
24 private Button group_chatBtn, private_chatBtn; 24 private Button group_chatBtn, private_chatBtn;
  25 + private Button group_3_chatBtn, private_3_chatBtn;
25 private String user_name_string; 26 private String user_name_string;
26 private String user_id_string; 27 private String user_id_string;
27 28
@@ -40,6 +41,10 @@ public class UserLoginActivity extends AppCompatActivity implements View.OnClick @@ -40,6 +41,10 @@ public class UserLoginActivity extends AppCompatActivity implements View.OnClick
40 group_chatBtn.setOnClickListener(this); 41 group_chatBtn.setOnClickListener(this);
41 private_chatBtn = (Button) findViewById(R.id.private_chat); 42 private_chatBtn = (Button) findViewById(R.id.private_chat);
42 private_chatBtn.setOnClickListener(this); 43 private_chatBtn.setOnClickListener(this);
  44 + group_3_chatBtn = (Button) findViewById(R.id.group_chat_3);
  45 + group_3_chatBtn.setOnClickListener(this);
  46 + private_3_chatBtn = (Button) findViewById(R.id.private_chat_3);
  47 + private_3_chatBtn.setOnClickListener(this);
43 } 48 }
44 49
45 50
@@ -51,7 +56,12 @@ public class UserLoginActivity extends AppCompatActivity implements View.OnClick @@ -51,7 +56,12 @@ public class UserLoginActivity extends AppCompatActivity implements View.OnClick
51 break; 56 break;
52 case R.id.private_chat: 57 case R.id.private_chat:
53 doIntentChatRoom(PrivateActivity.class); 58 doIntentChatRoom(PrivateActivity.class);
54 - 59 + break;
  60 + case R.id.group_chat_3:
  61 + doIntentChatRoom(ChatRoom3Activity.class);
  62 + break;
  63 + case R.id.private_chat_3:
  64 + doIntentChatRoom(Private3Activity.class);
55 break; 65 break;
56 } 66 }
57 67
app/src/main/java/com/cnlive/im/util/PlayerUtil.java 0 → 100644
  1 +package com.cnlive.im.util;
  2 +
  3 +import android.content.Context;
  4 +import android.media.MediaPlayer;
  5 +import android.net.Uri;
  6 +
  7 +/**
  8 + * Created by Lynn on 2017/7/21.
  9 + */
  10 +
  11 +public class PlayerUtil implements IVoiceManager{
  12 + private final String TAG = "PlayerUtil";
  13 + private String path;
  14 + private Context mContext;
  15 +
  16 + private MediaPlayer mediaPlayer;
  17 +
  18 + public PlayerUtil(Context context){
  19 + this.mContext = context;
  20 +// mediaPlayer = new MediaPlayer();
  21 +// mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
  22 +// @Override
  23 +// public void onCompletion(MediaPlayer mediaPlayer) {
  24 +//// mediaPlayer.stop();
  25 +// stop();
  26 +// }
  27 +// });
  28 + }
  29 +
  30 + @Override
  31 + public boolean start(Uri uri) {
  32 + try {
  33 + mediaPlayer = new MediaPlayer();
  34 + mediaPlayer.setOnCompletionListener(onCompletionListener);
  35 + mediaPlayer.setOnPreparedListener(onPreparedListener);
  36 + mediaPlayer.setDataSource(mContext, uri);
  37 + mediaPlayer.prepare();
  38 +// mediaPlayer.start();
  39 + }catch (Exception e){
  40 + e.printStackTrace();
  41 + }
  42 + return false;
  43 + }
  44 +
  45 + @Override
  46 + public boolean stop() {
  47 + mediaPlayer.stop();
  48 + mediaPlayer.release();
  49 + mediaPlayer = null;
  50 + return false;
  51 + }
  52 +
  53 + @Override
  54 + public void onDestory() {
  55 + mediaPlayer.release();
  56 + mediaPlayer = null;
  57 + }
  58 +
  59 + private MediaPlayer.OnCompletionListener onCompletionListener = new MediaPlayer.OnCompletionListener() {
  60 + @Override
  61 + public void onCompletion(MediaPlayer mediaPlayer) {
  62 + stop();
  63 + }
  64 + };
  65 +
  66 + private MediaPlayer.OnPreparedListener onPreparedListener = new MediaPlayer.OnPreparedListener() {
  67 + @Override
  68 + public void onPrepared(MediaPlayer mediaPlayer) {
  69 + mediaPlayer.start();
  70 + }
  71 + };
  72 +}
app/src/main/res/layout/activity_groupchat.xml
@@ -117,18 +117,6 @@ @@ -117,18 +117,6 @@
117 android:layout_height="wrap_content" 117 android:layout_height="wrap_content"
118 android:text="退出" /> 118 android:text="退出" />
119 119
120 - <Button  
121 - android:id="@+id/videoBtn"  
122 - android:layout_width="wrap_content"  
123 - android:layout_height="wrap_content"  
124 - android:text="视频文件"/>  
125 -  
126 - <Button  
127 - android:id="@+id/ugcVideoBtn"  
128 - android:layout_width="wrap_content"  
129 - android:layout_height="wrap_content"  
130 - android:text="短视频"/>  
131 -  
132 </LinearLayout> 120 </LinearLayout>
133 121
134 </LinearLayout> 122 </LinearLayout>
app/src/main/res/layout/activity_groupchat_3.xml 0 → 100644
  1 +<?xml version="1.0" encoding="utf-8"?>
  2 +<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3 + android:id="@+id/activity_chat_room"
  4 + android:layout_width="match_parent"
  5 + android:layout_height="match_parent">
  6 +
  7 + <RelativeLayout
  8 + android:id="@+id/re_chat"
  9 + android:layout_width="match_parent"
  10 + android:layout_height="match_parent"
  11 + android:layout_alignParentLeft="true"
  12 + android:layout_alignParentStart="true"
  13 + android:layout_alignParentTop="true"
  14 + android:clickable="true"
  15 + android:visibility="visible">
  16 +
  17 + <LinearLayout
  18 + android:id="@+id/listVIewTop"
  19 + android:layout_width="match_parent"
  20 + android:layout_height="match_parent"
  21 + android:layout_above="@+id/buttons"
  22 + android:layout_alignParentLeft="true"
  23 + android:layout_alignParentStart="true"
  24 + android:layout_alignParentTop="true"
  25 + android:orientation="horizontal">
  26 +
  27 +
  28 + <android.support.v7.widget.RecyclerView
  29 + android:id="@+id/chat_list_viewOne"
  30 + android:layout_width="match_parent"
  31 + android:layout_height="match_parent"
  32 + android:layout_above="@+id/edit_layout"
  33 + android:divider="@null"
  34 + android:fastScrollEnabled="true"
  35 + android:scrollbars="none" />
  36 + </LinearLayout>
  37 +
  38 + <LinearLayout
  39 + android:id="@+id/edit_layout"
  40 + android:layout_width="match_parent"
  41 + android:layout_height="wrap_content"
  42 + android:layout_alignParentBottom="true"
  43 + android:orientation="horizontal">
  44 +
  45 + <ImageView
  46 + android:id="@+id/keybord"
  47 + android:layout_width="0dp"
  48 + android:layout_height="wrap_content"
  49 + android:layout_gravity="center_vertical"
  50 + android:layout_weight="0.1"
  51 + android:src="@drawable/voice" />
  52 +
  53 + <EditText
  54 + android:id="@+id/message_content"
  55 + android:layout_width="0dp"
  56 + android:layout_height="wrap_content"
  57 + android:layout_gravity="center"
  58 + android:layout_weight="0.6" />
  59 +
  60 + <Button
  61 + android:id="@+id/send_voice"
  62 + android:layout_width="0dp"
  63 + android:layout_height="40dp"
  64 + android:layout_margin="10dp"
  65 + android:layout_weight="0.6"
  66 + android:background="@drawable/voice_bg"
  67 + android:text="按住说话"
  68 + android:visibility="gone" />
  69 +
  70 + <ImageView
  71 + android:id="@+id/ima_send"
  72 + android:layout_width="0dp"
  73 + android:layout_height="wrap_content"
  74 + android:layout_gravity="center_vertical"
  75 + android:layout_weight="0.1"
  76 + android:src="@drawable/pic"
  77 + android:visibility="visible" />
  78 +
  79 + <Button
  80 + android:id="@+id/send_messageOne"
  81 + android:layout_width="0dp"
  82 + android:layout_height="wrap_content"
  83 + android:layout_gravity="center"
  84 + android:layout_weight="0.2"
  85 + android:text="发送"
  86 + android:visibility="gone" />
  87 +
  88 + </LinearLayout>
  89 +
  90 + <LinearLayout
  91 + android:id="@+id/buttons"
  92 + android:layout_width="match_parent"
  93 + android:layout_height="wrap_content"
  94 + android:layout_above="@id/edit_layout"
  95 + android:orientation="vertical">
  96 +
  97 + <LinearLayout
  98 + android:layout_width="match_parent"
  99 + android:layout_height="wrap_content"
  100 + android:orientation="horizontal">
  101 +
  102 + <Button
  103 + android:id="@+id/creatChat"
  104 + android:layout_width="wrap_content"
  105 + android:layout_height="wrap_content"
  106 + android:text="加入聊天室" />
  107 +
  108 + <!--<Button-->
  109 + <!--android:id="@+id/ima_send"-->
  110 + <!--android:layout_width="wrap_content"-->
  111 + <!--android:layout_height="wrap_content"-->
  112 + <!--android:text="图片" />-->
  113 +
  114 + <Button
  115 + android:id="@+id/exit_chat1"
  116 + android:layout_width="wrap_content"
  117 + android:layout_height="wrap_content"
  118 + android:text="退出" />
  119 +
  120 + <Button
  121 + android:id="@+id/videoBtn"
  122 + android:layout_width="wrap_content"
  123 + android:layout_height="wrap_content"
  124 + android:text="视频文件"/>
  125 +
  126 + <Button
  127 + android:id="@+id/ugcVideoBtn"
  128 + android:layout_width="wrap_content"
  129 + android:layout_height="wrap_content"
  130 + android:text="短视频"/>
  131 +
  132 + </LinearLayout>
  133 +
  134 + </LinearLayout>
  135 + </RelativeLayout>
  136 +
  137 +</RelativeLayout>
app/src/main/res/layout/activity_private_chat.xml
@@ -7,14 +7,15 @@ @@ -7,14 +7,15 @@
7 7
8 <LinearLayout 8 <LinearLayout
9 android:layout_width="match_parent" 9 android:layout_width="match_parent"
10 - android:layout_height="wrap_content"  
11 - android:orientation="vertical"> 10 + android:layout_height="match_parent"
  11 + android:orientation="vertical"
  12 + android:layout_above="@+id/edit_layout">
12 13
13 14
14 <android.support.v7.widget.RecyclerView 15 <android.support.v7.widget.RecyclerView
15 android:id="@+id/private_list_view" 16 android:id="@+id/private_list_view"
16 android:layout_width="match_parent" 17 android:layout_width="match_parent"
17 - android:layout_height="300dp" 18 + android:layout_height="match_parent"
18 android:divider="@null" 19 android:divider="@null"
19 android:scrollbars="none" 20 android:scrollbars="none"
20 android:transcriptMode="alwaysScroll" 21 android:transcriptMode="alwaysScroll"
@@ -36,18 +37,6 @@ @@ -36,18 +37,6 @@
36 android:layout_height="wrap_content" 37 android:layout_height="wrap_content"
37 android:text="邀请对方聊天" /> 38 android:text="邀请对方聊天" />
38 39
39 - <Button  
40 - android:id="@+id/videoBtn"  
41 - android:layout_width="wrap_content"  
42 - android:layout_height="wrap_content"  
43 - android:text="视频文件"/>  
44 -  
45 - <Button  
46 - android:id="@+id/ugcVideoBtn"  
47 - android:layout_width="wrap_content"  
48 - android:layout_height="wrap_content"  
49 - android:text="短视频"/>  
50 -  
51 40
52 <LinearLayout 41 <LinearLayout
53 android:id="@+id/edit_layout_se" 42 android:id="@+id/edit_layout_se"
app/src/main/res/layout/activity_private_chat_3.xml 0 → 100644
  1 +<?xml version="1.0" encoding="utf-8"?>
  2 +<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3 + android:id="@+id/activity_private_chat"
  4 + android:layout_width="match_parent"
  5 + android:layout_height="match_parent"
  6 + android:clickable="true">
  7 +
  8 + <LinearLayout
  9 + android:layout_width="match_parent"
  10 + android:layout_height="match_parent"
  11 + android:orientation="vertical"
  12 + android:layout_above="@+id/edit_layout">
  13 +
  14 +
  15 + <android.support.v7.widget.RecyclerView
  16 + android:id="@+id/private_list_view"
  17 + android:layout_width="match_parent"
  18 + android:layout_height="match_parent"
  19 + android:divider="@null"
  20 + android:scrollbars="none"
  21 + android:transcriptMode="alwaysScroll"
  22 + android:visibility="invisible" />
  23 + </LinearLayout>
  24 +
  25 + <LinearLayout
  26 + android:id="@+id/edit_layout"
  27 + android:layout_width="match_parent"
  28 + android:layout_height="wrap_content"
  29 + android:layout_alignParentBottom="true"
  30 + android:orientation="vertical"
  31 + android:visibility="invisible">
  32 +
  33 +
  34 + <Button
  35 + android:id="@+id/vistor_Btn"
  36 + android:layout_width="wrap_content"
  37 + android:layout_height="wrap_content"
  38 + android:text="邀请对方聊天" />
  39 +
  40 + <Button
  41 + android:id="@+id/videoBtn"
  42 + android:layout_width="wrap_content"
  43 + android:layout_height="wrap_content"
  44 + android:text="视频文件"/>
  45 +
  46 + <Button
  47 + android:id="@+id/ugcVideoBtn"
  48 + android:layout_width="wrap_content"
  49 + android:layout_height="wrap_content"
  50 + android:text="短视频"/>
  51 +
  52 +
  53 + <LinearLayout
  54 + android:id="@+id/edit_layout_se"
  55 + android:layout_width="match_parent"
  56 + android:layout_height="wrap_content"
  57 + android:layout_alignParentBottom="true"
  58 + android:orientation="horizontal">
  59 +
  60 + <ImageView
  61 + android:id="@+id/keybord"
  62 + android:layout_width="0dp"
  63 + android:layout_height="wrap_content"
  64 + android:layout_gravity="center_vertical"
  65 + android:layout_weight="0.1"
  66 + android:src="@drawable/voice" />
  67 +
  68 + <EditText
  69 + android:id="@+id/message_content"
  70 + android:layout_width="0dp"
  71 + android:layout_height="wrap_content"
  72 + android:layout_gravity="center"
  73 + android:layout_weight="0.6" />
  74 +
  75 + <Button
  76 + android:id="@+id/send_voice"
  77 + android:layout_width="0dp"
  78 + android:layout_height="40dp"
  79 + android:layout_margin="10dp"
  80 + android:layout_weight="0.6"
  81 + android:background="@drawable/voice_bg"
  82 + android:text="按住说话"
  83 + android:visibility="gone" />
  84 +
  85 + <ImageView
  86 + android:id="@+id/ima_send"
  87 + android:layout_width="0dp"
  88 + android:layout_height="wrap_content"
  89 + android:layout_gravity="center_vertical"
  90 + android:layout_weight="0.1"
  91 + android:src="@drawable/pic"
  92 + android:visibility="visible" />
  93 +
  94 + <Button
  95 + android:id="@+id/send_message"
  96 + android:layout_width="0dp"
  97 + android:layout_height="wrap_content"
  98 + android:layout_gravity="center"
  99 + android:layout_weight="0.2"
  100 + android:text="发送"
  101 + android:visibility="gone" />
  102 +
  103 + </LinearLayout>
  104 + </LinearLayout>
  105 +
  106 +
  107 + <LinearLayout
  108 + android:id="@+id/opposite_liner_useID"
  109 + android:layout_width="match_parent"
  110 + android:layout_height="wrap_content"
  111 + android:layout_alignParentLeft="true"
  112 + android:layout_alignParentStart="true"
  113 + android:layout_centerVertical="true"
  114 + android:orientation="vertical">
  115 +
  116 + <LinearLayout
  117 + android:layout_width="match_parent"
  118 + android:layout_height="wrap_content"
  119 + android:orientation="horizontal">
  120 +
  121 +
  122 + <TextView
  123 + android:layout_width="wrap_content"
  124 + android:layout_height="wrap_content"
  125 + android:layout_gravity="center"
  126 + android:gravity="center"
  127 + android:text="对方id :"
  128 + android:textSize="22dp"
  129 + android:textStyle="bold" />
  130 +
  131 + <EditText
  132 + android:id="@+id/opposite_id_edit"
  133 + android:layout_width="0dp"
  134 + android:layout_height="wrap_content"
  135 + android:layout_gravity="center"
  136 + android:layout_weight="1" />
  137 + </LinearLayout>
  138 +
  139 + <LinearLayout
  140 + android:layout_width="match_parent"
  141 + android:layout_height="wrap_content">
  142 +
  143 + <TextView
  144 + android:layout_width="wrap_content"
  145 + android:layout_height="wrap_content"
  146 + android:layout_gravity="center"
  147 + android:gravity="center"
  148 + android:text="name:"
  149 + android:textSize="22dp"
  150 + android:textStyle="bold" />
  151 +
  152 + <EditText
  153 + android:id="@+id/opposite_name_edit"
  154 + android:layout_width="0dp"
  155 + android:layout_height="wrap_content"
  156 + android:layout_gravity="center"
  157 + android:layout_weight="1" />
  158 +
  159 + </LinearLayout>
  160 +
  161 +
  162 + <Button
  163 + android:id="@+id/userId_btn_top"
  164 + android:layout_width="match_parent"
  165 + android:layout_height="wrap_content"
  166 + android:layout_gravity="center"
  167 + android:text="确定" />
  168 + </LinearLayout>
  169 +
  170 + <LinearLayout
  171 + android:id="@+id/agree_reject_lin"
  172 + android:layout_width="wrap_content"
  173 + android:layout_height="wrap_content"
  174 + android:layout_centerInParent="true"
  175 + android:background="@drawable/slel_back"
  176 + android:orientation="vertical"
  177 + android:visibility="invisible">
  178 +
  179 + <TextView
  180 + android:layout_width="match_parent"
  181 + android:layout_height="wrap_content"
  182 + android:gravity="center"
  183 + android:text="有人请求和您进行私聊" />
  184 +
  185 + <LinearLayout
  186 + android:layout_width="wrap_content"
  187 + android:layout_height="wrap_content">
  188 +
  189 + <Button
  190 + android:id="@+id/agree_btn"
  191 + android:layout_width="wrap_content"
  192 + android:layout_height="wrap_content"
  193 + android:background="#00000000"
  194 + android:text="同意" />
  195 +
  196 + <Button
  197 + android:id="@+id/reject_btn"
  198 + android:layout_width="wrap_content"
  199 + android:layout_height="wrap_content"
  200 + android:background="#00000000"
  201 + android:text="拒绝" />
  202 + </LinearLayout>
  203 +
  204 + </LinearLayout>
  205 +
  206 +</RelativeLayout>
app/src/main/res/layout/activity_user.xml
@@ -78,6 +78,35 @@ @@ -78,6 +78,35 @@
78 78
79 </RelativeLayout> 79 </RelativeLayout>
80 80
  81 + <RelativeLayout
  82 + android:layout_width="wrap_content"
  83 + android:layout_height="wrap_content"
  84 + android:orientation="horizontal">
  85 +
  86 + <Button
  87 + android:id="@+id/group_chat_3"
  88 + android:layout_width="wrap_content"
  89 + android:layout_height="wrap_content"
  90 + android:text="群聊3.0"
  91 + android:layout_alignParentTop="true"
  92 + android:layout_alignParentLeft="true"
  93 + android:layout_alignParentStart="true"
  94 + android:layout_marginLeft="60dp"
  95 + android:layout_marginStart="60dp" />
  96 +
  97 + <Button
  98 + android:id="@+id/private_chat_3"
  99 + android:layout_width="wrap_content"
  100 + android:layout_height="wrap_content"
  101 + android:text="私聊3.0"
  102 + android:layout_marginRight="65dp"
  103 + android:layout_marginEnd="65dp"
  104 + android:layout_alignParentTop="true"
  105 + android:layout_alignParentRight="true"
  106 + android:layout_alignParentEnd="true" />
  107 +
  108 + </RelativeLayout>
  109 +
81 110
82 </LinearLayout> 111 </LinearLayout>
83 112
app/src/main/res/layout/voice_layout_item_oppsite.xml
@@ -51,7 +51,8 @@ @@ -51,7 +51,8 @@
51 android:layout_centerVertical="true" 51 android:layout_centerVertical="true"
52 android:layout_marginLeft="2dp" 52 android:layout_marginLeft="2dp"
53 android:layout_toRightOf="@+id/voice_duration" 53 android:layout_toRightOf="@+id/voice_duration"
54 - android:background="@drawable/voice_none_read" /> 54 + android:background="@drawable/voice_none_read"
  55 + android:visibility="gone" />
55 56
56 57
57 </RelativeLayout> 58 </RelativeLayout>