Commit 2ee02cec859f70d06fad2209d8b41d20b23e3cde

Authored by 谷俊
1 parent 34477381

修改3.0demo

app/src/main/java/com/cnlive/im/ui/ChatRoom3Activity.java
... ... @@ -37,7 +37,6 @@ import android.widget.Toast;
37 37  
38 38 import com.cnlive.im.R;
39 39 import com.cnlive.im.adapter.ChatRoom3Adpter;
40   -import com.cnlive.im.adapter.ChatRoomAdpter;
41 40 import com.cnlive.im.adapter.base.ItemClickListener;
42 41 import com.cnlive.im.mode.ImagMessage;
43 42 import com.cnlive.im.mode.StringMessage;
... ... @@ -75,6 +74,7 @@ public class ChatRoom3Activity extends AppCompatActivity implements View.OnClick
75 74  
76 75 private ArrayList<String> msgIds = new ArrayList<>();
77 76 private String roomIdOne = "ChatRoom05";
  77 +
78 78 private EditText editText;
79 79 private Button send_messageOneBtn;
80 80 private RecyclerView listViewOne;
... ... @@ -87,7 +87,6 @@ public class ChatRoom3Activity extends AppCompatActivity implements View.OnClick
87 87 private ImageView ima_send;
88 88 private Button sendVoice;
89 89  
90   - private Context context;
91 90 private AudioRecordUtil mAudioRecordUtil;
92 91 private PopupWindowFactory mPop;
93 92 private ImageView mImageView;
... ... @@ -118,14 +117,12 @@ public class ChatRoom3Activity extends AppCompatActivity implements View.OnClick
118 117 super.onCreate(savedInstanceState);
119 118 //取消状态栏
120 119 setContentView(R.layout.activity_groupchat_3);
121   - context = this;
122 120 initUI();
123 121 mAudioRecordUtil = new AudioRecordUtil();
124 122 mAudioRecordUtil.setOnAudioStatusUpdateListener(onAudioStatusUpdateListener);
125 123 //设置监听
126 124 mHandler = new Handler();
127 125 ChatUtil.setOnConnectStatusListener(connectStatusListener);
128   - ChatUtil.setOnChatRoomActionListener(chatRoomActionListener);
129 126 ChatUtil.setOnReceiveChatroomMessageListener(receiveMessageListener);
130 127 ChatUtil.setUploadProgressListener(uploadProgressListener);
131 128 ChatUtil.setKickedOfflineListener(onKickedOfflineListener);
... ... @@ -136,12 +133,10 @@ public class ChatRoom3Activity extends AppCompatActivity implements View.OnClick
136 133 ChatUtil.login(new CNUserInfo(user_id_string, user_name_string, ""), connectListener);
137 134 else ChatUtil.logOut(connectListener);
138 135 requestPermission();
139   -
140 136 }
141 137  
142 138  
143 139 private void initUI() {
144   -
145 140 final View view = View.inflate(this, R.layout.layout_microphone, null);
146 141 mPop = new PopupWindowFactory(this, view);
147 142 mImageView = (ImageView) view.findViewById(R.id.iv_recording_icon);
... ... @@ -150,9 +145,6 @@ public class ChatRoom3Activity extends AppCompatActivity implements View.OnClick
150 145 cancelSendLayout = (LinearLayout) view.findViewById(R.id.cancel_send_layout);
151 146 ima_send = (ImageView) findViewById(R.id.ima_send);
152 147 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 148 re_chat = (RelativeLayout) findViewById(R.id.re_chat);
157 149 foundChatBtn = (Button) findViewById(R.id.creatChat);
158 150 listViewOne = (RecyclerView) findViewById(R.id.chat_list_viewOne);
... ... @@ -167,25 +159,18 @@ public class ChatRoom3Activity extends AppCompatActivity implements View.OnClick
167 159 chatListAdapterOne.setonVoiceItemClickListener(this);
168 160 listViewOne.setLayoutManager(new LinearLayoutManager(ChatRoom3Activity.this));
169 161 listViewOne.setAdapter(chatListAdapterOne);
170   -
171 162 sendVoice = (Button) findViewById(R.id.send_voice);
172 163 re_chat = (RelativeLayout) findViewById(R.id.activity_chat_room);
173 164 keybordBtn = (ImageView) findViewById(R.id.keybord);
174 165 keybordBtn.setOnClickListener(this);
175   -
176 166 findViewById(R.id.videoBtn).setOnClickListener(this);
177 167 findViewById(R.id.ugcVideoBtn).setOnClickListener(this);
178 168  
179   -
180 169 StartListener();
181   -
182   -
183 170 }
184 171  
185 172  
186   - /**
187   - * 发送消息监听
188   - */
  173 + //发送消息监听
189 174 private IChat.OnSendMessageListener sendMessageListener = new IChat.OnSendMessageListener() {
190 175 @Override
191 176 public void onAttached(CNBaseMessage o) {
... ... @@ -211,7 +196,7 @@ public class ChatRoom3Activity extends AppCompatActivity implements View.OnClick
211 196 }
212 197 };
213 198  
214   -
  199 + //接收消息监听
215 200 private IChat.OnReceiveMessageListener receiveMessageListener = new IChat.OnReceiveMessageListener() {
216 201 @Override
217 202 public void processMessage(final CNBaseMessage o) {
... ... @@ -220,6 +205,7 @@ public class ChatRoom3Activity extends AppCompatActivity implements View.OnClick
220 205 }
221 206 };
222 207  
  208 + //更新上传进度
223 209 private IChat.OnUploadProgressListener uploadProgressListener = new IChat.OnUploadProgressListener() {
224 210 @Override
225 211 public void onMessagesUpdate(CNBaseMessage message, int elemIdx, int taskId, int progress) {
... ... @@ -228,6 +214,51 @@ public class ChatRoom3Activity extends AppCompatActivity implements View.OnClick
228 214 }
229 215 };
230 216  
  217 + //被踢下线状态
  218 + private IChat.OnKickedOfflineListener onKickedOfflineListener = new IChat.OnKickedOfflineListener() {
  219 + @Override
  220 + public void disconnected(int what, final String extra) {
  221 + Log.e(TAG, what + " : " + extra);
  222 + Toast.makeText(ChatRoom3Activity.this, extra, Toast.LENGTH_SHORT).show();
  223 + }
  224 +
  225 + @Override
  226 + public void conByDevId(final int what, final String extra) {
  227 + Log.e(TAG, what + " : " + extra);
  228 + Toast.makeText(ChatRoom3Activity.this, extra, Toast.LENGTH_SHORT).show();
  229 + }
  230 + };
  231 +
  232 + // 加入聊天室监听
  233 + private IChat.OnOperationListener onOperationListener = new IChat.OnOperationListener() {
  234 + @Override
  235 + public void onSuccess(int what, final String extra) {
  236 + Log.v(TAG, +what + extra);
  237 + }
  238 +
  239 + @Override
  240 + public void onError(int what, String extra) {
  241 + Log.v(TAG, "加入聊天室" + what + extra);
  242 + }
  243 + };
  244 +
  245 + //连接监听
  246 + private IChat.OnConnectListener connectListener = new IChat.OnConnectListener() {
  247 + @Override
  248 + public void onTokenIncorrect(String s) {
  249 + Log.e(TAG, "ConnectListener TokenIncorrect");
  250 + }
  251 +
  252 + @Override
  253 + public void onSuccess(final String s) {
  254 + Log.e(TAG, "ConnectListener 连接监听" + s);
  255 + }
  256 +
  257 + @Override
  258 + public void onError(int i, final String s) {
  259 + Log.e(TAG, "ConnectListener errorCode : " + i + " : errorMessage : " + s);
  260 + }
  261 + };
231 262  
232 263 /**
233 264 * 更新消息
... ... @@ -288,205 +319,6 @@ public class ChatRoom3Activity extends AppCompatActivity implements View.OnClick
288 319 }
289 320  
290 321  
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 322 /**
491 323 * 播放语音文件
492 324 *
... ... @@ -525,13 +357,13 @@ public class ChatRoom3Activity extends AppCompatActivity implements View.OnClick
525 357 if (FileUtil.isCacheFileExist(voiceUuid)) {
526 358 File file = new File(voicePath);
527 359 if (file.length() < voiceMessage.getCnVoiceMessage().getDataSize()) {
528   - Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
  360 + Toast.makeText(this, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
529 361 return;
530 362 }
531 363 startVoice(voicePath, animationDrawable);
532 364 } else {
533 365 if (voiceMessage.isDownloading()) {
534   - Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
  366 + Toast.makeText(this, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
535 367 } else {
536 368 voiceMessage.setDownloading(true);
537 369 voiceMessage.getCnVoiceMessage().getVoice(voicePath, new IChat.OnDownloadListener() {
... ... @@ -563,7 +395,7 @@ public class ChatRoom3Activity extends AppCompatActivity implements View.OnClick
563 395 if (FileUtil.isCacheFileExist(videoUuid)) {
564 396 File file = new File(videoPath);
565 397 if (file.length() < videoMessage.getCnVideoMessage().getVideo().getSize()) {
566   - Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
  398 + Toast.makeText(this, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
567 399 return;
568 400 }
569 401 Intent intent = new Intent(ChatRoom3Activity.this, VideoActivity.class);
... ... @@ -571,7 +403,7 @@ public class ChatRoom3Activity extends AppCompatActivity implements View.OnClick
571 403 startActivity(intent);
572 404 } else {
573 405 if (videoMessage.isDownloading()) {
574   - Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
  406 + Toast.makeText(this, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
575 407 } else {
576 408 videoMessage.setDownloading(true);
577 409 videoMessage.getCnVideoMessage().getVideo().getVideo(videoPath, new IChat.OnDownloadListener() {
... ... @@ -606,7 +438,7 @@ public class ChatRoom3Activity extends AppCompatActivity implements View.OnClick
606 438 if (FileUtil.isCacheFileExist(videoUuid)) {
607 439 File file = new File(videoPath);
608 440 if (file.length() < ugcVideoMessage.getCnUgcMessage().getVideo().getSize()) {
609   - Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
  441 + Toast.makeText(this, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
610 442 return;
611 443 }
612 444 Intent intent = new Intent(ChatRoom3Activity.this, VideoActivity.class);
... ... @@ -614,7 +446,7 @@ public class ChatRoom3Activity extends AppCompatActivity implements View.OnClick
614 446 startActivity(intent);
615 447 } else {
616 448 if (ugcVideoMessage.isDownloading()) {
617   - Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
  449 + Toast.makeText(this, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
618 450 } else {
619 451 ugcVideoMessage.setDownloading(true);
620 452 ugcVideoMessage.getCnUgcMessage().getVideo().getVideo(videoPath, new IChat.OnDownloadListener() {
... ... @@ -648,7 +480,7 @@ public class ChatRoom3Activity extends AppCompatActivity implements View.OnClick
648 480 if (FileUtil.isCacheFileExist(remoteUuid)) {
649 481 File file = new File(imagePath);
650 482 if (file.length() < imagMessage.getCnImgMessage().getRemoteSize()) {
651   - Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
  483 + Toast.makeText(this, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
652 484 return;
653 485 }
654 486 Intent intent = new Intent(ChatRoom3Activity.this, ImageViewActivity.class);
... ... @@ -656,7 +488,7 @@ public class ChatRoom3Activity extends AppCompatActivity implements View.OnClick
656 488 startActivity(intent);
657 489 } else {
658 490 if (imagMessage.isDownloading()) {
659   - Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
  491 + Toast.makeText(this, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
660 492 } else {
661 493 imagMessage.setDownloading(true);
662 494 imagMessage.getCnImgMessage().getRemoteImage(imagePath, new IChat.OnDownloadListener() {
... ... @@ -671,7 +503,7 @@ public class ChatRoom3Activity extends AppCompatActivity implements View.OnClick
671 503 @Override
672 504 public void onError(int i, String s) {
673 505 imagMessage.setDownloading(false);
674   - Toast.makeText(context, "下载失败", Toast.LENGTH_SHORT).show();
  506 + Toast.makeText(ChatRoom3Activity.this, "下载失败", Toast.LENGTH_SHORT).show();
675 507 }
676 508 });
677 509 }
... ... @@ -679,6 +511,112 @@ public class ChatRoom3Activity extends AppCompatActivity implements View.OnClick
679 511 }
680 512 }
681 513  
  514 + @Override
  515 + public void onClick(View view) {
  516 + switch (view.getId()) {
  517 + //向聊天室1发送消息
  518 + case R.id.send_messageOne:
  519 + //发送聊天室消息
  520 + if (editText.getText().toString().isEmpty()) {
  521 + Toast.makeText(this, "不能发送空消息呦~", Toast.LENGTH_SHORT).show();
  522 + } else {
  523 + ChatUtil.sendMessage(IChat.CHATROOM, roomIdOne, editText.getText().toString(), sendMessageListener);
  524 + editText.setText("");
  525 + }
  526 + break;
  527 + case R.id.creatChat:
  528 + ChatUtil.joinChatRoom(roomIdOne, -1, onOperationListener);
  529 + break;
  530 + case R.id.exit_chat1:
  531 + ChatUtil.quitChatRoom(roomIdOne, onOperationListener);
  532 + break;
  533 + case R.id.ima_send:
  534 + fromPicture();
  535 + break;
  536 + case R.id.keybord:
  537 + if (!isVoiceBtnVisible) {
  538 + editText.setVisibility(View.GONE);
  539 + sendVoice.setVisibility(View.VISIBLE);
  540 + keybordBtn.setImageResource(R.drawable.keybord);
  541 + InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
  542 + imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
  543 + isVoiceBtnVisible = true;
  544 + } else {
  545 + sendVoice.setVisibility(View.GONE);
  546 + editText.setVisibility(View.VISIBLE);
  547 + keybordBtn.setImageResource(R.drawable.voice);
  548 + isVoiceBtnVisible = false;
  549 + }
  550 + break;
  551 + case R.id.videoBtn:
  552 + openSystemRecorder();
  553 + break;
  554 + case R.id.ugcVideoBtn:
  555 + openSystemRecorder();
  556 + break;
  557 + }
  558 + }
  559 +
  560 + private void openSystemRecorder() {
  561 + // 创建拍照Intent并将控制权返回给调用的程序
  562 + Intent intent = new Intent();
  563 + intent.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
  564 + intent.addCategory(Intent.CATEGORY_DEFAULT);
  565 + // 保存录像到指定的路径
  566 + File file = new File("/sdcard/video.mp4");
  567 + Uri uri = Uri.fromFile(file);
  568 + intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
  569 + // 启动图像捕获Intent
  570 + startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);
  571 + }
  572 +
  573 +
  574 + public void fromPicture() {
  575 + if (Build.VERSION.SDK_INT >= 23) {
  576 + int checkCallPhonePermission = ContextCompat.checkSelfPermission(ChatRoom3Activity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
  577 + if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {
  578 + requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PICTURE_REQUEST_CODE);
  579 + return;
  580 + } else {
  581 + toIntentPicture();
  582 + }
  583 + } else {
  584 + toIntentPicture();
  585 + }
  586 + }
  587 +
  588 +
  589 + private void toIntentPicture() {
  590 + Intent intent = new Intent();
  591 + intent.setType("image/*");
  592 + intent.setAction(Intent.ACTION_PICK);
  593 + startActivityForResult(Intent.createChooser(intent, "Complete action using"), IMG_FROM_PICTURE);
  594 + }
  595 +
  596 +
  597 + @Override
  598 + public void onActivityResult(int requestCode, int resultCode, Intent data) {
  599 + super.onActivityResult(requestCode, resultCode, data);
  600 + if (resultCode == Activity.RESULT_OK) {
  601 + switch (requestCode) {
  602 + case IMG_FROM_PICTURE:
  603 + Uri selectedImage = data.getData();
  604 + String picturePath = UploadImageUtils.getPath(this, selectedImage);
  605 + Uri uri = Uri.parse("file://" + picturePath);
  606 + ChatUtil.sendImaMessage(IChat.CHATROOM, roomIdOne, uri, uri, sendMessageListener);
  607 + break;
  608 + case CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE:
  609 + String videoPath = data.getData().getPath();
  610 + Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, MediaStore.Images.Thumbnails.FULL_SCREEN_KIND);
  611 + String imgPath = FileUtil.createFile(bitmap, System.currentTimeMillis() + "");
  612 + ChatUtil.sendVideoMessage(IChat.CHATROOM, roomIdOne, videoPath, imgPath, MediaUtil.getInstance().getDuration(videoPath), sendMessageListener);
  613 + break;
  614 + default:
  615 + break;
  616 + }
  617 + }
  618 + }
  619 +
682 620  
683 621 /**************************************************************************/
684 622  
... ... @@ -687,18 +625,18 @@ public class ChatRoom3Activity extends AppCompatActivity implements View.OnClick
687 625 */
688 626 private void requestPermission() {
689 627 //判断是否开启摄像头权限
690   - if ((ContextCompat.checkSelfPermission(context,
  628 + if ((ContextCompat.checkSelfPermission(this,
691 629 Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) &&
692   - (ContextCompat.checkSelfPermission(context,
  630 + (ContextCompat.checkSelfPermission(this,
693 631 Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED)
694   - && (ContextCompat.checkSelfPermission(context,
  632 + && (ContextCompat.checkSelfPermission(this,
695 633 Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)) {
696 634 StartListener();
697 635  
698 636 //判断是否开启语音权限
699 637 } else {
700 638 //请求获取摄像头权限
701   - ActivityCompat.requestPermissions((Activity) context,
  639 + ActivityCompat.requestPermissions(this,
702 640 new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA}, VOICE_REQUEST_CODE);
703 641 }
704 642 }
... ... @@ -710,7 +648,7 @@ public class ChatRoom3Activity extends AppCompatActivity implements View.OnClick
710 648 if ((grantResults[0] == PackageManager.PERMISSION_GRANTED) && (grantResults[1] == PackageManager.PERMISSION_GRANTED)) {
711 649 StartListener();
712 650 } else {
713   - Toast.makeText(context, "已拒绝权限!", Toast.LENGTH_SHORT).show();
  651 + Toast.makeText(this, "已拒绝权限!", Toast.LENGTH_SHORT).show();
714 652 }
715 653 }
716 654 }
... ... @@ -738,7 +676,6 @@ public class ChatRoom3Activity extends AppCompatActivity implements View.OnClick
738 676  
739 677 case MotionEvent.ACTION_DOWN:
740 678 mPop.showAtLocation(re_chat, Gravity.CENTER, 0, 0);
741   -
742 679 sendVoice.setText("松开发送");
743 680 mAudioRecordUtil.startRecord();
744 681 downX = event.getX();
... ...
app/src/main/java/com/cnlive/im/ui/Private3Activity.java
... ... @@ -38,7 +38,6 @@ import android.widget.Toast;
38 38  
39 39 import com.cnlive.im.R;
40 40 import com.cnlive.im.adapter.ChatRoom3Adpter;
41   -import com.cnlive.im.adapter.ChatRoomAdpter;
42 41 import com.cnlive.im.adapter.base.ItemClickListener;
43 42 import com.cnlive.im.mode.CNMessageModole;
44 43 import com.cnlive.im.mode.ImagMessage;
... ... @@ -88,7 +87,6 @@ public class Private3Activity extends AppCompatActivity implements View.OnClickL
88 87 };
89 88  
90 89 public static final int IMG_FROM_PICTURE = 0x1001;
91   - public static final int VOICE = 0x1002;
92 90 public static final int PICTURE_REQUEST_CODE = 0x1003;
93 91 public static final int VOICE_REQUEST_CODE = 0x1004;
94 92  
... ... @@ -96,12 +94,12 @@ public class Private3Activity extends AppCompatActivity implements View.OnClickL
96 94 public static final String STRING_MESSAGE = "string_message";
97 95 public static final String VOICE_MESSAGE = "voice_message";
98 96  
  97 + private ArrayList<String> msgIds = new ArrayList<>();
  98 +
99 99 private RecyclerView chatListView;
100 100 private EditText messageContent;
101 101 private Button sendMessageBtn;
102 102 private ChatRoom3Adpter chatListAdapter;
103   - private ArrayList<String> msgIds = new ArrayList<>();
104   - // private CNMessage cnMessage;
105 103 private LinearLayout linearLayoutUseID;
106 104 private EditText user_id_edit;
107 105 private Button userId_btn_top;
... ... @@ -115,7 +113,6 @@ public class Private3Activity extends AppCompatActivity implements View.OnClickL
115 113 private Button agree_btn;
116 114 private Button reject_btn;
117 115 private LinearLayout agree_reject_lin;
118   - boolean isCanSendMeaasge = false;
119 116 private Button vistor_btn;
120 117  
121 118 private ImageView keybordBtn;
... ... @@ -132,13 +129,11 @@ public class Private3Activity extends AppCompatActivity implements View.OnClickL
132 129  
133 130 private boolean isVoiceBtnVisible = false;
134 131 private int duration;
135   - private Context context;
136 132  
137 133 @Override
138 134 protected void onCreate(Bundle savedInstanceState) {
139 135 super.onCreate(savedInstanceState);
140 136 setContentView(R.layout.activity_private_chat_3);
141   - context = this;
142 137 initView();
143 138 mAudioRecordUtil = new AudioRecordUtil();
144 139 mAudioRecordUtil.setOnAudioStatusUpdateListener(onAudioStatusUpdateListener);
... ... @@ -150,19 +145,15 @@ public class Private3Activity extends AppCompatActivity implements View.OnClickL
150 145 }
151 146  
152 147 private void initView() {
153   -
154 148 final View view = View.inflate(this, R.layout.layout_microphone, null);
155 149 mPop = new PopupWindowFactory(this, view);
156 150 sendMsgLayout = (LinearLayout) view.findViewById(R.id.send_layout);
157 151 cancelSendLayout = (LinearLayout) view.findViewById(R.id.cancel_send_layout);
158 152 mTextView = (TextView) view.findViewById(R.id.tv_recording_time);
159 153 mImageView = (ImageView) view.findViewById(R.id.iv_recording_icon);
160   -
161 154 opposite_name_edit = (EditText) findViewById(R.id.opposite_name_edit);
162 155 chatListView = (RecyclerView) findViewById(R.id.private_list_view);
163   -
164 156 privateLayout = (RelativeLayout) findViewById(R.id.activity_private_chat);
165   -
166 157 messageContent = (EditText) findViewById(R.id.message_content);
167 158 messageContent.addTextChangedListener(textWatcher);
168 159 sendMessageBtn = (Button) findViewById(R.id.send_message);
... ... @@ -182,20 +173,15 @@ public class Private3Activity extends AppCompatActivity implements View.OnClickL
182 173 agree_reject_lin = (LinearLayout) findViewById(R.id.agree_reject_lin);
183 174 vistor_btn = (Button) findViewById(R.id.vistor_Btn);
184 175 vistor_btn.setOnClickListener(this);
185   -
186 176 keybordBtn = (ImageView) findViewById(R.id.keybord);
187 177 keybordBtn.setOnClickListener(this);
188   -
189 178 ima_send = (ImageView) findViewById(R.id.ima_send);
190 179 ima_send.setOnClickListener(this);
191   -
192 180 sendVoice = (Button) findViewById(R.id.send_voice);
193   -
194 181 chatListAdapter = new ChatRoom3Adpter(this, true);
195 182 chatListAdapter.setonVoiceItemClickListener(this);
196 183 chatListView.setLayoutManager(new LinearLayoutManager(Private3Activity.this));
197 184 chatListView.setAdapter(chatListAdapter);
198   -
199 185 findViewById(R.id.videoBtn).setOnClickListener(this);
200 186 findViewById(R.id.ugcVideoBtn).setOnClickListener(this);
201 187  
... ... @@ -204,13 +190,10 @@ public class Private3Activity extends AppCompatActivity implements View.OnClickL
204 190  
205 191 @Override
206 192 public void onClick(View v) {
207   -
208 193 switch (v.getId()) {
209 194 case R.id.send_message:
210 195 sendMessage(STRING_MESSAGE);
211   -
212 196 break;
213   -
214 197 case R.id.userId_btn_top:
215 198 //发送私聊消息,需要传入目标ID与发送的消息内容
216 199 if (user_id_edit.getText().toString().isEmpty()) {
... ... @@ -218,36 +201,28 @@ public class Private3Activity extends AppCompatActivity implements View.OnClickL
218 201 } else if (opposite_name_edit.getText().toString().isEmpty()) {
219 202 Toast.makeText(this, "对方name不能为空", Toast.LENGTH_SHORT).show();
220 203 } else {
221   -
222 204 oppositeID = user_id_edit.getText().toString();
223 205 linearLayoutUseID.setVisibility(View.INVISIBLE);
224 206 edit_layout.setVisibility(View.VISIBLE);
225 207 chatListView.setVisibility(View.VISIBLE);
226 208 CNUserInfo cnName = new CNUserInfo(user_id_string, user_name_string, "");
227 209 ChatUtil.login(cnName, connectListener);
228   -
229 210 }
230   -
231 211 break;
232   -
233 212 case R.id.agree_btn:
234 213 String objToJsonString = ToStringUtils.objToJsonString(new CNMessageModole("同意", "3"));
235 214 ChatUtil.sendMessage(IChat.PRIVATE, oppositeID, objToJsonString, sendMessageListener);
236 215 agree_reject_lin.setVisibility(View.GONE);
237 216 break;
238   -
239 217 case R.id.reject_btn:
240 218 String objToJsonStringDis = ToStringUtils.objToJsonString(new CNMessageModole("拒绝", "3"));//私聊
241   -
242 219 ChatUtil.sendMessage(IChat.PRIVATE, oppositeID, objToJsonStringDis, sendMessageListener);
243 220 agree_reject_lin.setVisibility(View.GONE);
244 221 break;
245 222 case R.id.vistor_Btn:
246 223 String objToJsonStringInvt = ToStringUtils.objToJsonString(new CNMessageModole("你好我们可以进行私聊吗", "3"));//私聊
247 224 ChatUtil.sendMessage(IChat.PRIVATE, oppositeID, objToJsonStringInvt, sendMessageListener);
248   -
249 225 break;
250   -
251 226 case R.id.keybord:
252 227 if (!isVoiceBtnVisible) {
253 228 messageContent.setVisibility(View.GONE);
... ... @@ -280,12 +255,10 @@ public class Private3Activity extends AppCompatActivity implements View.OnClickL
280 255 Intent intent = new Intent();
281 256 intent.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
282 257 intent.addCategory(Intent.CATEGORY_DEFAULT);
283   -
284 258 // 保存录像到指定的路径
285 259 File file = new File("/sdcard/video.mp4");
286 260 Uri uri = Uri.fromFile(file);
287 261 intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
288   -
289 262 // 启动图像捕获Intent
290 263 startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);
291 264 }
... ... @@ -294,10 +267,8 @@ public class Private3Activity extends AppCompatActivity implements View.OnClickL
294 267 private void sendMessage(String type) {
295 268 if (sendInfo == 2) {
296 269 Toast.makeText(this, "等待对方接受邀请", Toast.LENGTH_SHORT).show();
297   -
298 270 } else if (sendInfo == 4) {
299 271 Toast.makeText(this, "对方已经拒绝不能发送", Toast.LENGTH_SHORT).show();
300   -
301 272 } else if (sendInfo == 3) {
302 273 Toast.makeText(this, "还没有邀请对方呦~", Toast.LENGTH_SHORT).show();
303 274 } else if (sendInfo == 5) {
... ... @@ -315,7 +286,6 @@ public class Private3Activity extends AppCompatActivity implements View.OnClickL
315 286 fromPicture();
316 287 } else if (type.equals(VOICE_MESSAGE)) {
317 288 mPop.showAtLocation(privateLayout, Gravity.CENTER, 0, 0);
318   -
319 289 sendVoice.setText("松开发送");
320 290 mAudioRecordUtil.startRecord();
321 291 }
... ... @@ -351,18 +321,13 @@ public class Private3Activity extends AppCompatActivity implements View.OnClickL
351 321 if (resultCode == Activity.RESULT_OK) {
352 322 switch (requestCode) {
353 323 case IMG_FROM_PICTURE:
354   -// String localImagePath = "/sdcard/test.png";
355   -// Uri selectedImage = Uri.parse("file://" + localImagePath);
356 324 Uri selectedImage = data.getData();
357 325 String[] filePathColumn = {MediaStore.Images.Media.DATA};
358   -
359 326 Cursor cursor = getContentResolver().query(selectedImage,
360 327 filePathColumn, null, null, null);
361 328 cursor.moveToFirst();
362   -
363 329 int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
364 330 final String picturePath = cursor.getString(columnIndex);
365   -
366 331 Uri uri = Uri.parse("file://" + picturePath);
367 332 ChatUtil.sendImaMessage(IChat.PRIVATE, oppositeID, uri, uri, sendMessageListener);
368 333 break;
... ... @@ -372,7 +337,6 @@ public class Private3Activity extends AppCompatActivity implements View.OnClickL
372 337 String imgPath = FileUtil.createFile(bitmap, System.currentTimeMillis() + "");
373 338 ChatUtil.sendVideoMessage(IChat.PRIVATE, oppositeID, videoPath, imgPath, MediaUtil.getInstance().getDuration(videoPath), sendMessageListener);
374 339 break;
375   -
376 340 default:
377 341 break;
378 342 }
... ... @@ -381,8 +345,6 @@ public class Private3Activity extends AppCompatActivity implements View.OnClickL
381 345  
382 346 //连接监听
383 347 private IChat.OnConnectListener connectListener = new IChat.OnConnectListener() {
384   -
385   -
386 348 @Override
387 349 public void onTokenIncorrect(String s) {
388 350 Log.e(TAG, "ConnectListener TokenIncorrect");
... ... @@ -393,21 +355,16 @@ public class Private3Activity extends AppCompatActivity implements View.OnClickL
393 355 public void onSuccess(String s) {
394 356 Log.e(TAG, "ConnectListener 连接监听" + s);
395 357 Toast.makeText(Private3Activity.this, s, Toast.LENGTH_SHORT).show();
396   -
397 358 }
398 359  
399 360 @Override
400 361 public void onError(int i, String s) {
401 362 Log.e(TAG, "ConnectListener errorCode : " + i + " : errorMessage : " + s);
402 363 Toast.makeText(Private3Activity.this, s, Toast.LENGTH_SHORT).show();
403   -
404 364 }
405 365 };
406 366  
407   -
408   - /**
409   - * 发送消息监听
410   - */
  367 + //发送消息监听
411 368 private IChat.OnSendMessageListener sendMessageListener = new IChat.OnSendMessageListener() {
412 369 @Override
413 370 public void onAttached(CNBaseMessage o) {
... ... @@ -558,18 +515,17 @@ public class Private3Activity extends AppCompatActivity implements View.OnClickL
558 515 */
559 516 private void requestPermission() {
560 517 //判断是否开启摄像头权限
561   - if ((ContextCompat.checkSelfPermission(context,
  518 + if ((ContextCompat.checkSelfPermission(this,
562 519 Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) &&
563   - (ContextCompat.checkSelfPermission(context,
  520 + (ContextCompat.checkSelfPermission(this,
564 521 Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED)
565   - && (ContextCompat.checkSelfPermission(context,
  522 + && (ContextCompat.checkSelfPermission(this,
566 523 Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)) {
567 524 StartListener();
568   -
569 525 //判断是否开启语音权限
570 526 } else {
571 527 //请求获取摄像头权限
572   - ActivityCompat.requestPermissions((Activity) context,
  528 + ActivityCompat.requestPermissions(this,
573 529 new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA}, VOICE_REQUEST_CODE);
574 530 }
575 531 }
... ... @@ -581,7 +537,7 @@ public class Private3Activity extends AppCompatActivity implements View.OnClickL
581 537 if ((grantResults[0] == PackageManager.PERMISSION_GRANTED) && (grantResults[1] == PackageManager.PERMISSION_GRANTED)) {
582 538 StartListener();
583 539 } else {
584   - Toast.makeText(context, "已拒绝权限!", Toast.LENGTH_SHORT).show();
  540 + Toast.makeText(this, "已拒绝权限!", Toast.LENGTH_SHORT).show();
585 541 }
586 542 }
587 543 }
... ... @@ -750,13 +706,13 @@ public class Private3Activity extends AppCompatActivity implements View.OnClickL
750 706 if (FileUtil.isCacheFileExist(voiceUuid)) {
751 707 File file = new File(voicePath);
752 708 if (file.length() < voiceMessage.getCnVoiceMessage().getDataSize()) {
753   - Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
  709 + Toast.makeText(this, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
754 710 return;
755 711 }
756 712 startVoice(voicePath, animationDrawable);
757 713 } else {
758 714 if (voiceMessage.isDownloading()) {
759   - Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
  715 + Toast.makeText(this, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
760 716 } else {
761 717 voiceMessage.setDownloading(true);
762 718 voiceMessage.getCnVoiceMessage().getVoice(voicePath, new IChat.OnDownloadListener() {
... ... @@ -788,7 +744,7 @@ public class Private3Activity extends AppCompatActivity implements View.OnClickL
788 744 if (FileUtil.isCacheFileExist(videoUuid)) {
789 745 File file = new File(videoPath);
790 746 if (file.length() < videoMessage.getCnVideoMessage().getVideo().getSize()) {
791   - Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
  747 + Toast.makeText(this, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
792 748 return;
793 749 }
794 750 Intent intent = new Intent(Private3Activity.this, VideoActivity.class);
... ... @@ -796,7 +752,7 @@ public class Private3Activity extends AppCompatActivity implements View.OnClickL
796 752 startActivity(intent);
797 753 } else {
798 754 if (videoMessage.isDownloading()) {
799   - Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
  755 + Toast.makeText(this, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
800 756 } else {
801 757 videoMessage.setDownloading(true);
802 758 videoMessage.getCnVideoMessage().getVideo().getVideo(videoPath, new IChat.OnDownloadListener() {
... ... @@ -831,7 +787,7 @@ public class Private3Activity extends AppCompatActivity implements View.OnClickL
831 787 if (FileUtil.isCacheFileExist(videoUuid)) {
832 788 File file = new File(videoPath);
833 789 if (file.length() < ugcVideoMessage.getCnUgcMessage().getVideo().getSize()) {
834   - Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
  790 + Toast.makeText(this, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
835 791 return;
836 792 }
837 793 Intent intent = new Intent(Private3Activity.this, VideoActivity.class);
... ... @@ -839,7 +795,7 @@ public class Private3Activity extends AppCompatActivity implements View.OnClickL
839 795 startActivity(intent);
840 796 } else {
841 797 if (ugcVideoMessage.isDownloading()) {
842   - Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
  798 + Toast.makeText(this, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
843 799 } else {
844 800 ugcVideoMessage.setDownloading(true);
845 801 ugcVideoMessage.getCnUgcMessage().getVideo().getVideo(videoPath, new IChat.OnDownloadListener() {
... ... @@ -873,7 +829,7 @@ public class Private3Activity extends AppCompatActivity implements View.OnClickL
873 829 if (FileUtil.isCacheFileExist(remoteUuid)) {
874 830 File file = new File(imagePath);
875 831 if (file.length() < imagMessage.getCnImgMessage().getRemoteSize()) {
876   - Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
  832 + Toast.makeText(this, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
877 833 return;
878 834 }
879 835 Intent intent = new Intent(Private3Activity.this, ImageViewActivity.class);
... ... @@ -881,7 +837,7 @@ public class Private3Activity extends AppCompatActivity implements View.OnClickL
881 837 startActivity(intent);
882 838 } else {
883 839 if (imagMessage.isDownloading()) {
884   - Toast.makeText(context, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
  840 + Toast.makeText(this, "请稍等,正在下载...", Toast.LENGTH_SHORT).show();
885 841 } else {
886 842 imagMessage.setDownloading(true);
887 843 imagMessage.getCnImgMessage().getRemoteImage(imagePath, new IChat.OnDownloadListener() {
... ... @@ -896,7 +852,7 @@ public class Private3Activity extends AppCompatActivity implements View.OnClickL
896 852 @Override
897 853 public void onError(int i, String s) {
898 854 imagMessage.setDownloading(false);
899   - Toast.makeText(context, "下载失败", Toast.LENGTH_SHORT).show();
  855 + Toast.makeText(Private3Activity.this, "下载失败", Toast.LENGTH_SHORT).show();
900 856 }
901 857 });
902 858 }
... ...