├── .gitignore ├── README.md ├── _config.yml ├── apk └── sample-debug.apk ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── image ├── 1498810770.png ├── example_01.png ├── example_02.png ├── example_03.png ├── example_04.png ├── example_05.png └── example_06.png ├── java.md ├── json ├── json.txt └── json1.txt ├── kotlin.md ├── sample ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── vector │ │ └── appupdatedemo │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── vector │ │ │ └── appupdatedemo │ │ │ ├── App.java │ │ │ ├── ext │ │ │ ├── ActivityExt.kt │ │ │ ├── CprogressExt.kt │ │ │ └── DialogExt.kt │ │ │ ├── http │ │ │ ├── OkGoUpdateHttpUtil.java │ │ │ └── UpdateAppHttpUtil.java │ │ │ ├── ui │ │ │ ├── JavaActivity.java │ │ │ ├── KotlinActivity.kt │ │ │ └── MainActivity.java │ │ │ └── util │ │ │ ├── CProgressDialogUtils.java │ │ │ └── HProgressDialogUtils.java │ └── res │ │ ├── layout │ │ ├── activity_java.xml │ │ ├── activity_kotlin.xml │ │ └── activity_main.xml │ │ ├── mipmap-hdpi │ │ ├── top_2.png │ │ ├── top_3.png │ │ ├── top_4.png │ │ ├── top_5.png │ │ ├── top_6.png │ │ └── top_7.png │ │ ├── mipmap-xxhdpi │ │ ├── app_update_logo.png │ │ └── top_8.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── vector │ └── appupdatedemo │ └── ExampleUnitTest.java ├── settings.gradle ├── update-app-kotlin ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── vector │ │ └── update_app_kotlin │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ └── kotlin │ │ └── com │ │ └── vector │ │ └── update_app_kotlin │ │ ├── TextViewExt.kt │ │ └── UpdateAppExt.kt │ └── test │ └── java │ └── com │ └── vector │ └── update_app_kotlin │ └── ExampleUnitTest.java ├── update-app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── vector │ │ └── update_app │ │ ├── HttpManager.java │ │ ├── SilenceUpdateCallback.java │ │ ├── UpdateAppBean.java │ │ ├── UpdateAppManager.java │ │ ├── UpdateCallback.java │ │ ├── UpdateDialogFragment.java │ │ ├── UpdateFileProvider.java │ │ ├── listener │ │ ├── ExceptionHandler.java │ │ ├── ExceptionHandlerHelper.java │ │ └── IUpdateDialogFragmentListener.java │ │ ├── service │ │ └── DownloadService.java │ │ ├── utils │ │ ├── AppUpdateUtils.java │ │ ├── ColorUtil.java │ │ ├── DrawableUtil.java │ │ └── Md5Util.java │ │ └── view │ │ └── NumberProgressBar.java │ └── res │ ├── anim │ ├── update_app_window_in.xml │ └── update_app_window_out.xml │ ├── drawable │ └── lib_update_app_info_bg.xml │ ├── layout │ └── lib_update_app_dialog.xml │ ├── mipmap-hdpi │ └── lib_update_app_top_bg.png │ ├── mipmap-xhdpi │ ├── lib_update_app_close.png │ └── lib_update_app_update_icon.png │ ├── values │ ├── attrs.xml │ └── style.xml │ └── xml │ └── new_app_file_paths.xml └── web └── AppVersionManger.rar /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | #*.apk 3 | #*.ap_ 4 | 5 | # Files for the ART/Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | out/ 15 | 16 | # Gradle files 17 | .gradle/ 18 | build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Android Studio Navigation editor temp files 30 | .navigation/ 31 | 32 | # Android Studio captures folder 33 | captures/ 34 | 35 | # Intellij 36 | *.iml 37 | .idea/ 38 | 39 | # Keystore files 40 | *.jks 41 | 42 | 43 | *.iml 44 | .gradle 45 | /local.properties 46 | /.idea/workspace.xml 47 | /.idea/libraries 48 | .DS_Store 49 | /build 50 | /captures 51 | .externalNativeBuild 52 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | ## Android 版本更新 3 | 4 | ## 目录 5 | 6 | * [功能介绍](#功能介绍) 7 | * [效果图与示例 apk](#效果图与示例-apk) 8 | * [Gradle 依赖](#Gradle依赖) 9 | * [简单使用](#简单使用) 10 | * [详细说明](#详细说明) 11 | * [更新日志](#更新日志) 12 | * [License](#license) 13 | 14 | ## 功能介绍 15 | 16 | - [x] 实现android版本更新 17 | - [x] 对kotlin适配,调用更简单 18 | - [x] 自定义接口协议,可以不改变现有项目的协议就能使用 19 | - [x] 支持get,post请求 20 | - [x] 支持进度显示,对话框进度条,和通知栏进度条展示 21 | - [x] 支持后台下载 22 | - [x] 支持强制更新 23 | - [x] 支持简单主题色配置(可以自动从顶部图片提取主色) 24 | - [x] 支持自定义对话框(可以监听下载进度) 25 | - [x] 支持静默下载(可以设置wifi状态下) 26 | - [x] 支持android7.0 27 | 28 | ## 效果图与示例 apk 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | [点击下载 Demo.apk](https://raw.githubusercontent.com/WVector/AppUpdateDemo/master/apk/sample-debug.apk) 或扫描下面的二维码安装 42 | 43 | ![Demo apk文件二维](https://raw.githubusercontent.com/WVector/AppUpdateDemo/master/image/1498810770.png) 44 | 45 | 46 | 47 | ## Gradle 依赖 48 | 49 | **java方式引用** 50 | 51 | ```gradle 52 | dependencies { 53 | compile 'com.qianwen:update-app:3.5.2' 54 | } 55 | ``` 56 | 57 | [![Download](https://api.bintray.com/packages/qianwen/maven/update-app/images/download.svg) ](https://bintray.com/qianwen/maven/update-app/_latestVersion) [![API](https://img.shields.io/badge/API-14%2B-orange.svg?style=flat)](https://android-arsenal.com/api?level=14) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![GitHub stars](https://img.shields.io/github/stars/WVector/AppUpdate.svg?style=plastic&label=Star) ](https://github.com/WVector/AppUpdate) 58 | 59 | 60 | **kotlin方式引用** 61 | 62 | ```gradle 63 | dependencies { 64 | compile 'com.qianwen:update-app-kotlin:1.2.3' 65 | } 66 | ``` 67 | 68 | [![Download](https://api.bintray.com/packages/qianwen/maven/update-app-kotlin/images/download.svg) ](https://bintray.com/qianwen/maven/update-app/_latestVersion) [![API](https://img.shields.io/badge/API-14%2B-orange.svg?style=flat)](https://android-arsenal.com/api?level=14) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![GitHub stars](https://img.shields.io/github/stars/WVector/AppUpdate.svg?style=plastic&label=Star) ](https://github.com/WVector/AppUpdate) 69 | 70 | 71 | ## 简单使用 72 | 73 | 74 | 75 | 1,java方式 76 | 77 | ```java 78 | new UpdateAppManager 79 | .Builder() 80 | //当前Activity 81 | .setActivity(this) 82 | //更新地址 83 | .setUpdateUrl(mUpdateUrl) 84 | //实现httpManager接口的对象 85 | .setHttpManager(new UpdateAppHttpUtil()) 86 | .build() 87 | .update(); 88 | ``` 89 | 2,kotlin方式 90 | 91 | ```kotlin 92 | updateApp(mUpdateUrl, UpdateAppHttpUtil()).update() 93 | ``` 94 | 95 | ## 详细说明 96 | 97 | - [java方式](java.md) 98 | - [kotlin方式](kotlin.md) 99 | 100 | #### 进度条使用的是代码家的「[NumberProgressBar](https://github.com/daimajia/NumberProgressBar)」 101 | 102 | ## 更新日志 103 | 104 | kotlin版本是依赖java版本的,所以java版本的问题kotlin自然修复 105 | 106 | 107 | v3.5.2 108 | 109 | 1,修复下载过程中,关闭对话框不能自动安装问题。 110 | 111 | v3.5.1 112 | 113 | 1,修复bug 114 | 115 | v3.5.0 116 | 117 | 1,优化强制更新 118 | 119 | v3.4.8 120 | 121 | 1,修复bug 122 | 123 | v3.4.7 124 | 125 | 1,优化 APP 安装的问题 126 | 127 | v3.4.6 128 | 129 | 1,优化 APP 安装的问题 130 | 131 | v3.4.5 132 | 133 | 1,增加全局异常捕获方法 134 | 135 | .handleException(new ExceptionHandler() { 136 | @Override 137 | public void onException(Exception e) { 138 | 139 | } 140 | }) 141 | 142 | v3.4.4 143 | 144 | 1,修复bug 145 | [bug](https://github.com/WVector/AppUpdate/pull/68) 146 | 147 | v3.4.3 148 | 149 | 1,修复bug 150 | [bug](https://github.com/WVector/AppUpdate/pull/67) 151 | 152 | v3.4.2 153 | 154 | 1,修复bug 155 | [bug](https://github.com/WVector/AppUpdate/pull/66) 156 | 157 | v3.4.1 158 | 159 | 1,给插件使用者更多的配置和开启一些钩子方便适配不同的业务需求 160 | 2,适配android8.0 161 | 162 | 感谢[Jiiiiiin](https://github.com/Jiiiiiin)对项目的维护 163 | 164 | v3.4.0 165 | 166 | 1,修复 167 | [issues#59](https://github.com/WVector/AppUpdate/issues/59) 168 | 169 | 170 | 171 | v3.3.9 172 | 173 | 1,适配android8.0的通知和安装未知来源的app 174 | 175 | 感谢[typ0520](https://github.com/typ0520)对项目的维护 176 | 177 | v3.3.8 178 | 179 | 1,增加存储空间权限申请 180 | 181 | V3.3.7 182 | 183 | 1,修改默认安装包下载路径为download/packageName 184 | 185 | 感谢[bean-liu](https://github.com/bean-liu)对项目的维护 186 | 187 | V3.3.6 188 | 189 | 1,去掉对下载路径前缀的校验。 190 | [https://github.com/WVector/AppUpdate/issues/26](https://github.com/WVector/AppUpdate/issues/26) 191 | 192 | V3.3.5 193 | 194 | 1,修复升级对话框布局中的问题。 195 | 2,修复静默下载,关闭更新弹窗 再点击更新 一直显示的问题。 196 | [https://github.com/WVector/AppUpdate/issues/21](https://github.com/WVector/AppUpdate/issues/21) 197 | 198 | V3.3.4 199 | 200 | 1,修复对话框更新内容过多,升级按钮被挤压的问题。 201 | 2,去掉自动从图片提取颜色的功能, 通过.setThemeColor()设置按钮和精度条颜色, 202 | 3,兼容compileSdkVersion <25 203 | 204 | V3.3.3 205 | 206 | 1,修复下载路径是重定向路径不能下载的问题 207 | 208 | V3.3.2 209 | 210 | 1,修复正在下载时,返回桌面报错的问题 211 | [https://github.com/WVector/AppUpdate/issues/14](https://github.com/WVector/AppUpdate/issues/14) 212 | 213 | V3.3.1 214 | 215 | 1,修复对话框外可以点击的问题 216 | 217 | V3.3.0 218 | 219 | 1,可以设置不显示通知栏进度条。 220 | 2,可以设置忽略版本。 221 | 3,优化下载时页面卡的问题(由于下载进度回调调用频繁,造成ui线程阻塞)。 222 | 4,可以静默下载,类似网易云音乐,并且设置wifi状态下。 223 | 224 | V3.2.9 225 | 226 | 1,新增自定义对话框。 227 | 2,适配kotlin,写法更简单。 228 | 229 | 230 | ## License 231 | 232 | Copyright 2017 千匍 233 | 234 | Licensed under the Apache License, Version 2.0 (the "License"); 235 | you may not use this file except in compliance with the License. 236 | You may obtain a copy of the License at 237 | 238 | http://www.apache.org/licenses/LICENSE-2.0 239 | 240 | Unless required by applicable law or agreed to in writing, software 241 | distributed under the License is distributed on an "AS IS" BASIS, 242 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 243 | See the License for the specific language governing permissions and 244 | limitations under the License. -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman -------------------------------------------------------------------------------- /apk/sample-debug.apk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WVector/AppUpdate/3041a62adeec3006f74a0b50728fe3b8c57d1a29/apk/sample-debug.apk -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | ext.kotlin_version = '1.1.4' 5 | ext.novoda_version = '0.4.0' 6 | repositories { 7 | jcenter() 8 | 9 | maven { 10 | url 'https://maven.google.com/' 11 | name 'Google' 12 | } 13 | } 14 | dependencies { 15 | classpath 'com.android.tools.build:gradle:2.3.3' 16 | classpath "com.novoda:bintray-release:$novoda_version" 17 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 18 | // NOTE: Do not place your application dependencies here; they belong 19 | // in the individual module build.gradle files 20 | } 21 | } 22 | allprojects { 23 | repositories { 24 | jcenter() 25 | mavenCentral() 26 | 27 | // maven{url 'https://dl.bintray.com/qianwen/maven/'} 28 | maven { url "https://www.jitpack.io" } 29 | maven { 30 | url 'https://maven.google.com/' 31 | name 'Google' 32 | } 33 | } 34 | tasks.withType(Javadoc) { 35 | options { 36 | encoding "UTF-8" 37 | charSet 'UTF-8' 38 | links "http://docs.oracle.com/javase/7/docs/api" 39 | } 40 | } 41 | } 42 | 43 | ext { 44 | update_app_version = '3.5.2' 45 | update_app_kotlin_version = '1.2.3' 46 | } 47 | 48 | 49 | //防止上传kotlin是报错, 50 | tasks.getByPath(":update-app-kotlin:releaseAndroidJavadocs").enabled = false 51 | task clean(type: Delete) { 52 | delete rootProject.buildDir 53 | } 54 | 55 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WVector/AppUpdate/3041a62adeec3006f74a0b50728fe3b8c57d1a29/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Jun 19 11:15:33 CST 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /image/1498810770.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WVector/AppUpdate/3041a62adeec3006f74a0b50728fe3b8c57d1a29/image/1498810770.png -------------------------------------------------------------------------------- /image/example_01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WVector/AppUpdate/3041a62adeec3006f74a0b50728fe3b8c57d1a29/image/example_01.png -------------------------------------------------------------------------------- /image/example_02.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WVector/AppUpdate/3041a62adeec3006f74a0b50728fe3b8c57d1a29/image/example_02.png -------------------------------------------------------------------------------- /image/example_03.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WVector/AppUpdate/3041a62adeec3006f74a0b50728fe3b8c57d1a29/image/example_03.png -------------------------------------------------------------------------------- /image/example_04.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WVector/AppUpdate/3041a62adeec3006f74a0b50728fe3b8c57d1a29/image/example_04.png -------------------------------------------------------------------------------- /image/example_05.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WVector/AppUpdate/3041a62adeec3006f74a0b50728fe3b8c57d1a29/image/example_05.png -------------------------------------------------------------------------------- /image/example_06.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WVector/AppUpdate/3041a62adeec3006f74a0b50728fe3b8c57d1a29/image/example_06.png -------------------------------------------------------------------------------- /json/json.txt: -------------------------------------------------------------------------------- 1 | { 2 | "update": "Yes", 3 | "new_version": "0.8.3", 4 | "apk_file_url": "https://raw.githubusercontent.com/WVector/AppUpdateDemo/master/apk/sample-debug.apk", 5 | "update_log": "1,添加删除信用卡接口。\r\n2,添加vip认证。\r\n3,区分自定义消费,一个小时不限制。\r\n4,添加放弃任务接口,小时内不生成。\r\n5,消费任务手动生成。", 6 | "target_size": "5M", 7 | "new_md5":"b97bea014531123f94c3ba7b7afbaad2", 8 | "constraint": false 9 | } -------------------------------------------------------------------------------- /json/json1.txt: -------------------------------------------------------------------------------- 1 | { 2 | "update": "Yes", 3 | "new_version": "1.1", 4 | "apk_file_url": "https://raw.githubusercontent.com/WVector/AppUpdateDemo/master/apk/sample-debug.apk", 5 | "update_log": "1,添加删除信用卡接口。\r\n2,添加vip认证。\r\n3,区分自定义消费,一个小时不限制。\r\n4,添加放弃任务接口,小时内不生成。\r\n5,消费任务手动生成。", 6 | "target_size": "5M", 7 | "new_md5":"b97bea014531123f94c3ba7b7afbaad2", 8 | "constraint": true 9 | } -------------------------------------------------------------------------------- /kotlin.md: -------------------------------------------------------------------------------- 1 | 2 | ### 目录 3 | 4 | * [默认接口协议](#默认接口协议) 5 | * [自定义接口协议](#自定义接口协议) 6 | * [自定义接口协议+自定义对话框](#自定义接口协议+自定义对话框) 7 | * [自定义接口协议+自定义对话框+显示进度对话框](#自定义接口协议+自定义对话框+显示进度对话框) 8 | * [静默下载](#静默下载) 9 | * [静默下载+自定义对话框](#静默下载+自定义对话框) 10 | * [实现HttpManager接口](#实现HttpManager接口) 11 | 12 | ### 默认接口协议 13 | 14 | #### 1,Request 请求参数 15 | 16 | GET:[https://raw.githubusercontent.com/WVector/AppUpdateDemo/master/json/json.txt?appKey=ab55ce55Ac4bcP408cPb8c1Aaeac179c5f6f&version=0.1.0](https://raw.githubusercontent.com/WVector/AppUpdateDemo/master/json/json.txt?appKey=ab55ce55Ac4bcP408cPb8c1Aaeac179c5f6f&version=0.1.0) 17 | 18 | 19 | 20 | 1,参数 appkey 21 | app的唯一标志 22 | appkey可以在manifest文件中配置,也可以在代码中添加 23 | xml配置如下: 24 | 25 | ```xml 26 | 27 | 30 | 31 | ``` 32 | 33 | 2,参数 version 34 | 版本号,工具自动添加(服务器判断客户端传过来的version和服务器存的最新的version,决定是否更新) 35 | 36 | ```json 37 | 38 | ``` 39 | 40 | 3, 服务器app后台管理界面 41 | 42 | [点击下载后台代码](https://raw.githubusercontent.com/WVector/AppUpdateDemo/master/web/AppVersionManger.rar) 43 | 44 | 45 | 46 | #### 2, Response 服务器的返回json格式 47 | 1,有新版本 48 | 49 | ```json 50 | 51 | { 52 | "update": "Yes",//有新版本 53 | "new_version": "0.8.3",//新版本号 54 | "apk_file_url": "https://raw.githubusercontent.com/WVector/AppUpdateDemo/master/apk/app-debug.apk", //apk下载地址 55 | "update_log": "1,添加删除信用卡接口\r\n2,添加vip认证\r\n3,区分自定义消费,一个小时不限制。\r\n4,添加放弃任务接口,小时内不生成。\r\n5,消费任务手动生成。",//更新内容 56 | "target_size": "5M",//apk大小 57 | "new_md5":"A818AD325EACC199BC62C552A32C35F2", 58 | "constraint": false//是否强制更新 59 | } 60 | 61 | ``` 62 | 63 | 2,没有新版本 64 | 65 | ```json 66 | 67 | { 68 | "update": "No",//没有新版本 69 | } 70 | 71 | ``` 72 | 73 | 74 | #### 3,客户端检测是否有新版本,并且更新下载 75 | 76 | 和自定义相比,不需要传自定义参数,和实现parseJson方法,其他都一样。 77 | 78 | 79 | ```java 80 | 81 | updateApp(mUpdateUrl, UpdateAppHttpUtil()).update() 82 | 83 | ``` 84 | 85 | ### 自定义接口协议 86 | 87 | 根据自己项目的接口,自己传参数给服务器,实现parseJson方法,解析json,设置新版本app信息。 88 | 89 | 同时可以设置以下功能 90 | 91 | - 请求方式,get,post 92 | - 请求参数 93 | - 是否显示下载进度对话框 94 | - 对话框顶部图片(设置图片后自动识别主色调,然后为按钮,进度条设置颜色) 95 | - 按钮,进度条颜色 96 | - apk的下载路径 97 | - 是否忽略版本 98 | - 是否显示通知栏进度条 99 | 100 | 如果以下的例子出错,请看项目中详细的使用案例 101 | 102 | ```java 103 | 104 | //下载路径 105 | val path = Environment.getExternalStorageDirectory().absolutePath 106 | //自定义参数 107 | val params = HashMap() 108 | params.put("appKey", "ab55ce55Ac4bcP408cPb8c1Aaeac179c5f6f") 109 | params.put("appVersion", AppUpdateUtils.getVersionName(this)) 110 | params.put("key1", "value2") 111 | params.put("key2", "value3") 112 | 113 | updateApp(mUpdateUrl, UpdateAppHttpUtil()) 114 | //自定义配置 115 | { 116 | //以下设置,都是可选 117 | //设置请求方式,默认get 118 | isPost = false 119 | //添加自定义参数,默认version=1.0.0(app的versionName);apkKey=唯一表示(在AndroidManifest.xml配置) 120 | setParams(params) 121 | //设置点击升级后,消失对话框,默认点击升级后,对话框显示下载进度 122 | hideDialogOnDownloading(true) 123 | //设置头部,不设置显示默认的图片,设置图片后自动识别主色调,然后为按钮,进度条设置颜色 124 | topPic = R.mipmap.top_8 125 | //为按钮,进度条设置颜色,默认从顶部图片自动识别。 126 | //setThemeColor(ColorUtil.getRandomColor()) 127 | //设置apk下砸路径,默认是在下载到sd卡下/Download/1.0.0/test.apk 128 | targetPath = path 129 | //设置appKey,默认从AndroidManifest.xml获取,如果,使用自定义参数,则此项无效 130 | //setAppKey("ab55ce55Ac4bcP408cPb8c1Aaeac179c5f6f") 131 | 132 | } 133 | .check { 134 | onBefore { showProgressDialog() } 135 | //自定义解析 136 | parseJson { 137 | val jsonObject = JSONObject(it) 138 | UpdateAppBean() 139 | //(必须)是否更新Yes,No 140 | .setUpdate(jsonObject.optString("update")) 141 | //(必须)新版本号, 142 | .setNewVersion(jsonObject.optString("new_version")) 143 | //(必须)下载地址 144 | .setApkFileUrl(jsonObject.optString("apk_file_url")) 145 | //(必须)更新内容 146 | .setUpdateLog(jsonObject.optString("update_log")) 147 | //大小,不设置不显示大小,可以不设置 148 | .setTargetSize(jsonObject.optString("target_size")) 149 | //是否强制更新,可以不设置 150 | .setConstraint(false) 151 | //设置md5,可以不设置 152 | .setNewMd5(jsonObject.optString("new_md5")) 153 | 154 | } 155 | noNewApp { toast("没有新版本") } 156 | onAfter { cancelProgressDialog() } 157 | } 158 | 159 | 160 | ``` 161 | 162 | ### 自定义接口协议+自定义对话框 163 | 164 | 其他代码和上面一样,只需重写UpdateCallback 的 hasNewApp方法,然后调用自己的对话框 165 | 166 | ```java 167 | 168 | hasNewApp { updateApp, updateAppManager -> 169 | showDiyDialog(updateApp, updateAppManager) 170 | } 171 | 172 | ``` 173 | 174 | 175 | 176 | 下面是简单的对话框,新版本信息从 updateApp 对象获取,updateAppManager 可以控制后台开始下载,下载完自动安装 177 | 178 | 直接调用 'updateAppManager.download();' ,进行下载。 179 | 180 | ```java 181 | 182 | dialog("是否升级到${updateApp.newVersion}版本?" 183 | , "新版本大小:${updateApp.targetSize}\n\n${updateApp.updateLog}") 184 | { 185 | positiveButton("升级") { 186 | updateAppManager.download() 187 | dismiss() 188 | 189 | } 190 | negativeButton("暂不升级") { 191 | dismiss() 192 | } 193 | show() 194 | } 195 | 196 | ``` 197 | 198 | ### 自定义接口协议+自定义对话框+显示进度对话框 199 | 200 | 和上面的例子只有在控制下载有区别,传个回调,监听到下载进度。 201 | 202 | onFinish() 当返回 true :下载完自动跳到安装界面,false:则不进行安装 203 | 204 | ```java 205 | 206 | updateAppManager.download { 207 | onStart { HProgressDialogUtils.showHorizontalProgressDialog(this@KotlinActivity, "下载进度", false) } 208 | onProgress { progress, _ -> HProgressDialogUtils.setProgress(Math.round(progress * 100)) } 209 | onFinish { 210 | HProgressDialogUtils.cancel() 211 | true 212 | } 213 | onError { 214 | toast(it) 215 | HProgressDialogUtils.cancel() 216 | } 217 | } 218 | ``` 219 | 220 | ### 静默下载 221 | 以下是使用默认协议的例子, 222 | ```java 223 | 224 | 225 | updateApp(mUpdateUrl, UpdateAppHttpUtil()) 226 | { 227 | setOnlyWifi() 228 | }.silenceUpdate() 229 | 230 | 231 | 232 | ``` 233 | 234 | ### 静默下载+自定义对话框 235 | 236 | 以下是使用默认协议的例子,也可以使用自定义协议(请参考自定义协议例子) 237 | 238 | ```java 239 | 240 | 241 | updateApp(mUpdateUrl, UpdateAppHttpUtil()) 242 | { 243 | setOnlyWifi() 244 | }.checkNewApp(object : SilenceUpdateCallback() { 245 | override fun showDialog(updateApp: UpdateAppBean, updateAppManager: UpdateAppManager?, appFile: File) { 246 | showSilenceDiyDialog(updateApp, appFile) 247 | } 248 | }) 249 | 250 | 251 | /** 252 | * 自定义对话框 253 | */ 254 | private fun showSilenceDiyDialog(updateApp: UpdateAppBean, appFile: File?) { 255 | dialog("是否升级到${updateApp.newVersion}版本?" 256 | , "新版本大小:${updateApp.targetSize}\n\n${updateApp.updateLog}") 257 | { 258 | positiveButton("升级") { 259 | AppUpdateUtils.installApp(this@KotlinActivity, appFile) 260 | dismiss() 261 | 262 | } 263 | negativeButton("暂不升级") { 264 | dismiss() 265 | } 266 | show() 267 | } 268 | } 269 | 270 | 271 | ``` 272 | 273 | ### 实现HttpManager接口 274 | 275 | 根据自己项目使用的网络框架,自己实现HttpManager接口 276 | 277 | ```java 278 | 279 | class UpdateAppHttpUtil implements HttpManager { 280 | /** 281 | * 异步get 282 | * 283 | * @param url get请求地址 284 | * @param params get参数 285 | * @param callBack 回调 286 | */ 287 | @Override 288 | public void asyncGet(@NonNull String url, @NonNull Map params, @NonNull final Callback callBack) { 289 | OkHttpUtils.get() 290 | .url(url) 291 | .params(params) 292 | .build() 293 | .execute(new StringCallback() { 294 | @Override 295 | public void onError(Call call, Response response, Exception e, int id) { 296 | callBack.onError(validateError(e, response)); 297 | } 298 | 299 | @Override 300 | public void onResponse(String response, int id) { 301 | callBack.onResponse(response); 302 | } 303 | }); 304 | } 305 | 306 | /** 307 | * 异步post 308 | * 309 | * @param url post请求地址 310 | * @param params post请求参数 311 | * @param callBack 回调 312 | */ 313 | @Override 314 | public void asyncPost(@NonNull String url, @NonNull Map params, @NonNull final Callback callBack) { 315 | OkHttpUtils.post() 316 | .url(url) 317 | .params(params) 318 | .build() 319 | .execute(new StringCallback() { 320 | @Override 321 | public void onError(Call call, Response response, Exception e, int id) { 322 | callBack.onError(validateError(e, response)); 323 | } 324 | 325 | @Override 326 | public void onResponse(String response, int id) { 327 | callBack.onResponse(response); 328 | } 329 | }); 330 | 331 | } 332 | 333 | /** 334 | * 下载 335 | * 336 | * @param url 下载地址 337 | * @param path 文件保存路径 338 | * @param fileName 文件名称 339 | * @param callback 回调 340 | */ 341 | @Override 342 | public void download(@NonNull String url, @NonNull String path, @NonNull String fileName, @NonNull final FileCallback callback) { 343 | OkHttpUtils.get() 344 | .url(url) 345 | .build() 346 | .execute(new FileCallBack(path, fileName) { 347 | @Override 348 | public void inProgress(float progress, long total, int id) { 349 | super.inProgress(progress, total, id); 350 | callback.onProgress(progress, total); 351 | } 352 | 353 | @Override 354 | public void onError(Call call, Response response, Exception e, int id) { 355 | callback.onError(validateError(e, response)); 356 | } 357 | 358 | @Override 359 | public void onResponse(File response, int id) { 360 | callback.onResponse(response); 361 | 362 | } 363 | 364 | @Override 365 | public void onBefore(Request request, int id) { 366 | super.onBefore(request, id); 367 | callback.onBefore(); 368 | } 369 | }); 370 | 371 | } 372 | } 373 | 374 | ``` 375 | 376 | -------------------------------------------------------------------------------- /sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android-extensions' 3 | apply plugin: 'kotlin-android' 4 | android { 5 | compileSdkVersion 26 6 | buildToolsVersion "25.0.3" 7 | defaultConfig { 8 | applicationId "com.vector.appupdatedemo" 9 | minSdkVersion 14 10 | targetSdkVersion 26 11 | versionCode 1 12 | versionName "0.1.0" 13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 14 | } 15 | buildTypes { 16 | release { 17 | minifyEnabled false 18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 19 | } 20 | } 21 | lintOptions { 22 | abortOnError false 23 | } 24 | } 25 | 26 | dependencies { 27 | 28 | compile fileTree(include: ['*.jar'], dir: 'libs') 29 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 30 | exclude group: 'com.android.support', module: 'support-annotations' 31 | }) 32 | compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" 33 | 34 | // compile project(':update-app') 35 | compile project(':update-app-kotlin') 36 | 37 | //compile 'com.qianwen:update-app-kotlin:1.1.0' 38 | //okgo 39 | //rxjava 1 40 | compile 'com.android.support:palette-v7:26.1.0' 41 | //权限引导 42 | compile 'com.android.support:appcompat-v7:26.1.0' 43 | 44 | compile 'com.android.support.constraint:constraint-layout:1.0.2' 45 | 46 | compile 'com.qianwen:okhttp-utils:3.8.0' 47 | 48 | compile 'com.lzy.net:okgo:3.0.4' 49 | 50 | compile 'io.reactivex:rxjava:1.2.9' 51 | 52 | compile 'io.reactivex:rxandroid:1.2.0' 53 | compile 'com.tbruyelle.rxpermissions:rxpermissions:0.9.4@aar' 54 | testCompile 'junit:junit:4.12' 55 | } 56 | -------------------------------------------------------------------------------- /sample/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\code\AndroidStudio\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /sample/src/androidTest/java/com/vector/appupdatedemo/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.vector.appupdatedemo; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.assertEquals; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.vector.appupdatedemo", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 18 | 19 | 22 | 23 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /sample/src/main/java/com/vector/appupdatedemo/App.java: -------------------------------------------------------------------------------- 1 | package com.vector.appupdatedemo; 2 | 3 | import android.app.Application; 4 | 5 | import com.lzy.okgo.OkGo; 6 | import com.zhy.http.okhttp.OkHttpUtils; 7 | 8 | /** 9 | * Created by Vector 10 | * on 2017/7/17 0017. 11 | */ 12 | 13 | public class App extends Application { 14 | @Override 15 | public void onCreate() { 16 | super.onCreate(); 17 | 18 | 19 | OkHttpUtils.getInstance() 20 | .init(this) 21 | .debug(true, "okHttp") 22 | .timeout(20 * 1000); 23 | 24 | 25 | OkGo.getInstance().init(this); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /sample/src/main/java/com/vector/appupdatedemo/ext/ActivityExt.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Paweł Gajda 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | @file:Suppress("NOTHING_TO_INLINE") 18 | 19 | package com.vector.appupdatedemo.ext 20 | 21 | import android.app.Activity 22 | import android.support.annotation.IdRes 23 | import android.support.annotation.StringRes 24 | import android.view.View 25 | import android.widget.Toast 26 | 27 | inline fun Activity.find(@IdRes id: Int): T = findViewById(id) 28 | 29 | inline fun Activity.toast(text: CharSequence): Unit = Toast.makeText(this, text, Toast.LENGTH_SHORT).show() 30 | 31 | inline fun Activity.longToast(text: CharSequence): Unit = Toast.makeText(this, text, Toast.LENGTH_LONG).show() 32 | 33 | inline fun Activity.toast(@StringRes resId: Int): Unit = Toast.makeText(this, resId, Toast.LENGTH_SHORT).show() 34 | 35 | inline fun Activity.longToast(@StringRes resId: Int): Unit = Toast.makeText(this, resId, Toast.LENGTH_LONG).show() 36 | -------------------------------------------------------------------------------- /sample/src/main/java/com/vector/appupdatedemo/ext/CprogressExt.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("NOTHING_TO_INLINE") 2 | 3 | package com.vector.appupdatedemo.ext 4 | 5 | import android.app.Activity 6 | import com.vector.appupdatedemo.util.CProgressDialogUtils 7 | 8 | /** 9 | * Created by Vector 10 | * on 2017/7/18 0018. 11 | */ 12 | inline fun Activity.showProgressDialog() { 13 | CProgressDialogUtils.showProgressDialog(this) 14 | } 15 | 16 | inline fun Activity.cancelProgressDialog() { 17 | CProgressDialogUtils.cancelProgressDialog(this) 18 | } -------------------------------------------------------------------------------- /sample/src/main/java/com/vector/appupdatedemo/ext/DialogExt.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Paweł Gajda 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.vector.appupdatedemo.ext 18 | 19 | import android.app.ProgressDialog 20 | import android.content.Context 21 | import android.content.DialogInterface 22 | import android.database.Cursor 23 | import android.graphics.drawable.Drawable 24 | import android.support.annotation.ArrayRes 25 | import android.support.annotation.DrawableRes 26 | import android.support.annotation.StringRes 27 | import android.support.v4.app.Fragment 28 | import android.support.v7.app.AlertDialog 29 | import android.view.KeyEvent 30 | import android.view.View 31 | import android.widget.ListAdapter 32 | 33 | 34 | fun Fragment.dialog( 35 | title: String? = null, 36 | message: String? = null, 37 | init: (KAlertDialogBuilder.() -> Unit)? = null 38 | ) = activity.dialog(title, message, init) 39 | 40 | fun Context.dialog( 41 | title: String? = null, 42 | message: String? = null, 43 | init: (KAlertDialogBuilder.() -> Unit)? = null 44 | ) = KAlertDialogBuilder(this).apply { 45 | if (title != null) title(title) 46 | if (message != null) message(message) 47 | if (init != null) init() 48 | } 49 | 50 | fun Fragment.dialog( 51 | @StringRes message: Int, 52 | @StringRes title: Int? = null, 53 | init: (KAlertDialogBuilder.() -> Unit)? = null 54 | ) = activity.dialog(message, title, init) 55 | 56 | fun Context.dialog( 57 | @StringRes message: Int, 58 | @StringRes title: Int? = null, 59 | init: (KAlertDialogBuilder.() -> Unit)? = null 60 | ) = KAlertDialogBuilder(this).apply { 61 | if (title != null) title(title) 62 | message(message) 63 | if (init != null) init() 64 | } 65 | 66 | fun Fragment.dialog(init: KAlertDialogBuilder.() -> Unit): KAlertDialogBuilder = activity.dialog(init) 67 | 68 | fun Context.dialog(init: KAlertDialogBuilder.() -> Unit) = KAlertDialogBuilder(this).apply { init() } 69 | 70 | fun Fragment.progressDialog( 71 | @StringRes message: Int? = null, 72 | @StringRes title: Int? = null, 73 | init: (ProgressDialog.() -> Unit)? = null 74 | ) = activity.progressDialog(message, title, init) 75 | 76 | fun Context.progressDialog( 77 | @StringRes message: Int? = null, 78 | @StringRes title: Int? = null, 79 | init: (ProgressDialog.() -> Unit)? = null 80 | ) = progressDialog(false, message?.let { getString(it) }, title?.let { getString(it) }, init) 81 | 82 | fun Fragment.indeterminateProgressDialog( 83 | @StringRes message: Int? = null, 84 | @StringRes title: Int? = null, 85 | init: (ProgressDialog.() -> Unit)? = null 86 | ) = activity.progressDialog(message, title, init) 87 | 88 | fun Context.indeterminateProgressDialog( 89 | @StringRes message: Int? = null, 90 | @StringRes title: Int? = null, 91 | init: (ProgressDialog.() -> Unit)? = null 92 | ) = progressDialog(true, message?.let { getString(it) }, title?.let { getString(it) }, init) 93 | 94 | fun Fragment.progressDialog( 95 | message: String? = null, 96 | title: String? = null, 97 | init: (ProgressDialog.() -> Unit)? = null 98 | ) = activity.progressDialog(message, title, init) 99 | 100 | fun Context.progressDialog( 101 | message: String? = null, 102 | title: String? = null, 103 | init: (ProgressDialog.() -> Unit)? = null 104 | ) = progressDialog(false, message, title, init) 105 | 106 | fun Fragment.indeterminateProgressDialog( 107 | message: String? = null, 108 | title: String? = null, 109 | init: (ProgressDialog.() -> Unit)? = null 110 | ) = activity.indeterminateProgressDialog(message, title, init) 111 | 112 | fun Context.indeterminateProgressDialog( 113 | message: String? = null, 114 | title: String? = null, 115 | init: (ProgressDialog.() -> Unit)? = null 116 | ) = progressDialog(true, message, title, init) 117 | 118 | private fun Context.progressDialog( 119 | indeterminate: Boolean, 120 | message: String? = null, 121 | title: String? = null, 122 | init: (ProgressDialog.() -> Unit)? = null 123 | ) = ProgressDialog(this).apply { 124 | isIndeterminate = indeterminate 125 | if (!indeterminate) setProgressStyle(ProgressDialog.STYLE_HORIZONTAL) 126 | if (message != null) setMessage(message) 127 | if (title != null) setTitle(title) 128 | if (init != null) init() 129 | show() 130 | } 131 | 132 | fun Fragment.sheet( 133 | title: CharSequence? = null, 134 | items: List, 135 | onClick: (Int) -> Unit 136 | ) = activity.sheet(title, items, onClick) 137 | 138 | fun Context.sheet( 139 | title: CharSequence? = null, 140 | items: List, 141 | onClick: (Int) -> Unit 142 | ) { 143 | with(KAlertDialogBuilder(this)) { 144 | if (title != null) title(title) 145 | items(items, onClick) 146 | show() 147 | } 148 | } 149 | 150 | class KAlertDialogBuilder(val ctx: Context) { 151 | 152 | val builder: AlertDialog.Builder = AlertDialog.Builder(ctx) 153 | var dialog: AlertDialog? = null 154 | 155 | fun dismiss() = dialog?.dismiss() 156 | 157 | fun show(): KAlertDialogBuilder { 158 | dialog = builder.create() 159 | dialog!!.show() 160 | return this 161 | } 162 | 163 | fun title(title: CharSequence) { 164 | builder.setTitle(title) 165 | } 166 | 167 | fun title(@StringRes resource: Int) { 168 | builder.setTitle(resource) 169 | } 170 | 171 | fun message(title: CharSequence) { 172 | builder.setMessage(title) 173 | } 174 | 175 | fun message(@StringRes resource: Int) { 176 | builder.setMessage(resource) 177 | } 178 | 179 | fun icon(@DrawableRes icon: Int) { 180 | builder.setIcon(icon) 181 | } 182 | 183 | fun icon(icon: Drawable) { 184 | builder.setIcon(icon) 185 | } 186 | 187 | fun customTitle(title: View) { 188 | builder.setCustomTitle(title) 189 | } 190 | 191 | fun customView(view: View) { 192 | builder.setView(view) 193 | } 194 | 195 | fun cancellable(value: Boolean = true) { 196 | builder.setCancelable(value) 197 | } 198 | 199 | fun onCancel(f: () -> Unit) { 200 | builder.setOnCancelListener { f() } 201 | } 202 | 203 | fun onKey(f: (keyCode: Int, e: KeyEvent) -> Boolean) { 204 | builder.setOnKeyListener({ _, keyCode, event -> f(keyCode, event) }) 205 | } 206 | 207 | fun neutralButton(@StringRes textResource: Int = android.R.string.ok, f: DialogInterface.() -> Unit = { dismiss() }) { 208 | neutralButton(ctx.getString(textResource), f) 209 | } 210 | 211 | fun neutralButton(title: String, f: DialogInterface.() -> Unit = { dismiss() }) { 212 | builder.setNeutralButton(title, { dialog, _ -> dialog.f() }) 213 | } 214 | 215 | fun positiveButton(@StringRes textResource: Int = android.R.string.ok, f: DialogInterface.() -> Unit) { 216 | positiveButton(ctx.getString(textResource), f) 217 | } 218 | 219 | fun positiveButton(title: String, f: DialogInterface.() -> Unit) { 220 | builder.setPositiveButton(title, { dialog, _ -> dialog.f() }) 221 | } 222 | 223 | fun negativeButton(@StringRes textResource: Int = android.R.string.cancel, f: DialogInterface.() -> Unit = { dismiss() }) { 224 | negativeButton(ctx.getString(textResource), f) 225 | } 226 | 227 | fun negativeButton(title: String, f: DialogInterface.() -> Unit = { dismiss() }) { 228 | builder.setNegativeButton(title, { dialog, _ -> dialog.f() }) 229 | } 230 | 231 | fun items(@ArrayRes itemsId: Int, f: (which: Int) -> Unit) { 232 | items(ctx.resources!!.getTextArray(itemsId), f) 233 | } 234 | 235 | fun items(items: List, f: (which: Int) -> Unit) { 236 | items(items.toTypedArray(), f) 237 | } 238 | 239 | fun items(items: Array, f: (which: Int) -> Unit) { 240 | builder.setItems(items, { _, which -> f(which) }) 241 | } 242 | 243 | fun adapter(adapter: ListAdapter, f: (which: Int) -> Unit) { 244 | builder.setAdapter(adapter, { _, which -> f(which) }) 245 | } 246 | 247 | fun adapter(cursor: Cursor, labelColumn: String, f: (which: Int) -> Unit) { 248 | builder.setCursor(cursor, { _, which -> f(which) }, labelColumn) 249 | } 250 | } -------------------------------------------------------------------------------- /sample/src/main/java/com/vector/appupdatedemo/http/OkGoUpdateHttpUtil.java: -------------------------------------------------------------------------------- 1 | package com.vector.appupdatedemo.http; 2 | 3 | import android.support.annotation.NonNull; 4 | 5 | import com.lzy.okgo.OkGo; 6 | import com.lzy.okgo.model.Progress; 7 | import com.vector.update_app.HttpManager; 8 | 9 | import java.io.File; 10 | import java.util.Map; 11 | 12 | /** 13 | * 使用OkGo实现接口 14 | */ 15 | 16 | public class OkGoUpdateHttpUtil implements HttpManager { 17 | /** 18 | * 异步get 19 | * 20 | * @param url get请求地址 21 | * @param params get参数 22 | * @param callBack 回调 23 | */ 24 | @Override 25 | public void asyncGet(@NonNull String url, @NonNull Map params, @NonNull final Callback callBack) { 26 | OkGo.get(url).params(params).execute(new com.lzy.okgo.callback.StringCallback() { 27 | @Override 28 | public void onSuccess(com.lzy.okgo.model.Response response) { 29 | callBack.onResponse(response.body()); 30 | } 31 | 32 | @Override 33 | public void onError(com.lzy.okgo.model.Response response) { 34 | super.onError(response); 35 | callBack.onError("异常"); 36 | } 37 | }); 38 | } 39 | 40 | /** 41 | * 异步post 42 | * 43 | * @param url post请求地址 44 | * @param params post请求参数 45 | * @param callBack 回调 46 | */ 47 | @Override 48 | public void asyncPost(@NonNull String url, @NonNull Map params, @NonNull final Callback callBack) { 49 | OkGo.post(url).params(params).execute(new com.lzy.okgo.callback.StringCallback() { 50 | @Override 51 | public void onSuccess(com.lzy.okgo.model.Response response) { 52 | callBack.onResponse(response.body()); 53 | } 54 | 55 | @Override 56 | public void onError(com.lzy.okgo.model.Response response) { 57 | super.onError(response); 58 | callBack.onError("异常"); 59 | } 60 | }); 61 | } 62 | 63 | /** 64 | * 下载 65 | * 66 | * @param url 下载地址 67 | * @param path 文件保存路径 68 | * @param fileName 文件名称 69 | * @param callback 回调 70 | */ 71 | @Override 72 | public void download(@NonNull String url, @NonNull String path, @NonNull String fileName, @NonNull final FileCallback callback) { 73 | OkGo.get(url).execute(new com.lzy.okgo.callback.FileCallback(path, fileName) { 74 | @Override 75 | public void onSuccess(com.lzy.okgo.model.Response response) { 76 | callback.onResponse(response.body()); 77 | } 78 | 79 | @Override 80 | public void onStart(com.lzy.okgo.request.base.Request request) { 81 | super.onStart(request); 82 | callback.onBefore(); 83 | } 84 | 85 | @Override 86 | public void onError(com.lzy.okgo.model.Response response) { 87 | super.onError(response); 88 | callback.onError("异常"); 89 | } 90 | 91 | @Override 92 | public void downloadProgress(Progress progress) { 93 | super.downloadProgress(progress); 94 | 95 | callback.onProgress(progress.fraction, progress.totalSize); 96 | } 97 | }); 98 | } 99 | } -------------------------------------------------------------------------------- /sample/src/main/java/com/vector/appupdatedemo/http/UpdateAppHttpUtil.java: -------------------------------------------------------------------------------- 1 | package com.vector.appupdatedemo.http; 2 | 3 | import android.support.annotation.NonNull; 4 | 5 | import com.vector.update_app.HttpManager; 6 | import com.zhy.http.okhttp.OkHttpUtils; 7 | import com.zhy.http.okhttp.callback.FileCallBack; 8 | import com.zhy.http.okhttp.callback.StringCallback; 9 | 10 | import java.io.File; 11 | import java.util.Map; 12 | 13 | import okhttp3.Call; 14 | import okhttp3.Request; 15 | import okhttp3.Response; 16 | 17 | /** 18 | * Created by Vector 19 | * on 2017/6/19 0019. 20 | */ 21 | 22 | public class UpdateAppHttpUtil implements HttpManager { 23 | /** 24 | * 异步get 25 | * 26 | * @param url get请求地址 27 | * @param params get参数 28 | * @param callBack 回调 29 | */ 30 | @Override 31 | public void asyncGet(@NonNull String url, @NonNull Map params, @NonNull final Callback callBack) { 32 | OkHttpUtils.get() 33 | .url(url) 34 | .params(params) 35 | .build() 36 | .execute(new StringCallback() { 37 | @Override 38 | public void onError(Call call, Response response, Exception e, int id) { 39 | callBack.onError(validateError(e, response)); 40 | } 41 | 42 | @Override 43 | public void onResponse(String response, int id) { 44 | callBack.onResponse(response); 45 | } 46 | }); 47 | } 48 | 49 | /** 50 | * 异步post 51 | * 52 | * @param url post请求地址 53 | * @param params post请求参数 54 | * @param callBack 回调 55 | */ 56 | @Override 57 | public void asyncPost(@NonNull String url, @NonNull Map params, @NonNull final Callback callBack) { 58 | OkHttpUtils.post() 59 | .url(url) 60 | .params(params) 61 | .build() 62 | .execute(new StringCallback() { 63 | @Override 64 | public void onError(Call call, Response response, Exception e, int id) { 65 | callBack.onError(validateError(e, response)); 66 | } 67 | 68 | @Override 69 | public void onResponse(String response, int id) { 70 | callBack.onResponse(response); 71 | } 72 | }); 73 | 74 | } 75 | 76 | /** 77 | * 下载 78 | * 79 | * @param url 下载地址 80 | * @param path 文件保存路径 81 | * @param fileName 文件名称 82 | * @param callback 回调 83 | */ 84 | @Override 85 | public void download(@NonNull String url, @NonNull String path, @NonNull String fileName, @NonNull final FileCallback callback) { 86 | OkHttpUtils.get() 87 | .url(url) 88 | .build() 89 | .execute(new FileCallBack(path, fileName) { 90 | @Override 91 | public void inProgress(float progress, long total, int id) { 92 | callback.onProgress(progress, total); 93 | } 94 | 95 | @Override 96 | public void onError(Call call, Response response, Exception e, int id) { 97 | callback.onError(validateError(e, response)); 98 | } 99 | 100 | @Override 101 | public void onResponse(File response, int id) { 102 | callback.onResponse(response); 103 | 104 | } 105 | 106 | @Override 107 | public void onBefore(Request request, int id) { 108 | super.onBefore(request, id); 109 | callback.onBefore(); 110 | } 111 | }); 112 | 113 | } 114 | } -------------------------------------------------------------------------------- /sample/src/main/java/com/vector/appupdatedemo/ui/KotlinActivity.kt: -------------------------------------------------------------------------------- 1 | package com.vector.appupdatedemo.ui 2 | 3 | import android.os.Bundle 4 | import android.os.Environment 5 | import android.support.v7.app.AppCompatActivity 6 | import com.vector.appupdatedemo.R 7 | import com.vector.appupdatedemo.ext.cancelProgressDialog 8 | import com.vector.appupdatedemo.ext.dialog 9 | import com.vector.appupdatedemo.ext.showProgressDialog 10 | import com.vector.appupdatedemo.ext.toast 11 | import com.vector.appupdatedemo.http.UpdateAppHttpUtil 12 | import com.vector.appupdatedemo.util.HProgressDialogUtils 13 | import com.vector.update_app.SilenceUpdateCallback 14 | import com.vector.update_app.UpdateAppBean 15 | import com.vector.update_app.UpdateAppManager 16 | import com.vector.update_app.utils.AppUpdateUtils 17 | import com.vector.update_app_kotlin.* 18 | import kotlinx.android.synthetic.main.activity_kotlin.* 19 | import org.json.JSONObject 20 | import java.io.File 21 | import java.util.* 22 | 23 | class KotlinActivity : AppCompatActivity() { 24 | private val mUpdateUrl = "https://raw.githubusercontent.com/WVector/AppUpdateDemo/master/json/json.txt" 25 | override fun onCreate(savedInstanceState: Bundle?) { 26 | super.onCreate(savedInstanceState) 27 | setContentView(R.layout.activity_kotlin) 28 | btn_default.setSolidTheme() 29 | btn_diy_1.setStrokeTheme() 30 | btn_diy_2.setStrokeTheme() 31 | btn_diy_3.setStrokeTheme() 32 | btn_diy_4.setStrokeTheme() 33 | btn_diy_5.setStrokeTheme() 34 | 35 | btn_default.setOnClickListener { 36 | defaultUpdate() 37 | } 38 | btn_diy_1.setOnClickListener { 39 | updateDiy1() 40 | } 41 | 42 | btn_diy_2.setOnClickListener { 43 | updateDiy2() 44 | } 45 | btn_diy_3.setOnClickListener { 46 | updateDiy3() 47 | } 48 | btn_diy_4.setOnClickListener { 49 | updateDiy4() 50 | } 51 | btn_diy_5.setOnClickListener { 52 | updateDiy5() 53 | } 54 | } 55 | 56 | 57 | private var isShowDownloadProgress: Boolean = false 58 | /** 59 | * 最简方式 60 | */ 61 | private fun defaultUpdate() { 62 | updateApp(mUpdateUrl, UpdateAppHttpUtil()).update() 63 | } 64 | 65 | /** 66 | * 自定义接口协议+自定义对话框 67 | */ 68 | private fun updateDiy2() { 69 | isShowDownloadProgress = false 70 | diy() 71 | } 72 | 73 | 74 | /** 75 | * 自定义接口协议+自定义对话框+显示进度对话框 76 | */ 77 | private fun updateDiy3() { 78 | isShowDownloadProgress = true 79 | diy() 80 | } 81 | 82 | private fun diy() { 83 | //下载路径 84 | val path = Environment.getExternalStorageDirectory().absolutePath 85 | //自定义参数 86 | val params = HashMap() 87 | params.put("appKey", "ab55ce55Ac4bcP408cPb8c1Aaeac179c5f6f") 88 | params.put("appVersion", AppUpdateUtils.getVersionName(this)) 89 | params.put("key1", "value2") 90 | params.put("key2", "value3") 91 | 92 | updateApp(mUpdateUrl, UpdateAppHttpUtil()) 93 | //自定义配置 94 | { 95 | //以下设置,都是可选 96 | //设置请求方式,默认get 97 | isPost = false 98 | //添加自定义参数,默认version=1.0.0(app的versionName);apkKey=唯一表示(在AndroidManifest.xml配置) 99 | setParams(params) 100 | //设置apk下砸路径,默认是在下载到sd卡下/Download/1.0.0/test.apk 101 | targetPath = path 102 | //设置appKey,默认从AndroidManifest.xml获取,如果,使用自定义参数,则此项无效 103 | // setAppKey("ab55ce55Ac4bcP408cPb8c1Aaeac179c5f6f") 104 | 105 | } 106 | .check { 107 | onBefore { showProgressDialog() } 108 | //自定义解析 109 | parseJson { 110 | val jsonObject = JSONObject(it) 111 | UpdateAppBean() 112 | //(必须)是否更新Yes,No 113 | .setUpdate(jsonObject.optString("update")) 114 | //(必须)新版本号, 115 | .setNewVersion(jsonObject.optString("new_version")) 116 | //(必须)下载地址 117 | .setApkFileUrl(jsonObject.optString("apk_file_url")) 118 | //(必须)更新内容 119 | .setUpdateLog(jsonObject.optString("update_log")) 120 | //大小,不设置不显示大小,可以不设置 121 | .setTargetSize(jsonObject.optString("target_size")) 122 | //是否强制更新,可以不设置 123 | .setConstraint(false) 124 | //设置md5,可以不设置 125 | .setNewMd5(jsonObject.optString("new_md5")) 126 | 127 | } 128 | 129 | hasNewApp { updateApp, updateAppManager -> 130 | showDiyDialog(updateApp, updateAppManager) 131 | } 132 | noNewApp { toast("没有新版本") } 133 | onAfter { cancelProgressDialog() } 134 | } 135 | } 136 | 137 | /** 138 | * 自定义对话框 139 | */ 140 | private fun showDiyDialog(updateApp: UpdateAppBean, updateAppManager: UpdateAppManager) { 141 | dialog("是否升级到${updateApp.newVersion}版本?" 142 | , "新版本大小:${updateApp.targetSize}\n\n${updateApp.updateLog}") 143 | { 144 | positiveButton("升级") { 145 | if (isShowDownloadProgress) { 146 | updateAppManager.download { 147 | onStart { HProgressDialogUtils.showHorizontalProgressDialog(this@KotlinActivity, "下载进度", false) } 148 | onProgress { progress, _ -> HProgressDialogUtils.setProgress(Math.round(progress * 100)) } 149 | onFinish { 150 | HProgressDialogUtils.cancel() 151 | true 152 | } 153 | onError { 154 | toast(it) 155 | HProgressDialogUtils.cancel() 156 | } 157 | } 158 | 159 | } else { 160 | updateAppManager.download() 161 | } 162 | 163 | dismiss() 164 | 165 | } 166 | negativeButton("暂不升级") { 167 | dismiss() 168 | } 169 | show() 170 | } 171 | 172 | } 173 | 174 | 175 | /** 176 | * 自定义接口协议 177 | */ 178 | private fun updateDiy1() { 179 | //下载路径 180 | val path = Environment.getExternalStorageDirectory().absolutePath 181 | //自定义参数 182 | val params = HashMap() 183 | params.put("appKey", "ab55ce55Ac4bcP408cPb8c1Aaeac179c5f6f") 184 | params.put("appVersion", AppUpdateUtils.getVersionName(this)) 185 | params.put("key1", "value2") 186 | params.put("key2", "value3") 187 | 188 | updateApp(mUpdateUrl, UpdateAppHttpUtil()) 189 | //自定义配置 190 | { 191 | //以下设置,都是可选 192 | //设置请求方式,默认get 193 | isPost = false 194 | //添加自定义参数,默认version=1.0.0(app的versionName);apkKey=唯一表示(在AndroidManifest.xml配置) 195 | setParams(params) 196 | //设置点击升级后,消失对话框,默认点击升级后,对话框显示下载进度 197 | hideDialogOnDownloading() 198 | //设置头部,不设置显示默认的图片,设置图片后自动识别主色调,然后为按钮,进度条设置颜色 199 | topPic = R.mipmap.top_8 200 | 201 | //为按钮,进度条设置颜色。 202 | themeColor = 0xffffac5d.toInt() 203 | //设置apk下砸路径,默认是在下载到sd卡下/Download/1.0.0/test.apk 204 | targetPath = path 205 | //设置appKey,默认从AndroidManifest.xml获取,如果,使用自定义参数,则此项无效 206 | // setAppKey("ab55ce55Ac4bcP408cPb8c1Aaeac179c5f6f") 207 | 208 | } 209 | .check { 210 | onBefore { showProgressDialog() } 211 | //自定义解析 212 | parseJson { 213 | val jsonObject = JSONObject(it) 214 | UpdateAppBean() 215 | //(必须)是否更新Yes,No 216 | .setUpdate(jsonObject.optString("update")) 217 | //(必须)新版本号, 218 | .setNewVersion(jsonObject.optString("new_version")) 219 | //(必须)下载地址 220 | .setApkFileUrl(jsonObject.optString("apk_file_url")) 221 | //(必须)更新内容 222 | .setUpdateLog(jsonObject.optString("update_log")) 223 | //大小,不设置不显示大小,可以不设置 224 | .setTargetSize(jsonObject.optString("target_size")) 225 | //是否强制更新,可以不设置 226 | .setConstraint(false) 227 | //设置md5,可以不设置 228 | .setNewMd5(jsonObject.optString("new_md5")) 229 | 230 | } 231 | noNewApp { toast("没有新版本") } 232 | onAfter { cancelProgressDialog() } 233 | } 234 | 235 | } 236 | 237 | /** 238 | * 静默下载 239 | */ 240 | private fun updateDiy4() { 241 | updateApp(mUpdateUrl, UpdateAppHttpUtil()) 242 | { 243 | setOnlyWifi() 244 | }.silenceUpdate() 245 | } 246 | 247 | /** 248 | * 静默下载+自定义对话框 249 | */ 250 | private fun updateDiy5() { 251 | updateApp(mUpdateUrl, UpdateAppHttpUtil()) 252 | { 253 | setOnlyWifi() 254 | }.checkNewApp(object : SilenceUpdateCallback() { 255 | override fun showDialog(updateApp: UpdateAppBean, updateAppManager: UpdateAppManager?, appFile: File) { 256 | showSilenceDiyDialog(updateApp, appFile) 257 | } 258 | }) 259 | } 260 | 261 | /** 262 | * 自定义对话框 263 | */ 264 | private fun showSilenceDiyDialog(updateApp: UpdateAppBean, appFile: File?) { 265 | dialog("是否升级到${updateApp.newVersion}版本?" 266 | , "新版本大小:${updateApp.targetSize}\n\n${updateApp.updateLog}") 267 | { 268 | positiveButton("升级") { 269 | AppUpdateUtils.installApp(this@KotlinActivity, appFile) 270 | dismiss() 271 | 272 | } 273 | negativeButton("暂不升级") { 274 | dismiss() 275 | } 276 | show() 277 | } 278 | } 279 | 280 | 281 | } 282 | -------------------------------------------------------------------------------- /sample/src/main/java/com/vector/appupdatedemo/ui/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.vector.appupdatedemo.ui; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.View; 7 | import android.widget.Button; 8 | import android.widget.ImageView; 9 | import android.widget.Toast; 10 | 11 | import com.tbruyelle.rxpermissions.RxPermissions; 12 | import com.vector.appupdatedemo.R; 13 | import com.vector.update_app.utils.AppUpdateUtils; 14 | import com.vector.update_app.utils.DrawableUtil; 15 | 16 | import rx.functions.Action1; 17 | 18 | import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE; 19 | 20 | public class MainActivity extends AppCompatActivity { 21 | 22 | @Override 23 | protected void onCreate(Bundle savedInstanceState) { 24 | super.onCreate(savedInstanceState); 25 | setContentView(R.layout.activity_main); 26 | DrawableUtil.setTextSolidTheme((Button) findViewById(R.id.btn_java), 2, 60, 0xffedd0be); 27 | DrawableUtil.setTextSolidTheme((Button) findViewById(R.id.btn_kotlin), 2, 60, 0xffff534d); 28 | 29 | ImageView im = findViewById(R.id.iv); 30 | 31 | im.setImageBitmap(AppUpdateUtils.drawableToBitmap(AppUpdateUtils.getAppIcon(this))); 32 | 33 | getPermission(); 34 | 35 | } 36 | 37 | 38 | public void getPermission() { 39 | RxPermissions rxPermissions = new RxPermissions(this); 40 | rxPermissions.request(WRITE_EXTERNAL_STORAGE) 41 | .subscribe(new Action1() { 42 | @Override 43 | public void call(Boolean aBoolean) { 44 | if (aBoolean) { 45 | Toast.makeText(MainActivity.this, "已授权", Toast.LENGTH_SHORT).show(); 46 | } else { 47 | Toast.makeText(MainActivity.this, "未授权", Toast.LENGTH_SHORT).show(); 48 | } 49 | } 50 | }); 51 | 52 | } 53 | 54 | public void updateJava(View view) { 55 | startActivity(new Intent(this, JavaActivity.class)); 56 | } 57 | 58 | public void updateKotlin(View view) { 59 | startActivity(new Intent(this, KotlinActivity.class)); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /sample/src/main/java/com/vector/appupdatedemo/util/CProgressDialogUtils.java: -------------------------------------------------------------------------------- 1 | package com.vector.appupdatedemo.util; 2 | 3 | import android.app.Activity; 4 | import android.app.ProgressDialog; 5 | import android.content.DialogInterface; 6 | 7 | /** 8 | * Created by Vector on 2016/8/12 0012. 9 | */ 10 | public class CProgressDialogUtils { 11 | private static final String TAG = CProgressDialogUtils.class.getSimpleName(); 12 | private static ProgressDialog sCircleProgressDialog; 13 | 14 | private CProgressDialogUtils() { 15 | throw new UnsupportedOperationException("cannot be instantiated"); 16 | } 17 | 18 | public static void showProgressDialog(Activity activity) { 19 | showProgressDialog(activity, "加载中", false, null); 20 | } 21 | 22 | public static void showProgressDialog(Activity activity, DialogInterface.OnCancelListener listener) { 23 | showProgressDialog(activity, "加载中", true, listener); 24 | } 25 | 26 | public static void showProgressDialog(Activity activity, String msg) { 27 | showProgressDialog(activity, msg, false, null); 28 | } 29 | 30 | public static void showProgressDialog(Activity activity, String msg, DialogInterface.OnCancelListener listener) { 31 | showProgressDialog(activity, msg, true, listener); 32 | } 33 | 34 | public static void showProgressDialog(final Activity activity, String msg, boolean cancelable, DialogInterface.OnCancelListener listener) { 35 | if (activity == null || activity.isFinishing()) { 36 | return; 37 | } 38 | 39 | 40 | if (sCircleProgressDialog == null) { 41 | sCircleProgressDialog = new ProgressDialog(activity); 42 | sCircleProgressDialog.setMessage(msg); 43 | sCircleProgressDialog.setOwnerActivity(activity); 44 | sCircleProgressDialog.setOnCancelListener(listener); 45 | sCircleProgressDialog.setCancelable(cancelable); 46 | } else { 47 | if (activity.equals(sCircleProgressDialog.getOwnerActivity())) { 48 | sCircleProgressDialog.setMessage(msg); 49 | sCircleProgressDialog.setCancelable(cancelable); 50 | sCircleProgressDialog.setOnCancelListener(listener); 51 | } else { 52 | //不相等,所以取消任何ProgressDialog 53 | cancelProgressDialog(); 54 | sCircleProgressDialog = new ProgressDialog(activity); 55 | sCircleProgressDialog.setMessage(msg); 56 | sCircleProgressDialog.setCancelable(cancelable); 57 | sCircleProgressDialog.setOwnerActivity(activity); 58 | sCircleProgressDialog.setOnCancelListener(listener); 59 | } 60 | } 61 | 62 | if (!sCircleProgressDialog.isShowing()) { 63 | sCircleProgressDialog.show(); 64 | } 65 | 66 | } 67 | 68 | 69 | public static void cancelProgressDialog(Activity activity) { 70 | if (sCircleProgressDialog != null && sCircleProgressDialog.isShowing()) { 71 | if (sCircleProgressDialog.getOwnerActivity() == activity) { 72 | sCircleProgressDialog.cancel(); 73 | sCircleProgressDialog = null; 74 | } 75 | } 76 | } 77 | 78 | public static void cancelProgressDialog() { 79 | if (sCircleProgressDialog != null && sCircleProgressDialog.isShowing()) { 80 | sCircleProgressDialog.cancel(); 81 | sCircleProgressDialog = null; 82 | } 83 | } 84 | 85 | } 86 | -------------------------------------------------------------------------------- /sample/src/main/java/com/vector/appupdatedemo/util/HProgressDialogUtils.java: -------------------------------------------------------------------------------- 1 | package com.vector.appupdatedemo.util; 2 | 3 | import android.annotation.SuppressLint; 4 | import android.app.Activity; 5 | import android.app.ProgressDialog; 6 | import android.text.TextUtils; 7 | 8 | /** 9 | * Created by Vector on 2016/8/12 0012. 10 | */ 11 | public class HProgressDialogUtils { 12 | private static ProgressDialog sHorizontalProgressDialog; 13 | 14 | private HProgressDialogUtils() { 15 | throw new UnsupportedOperationException("cannot be instantiated"); 16 | } 17 | 18 | @SuppressLint("NewApi") 19 | public static void showHorizontalProgressDialog(Activity context, String msg, boolean isShowSize) { 20 | cancel(); 21 | 22 | if (sHorizontalProgressDialog == null) { 23 | sHorizontalProgressDialog = new ProgressDialog(context); 24 | sHorizontalProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 25 | sHorizontalProgressDialog.setCanceledOnTouchOutside(false); 26 | if (isShowSize) 27 | sHorizontalProgressDialog.setProgressNumberFormat("%2dMB/%1dMB"); 28 | 29 | } 30 | if (!TextUtils.isEmpty(msg)) { 31 | sHorizontalProgressDialog.setMessage(msg); 32 | } 33 | sHorizontalProgressDialog.show(); 34 | 35 | } 36 | 37 | public static void setMax(long total) { 38 | if (sHorizontalProgressDialog != null) { 39 | sHorizontalProgressDialog.setMax(((int) total) / (1024 * 1024)); 40 | } 41 | } 42 | 43 | public static void cancel() { 44 | if (sHorizontalProgressDialog != null) { 45 | sHorizontalProgressDialog.dismiss(); 46 | sHorizontalProgressDialog = null; 47 | } 48 | } 49 | 50 | public static void setProgress(int current) { 51 | if (sHorizontalProgressDialog == null) { 52 | return; 53 | } 54 | sHorizontalProgressDialog.setProgress(current); 55 | if (sHorizontalProgressDialog.getProgress() >= sHorizontalProgressDialog.getMax()) { 56 | sHorizontalProgressDialog.dismiss(); 57 | sHorizontalProgressDialog = null; 58 | } 59 | } 60 | 61 | public static void setProgress(long current) { 62 | if (sHorizontalProgressDialog == null) { 63 | return; 64 | } 65 | sHorizontalProgressDialog.setProgress(((int) current) / (1024 * 1024)); 66 | if (sHorizontalProgressDialog.getProgress() >= sHorizontalProgressDialog.getMax()) { 67 | sHorizontalProgressDialog.dismiss(); 68 | sHorizontalProgressDialog = null; 69 | } 70 | } 71 | 72 | public static void onLoading(long total, long current) { 73 | if (sHorizontalProgressDialog == null) { 74 | return; 75 | } 76 | if (current == 0) { 77 | sHorizontalProgressDialog.setMax(((int) total) / (1024 * 1024)); 78 | } 79 | sHorizontalProgressDialog.setProgress(((int) current) / (1024 * 1024)); 80 | if (sHorizontalProgressDialog.getProgress() >= sHorizontalProgressDialog.getMax()) { 81 | sHorizontalProgressDialog.dismiss(); 82 | sHorizontalProgressDialog = null; 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/activity_java.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 9 | 10 | 18 | 19 |