Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexLiuSheng committed Dec 11, 2019
2 parents 3c8fc25 + f4f6f28 commit c32b76d
Show file tree
Hide file tree
Showing 16 changed files with 201 additions and 53 deletions.
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ The strongest feature is easier to integrate than version of V1.+

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

- [x] Adapt to Android O
- [x] Adapt to Android Q

### include

#### jcenter

```
implementation 'com.allenliu.versionchecklib:library:2.2.1'
implementation 'com.allenliu.versionchecklib:library:2.2.2'
```
#### jitpack && androiud x
```
Expand All @@ -40,9 +39,10 @@ allprojects {
}
}
dependencies {
implementation 'com.github.AlexLiuSheng:CheckVersionLib:2.2.1'
implementation 'com.github.AlexLiuSheng:CheckVersionLib:2.2.2'
}
```

### usage


Expand Down Expand Up @@ -207,6 +207,11 @@ builder.setShowDownloadingDialog(false); true for default
```
builder.setShowNotification(false); true for default
```
> **run as foreground service(update in 2.2.2)**
recomended
```
builder.setRunOnForegroundService(true); 默认true
```
> customize notification
```
builder.setNotificationBuilder(
Expand Down Expand Up @@ -259,6 +264,15 @@ builder.setShowNotification(false); true for default
Toast.makeText(V2Activity.this,"Cancel Hanlde",Toast.LENGTH_SHORT).show();
});
```
**if u want to monitor the cancel operation in different state**
- ` builder.setDownloadingCancelListener();`
- `builder.setDownloadFailedCancelListener();`
- `builder.setReadyDownloadCancelListener();`

> set commit click listener(**added after 2.2.2**)
- ` builder.setReadyDownloadCommitClickListener();`
- `builder.setDownloadFailedCommitClickListener();`
> silent download+install directly(dont popup update dialog)
```
builder.setDirectDownload(true);
Expand Down Expand Up @@ -356,6 +370,7 @@ setCustomDownloadFailedListener
-keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
<init>(java.lang.Throwable);
}
-keep class com.allenliu.versionchecklib.**{*;}
```

### update Log
Expand Down
26 changes: 21 additions & 5 deletions README_UN.MD
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@

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

- [x] 适配到Android O
- [x] 适配到Android Q

### 导入

#### jcenter
```
implementation 'com.allenliu.versionchecklib:library:2.2.1'
implementation 'com.allenliu.versionchecklib:library:2.2.2'
```
#### jitpack && androiud x
```
Expand All @@ -38,7 +38,7 @@ allprojects {
}
}
dependencies {
implementation 'com.github.AlexLiuSheng:CheckVersionLib:2.2.1'
implementation 'com.github.AlexLiuSheng:CheckVersionLib:2.2.2'
}
```

Expand Down Expand Up @@ -203,9 +203,14 @@ dependencies {
builder.setShowDownloadingDialog(false); 默认true
```
> 是否显示通知栏
```
builder.setShowNotification(false); 默认true
```
> **以前台service运行(update in 2.2.2)**
推荐以前台服务运行更新,防止在后台时,服务被杀死
```
builder.setRunOnForegroundService(true); 默认true
```
> 自定义通知栏
```
Expand Down Expand Up @@ -253,12 +258,23 @@ builder.setShowNotification(false); 默认true
});
```
> 设置取消监听
此回调会监听所有cancel事件
```
builder.setOnCancelListener(() -> {
Toast.makeText(V2Activity.this,"Cancel Hanlde",Toast.LENGTH_SHORT).show();
});
```
**如果想单独监听几种状态下的cancel,可像如下这样设置**
- ` builder.setDownloadingCancelListener();`
- `builder.setDownloadFailedCancelListener();`
- `builder.setReadyDownloadCancelListener();`

> 设置确定监听(**added after 2.2.2**)
- ` builder.setReadyDownloadCommitClickListener();`
- `builder.setDownloadFailedCommitClickListener();`

> 静默下载+直接安装(不会弹出升级对话框)
```
builder.setDirectDownload(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,19 @@ private static String assembleUrl(String url, HttpParams params) {


private static String getRequestParamsJson(HttpParams params) {
String json;
JSONObject jsonObject = new JSONObject();
for (Map.Entry<String, Object> entry : params.entrySet()) {
try {
jsonObject.put(entry.getKey(), entry.getValue());
} catch (JSONException e) {
e.printStackTrace();
String json=null;
if(params!=null) {
JSONObject jsonObject = new JSONObject();
for (Map.Entry<String, Object> entry : params.entrySet()) {
try {
jsonObject.put(entry.getKey(), entry.getValue());
} catch (JSONException e) {
e.printStackTrace();
}
}
}

json = jsonObject.toString();
json = jsonObject.toString();
}
ALog.e("json:" + json);
return json;
}
Expand Down Expand Up @@ -207,9 +209,12 @@ public static Request.Builder postJson(RequestVersionBuilder versionParams) {
private static FormBody getRequestParams(RequestVersionBuilder versionParams) {
FormBody.Builder builder = new FormBody.Builder();
HttpParams params = versionParams.getRequestParams();
for (Map.Entry<String, Object> entry : params.entrySet()) {
builder.add(entry.getKey(), entry.getValue() + "");
ALog.e("params key:" + entry.getKey() + "-----value:" + entry.getValue());
//#https://github.com/AlexLiuSheng/CheckVersionLib/issues/293
if(params!=null) {
for (Map.Entry<String, Object> entry : params.entrySet()) {
builder.add(entry.getKey(), entry.getValue() + "");
ALog.e("params key:" + entry.getKey() + "-----value:" + entry.getValue());
}
}
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,17 @@ public void run() {
}

@Override
public void onResponse(final Call call, final Response response) throws IOException {
public void onResponse(final Call call, final Response response) {
if(!response.isSuccessful()){
handler.post(new Runnable() {
@Override
public void run() {
onDownloadFailed();

}
});
return;
}
InputStream is = null;
byte[] buf = new byte[2048];
int len = 0;
Expand All @@ -56,9 +66,7 @@ public void onResponse(final Call call, final Response response) throws IOExcept

try {
is = response.body().byteStream();
long total = response.body().contentLength();


long total;
final File file = new File(path, name);
if (file.exists()) {
file.delete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ public static void sendEventBus(int eventType) {
commonEvent.setEventType(eventType);
EventBus.getDefault().post(commonEvent);
}
public static void sendEventBusStick(int eventType) {
CommonEvent commonEvent = new CommonEvent();
commonEvent.setSuccessful(true);
commonEvent.setEventType(eventType);
EventBus.getDefault().postSticky(commonEvent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public void cancelAllMission() {
AllenHttp.getHttpClient().dispatcher().cancelAll();
// Intent intent = new Intent(context.getApplicationContext(), VersionService.class);
// context.getApplicationContext().stopService(intent);
AllenEventBusUtil.sendEventBus(AllenEventType.CLOSE);
AllenEventBusUtil.sendEventBus(AllenEventType.STOP_SERVICE);
AllenEventBusUtil.sendEventBusStick(AllenEventType.CLOSE);
AllenEventBusUtil.sendEventBusStick(AllenEventType.STOP_SERVICE);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import androidx.annotation.NonNull;

import com.allenliu.versionchecklib.callback.APKDownloadListener;
import com.allenliu.versionchecklib.callback.CommitClickListener;
import com.allenliu.versionchecklib.callback.OnCancelListener;
import com.allenliu.versionchecklib.utils.FileHelper;
import com.allenliu.versionchecklib.v2.callback.CustomDownloadFailedListener;
Expand All @@ -28,6 +29,7 @@ public class DownloadBuilder {
private String downloadUrl;
private boolean isShowDownloadingDialog;
private boolean isShowNotification;
private boolean runOnForegroundService;
private boolean isShowDownloadFailDialog;
private boolean isDirectDownload;
private NotificationBuilder notificationBuilder;
Expand All @@ -38,6 +40,11 @@ public class DownloadBuilder {
private CustomVersionDialogListener customVersionDialogListener;
private CustomInstallListener customInstallListener;
private OnCancelListener onCancelListener;
private CommitClickListener readyDownloadCommitClickListener;
private CommitClickListener downloadFailedCommitClickListener;
private OnCancelListener downloadingCancelListener;
private OnCancelListener downloadFailedCancelListener;
private OnCancelListener readyDownloadCancelListener;

private ForceUpdateListener forceUpdateListener;
private UIData versionBundle;
Expand All @@ -58,6 +65,7 @@ private void initialize() {
isDirectDownload = false;
isShowDownloadFailDialog = true;
notificationBuilder = NotificationBuilder.create();
runOnForegroundService=true;
}

public DownloadBuilder(RequestVersionBuilder requestVersionBuilder, UIData versionBundle) {
Expand Down Expand Up @@ -115,6 +123,14 @@ public DownloadBuilder setCustomDownloadInstallListener(CustomInstallListener cu
return this;
}

public boolean isRunOnForegroundService() {
return runOnForegroundService;
}

public DownloadBuilder setRunOnForegroundService(boolean runOnForegroundService) {
this.runOnForegroundService = runOnForegroundService;
return this;
}

public DownloadBuilder setSilentDownload(boolean silentDownload) {
isSilentDownload = silentDownload;
Expand Down Expand Up @@ -165,6 +181,50 @@ public DownloadBuilder setApkDownloadListener(APKDownloadListener apkDownloadLis
return this;
}

public CommitClickListener getReadyDownloadCommitClickListener() {
return readyDownloadCommitClickListener;
}

public DownloadBuilder setReadyDownloadCommitClickListener(CommitClickListener readyDownloadCommitClickListener) {
this.readyDownloadCommitClickListener = readyDownloadCommitClickListener;
return this;
}

public CommitClickListener getDownloadFailedCommitClickListener() {
return downloadFailedCommitClickListener;
}

public DownloadBuilder setDownloadFailedCommitClickListener(CommitClickListener downloadFailedCommitClickListener) {
this.downloadFailedCommitClickListener = downloadFailedCommitClickListener;
return this;
}

public OnCancelListener getDownloadingCancelListener() {
return downloadingCancelListener;
}

public DownloadBuilder setDownloadingCancelListener(OnCancelListener downloadingCancelListener) {
this.downloadingCancelListener = downloadingCancelListener;
return this;
}

public OnCancelListener getDownloadFailedCancelListener() {
return downloadFailedCancelListener;
}

public DownloadBuilder setDownloadFailedCancelListener(OnCancelListener downloadFailedCancelListener) {
this.downloadFailedCancelListener = downloadFailedCancelListener;
return this;
}

public OnCancelListener getReadyDownloadCancelListener() {
return readyDownloadCancelListener;
}

public DownloadBuilder setReadyDownloadCancelListener(OnCancelListener readyDownloadCancelListener) {
this.readyDownloadCancelListener = readyDownloadCancelListener;
return this;
}

public boolean isSilentDownload() {
return isSilentDownload;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public static void download(final String url, final String downloadApkPath, fina
Request request = new Request
.Builder()
//#issue 220

.addHeader("Accept-Encoding", "identity")
.url(url).build();
new Handler(Looper.getMainLooper()).post(new Runnable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
Expand Down Expand Up @@ -103,16 +105,25 @@ protected void checkForceUpdate() {
}

protected void cancelHandler() {
if (getVersionBuilder() != null && getVersionBuilder().getOnCancelListener() != null) {
getVersionBuilder().getOnCancelListener().onCancel();

DownloadBuilder builder=getVersionBuilder();
if (builder != null) {
if (builder.getOnCancelListener() != null)
builder.getOnCancelListener().onCancel();
if(this instanceof UIActivity&&builder.getReadyDownloadCancelListener()!=null){
builder.getReadyDownloadCancelListener().onCancel();
}else if(this instanceof DownloadFailedActivity&&builder.getDownloadFailedCancelListener()!=null){
builder.getDownloadFailedCancelListener().onCancel();
}else if(this instanceof DownloadingActivity&&builder.getDownloadingCancelListener()!=null){
builder.getDownloadingCancelListener().onCancel();
}
}
}

@Subscribe(threadMode = ThreadMode.MAIN)
public void receiveEvent(CommonEvent commonEvent) {
if(commonEvent.getEventType()==AllenEventType.CLOSE){
if (commonEvent.getEventType() == AllenEventType.CLOSE) {
finish();
EventBus.getDefault().removeStickyEvent(commonEvent);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void checkAndDeleteAPK() {
public void checkForceUpdate() {
if (builder.getForceUpdateListener() != null) {
builder.getForceUpdateListener().onShouldForceUpdate();
AllenVersionChecker.getInstance().cancelAllMission(context);
AllenVersionChecker.getInstance().cancelAllMission();
}
}
}
Loading

0 comments on commit c32b76d

Please sign in to comment.