1.WorkManager
简介
- 2.https://developer.android.com/topic/libraries/architecture/workmanager/how-to/intermediate-progress
- androidx.work:work-runtime:2.7.0
2.使用
2.1 定义Worker
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.work.Data;
import androidx.work.ListenableWorker;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
import com.alibaba.fastjson.JSON;
import com.tencent.mmkv.MMKV;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.ReadableByteChannel;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class DownloadWorker extends Worker {
private static final int MAX_BUFFER_SIZE = 4096;
public DownloadWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
// 下载进度初始化
// 观察工作器的中间进度
// https://developer.android.com/topic/libraries/architecture/workmanager/how-to/intermediate-progress
setProgressAsync(new Data.Builder().putDouble(DOWNLOAD_FILE_PROGRESS, 0).build());
}
@NonNull
@Override
public Result doWork() {
Log.e("currentThread", Thread.currentThread().getName());
String downloadUrl = getInputData().getString(DOWNLOAD_URL);
if (downloadUrl != null) {
OkHttpClient client =
new OkHttpClient.Builder()
.connectTimeout(5, TimeUnit.SECONDS)
.readTimeout(5, TimeUnit.SECONDS)
.build();
Request request = new Request.Builder().url(downloadUrl).build();
Response response;
try {
response = client.newCall(request).execute();
if (!response.isSuccessful()) {
return Result.retry();
} else {
if (response.body() != null) {
String fileName = DownloadFileHeaderParse.findFilename(response.headers(), downloadUrl, true, DOWNLOAD_DEFAULT_FILE_NAME);
String parentFilePath = getParentFile().getAbsolutePath() + File.separator + DOWNLOAD_FOLDER_NAME;
String saveFilePath = parentFilePath + File.separator + fileName;
long contentLength = response.body().contentLength();
String etag = DownloadFileHeaderParse.findFileEtag(response.headers());
String lastModified = DownloadFileHeaderParse.findFileLastModified(response.headers());
File downloadFolder = new File(parentFilePath);
if (!downloadFolder.exists() && !downloadFolder.isDirectory()) {
downloadFolder.mkdir();
}
DownloadedModel downloadedModel = doCheckSameFileWork(contentLength, etag, lastModified);
if (downloadedModel != null && downloadedModel.isSameFile()) {
String jsonObjectString = JSON.toJSONString(downloadedModel);
Data output = new Data.Builder()
.putString(DOWNLOAD_MODEL_JSON_STRING, jsonObjectString)
.build();
return ListenableWorker.Result.success(output);
} else {
Data output =
saveFile(downloadUrl, response, parentFilePath, saveFilePath, contentLength, etag, lastModified);
return ListenableWorker.Result.success(output);
}
} else {
Data output =
new Data.Builder()
.putString(DOWNLOAD_FILE_EXCEPTION, "response.body() is null")
.build();
return ListenableWorker.Result.failure(output);
}
}
} catch (Exception e) {
e.printStackTrace();
Data output = new Data.Builder().putString(DOWNLOAD_FILE_EXCEPTION, e.getMessage()).build();
return ListenableWorker.Result.failure(output);
}
} else {
return ListenableWorker.Result.success();
}
}
@NonNull
private Data saveFile(
String downloadUrl,
Response response,
String parentFilePath,
String saveFilePath,
long contentLength,
String eTag,
String lastModified)
throws Exception {
int written = 0;
InputStream inputStream = response.body().byteStream();
final File outputFile = new File(saveFilePath);
// 打开FileChannel
// https://www.programcreek.com/java-api-examples/?class=java.io.FileOutputStream&method=getChannel
RandomAccessFile randomAccessFile = new RandomAccessFile(outputFile, "rw");
FileChannel fileChannel = randomAccessFile.getChannel();
final FileLock fileLock = fileChannel.tryLock();
if (fileLock == null) {
throw new RuntimeException("Failed to acquire lock");
} else {
try {
final ReadableByteChannel in = Channels.newChannel(new BufferedInputStream(inputStream));
final ByteBuffer byteBuffer = ByteBuffer.allocate(MAX_BUFFER_SIZE);
while (in.read(byteBuffer) != -1) {
byteBuffer.flip();
written += fileChannel.write(byteBuffer);
// 下载进度
setProgressAsync(
new Data.Builder()
.putDouble(DOWNLOAD_FILE_PROGRESS, (written * 1.0 / contentLength))
.build());
byteBuffer.clear();
}
// 下载完成
setProgressAsync(new Data.Builder().putDouble(DOWNLOAD_FILE_PROGRESS, 1).build());
} finally {
fileLock.release();
fileChannel.close();
}
}
DownloadedModel downloadedModel = new DownloadedModel();
downloadedModel.setDownloadUrl(downloadUrl);
downloadedModel.setContentLength(contentLength);
downloadedModel.setLastModified(lastModified);
downloadedModel.seteTag(eTag);
downloadedModel.setParentPath(parentFilePath);
downloadedModel.setFilePath(saveFilePath);
// downloadedModel.setMd5(Md5Util.get32LittleMD5Str(etag + lastModified + contentLength));
MMKV kv = MMKV.defaultMMKV();
String jsonObjectString = JSON.toJSONString(downloadedModel);
kv.encode(DOWNLOAD_SUCCESS_CONFIG, jsonObjectString);
return new Data.Builder()
.putString(DOWNLOAD_MODEL_JSON_STRING, jsonObjectString)
.build();
}
public DownloadedModel doCheckSameFileWork(
long contentLength,
String etag,
String lastModified) {
MMKV kv = MMKV.defaultMMKV();
String downloadConfig = kv.decodeString(DOWNLOAD_SUCCESS_CONFIG);
DownloadedModel downloadedModel = JSON.parseObject(downloadConfig, DownloadedModel.class);
if (downloadConfig == null || downloadedModel == null) {
return null;
}
String oldSavePath = downloadedModel.getFilePath();
Long oldContentLength = downloadedModel.getContentLength();
String oldETag = downloadedModel.geteTag();
String oldLastModified = downloadedModel.getLastModified();
if (!TextUtils.isEmpty(oldSavePath)) {
downloadedModel.setSameFile(
contentLength == oldContentLength
&& !TextUtils.isEmpty(oldETag)
&& !TextUtils.isEmpty(etag)
&& oldETag.equals(etag)
&& !TextUtils.isEmpty(oldLastModified)
&& !TextUtils.isEmpty(lastModified)
&& oldLastModified.equals(lastModified));
return downloadedModel;
} else {
return null;
}
}
}
2.2 注册监听
- 2.2.1 输入Data
- 2.2.2 Constraints 约束
- 2.2.3 WorkRequest
- 2.2.4 执行
- 2.2.5 监听
- 2.2.6 取消任务
public void downloadingFile(String downUrl, @NonNull LifecycleOwner owner, boolean needUnZip) {
Data data = new Data.Builder()
.putString(DOWNLOAD_URL, downUrl)
.build();
Constraints constraints =
new Constraints.Builder()
//.setRequiredNetworkType(NetworkType.CONNECTED)
.build();
OneTimeWorkRequest downloadWorker =
new OneTimeWorkRequest.Builder(DownloadWorker.class)
.setConstraints(constraints)
/*.setBackoffCriteria(
BackoffPolicy.LINEAR, OneTimeWorkRequest.MIN_BACKOFF_MILLIS, TimeUnit.MILLISECONDS)*/
//.setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
.setInputData(data)
.build();
// https://proandroiddev.com/step-by-step-guide-to-download-files-with-workmanager-b0231b03efd1
WorkManager.getInstance(ContextProvider.get().getContext())
.enqueueUniqueWork(DOWNLOAD_FILE_UNIQUE_TAG, ExistingWorkPolicy.KEEP, downloadWorker);
WorkManager.getInstance(ContextProvider.get().getContext())
.getWorkInfoByIdLiveData(downloadWorker.getId())
.observe(
owner,
workInfo -> {
Log.e("downloadWorker", workInfo.toString());
switch (workInfo.getState()) {
case RUNNING: {
Data progress = workInfo.getProgress();
double value = progress.getDouble(DOWNLOAD_FILE_PROGRESS, 0.0);
Log.e("downloadWorker", "progress:" + value);
}
break;
case SUCCEEDED: {
Data isSuccessfulData = workInfo.getOutputData();
String result = isSuccessfulData.getString(DOWNLOAD_MODEL_JSON_STRING);
DownloadedModel downloadedModel = JSON.parseObject(result, DownloadedModel.class);
String savePath = downloadedModel.getFilePath();
String parentFilePath = downloadedModel.getParentPath();
if (needUnZip) {
unZipFileWorkerDownloadingFile(downUrl, savePath, parentFilePath, owner);
}
downloadedSuccessMessage.postValue(result);
Log.e("downloadWorker", "result = " + result);
}
break;
case FAILED: {
MMKV kv = MMKV.defaultMMKV();
String unZipConfig = kv.decodeString(UNZIP_SUCCESS_CONFIG);
String downloadConfig = kv.decodeString(DOWNLOAD_SUCCESS_CONFIG);
Log.e("downloadWorker", "unZipConfig = " + unZipConfig);
Log.e("downloadWorker", "downloadConfig = " + downloadConfig);
// 文件下载失败,旧文件不存在不能进入,旧文件存在进入解压程序
Data isFailedData = workInfo.getOutputData();
downloadedFailedMessage.postValue(isFailedData.getString(DOWNLOAD_FILE_EXCEPTION));
}
break;
default:
break;
}
});
downloadId = downloadWorker.getId();
}
@Override
protected void onCleared() {
super.onCleared();
WorkManager.getInstance(ContextProvider.get().getContext()).cancelWorkById(unZipAssertId);
WorkManager.getInstance(ContextProvider.get().getContext()).cancelWorkById(downloadId);
WorkManager.getInstance(ContextProvider.get().getContext()).cancelWorkById(unZipId);
}