引入 okdownload 下载库 下载表情. 避免网络请求卡死
This commit is contained in:
@@ -103,6 +103,9 @@ dependencies {
|
||||
api project(':nim_uikit')
|
||||
api project(':library')
|
||||
|
||||
implementation 'com.liulishuo.okdownload:okdownload:1.0.7'
|
||||
implementation 'com.liulishuo.okdownload:okhttp:1.0.7'
|
||||
|
||||
implementation 'com.tencent.liteav:LiteAVSDK_TRTC:11.4.0.13189'
|
||||
}
|
||||
repositories {
|
||||
|
@@ -7,7 +7,13 @@ import android.annotation.SuppressLint;
|
||||
import android.text.TextUtils;
|
||||
import android.util.SparseArray;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.liulishuo.okdownload.DownloadTask;
|
||||
import com.liulishuo.okdownload.core.cause.EndCause;
|
||||
import com.liulishuo.okdownload.core.listener.DownloadListener2;
|
||||
import com.netease.nim.uikit.common.util.string.StringUtil;
|
||||
import com.netease.nimlib.sdk.chatroom.ChatRoomMessageBuilder;
|
||||
import com.netease.nimlib.sdk.chatroom.model.ChatRoomMessage;
|
||||
@@ -16,9 +22,7 @@ import com.orhanobut.logger.Logger;
|
||||
import com.yizhuan.xchat_android_core.DemoCache;
|
||||
import com.yizhuan.xchat_android_core.auth.AuthModel;
|
||||
import com.yizhuan.xchat_android_core.base.BaseModel;
|
||||
import com.yizhuan.xchat_android_core.bean.ProgressInfo;
|
||||
import com.yizhuan.xchat_android_core.bean.RoomQueueInfo;
|
||||
import com.yizhuan.xchat_android_core.bean.response.RequestError;
|
||||
import com.yizhuan.xchat_android_core.gift.bean.SimpleVipInfo;
|
||||
import com.yizhuan.xchat_android_core.im.custom.bean.CustomAttachment;
|
||||
import com.yizhuan.xchat_android_core.im.custom.bean.FaceAttachment;
|
||||
@@ -35,7 +39,6 @@ import com.yizhuan.xchat_android_core.user.UserModel;
|
||||
import com.yizhuan.xchat_android_core.user.bean.UserInfo;
|
||||
import com.yizhuan.xchat_android_core.utils.LogUtils;
|
||||
import com.yizhuan.xchat_android_core.vip.UserVipInfo;
|
||||
import com.yizhuan.xchat_android_library.net.rxnet.RxNet;
|
||||
import com.yizhuan.xchat_android_library.utils.ListUtils;
|
||||
import com.yizhuan.xchat_android_library.utils.NetworkUtils;
|
||||
import com.yizhuan.xchat_android_library.utils.codec.DESUtils;
|
||||
@@ -49,9 +52,6 @@ import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
@@ -59,17 +59,10 @@ import java.util.concurrent.TimeUnit;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
import io.reactivex.Observable;
|
||||
import io.reactivex.ObservableEmitter;
|
||||
import io.reactivex.ObservableOnSubscribe;
|
||||
import io.reactivex.Observer;
|
||||
import io.reactivex.Single;
|
||||
import io.reactivex.SingleObserver;
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.disposables.Disposable;
|
||||
import io.reactivex.functions.BiConsumer;
|
||||
import io.reactivex.functions.Consumer;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
import okhttp3.ResponseBody;
|
||||
import retrofit2.http.GET;
|
||||
import retrofit2.http.Headers;
|
||||
@@ -630,105 +623,25 @@ public class DynamicFaceModel extends BaseModel implements IDynamicFaceModel {
|
||||
|
||||
String url = onlineFacesList.getZipUrl();
|
||||
|
||||
Observable.create(new ObservableOnSubscribe<ProgressInfo>() {
|
||||
DownloadTask task = new DownloadTask.Builder(url, facesRootDir)
|
||||
.setFilename("face.zip")
|
||||
.setMinIntervalMillisCallbackProcess(1000)
|
||||
.setPassIfAlreadyCompleted(true)
|
||||
.build();
|
||||
|
||||
task.enqueue(new DownloadListener2() {
|
||||
@Override
|
||||
public void subscribe(ObservableEmitter<ProgressInfo> emitter) throws Exception {
|
||||
RxNet.create(Api.class).downloadFaceResource(url).subscribe(new SingleObserver<ResponseBody>() {
|
||||
@Override
|
||||
public void onSubscribe(Disposable d) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSuccess(ResponseBody responseBody) {
|
||||
String mDownloadFilePath = facesZipPath.getAbsolutePath();
|
||||
File file = new File(mDownloadFilePath.concat(".tmp"));
|
||||
OutputStream outputStream = null;
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
if (!file.exists()) {
|
||||
if (!file.createNewFile()) {
|
||||
emitter.onError(new RequestError("can not create file !"));
|
||||
return;
|
||||
}
|
||||
}
|
||||
outputStream = new BufferedOutputStream(new FileOutputStream(file));
|
||||
inputStream = responseBody.byteStream();
|
||||
if (inputStream == null) {
|
||||
emitter.onError(new RequestError());
|
||||
return;
|
||||
}
|
||||
byte[] buffer = new byte[1024];
|
||||
long total = responseBody.contentLength();
|
||||
int count;
|
||||
long progress = 0;
|
||||
while ((count = inputStream.read(buffer)) != -1) {
|
||||
outputStream.write(buffer, 0, count);
|
||||
progress += count;
|
||||
emitter.onNext(new ProgressInfo(progress, total));
|
||||
}
|
||||
outputStream.flush();
|
||||
Logger.d("File download completed");
|
||||
file.renameTo(new File(mDownloadFilePath));
|
||||
Logger.d("File rename completed");
|
||||
emitter.onNext(new ProgressInfo(total, total));
|
||||
emitter.onComplete();
|
||||
|
||||
} catch (Exception e) {
|
||||
emitter.onError(e);
|
||||
|
||||
} finally {
|
||||
try {
|
||||
if (inputStream != null) {
|
||||
inputStream.close();
|
||||
}
|
||||
if (outputStream != null) {
|
||||
outputStream.close();
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
Logger.d("entity to bytes consumingContent error");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable e) {
|
||||
emitter.onError(e);
|
||||
}
|
||||
});
|
||||
public void taskStart(@NonNull DownloadTask task) {
|
||||
Logger.d("face.zip download start");
|
||||
}
|
||||
|
||||
}).subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Observer<ProgressInfo>() {
|
||||
@Override
|
||||
public void onSubscribe(Disposable d) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(ProgressInfo progressInfo) {
|
||||
//Logger.d("zip " + progressInfo.getProgress() + "");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable e) {
|
||||
//Logger.d("zip " + "error");
|
||||
isRequestingZip = false;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
Logger.d("zip " + "response dir: " + facesRootDir.getAbsolutePath());
|
||||
unzipFaceZipFile(onlineFacesList);
|
||||
isRequestingZip = false;
|
||||
}
|
||||
});
|
||||
|
||||
@Override
|
||||
public void taskEnd(@NonNull DownloadTask task, @NonNull EndCause cause, @Nullable Exception realCause) {
|
||||
Logger.d("face.zip download end" + "response dir: " + facesRootDir.getAbsolutePath());
|
||||
unzipFaceZipFile(onlineFacesList);
|
||||
isRequestingZip = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -792,7 +705,7 @@ public class DynamicFaceModel extends BaseModel implements IDynamicFaceModel {
|
||||
e.printStackTrace();
|
||||
}
|
||||
long endTime = System.currentTimeMillis();
|
||||
LogUtils.d("unzipFaceZipFileTime="+(endTime-startTime));
|
||||
LogUtils.d("unzipFaceZipFileTime=" + (endTime - startTime));
|
||||
// 更新对应的faceInfoList中的图片根目录
|
||||
setPicRootDirectoryIntoFaceInfo(faceListInfo);
|
||||
// 如果有dialog.可以显示出对应的数据
|
||||
|
@@ -1,161 +0,0 @@
|
||||
package com.yizhuan.xchat_android_library.utils.download;
|
||||
|
||||
import android.app.DownloadManager;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.IntentFilter;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Environment;
|
||||
|
||||
import com.google.gson.internal.LinkedTreeMap;
|
||||
import com.yizhuan.xchat_android_library.R;
|
||||
import com.yizhuan.xchat_android_library.utils.MimeType;
|
||||
import com.yizhuan.xchat_android_library.utils.ResUtil;
|
||||
import com.yizhuan.xchat_android_library.utils.StringUtils;
|
||||
import com.yizhuan.xchat_android_library.utils.cache.CacheClientFactory;
|
||||
import com.yizhuan.xchat_android_library.utils.cache.ReturnCallback;
|
||||
import com.yizhuan.xchat_android_library.utils.log.MLog;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* Created by lijun on 2015/7/20.
|
||||
*/
|
||||
public class DownloadManagerUtil {
|
||||
|
||||
public static final String DOWNLOAD_FILE_ID = "DOWNLOAD_FILE_ID";
|
||||
|
||||
private static LinkedTreeMap<String, String> downloadIdAndUrls;
|
||||
private static BroadcastReceiver downloadReceiver;
|
||||
|
||||
public static boolean existDownloadId(long id) {
|
||||
if (null != downloadIdAndUrls) {
|
||||
return downloadIdAndUrls.containsKey(String.valueOf(id));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void addDownloadId(Context context, final long id, final String url) {
|
||||
if (null == context) {
|
||||
return;
|
||||
}
|
||||
|
||||
CacheClientFactory.getPublic().get(DOWNLOAD_FILE_ID, new ReturnCallback() {
|
||||
@Override
|
||||
public void onReturn(Object data) throws Exception {
|
||||
try {
|
||||
if (data != null && data instanceof LinkedTreeMap) {
|
||||
downloadIdAndUrls = (LinkedTreeMap) data;
|
||||
} else {
|
||||
downloadIdAndUrls = new LinkedTreeMap();
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
MLog.warn("CacheClient", e.toString());
|
||||
downloadIdAndUrls = new LinkedTreeMap();
|
||||
}
|
||||
|
||||
if (null != downloadIdAndUrls) {
|
||||
downloadIdAndUrls.put(String.valueOf(id), url);
|
||||
CacheClientFactory.getPublic().put(DOWNLOAD_FILE_ID, downloadIdAndUrls);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void removeDownloadId(Context context, final long id) {
|
||||
if (null == context) {
|
||||
return;
|
||||
}
|
||||
|
||||
CacheClientFactory.getPublic().get(DOWNLOAD_FILE_ID, new ReturnCallback() {
|
||||
@Override
|
||||
public void onReturn(Object data) throws Exception {
|
||||
try {
|
||||
if (data != null && data instanceof LinkedTreeMap) {
|
||||
downloadIdAndUrls = (LinkedTreeMap) data;
|
||||
} else {
|
||||
downloadIdAndUrls = new LinkedTreeMap();
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
MLog.warn("CacheClient", e.toString());
|
||||
downloadIdAndUrls = new LinkedTreeMap();
|
||||
}
|
||||
|
||||
if (null != downloadIdAndUrls && downloadIdAndUrls.containsKey(id)) {
|
||||
downloadIdAndUrls.remove(String.valueOf(id));
|
||||
CacheClientFactory.getPublic().put(DOWNLOAD_FILE_ID, downloadIdAndUrls);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void unregisterReceiver(Context context) {
|
||||
if (null != context && null != downloadReceiver) {
|
||||
context.unregisterReceiver(downloadReceiver);
|
||||
}
|
||||
}
|
||||
|
||||
public static void registerReceiver(Context context, BroadcastReceiver receiver) {
|
||||
if (null != context && null != receiver) {
|
||||
downloadReceiver = receiver;
|
||||
|
||||
IntentFilter intentFilter = new IntentFilter();
|
||||
intentFilter.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
|
||||
intentFilter.addAction(DownloadManager.ACTION_NOTIFICATION_CLICKED);
|
||||
context.registerReceiver(receiver, intentFilter);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param url
|
||||
* @param context
|
||||
* @return
|
||||
*/
|
||||
public static long downloadApkFromUrl(String url, Context context) {
|
||||
return downloadFileFromUrl(url, context, MimeType.APK, true);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param url
|
||||
* @param context
|
||||
* @param mimeType
|
||||
* @param onlyInWifi
|
||||
* @return
|
||||
*/
|
||||
public static long downloadFileFromUrl(String url, Context context, String mimeType, boolean onlyInWifi) {
|
||||
if (StringUtils.isEmpty(url) || null == context) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
String fileName = StringUtils.substringAfterLast(url, File.separator);
|
||||
|
||||
Uri source = Uri.parse(url);
|
||||
|
||||
// Make a new request pointing to the .apk url
|
||||
DownloadManager.Request request = new DownloadManager.Request(source);
|
||||
// appears the same in Notification bar while downloading
|
||||
request.setDescription(ResUtil.getString(R.string.utils_download_downloadmanagerutil_01));
|
||||
request.setTitle(fileName);
|
||||
request.setMimeType(mimeType);
|
||||
|
||||
if (onlyInWifi) {
|
||||
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
|
||||
}
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
|
||||
// request.allowScanningByMediaScanner();
|
||||
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
|
||||
}
|
||||
// save the file in the "Downloads" folder of SDCARD
|
||||
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
|
||||
// get download service and enqueue file
|
||||
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
|
||||
return manager.enqueue(request);
|
||||
}
|
||||
}
|
@@ -1,50 +0,0 @@
|
||||
package com.yizhuan.xchat_android_library.utils.download;
|
||||
|
||||
import android.app.DownloadManager;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
import com.yizhuan.xchat_android_library.utils.MimeType;
|
||||
import com.yizhuan.xchat_android_library.utils.log.MLog;
|
||||
|
||||
/**
|
||||
* Created by lijun on 2015/7/20.
|
||||
*/
|
||||
public class DownloadReceiver extends BroadcastReceiver {
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
MLog.debug(this, "DM onReceive: %s", intent);
|
||||
|
||||
if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
|
||||
long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
|
||||
if (!DownloadManagerUtil.existDownloadId(id)) {
|
||||
return;
|
||||
}
|
||||
|
||||
DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
|
||||
if (MimeType.APK.equalsIgnoreCase(dm.getMimeTypeForDownloadedFile(id))) {
|
||||
intent = new Intent(Intent.ACTION_VIEW);
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
intent.setDataAndType(dm.getUriForDownloadedFile(id),
|
||||
dm.getMimeTypeForDownloadedFile(id));
|
||||
context.startActivity(intent);
|
||||
}
|
||||
|
||||
DownloadManagerUtil.removeDownloadId(context, id);
|
||||
} else if (intent.getAction().equals(DownloadManager.ACTION_NOTIFICATION_CLICKED)) {
|
||||
// can not receive the event ???
|
||||
|
||||
// long[] ids = intent.getLongArrayExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS);
|
||||
// DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
|
||||
// //点击通知栏取消下载
|
||||
// dm.remove(ids);
|
||||
// SingleToastUtil.showToast(context, ResUtil.getString(R.string.utils_download_downloadreceiver_01));
|
||||
//
|
||||
// for (int i = 0, size = ids.length; i < size; i++) {
|
||||
// DownloadManagerUtil.removeDownloadId(context, ids[i]);
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user