Commit 0022d93c by 段启岩

app更新优化

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