Skip to content

Commit

Permalink
callback has new version
Browse files Browse the repository at this point in the history
  • Loading branch information
caikaidev committed Sep 25, 2017
1 parent fce3b6f commit c59aff9
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 30 deletions.
15 changes: 1 addition & 14 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
```

Expand All @@ -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());
}
})
Expand Down
4 changes: 2 additions & 2 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Android 检查更新库

```groovy
dependencies {
compile 'com.github.fccaikai:AppUpdate:2.1.3'
compile 'com.github.fccaikai:AppUpdate:2.1.4'
}
```

Expand All @@ -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());
}
})
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/kcode/appupdate/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
});

Expand Down
8 changes: 4 additions & 4 deletions lib/src/main/java/com/kcode/lib/UpdateWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
21 changes: 15 additions & 6 deletions lib/src/main/java/com/kcode/lib/net/CheckUpdateTask.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
}
Expand All @@ -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();
Expand All @@ -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;
Expand All @@ -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);
}
}

0 comments on commit c59aff9

Please sign in to comment.