Skip to content

Commit

Permalink
修改文件上传进度显示和无消息对话过滤状态下上传文件策略
Browse files Browse the repository at this point in the history
  • Loading branch information
huzhenhuastudy committed Jul 16, 2019
1 parent d718d1b commit ddaad57
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 38 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,7 @@ UdeskSDKManager.getInstance().toLaunchHelperAcitivty(getApplicationContext(), Ud
10. 修改无消息对话过滤状态发送消息问题
11. 添加和修改商品消息背景,字体颜色,行数设置api
12. 修改4.x版本覆盖安装5.x 请求客服问题
13. 修改文件上传进度显示策略

### sdk v5.0.0版本更新功能: ###

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -50,6 +51,7 @@ public class FileLiveData<M> extends MutableLiveData<MergeMode> {
private String sdktoken = "";
private String appid = "";
OkHttpClient okHttpClient;
private UdeskChatActivity.MyHandler myHandler;

private Map<String, Call> concurrentHashMap = new ConcurrentHashMap();

Expand All @@ -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());
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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();
}
Expand Down Expand Up @@ -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);
}


Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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);

}

Expand All @@ -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);
}

}
});
Expand All @@ -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) {
Expand All @@ -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);

}

Expand Down
Loading

0 comments on commit ddaad57

Please sign in to comment.