Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	app/src/main/java/com/lwy/appsmartupdate/MainActivity.java
  • Loading branch information
itlwy committed Jan 11, 2020
2 parents 2271a39 + 36be822 commit 26bf805
Show file tree
Hide file tree
Showing 8 changed files with 275 additions and 32 deletions.
114 changes: 111 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
- [x] 支持外部注入网络框架(库默认使用okhttp)
- [x] 支持前台或后台自动更新
- [x] 支持基于版本的强制更新
- [ ] 支持对外定制提示界面
- [ ] 支持暂停、多线程断点下载
- [x] 支持对外定制更新提示和更新进度界面
- [ ] 记忆下载
- [x] 含发布功能后台服务端[github](https://github.com/itlwy/App-Update-Server) (Node.js实现)

## 流程图
Expand Down Expand Up @@ -58,7 +58,7 @@ Add the dependency

```
dependencies {
implementation 'com.github.itlwy:AppSmartUpdate:v1.0.5'
implementation 'com.github.itlwy:AppSmartUpdate:v1.0.6'
}
```
Expand Down Expand Up @@ -227,6 +227,114 @@ public interface IHttpManager {
}
```

### 定制更新交互界面

每个应用的风格都可能是不一样的,因此这里也支持自定义弹出的提示框和进度框,详细见如下代码示例:

1. 初始化config时需要将内部默认的弹框屏蔽掉

```java
public class MyApplication extends Application {

@Override
public void onCreate() {
super.onCreate();
Config config = new Config.Builder()
.isShowInternalDialog(false)
.build(this);
UpdateManager.getInstance().init(config);
}
}
```

2. 自定义对话框,如下(详细代码在MainActivity.java里):

```java
public void registerUpdateCallbak() {
mCallback = new IUpdateCallback() {
@Override
public void noNewApp() {
Toast.makeText(MainActivity.this, "当前已是最新版本!", Toast.LENGTH_LONG).show();
}

@Override
public void hasNewApp(AppUpdateModel appUpdateModel, UpdateManager updateManager, final int updateMethod) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
mDialog = builder.setTitle("自动更新提示")
.setMessage(appUpdateModel.getTip())
.setPositiveButton("更新", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
UpdateManager.getInstance().startUpdate(updateMethod);
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
}).create();
mDialog.show();
}

@Override
public void beforeUpdate() {
// 更新开始
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setTitle("更新中...");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setMessage("正在玩命更新中...");
mProgressDialog.setMax(100);
mProgressDialog.setProgress(0);
mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// 退到后台自动更新,进度由通知栏显示
if (UpdateManager.getInstance().isRunning()) {
UpdateManager.getInstance().onBackgroundTrigger();
}
}
});
mProgressDialog.show();
}

@Override
public void onProgress(int percent, long totalLength, int patchIndex, int patchCount) {
String tip;
if (patchCount > 0) {
tip = String.format("正在下载补丁%d/%d", patchIndex, patchCount);
} else {
tip = "正在下载更新中...";
}
mProgressDialog.setProgress(percent);
mProgressDialog.setMessage(tip);
}

@Override
public void onCompleted() {
mProgressDialog.dismiss();
}

@Override
public void onError(String error) {
Toast.makeText(MainActivity.this, error, Toast.LENGTH_LONG).show();
mProgressDialog.dismiss();
}

@Override
public void onCancelUpdate() {

}

@Override
public void onBackgroundTrigger() {
Toast.makeText(MainActivity.this, "转为后台更新,进度由通知栏提示!", Toast.LENGTH_LONG).show();
}
};
UpdateManager.getInstance().register(mCallback);
}
```



### 差分包合成(jni)
Expand Down
66 changes: 60 additions & 6 deletions app/src/main/java/com/lwy/appsmartupdate/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package com.lwy.appsmartupdate;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;

import com.lwy.smartupdate.UpdateManager;
import com.lwy.smartupdate.api.IUpdateCallback;
import com.lwy.smartupdate.data.AppUpdateModel;

public class MainActivity extends AppCompatActivity {

Expand All @@ -23,6 +29,8 @@ public class MainActivity extends AppCompatActivity {
// private String manifestJsonUrl = "http://192.168.2.107:8000/app/UpdateManifest.json";
private IUpdateCallback mCallback;
private TextView mVersionTV;
private ProgressDialog mProgressDialog;
private AlertDialog mDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -31,39 +39,85 @@ protected void onCreate(Bundle savedInstanceState) {
mVersionTV = (TextView) findViewById(R.id.version_tv);
mVersionTV.setText(BuildConfig.VERSION_CODE + "");
checkAndroidOUnknowSource();
// registerUpdateCallbak(); // 需要自定义弹框时打开注释
checkUpdate();

}

private void checkUpdate() {
UpdateManager.getInstance().update(this, manifestJsonUrl, null);
UpdateManager.getInstance().update(this, manifestJsonUrl);
}


public void registerUpdateCallbak() {
mCallback = new IUpdateCallback() {
@Override
public void noNewApp() {
Toast.makeText(MainActivity.this, "当前已是最新版本!", Toast.LENGTH_LONG).show();
}

@Override
public void hasNewApp(AppUpdateModel appUpdateModel, UpdateManager updateManager, final int updateMethod) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
mDialog = builder.setTitle("自动更新提示")
.setMessage(appUpdateModel.getTip())
.setPositiveButton("更新", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
UpdateManager.getInstance().startUpdate(updateMethod);
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
}).create();
mDialog.show();
}

@Override
public void beforeUpdate() {

// 更新开始
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setTitle("更新中...");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setMessage("正在玩命更新中...");
mProgressDialog.setMax(100);
mProgressDialog.setProgress(0);
mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// 退到后台自动更新,进度由通知栏显示
if (UpdateManager.getInstance().isRunning()) {
UpdateManager.getInstance().onBackgroundTrigger();
}
}
});
mProgressDialog.show();
}

@Override
public void onProgress(int percent, long totalLength, int patchIndex, int patchCount) {

String tip;
if (patchCount > 0) {
tip = String.format("正在下载补丁%d/%d", patchIndex, patchCount);
} else {
tip = "正在下载更新中...";
}
mProgressDialog.setProgress(percent);
mProgressDialog.setMessage(tip);
}

@Override
public void onCompleted() {

mProgressDialog.dismiss();
}

@Override
public void onError(String error) {

Toast.makeText(MainActivity.this, error, Toast.LENGTH_LONG).show();
mProgressDialog.dismiss();
}

@Override
Expand All @@ -73,7 +127,7 @@ public void onCancelUpdate() {

@Override
public void onBackgroundTrigger() {

Toast.makeText(MainActivity.this, "转为后台更新,进度由通知栏提示!", Toast.LENGTH_LONG).show();
}
};
UpdateManager.getInstance().register(mCallback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public void onCreate() {
super.onCreate();
Config config = new Config.Builder()
.isDebug(true)
// .isShowInternalDialog(false) // 需要自定义弹框时打开注释
.httpManager(new OkhttpManager())
.build(this);
UpdateManager.getInstance().init(config);
Expand Down
14 changes: 14 additions & 0 deletions smartupdate/src/main/java/com/lwy/smartupdate/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ public class Config {
private boolean isDebug;
private String updateDirPath;
private boolean isOnlyWifi;
private boolean isShowDialog;
private IHttpManager httpManager;

private Config(Builder builder) {
isDebug = builder.isDebug;
updateDirPath = builder.updateDirPath;
isOnlyWifi = builder.isOnlyWifi;
isShowDialog = builder.isShowDialog;
httpManager = builder.httpManager;
if (httpManager == null)
httpManager = new OkhttpManager();
Expand All @@ -40,6 +42,10 @@ public boolean isOnlyWifi() {
return isOnlyWifi;
}

public boolean isShowDialog() {
return isShowDialog;
}

public IHttpManager getHttpManager() {
return httpManager;
}
Expand All @@ -48,6 +54,7 @@ public static class Builder {
private boolean isDebug;
private String updateDirPath;
private boolean isOnlyWifi;
private boolean isShowDialog = true;
private IHttpManager httpManager;

public Builder() {
Expand All @@ -67,10 +74,17 @@ public Builder isOnlyWifi(boolean flag) {
isOnlyWifi = flag;
return this;
}

public Builder isShowInternalDialog(boolean flag) {
isShowDialog = flag;
return this;
}

public Builder httpManager(IHttpManager httpManager) {
this.httpManager = httpManager;
return this;
}

public Config build(Context context) {
if (TextUtils.isEmpty(updateDirPath))
updateDirPath = context.getExternalFilesDir("update").getAbsolutePath() + "/";
Expand Down
Loading

0 comments on commit 26bf805

Please sign in to comment.