Skip to content

Commit

Permalink
Refactor force exit logic
Browse files Browse the repository at this point in the history
  • Loading branch information
yjfnypeu committed Sep 27, 2017
1 parent 57e2713 commit 3523297
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 9 deletions.
9 changes: 9 additions & 0 deletions app/app.iml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/animated-vector-drawable/25.1.0/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/appcompat-v7/25.1.0/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/support-compat/25.1.0/jars" />
Expand All @@ -89,11 +92,17 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.squareup.leakcanary/leakcanary-android/1.4-beta2/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.tbruyelle.rxpermissions/rxpermissions/0.9.2/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-safeguard" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/pre-dexed" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/shaders" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/transforms" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="jdk" jdkName="Android API 25 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,7 @@ public void setUpdate(Update update) {

public void sendToInstall(String filename) {
if (builder.getFileChecker().checkAfterDownload(update,filename)) {
builder.getInstallStrategy().install(ActivityManager.get().getApplicationContext(), filename);
if (update.isForced()) {
// 对于是强制更新的。当调起安装任务后。直接kill掉自身进程。
// 单独使用一种有时候会kill失效。两种一起用。。。双管齐下。。。
Process.killProcess(Process.myPid());
System.exit(0);
}
builder.getInstallStrategy().install(ActivityManager.get().getApplicationContext(), filename, update);
} else {
builder.getCheckCB().onCheckError(new RuntimeException(String.format("apk %s checked failed",filename)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Process;
import android.text.TextUtils;

import org.lzh.framework.updatepluginlib.model.Update;
import org.lzh.framework.updatepluginlib.util.ActivityManager;
import org.lzh.framework.updatepluginlib.util.UpdateInstallProvider;
import org.lzh.framework.updatepluginlib.util.Utils;

Expand All @@ -38,7 +41,7 @@ public final class DefaultInstallStrategy implements InstallStrategy{
private static String DEFAULT_AUTHOR = null;

@Override
public void install(Context context, String filename) {
public void install(Context context, String filename, Update update) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
Expand All @@ -57,6 +60,23 @@ public void install(Context context, String filename) {
((Activity) context).startActivityForResult(intent,REQUEST_INSTALL);
}
context.startActivity(intent);

exitIfForceUpdate(update);
}

/**
* 进行判断是否是强制更新:若是则强制退出应用。
* @param update 更新实体数据
*/
protected void exitIfForceUpdate(Update update) {
if (!update.isForced()) {
return;
}
// 释放Activity资源。避免进程被杀后导致自动重启
ActivityManager.get().finishAll();
// 两种kill进程方式一起使用。双管齐下!
Process.killProcess(Process.myPid());
System.exit(0);
}

private String getAuthor(Context context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.lzh.framework.updatepluginlib.UpdateBuilder;
import org.lzh.framework.updatepluginlib.UpdateConfig;
import org.lzh.framework.updatepluginlib.model.Update;

/**
* 提供一个安装策略。便于针对不同的应用场景。定制不同的安装策略实现。
Expand All @@ -38,6 +39,7 @@ public interface InstallStrategy {
* 3. 热修复环境下,在此进行热修复dex合并及更多兼容操作
* @param context application context
* @param filename The apk filename
* @param update 更新数据实体类
*/
void install(Context context, String filename);
void install(Context context, String filename, Update update);
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,13 @@ void registerSelf(Context context) {
application.registerActivityLifecycleCallbacks(ActivityManager.get());
this.applicationContext = context.getApplicationContext();
}

public void finishAll() {
while (!stack.isEmpty()) {
Activity pop = stack.pop();
if (!pop.isFinishing()) {
pop.finish();
}
}
}
}
6 changes: 6 additions & 0 deletions updatepluginlib/updatepluginlib.iml
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,20 @@
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/bundles" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-safeguard" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/lint" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/shaders" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/transforms" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="jdk" jdkName="Android API 25 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
Expand Down

0 comments on commit 3523297

Please sign in to comment.