Skip to content

Commit 764c09d

Browse files
committed
更新默认版本更新提示器
1 parent f9a0e41 commit 764c09d

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed

app/src/main/java/com/xuexiang/xupdatedemo/activity/UpdateActivity.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,19 @@
1818

1919
import android.app.Activity;
2020
import android.os.Bundle;
21-
import androidx.annotation.Nullable;
2221
import android.view.View;
2322

23+
import androidx.annotation.Nullable;
24+
2425
import com.xuexiang.xupdate.XUpdate;
26+
import com.xuexiang.xupdatedemo.Constants;
2527
import com.xuexiang.xupdatedemo.R;
2628

2729
/**
2830
* @author xuexiang
2931
* @since 2018/7/24 上午10:38
3032
*/
31-
public class UpdateActivity extends Activity implements View.OnClickListener{
32-
33-
private String mUpdateUrl = "https://gitee.com/xuexiangjys/XUpdate/raw/master/jsonapi/update_test.json";
34-
private String mUpdateUrl2 = "https://gitee.com/xuexiangjys/XUpdate/raw/master/jsonapi/update_forced.json";
33+
public class UpdateActivity extends Activity implements View.OnClickListener {
3534

3635
@Override
3736
protected void onCreate(@Nullable Bundle savedInstanceState) {
@@ -41,29 +40,29 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
4140

4241
@Override
4342
public void onClick(View v) {
44-
switch(v.getId()) {
43+
switch (v.getId()) {
4544
case R.id.btn_update:
4645
XUpdate.newBuild(this)
47-
.updateUrl(mUpdateUrl)
46+
.updateUrl(Constants.DEFAULT_UPDATE_URL)
4847
.update();
4948
break;
5049
case R.id.btn_support_background_update:
5150
XUpdate.newBuild(this)
52-
.updateUrl(mUpdateUrl)
51+
.updateUrl(Constants.DEFAULT_UPDATE_URL)
5352
.promptWidthRatio(0.7F)
5453
.supportBackgroundUpdate(true)
5554
.update();
5655
break;
5756
case R.id.btn_auto_update:
5857
XUpdate.newBuild(this)
59-
.updateUrl(mUpdateUrl)
58+
.updateUrl(Constants.DEFAULT_UPDATE_URL)
6059
//如果需要完全无人干预,自动更新,需要root权限【静默安装需要】
6160
.isAutoMode(true)
6261
.update();
6362
break;
6463
case R.id.btn_force_update:
6564
XUpdate.newBuild(this)
66-
.updateUrl(mUpdateUrl2)
65+
.updateUrl(Constants.FORCED_UPDATE_URL)
6766
.update();
6867
break;
6968
default:

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.xuexiang.xupdate.proxy.impl;
1818

19+
import android.app.Activity;
1920
import android.content.Context;
2021

2122
import androidx.annotation.NonNull;
@@ -24,8 +25,10 @@
2425
import com.xuexiang.xupdate.entity.PromptEntity;
2526
import com.xuexiang.xupdate.entity.UpdateEntity;
2627
import com.xuexiang.xupdate.logs.UpdateLog;
28+
import com.xuexiang.xupdate.proxy.IPrompterProxy;
2729
import com.xuexiang.xupdate.proxy.IUpdatePrompter;
2830
import com.xuexiang.xupdate.proxy.IUpdateProxy;
31+
import com.xuexiang.xupdate.widget.UpdateDialog;
2932
import com.xuexiang.xupdate.widget.UpdateDialogActivity;
3033
import com.xuexiang.xupdate.widget.UpdateDialogFragment;
3134

@@ -54,9 +57,11 @@ public void showPrompt(@NonNull UpdateEntity updateEntity, @NonNull IUpdateProxy
5457
beforeShowPrompt(updateEntity, promptEntity);
5558
UpdateLog.d("[DefaultUpdatePrompter] showPrompt, " + promptEntity);
5659
if (context instanceof FragmentActivity) {
57-
UpdateDialogFragment.show(((FragmentActivity) context).getSupportFragmentManager(), updateEntity, new DefaultPrompterProxyImpl(updateProxy), promptEntity);
60+
UpdateDialogFragment.show(((FragmentActivity) context).getSupportFragmentManager(), updateEntity, getPrompterProxy(updateProxy), promptEntity);
61+
} else if (context instanceof Activity) {
62+
UpdateDialog.newInstance(context, updateEntity, getPrompterProxy(updateProxy), promptEntity).show();
5863
} else {
59-
UpdateDialogActivity.show(context, updateEntity, new DefaultPrompterProxyImpl(updateProxy), promptEntity);
64+
UpdateDialogActivity.show(context, updateEntity, getPrompterProxy(updateProxy), promptEntity);
6065
}
6166
}
6267

@@ -72,4 +77,15 @@ protected void beforeShowPrompt(@NonNull UpdateEntity updateEntity, @NonNull Pro
7277
promptEntity.setIgnoreDownloadError(true);
7378
}
7479
}
80+
81+
/**
82+
* 构建版本更新提示器代理【可自定义属于自己的业务逻辑】
83+
*
84+
* @param updateProxy 版本更新代理
85+
* @return 版本更新提示器代理
86+
*/
87+
protected IPrompterProxy getPrompterProxy(@NonNull IUpdateProxy updateProxy) {
88+
return new DefaultPrompterProxyImpl(updateProxy);
89+
}
90+
7591
}

xupdate-lib/src/main/java/com/xuexiang/xupdate/widget/UpdateDialog.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,4 +419,17 @@ private void onInstallApk() {
419419
_XUpdate.startInstallApk(getContext(), UpdateUtils.getApkFileByUpdateEntity(mUpdateEntity), mUpdateEntity.getDownLoadEntity());
420420
}
421421

422+
@Override
423+
public void onAttachedToWindow() {
424+
super.onAttachedToWindow();
425+
_XUpdate.setIsPrompterShow(getUrl(), true);
426+
}
427+
428+
@Override
429+
public void onDetachedFromWindow() {
430+
_XUpdate.setIsPrompterShow(getUrl(), false);
431+
clearIPrompterProxy();
432+
super.onDetachedFromWindow();
433+
}
434+
422435
}

0 commit comments

Comments
 (0)