From ddaad571336ff2a5881c6a042e2524db26d3ac5a Mon Sep 17 00:00:00 2001 From: hu <799305676@qq.com> Date: Tue, 16 Jul 2019 17:11:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=96=87=E4=BB=B6=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E8=BF=9B=E5=BA=A6=E6=98=BE=E7=A4=BA=E5=92=8C=E6=97=A0?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E5=AF=B9=E8=AF=9D=E8=BF=87=E6=BB=A4=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E4=B8=8B=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E7=AD=96?= =?UTF-8?q?=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + .../main/java/cn/udesk/aac/UdeskViewMode.java | 7 +- .../cn/udesk/aac/livedata/FileLiveData.java | 50 ++++++++----- .../cn/udesk/activity/UdeskChatActivity.java | 73 ++++++++++++++----- udesksdk/udeskNewDemo/udeskNewDemo.iml | 39 +++++++++- udesksdk/udeskvideo/udeskvideo.iml | 21 +++++- 6 files changed, 153 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 33635b09..664ffb97 100644 --- a/README.md +++ b/README.md @@ -1039,6 +1039,7 @@ UdeskSDKManager.getInstance().toLaunchHelperAcitivty(getApplicationContext(), Ud 10. 修改无消息对话过滤状态发送消息问题 11. 添加和修改商品消息背景,字体颜色,行数设置api 12. 修改4.x版本覆盖安装5.x 请求客服问题 +13. 修改文件上传进度显示策略 ### sdk v5.0.0版本更新功能: ### diff --git a/udesksdk/UdeskSDKUI/src/main/java/cn/udesk/aac/UdeskViewMode.java b/udesksdk/UdeskSDKUI/src/main/java/cn/udesk/aac/UdeskViewMode.java index 62c1528a..a99634b5 100644 --- a/udesksdk/UdeskSDKUI/src/main/java/cn/udesk/aac/UdeskViewMode.java +++ b/udesksdk/UdeskSDKUI/src/main/java/cn/udesk/aac/UdeskViewMode.java @@ -21,9 +21,10 @@ import cn.udesk.aac.livedata.APILiveData; import cn.udesk.aac.livedata.DBLiveData; import cn.udesk.aac.livedata.FileLiveData; +import cn.udesk.aac.livedata.ReceiveLivaData; import cn.udesk.aac.livedata.RobotApiData; import cn.udesk.aac.livedata.SendMessageLiveData; -import cn.udesk.aac.livedata.ReceiveLivaData; +import cn.udesk.activity.UdeskChatActivity; import cn.udesk.db.UdeskDBManager; import cn.udesk.model.UdeskCommodityItem; import udesk.core.UdeskConst; @@ -127,7 +128,9 @@ public void setBaseValue(String domain, String secretKey, String sdktoken, upLoadFileLiveData.setBaseValue(domain, secretKey, sdktoken, appid); robotApiData.setBaseValue(domain, secretKey, sdktoken, appid); } - + public void setHandler(UdeskChatActivity.MyHandler handler){ + upLoadFileLiveData.setHandler(handler); + } public void setCustomerId(String customerId) { apiLiveData.setCustomerId(customerId); sendMessageLiveData.setCustomerId(customerId); diff --git a/udesksdk/UdeskSDKUI/src/main/java/cn/udesk/aac/livedata/FileLiveData.java b/udesksdk/UdeskSDKUI/src/main/java/cn/udesk/aac/livedata/FileLiveData.java index b6e5aa7b..6e84c1df 100644 --- a/udesksdk/UdeskSDKUI/src/main/java/cn/udesk/aac/livedata/FileLiveData.java +++ b/udesksdk/UdeskSDKUI/src/main/java/cn/udesk/aac/livedata/FileLiveData.java @@ -1,9 +1,9 @@ package cn.udesk.aac.livedata; -import android.arch.lifecycle.LiveData; import android.arch.lifecycle.MutableLiveData; import android.content.Context; import android.graphics.Bitmap; +import android.os.Message; import android.support.annotation.NonNull; import android.text.TextUtils; import android.util.Log; @@ -22,6 +22,7 @@ import cn.udesk.UdeskUtil; import cn.udesk.aac.MergeMode; import cn.udesk.aac.MergeModeManager; +import cn.udesk.activity.UdeskChatActivity; import cn.udesk.db.UdeskDBManager; import okhttp3.Call; import okhttp3.MediaType; @@ -50,6 +51,7 @@ public class FileLiveData extends MutableLiveData { private String sdktoken = ""; private String appid = ""; OkHttpClient okHttpClient; + private UdeskChatActivity.MyHandler myHandler; private Map concurrentHashMap = new ConcurrentHashMap(); @@ -61,7 +63,9 @@ public void setBaseValue(String domain, String secretKey, String sdktoken, this.sdktoken = sdktoken; this.appid = appid; } - + public void setHandler(UdeskChatActivity.MyHandler handler){ + myHandler=handler; + } public void cancleUploadFile(MessageInfo message) { try { concurrentHashMap.remove(message.getMsgId()); @@ -123,7 +127,7 @@ public void onFail(String message) { } - private void minioUpload(final String url, final String fields, final String filePath, final MessageInfo messageInfo,String referer){ + private void minioUpload(final String url, final String fields, final String filePath, final MessageInfo messageInfo, String referer){ if (okHttpClient == null) { okHttpClient = new OkHttpClient.Builder().addInterceptor (new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)).build(); @@ -189,7 +193,7 @@ public void onResponse(Call call, Response response) throws IOException { private void aliUpload(final String url, final String filePath, final String accessid, final String bucket, final String policy, final String signature, final String dir, - final String expire, final String fileName, final MessageInfo messageInfo,String referer) { + final String expire, final String fileName, final MessageInfo messageInfo, String referer) { try{ if (okHttpClient == null) { okHttpClient = new OkHttpClient.Builder().addInterceptor @@ -244,7 +248,7 @@ public void onResponse(Call call, Response response){ } private void qiNiuUpload(final String url, final String filePath, final String token, final String bucket, - final MessageInfo messageInfo, final String fileName,String referer) { + final MessageInfo messageInfo, final String fileName, String referer) { try{ if (okHttpClient == null) { okHttpClient = new OkHttpClient(); @@ -306,7 +310,7 @@ public void uploadFailure(MessageInfo messageInfo) { } @NonNull - private Call getCall(String url, MessageInfo messageInfo, MultipartBody.Builder builder,String referer) { + private Call getCall(String url, MessageInfo messageInfo, MultipartBody.Builder builder, String referer) { RequestBody requestBody = builder.build(); Request request = new Request.Builder() .url(url) @@ -319,9 +323,10 @@ private Call getCall(String url, MessageInfo messageInfo, MultipartBody.Builder } @NonNull - private void addCustomRequestBody (MultipartBody.Builder builder, File file ,final MessageInfo messageInfo, String fileName) { + private void addCustomRequestBody (MultipartBody.Builder builder, File file , final MessageInfo messageInfo, String fileName) { try { builder.addFormDataPart("file", URLEncoder.encode(fileName,"UTF-8"), createCustomRequestBody(file, new ProgressListener() { + int lastProgress = 0; @Override public void onProgress(long totalBytes, long remainingBytes, boolean done) { try { @@ -330,8 +335,15 @@ public void onProgress(long totalBytes, long remainingBytes, boolean done) { } float percent = (totalBytes - remainingBytes) * 100 / totalBytes; int progress = Float.valueOf(percent).intValue(); - messageInfo.setPrecent(progress); - fileProgress(messageInfo); + if (progress != lastProgress ){ + lastProgress=progress; + messageInfo.setPrecent(progress); + if (myHandler!=null){ + Message message = myHandler.obtainMessage(UdeskConst.LiveDataType.UpLoadFileLiveData_progress); + message.obj = messageInfo; + myHandler.sendMessage(message); + } + } } catch (Exception e) { e.printStackTrace(); } @@ -380,7 +392,7 @@ interface ProgressListener { public void fileProgress(MessageInfo info) { MergeMode mergeMode = new MergeMode(UdeskConst.LiveDataType.UpLoadFileLiveData_progress, info,UUID.randomUUID().toString()); - MergeModeManager.getmInstance().putMergeMode(mergeMode,FileLiveData.this); + MergeModeManager.getmInstance().putMergeMode(mergeMode, FileLiveData.this); } @@ -404,7 +416,7 @@ public void downVideo(final MessageInfo info, Context context) { final File file = new File(UdeskUtils.getDirectoryPath(context, UdeskConst.FileVideo), UdeskUtils.getFileName(info.getMsgContent(), UdeskConst.FileVideo)); - UdeskHttpFacade.getInstance().downloadFile(file.getAbsolutePath(), info.getMsgContent(),UdeskConst.REFERER_VALUE, new UdeskHttpCallBack() { + UdeskHttpFacade.getInstance().downloadFile(file.getAbsolutePath(), info.getMsgContent(), UdeskConst.REFERER_VALUE, new UdeskHttpCallBack() { }); } catch (Exception e) { @@ -417,7 +429,7 @@ public void downFile(final MessageInfo info, Context context) { try { final File file = new File(UdeskUtils.getDirectoryPath(context, UdeskConst.File_File), UdeskUtils.getFileName(info.getMsgContent(), UdeskConst.File_File)); - UdeskHttpFacade.getInstance().downloadFile(file.getAbsolutePath(), info.getMsgContent(),UdeskConst.REFERER_VALUE, new UdeskHttpCallBack() { + UdeskHttpFacade.getInstance().downloadFile(file.getAbsolutePath(), info.getMsgContent(), UdeskConst.REFERER_VALUE, new UdeskHttpCallBack() { @Override @@ -432,7 +444,7 @@ public void onSuccess(byte[] t) { @Override public void onFailure(int errorNo, String strMsg) { MergeMode mergeMode = new MergeMode(UdeskConst.LiveDataType.DownFileError, info.getMsgId(),UUID.randomUUID().toString()); - MergeModeManager.getmInstance().putMergeMode(mergeMode,FileLiveData.this); + MergeModeManager.getmInstance().putMergeMode(mergeMode, FileLiveData.this); } @@ -441,7 +453,11 @@ public void onLoading(long count, long current) { double percent = current / (double) count; int progress = Double.valueOf(percent * 100).intValue(); info.setPrecent(progress); - fileProgress(info); + if (myHandler!=null){ + Message message = myHandler.obtainMessage(UdeskConst.LiveDataType.UpLoadFileLiveData_progress); + message.obj = info; + myHandler.sendMessage(message); + } } }); @@ -462,7 +478,7 @@ public void run() { if (bitmap != null) { UdeskUtils.saveBitmap(context, info.getMsgContent(), bitmap); MergeMode mergeMode = new MergeMode(UdeskConst.LiveDataType.ChangeVideoThumbnail, info.getMsgId(),UUID.randomUUID().toString()); - MergeModeManager.getmInstance().putMergeMode(mergeMode,FileLiveData.this); + MergeModeManager.getmInstance().putMergeMode(mergeMode, FileLiveData.this); } } catch (Exception e) { @@ -475,14 +491,14 @@ public void run() { private void addMessage(MessageInfo msg) { UdeskUtils.printStackTrace(); MergeMode mergeMode = new MergeMode(UdeskConst.LiveDataType.AddMessage, msg,UUID.randomUUID().toString()); - MergeModeManager.getmInstance().putMergeMode(mergeMode,FileLiveData.this); + MergeModeManager.getmInstance().putMergeMode(mergeMode, FileLiveData.this); } private void updateFailure(String msgId) { UdeskUtils.printStackTrace(); MergeMode mergeMode = new MergeMode(UdeskConst.LiveDataType.Send_Message_Failure, msgId,UUID.randomUUID().toString()); - MergeModeManager.getmInstance().putMergeMode(mergeMode,FileLiveData.this); + MergeModeManager.getmInstance().putMergeMode(mergeMode, FileLiveData.this); } diff --git a/udesksdk/UdeskSDKUI/src/main/java/cn/udesk/activity/UdeskChatActivity.java b/udesksdk/UdeskSDKUI/src/main/java/cn/udesk/activity/UdeskChatActivity.java index d32ada29..5e4994d2 100644 --- a/udesksdk/UdeskSDKUI/src/main/java/cn/udesk/activity/UdeskChatActivity.java +++ b/udesksdk/UdeskSDKUI/src/main/java/cn/udesk/activity/UdeskChatActivity.java @@ -19,6 +19,7 @@ import android.os.Bundle; import android.os.Handler; import android.os.IBinder; +import android.os.Message; import android.provider.MediaStore; import android.support.annotation.NonNull; import android.support.annotation.Nullable; @@ -50,6 +51,7 @@ import org.json.JSONObject; import java.io.File; +import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -167,7 +169,7 @@ public class UdeskChatActivity extends UdeskBaseActivity implements IEmotionSele private final int CAPTURE_IMAGE_SMALLVIDEO_ACTIVITY_REQUEST_CODE = 107; private final int IM_GROUP_REQUEST_CODE = 108; - private Handler mHandler; + private MyHandler mHandler; private BroadcastReceiver mConnectivityChangedReceiver = null; private boolean isSurvyOperate = false;//如果是收到客服的满意度调查,则在onresume 处不在请求分配客服 // public boolean isInitComplete = false; //标识进入请求分配客服的流程是否结束 @@ -222,6 +224,33 @@ public class UdeskChatActivity extends UdeskBaseActivity implements IEmotionSele private boolean isExit=true; private RelativeLayout commodityRoot; + public static class MyHandler extends Handler { + WeakReference mWeakActivity; + public MyHandler(UdeskChatActivity activity) { + mWeakActivity = new WeakReference<>(activity); + } + + @Override + public void handleMessage(Message msg) { + try { + UdeskChatActivity udeskChatActivity = mWeakActivity.get(); + if (udeskChatActivity == null){ + return; + } + switch (msg.what){ + case UdeskConst.LiveDataType.UpLoadFileLiveData_progress: + MessageInfo progressMsg = (MessageInfo) msg.obj; + udeskChatActivity.changeFileProgress(progressMsg.getMsgId(), progressMsg.getPrecent(), 0, true); + break; + + default: + break; + } + }catch (Exception e){ + e.printStackTrace(); + } + } + } class ConnectivtyChangedReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { @@ -260,7 +289,7 @@ public void onReceive(Context context, Intent intent) { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); try { - mHandler = new Handler(); + mHandler = new MyHandler(this); initUdeskViewMode(); UdeskUtils.resetTime(); UdeskUtil.setOrientation(this); @@ -341,8 +370,8 @@ private void doWithInitCustomer() { UdeskConst.REFERER_VALUE = initCustomer.getUploadService().getReferer(); } curentStatus = initCustomer.getStatus(); - if (!TextUtils.equals(curentStatus,UdeskConst.Status.chatting)){ - udeskViewMode.getApiLiveData().messages("",UdeskConst.PullMsgFrom.init); + if (!TextUtils.equals(curentStatus, UdeskConst.Status.chatting)){ + udeskViewMode.getApiLiveData().messages("", UdeskConst.PullMsgFrom.init); } Customer customer = initCustomer.getCustomer(); customerId = customer != null ? customer.getId() : ""; @@ -434,6 +463,7 @@ private void initUdeskViewMode() { UdeskSDKManager.getInstance().getSdkToken(getApplicationContext()), UdeskSDKManager.getInstance().getAppId(getApplicationContext()) ); + udeskViewMode.setHandler(mHandler); udeskViewMode.getLiveDataMerger().observeForever(new Observer() { @Override public void onChanged(@Nullable MergeMode mergeMode) { @@ -506,10 +536,10 @@ public void onChanged(@Nullable MergeMode mergeMode) { showTranferAgent(); return; } - if (TextUtils.equals(sendMessage.getMsgtype(),UdeskConst.ChatMsgTypeString.TYPE_IMAGE) - ||TextUtils.equals(sendMessage.getMsgtype(),UdeskConst.ChatMsgTypeString.TYPE_File) - ||TextUtils.equals(sendMessage.getMsgtype(),UdeskConst.ChatMsgTypeString.TYPE_VIDEO) - ||TextUtils.equals(sendMessage.getMsgtype(),UdeskConst.ChatMsgTypeString.TYPE_SHORT_VIDEO)){ + if (TextUtils.equals(sendMessage.getMsgtype(), UdeskConst.ChatMsgTypeString.TYPE_IMAGE) + ||TextUtils.equals(sendMessage.getMsgtype(), UdeskConst.ChatMsgTypeString.TYPE_File) + ||TextUtils.equals(sendMessage.getMsgtype(), UdeskConst.ChatMsgTypeString.TYPE_VIDEO) + ||TextUtils.equals(sendMessage.getMsgtype(), UdeskConst.ChatMsgTypeString.TYPE_SHORT_VIDEO)){ changeFileProgress(sendMessage.getMsgId(), 100, 0, true); } if (mAgentInfo != null && mAgentInfo.getAgentCode() == 2000) { @@ -581,7 +611,7 @@ public void onChanged(@Nullable MergeMode mergeMode) { int agent_seq_num = (int) mergeMode.getData(); if (getAgentSeqNum() != 0 && agent_seq_num > getAgentSeqNum()) { // isFirstLoad=true; - udeskViewMode.getApiLiveData().messages("",UdeskConst.PullMsgFrom.jump); + udeskViewMode.getApiLiveData().messages("", UdeskConst.PullMsgFrom.jump); } break; //消息发送失败 @@ -718,6 +748,15 @@ public void onChanged(@Nullable MergeMode mergeMode) { if (!TextUtils.isEmpty(customerId)) { fileMessage.setCustomerId(customerId); } + if (getPressionStatus()) { + JSONObject preMessage = JsonObjectUtils.buildPreSessionInfo(fileMessage.getMsgtype(), + fileMessage.getMsgContent(), fileMessage.getMsgId(), + fileMessage.getDuration(), pre_session_id, fileMessage.getFilename(), + fileMessage.getFilesize()); + udeskViewMode.getApiLiveData().getAgentInfo(pre_session_id, preMessage); + udeskViewMode.addPressionMsg(fileMessage); + return; + } mChatAdapter.addItem(fileMessage); mListView.smoothScrollToPosition(mChatAdapter.getCount()); udeskViewMode.getDbLiveData().saveMessageDB(fileMessage); @@ -941,7 +980,7 @@ public void onChanged(@Nullable MergeMode mergeMode) { messageInfoList.add(info); } } - if (TextUtils.equals(from,UdeskConst.PullMsgFrom.init)){ + if (TextUtils.equals(from, UdeskConst.PullMsgFrom.init)){ currentMode= PullEventModel; udeskViewMode.getDbLiveData().addAllMessageInfo(messageInfoList); reFreshMessages(messageInfoList); @@ -949,14 +988,14 @@ public void onChanged(@Nullable MergeMode mergeMode) { isExit = false; moreMarking = allMessageMode.getMore_marking(); } - }else if (TextUtils.equals(from,UdeskConst.PullMsgFrom.refresh)){ + }else if (TextUtils.equals(from, UdeskConst.PullMsgFrom.refresh)){ reFreshMessages(messageInfoList); moreMarking = allMessageMode.getMore_marking(); - }else if (TextUtils.equals(from,UdeskConst.PullMsgFrom.jump)){ + }else if (TextUtils.equals(from, UdeskConst.PullMsgFrom.jump)){ currentMode= PullEventModel; udeskViewMode.getDbLiveData().addAllMessageInfo(messageInfoList); reFreshMessages(messageInfoList); - }else if (TextUtils.equals(from,UdeskConst.PullMsgFrom.hasAgent)){ + }else if (TextUtils.equals(from, UdeskConst.PullMsgFrom.hasAgent)){ udeskViewMode.getDbLiveData().deleteAllMsg(); udeskViewMode.getDbLiveData().addAllMessageInfo(messageInfoList); if (TextUtils.isEmpty(moreMarking)&& isExit){ @@ -1374,7 +1413,7 @@ public void onRefresh() { mListView.setSelection(0); } else { currentMode = PullRefreshModel; - udeskViewMode.getApiLiveData().messages(moreMarking,UdeskConst.PullMsgFrom.refresh); + udeskViewMode.getApiLiveData().messages(moreMarking, UdeskConst.PullMsgFrom.refresh); } } }); @@ -2544,7 +2583,7 @@ public void dealAgentInfo(final AgentInfo agentInfo, boolean isRedirect) { if (mAgentInfo != null && isOpenVideo()) { UdeskUtil.sendVideoMessage(imSetting, mAgentInfo, getApplicationContext()); } - udeskViewMode.getApiLiveData().messages("",UdeskConst.PullMsgFrom.hasAgent); + udeskViewMode.getApiLiveData().messages("", UdeskConst.PullMsgFrom.hasAgent); break; case UdeskConst.AgentResponseCode.WaitAgent: curentStatus = UdeskConst.Status.queuing; @@ -2961,7 +3000,7 @@ private void setTitlebar(String title, String status) { try { if (mTitlebar != null) { mTitlebar.setTopTextSequence(title); - if (TextUtils.equals(curentStatus,UdeskConst.Status.pre_session)){ + if (TextUtils.equals(curentStatus, UdeskConst.Status.pre_session)){ mTitlebar.setUdeskBottomTextVis(View.GONE); }else { mTitlebar.setUdeskBottomTextVis(View.VISIBLE); @@ -3078,7 +3117,7 @@ public void showVideoThumbnail(MessageInfo info) { public void pullByJumpOrder() { try { moreMarking = ""; - udeskViewMode.getApiLiveData().messages(moreMarking,UdeskConst.PullMsgFrom.jump); + udeskViewMode.getApiLiveData().messages(moreMarking, UdeskConst.PullMsgFrom.jump); } catch (Exception e) { e.printStackTrace(); } diff --git a/udesksdk/udeskNewDemo/udeskNewDemo.iml b/udesksdk/udeskNewDemo/udeskNewDemo.iml index 9b533940..49cace0c 100644 --- a/udesksdk/udeskNewDemo/udeskNewDemo.iml +++ b/udesksdk/udeskNewDemo/udeskNewDemo.iml @@ -17,7 +17,7 @@