Commit d3788873 authored by jyx's avatar jyx

强升代码优化

parent 32edb132
......@@ -8,7 +8,9 @@ import android.content.Intent;
import android.content.ServiceConnection;
import android.net.Uri;
import android.os.IBinder;
import androidx.appcompat.app.AlertDialog;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.KeyEvent;
......@@ -56,6 +58,7 @@ public class VersionUpdatePresenter {
Button btnDuUpdate;
private Subscription subscription;
private UpdateService mUpdateService;
public void updateVersion(Activity activity) {
if (activity == null)
......@@ -177,64 +180,61 @@ public class VersionUpdatePresenter {
if (!forceUpgrade) {
// 非强升
btnDuUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (dialogDetails != null && dialogDetails.isShowing()) {
dialogDetails.dismiss();
AppConfig.app_updateing = false;
}
boolean url = CommonUtils.isUrl(appFileUrl);
if (url) {
updateApp(appFileUrl, granted);
} else {
ToastUtil.show(loanApplication.getApplicationContext(), "升级地址有误,请联系管理员!");
}
btnDuUpdate.setOnClickListener(v -> {
if (dialogDetails != null && dialogDetails.isShowing()) {
dialogDetails.dismiss();
AppConfig.app_updateing = false;
}
boolean url = CommonUtils.isUrl(appFileUrl);
if (url) {
updateApp(appFileUrl, granted);
} else {
ToastUtil.show(loanApplication.getApplicationContext(), "升级地址有误,请联系管理员!");
}
});
ivDilogKnow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (dialogDetails != null && dialogDetails.isShowing()) {
dialogDetails.dismiss();
}
ivDilogKnow.setOnClickListener(v -> {
if (dialogDetails != null && dialogDetails.isShowing()) {
dialogDetails.dismiss();
}
});
} else {
ivDilogKnow.setVisibility(View.GONE);
btnDuUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean url = CommonUtils.isUrl(appFileUrl);
if (url) {
updateApp(appFileUrl, granted);
//如果已经获取到sd卡存储权限则把按钮设置成不可点击
if (granted) {
btnDuUpdate.setEnabled(false);
}
btnDuUpdate.setOnClickListener(v -> {
if (TextUtils.equals(btnDuUpdate.getText(), "立即安装")) {
if (mUpdateService != null) {
mUpdateService.autoInstallApk();
} else {
ToastUtil.show(loanApplication.getApplicationContext(), "升级地址有误,请联系管理员!");
ToastUtil.show(loanApplication.getApplicationContext(), "安装失败!");
System.exit(0);
}
return;
}
boolean url = CommonUtils.isUrl(appFileUrl);
if (url) {
updateApp(appFileUrl, granted);
//如果已经获取到sd卡存储权限则把按钮设置成不可点击
if (granted) {
btnDuUpdate.setEnabled(false);
}
} else {
ToastUtil.show(loanApplication.getApplicationContext(), "升级地址有误,请联系管理员!");
}
});
dialogDetails.setCanceledOnTouchOutside(false);
dialogDetails.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return false;
dialogDetails.setOnKeyListener((dialog, keyCode, event) -> {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return false;
});
}
......@@ -254,15 +254,15 @@ public class VersionUpdatePresenter {
public void onServiceConnected(ComponentName name, IBinder service) {
// //返回一个MsgService对象
UpdateService updateService = ((UpdateService.UpdateBinder) service).getService();
mUpdateService = ((UpdateService.UpdateBinder) service).getService();
//
// //注册回调接口来接收下载进度的变化
updateService.setUpdateNotification(new UpdateService.UpdateNotification() {
mUpdateService.setUpdateNotification(new UpdateService.UpdateNotification() {
@Override
public void updateProgress(int progress) {
if (pbDuUpdating != null) {
pbDuUpdating.setProgress(progress);
if (btnDuUpdate != null && progress == 100) {
if (btnDuUpdate != null) {
btnDuUpdate.setEnabled(true);
}
}
......@@ -270,7 +270,10 @@ public class VersionUpdatePresenter {
@Override
public void finishProgress() {
if (btnDuUpdate != null) {
btnDuUpdate.setEnabled(true);
btnDuUpdate.setText("立即安装");
}
}
});
......
......@@ -12,13 +12,17 @@ import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import androidx.core.app.NotificationCompat;
import androidx.core.content.FileProvider;
import android.widget.RemoteViews;
import com.mints.goldspace.MintsApplication;
import com.mints.goldspace.R;
import com.mints.goldspace.common.Constant;
import com.mints.goldspace.ui.activitys.MainActivity;
import com.mints.goldspace.utils.ToastUtil;
import java.io.File;
import java.io.FileOutputStream;
......@@ -52,10 +56,8 @@ public class UpdateService extends Service {
// updateFile = FileUtils.createAttachFile(tempFilepath, tempFilename + ".apk");// 创建文件
updateFile = new File(this.getCacheDir().getPath() + "/" + tempFilename + ".apk");// 创建文件
//创建的file不为空的话
if (updateFile != null) {
createNotification();// 首次创建
createThread();// 线程下载
}
createNotification();// 首次创建
createThread();// 线程下载
} else {
stopService();
}
......@@ -183,25 +185,35 @@ public class UpdateService extends Service {
}
};
final Message message = new Message();
new Thread(new Runnable() {
public void run() {
try {
long downloadSize = downloadUpdateFile(handler, down_url,
updateFile.toString());
if (downloadSize > 0) {
// 下载成功
message.what = DOWN_OK;
handler.sendMessage(message);
}
} catch (Exception e) {
e.printStackTrace();
message.what = DOWN_ERROR;
new Thread(() -> {
try {
long downloadSize = downloadUpdateFile(handler, down_url,
updateFile.toString());
if (downloadSize > 0) {
// 下载成功
message.what = DOWN_OK;
handler.sendMessage(message);
}
} catch (Exception e) {
e.printStackTrace();
message.what = DOWN_ERROR;
handler.sendMessage(message);
}
}).start();
}
/**
* 安装 apk 文件
*/
public void autoInstallApk() {
if (updateFile != null) {
autoInstallApk(updateFile);
} else {
ToastUtil.show(MintsApplication.getContext(), "安装失败!");
System.exit(0);
}
}
/**
* 安装 apk 文件
*/
......@@ -210,7 +222,7 @@ public class UpdateService extends Service {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
} else {//Android7.0之后获取uri要用contentProvider
Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), Constant.MINTS_PKG_NAME+".fileprovider", file);
Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), Constant.MINTS_PKG_NAME + ".fileprovider", file);
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
......@@ -250,7 +262,7 @@ public class UpdateService extends Service {
while ((readsize = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, readsize);
downloadCount += readsize;// 时时获取下载到的大小
/**
/*
* 每次增张5%
*/
if (updateCount == 0
......@@ -262,9 +274,7 @@ public class UpdateService extends Service {
handler.sendMessage(message);
}
}
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
httpURLConnection.disconnect();
inputStream.close();
outputStream.close();
return downloadCount;
......@@ -274,12 +284,9 @@ public class UpdateService extends Service {
* 关闭service
*/
private void stopService() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
notificationManager.cancel(notification_id);
stopSelf();
}
new Handler().postDelayed(() -> {
notificationManager.cancel(notification_id);
stopSelf();
}, 3000);
}
......@@ -287,7 +294,7 @@ public class UpdateService extends Service {
* apk更新进度通知
*/
public interface UpdateNotification {
public void updateProgress(int progress);
void updateProgress(int progress);
void finishProgress();
}
......
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