Skip to content

Commit

Permalink
为兼容flutter插件,支持直接传入UpdateEntity进行更新
Browse files Browse the repository at this point in the history
  • Loading branch information
xuexiangjys committed Feb 15, 2020
1 parent 9dc1581 commit 76e8d47
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 40 deletions.
12 changes: 12 additions & 0 deletions app/src/main/assets/update_test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Code": 0,
"Msg": "",
"UpdateStatus": 1,
"VersionCode": 3,
"VersionName": "1.0.2",
"UploadTime": "2018-07-10 17:28:41",
"ModifyContent": "\r\n1、优化api接口。\r\n2、添加使用demo演示。\r\n3、新增自定义更新服务API接口。\r\n4、优化更新提示界面。",
"DownloadUrl": "https://raw.githubusercontent.com/xuexiangjys/XUpdate/master/apk/xupdate_demo_1.0.2.apk",
"ApkSize": 2048,
"ApkMd5": "E4B79A36EFB9F17DF7E3BB161F9BCFD8"
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@
import android.view.KeyEvent;
import android.view.View;

import com.xuexiang.xaop.annotation.MemoryCache;
import com.xuexiang.xaop.annotation.Permission;
import com.xuexiang.xaop.consts.PermissionConsts;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.base.XPageSimpleListFragment;
import com.xuexiang.xpage.utils.TitleBar;
import com.xuexiang.xupdate.XUpdate;
import com.xuexiang.xupdate._XUpdate;
import com.xuexiang.xupdate.entity.UpdateEntity;
import com.xuexiang.xupdate.proxy.impl.DefaultUpdateChecker;
import com.xuexiang.xupdate.proxy.impl.DefaultUpdateParser;
import com.xuexiang.xupdate.service.OnFileDownloadListener;
import com.xuexiang.xupdatedemo.R;
import com.xuexiang.xupdatedemo.activity.UpdateActivity;
Expand All @@ -42,6 +45,7 @@
import com.xuexiang.xutil.common.ClickUtils;
import com.xuexiang.xutil.file.FileUtils;
import com.xuexiang.xutil.resource.ResUtils;
import com.xuexiang.xutil.resource.ResourceUtils;
import com.xuexiang.xutil.tip.ToastUtils;

import java.io.File;
Expand All @@ -62,7 +66,7 @@ public class MainFragment extends XPageSimpleListFragment {

private String mUpdateUrl3 = "https://gitee.com/xuexiangjys/XUpdate/raw/master/jsonapi/update_custom.json";

private String mDownloadUrl = "https://gitee.com/xuexiangjys/XUpdate/raw/master/apk/xupdate_demo_1.0.2.apk";
private String mDownloadUrl = "https://ali-fir-pro-binary.fir.im/42b952d526a8b1e778bedfb2d2786857961e3194.apk?auth_key=1581788635-0-0-f1eebcb314ef173ef068e66be9f0916b";

private final static int REQUEST_CODE_SELECT_APK_FILE = 1000;
@Override
Expand All @@ -75,6 +79,7 @@ protected List<String> initSimpleData(List<String> lists) {
lists.add("默认App更新 + 自定义提示弹窗主题");
lists.add("默认App更新 + 自定义Api");
lists.add("默认App更新 + 自定义Api + 自定义提示弹窗(系统)");
lists.add("直接传入UpdateEntity进行更新");
lists.add("使用apk下载功能");
lists.add("使用apk安装功能");
lists.add("版本更新提示框在FragmentActivity中使用UpdateDialogFragment, 在普通Activity中使用UpdateDialog");
Expand Down Expand Up @@ -145,22 +150,31 @@ public void onAfterCheck() {
.update();
break;
case 8:
useApkDownLoadFunction();
XUpdate.newBuild(getActivity())
.build()
.update(getUpdateEntityFromAssets());
break;
case 9:
selectAPKFile();
useApkDownLoadFunction();
break;
case 10:
startActivity(new Intent(getContext(), UpdateActivity.class));
selectAPKFile();
break;
case 11:
startActivity(new Intent(getContext(), UpdateActivity.class));
break;
case 12:
openPage(XUpdateServiceFragment.class);
break;
default:
break;
}
}

@MemoryCache
private UpdateEntity getUpdateEntityFromAssets() {
return new DefaultUpdateParser().parseJson(ResourceUtils.readStringFromAssert("update_test.json"));
}

@Permission(PermissionConsts.STORAGE)
private void useApkDownLoadFunction() {
Expand Down
2 changes: 1 addition & 1 deletion jsonapi/update_custom.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"versionCode": 3,
"versionName": "1.0.2",
"updateLog": "\r\n1、优化api接口。\r\n2、添加使用demo演示。\r\n3、新增自定义更新服务API接口。\r\n4、优化更新提示界面。",
"apkUrl": "https://raw.githubusercontent.com/xuexiangjys/XUpdate/master/apk/xupdate_demo_1.0.2.apk",
"apkUrl": "https://ali-fir-pro-binary.fir.im/42b952d526a8b1e778bedfb2d2786857961e3194.apk?auth_key=1581788635-0-0-f1eebcb314ef173ef068e66be9f0916b",
"apkSize": 4096
}
2 changes: 1 addition & 1 deletion jsonapi/update_forced.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"VersionName": "1.0.2",
"UploadTime": "2018-07-10 17:28:41",
"ModifyContent": "\r\n1、优化api接口。\r\n2、添加使用demo演示。\r\n3、新增自定义更新服务API接口。\r\n4、优化更新提示界面。",
"DownloadUrl": "https://raw.githubusercontent.com/xuexiangjys/XUpdate/master/apk/xupdate_demo_1.0.2.apk",
"DownloadUrl": "https://ali-fir-pro-binary.fir.im/42b952d526a8b1e778bedfb2d2786857961e3194.apk?auth_key=1581788635-0-0-f1eebcb314ef173ef068e66be9f0916b",
"ApkSize": 4096,
"ApkMd5": "E4B79A36EFB9F17DF7E3BB161F9BCFD8"
}
2 changes: 1 addition & 1 deletion jsonapi/update_test.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"VersionName": "1.0.2",
"UploadTime": "2018-07-10 17:28:41",
"ModifyContent": "\r\n1、优化api接口。\r\n2、添加使用demo演示。\r\n3、新增自定义更新服务API接口。\r\n4、优化更新提示界面。",
"DownloadUrl": "https://raw.githubusercontent.com/xuexiangjys/XUpdate/master/apk/xupdate_demo_1.0.2.apk",
"DownloadUrl": "https://ali-fir-pro-binary.fir.im/42b952d526a8b1e778bedfb2d2786857961e3194.apk?auth_key=1581788635-0-0-f1eebcb314ef173ef068e66be9f0916b",
"ApkSize": 2048,
"ApkMd5": "E4B79A36EFB9F17DF7E3BB161F9BCFD8"
}
31 changes: 23 additions & 8 deletions xupdate-lib/src/main/java/com/xuexiang/xupdate/UpdateManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,18 @@ public void backgroundDownload() {
}
}

@Override
public void cancelDownload() {
UpdateLog.d("正在取消更新文件的下载...");
if (mIUpdateProxy != null) {
mIUpdateProxy.cancelDownload();
} else {
mIUpdateDownloader.cancelDownload();
}
}

//============================对外提供的自定义使用api===============================//

/**
* 为外部提供简单的下载功能
*
Expand All @@ -393,18 +405,21 @@ public void download(String downloadUrl, @Nullable OnFileDownloadListener downlo
startDownload(refreshParams(new UpdateEntity().setDownloadUrl(downloadUrl)), downloadListener);
}

@Override
public void cancelDownload() {
UpdateLog.d("正在取消更新文件的下载...");
if (mIUpdateProxy != null) {
mIUpdateProxy.cancelDownload();
} else {
mIUpdateDownloader.cancelDownload();
/**
* 直接更新,不使用版本更新检查器
*
* @param updateEntity 版本更新信息
*/
public void update(UpdateEntity updateEntity) {
mUpdateEntity = refreshParams(updateEntity);
try {
UpdateUtils.processUpdateEntity(mUpdateEntity, "这里调用的是直接更新方法,因此没有json!", this);
} catch (Exception e) {
e.printStackTrace();
}
}



//============================构建者===============================//

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@

import java.util.Map;

import static com.xuexiang.xupdate.entity.UpdateError.ERROR.CHECK_APK_CACHE_DIR_EMPTY;
import static com.xuexiang.xupdate.entity.UpdateError.ERROR.CHECK_IGNORED_VERSION;
import static com.xuexiang.xupdate.entity.UpdateError.ERROR.CHECK_JSON_EMPTY;
import static com.xuexiang.xupdate.entity.UpdateError.ERROR.CHECK_NET_REQUEST;
import static com.xuexiang.xupdate.entity.UpdateError.ERROR.CHECK_NO_NEW_VERSION;
import static com.xuexiang.xupdate.entity.UpdateError.ERROR.CHECK_PARSE;
import static com.xuexiang.xupdate.entity.UpdateError.ERROR.CHECK_UPDATING;

Expand Down Expand Up @@ -125,38 +122,23 @@ public void processCheckResult(final @NonNull String result, final @NonNull IUpd
updateProxy.parseJson(result, new IUpdateParseCallback() {
@Override
public void onParseResult(UpdateEntity updateEntity) {
processUpdateEntity(updateEntity, result, updateProxy);
try {
UpdateUtils.processUpdateEntity(updateEntity, result, updateProxy);
} catch (Exception e) {
e.printStackTrace();
_XUpdate.onUpdateError(CHECK_PARSE, e.getMessage());
}
}
});
} else {
//同步解析
processUpdateEntity(updateProxy.parseJson(result), result, updateProxy);
UpdateUtils.processUpdateEntity(updateProxy.parseJson(result), result, updateProxy);
}

} catch (Exception e) {
e.printStackTrace();
_XUpdate.onUpdateError(CHECK_PARSE, e.getMessage());
}
}

private void processUpdateEntity(UpdateEntity updateEntity, @NonNull String result, @NonNull IUpdateProxy updateProxy) {
if (updateEntity != null) {
if (updateEntity.isHasUpdate()) {
//校验是否是已忽略版本
if (UpdateUtils.isIgnoreVersion(updateProxy.getContext(), updateEntity.getVersionName())) {
_XUpdate.onUpdateError(CHECK_IGNORED_VERSION);
//校验apk下载缓存目录是否为空
} else if (TextUtils.isEmpty(updateEntity.getApkCacheDir())) {
_XUpdate.onUpdateError(CHECK_APK_CACHE_DIR_EMPTY);
} else {
updateProxy.findNewVersion(updateEntity, updateProxy);
}
} else {
_XUpdate.onUpdateError(CHECK_NO_NEW_VERSION);
}
} else {
_XUpdate.onUpdateError(CHECK_PARSE, "json:" + result);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,16 @@
import com.xuexiang.xupdate.R;
import com.xuexiang.xupdate._XUpdate;
import com.xuexiang.xupdate.entity.UpdateEntity;
import com.xuexiang.xupdate.proxy.IUpdateProxy;

import java.io.File;
import java.util.List;

import static com.xuexiang.xupdate.entity.UpdateError.ERROR.CHECK_APK_CACHE_DIR_EMPTY;
import static com.xuexiang.xupdate.entity.UpdateError.ERROR.CHECK_IGNORED_VERSION;
import static com.xuexiang.xupdate.entity.UpdateError.ERROR.CHECK_NO_NEW_VERSION;
import static com.xuexiang.xupdate.entity.UpdateError.ERROR.CHECK_PARSE;

/**
* 更新工具类
*
Expand All @@ -60,6 +66,33 @@ private UpdateUtils() {

//=======================检查========================//

/**
* 处理解析获取到的最新版本更新信息【版本处理的核心】
*
* @param updateEntity 版本更新信息
* @param result 版本的json信息
* @param updateProxy 更新代理
*/
public static void processUpdateEntity(UpdateEntity updateEntity, @NonNull String result, @NonNull IUpdateProxy updateProxy) throws Exception {
if (updateEntity != null) {
if (updateEntity.isHasUpdate()) {
//校验是否是已忽略版本
if (UpdateUtils.isIgnoreVersion(updateProxy.getContext(), updateEntity.getVersionName())) {
_XUpdate.onUpdateError(CHECK_IGNORED_VERSION);
//校验apk下载缓存目录是否为空
} else if (TextUtils.isEmpty(updateEntity.getApkCacheDir())) {
_XUpdate.onUpdateError(CHECK_APK_CACHE_DIR_EMPTY);
} else {
updateProxy.findNewVersion(updateEntity, updateProxy);
}
} else {
_XUpdate.onUpdateError(CHECK_NO_NEW_VERSION);
}
} else {
_XUpdate.onUpdateError(CHECK_PARSE, "json:" + result);
}
}

/**
* 不能为null
*
Expand Down Expand Up @@ -171,6 +204,7 @@ public static <T> T fromJson(String json, Class<T> classOfT) {

/**
* 把 单个指定类型的对象 转换为 JSON 字符串
*
* @param src
* @return
*/
Expand Down

0 comments on commit 76e8d47

Please sign in to comment.