Skip to content

Commit

Permalink
Refactor force-update logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
yjfnypeu committed Jan 31, 2018
1 parent 7788c5a commit acafdc7
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 58 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
// compile project(':updatepluginlib')
compile 'com.github.yjfnypeu:UpdatePlugin:2.8.0'
compile project(':updatepluginlib')
// compile 'com.github.yjfnypeu:UpdatePlugin:2.8.0'
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.jakewharton:butterknife:8.5.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private Update preHandle(Update update) {
// 当需要进行强制更新时。覆盖替换更新策略,且关闭忽略功能
if (update.isForced()) {
update.setIgnore(false);
builder.setUpdateStrategy(new ForcedUpdateStrategy());
builder.setUpdateStrategy(new ForcedUpdateStrategy(builder.getUpdateStrategy()));
}
return update;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.lzh.framework.updatepluginlib.base.InstallNotifier;
import org.lzh.framework.updatepluginlib.util.SafeDialogHandle;

import java.lang.reflect.Field;

/**
* 默认使用的下载完成后的通知创建器:创建一个弹窗提示用户已下载完成。可直接安装。
*
Expand All @@ -47,7 +49,9 @@ public Dialog create(Activity activity) {
.setPositiveButton(R.string.install_immediate, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (!update.isForced()) {
if (update.isForced()) {
preventDismissDialog(dialog);
} else {
SafeDialogHandle.safeDismissDialog((Dialog) dialog);
}
sendToInstall();
Expand Down Expand Up @@ -79,4 +83,18 @@ public void onClick(DialogInterface dialog, int which) {
return installDialog;
}

/**
* 通过反射 阻止自动关闭对话框
*/
private void preventDismissDialog(DialogInterface dialog) {
try {
Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
field.setAccessible(true);
//设置mShowing值,欺骗android系统
field.set(dialog, false);
} catch (Exception e) {
// ignore
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,44 +49,16 @@ public void install(Context context, String filename, final Update update) {
Uri uri;
if (Build.VERSION.SDK_INT >= 24) {
// Adaptive with api version 24+
uri = UpdateInstallProvider.getUriByFile(pluginFile, getAuthor(context), new Runnable() {
@Override
public void run() {
exitIfForceUpdate(update);
}
});
uri = UpdateInstallProvider.getUriByFile(pluginFile, getAuthor(context));
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
uri = Uri.fromFile(pluginFile);
exitIfForceUpdate(update);
}
intent.setDataAndType(uri, type);
context.startActivity(intent);

}

/**
* 进行判断是否是强制更新:若是则强制退出应用。
* @param update 更新实体数据
*/
protected void exitIfForceUpdate(Update update) {
if (!update.isForced()) {
return;
}
// 延迟强制更新时的退出操作。因为部分机型上安装程序读取安装包的时机有延迟。
Utils.getMainHandler().post(new Runnable() {
@Override
public void run() {
// 释放Activity资源。避免进程被杀后导致自动重启
ActivityManager.get().finishAll();
// 两种kill进程方式一起使用。双管齐下!
Process.killProcess(Process.myPid());
System.exit(0);
}
});

}

private String getAuthor(Context context) {
if (TextUtils.isEmpty(DEFAULT_AUTHOR)) {
DEFAULT_AUTHOR = "update.plugin." + context.getPackageName() + ".UpdateInstallProvider";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,30 @@
* 当为强制更新时,将会强制使用此更新策略。
*
* <p>此更新策略的表现为:<br>
* 1. 当存在更新时:直接展示有新更新时的通知<br>
* 2. 当点击进行更新时:直接展示下载进度通知<br>
* 3. 当下载完成后。直接调起安装任务。并调起安装后。杀死进程
* 当下载完成后。强制显示下载完成后的界面通知,其他的通知策略默认不变。
*
* @author haoge on 2017/9/25.
*/
public class ForcedUpdateStrategy implements UpdateStrategy {

private UpdateStrategy delegate;

public ForcedUpdateStrategy(UpdateStrategy delegate) {
this.delegate = delegate;
}

@Override
public boolean isShowUpdateDialog(Update update) {
return true;
return delegate.isShowUpdateDialog(update);
}

@Override
public boolean isAutoInstall() {
return true;
return false;
}

@Override
public boolean isShowDownloadDialog() {
return true;
return delegate.isShowDownloadDialog();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,4 @@ void registerSelf(Context context) {
this.applicationContext = context.getApplicationContext();
}

public void finishAll() {
while (!stack.isEmpty()) {
Activity pop = stack.pop();
if (!pop.isFinishing()) {
pop.finish();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
public class UpdateInstallProvider extends ContentProvider {

static Map<Uri, File> mCache = new HashMap<>();
static Map<Uri, Runnable> tasks = new HashMap<>();
Context applicationContext;

@Override
Expand Down Expand Up @@ -84,21 +83,15 @@ public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundEx
File file = mCache.get(uri);
final int fileMode = modeToMode(mode);

if (tasks.containsKey(uri)) {
tasks.get(uri).run();
tasks.remove(uri);
}

return ParcelFileDescriptor.open(file, fileMode);
}

/**
* @param file 需要被安装的文件
* @param author 标识信息
* @param notify 指定此文件被通过{@link #openFile(Uri, String)}方法打开时。触发此任务
* @return 被创建的uri
*/
public static Uri getUriByFile (File file, String author, Runnable notify) {
public static Uri getUriByFile (File file, String author) {
String path;
try {
path = file.getCanonicalPath();
Expand All @@ -108,9 +101,7 @@ public static Uri getUriByFile (File file, String author, Runnable notify) {
Uri uri = new Uri.Builder().scheme("content")
.authority(author).encodedPath(path).build();
mCache.put(uri,file);
if (notify != null) {
tasks.put(uri, notify);
}

return uri;
}

Expand Down

0 comments on commit acafdc7

Please sign in to comment.