Skip to content

Commit

Permalink
fix Toast bug
Browse files Browse the repository at this point in the history
  • Loading branch information
caikaidev committed Mar 25, 2017
1 parent d764f1c commit ee4747e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 41 deletions.
2 changes: 1 addition & 1 deletion 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.0.4'
compile 'com.github.fccaikai:AppUpdate:2.0.5'
}
```

Expand Down
2 changes: 1 addition & 1 deletion 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.0.4'
compile 'com.github.fccaikai:AppUpdate:2.0.5'
}
```

Expand Down
9 changes: 2 additions & 7 deletions app/src/main/java/com/kcode/appupdate/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.kcode.lib.UpdateWrapper;
import com.kcode.lib.bean.VersionModel;
import com.kcode.lib.log.L;
import com.kcode.lib.net.CheckUpdateTask;
import com.kcode.permissionslib.main.OnRequestPermissionsCallBack;
import com.kcode.permissionslib.main.PermissionCompat;
Expand Down Expand Up @@ -52,12 +52,7 @@ public void onGrant() {
.setCallback(new CheckUpdateTask.Callback() {
@Override
public void callBack(VersionModel model) {
Log.d(TAG,"new version :" + model.getVersionName());
}

@Override
public void isLatestVersion() {

L.d(TAG,"new version :" + model.getVersionName());
}
});

Expand Down
78 changes: 49 additions & 29 deletions lib/src/main/java/com/kcode/lib/UpdateWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.widget.Toast;

import com.kcode.lib.bean.VersionModel;
import com.kcode.lib.common.Constant;
Expand All @@ -21,40 +24,50 @@ public class UpdateWrapper {
private static final String TAG = "UpdateWrapper";

private Context mContext;
private String url;
private String mUrl;
private String mToastMsg;
private CheckUpdateTask.Callback mCallback;
private int notificationIcon;
private long time;
private Class<? extends FragmentActivity> cls;
private int mNotificationIcon;
private long mTime;
private Class<? extends FragmentActivity> mCls;

private UpdateWrapper() {
}

public void start() {

if (TextUtils.isEmpty(url)) {
if (TextUtils.isEmpty(mUrl)) {
throw new RuntimeException("url not be null");
}

if (checkUpdateTime(time)) {
if (checkUpdateTime(mTime)) {
L.d(TAG,"距离上次更新时间太近");
return;
}
new CheckUpdateTask(url, new CheckUpdateTask.Callback() {
@Override
public void callBack(VersionModel model) {
//记录本次更新时间
PublicFunctionUtils.setLastCheckTime(mContext, System.currentTimeMillis());
mCallback.callBack(model);
start2Activity(mContext, model);
}
new CheckUpdateTask(mUrl, innerCallBack).start();
}

@Override
public void isLatestVersion() {
mCallback.isLatestVersion();
private CheckUpdateTask.Callback innerCallBack = new CheckUpdateTask.Callback() {
@Override
public void callBack(VersionModel model) {

if (model == null) {
mHandler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(mContext,
TextUtils.isEmpty(mToastMsg) ? "当前已是最新版本" : mToastMsg,
Toast.LENGTH_SHORT).show();
}
});
return;
}
}).start();
}
//记录本次更新时间
PublicFunctionUtils.setLastCheckTime(mContext, System.currentTimeMillis());
mCallback.callBack(model);
start2Activity(mContext, model);
}
};

private boolean checkUpdateTime(long time) {
long lastCheckUpdateTime = PublicFunctionUtils.getLastCheckTime(mContext);
Expand All @@ -67,10 +80,10 @@ private boolean checkUpdateTime(long time) {

private void start2Activity(Context context, VersionModel model) {
try {
Intent intent = new Intent(context, cls == null ? UpdateActivity.class : cls);
Intent intent = new Intent(context, mCls == null ? UpdateActivity.class : mCls);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Constant.MODEL, model);
intent.putExtra(Constant.NOTIFICATION_ICON, notificationIcon);
intent.putExtra(Constant.NOTIFICATION_ICON, mNotificationIcon);
context.startActivity(intent);
} catch (Exception e) {

Expand All @@ -79,39 +92,46 @@ private void start2Activity(Context context, VersionModel model) {
}

public static class Builder {
private UpdateWrapper mUpdateWrapper = new UpdateWrapper();
private UpdateWrapper wrapper = new UpdateWrapper();

public Builder(Context context) {
mUpdateWrapper.mContext = context;
wrapper.mContext = context;
}

public Builder setUrl(String url) {
mUpdateWrapper.url = url;
wrapper.mUrl = url;
return this;
}

public Builder setTime(long time) {
mUpdateWrapper.time = time;
wrapper.mTime = time;
return this;
}

public Builder setNotificationIcon(int notificationIcon) {
mUpdateWrapper.notificationIcon = notificationIcon;
wrapper.mNotificationIcon = notificationIcon;
return this;
}

public Builder setCustomsActivity(Class<? extends FragmentActivity> cls) {
mUpdateWrapper.cls = cls;
wrapper.mCls = cls;
return this;
}

public Builder setCallback(CheckUpdateTask.Callback callback) {
mUpdateWrapper.mCallback = callback;
wrapper.mCallback = callback;
return this;
}

public Builder setToastMsg(String toastMsg) {
wrapper.mToastMsg = toastMsg;
return this;
}

public UpdateWrapper build() {
return mUpdateWrapper;
return wrapper;
}
}

private Handler mHandler = new Handler(Looper.getMainLooper());
}
4 changes: 1 addition & 3 deletions lib/src/main/java/com/kcode/lib/net/CheckUpdateTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void run() {
mOkHttpClient.newCall(request).enqueue(new okhttp3.Callback() {
@Override
public void onFailure(Call call, IOException e) {
mCallBack.isLatestVersion();
mCallBack.callBack(null);
}

@Override
Expand All @@ -56,7 +56,5 @@ public void onResponse(Call call, Response response) throws IOException {

public interface Callback {
void callBack(VersionModel model);

void isLatestVersion();
}
}

0 comments on commit ee4747e

Please sign in to comment.