Commit d3788873 authored by jyx's avatar jyx

强升代码优化

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