diff --git a/.gitignore b/.gitignore
index 5edb4ee..09b993d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,9 +1,7 @@
*.iml
.gradle
/local.properties
-/.idea/libraries
-/.idea/modules.xml
-/.idea/workspace.xml
+/.idea
.DS_Store
/build
/captures
diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser
deleted file mode 100644
index 39f4cbf..0000000
Binary files a/.idea/caches/build_file_checksums.ser and /dev/null differ
diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml
deleted file mode 100644
index 49f7b87..0000000
--- a/.idea/codeStyles/Project.xml
+++ /dev/null
@@ -1,52 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/compiler.xml b/.idea/compiler.xml
deleted file mode 100644
index a1757ae..0000000
--- a/.idea/compiler.xml
+++ /dev/null
@@ -1,8 +0,0 @@
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/dbnavigator.xml b/.idea/dbnavigator.xml
deleted file mode 100644
index 8095114..0000000
--- a/.idea/dbnavigator.xml
+++ /dev/null
@@ -1,456 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/encodings.xml b/.idea/encodings.xml
deleted file mode 100644
index 97626ba..0000000
--- a/.idea/encodings.xml
+++ /dev/null
@@ -1,6 +0,0 @@
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/gradle.xml b/.idea/gradle.xml
deleted file mode 100644
index d28b75a..0000000
--- a/.idea/gradle.xml
+++ /dev/null
@@ -1,20 +0,0 @@
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/markdown-navigator.xml b/.idea/markdown-navigator.xml
deleted file mode 100644
index 7758d01..0000000
--- a/.idea/markdown-navigator.xml
+++ /dev/null
@@ -1,83 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/markdown-navigator/profiles_settings.xml b/.idea/markdown-navigator/profiles_settings.xml
deleted file mode 100644
index db06266..0000000
--- a/.idea/markdown-navigator/profiles_settings.xml
+++ /dev/null
@@ -1,3 +0,0 @@
-
-
-
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
deleted file mode 100644
index 51fa3e5..0000000
--- a/.idea/misc.xml
+++ /dev/null
@@ -1,44 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml
deleted file mode 100644
index 7f68460..0000000
--- a/.idea/runConfigurations.xml
+++ /dev/null
@@ -1,12 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
deleted file mode 100644
index 35eb1dd..0000000
--- a/.idea/vcs.xml
+++ /dev/null
@@ -1,6 +0,0 @@
-
-
-
-
-
-
\ No newline at end of file
diff --git a/app-updater/src/main/AndroidManifest.xml b/app-updater/src/main/AndroidManifest.xml
index 1a466ab..16bef19 100644
--- a/app-updater/src/main/AndroidManifest.xml
+++ b/app-updater/src/main/AndroidManifest.xml
@@ -11,10 +11,10 @@
-
+
diff --git a/app-updater/src/main/java/com/king/app/updater/AppUpdater.java b/app-updater/src/main/java/com/king/app/updater/AppUpdater.java
index ed8430e..38ee0a0 100644
--- a/app-updater/src/main/java/com/king/app/updater/AppUpdater.java
+++ b/app-updater/src/main/java/com/king/app/updater/AppUpdater.java
@@ -111,6 +111,21 @@ public class AppUpdater {
}
}
+ /**
+ * 停止
+ */
+ public void stop(){
+ stopDownloadService();
+ }
+
+ /**
+ * 停止下载服务
+ */
+ private void stopDownloadService(){
+ Intent intent = new Intent(mContext, DownloadService.class);
+ intent.putExtra(Constants.KEY_STOP_DOWNLOAD_SERVICE,true);
+ mContext.startService(intent);
+ }
/**
* AppUpdater构建器
@@ -296,6 +311,16 @@ public class AppUpdater {
return this;
}
+ /**
+ * 设置是否自动删除取消下载的文件
+ * @param isDeleteCancelFile 是否删除取消下载的文件(默认为true)
+ * @return
+ */
+ public Builder setDeleteCancelFile(boolean isDeleteCancelFile){
+ mConfig.setDeleteCancelFile(isDeleteCancelFile);
+ return this;
+ }
+
public AppUpdater build(@NonNull Context context){
AppUpdater appUpdater = new AppUpdater(context,mConfig);
return appUpdater;
diff --git a/app-updater/src/main/java/com/king/app/updater/UpdateConfig.java b/app-updater/src/main/java/com/king/app/updater/UpdateConfig.java
index 30f55bb..2ecc07e 100644
--- a/app-updater/src/main/java/com/king/app/updater/UpdateConfig.java
+++ b/app-updater/src/main/java/com/king/app/updater/UpdateConfig.java
@@ -87,6 +87,11 @@ public class UpdateConfig implements Parcelable {
*/
private Map mRequestProperty;
+ /**
+ * 是否删除取消下载的文件
+ */
+ private boolean isDeleteCancelFile = true;
+
public UpdateConfig() {
@@ -233,6 +238,15 @@ public class UpdateConfig implements Parcelable {
}
+ public boolean isDeleteCancelFile() {
+ return isDeleteCancelFile;
+ }
+
+ public void setDeleteCancelFile(boolean deleteCancelFile) {
+ isDeleteCancelFile = deleteCancelFile;
+ }
+
+
@Override
public int describeContents() {
return 0;
@@ -255,14 +269,14 @@ public class UpdateConfig implements Parcelable {
dest.writeByte(this.isVibrate ? (byte) 1 : (byte) 0);
dest.writeByte(this.isSound ? (byte) 1 : (byte) 0);
dest.writeValue(this.versionCode);
- dest.writeInt(mRequestProperty!=null ? this.mRequestProperty.size():0);
+ dest.writeInt(mRequestProperty!=null ? this.mRequestProperty.size() : 0);
if(mRequestProperty!=null){
for (Map.Entry entry : this.mRequestProperty.entrySet()) {
dest.writeString(entry.getKey());
dest.writeString(entry.getValue());
}
}
-
+ dest.writeByte(this.isDeleteCancelFile ? (byte) 1 : (byte) 0);
}
protected UpdateConfig(Parcel in) {
@@ -288,6 +302,7 @@ public class UpdateConfig implements Parcelable {
String value = in.readString();
this.mRequestProperty.put(key, value);
}
+ this.isDeleteCancelFile = in.readByte() != 0;
}
public static final Creator CREATOR = new Creator() {
diff --git a/app-updater/src/main/java/com/king/app/updater/http/HttpManager.java b/app-updater/src/main/java/com/king/app/updater/http/HttpManager.java
index 95a3b2a..cf89805 100644
--- a/app-updater/src/main/java/com/king/app/updater/http/HttpManager.java
+++ b/app-updater/src/main/java/com/king/app/updater/http/HttpManager.java
@@ -27,6 +27,8 @@ public class HttpManager implements IHttpManager {
private int mTimeout;
+ private boolean isCancel;
+
private static HttpManager INSTANCE;
public static HttpManager getInstance(){
@@ -49,9 +51,14 @@ public class HttpManager implements IHttpManager {
@Override
public void download(String url, String path, String filename, @Nullable Map requestProperty, DownloadCallback callback) {
+ isCancel = false;
new DownloadTask(url,path,filename,requestProperty,callback).execute();
}
+ @Override
+ public void cancel() {
+ isCancel = true;
+ }
/**
* 异步下载任务
@@ -118,6 +125,10 @@ public class HttpManager implements IHttpManager {
File file = new File(path,filename);
FileOutputStream fos = new FileOutputStream(file);
while ((len = is.read(buffer)) != -1){
+ if(isCancel){
+ cancel(true);
+ break;
+ }
fos.write(buffer,0,len);
progress += len;
//更新进度
@@ -130,6 +141,10 @@ public class HttpManager implements IHttpManager {
connect.disconnect();
+ if(isCancelled()){
+ return null;
+ }
+
return file;
}else {//连接失败
throw new ConnectException(String.format("responseCode = %d",responseCode));
@@ -168,7 +183,10 @@ public class HttpManager implements IHttpManager {
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
if(callback!=null){
- callback.onProgress(values[0],values[1]);
+ if(!isCancelled()){
+ callback.onProgress(values[0],values[1]);
+ }
+
}
}
diff --git a/app-updater/src/main/java/com/king/app/updater/http/IHttpManager.java b/app-updater/src/main/java/com/king/app/updater/http/IHttpManager.java
index a927a5f..8d8e724 100644
--- a/app-updater/src/main/java/com/king/app/updater/http/IHttpManager.java
+++ b/app-updater/src/main/java/com/king/app/updater/http/IHttpManager.java
@@ -22,6 +22,10 @@ public interface IHttpManager {
*/
void download(String url, String path, String filename, @Nullable Map requestProperty, DownloadCallback callback);
+ /**
+ * 取消下载
+ */
+ void cancel();
interface DownloadCallback extends Serializable{
/**
diff --git a/app-updater/src/main/java/com/king/app/updater/service/DownloadService.java b/app-updater/src/main/java/com/king/app/updater/service/DownloadService.java
index dce850c..02e7f7e 100644
--- a/app-updater/src/main/java/com/king/app/updater/service/DownloadService.java
+++ b/app-updater/src/main/java/com/king/app/updater/service/DownloadService.java
@@ -54,6 +54,10 @@ public class DownloadService extends Service {
*/
private int mCount = 0;
+ private IHttpManager mHttpManager;
+
+ private File mFile;
+
private Context getContext(){
return this;
}
@@ -62,20 +66,19 @@ public class DownloadService extends Service {
public int onStartCommand(Intent intent, int flags, int startId) {
if(intent != null){
- if(!isDownloading){
- boolean isStop = intent.getBooleanExtra(Constants.KEY_STOP_DOWNLOAD_SERVICE,false);
- if(isStop){
- stopService();
- }else{
- //是否实通过通知栏触发重复下载
- boolean isReDownload = intent.getBooleanExtra(Constants.KEY_RE_DOWNLOAD,false);
- if(isReDownload){
- mCount++;
- }
- //获取配置信息
- UpdateConfig config = intent.getParcelableExtra(Constants.KEY_UPDATE_CONFIG);
- startDownload(config,null,null);
+
+ boolean isStop = intent.getBooleanExtra(Constants.KEY_STOP_DOWNLOAD_SERVICE,false);
+ if(isStop){
+ stopDownload();
+ } else if(!isDownloading){
+ //是否实通过通知栏触发重复下载
+ boolean isReDownload = intent.getBooleanExtra(Constants.KEY_RE_DOWNLOAD,false);
+ if(isReDownload){
+ mCount++;
}
+ //获取配置信息
+ UpdateConfig config = intent.getParcelableExtra(Constants.KEY_UPDATE_CONFIG);
+ startDownload(config,null,null);
}else{
Log.w(Constants.TAG,"Please do not repeat the download.");
}
@@ -127,23 +130,23 @@ public class DownloadService extends Service {
filename = AppUtils.INSTANCE.getAppFullName(getContext(),url,getResources().getString(R.string.app_name));
}
- File file = new File(path,filename);
- if(file.exists()){//文件是否存在
+ mFile = new File(path,filename);
+ if(mFile.exists()){//文件是否存在
Integer versionCode = config.getVersionCode();
if(versionCode!=null){
try{
- if(AppUtils.INSTANCE.apkExists(getContext(),versionCode,file)){
+ if(AppUtils.INSTANCE.apkExists(getContext(),versionCode,mFile)){
//本地已经存在要下载的APK
- Log.d(Constants.TAG,"CacheFile:" + file);
+ Log.d(Constants.TAG,"CacheFile:" + mFile);
if(config.isInstallApk()){
String authority = config.getAuthority();
if(TextUtils.isEmpty(authority)){//如果为空则默认
authority = getContext().getPackageName() + ".fileProvider";
}
- AppUtils.INSTANCE.installApk(getContext(),file,authority);
+ AppUtils.INSTANCE.installApk(getContext(),mFile,authority);
}
if(callback!=null){
- callback.onFinish(file);
+ callback.onFinish(mFile);
}
stopService();
return;
@@ -153,17 +156,28 @@ public class DownloadService extends Service {
}
}
//删除旧文件
- file.delete();
+ mFile.delete();
}
- Log.d(Constants.TAG,"File:" + file);
+ Log.d(Constants.TAG,"File:" + mFile);
+
if(httpManager != null){
- httpManager.download(url,path,filename,config.getRequestProperty(),new AppDownloadCallback(config,callback));
+ mHttpManager = httpManager;
}else{
- HttpManager.getInstance().download(url,path,filename,config.getRequestProperty(),new AppDownloadCallback(config,callback));
+ mHttpManager = HttpManager.getInstance();
}
+ mHttpManager.download(url,path,filename,config.getRequestProperty(),new AppDownloadCallback(config,callback));
}
+ /**
+ * 停止下载
+ */
+ public void stopDownload(){
+ if(mHttpManager!=null){
+ mHttpManager.cancel();
+ }
+ }
+
/**
* 获取缓存路径
* @param context
@@ -326,6 +340,9 @@ public class DownloadService extends Service {
if(callback!=null){
callback.onCancel();
}
+ if(mFile!=null){
+ mFile.delete();
+ }
stopService();
}
}
diff --git a/app/src/main/java/com/king/appupdater/MainActivity.java b/app/src/main/java/com/king/appupdater/MainActivity.java
index e572638..d5fec7e 100644
--- a/app/src/main/java/com/king/appupdater/MainActivity.java
+++ b/app/src/main/java/com/king/appupdater/MainActivity.java
@@ -34,6 +34,8 @@ public class MainActivity extends AppCompatActivity {
private Toast toast;
+ private AppUpdater mAppUpdater;
+
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -64,7 +66,8 @@ public class MainActivity extends AppCompatActivity {
* 简单一键后台升级
*/
private void clickBtn1(){
- new AppUpdater(getContext(),mUrl).start();
+ mAppUpdater = new AppUpdater(getContext(),mUrl);
+ mAppUpdater.start();
}
/**
@@ -74,7 +77,7 @@ public class MainActivity extends AppCompatActivity {
UpdateConfig config = new UpdateConfig();
config.setUrl(mUrl);
config.addHeader("token","xxxxxx");
- new AppUpdater(getContext(),config)
+ mAppUpdater = new AppUpdater(getContext(),config)
.setUpdateCallback(new UpdateCallback() {
@Override
@@ -112,8 +115,8 @@ public class MainActivity extends AppCompatActivity {
public void onCancel() {
progressBar.setVisibility(View.INVISIBLE);
}
- })
- .start();
+ });
+ mAppUpdater.start();
}
/**
@@ -126,7 +129,7 @@ public class MainActivity extends AppCompatActivity {
.setPositiveButton("升级", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
- new AppUpdater.Builder()
+ mAppUpdater = new AppUpdater.Builder()
.serUrl(mUrl)
.build(getContext())
.setUpdateCallback(new AppUpdateCallback() {
@@ -139,8 +142,8 @@ public class MainActivity extends AppCompatActivity {
public void onFinish(File file) {
showToast("下载完成");
}
- })
- .start();
+ });
+ mAppUpdater.start();
}
}).show();
}
@@ -156,7 +159,8 @@ public class MainActivity extends AppCompatActivity {
.setOnClickOk(new View.OnClickListener() {
@Override
public void onClick(View v) {
- new AppUpdater(getContext(),mUrl).start();
+ mAppUpdater = new AppUpdater(getContext(),mUrl);
+ mAppUpdater.start();
AppDialog.INSTANCE.dismissDialog();
}
});
@@ -176,10 +180,10 @@ public class MainActivity extends AppCompatActivity {
.setOnClickOk(new View.OnClickListener() {
@Override
public void onClick(View v) {
- new AppUpdater.Builder()
+ mAppUpdater = new AppUpdater.Builder()
.serUrl(mUrl)
- .build(getContext())
- .start();
+ .build(getContext());
+ mAppUpdater.start();
AppDialog.INSTANCE.dismissDialog();
}
});
@@ -208,14 +212,14 @@ public class MainActivity extends AppCompatActivity {
btnOK.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
- new AppUpdater.Builder()
+ mAppUpdater = new AppUpdater.Builder()
.serUrl(mUrl)
.setPath(Environment.getExternalStorageDirectory() + "/.AppUpdater")
.setVersionCode(BuildConfig.VERSION_CODE)//设置versionCode之后,新版本相同的apk只下载一次,优先取本地缓存。
.setFilename("AppUpdater1.apk")
.setVibrate(true)
- .build(getContext())
- .start();
+ .build(getContext());
+ mAppUpdater.start();
AppDialog.INSTANCE.dismissDialog();
}
});
@@ -234,12 +238,12 @@ public class MainActivity extends AppCompatActivity {
.setOnClickOk(new View.OnClickListener() {
@Override
public void onClick(View v) {
- new AppUpdater.Builder()
+ mAppUpdater = new AppUpdater.Builder()
.serUrl(mUrl)
.setVibrate(true)
.setSound(true)
- .build(getContext())
- .start();
+ .build(getContext());
+ mAppUpdater.start();
AppDialog.INSTANCE.dismissDialogFragment(getSupportFragmentManager());
}
});
@@ -247,6 +251,13 @@ public class MainActivity extends AppCompatActivity {
}
+ private void clickCancel(){
+ if(mAppUpdater != null){
+ mAppUpdater.stop();
+ }
+
+ }
+
public void OnClick(View v){
switch (v.getId()){
case R.id.btn1:
@@ -270,6 +281,9 @@ public class MainActivity extends AppCompatActivity {
case R.id.btn7:
clickBtn7();
break;
+ case R.id.btnCancel:
+ clickCancel();
+ break;
}
}
}
diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml
index 5172e82..2bca222 100644
--- a/app/src/main/res/layout/activity_main.xml
+++ b/app/src/main/res/layout/activity_main.xml
@@ -104,4 +104,17 @@
app:layout_constraintRight_toRightOf="parent"
style="@style/OnClick"/>
+
+
\ No newline at end of file