1717package com .xuexiang .xupdate .proxy .impl ;
1818
1919import android .content .ComponentName ;
20+ import android .content .Intent ;
2021import android .content .ServiceConnection ;
22+ import android .net .Uri ;
2123import android .os .IBinder ;
24+ import android .text .TextUtils ;
25+
2226import androidx .annotation .NonNull ;
2327import androidx .annotation .Nullable ;
2428
2731import com .xuexiang .xupdate .proxy .IUpdateDownloader ;
2832import com .xuexiang .xupdate .service .DownloadService ;
2933import 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 ;
0 commit comments