Commit 0022d93c by 段启岩

app更新优化

parent 8aa07a97
......@@ -3,8 +3,10 @@ package cn.yunliyunwai.beyondclouds;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.widget.Toast;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
......@@ -65,9 +67,29 @@ public class MainActivity extends BaseActivity<MainActivityViewModel, ActivityMa
confirmDialog.setConfirmDialogListener(new ConfirmDialog.ConfirmDialogListener() {
@Override
public void onDialogPositiveClick(DialogFragment dialog) {
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
appViewModel.downloadUpdate(progressDialog);
dialog.dismiss();
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("正在下载新版本");
progressDialog.setCancelable(false);//不能手动取消下载进度对话框
progressDialog.setMax(100);
progressDialog.show();
appViewModel.getNewVersionDownloadProgress().observe(MainActivity.this, new Observer<Integer>() {
@Override
public void onChanged(Integer progress) {
Log.d("adsadsd", progress + "");
if (progress == 100) {
progressDialog.dismiss();
} else if (progress == -1) {
progressDialog.dismiss();
Toast.makeText(MainActivity.this, "更新失败!", Toast.LENGTH_SHORT).show();
} else {
progressDialog.setProgress(progress);
}
}
});
appViewModel.downloadUpdate();
}
@Override
......@@ -88,7 +110,7 @@ public class MainActivity extends BaseActivity<MainActivityViewModel, ActivityMa
@Override
protected MainActivityViewModel initViewModel() {
return new ViewModelProvider(this, factory).get(MainActivityViewModel.class);
return new ViewModelProvider(this, factory).get(MainActivityViewModel.class);
}
@Override
......
......@@ -3,9 +3,12 @@ package cn.yunliyunwai.beyondclouds.data.source.remote;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Streaming;
import retrofit2.http.Url;
public interface FileDownloadService {
@Streaming
@GET
Call<ResponseBody> downloadFile(@Url String fileUrl);
}
package cn.yunliyunwai.beyondclouds.viewmodel;
import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;
......@@ -22,6 +21,7 @@ import cn.yunliyunwai.beyondclouds.data.model.AppVersion;
import cn.yunliyunwai.beyondclouds.data.model.Result;
import cn.yunliyunwai.beyondclouds.data.source.IAppRepository;
import cn.yunliyunwai.beyondclouds.data.source.remote.FileDownloadService;
import cn.yunliyunwai.beyondclouds.util.AppExecutors;
import cn.yunliyunwai.beyondclouds.util.IntentUtils;
import cn.yunliyunwai.beyondclouds.util.RetrofitServiceGenerator;
import cn.yunliyunwai.beyondclouds.util.VersionUtils;
......@@ -39,11 +39,14 @@ public class AppViewModel extends ViewModel {
private IAppRepository appRepository;
private AppExecutors appExecutors;
/**
* 是否有新版本
*/
MutableLiveData<Boolean> hasNewVersion = new MutableLiveData<>();
private MutableLiveData<Integer> newVersionDownloadProgress = new MutableLiveData<>();
/**
* 新版本信息
......@@ -56,13 +59,18 @@ public class AppViewModel extends ViewModel {
private AppVersion currentVersion;
@Inject
public AppViewModel(Context context, IAppRepository appRepository) {
public AppViewModel(Context context, AppExecutors appExecutors, IAppRepository appRepository) {
this.mContext = context;
this.appExecutors = appExecutors;
this.appRepository = appRepository;
currentVersion = VersionUtils.getCurrentVersion(mContext);
hasNewVersion.setValue(false);
}
public LiveData<Integer> getNewVersionDownloadProgress() {
return newVersionDownloadProgress;
}
public AppVersion getNewVersion() {
return newVersion;
}
......@@ -92,48 +100,52 @@ public class AppViewModel extends ViewModel {
}
}
public void downloadUpdate(ProgressDialog progressDialog) {
public void downloadUpdate() {
if (hasNewVersion.getValue() == true) {
newVersionDownloadProgress.setValue(0);
FileDownloadService fileDownloadService = RetrofitServiceGenerator.create(FileDownloadService.class);
Call<ResponseBody> call = fileDownloadService.downloadFile(newVersion.getDownloadUrl());
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("正在下载新版本");
progressDialog.setCancelable(false);//不能手动取消下载进度对话框
progressDialog.show();
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
Log.d(TAG, "server contacted and has file");
boolean writtenToDisk = writeResponseBodyToDisk(response.body(), progressDialog);
Log.d(TAG, "file download was a success? " + writtenToDisk);
appExecutors.networkIO().execute(new Runnable() {
@Override
public void run() {
Log.d(TAG, "找到安装包");
boolean writtenToDisk = writeResponseBodyToDisk(response.body());
Log.d(TAG, "文件下载结果: " + writtenToDisk);
if (!writtenToDisk) {
newVersionDownloadProgress.postValue(-1);
}
}
});
} else {
Log.d(TAG, "server contact failed");
Log.d(TAG, "未找到安装包");
newVersionDownloadProgress.postValue(-1);
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
newVersionDownloadProgress.postValue(-1);
}
});
}
}
private boolean writeResponseBodyToDisk(ResponseBody body, ProgressDialog progressDialog) {
private boolean writeResponseBodyToDisk(ResponseBody body ) {
try {
progressDialog.setMax((int) body.contentLength());
File futureStudioIconFile = new File(mContext.getExternalFilesDir(null) + File.separator + "beyond-clouds-v" + newVersion.getVersionName() + ".apk");
InputStream inputStream = null;
OutputStream outputStream = null;
try {
byte[] fileReader = new byte[4096];
byte[] fileReader = new byte[1024];
long fileSize = body.contentLength();
long fileSizeDownloaded = 0;
float fileSize = body.contentLength();
float fileSizeDownloaded = 0;
inputStream = body.byteStream();
outputStream = new FileOutputStream(futureStudioIconFile);
......@@ -150,15 +162,18 @@ public class AppViewModel extends ViewModel {
fileSizeDownloaded += read;
Log.d(TAG, "file download: " + fileSizeDownloaded + " of " + fileSize);
progressDialog.setProgress((int) fileSizeDownloaded);
newVersionDownloadProgress.postValue((int) (100 * (fileSizeDownloaded / fileSize)));
}
outputStream.flush();
progressDialog.dismiss();
if (newVersionDownloadProgress.getValue() != 100) {
newVersionDownloadProgress.postValue((100));
}
mContext.startActivity(IntentUtils.getInstallAppIntent(mContext, futureStudioIconFile));
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
if (inputStream != null) {
......@@ -170,6 +185,7 @@ public class AppViewModel extends ViewModel {
}
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment