From c59aff93d674221bf673c0f64bbc25be977492c2 Mon Sep 17 00:00:00 2001
From: fccaikai <714564390@qq.com>
Date: Mon, 25 Sep 2017 14:07:16 +0800
Subject: [PATCH] callback has new version
---
.idea/misc.xml | 15 +------------
README.md | 4 ++--
README_zh.md | 4 ++--
.../com/kcode/appupdate/MainActivity.java | 4 ++--
.../java/com/kcode/lib/UpdateWrapper.java | 8 +++----
.../com/kcode/lib/net/CheckUpdateTask.java | 21 +++++++++++++------
6 files changed, 26 insertions(+), 30 deletions(-)
diff --git a/.idea/misc.xml b/.idea/misc.xml
index 319e461..f61a7d3 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -43,25 +43,12 @@
-
-
-
-
-
-
-
-
-
-
-
+
-
- $USER_HOME$/.subversion
-
diff --git a/README.md b/README.md
index f748190..ab00f78 100644
--- a/README.md
+++ b/README.md
@@ -32,7 +32,7 @@ android app update library
```groovy
dependencies {
- compile 'com.github.fccaikai:AppUpdate:2.1.3'
+ compile 'com.github.fccaikai:AppUpdate:2.1.4'
}
```
@@ -54,7 +54,7 @@ UpdateWrapper updateWrapper = new UpdateWrapper.Builder(getApplicationContext())
//add callback ,return new version info
.setCallback(new CheckUpdateTask.Callback() {
@Override
- public void callBack(VersionModel model) {
+ public void callBack(VersionModel model,booleab hasNewVersion) {
Log.d(TAG,"new version :" + model.getVersionName());
}
})
diff --git a/README_zh.md b/README_zh.md
index 201f05c..00514ee 100644
--- a/README_zh.md
+++ b/README_zh.md
@@ -33,7 +33,7 @@ Android 检查更新库
```groovy
dependencies {
- compile 'com.github.fccaikai:AppUpdate:2.1.3'
+ compile 'com.github.fccaikai:AppUpdate:2.1.4'
}
```
@@ -52,7 +52,7 @@ UpdateWrapper updateWrapper = new UpdateWrapper.Builder(getApplicationContext())
//add callback ,return new version info
.setCallback(new CheckUpdateTask.Callback() {
@Override
- public void callBack(VersionModel model) {
+ public void callBack(VersionModel model,boolean hasNewVersion) {
Log.d(TAG,"new version :" + model.getVersionName());
}
})
diff --git a/app/src/main/java/com/kcode/appupdate/MainActivity.java b/app/src/main/java/com/kcode/appupdate/MainActivity.java
index eeee9b9..746275d 100644
--- a/app/src/main/java/com/kcode/appupdate/MainActivity.java
+++ b/app/src/main/java/com/kcode/appupdate/MainActivity.java
@@ -44,8 +44,8 @@ private void checkUpdate(final long time, final Class extends FragmentActivity
.setIsShowToast(false)
.setCallback(new CheckUpdateTask.Callback() {
@Override
- public void callBack(VersionModel model) {
- L.d(TAG,"new version :" + model.getVersionName());
+ public void callBack(VersionModel model, boolean hasNewVersion) {
+ L.d(TAG,"has new version:" + hasNewVersion + ";version info :" + model.getVersionName());
}
});
diff --git a/lib/src/main/java/com/kcode/lib/UpdateWrapper.java b/lib/src/main/java/com/kcode/lib/UpdateWrapper.java
index bf0a223..564bcd2 100644
--- a/lib/src/main/java/com/kcode/lib/UpdateWrapper.java
+++ b/lib/src/main/java/com/kcode/lib/UpdateWrapper.java
@@ -48,13 +48,12 @@ public void start() {
if (checkUpdateTime(mTime)) {
return;
}
- new CheckUpdateTask(mUrl, mInnerCallBack).start();
+ new CheckUpdateTask(mContext,mUrl, mInnerCallBack).start();
}
private CheckUpdateTask.Callback mInnerCallBack = new CheckUpdateTask.Callback() {
@Override
- public void callBack(VersionModel model) {
-
+ public void callBack(VersionModel model, boolean hasNewVersion) {
if (model == null) {
mHandler.post(new Runnable() {
@Override
@@ -72,11 +71,12 @@ public void run() {
//记录本次更新时间
PublicFunctionUtils.setLastCheckTime(mContext, System.currentTimeMillis());
if (mCallback != null) {
- mCallback.callBack(model);
+ mCallback.callBack(model,hasNewVersion);
}
start2Activity(mContext, model);
}
+
};
private boolean checkUpdateTime(long time) {
diff --git a/lib/src/main/java/com/kcode/lib/net/CheckUpdateTask.java b/lib/src/main/java/com/kcode/lib/net/CheckUpdateTask.java
index c8beb72..afa481b 100644
--- a/lib/src/main/java/com/kcode/lib/net/CheckUpdateTask.java
+++ b/lib/src/main/java/com/kcode/lib/net/CheckUpdateTask.java
@@ -1,7 +1,10 @@
package com.kcode.lib.net;
+import android.content.Context;
+
import com.kcode.lib.bean.VersionModel;
import com.kcode.lib.log.L;
+import com.kcode.lib.utils.PackageUtils;
import org.json.JSONException;
@@ -21,10 +24,12 @@ public class CheckUpdateTask extends Thread {
private static final String TAG = "CheckUpdateTask";
+ private Context mContext;
private Callback mCallBack;
private String mCheckUpdateUrl;
- public CheckUpdateTask(String checkUpdateUrl, Callback callBack) {
+ public CheckUpdateTask(Context context,String checkUpdateUrl, Callback callBack) {
+ mContext = context;
mCheckUpdateUrl = checkUpdateUrl;
this.mCallBack = callBack;
}
@@ -45,19 +50,19 @@ public void run() {
VersionModel model = new VersionModel();
try {
model.parse(data);
- mCallBack.callBack(model);
+ mCallBack.callBack(model,hasNewVersion(PackageUtils.getVersionCode(mContext),model.getVersionCode()));
} catch (JSONException e) {
e.printStackTrace();
- mCallBack.callBack(null);
+ mCallBack.callBack(null,false);
}
} catch (MalformedURLException e) {
e.printStackTrace();
- mCallBack.callBack(null);
+ mCallBack.callBack(null,false);
} catch (IOException e) {
e.printStackTrace();
- mCallBack.callBack(null);
+ mCallBack.callBack(null,false);
} finally {
if (connection != null) {
connection.disconnect();
@@ -66,6 +71,10 @@ public void run() {
}
+ private boolean hasNewVersion(int old,int n) {
+ return old < n;
+ }
+
private static String read(final InputStream in) throws IOException {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
int b;
@@ -76,6 +85,6 @@ private static String read(final InputStream in) throws IOException {
}
public interface Callback {
- void callBack(VersionModel model);
+ void callBack(VersionModel model,boolean hasNewVersion);
}
}