Skip to content

Commit

Permalink
1.change update.json. change "mustUpdate" to "minSupport"
Browse files Browse the repository at this point in the history
2.add showToast config
  • Loading branch information
caikaidev committed Apr 11, 2017
1 parent 54ff272 commit da05802
Show file tree
Hide file tree
Showing 18 changed files with 69 additions and 62 deletions.
3 changes: 1 addition & 2 deletions .idea/gradle.xml

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

2 changes: 1 addition & 1 deletion .idea/misc.xml

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

2 changes: 1 addition & 1 deletion .idea/vcs.xml

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

6 changes: 4 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.0.7'
compile 'com.github.fccaikai:AppUpdate:2.0.8'
}
```

Expand All @@ -49,6 +49,8 @@ UpdateWrapper updateWrapper = new UpdateWrapper.Builder(getApplicationContext())
.setUrl("you update json file url")
//set customs activity
.setCustomsActivity(cls)
//set showToast. default is true
.setIsShowToast(false)
//add callback ,return new version info
.setCallback(new CheckUpdateTask.Callback() {
@Override
Expand All @@ -68,7 +70,7 @@ updateWrapper.start();
"versionCode":1,
"versionName":"1.0.0",
"content":"1.add something#2.add something",//use # to wrap
"mustUpdate":true, //must update ,true or false
"minSupport":1, //min support version. while your app versionCode less than minSupport,You must update app
"url":"apk download url"
}
```
Expand Down
6 changes: 4 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.0.7'
compile 'com.github.fccaikai:AppUpdate:2.0.8'
}
```

Expand All @@ -47,6 +47,8 @@ UpdateWrapper updateWrapper = new UpdateWrapper.Builder(getApplicationContext())
.setNotificationIcon(R.mipmap.ic_launcher_round)
//set update file url
.setUrl("you update json file url")
//set showToast. default is true
.setIsShowToast(false)
//add callback ,return new version info
.setCallback(new CheckUpdateTask.Callback() {
@Override
Expand All @@ -65,7 +67,7 @@ updateWrapper.start();
"versionCode":1,
"versionName":"1.0.0",
"content":"1.新增抢单功能#2.性能优化",//使用 # 来进行换行
"mustUpdate":true, //是否强制 ,true or false
"minSupport":1, //最低支持版本,小于此版本必须强制更新
"url":"apk download url"
}
```
Expand Down
1 change: 0 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,5 @@ dependencies {
compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'
testCompile 'junit:junit:4.12'
compile 'com.github.fccaikai:AndroidPermissionX:1.0.0'
compile project(':lib')
}
2 changes: 1 addition & 1 deletion app/src/main/assets/update.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"versionCode":2,
"versionName":"2.0.0",
"content":"1.新增抢单功能#2.性能优化",
"mustUpdate":false,
"minSupport":1,
"url":"http://45.78.52.169/app/app.apk"
}
41 changes: 13 additions & 28 deletions app/src/main/java/com/kcode/appupdate/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
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;

public class MainActivity extends AppCompatActivity {

Expand Down Expand Up @@ -39,36 +37,23 @@ public void checkCustoms(View view) {

private void checkUpdate(final long time, final Class<? extends FragmentActivity> cls) {

PermissionCompat.Builder pBuilder = new PermissionCompat.Builder(this);
pBuilder.addPermissions(permissions)
.addRequestPermissionsCallBack(new OnRequestPermissionsCallBack() {
UpdateWrapper.Builder builder = new UpdateWrapper.Builder(getApplicationContext())
.setTime(time)
.setNotificationIcon(R.mipmap.ic_launcher_round)
.setUrl("http://45.78.52.169/app/update.json")
.setIsShowToast(false)
.setCallback(new CheckUpdateTask.Callback() {
@Override
public void onGrant() {

UpdateWrapper.Builder builder = new UpdateWrapper.Builder(getApplicationContext())
.setTime(time)
.setNotificationIcon(R.mipmap.ic_launcher_round)
.setUrl("http://45.78.52.169/app/update.json")
.setCallback(new CheckUpdateTask.Callback() {
@Override
public void callBack(VersionModel model) {
L.d(TAG,"new version :" + model.getVersionName());
}
});

if (cls != null) {
builder.setCustomsActivity(cls);
}

builder.build().start();
public void callBack(VersionModel model) {
L.d(TAG,"new version :" + model.getVersionName());
}
});

@Override
public void onDenied(String s) {
if (cls != null) {
builder.setCustomsActivity(cls);
}

}
});
pBuilder.build().request();
builder.build().start();


}
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
classpath 'com.android.tools.build:gradle:2.2.0'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
1 change: 0 additions & 1 deletion lib/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true">
<service android:name=".net.DownLoadService"/>

Expand Down
16 changes: 13 additions & 3 deletions lib/src/main/java/com/kcode/lib/UpdateWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class UpdateWrapper {
private CheckUpdateTask.Callback mCallback;
private int mNotificationIcon;
private long mTime;
private boolean mIsShowToast = true;
private Class<? extends FragmentActivity> mCls;

private UpdateWrapper() {
Expand Down Expand Up @@ -58,9 +59,12 @@ public void callBack(VersionModel model) {
mHandler.post(new Runnable() {
@Override
public void run() {
ToastUtils.show(mContext,
TextUtils.isEmpty(mToastMsg) ?
mContext.getResources().getString(R.string.update_lib_default_toast) : mToastMsg);
if (mIsShowToast) {
ToastUtils.show(mContext,
TextUtils.isEmpty(mToastMsg) ?
mContext.getResources().getString(R.string.update_lib_default_toast) : mToastMsg);
}

}
});
return;
Expand Down Expand Up @@ -91,6 +95,7 @@ private void start2Activity(Context context, VersionModel model) {
intent.putExtra(Constant.MODEL, model);
intent.putExtra(Constant.TOAST_MSG, mToastMsg);
intent.putExtra(Constant.NOTIFICATION_ICON, mNotificationIcon);
intent.putExtra(Constant.IS_SHOW_TOAST_MSG, mIsShowToast);
context.startActivity(intent);
} catch (Exception e) {

Expand Down Expand Up @@ -135,6 +140,11 @@ public Builder setToastMsg(String toastMsg) {
return this;
}

public Builder setIsShowToast(boolean isShowToast) {
wrapper.mIsShowToast = isShowToast;
return this;
}

public UpdateWrapper build() {
return wrapper;
}
Expand Down
14 changes: 7 additions & 7 deletions lib/src/main/java/com/kcode/lib/bean/VersionModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ public class VersionModel implements Serializable{
* versionCode : 1
* versionName : 1.0.0
* content : 更新描述
* mustUpdate : true
* minSupport : 1
* url : 文件下载地址
*/

private int versionCode;
private String versionName;
private String content;
private boolean mustUpdate;
private int minSupport;
private String url;

public int getVersionCode() {
Expand All @@ -51,12 +51,12 @@ public void setContent(String content) {
this.content = content;
}

public boolean isMustUpdate() {
return mustUpdate;
public int getMinSupport() {
return minSupport;
}

public void setMustUpdate(boolean mustUpdate) {
this.mustUpdate = mustUpdate;
public void setMinSupport(int minSupport) {
this.minSupport = minSupport;
}

public String getUrl() {
Expand All @@ -74,7 +74,7 @@ public VersionModel parse(String json) {
versionName = object.getString("versionName");
content = object.getString("content");
url = object.getString("url");
mustUpdate = object.getBoolean("mustUpdate");
minSupport = object.optInt("minSupport");

return this;

Expand Down
1 change: 1 addition & 0 deletions lib/src/main/java/com/kcode/lib/common/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ public class Constant {
public static final String MODEL = "model";
public static final String TOAST_MSG = "toast_msg";
public static final String NOTIFICATION_ICON = "notification_icon";
public static final String IS_SHOW_TOAST_MSG = "is_show_toast";
}
7 changes: 5 additions & 2 deletions lib/src/main/java/com/kcode/lib/dialog/UpdateActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import com.kcode.lib.base.AbstractUpdateActivity;
import com.kcode.lib.bean.VersionModel;
import com.kcode.lib.common.Constant;
import com.kcode.lib.utils.PackageUtils;

public class UpdateActivity extends AbstractUpdateActivity{

private int notificationIcon;
protected VersionModel mModel;
protected String mToastMsg;
protected boolean mIsShowToast;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -24,6 +26,7 @@ protected void onCreate(Bundle savedInstanceState) {
notificationIcon = getIntent().getIntExtra(Constant.NOTIFICATION_ICON, 0);
mModel = (VersionModel) getIntent().getSerializableExtra(Constant.MODEL);
mToastMsg = getIntent().getStringExtra(Constant.TOAST_MSG);
mIsShowToast = getIntent().getBooleanExtra(Constant.IS_SHOW_TOAST_MSG,true);
if (mModel == null) {
finish();
return;
Expand All @@ -45,15 +48,15 @@ public void showDownLoadProgress() {

@Override
public void onBackPressed() {
if (mModel.isMustUpdate()) {
if (PackageUtils.getVersionCode(getApplicationContext()) < mModel.getMinSupport()) {
return;
}
super.onBackPressed();
}

@Override
protected Fragment getUpdateDialogFragment() {
return UpdateDialog.newInstance(mModel,mToastMsg);
return UpdateDialog.newInstance(mModel,mToastMsg,mIsShowToast);
}

@Override
Expand Down
17 changes: 11 additions & 6 deletions lib/src/main/java/com/kcode/lib/dialog/UpdateDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ public class UpdateDialog extends AbstractFragment implements View.OnClickListen
private UpdateActivity mActivity;
protected VersionModel mModel;
protected String mToastMsg;
protected boolean mIsShowToast;

public static UpdateDialog newInstance(VersionModel model, String toastMsg) {
public static UpdateDialog newInstance(VersionModel model, String toastMsg,boolean isShowToast) {

Bundle args = new Bundle();
args.putSerializable(Constant.MODEL, model);
args.putString(Constant.TOAST_MSG, toastMsg);
args.putBoolean(Constant.IS_SHOW_TOAST_MSG,isShowToast);
UpdateDialog fragment = new UpdateDialog();
fragment.setArguments(args);
return fragment;
Expand All @@ -42,6 +44,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mModel = (VersionModel) getArguments().getSerializable(Constant.MODEL);
mToastMsg = getArguments().getString(Constant.TOAST_MSG);
mIsShowToast = getArguments().getBoolean(Constant.IS_SHOW_TOAST_MSG);
closeIfNoNewVersionUpdate();
}

Expand All @@ -66,19 +69,21 @@ private void closeIfNoNewVersionUpdate() {

private String getContent() {
StringBuilder sb = new StringBuilder();
sb.append("版本号:")
sb.append(getActivity().getResources().getString(R.string.update_lib_version_code))
.append(mModel.getVersionName())
.append("\n")
.append("\n")
.append("更新内容:")
.append(getActivity().getResources().getString(R.string.update_lib_update_content))
.append("\n")
.append(mModel.getContent().replaceAll("#", "\\\n"));
return sb.toString();
}

private void isLatest() {
ToastUtils.show(getActivity(),
TextUtils.isEmpty(mToastMsg) ? getResources().getString(R.string.update_lib_default_toast) : mToastMsg);
if (mIsShowToast) {
ToastUtils.show(getActivity(),
TextUtils.isEmpty(mToastMsg) ? getResources().getString(R.string.update_lib_default_toast) : mToastMsg);
}
}

@Override
Expand Down Expand Up @@ -119,7 +124,7 @@ protected void setContent(View view, int contentId) {
}

protected void initIfMustUpdate(View view, int id) {
if (mModel.isMustUpdate()) {
if (PackageUtils.getVersionCode(mActivity.getApplicationContext()) < mModel.getMinSupport()) {
view.findViewById(id).setVisibility(View.GONE);
PublicFunctionUtils.setLastCheckTime(getActivity().getApplicationContext(), 0);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/main/java/com/kcode/lib/utils/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.support.v4.content.FileProvider;

import java.io.File;
Expand All @@ -16,7 +15,8 @@
public class FileUtils {

public static String getApkFilePath(Context context,String downLoadUrl) {
String filePath = Environment.getExternalStorageDirectory().getAbsolutePath();
File externalFile = context.getExternalFilesDir(null);
String filePath = externalFile.getAbsolutePath();
String fileName;
if (downLoadUrl.endsWith(".apk")) {
int index = downLoadUrl.lastIndexOf("/");
Expand Down
Loading

0 comments on commit da05802

Please sign in to comment.