|
4 | 4 | import android.app.Activity; |
5 | 5 | import android.content.Intent; |
6 | 6 | import android.graphics.Bitmap; |
| 7 | +import android.graphics.BitmapFactory; |
7 | 8 | import android.media.MediaPlayer; |
8 | 9 | import android.net.Uri; |
| 10 | +import android.os.Environment; |
9 | 11 | import android.text.TextUtils; |
10 | 12 | import android.util.Log; |
11 | 13 |
|
|
21 | 23 |
|
22 | 24 | import java.io.File; |
23 | 25 | import java.io.FileNotFoundException; |
| 26 | +import java.io.IOException; |
24 | 27 | import java.lang.reflect.InvocationTargetException; |
25 | 28 | import java.lang.reflect.Method; |
26 | 29 | import java.util.ArrayList; |
|
46 | 49 | import cn.jpush.im.android.api.content.ImageContent; |
47 | 50 | import cn.jpush.im.android.api.content.LocationContent; |
48 | 51 | import cn.jpush.im.android.api.content.TextContent; |
| 52 | +import cn.jpush.im.android.api.content.VideoContent; |
49 | 53 | import cn.jpush.im.android.api.content.VoiceContent; |
50 | 54 | import cn.jpush.im.android.api.enums.ContentType; |
51 | 55 | import cn.jpush.im.android.api.event.ChatRoomMessageEvent; |
@@ -628,6 +632,57 @@ void sendVoiceMessage(JSONArray data, CallbackContext callback) { |
628 | 632 | } |
629 | 633 | } |
630 | 634 |
|
| 635 | + void sendVideoMessage(JSONArray data, CallbackContext callback) { |
| 636 | + boolean hasPermission = PermissionHelper.hasPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE); |
| 637 | + if (!hasPermission) { |
| 638 | + handleResult(ERR_CODE_PERMISSION, ERR_MSG_PERMISSION_WRITE_EXTERNAL_STORAGE, callback); |
| 639 | + return; |
| 640 | + } |
| 641 | + String videoFilePath; |
| 642 | + String videoFileName; |
| 643 | + String videoImagePath; |
| 644 | + String videoImageFormat; |
| 645 | + int videoDuration; |
| 646 | + Map<String, String> extras = null; |
| 647 | + MessageSendingOptions messageSendingOptions = null; |
| 648 | + Conversation conversation; |
| 649 | + try { |
| 650 | + JSONObject params = data.getJSONObject(0); |
| 651 | + conversation = JMessageUtils.createConversation(params); |
| 652 | + if (conversation == null) { |
| 653 | + handleResult(ERR_CODE_CONVERSATION, ERR_MSG_CONVERSATION, callback); |
| 654 | + return; |
| 655 | + } |
| 656 | + videoFilePath = params.getString("videoFilePath"); |
| 657 | + videoFileName = params.getString("videoFileName"); |
| 658 | + videoImagePath = params.getString("videoImagePath"); |
| 659 | + videoImageFormat = params.getString("videoImageFormat"); |
| 660 | + videoDuration = params.getInt("videoDuration"); |
| 661 | + if (params.has("extras")) { |
| 662 | + extras = fromJson(params.getJSONObject("extras")); |
| 663 | + } |
| 664 | + if (params.has("messageSendingOptions")) { |
| 665 | + messageSendingOptions = toMessageSendingOptions(params.getJSONObject("messageSendingOptions")); |
| 666 | + } |
| 667 | + } catch (JSONException e) { |
| 668 | + e.printStackTrace(); |
| 669 | + handleResult(ERR_CODE_PARAMETER, ERR_MSG_PARAMETER, callback); |
| 670 | + return; |
| 671 | + } |
| 672 | + try { |
| 673 | + Bitmap thumbImage = BitmapFactory.decodeFile(videoImagePath); |
| 674 | + File videoFile = new File(videoFilePath); |
| 675 | + VideoContent videoContent = new VideoContent(thumbImage, videoImageFormat, videoFile, videoFileName, videoDuration); |
| 676 | + if (extras != null) { |
| 677 | + videoContent.setExtras(extras); |
| 678 | + } |
| 679 | + sendMessage(conversation, videoContent, messageSendingOptions, callback); |
| 680 | + } catch (IOException e) { |
| 681 | + e.printStackTrace(); |
| 682 | + handleResult(ERR_CODE_FILE, ERR_MSG_FILE, callback); |
| 683 | + } |
| 684 | + } |
| 685 | + |
631 | 686 | void sendCustomMessage(JSONArray data, CallbackContext callback) { |
632 | 687 | try { |
633 | 688 | JSONObject params = data.getJSONObject(0); |
@@ -1061,6 +1116,48 @@ public void onComplete(int status, String desc, File file) { |
1061 | 1116 | }); |
1062 | 1117 | } |
1063 | 1118 |
|
| 1119 | + void downloadVideoFile(JSONArray data, final CallbackContext callback) { |
| 1120 | + final Message msg; |
| 1121 | + |
| 1122 | + try { |
| 1123 | + JSONObject params = data.getJSONObject(0); |
| 1124 | + msg = JMessageUtils.getMessage(params); |
| 1125 | + if (msg == null) { |
| 1126 | + handleResult(ERR_CODE_MESSAGE, ERR_MSG_MESSAGE, callback); |
| 1127 | + return; |
| 1128 | + } |
| 1129 | + } catch (JSONException e) { |
| 1130 | + e.printStackTrace(); |
| 1131 | + handleResult(ERR_CODE_PARAMETER, ERR_MSG_PARAMETER, callback); |
| 1132 | + return; |
| 1133 | + } |
| 1134 | + |
| 1135 | + if (msg.getContentType() != ContentType.file) { |
| 1136 | + handleResult(ERR_CODE_MESSAGE, "Message type isn't video", callback); |
| 1137 | + return; |
| 1138 | + } |
| 1139 | + |
| 1140 | + VideoContent content = (VideoContent) msg.getContent(); |
| 1141 | + content.downloadVideoFile(msg, new DownloadCompletionCallback() { |
| 1142 | + @Override |
| 1143 | + public void onComplete(int status, String desc, File file) { |
| 1144 | + if (status == 0) { |
| 1145 | + JSONObject result = new JSONObject(); |
| 1146 | + try { |
| 1147 | + result.put("messageId", msg.getId()); |
| 1148 | + result.put("filePath", file.getAbsolutePath()); |
| 1149 | + } catch (JSONException e) { |
| 1150 | + e.printStackTrace(); |
| 1151 | + } |
| 1152 | + handleResult(result, status, desc, callback); |
| 1153 | + |
| 1154 | + } else { |
| 1155 | + handleResult(status, desc, callback); |
| 1156 | + } |
| 1157 | + } |
| 1158 | + }); |
| 1159 | + } |
| 1160 | + |
1064 | 1161 | void downloadFile(JSONArray data, final CallbackContext callback) { |
1065 | 1162 | final Message msg; |
1066 | 1163 |
|
@@ -2630,46 +2727,67 @@ public void onEvent(OfflineMessageEvent event) throws JSONException { |
2630 | 2727 | final int fI = i; |
2631 | 2728 |
|
2632 | 2729 | switch (msg.getContentType()) { |
2633 | | - case image: |
2634 | | - ((ImageContent) msg.getContent()).downloadThumbnailImage(msg, new DownloadCompletionCallback() { |
2635 | | - @Override |
2636 | | - public void onComplete(int status, String desc, File file) { |
2637 | | - if (fI == fLatestMediaMessageIndex) { |
2638 | | - for (Message msg : offlineMsgList) { |
2639 | | - msgJsonArr.put(toJson(msg)); |
2640 | | - } |
2641 | | - try { |
2642 | | - json.put("messageArray", msgJsonArr); |
2643 | | - } catch (JSONException e) { |
2644 | | - e.printStackTrace(); |
2645 | | - } |
| 2730 | + case image: |
| 2731 | + ((ImageContent) msg.getContent()).downloadThumbnailImage(msg, new DownloadCompletionCallback() { |
| 2732 | + @Override |
| 2733 | + public void onComplete(int status, String desc, File file) { |
| 2734 | + if (fI == fLatestMediaMessageIndex) { |
| 2735 | + for (Message msg : offlineMsgList) { |
| 2736 | + msgJsonArr.put(toJson(msg)); |
| 2737 | + } |
| 2738 | + try { |
| 2739 | + json.put("messageArray", msgJsonArr); |
| 2740 | + } catch (JSONException e) { |
| 2741 | + e.printStackTrace(); |
| 2742 | + } |
2646 | 2743 |
|
2647 | | - JSONObject eventJson = toJson("syncOfflineMessage", json); |
2648 | | - eventSuccess(eventJson); |
2649 | | - } |
2650 | | - } |
2651 | | - }); |
2652 | | - break; |
2653 | | - case voice: |
2654 | | - ((VoiceContent) msg.getContent()).downloadVoiceFile(msg, new DownloadCompletionCallback() { |
2655 | | - @Override |
2656 | | - public void onComplete(int status, String desc, File file) { |
2657 | | - if (fI == fLatestMediaMessageIndex) { |
2658 | | - for (Message msg : offlineMsgList) { |
2659 | | - msgJsonArr.put(toJson(msg)); |
| 2744 | + JSONObject eventJson = toJson("syncOfflineMessage", json); |
| 2745 | + eventSuccess(eventJson); |
2660 | 2746 | } |
2661 | | - try { |
2662 | | - json.put("messageArray", msgJsonArr); |
2663 | | - } catch (JSONException e) { |
2664 | | - e.printStackTrace(); |
| 2747 | + } |
| 2748 | + }); |
| 2749 | + break; |
| 2750 | + case voice: |
| 2751 | + ((VoiceContent) msg.getContent()).downloadVoiceFile(msg, new DownloadCompletionCallback() { |
| 2752 | + @Override |
| 2753 | + public void onComplete(int status, String desc, File file) { |
| 2754 | + if (fI == fLatestMediaMessageIndex) { |
| 2755 | + for (Message msg : offlineMsgList) { |
| 2756 | + msgJsonArr.put(toJson(msg)); |
| 2757 | + } |
| 2758 | + try { |
| 2759 | + json.put("messageArray", msgJsonArr); |
| 2760 | + } catch (JSONException e) { |
| 2761 | + e.printStackTrace(); |
| 2762 | + } |
| 2763 | + |
| 2764 | + JSONObject eventJson = toJson("syncOfflineMessage", json); |
| 2765 | + eventSuccess(eventJson); |
2665 | 2766 | } |
| 2767 | + } |
| 2768 | + }); |
| 2769 | + break; |
| 2770 | + case video: |
| 2771 | + ((VideoContent) msg.getContent()).downloadVideoFile(msg, new DownloadCompletionCallback() { |
| 2772 | + @Override |
| 2773 | + public void onComplete(int status, String desc, File file) { |
| 2774 | + if (fI == fLatestMediaMessageIndex) { |
| 2775 | + for (Message msg : offlineMsgList) { |
| 2776 | + msgJsonArr.put(toJson(msg)); |
| 2777 | + } |
| 2778 | + try { |
| 2779 | + json.put("messageArray", msgJsonArr); |
| 2780 | + } catch (JSONException e) { |
| 2781 | + e.printStackTrace(); |
| 2782 | + } |
2666 | 2783 |
|
2667 | | - JSONObject eventJson = toJson("syncOfflineMessage", json); |
2668 | | - eventSuccess(eventJson); |
| 2784 | + JSONObject eventJson = toJson("syncOfflineMessage", json); |
| 2785 | + eventSuccess(eventJson); |
| 2786 | + } |
2669 | 2787 | } |
2670 | | - } |
2671 | | - }); |
2672 | | - default: |
| 2788 | + }); |
| 2789 | + break; |
| 2790 | + default: |
2673 | 2791 | } |
2674 | 2792 | } |
2675 | 2793 | } |
|
0 commit comments