Skip to content

Commit 78cd09e

Browse files
committed
增加下载兼容,可以通过跳转静态网页的方式进行更新
1 parent 5f3b029 commit 78cd09e

File tree

2 files changed

+108
-3
lines changed

2 files changed

+108
-3
lines changed

xupdate-lib/src/main/java/com/xuexiang/xupdate/proxy/impl/DefaultUpdateDownloader.java

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@
1717
package com.xuexiang.xupdate.proxy.impl;
1818

1919
import android.content.ComponentName;
20+
import android.content.Intent;
2021
import android.content.ServiceConnection;
22+
import android.net.Uri;
2123
import android.os.IBinder;
24+
import android.text.TextUtils;
25+
2226
import androidx.annotation.NonNull;
2327
import androidx.annotation.Nullable;
2428

@@ -27,6 +31,7 @@
2731
import com.xuexiang.xupdate.proxy.IUpdateDownloader;
2832
import com.xuexiang.xupdate.service.DownloadService;
2933
import com.xuexiang.xupdate.service.OnFileDownloadListener;
34+
import com.xuexiang.xupdate.utils.UpdateUtils;
3035

3136
/**
3237
* 默认版本更新下载器
@@ -50,6 +55,57 @@ public class DefaultUpdateDownloader implements IUpdateDownloader {
5055

5156
@Override
5257
public void startDownload(final @NonNull UpdateEntity updateEntity, final @Nullable OnFileDownloadListener downloadListener) {
58+
if (isDownloadUrl(updateEntity)) {
59+
startDownloadService(updateEntity, downloadListener);
60+
} else {
61+
startOpenHtml(updateEntity, downloadListener);
62+
}
63+
}
64+
65+
/**
66+
* 地址是否是下载地址,需要开启下载服务进行下载【可以根据自己的逻辑进行重写】
67+
*
68+
* @param updateEntity 版本更新信息
69+
* @return 地址是否是下载地址
70+
*/
71+
protected boolean isDownloadUrl(@NonNull UpdateEntity updateEntity) {
72+
return isApkDownloadUrl(updateEntity) || !isStaticHtmlUrl(updateEntity);
73+
}
74+
75+
/**
76+
* 地址是否是apk的下载地址
77+
*
78+
* @param updateEntity 版本更新信息
79+
* @return 地址是否是apk的下载地址
80+
*/
81+
protected boolean isApkDownloadUrl(@NonNull UpdateEntity updateEntity) {
82+
String downloadUrl = updateEntity.getDownloadUrl();
83+
return !TextUtils.isEmpty(downloadUrl) && downloadUrl.substring(downloadUrl.lastIndexOf("/") + 1).endsWith(".apk");
84+
}
85+
86+
/**
87+
* 地址是否是静态网页
88+
*
89+
* @param updateEntity 版本更新信息
90+
* @return 地址是否是静态网页
91+
*/
92+
protected boolean isStaticHtmlUrl(@NonNull UpdateEntity updateEntity) {
93+
String downloadUrl = updateEntity.getDownloadUrl();
94+
if (TextUtils.isEmpty(downloadUrl)) {
95+
return false;
96+
}
97+
String urlContent = downloadUrl.substring(downloadUrl.lastIndexOf("/") + 1);
98+
return urlContent.contains(".htm") || urlContent.contains(".shtm");
99+
}
100+
101+
102+
/**
103+
* 开启下载服务
104+
*
105+
* @param updateEntity 版本更新信息
106+
* @param downloadListener 下载监听
107+
*/
108+
protected void startDownloadService(@NonNull final UpdateEntity updateEntity, @Nullable final OnFileDownloadListener downloadListener) {
53109
DownloadService.bindService(mServiceConnection = new ServiceConnection() {
54110
@Override
55111
public void onServiceConnected(ComponentName name, IBinder service) {
@@ -64,11 +120,33 @@ public void onServiceDisconnected(ComponentName name) {
64120
});
65121
}
66122

123+
/**
124+
* 使用系统的api打开网页
125+
*
126+
* @param updateEntity 版本更新信息
127+
* @param downloadListener 监听回调
128+
*/
129+
protected void startOpenHtml(@NonNull UpdateEntity updateEntity, @Nullable OnFileDownloadListener downloadListener) {
130+
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(updateEntity.getDownloadUrl()));
131+
boolean result = UpdateUtils.startActivity(intent);
132+
if (downloadListener != null) {
133+
if (result) {
134+
// 强制更新的话,不能关闭更新弹窗
135+
if (!updateEntity.isForce()) {
136+
downloadListener.onCompleted(null);
137+
}
138+
} else {
139+
downloadListener.onError(null);
140+
}
141+
}
142+
}
143+
67144
/**
68145
* 开始下载
69-
* @param binder
70-
* @param updateEntity
71-
* @param downloadListener
146+
*
147+
* @param binder 下载服务绑定
148+
* @param updateEntity 版本更新信息
149+
* @param downloadListener 下载监听
72150
*/
73151
private void startDownload(DownloadService.DownloadBinder binder, @NonNull UpdateEntity updateEntity, @Nullable OnFileDownloadListener downloadListener) {
74152
mDownloadBinder = binder;

xupdate-lib/src/main/java/com/xuexiang/xupdate/utils/UpdateUtils.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
import android.annotation.SuppressLint;
2020
import android.app.ActivityManager;
21+
import android.content.ActivityNotFoundException;
2122
import android.content.Context;
23+
import android.content.Intent;
2224
import android.content.SharedPreferences;
2325
import android.content.pm.PackageInfo;
2426
import android.content.pm.PackageManager;
@@ -40,6 +42,7 @@
4042
import com.xuexiang.xupdate.XUpdate;
4143
import com.xuexiang.xupdate._XUpdate;
4244
import com.xuexiang.xupdate.entity.UpdateEntity;
45+
import com.xuexiang.xupdate.logs.UpdateLog;
4346
import com.xuexiang.xupdate.proxy.IUpdateProxy;
4447

4548
import java.io.File;
@@ -440,4 +443,28 @@ public static boolean isMainThread() {
440443
return Looper.getMainLooper() == Looper.myLooper();
441444
}
442445

446+
/**
447+
* 页面跳转
448+
*
449+
* @param intent 跳转意图
450+
*/
451+
public static boolean startActivity(final Intent intent) {
452+
if (intent == null) {
453+
UpdateLog.e("[startActivity failed]: intent == null");
454+
return false;
455+
}
456+
if (XUpdate.getContext().getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null) {
457+
try {
458+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
459+
XUpdate.getContext().startActivity(intent);
460+
return true;
461+
} catch (ActivityNotFoundException e) {
462+
e.printStackTrace();
463+
UpdateLog.e(e);
464+
}
465+
} else {
466+
UpdateLog.e("[resolveActivity failed]: " + (intent.getComponent() != null ? intent.getComponent().getClassName() : intent.getAction()) + " do not register in manifest");
467+
}
468+
return false;
469+
}
443470
}

0 commit comments

Comments
 (0)