Skip to content

Commit

Permalink
添加下载失败重启逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
yjfnypeu committed Aug 6, 2018
1 parent 92d4296 commit 460658c
Show file tree
Hide file tree
Showing 19 changed files with 72 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;

/**
* Created by admin on 16-6-21.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import org.json.JSONObject;
import org.lzh.framework.updateplugin.widget.ToastTool;
import org.lzh.framework.updatepluginlib.UpdateConfig;
import org.lzh.framework.updatepluginlib.model.Update;
import org.lzh.framework.updatepluginlib.base.UpdateParser;
import org.lzh.framework.updatepluginlib.model.Update;

/**
* @author Administrator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import org.lzh.framework.updateplugin.widget.CheckedView;
import org.lzh.framework.updatepluginlib.UpdateBuilder;
import org.lzh.framework.updatepluginlib.UpdateConfig;
import org.lzh.framework.updatepluginlib.model.Update;
import org.lzh.framework.updatepluginlib.base.UpdateParser;
import org.lzh.framework.updatepluginlib.model.Update;

import butterknife.BindView;
import butterknife.ButterKnife;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.lzh.framework.updateplugin.update;

import org.lzh.framework.updatepluginlib.base.UpdateStrategy;
import org.lzh.framework.updatepluginlib.impl.WifiFirstStrategy;
import org.lzh.framework.updatepluginlib.model.Update;
import org.lzh.framework.updatepluginlib.base.UpdateStrategy;

/**
* 自定义强制显示所有Dialog策略,
* 默认使用参考 {@link WifiFirstStrategy}
*/
public class AllDialogShowStrategy implements UpdateStrategy {
public class AllDialogShowStrategy extends UpdateStrategy {
/** 指定是否在判断出有需要更新的版本时。弹出更新提醒弹窗
* @param update 需要更新的版本信息
* @return true 显示弹窗
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* 默认使用参考:{@link DefaultDownloadNotifier}
* </p>
*/
public class NotificationDownloadCreator implements DownloadNotifier {
public class NotificationDownloadCreator extends DownloadNotifier {
@Override
public DownloadCallback create(Update update, Activity activity) {
// 返回一个UpdateDownloadCB对象用于下载时使用来更新界面。
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.flow.Launcher;
import org.lzh.framework.updatepluginlib.impl.DefaultDownloadNotifier;
import org.lzh.framework.updatepluginlib.model.Update;
import org.lzh.framework.updatepluginlib.util.ActivityManager;
Expand All @@ -34,7 +35,20 @@
*
* @author haoge
*/
public interface DownloadNotifier {
public abstract class DownloadNotifier {

protected Update update;
protected UpdateBuilder builder;

public final DownloadNotifier bind(UpdateBuilder builder, Update update) {
this.update = update;
this.builder = builder;
return this;
}

protected final void restartDownload() {
Launcher.getInstance().launchDownload(update, builder);
}

/**
* 创建一个下载任务的下载进度回调。此回调将用于接收下载任务的状态并更新UI。
Expand All @@ -43,5 +57,7 @@ public interface DownloadNotifier {
* @param activity 顶部的Activity实例。通过{@link ActivityManager#topActivity()}进行获取
* @return 被创建的回调器。允许为null。
*/
DownloadCallback create(Update update, Activity activity);
public abstract DownloadCallback create(Update update, Activity activity);


}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

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

/**
* 提供一个安装策略。便于针对不同的应用场景。定制不同的安装策略实现。
Expand All @@ -31,7 +31,7 @@
*
* @author haoge
*/
public interface InstallStrategy {
public abstract class InstallStrategy {

/**
* 在此定制你自己的安装策略。如:
Expand All @@ -42,5 +42,5 @@ public interface InstallStrategy {
* @param filename The apk filename
* @param update 更新数据实体类
*/
void install(Context context, String filename, Update update);
public abstract void install(Context context, String filename, Update update);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
*
* @author haoge
*/
public interface UpdateChecker {
public abstract class UpdateChecker {

/**
* 对提供的更新实体类进行检查。判断是否需要进行更新。
* @param update 更新数据实体类
* @return True代表检查通过。此版本需要被更新
* @throws Exception error occurs.
*/
boolean check(Update update) throws Exception;
public abstract boolean check(Update update) throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*
* @author haoge
*/
public interface UpdateParser {
public abstract class UpdateParser {

/**
* 当更新api网络任务请求成功时。将会触发到此,在此根据网络数据解析创建出对应的更新数据实体类并返回给框架层使用。
Expand All @@ -35,5 +35,5 @@ public interface UpdateParser {
* @return 被创建的更新数据实体类。不能为null
* @throws Exception 捕获异常。防止crash并统一异常流程
*/
Update parse(String response) throws Exception;
public abstract Update parse(String response) throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

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

/**
* 此接口用于定制更新时各节点通知的显示逻辑。
Expand All @@ -37,7 +37,7 @@
*
* @author haoge
*/
public interface UpdateStrategy {
public abstract class UpdateStrategy {

/**
* 当通过{@link UpdateChecker}检测到需要更新时。是否显示界面更新通知
Expand All @@ -46,21 +46,21 @@ public interface UpdateStrategy {
* @return True代表需要显示更新。False代表不进行界面更新通知。直接调起后续流程(启动apk文件下载任务)
* @see CheckNotifier
*/
boolean isShowUpdateDialog(Update update);
public abstract boolean isShowUpdateDialog(Update update);

/**
* 在使用{@link DownloadWorker}执行文件下载任务时。是否显示界面进度条通知
*
* @return True代表需要显示下载进度通知,
* @see DownloadNotifier
*/
boolean isShowDownloadDialog();
public abstract boolean isShowDownloadDialog();

/**
* 是否在下载完成后。跳过下载完成的界面通知。直接自动启动安装任务
*
* @return True代表将跳过展示apk下载完成的通知。自动进行安装。
* @see InstallNotifier
*/
boolean isAutoInstall();
public abstract boolean isAutoInstall();
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private DownloadCallback getInnerCB() {

Activity current = ActivityManager.get().topActivity();
if (Utils.isValid(current)) {
innerCB = builder.getDownloadNotifier().create(update,current);
innerCB = builder.getDownloadNotifier().bind(builder, update).create(update,current);
}
return innerCB;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@
package org.lzh.framework.updatepluginlib.impl;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Process;

import org.lzh.framework.updatepluginlib.base.DownloadCallback;
import org.lzh.framework.updatepluginlib.base.DownloadNotifier;
import org.lzh.framework.updatepluginlib.model.Update;
import org.lzh.framework.updatepluginlib.util.ActivityManager;
import org.lzh.framework.updatepluginlib.util.SafeDialogHandle;

import java.io.File;
Expand All @@ -29,7 +34,7 @@
* 默认使用的下载进度通知创建器: 在此创建Dialog弹窗显示并根据下载回调通知进行进度条更新
* @author haoge
*/
public class DefaultDownloadNotifier implements DownloadNotifier {
public class DefaultDownloadNotifier extends DownloadNotifier {
@Override
public DownloadCallback create(Update update, Activity activity) {
final ProgressDialog dialog = new ProgressDialog(activity);
Expand Down Expand Up @@ -58,7 +63,32 @@ public void onDownloadProgress(long current, long total) {
@Override
public void onDownloadError(Throwable t) {
SafeDialogHandle.safeDismissDialog(dialog);
createRestartDialog();
}
};
}

private void createRestartDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(ActivityManager.get().topActivity())
.setCancelable(!update.isForced())
.setMessage("下载apk失败。是否重新下载?")
.setNegativeButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
restartDownload();
}
});

if (!update.isForced()) {
builder.setNeutralButton("退出", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.exit(0);
Process.killProcess(Process.myPid());
}
});
}

builder.show();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.lzh.framework.updatepluginlib.UpdateBuilder;
import org.lzh.framework.updatepluginlib.UpdateConfig;
import org.lzh.framework.updatepluginlib.base.DownloadWorker;
import org.lzh.framework.updatepluginlib.util.UpdatePreference;

import java.io.File;
import java.io.FileOutputStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*
* @author haoge
*/
public class DefaultInstallStrategy implements InstallStrategy {
public class DefaultInstallStrategy extends InstallStrategy {

private static String DEFAULT_AUTHOR = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*
* @author haoge
*/
public class DefaultUpdateChecker implements UpdateChecker {
public class DefaultUpdateChecker extends UpdateChecker {
@Override
public boolean check(Update update) throws Exception{
int curVersion = getApkVersion(ActivityManager.get().getApplicationContext());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*
* @author haoge on 2017/9/25.
*/
public class ForcedUpdateStrategy implements UpdateStrategy {
public class ForcedUpdateStrategy extends UpdateStrategy {

private UpdateStrategy delegate;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* 1. 当处于wifi环境时,只展示下载完成后的通知
* 2. 当处于非wifi环境是:只展示有新版本更新及下载进度的通知。
*/
public class WifiFirstStrategy implements UpdateStrategy {
public class WifiFirstStrategy extends UpdateStrategy {

private boolean isWifi;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package org.lzh.framework.updatepluginlib.model;

import org.lzh.framework.updatepluginlib.impl.DefaultUpdateChecker;
import org.lzh.framework.updatepluginlib.impl.DefaultInstallNotifier;
import org.lzh.framework.updatepluginlib.impl.DefaultUpdateChecker;
import org.lzh.framework.updatepluginlib.impl.DefaultUpdateNotifier;
import org.lzh.framework.updatepluginlib.impl.ForcedUpdateStrategy;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
import android.database.Cursor;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.os.Process;

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

import java.io.File;
import java.io.FileNotFoundException;
Expand Down

0 comments on commit 460658c

Please sign in to comment.