Skip to content

Commit c32b76d

Browse files
committed
2 parents 3c8fc25 + f4f6f28 commit c32b76d

File tree

16 files changed

+201
-53
lines changed

16 files changed

+201
-53
lines changed

README.md

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@ The strongest feature is easier to integrate than version of V1.+
2222

2323
- [x] Support Silence Download (one line code)
2424

25-
- [x] Adapt to Android O
25+
- [x] Adapt to Android Q
2626

2727
### include
2828

2929
#### jcenter
30-
3130
```
32-
implementation 'com.allenliu.versionchecklib:library:2.2.1'
31+
implementation 'com.allenliu.versionchecklib:library:2.2.2'
3332
```
3433
#### jitpack && androiud x
3534
```
@@ -40,9 +39,10 @@ allprojects {
4039
}
4140
}
4241
dependencies {
43-
implementation 'com.github.AlexLiuSheng:CheckVersionLib:2.2.1'
42+
implementation 'com.github.AlexLiuSheng:CheckVersionLib:2.2.2'
4443
}
4544
```
45+
4646
### usage
4747

4848

@@ -207,6 +207,11 @@ builder.setShowDownloadingDialog(false); true for default
207207
```
208208
builder.setShowNotification(false); true for default
209209
```
210+
> **run as foreground service(update in 2.2.2)**
211+
recomended
212+
```
213+
builder.setRunOnForegroundService(true); 默认true
214+
```
210215
> customize notification
211216
```
212217
builder.setNotificationBuilder(
@@ -259,6 +264,15 @@ builder.setShowNotification(false); true for default
259264
Toast.makeText(V2Activity.this,"Cancel Hanlde",Toast.LENGTH_SHORT).show();
260265
});
261266
```
267+
**if u want to monitor the cancel operation in different state**
268+
- ` builder.setDownloadingCancelListener();`
269+
- `builder.setDownloadFailedCancelListener();`
270+
- `builder.setReadyDownloadCancelListener();`
271+
272+
> set commit click listener(**added after 2.2.2**)
273+
274+
- ` builder.setReadyDownloadCommitClickListener();`
275+
- `builder.setDownloadFailedCommitClickListener();`
262276
> silent download+install directly(dont popup update dialog)
263277
```
264278
builder.setDirectDownload(true);
@@ -356,6 +370,7 @@ setCustomDownloadFailedListener
356370
-keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
357371
<init>(java.lang.Throwable);
358372
}
373+
-keep class com.allenliu.versionchecklib.**{*;}
359374
```
360375

361376
### update Log

README_UN.MD

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121

2222
- [x] 支持静默下载 (一行代码)
2323

24-
- [x] 适配到Android O
24+
- [x] 适配到Android Q
2525

2626
### 导入
2727

2828
#### jcenter
2929
```
30-
implementation 'com.allenliu.versionchecklib:library:2.2.1'
30+
implementation 'com.allenliu.versionchecklib:library:2.2.2'
3131
```
3232
#### jitpack && androiud x
3333
```
@@ -38,7 +38,7 @@ allprojects {
3838
}
3939
}
4040
dependencies {
41-
implementation 'com.github.AlexLiuSheng:CheckVersionLib:2.2.1'
41+
implementation 'com.github.AlexLiuSheng:CheckVersionLib:2.2.2'
4242
}
4343
```
4444

@@ -203,9 +203,14 @@ dependencies {
203203
builder.setShowDownloadingDialog(false); 默认true
204204
```
205205
> 是否显示通知栏
206-
207206
```
208207
builder.setShowNotification(false); 默认true
208+
```
209+
> **以前台service运行(update in 2.2.2)**
210+
推荐以前台服务运行更新,防止在后台时,服务被杀死
211+
```
212+
builder.setRunOnForegroundService(true); 默认true
213+
209214
```
210215
> 自定义通知栏
211216
```
@@ -253,12 +258,23 @@ builder.setShowNotification(false); 默认true
253258
});
254259
```
255260
> 设置取消监听
261+
此回调会监听所有cancel事件
256262
```
257-
263+
258264
builder.setOnCancelListener(() -> {
259265
Toast.makeText(V2Activity.this,"Cancel Hanlde",Toast.LENGTH_SHORT).show();
260266
});
261267
```
268+
**如果想单独监听几种状态下的cancel,可像如下这样设置**
269+
- ` builder.setDownloadingCancelListener();`
270+
- `builder.setDownloadFailedCancelListener();`
271+
- `builder.setReadyDownloadCancelListener();`
272+
273+
> 设置确定监听(**added after 2.2.2**)
274+
275+
- ` builder.setReadyDownloadCommitClickListener();`
276+
- `builder.setDownloadFailedCommitClickListener();`
277+
262278
> 静默下载+直接安装(不会弹出升级对话框)
263279
```
264280
builder.setDirectDownload(true);

library/src/main/java/com/allenliu/versionchecklib/core/http/AllenHttp.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,19 @@ private static String assembleUrl(String url, HttpParams params) {
100100

101101

102102
private static String getRequestParamsJson(HttpParams params) {
103-
String json;
104-
JSONObject jsonObject = new JSONObject();
105-
for (Map.Entry<String, Object> entry : params.entrySet()) {
106-
try {
107-
jsonObject.put(entry.getKey(), entry.getValue());
108-
} catch (JSONException e) {
109-
e.printStackTrace();
103+
String json=null;
104+
if(params!=null) {
105+
JSONObject jsonObject = new JSONObject();
106+
for (Map.Entry<String, Object> entry : params.entrySet()) {
107+
try {
108+
jsonObject.put(entry.getKey(), entry.getValue());
109+
} catch (JSONException e) {
110+
e.printStackTrace();
111+
}
110112
}
111-
}
112113

113-
json = jsonObject.toString();
114+
json = jsonObject.toString();
115+
}
114116
ALog.e("json:" + json);
115117
return json;
116118
}
@@ -207,9 +209,12 @@ public static Request.Builder postJson(RequestVersionBuilder versionParams) {
207209
private static FormBody getRequestParams(RequestVersionBuilder versionParams) {
208210
FormBody.Builder builder = new FormBody.Builder();
209211
HttpParams params = versionParams.getRequestParams();
210-
for (Map.Entry<String, Object> entry : params.entrySet()) {
211-
builder.add(entry.getKey(), entry.getValue() + "");
212-
ALog.e("params key:" + entry.getKey() + "-----value:" + entry.getValue());
212+
//#https://github.com/AlexLiuSheng/CheckVersionLib/issues/293
213+
if(params!=null) {
214+
for (Map.Entry<String, Object> entry : params.entrySet()) {
215+
builder.add(entry.getKey(), entry.getValue() + "");
216+
ALog.e("params key:" + entry.getKey() + "-----value:" + entry.getValue());
217+
}
213218
}
214219
return builder.build();
215220
}

library/src/main/java/com/allenliu/versionchecklib/core/http/FileCallBack.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,17 @@ public void run() {
4343
}
4444

4545
@Override
46-
public void onResponse(final Call call, final Response response) throws IOException {
46+
public void onResponse(final Call call, final Response response) {
47+
if(!response.isSuccessful()){
48+
handler.post(new Runnable() {
49+
@Override
50+
public void run() {
51+
onDownloadFailed();
52+
53+
}
54+
});
55+
return;
56+
}
4757
InputStream is = null;
4858
byte[] buf = new byte[2048];
4959
int len = 0;
@@ -56,9 +66,7 @@ public void onResponse(final Call call, final Response response) throws IOExcept
5666

5767
try {
5868
is = response.body().byteStream();
59-
long total = response.body().contentLength();
60-
61-
69+
long total;
6270
final File file = new File(path, name);
6371
if (file.exists()) {
6472
file.delete();

library/src/main/java/com/allenliu/versionchecklib/utils/AllenEventBusUtil.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,10 @@ public static void sendEventBus(int eventType) {
1515
commonEvent.setEventType(eventType);
1616
EventBus.getDefault().post(commonEvent);
1717
}
18+
public static void sendEventBusStick(int eventType) {
19+
CommonEvent commonEvent = new CommonEvent();
20+
commonEvent.setSuccessful(true);
21+
commonEvent.setEventType(eventType);
22+
EventBus.getDefault().postSticky(commonEvent);
23+
}
1824
}

library/src/main/java/com/allenliu/versionchecklib/v2/AllenVersionChecker.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public void cancelAllMission() {
4141
AllenHttp.getHttpClient().dispatcher().cancelAll();
4242
// Intent intent = new Intent(context.getApplicationContext(), VersionService.class);
4343
// context.getApplicationContext().stopService(intent);
44-
AllenEventBusUtil.sendEventBus(AllenEventType.CLOSE);
45-
AllenEventBusUtil.sendEventBus(AllenEventType.STOP_SERVICE);
44+
AllenEventBusUtil.sendEventBusStick(AllenEventType.CLOSE);
45+
AllenEventBusUtil.sendEventBusStick(AllenEventType.STOP_SERVICE);
4646
}
4747

4848
/**

library/src/main/java/com/allenliu/versionchecklib/v2/builder/DownloadBuilder.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import androidx.annotation.NonNull;
77

88
import com.allenliu.versionchecklib.callback.APKDownloadListener;
9+
import com.allenliu.versionchecklib.callback.CommitClickListener;
910
import com.allenliu.versionchecklib.callback.OnCancelListener;
1011
import com.allenliu.versionchecklib.utils.FileHelper;
1112
import com.allenliu.versionchecklib.v2.callback.CustomDownloadFailedListener;
@@ -28,6 +29,7 @@ public class DownloadBuilder {
2829
private String downloadUrl;
2930
private boolean isShowDownloadingDialog;
3031
private boolean isShowNotification;
32+
private boolean runOnForegroundService;
3133
private boolean isShowDownloadFailDialog;
3234
private boolean isDirectDownload;
3335
private NotificationBuilder notificationBuilder;
@@ -38,6 +40,11 @@ public class DownloadBuilder {
3840
private CustomVersionDialogListener customVersionDialogListener;
3941
private CustomInstallListener customInstallListener;
4042
private OnCancelListener onCancelListener;
43+
private CommitClickListener readyDownloadCommitClickListener;
44+
private CommitClickListener downloadFailedCommitClickListener;
45+
private OnCancelListener downloadingCancelListener;
46+
private OnCancelListener downloadFailedCancelListener;
47+
private OnCancelListener readyDownloadCancelListener;
4148

4249
private ForceUpdateListener forceUpdateListener;
4350
private UIData versionBundle;
@@ -58,6 +65,7 @@ private void initialize() {
5865
isDirectDownload = false;
5966
isShowDownloadFailDialog = true;
6067
notificationBuilder = NotificationBuilder.create();
68+
runOnForegroundService=true;
6169
}
6270

6371
public DownloadBuilder(RequestVersionBuilder requestVersionBuilder, UIData versionBundle) {
@@ -115,6 +123,14 @@ public DownloadBuilder setCustomDownloadInstallListener(CustomInstallListener cu
115123
return this;
116124
}
117125

126+
public boolean isRunOnForegroundService() {
127+
return runOnForegroundService;
128+
}
129+
130+
public DownloadBuilder setRunOnForegroundService(boolean runOnForegroundService) {
131+
this.runOnForegroundService = runOnForegroundService;
132+
return this;
133+
}
118134

119135
public DownloadBuilder setSilentDownload(boolean silentDownload) {
120136
isSilentDownload = silentDownload;
@@ -165,6 +181,50 @@ public DownloadBuilder setApkDownloadListener(APKDownloadListener apkDownloadLis
165181
return this;
166182
}
167183

184+
public CommitClickListener getReadyDownloadCommitClickListener() {
185+
return readyDownloadCommitClickListener;
186+
}
187+
188+
public DownloadBuilder setReadyDownloadCommitClickListener(CommitClickListener readyDownloadCommitClickListener) {
189+
this.readyDownloadCommitClickListener = readyDownloadCommitClickListener;
190+
return this;
191+
}
192+
193+
public CommitClickListener getDownloadFailedCommitClickListener() {
194+
return downloadFailedCommitClickListener;
195+
}
196+
197+
public DownloadBuilder setDownloadFailedCommitClickListener(CommitClickListener downloadFailedCommitClickListener) {
198+
this.downloadFailedCommitClickListener = downloadFailedCommitClickListener;
199+
return this;
200+
}
201+
202+
public OnCancelListener getDownloadingCancelListener() {
203+
return downloadingCancelListener;
204+
}
205+
206+
public DownloadBuilder setDownloadingCancelListener(OnCancelListener downloadingCancelListener) {
207+
this.downloadingCancelListener = downloadingCancelListener;
208+
return this;
209+
}
210+
211+
public OnCancelListener getDownloadFailedCancelListener() {
212+
return downloadFailedCancelListener;
213+
}
214+
215+
public DownloadBuilder setDownloadFailedCancelListener(OnCancelListener downloadFailedCancelListener) {
216+
this.downloadFailedCancelListener = downloadFailedCancelListener;
217+
return this;
218+
}
219+
220+
public OnCancelListener getReadyDownloadCancelListener() {
221+
return readyDownloadCancelListener;
222+
}
223+
224+
public DownloadBuilder setReadyDownloadCancelListener(OnCancelListener readyDownloadCancelListener) {
225+
this.readyDownloadCancelListener = readyDownloadCancelListener;
226+
return this;
227+
}
168228

169229
public boolean isSilentDownload() {
170230
return isSilentDownload;

library/src/main/java/com/allenliu/versionchecklib/v2/net/DownloadMangerV2.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public static void download(final String url, final String downloadApkPath, fina
2626
Request request = new Request
2727
.Builder()
2828
//#issue 220
29-
3029
.addHeader("Accept-Encoding", "identity")
3130
.url(url).build();
3231
new Handler(Looper.getMainLooper()).post(new Runnable() {

library/src/main/java/com/allenliu/versionchecklib/v2/ui/AllenBaseActivity.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import android.graphics.Color;
66
import android.os.Build;
77
import android.os.Bundle;
8+
89
import androidx.annotation.Nullable;
910
import androidx.appcompat.app.AppCompatActivity;
11+
1012
import android.view.View;
1113
import android.view.ViewGroup;
1214
import android.view.WindowManager;
@@ -103,16 +105,25 @@ protected void checkForceUpdate() {
103105
}
104106

105107
protected void cancelHandler() {
106-
if (getVersionBuilder() != null && getVersionBuilder().getOnCancelListener() != null) {
107-
getVersionBuilder().getOnCancelListener().onCancel();
108-
108+
DownloadBuilder builder=getVersionBuilder();
109+
if (builder != null) {
110+
if (builder.getOnCancelListener() != null)
111+
builder.getOnCancelListener().onCancel();
112+
if(this instanceof UIActivity&&builder.getReadyDownloadCancelListener()!=null){
113+
builder.getReadyDownloadCancelListener().onCancel();
114+
}else if(this instanceof DownloadFailedActivity&&builder.getDownloadFailedCancelListener()!=null){
115+
builder.getDownloadFailedCancelListener().onCancel();
116+
}else if(this instanceof DownloadingActivity&&builder.getDownloadingCancelListener()!=null){
117+
builder.getDownloadingCancelListener().onCancel();
118+
}
109119
}
110120
}
111121

112122
@Subscribe(threadMode = ThreadMode.MAIN)
113123
public void receiveEvent(CommonEvent commonEvent) {
114-
if(commonEvent.getEventType()==AllenEventType.CLOSE){
124+
if (commonEvent.getEventType() == AllenEventType.CLOSE) {
115125
finish();
126+
EventBus.getDefault().removeStickyEvent(commonEvent);
116127
}
117128
}
118129

library/src/main/java/com/allenliu/versionchecklib/v2/ui/BuilderHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void checkAndDeleteAPK() {
4343
public void checkForceUpdate() {
4444
if (builder.getForceUpdateListener() != null) {
4545
builder.getForceUpdateListener().onShouldForceUpdate();
46-
AllenVersionChecker.getInstance().cancelAllMission(context);
46+
AllenVersionChecker.getInstance().cancelAllMission();
4747
}
4848
}
4949
}

0 commit comments

Comments
 (0)