import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import com.tencent.cos.xml.CosXmlService;
import com.tencent.cos.xml.CosXmlServiceConfig;
import com.tencent.cos.xml.common.ClientErrorCode;
import com.tencent.cos.xml.common.Region;
import com.tencent.cos.xml.exception.CosXmlClientException;
import com.tencent.cos.xml.exception.CosXmlServiceException;
import com.tencent.cos.xml.listener.CosXmlProgressListener;
import com.tencent.cos.xml.listener.CosXmlResultListener;
import com.tencent.cos.xml.model.CosXmlRequest;
import com.tencent.cos.xml.model.CosXmlResult;
import com.tencent.cos.xml.transfer.COSXMLUploadTask;
import com.tencent.cos.xml.transfer.TransferConfig;
import com.tencent.cos.xml.transfer.TransferManager;
import com.tencent.cos.xml.transfer.TransferState;
import com.tencent.cos.xml.transfer.TransferStateListener;
import com.tencent.qcloud.core.auth.QCloudCredentialProvider;
import com.tencent.qcloud.core.auth.ShortTimeCredentialProvider;
public class ResumeHelper {
private final static String TAG = ResumeHelper.class.getSimpleName();
private Context context;
String bucket = "examplebucket-1250000000";
String region = Region.AP_Guangzhou.getRegion();
private CosXmlService cosXmlService;
private Handler mainHandler;
private boolean isTriedOnce = false;
private final int MESSAGE_RETRY = 1;
public ResumeHelper(Context context){
this.context = context;
this.mainHandler = new Handler(context.getMainLooper()){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case MESSAGE_RETRY:
if(isTriedOnce)return;
isTriedOnce = true;
Parameter parameter = (Parameter) msg.obj;
upload(parameter.srcPath, parameter.cosPath, parameter.uploadId, parameter.sliceSize);
break;
}
}
};
initCosXml();
}
private void initCosXml(){
CosXmlServiceConfig cosXmlServiceConfig = new CosXmlServiceConfig.Builder()
.setDebuggable(true)
.isHttps(true)
.setRegion(region)
.builder();
String secretId = "COS_SECRETID";
String secretKey = "COS_SECRETKEY";
QCloudCredentialProvider qCloudCredentialProvider = new ShortTimeCredentialProvider(secretId, secretKey, 6000);
cosXmlService = new CosXmlService(context, cosXmlServiceConfig, qCloudCredentialProvider);
}
public void upload(final String srcPath, final String cosPath, final String uploadId, long sliceSize){
TransferConfig transferConfig = new TransferConfig.Builder()
.setSliceSizeForUpload(sliceSize)
.build();
TransferManager transferManager = new TransferManager(cosXmlService, transferConfig);
final COSXMLUploadTask uploadTask = transferManager.upload(bucket, cosPath, srcPath, uploadId);
uploadTask.setTransferStateListener(new TransferStateListener() {
@Override
public void onStateChanged(TransferState state) {
Log.d(TAG, "upload task state: " + state.name());
}
});
uploadTask.setCosXmlProgressListener(new CosXmlProgressListener() {
@Override
public void onProgress(long complete, long target) {
Log.d(TAG, "upload task progress: " + complete + "/" + target);
}
});
uploadTask.setCosXmlResultListener(new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
COSXMLUploadTask.COSXMLUploadTaskResult uploadTaskResult = (COSXMLUploadTask.COSXMLUploadTaskResult) result;
Log.d(TAG, "upload task success: " + uploadTaskResult.printResult());
}
@Override
public void onFail(CosXmlRequest request, CosXmlClientException exception, CosXmlServiceException serviceException) {
Log.d(TAG, "upload task failed: " + (exception == null ? serviceException.getMessage() :
(exception.errorCode + "," + exception.getMessage())));
if(exception != null){
if(exception.errorCode == ClientErrorCode.IO_ERROR.getCode()
|| exception.errorCode == ClientErrorCode.POOR_NETWORK.getCode()){
Log.d(TAG, "upload task try again");
Message msg = mainHandler.obtainMessage();
msg.what = MESSAGE_RETRY;
Parameter parameter = new Parameter();
parameter.cosPath = cosPath;
parameter.srcPath = srcPath;
parameter.uploadId = uploadTask.getUploadId();
parameter.sliceSize = 100 * 1024L;
msg.obj = parameter;
mainHandler.sendMessage(msg);
}
}
}
});
}
private static class Parameter{
private String cosPath;
private String srcPath;
private String uploadId;
private long sliceSize;
}
}
Was this page helpful?