├── .gitignore
├── .idea
├── compiler.xml
├── copyright
│ └── profiles_settings.xml
├── dictionaries
│ └── gyj.xml
├── encodings.xml
├── gradle.xml
├── markdown-exported-files.xml
├── markdown-navigator.xml
├── markdown-navigator
│ └── profiles_settings.xml
├── misc.xml
├── modules.xml
├── runConfigurations.xml
└── vcs.xml
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── trycatch
│ │ └── myupdateapk
│ │ └── ApplicationTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── trycatch
│ │ │ └── myupdateapk
│ │ │ └── demo
│ │ │ └── MainActivity.java
│ └── res
│ │ ├── layout
│ │ └── activity_main.xml
│ │ ├── mipmap-hdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-mdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xhdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xxhdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xxxhdpi
│ │ └── ic_launcher.png
│ │ ├── values-w820dp
│ │ └── dimens.xml
│ │ └── values
│ │ ├── colors.xml
│ │ ├── dimens.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── com
│ └── trycatch
│ └── myupdateapk
│ └── ExampleUnitTest.java
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── images
└── updateApp320.gif
├── myupdateapklibrary
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── trycath
│ │ └── myupdateapklibrary
│ │ └── ApplicationTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── trycath
│ │ │ └── myupdateapklibrary
│ │ │ ├── UpdateApk.java
│ │ │ ├── UpdateKey.java
│ │ │ ├── UpdateState.java
│ │ │ ├── dialogactivity
│ │ │ ├── ProgressBarActivity.java
│ │ │ └── PromptDialogActivity.java
│ │ │ ├── exception
│ │ │ └── CustomizeException.java
│ │ │ ├── httprequest
│ │ │ └── DownloadServiceApi.java
│ │ │ ├── listener
│ │ │ ├── AppUpdateListener.java
│ │ │ ├── HttpClientHelper.java
│ │ │ ├── ProgressResponseBody.java
│ │ │ ├── ProgressResponseListener.java
│ │ │ └── ServiceGenerator.java
│ │ │ ├── model
│ │ │ ├── AppInfoModel.java
│ │ │ ├── BinaryModel.java
│ │ │ └── DownloadModel.java
│ │ │ ├── rxbus
│ │ │ ├── RxBus.java
│ │ │ └── RxBusResult.java
│ │ │ ├── service
│ │ │ ├── DownloadFileService.java
│ │ │ └── DownloadService.java
│ │ │ └── util
│ │ │ ├── FileUtils.java
│ │ │ ├── GetAppInfo.java
│ │ │ ├── InstallApk.java
│ │ │ ├── IntenetUtil.java
│ │ │ ├── PreferenceUtils.java
│ │ │ └── StringUtils.java
│ └── res
│ │ ├── drawable
│ │ ├── update_background_button.xml
│ │ ├── update_background_button_cancel.xml
│ │ ├── update_bg_progress.xml
│ │ ├── update_button_bg_state_pressed_cancel_false.xml
│ │ ├── update_button_bg_state_pressed_cancel_true.xml
│ │ ├── update_button_bg_state_pressed_false.xml
│ │ ├── update_button_bg_state_pressed_true.xml
│ │ ├── update_ic_close_white_24dp.xml
│ │ └── update_material_card.xml
│ │ ├── layout
│ │ ├── activity_progress_bar.xml
│ │ └── activity_prompt_dialog.xml
│ │ ├── values-en-rUS
│ │ └── strings.xml
│ │ ├── values-w820dp
│ │ └── dimens.xml
│ │ ├── values-zh-rCN
│ │ └── strings.xml
│ │ ├── values
│ │ ├── colors.xml
│ │ ├── dimens.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ │ └── xml
│ │ └── file_paths.xml
│ └── test
│ └── java
│ └── com
│ └── trycath
│ └── myupdateapklibrary
│ └── ExampleUnitTest.java
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.idea/dictionaries/gyj.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
18 |
19 |
--------------------------------------------------------------------------------
/.idea/markdown-exported-files.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/markdown-navigator.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
--------------------------------------------------------------------------------
/.idea/markdown-navigator/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## MyUpdateApk
2 | 本项目的存在是因为友盟关闭了自动更新服务,所以自己结合友盟所说的一些自动更新方案,利用Rxjava + Retrofit2 + Okhttp3 + Rxandroid + Service + Fim实现app自动更新
3 |
4 | ### Introduction
5 | 适用于api 14及以上,且加入了Android6.0的权限判断,由于本人很懒就没开发后台apk管理功能,就直接采用fim的公共api实现版本比对
6 |
7 | ## ScreenShot
8 | 
9 |
10 | ## Gradle Config
11 | first add dependences
12 | ```
13 | dependencies {
14 | compile 'com.trycatch.android:myupdateapklibrary:1.4.4'
15 | }
16 | ```
17 |
18 | ## use
19 | 首先必须保证你的最新版本versionCode和versionName是随版本升级递增的
20 | ```
21 | versionCode 1
22 | versionName "1.0"
23 | ```
24 | 1.自动更新
25 | Activity
26 |
27 | ```
28 | public class MainActivity extends AppCompatActivity {
29 |
30 | @Override
31 | protected void onCreate(Bundle savedInstanceState) {
32 | super.onCreate(savedInstanceState);
33 | setContentView(R.layout.activity_main);
34 | UpdateKey.API_TOKEN = "160105e0223dec1896a6648b8265cc95";
35 | UpdateKey.APP_ID = "57d75987ca87a87c89000b91";
36 | UpdateKey.WITH_DIALOG=true; //设置是否显示弹出带进度条框
37 | UpdateKey.WITH_NOTIFITION=true;//设置是否在NOTIFITION上显示进度
38 | UpdateApk.init(this);
39 | }
40 |
41 | @Override
42 | protected void onDestroy() {
43 | super.onDestroy();
44 | UpdateApk.destory();
45 | }
46 | }
47 | ```
48 | 2.手动更新
49 | ```
50 | UpdateApk.setAppUpdateListener(new AppUpdateListener() {
51 | @Override
52 | public void onStart() {
53 | if(dialog!=null&&!dialog.isShowing()){
54 | dialog.show();
55 | }
56 | }
57 |
58 | @Override
59 | public void onCompleted() {
60 | Log.d(TAG,"onCompleted()===");
61 | if(dialog!=null&&dialog.isShowing()){
62 | dialog.dismiss();
63 | }
64 | }
65 |
66 | @Override
67 | public void onError(Throwable e) {
68 | Log.d(TAG,e.toString());
69 | if(dialog!=null&&dialog.isShowing()){
70 | dialog.dismiss();
71 | }
72 | }
73 |
74 | @Override
75 | public void onNext(AppInfoModel appInfoModel) {
76 | Log.d(TAG,appInfoModel.toString());
77 | if(dialog!=null&&dialog.isShowing()){
78 | dialog.dismiss();
79 | }
80 | }
81 |
82 | @Override
83 | public void onNext(AppInfoModel appInfoModel, int state) {
84 | Log.d(TAG,appInfoModel.toString());
85 | Log.d(TAG,"=state=="+state);
86 | switch (state){
87 | case UpdateState.BEST_NEW_VSERSION:
88 | Toast.makeText(MainActivity.this,"this is best new version",Toast.LENGTH_SHORT).show();
89 | break;
90 | case UpdateState.BEST_HEIGHT_VERSION:
91 | Toast.makeText(MainActivity.this,"this is highest version",Toast.LENGTH_SHORT).show();
92 | break;
93 | case UpdateState.NEED_UPDATE_VERSION:
94 | Toast.makeText(MainActivity.this,"need update new version",Toast.LENGTH_SHORT).show();
95 | break;
96 | default:
97 |
98 | }
99 | if(dialog!=null&&dialog.isShowing()){
100 | dialog.dismiss();
101 | }
102 | }
103 | });
104 | UpdateApk.init(MainActivity.this);
105 | ```
106 |
107 | AndroidManifest.xml
108 | ```
109 |
110 |
111 |
116 |
117 |
120 |
121 |
122 |
123 | ```
124 | ## License
125 |
126 | ```
127 | Copyright 2016 trycatch
128 |
129 | Licensed under the Apache License, Version 2.0 (the "License");
130 | you may not use this file except in compliance with the License.
131 | You may obtain a copy of the License at
132 |
133 | http://www.apache.org/licenses/LICENSE-2.0
134 |
135 | Unless required by applicable law or agreed to in writing, software
136 | distributed under the License is distributed on an "AS IS" BASIS,
137 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138 | See the License for the specific language governing permissions and
139 | limitations under the License.
140 | ```
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion '25.0.0'
6 |
7 | defaultConfig {
8 | applicationId "com.trycatch.myupdateapk.demo"
9 | minSdkVersion 14
10 | targetSdkVersion 25
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(dir: 'libs', include: ['*.jar'])
24 | testCompile 'junit:junit:4.12'
25 | compile 'com.android.support:appcompat-v7:25.2.0'
26 | compile project(':myupdateapklibrary')
27 | }
28 |
--------------------------------------------------------------------------------
/app/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 /Users/guoyoujin/Library/Android/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 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/trycatch/myupdateapk/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.trycatch.myupdateapk;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
27 |
28 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/app/src/main/java/com/trycatch/myupdateapk/demo/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.trycatch.myupdateapk.demo;
2 |
3 | import android.app.ProgressDialog;
4 | import android.os.Bundle;
5 | import android.support.v7.app.AppCompatActivity;
6 | import android.util.Log;
7 | import android.view.View;
8 | import android.widget.Button;
9 | import android.widget.Toast;
10 |
11 | import com.trycath.myupdateapklibrary.UpdateApk;
12 | import com.trycath.myupdateapklibrary.UpdateKey;
13 | import com.trycath.myupdateapklibrary.UpdateState;
14 | import com.trycath.myupdateapklibrary.listener.AppUpdateListener;
15 | import com.trycath.myupdateapklibrary.model.AppInfoModel;
16 |
17 |
18 | public class MainActivity extends AppCompatActivity implements View.OnClickListener {
19 | private Button updateApp;
20 | public static final String TAG = MainActivity.class.getSimpleName();
21 |
22 | @Override
23 | protected void onCreate(Bundle savedInstanceState) {
24 | super.onCreate(savedInstanceState);
25 | setContentView(R.layout.activity_main);
26 | UpdateKey.API_TOKEN = "160105e0223dec1896a6648b8265cc95";
27 | UpdateKey.APP_ID = "57d75987ca87a87c89000b91";
28 | UpdateKey.WITH_DIALOG=true; //设置是否显示弹出带进度条框
29 | UpdateKey.WITH_NOTIFITION=true;//设置是否在NOTIFITION上显示进度
30 | UpdateApk.init(this);
31 | initView();
32 | }
33 |
34 | @Override
35 | protected void onDestroy() {
36 | super.onDestroy();
37 | UpdateApk.destory();
38 | }
39 |
40 |
41 | public void initView(){
42 | updateApp = (Button) findViewById(R.id.updateApp);
43 | updateApp.setOnClickListener(this);
44 | }
45 |
46 | @Override
47 | public void onClick(View v) {
48 | final ProgressDialog dialog = new ProgressDialog(MainActivity.this);
49 | dialog.setIndeterminate(true);
50 | dialog.setMessage("正在检查更新");
51 | dialog.setCancelable(false);
52 | switch (v.getId()){
53 | case R.id.updateApp:
54 | Log.d(TAG,"switch updateApp");
55 | UpdateApk.setmManualUpdateFlg(true);
56 | UpdateApk.setAppUpdateListener(new AppUpdateListener() {
57 | @Override
58 | public void onStart() {
59 | if(dialog!=null&&!dialog.isShowing()){
60 | dialog.show();
61 | }
62 | }
63 |
64 | @Override
65 | public void onCompleted() {
66 | Log.d(TAG,"onCompleted()===");
67 | if(dialog!=null&&dialog.isShowing()){
68 | dialog.dismiss();
69 | }
70 | }
71 |
72 | @Override
73 | public void onError(Throwable e) {
74 | Log.d(TAG,e.toString());
75 | if(dialog!=null&&dialog.isShowing()){
76 | dialog.dismiss();
77 | }
78 | }
79 |
80 | @Override
81 | public void onNext(AppInfoModel appInfoModel) {
82 | Log.d(TAG,appInfoModel.toString());
83 | if(dialog!=null&&dialog.isShowing()){
84 | dialog.dismiss();
85 | }
86 | }
87 |
88 | @Override
89 | public void onNext(AppInfoModel appInfoModel, int state) {
90 | Log.d(TAG,appInfoModel.toString());
91 | Log.d(TAG,"=state=="+state);
92 | switch (state){
93 | case UpdateState.BEST_NEW_VSERSION:
94 | Toast.makeText(MainActivity.this,"this is best new version",Toast.LENGTH_SHORT).show();
95 | break;
96 | case UpdateState.BEST_HEIGHT_VERSION:
97 | Toast.makeText(MainActivity.this,"this is highest version",Toast.LENGTH_SHORT).show();
98 | break;
99 | case UpdateState.NEED_UPDATE_VERSION:
100 | Toast.makeText(MainActivity.this,"need update new version",Toast.LENGTH_SHORT).show();
101 | break;
102 | default:
103 |
104 | }
105 | if(dialog!=null&&dialog.isShowing()){
106 | dialog.dismiss();
107 | }
108 | }
109 | });
110 | UpdateApk.init(MainActivity.this);
111 | break;
112 | default:
113 | Log.d(TAG,"switch default");
114 | }
115 | }
116 |
117 | }
118 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/guoyoujin/MyUpdateApk/9b011a649513ba79f72f010211ff750b1b95864b/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/guoyoujin/MyUpdateApk/9b011a649513ba79f72f010211ff750b1b95864b/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/guoyoujin/MyUpdateApk/9b011a649513ba79f72f010211ff750b1b95864b/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/guoyoujin/MyUpdateApk/9b011a649513ba79f72f010211ff750b1b95864b/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/guoyoujin/MyUpdateApk/9b011a649513ba79f72f010211ff750b1b95864b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | MyUpdateApk
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/com/trycatch/myupdateapk/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.trycatch.myupdateapk;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | mavenCentral()
7 | }
8 | dependencies {
9 | classpath 'com.android.tools.build:gradle:2.2.0'
10 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.4'
11 | classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
12 | // NOTE: Do not place your application dependencies here; they belong
13 | // in the individual module build.gradle files
14 | }
15 | }
16 |
17 | allprojects {
18 | repositories {
19 | jcenter()
20 | mavenCentral()
21 | }
22 | tasks.withType(Javadoc).all { enabled = false }
23 | }
24 |
25 | task clean(type: Delete) {
26 | delete rootProject.buildDir
27 | }
28 |
--------------------------------------------------------------------------------
/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 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/guoyoujin/MyUpdateApk/9b011a649513ba79f72f010211ff750b1b95864b/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Dec 28 10:00:20 PST 2015
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-2.14.1-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 |
--------------------------------------------------------------------------------
/images/updateApp320.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/guoyoujin/MyUpdateApk/9b011a649513ba79f72f010211ff750b1b95864b/images/updateApp320.gif
--------------------------------------------------------------------------------
/myupdateapklibrary/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/myupdateapklibrary/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'com.github.dcendents.android-maven'
3 | apply plugin: 'com.jfrog.bintray'
4 | android {
5 | compileSdkVersion 25
6 | buildToolsVersion '25.0.0'
7 | resourcePrefix "bounceprogressbar__" //这个随便填
8 |
9 | defaultConfig {
10 | minSdkVersion 14
11 | targetSdkVersion 25
12 | versionCode 1
13 | versionName "1.0"
14 | }
15 | buildTypes {
16 | release {
17 | minifyEnabled false
18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
19 | }
20 | }
21 | }
22 |
23 | dependencies {
24 | compile fileTree(dir: 'libs', include: ['*.jar'])
25 | testCompile 'junit:junit:4.12'
26 | compile 'com.android.support:appcompat-v7:25.2.0'
27 | compile 'com.nineoldandroids:library:2.4.0+'
28 | compile 'io.reactivex:rxjava:1.1.8'
29 | compile 'io.reactivex:rxandroid:1.2.1'
30 | compile 'com.squareup.retrofit2:retrofit:2.0.2'
31 | compile 'com.squareup.retrofit2:converter-gson:2.0.2'
32 | compile 'com.squareup.retrofit2:adapter-rxjava:2.0.2'
33 | compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'
34 | compile "com.squareup.okhttp3:okhttp-urlconnection:3.3.1"
35 | compile 'com.squareup.okhttp3:okhttp:3.3.1'
36 | compile 'com.squareup.retrofit2:converter-gson:2.0.2'
37 | }
38 | // 这个version是区分library版本的,因此当我们需要更新library时记得修改这个version
39 | // 这个version影响后面的引用
40 | version = "1.4.4"
41 | def siteUrl = 'https://github.com/guoyoujin/MyUpdateApk' // 项目的主页
42 | def gitUrl = 'https://github.com/guoyoujin/MyUpdateApk.git' // Git仓库的url
43 | group = "com.trycatch.android" // Maven Group ID for the artifact,一般填你唯一的包名
44 | install {
45 | repositories.mavenInstaller {
46 | // This generates POM.xml with proper parameters
47 | pom {
48 | project {
49 | packaging 'aar'
50 | // Add your description here
51 | name 'Android MyUpdateApkLibrary Widget' //项目描述
52 | url siteUrl
53 | // Set your license
54 | licenses {
55 | license {
56 | name 'The Apache Software License, Version 2.0'
57 | url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
58 | }
59 | }
60 | developers {
61 | developer {
62 | id 'guoyoujin' //填写的一些基本信息
63 | name 'TryCatch'
64 | email 'guoyoujin123@gmail.com'
65 | }
66 | }
67 | scm {
68 | connection gitUrl
69 | developerConnection gitUrl
70 | url siteUrl
71 | }
72 | }
73 | }
74 | }
75 | }
76 | task sourcesJar(type: Jar) {
77 | from android.sourceSets.main.java.srcDirs
78 | classifier = 'sources'
79 | }
80 | task javadoc(type: Javadoc) {
81 | source = android.sourceSets.main.java.srcDirs
82 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
83 | }
84 | task javadocJar(type: Jar, dependsOn: javadoc) {
85 | classifier = 'javadoc'
86 | from javadoc.destinationDir
87 | }
88 | artifacts {
89 | archives javadocJar
90 | archives sourcesJar
91 | }
92 | Properties properties = new Properties()
93 | properties.load(project.rootProject.file('local.properties').newDataInputStream())
94 | bintray {
95 | user = properties.getProperty("bintray.user")
96 | key = properties.getProperty("bintray.apikey")
97 | configurations = ['archives']
98 | pkg {
99 | repo = "maven"
100 | name = "MyUpdateApkLibrary" //发布到JCenter上的项目名字
101 | websiteUrl = siteUrl
102 | vcsUrl = gitUrl
103 | licenses = ["Apache-2.0"]
104 | publish = true
105 | }
106 | }
107 |
108 |
--------------------------------------------------------------------------------
/myupdateapklibrary/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 /Users/guoyoujin/Library/Android/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 |
--------------------------------------------------------------------------------
/myupdateapklibrary/src/androidTest/java/com/trycath/myupdateapklibrary/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.trycath.myupdateapklibrary;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/myupdateapklibrary/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
10 |
14 |
15 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/myupdateapklibrary/src/main/java/com/trycath/myupdateapklibrary/UpdateApk.java:
--------------------------------------------------------------------------------
1 | package com.trycath.myupdateapklibrary;
2 |
3 | import android.content.Context;
4 | import android.util.Log;
5 |
6 | import com.trycath.myupdateapklibrary.dialogactivity.PromptDialogActivity;
7 | import com.trycath.myupdateapklibrary.httprequest.DownloadServiceApi;
8 | import com.trycath.myupdateapklibrary.listener.AppUpdateListener;
9 | import com.trycath.myupdateapklibrary.listener.ServiceGenerator;
10 | import com.trycath.myupdateapklibrary.model.AppInfoModel;
11 | import com.trycath.myupdateapklibrary.util.GetAppInfo;
12 | import com.trycath.myupdateapklibrary.util.IntenetUtil;
13 | import com.trycath.myupdateapklibrary.util.PreferenceUtils;
14 |
15 | import java.util.concurrent.TimeUnit;
16 |
17 | import rx.Subscriber;
18 | import rx.Subscription;
19 | import rx.android.schedulers.AndroidSchedulers;
20 | import rx.schedulers.Schedulers;
21 |
22 | /**
23 | * 在此写用途
24 | *
25 | * @author: guoyoujin
26 | * @mail: guoyoujin123@gmail.com
27 | * @date: 2016-09-12 14:02
28 | * @version: V1.0 <描述当前版本功能>
29 | */
30 |
31 | public class UpdateApk {
32 | private static final String TAG = "UpdateApk";
33 | private static volatile UpdateApk sInst = null;
34 | private static volatile Subscription subscription;
35 | private static Context mContext;
36 | private static AppUpdateListener appUpdateListener = null;
37 | private static boolean mManualUpdateFlg = false;
38 |
39 | public static UpdateApk init(Context context) {
40 | UpdateApk inst = sInst;
41 | if (inst == null) {
42 | synchronized (UpdateApk.class) {
43 | inst = sInst;
44 | if (inst == null) {
45 | mContext = context;
46 | inst = new UpdateApk(context);
47 | sInst = inst;
48 | }else{
49 | sInst.update();
50 | }
51 | }
52 | }else{
53 | sInst.update();
54 | }
55 | return inst;
56 | }
57 |
58 | public static void setAppUpdateListener(AppUpdateListener listener) {
59 | appUpdateListener = listener;
60 | }
61 |
62 | public static void setmManualUpdateFlg(boolean mManualUpdateFlg) {
63 | UpdateApk.mManualUpdateFlg = mManualUpdateFlg;
64 | }
65 | public static void setmManualUpdate(boolean mManualUpdateFlg,AppUpdateListener listener) {
66 | UpdateApk.mManualUpdateFlg = mManualUpdateFlg;
67 | appUpdateListener = listener;
68 | }
69 |
70 | private UpdateApk(final Context context) {
71 | Log.d(TAG, "UpdateApk");
72 | update();
73 | }
74 | private void update(){
75 | Log.d(TAG, "GETAPPINFO");
76 | if(UpdateKey.DOWNLOAD_WIFI){
77 | switch (IntenetUtil.getNetworkState(mContext)){
78 | case IntenetUtil.NETWORN_NONE:
79 | Log.d(TAG,"IntenetUtil.NETWORN_NONE");
80 | break;
81 | case IntenetUtil.NETWORN_2G:
82 | Log.d(TAG,"IntenetUtil.NETWORN_2G");
83 | break;
84 | case IntenetUtil.NETWORN_3G:
85 | Log.d(TAG,"IntenetUtil.NETWORN_3G");
86 | break;
87 | case IntenetUtil.NETWORN_4G:
88 | Log.d(TAG,"IntenetUtil.NETWORN_4G");
89 | break;
90 | case IntenetUtil.NETWORN_MOBILE:
91 | Log.d(TAG,"IntenetUtil.NETWORN_MOBILE");
92 | break;
93 | case IntenetUtil.NETWORN_WIFI:
94 | Log.d(TAG,"IntenetUtil.NETWORN_WIFI");
95 | getAppinfo();
96 | break;
97 | default:
98 | }
99 | }else{
100 | getAppinfo();
101 | }
102 | }
103 |
104 | public void getAppinfo(){
105 | DownloadServiceApi downloadService = ServiceGenerator.createService(DownloadServiceApi.class);
106 | subscription = downloadService.getUpdateApkInfo(UpdateKey.APP_ID,UpdateKey.API_TOKEN)
107 | .delaySubscription(1, TimeUnit.SECONDS)
108 | .subscribeOn(Schedulers.newThread())
109 | .observeOn(AndroidSchedulers.mainThread())
110 | .subscribe(new Subscriber() {
111 | @Override
112 | public void onStart() {
113 | super.onStart();
114 | if(appUpdateListener!=null){
115 | appUpdateListener.onStart();
116 | }
117 | }
118 |
119 | @Override
120 | public void onCompleted() {
121 | if(appUpdateListener!=null){
122 | appUpdateListener.onCompleted();
123 | }
124 | mManualUpdateFlg = false;
125 |
126 | }
127 | @Override
128 | public void onError(Throwable e) {
129 | Log.d(TAG, e.toString());
130 | if(appUpdateListener!=null){
131 | appUpdateListener.onError(e);
132 | }
133 | mManualUpdateFlg = false;
134 | }
135 | @Override
136 | public void onNext(AppInfoModel appInfoModel) {
137 | Log.d(TAG, appInfoModel.toString());
138 | if(appUpdateListener!=null){
139 | appUpdateListener.onNext(appInfoModel);
140 | }
141 | valAppInfo(appInfoModel);
142 | mManualUpdateFlg = false;
143 |
144 | }
145 | });
146 | }
147 |
148 | public void valAppInfo(AppInfoModel appInfoModel){
149 | if(appInfoModel.getVersion()!=null){
150 | if(!PreferenceUtils.getPrefBoolean(mContext,appInfoModel.getVersion(),false) || mManualUpdateFlg){
151 | switch (GetAppInfo.compareVersionCode(GetAppInfo.getVersionCode(mContext),Integer.parseInt(appInfoModel.getVersion()))){
152 | case UpdateState.BEST_NEW_VSERSION:
153 | Log.d(TAG,"this is best new version");
154 | if(appUpdateListener!=null){
155 | appUpdateListener.onNext(appInfoModel,0);
156 | }
157 | break;
158 | case UpdateState.BEST_HEIGHT_VERSION:
159 | Log.d(TAG,"this is highest version");
160 | if(appUpdateListener!=null){
161 | appUpdateListener.onNext(appInfoModel,1);
162 | }
163 | break;
164 | case UpdateState.NEED_UPDATE_VERSION:
165 | Log.d(TAG,"need update new version");
166 | PromptDialogActivity.startActivity(mContext,appInfoModel);
167 | if(appUpdateListener!=null){
168 | appUpdateListener.onNext(appInfoModel,-1);
169 | }
170 | break;
171 | default:
172 |
173 | }
174 | }
175 | }
176 | }
177 |
178 | public static void destory() {
179 | if (sInst!=null) {
180 | sInst = null;
181 | }
182 | if(mContext!=null){
183 | mContext=null;
184 | }
185 | if (subscription!=null && !subscription.isUnsubscribed()){
186 | subscription.unsubscribe();
187 | subscription = null;
188 | }
189 | if(appUpdateListener!=null){
190 | appUpdateListener=null;
191 | }
192 | }
193 | }
--------------------------------------------------------------------------------
/myupdateapklibrary/src/main/java/com/trycath/myupdateapklibrary/UpdateKey.java:
--------------------------------------------------------------------------------
1 | package com.trycath.myupdateapklibrary;
2 |
3 | /**
4 | * 在此写用途
5 | *
6 | * @author: guoyoujin
7 | * @mail: guoyoujin123@gmail.com
8 | * @date: 2016-09-12 14:02
9 | * @version: V1.0 <描述当前版本功能>
10 | */
11 |
12 | public class UpdateKey {
13 | public static final String TAG = "UpdateKey";
14 | public static String API_TOKEN = "";
15 | public static String APP_ID = "";
16 | public static String BASE_URL="http://api.fir.im";
17 | public static String DOWNLOAD_NAME="";
18 | public static String DOWNLOAD_APK_NAME="";
19 | public static boolean DOWNLOAD_WIFI = false;
20 | public static boolean WITH_DIALOG = false;
21 | public static boolean WITH_NOTIFITION = false;
22 | public static boolean DialogOrNotification = WITH_NOTIFITION;
23 | }
--------------------------------------------------------------------------------
/myupdateapklibrary/src/main/java/com/trycath/myupdateapklibrary/UpdateState.java:
--------------------------------------------------------------------------------
1 | package com.trycath.myupdateapklibrary;
2 |
3 | /**
4 | * 在此写用途
5 | *
6 | * @author: guoyoujin
7 | * @mail: guoyoujin123@gmail.com
8 | * @date: 2016-09-18 12:00
9 | * @version: V1.0 <描述当前版本功能>
10 | */
11 |
12 | public class UpdateState {
13 | private static final String TAG = "UpdateState";
14 | public static final int BEST_NEW_VSERSION = 0;
15 | public static final int BEST_HEIGHT_VERSION = 1;
16 | public static final int NEED_UPDATE_VERSION = -1;
17 |
18 | public UpdateState() {
19 | }
20 | }
--------------------------------------------------------------------------------
/myupdateapklibrary/src/main/java/com/trycath/myupdateapklibrary/dialogactivity/ProgressBarActivity.java:
--------------------------------------------------------------------------------
1 | package com.trycath.myupdateapklibrary.dialogactivity;
2 |
3 | import android.content.Context;
4 | import android.content.Intent;
5 | import android.os.Bundle;
6 | import android.support.v7.app.AppCompatActivity;
7 | import android.util.Log;
8 | import android.view.View;
9 | import android.widget.ImageView;
10 | import android.widget.ProgressBar;
11 | import android.widget.TextView;
12 |
13 | import com.trycath.myupdateapklibrary.R;
14 | import com.trycath.myupdateapklibrary.UpdateKey;
15 | import com.trycath.myupdateapklibrary.model.AppInfoModel;
16 | import com.trycath.myupdateapklibrary.model.DownloadModel;
17 | import com.trycath.myupdateapklibrary.rxbus.RxBus;
18 | import com.trycath.myupdateapklibrary.rxbus.RxBusResult;
19 | import com.trycath.myupdateapklibrary.util.StringUtils;
20 |
21 | public class ProgressBarActivity extends AppCompatActivity {
22 | private ProgressBar downloaddialog_progress;
23 | private TextView downloaddialog_count;
24 | private TextView tv_title;
25 | private ImageView downloaddialog_close;
26 | public static final String TAG = ProgressBarActivity.class.getSimpleName();
27 | public static final String MESSAGE_PROGRESS = "MESSAGE_PROGRESS";
28 | public static final String MESSAGE_COLOSE = "MESSAGE_COLOSE";
29 | private RxBus rxBus = RxBus.getInstance();
30 | private AppInfoModel appInfoModel ;
31 |
32 |
33 | @Override
34 | protected void onCreate(Bundle savedInstanceState) {
35 | super.onCreate(savedInstanceState);
36 | setContentView(R.layout.activity_progress_bar);
37 | Log.d(TAG,"onCreate");
38 | if(getIntent()!=null&&getIntent().getExtras()!=null&&getIntent().getExtras().getSerializable(PromptDialogActivity.INTENT_DOWNLOAD_MODEL)!=null) {
39 | appInfoModel = (AppInfoModel) getIntent().getExtras().getSerializable(PromptDialogActivity.INTENT_DOWNLOAD_MODEL);
40 | initView();
41 | initRxBusProgressDownload();
42 | }else{
43 | finish();
44 | }
45 |
46 | }
47 |
48 | private void initRxBusProgressDownload(){
49 | Log.d(TAG,"initRxBusProgressDownload");
50 | rxBus.toObserverableOnMainThread(ProgressBarActivity.MESSAGE_PROGRESS, new RxBusResult() {
51 | @Override
52 | public void onRxBusResult(Object o) {
53 | if(o instanceof DownloadModel){
54 | DownloadModel download = (DownloadModel) o;
55 | downloaddialog_progress.setProgress(download.getProgress());
56 | if (download.getProgress() == 100) {
57 | downloaddialog_count.setText(getResources().getString(R.string.download_successful));
58 | finish();
59 | } else {
60 | downloaddialog_count.setText(StringUtils.getDataSize(download.getCurrentFileSize())+ "/" + StringUtils.getDataSize(download.getTotalFileSize()));
61 | }
62 | }
63 | }
64 | });
65 | }
66 |
67 | private void initView(){
68 | Log.d(TAG,"initView");
69 | downloaddialog_progress = (ProgressBar) findViewById(R.id.downloaddialog_progress);
70 | downloaddialog_count = (TextView) findViewById(R.id.downloaddialog_count);
71 | downloaddialog_close = (ImageView) findViewById(R.id.downloaddialog_close);
72 | downloaddialog_close.setOnClickListener(onClickListenerDownLoadingClose);
73 | tv_title = (TextView) findViewById(R.id.tv_title);
74 | if(UpdateKey.DOWNLOAD_NAME!=null&&!"".equals(UpdateKey.DOWNLOAD_NAME)){
75 | tv_title.setText(String.format("%s%s",getResources().getString(R.string.is_downloading),UpdateKey.DOWNLOAD_NAME));
76 | }else{
77 | tv_title.setText(String.format("%s%s",getResources().getString(R.string.is_downloading),appInfoModel.getName()));
78 |
79 | }
80 | }
81 |
82 | View.OnClickListener onClickListenerDownLoadingClose = new View.OnClickListener() {
83 | @Override
84 | public void onClick(View v) {
85 | Log.d(TAG,"onClickListenerDownLoadingClose");
86 | rxBus.post(ProgressBarActivity.MESSAGE_COLOSE,ProgressBarActivity.MESSAGE_COLOSE);
87 | finish();
88 | }
89 | };
90 |
91 | @Override
92 | protected void onDestroy() {
93 | super.onDestroy();
94 | Log.d(TAG,"onDestroy");
95 | if(rxBus!=null){
96 | rxBus.removeObserverable(ProgressBarActivity.MESSAGE_COLOSE);
97 | }
98 | }
99 | public static void startActivity(Context context,AppInfoModel appInfoModel) {
100 | Log.d(TAG,"startActivity");
101 | Intent intent = new Intent(context,ProgressBarActivity.class);
102 | intent.putExtra(PromptDialogActivity.INTENT_DOWNLOAD_MODEL,appInfoModel);
103 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
104 | context.startActivity(intent);
105 | }
106 | @Override
107 | protected void onRestoreInstanceState(Bundle savedInstanceState) {
108 | super.onRestoreInstanceState(savedInstanceState);
109 | Log.d(TAG,"onRestoreInstanceState");
110 | appInfoModel = (AppInfoModel) savedInstanceState.getSerializable(PromptDialogActivity.INTENT_DOWNLOAD_MODEL);
111 | }
112 |
113 | @Override
114 | protected void onSaveInstanceState(Bundle outState) {
115 | super.onSaveInstanceState(outState);
116 | Log.d(TAG,"onSaveInstanceState");
117 | outState.putSerializable(PromptDialogActivity.INTENT_DOWNLOAD_MODEL,appInfoModel);
118 | }
119 | }
120 |
--------------------------------------------------------------------------------
/myupdateapklibrary/src/main/java/com/trycath/myupdateapklibrary/dialogactivity/PromptDialogActivity.java:
--------------------------------------------------------------------------------
1 | package com.trycath.myupdateapklibrary.dialogactivity;
2 |
3 | import android.Manifest;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.content.pm.PackageManager;
7 | import android.os.Bundle;
8 | import android.support.annotation.NonNull;
9 | import android.support.v4.app.ActivityCompat;
10 | import android.support.v7.app.AppCompatActivity;
11 | import android.text.method.ScrollingMovementMethod;
12 | import android.util.Log;
13 | import android.view.View;
14 | import android.view.WindowManager;
15 | import android.widget.Button;
16 | import android.widget.CheckBox;
17 | import android.widget.CompoundButton;
18 | import android.widget.TextView;
19 | import android.widget.Toast;
20 |
21 | import com.trycath.myupdateapklibrary.R;
22 | import com.trycath.myupdateapklibrary.model.AppInfoModel;
23 | import com.trycath.myupdateapklibrary.service.DownloadFileService;
24 | import com.trycath.myupdateapklibrary.util.FileUtils;
25 | import com.trycath.myupdateapklibrary.util.InstallApk;
26 | import com.trycath.myupdateapklibrary.util.PreferenceUtils;
27 | import com.trycath.myupdateapklibrary.util.StringUtils;
28 |
29 |
30 | public class PromptDialogActivity extends AppCompatActivity{
31 | private Button btnNowUpdate;
32 | private Button btnAfterUpdate;
33 | private TextView tvTitle;
34 | private TextView tvVersion;
35 | private TextView tvSize;
36 | private TextView tvContent;
37 | private CheckBox chBox;
38 | private static String TAG = PromptDialogActivity.class.getSimpleName();
39 | private static final int REQUEST_EXTERNAL_STORAGE = 111;
40 | public static final String INTENT_DOWNLOAD_MODEL= "INTENT_DOWNLOAD_MODEL";
41 | private AppInfoModel appInfoModel ;
42 | private static String[] PERMISSIONS_STORAGE = {
43 | Manifest.permission.READ_EXTERNAL_STORAGE,
44 | Manifest.permission.WRITE_EXTERNAL_STORAGE
45 | };
46 |
47 | @Override
48 | protected void onCreate(Bundle savedInstanceState) {
49 | super.onCreate(savedInstanceState);
50 | setContentView(R.layout.activity_prompt_dialog);
51 | Log.d(TAG,"==onCreate==");
52 | if(getIntent()!=null&&getIntent().getExtras()!=null&&getIntent().getExtras().getSerializable(PromptDialogActivity.INTENT_DOWNLOAD_MODEL)!=null){
53 | appInfoModel = (AppInfoModel) getIntent().getExtras().getSerializable(PromptDialogActivity.INTENT_DOWNLOAD_MODEL);
54 | initView();
55 | initContent();
56 | }else{
57 | finish();
58 | }
59 | }
60 |
61 | private void initView(){
62 | btnNowUpdate = (Button) findViewById(R.id.btnNowUpdate);
63 | btnAfterUpdate = (Button) findViewById(R.id.btnAfterUpdate);
64 | tvTitle = (TextView)findViewById(R.id.tvTitle);
65 | tvVersion = (TextView)findViewById(R.id.tvVersion);
66 | tvSize = (TextView) findViewById(R.id.tvSize);
67 | tvContent = (TextView) findViewById(R.id.tvContent);
68 | chBox = (CheckBox) findViewById(R.id.chBox);
69 | btnNowUpdate.setOnClickListener(nowUpdateListener);
70 | btnAfterUpdate.setOnClickListener(afterUpdateListener);
71 | chBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
72 | @Override
73 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
74 | if(isChecked){
75 | PreferenceUtils.setPrefBoolean(PromptDialogActivity.this,appInfoModel.getVersion(),isChecked);
76 | }
77 | }
78 | });
79 |
80 | }
81 |
82 | private void initContent(){
83 | tvVersion.setText(String.format("%s:%s",getResources().getString(R.string.most_version),appInfoModel.getVersionShort()));
84 | double size = (double)appInfoModel.getBinary().getFsize();
85 | tvContent.setText(String.format("%s\n%s",getResources().getString(R.string.update_content),appInfoModel.getChangelog()));
86 | tvContent.setMovementMethod(ScrollingMovementMethod.getInstance());
87 | if(FileUtils.getFile(appInfoModel).exists() && FileUtils.getFileSize(FileUtils.getFile(appInfoModel))==appInfoModel.getBinary().getFsize()){
88 | tvSize.setText(getResources().getString(R.string.most_version_downloaded));
89 | }else{
90 | tvSize.setText(String.format("%s:%s",getResources().getString(R.string.new_version_size), StringUtils.getDataSize(appInfoModel.getBinary().getFsize())));
91 | }
92 | }
93 |
94 | View.OnClickListener nowUpdateListener = new View.OnClickListener() {
95 | @Override
96 | public void onClick(View v) {
97 | int writePermission = ActivityCompat.checkSelfPermission(PromptDialogActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
98 | int readPermission = ActivityCompat.checkSelfPermission(PromptDialogActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE);
99 | if (writePermission != PackageManager.PERMISSION_GRANTED || readPermission != PackageManager.PERMISSION_GRANTED) {
100 | ActivityCompat.requestPermissions(PromptDialogActivity.this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
101 | }else{
102 | startService();
103 | }
104 | }
105 | };
106 |
107 | View.OnClickListener afterUpdateListener = new View.OnClickListener() {
108 | @Override
109 | public void onClick(View v) {
110 | finish();
111 | }
112 | };
113 |
114 | @Override
115 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
116 | super.onRequestPermissionsResult(requestCode, permissions, grantResults);
117 | Log.d(TAG,requestCode+"====");
118 | switch (requestCode) {
119 | case REQUEST_EXTERNAL_STORAGE:
120 | if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
121 | startService();
122 | } else {
123 | Toast.makeText(PromptDialogActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
124 | }
125 | break;
126 | default:
127 | }
128 | }
129 |
130 | public static void startActivity(Context context,AppInfoModel appInfoModel) {
131 | if(appInfoModel!=null){
132 | Intent intent = new Intent(context,PromptDialogActivity.class);
133 | intent.putExtra(INTENT_DOWNLOAD_MODEL,appInfoModel);
134 | context.startActivity(intent);
135 |
136 | }
137 | }
138 |
139 | public void startService(){
140 | if(FileUtils.getFile(appInfoModel).exists() && FileUtils.getFileSize(FileUtils.getFile(appInfoModel))==appInfoModel.getBinary().getFsize()){
141 | tvSize.setText(getResources().getString(R.string.most_version_downloaded));
142 | InstallApk.startInstall(PromptDialogActivity.this,FileUtils.getFile(appInfoModel));
143 | finish();
144 | }else{
145 | DownloadFileService.startDownloadFileService(PromptDialogActivity.this,appInfoModel);
146 | finish();
147 | }
148 |
149 | }
150 |
151 | @Override
152 | protected void onDestroy() {
153 | super.onDestroy();
154 | }
155 |
156 | @Override
157 | protected void onRestoreInstanceState(Bundle savedInstanceState) {
158 | Log.d(TAG,"==onRestoreInstanceState==");
159 | super.onRestoreInstanceState(savedInstanceState);
160 | appInfoModel = (AppInfoModel) savedInstanceState.getSerializable(PromptDialogActivity.INTENT_DOWNLOAD_MODEL);
161 | }
162 |
163 | @Override
164 | protected void onSaveInstanceState(Bundle outState) {
165 | super.onSaveInstanceState(outState);
166 | Log.d(TAG,"==onSaveInstanceState==");
167 | outState.putSerializable(PromptDialogActivity.INTENT_DOWNLOAD_MODEL,appInfoModel);
168 | }
169 |
170 |
171 | @Override
172 | protected void onPause() {
173 | super.onPause();
174 | Log.d(TAG,"==onPause==");
175 | WindowManager.LayoutParams lp = getWindow().getAttributes();
176 | lp.alpha = 0.0f;
177 | lp.dimAmount = 0.0f;
178 | getWindow().setAttributes(lp);
179 | }
180 |
181 | @Override
182 | protected void onStart() {
183 | super.onStart();
184 | Log.d(TAG,"==onStart==");
185 | }
186 |
187 | @Override
188 | protected void onResume() {
189 | super.onResume();
190 | WindowManager.LayoutParams lp = getWindow().getAttributes();
191 | lp.alpha = 1.0f;
192 | lp.dimAmount = 0.5f;
193 | getWindow().setAttributes(lp);
194 | Log.d(TAG,"==onResume==");
195 | }
196 |
197 | @Override
198 | protected void onRestart() {
199 | super.onRestart();
200 | Log.d(TAG,"==onRestart==");
201 | }
202 |
203 | @Override
204 | protected void onStop() {
205 | super.onStop();
206 | Log.d(TAG,"==onStop==");
207 | }
208 | }
209 |
--------------------------------------------------------------------------------
/myupdateapklibrary/src/main/java/com/trycath/myupdateapklibrary/exception/CustomizeException.java:
--------------------------------------------------------------------------------
1 | package com.trycath.myupdateapklibrary.exception;
2 |
3 | /**
4 | * 在此写用途
5 | *
6 | * @author: guoyoujin
7 | * @mail: guoyoujin123@gmail.com
8 | * @date: 2016-09-9 12:10
9 | * @version: V1.0 <描述当前版本功能>
10 | */
11 | public class CustomizeException extends RuntimeException {
12 | public CustomizeException(String message, Throwable cause) {
13 | super(message, cause);
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/myupdateapklibrary/src/main/java/com/trycath/myupdateapklibrary/httprequest/DownloadServiceApi.java:
--------------------------------------------------------------------------------
1 | package com.trycath.myupdateapklibrary.httprequest;
2 |
3 | import com.trycath.myupdateapklibrary.model.AppInfoModel;
4 |
5 | import java.util.List;
6 |
7 | import okhttp3.ResponseBody;
8 | import retrofit2.Call;
9 | import retrofit2.http.GET;
10 | import retrofit2.http.Path;
11 | import retrofit2.http.Query;
12 | import retrofit2.http.Url;
13 | import rx.Observable;
14 |
15 | /**
16 | * 在此写用途
17 | *
18 | * @author: guoyoujin
19 | * @mail: guoyoujin123@gmail.com
20 | * @date: 2016-09-6 12:10
21 | * @version: V1.0 <描述当前版本功能>
22 | */
23 | public interface DownloadServiceApi {
24 | @GET
25 | Observable download(@Url String url);
26 |
27 | @GET("/apps/latest/{app_id}")
28 | Observable getUpdateApkInfo(@Path("app_id") String app_id , @Query("api_token") String api_token);
29 |
30 | @GET
31 | Call> downloadSync(@Url String url);
32 | }
33 |
--------------------------------------------------------------------------------
/myupdateapklibrary/src/main/java/com/trycath/myupdateapklibrary/listener/AppUpdateListener.java:
--------------------------------------------------------------------------------
1 | package com.trycath.myupdateapklibrary.listener;
2 |
3 | import com.trycath.myupdateapklibrary.model.AppInfoModel;
4 |
5 | /**
6 | * 在此写用途
7 | *
8 | * @author: guoyoujin
9 | * @mail: guoyoujin123@gmail.com
10 | * @date: 2016-09-18 11:15
11 | * @version: V1.0 <描述当前版本功能>
12 | */
13 |
14 | public interface AppUpdateListener {
15 | static final String TAG = "AppUpdateListener";
16 | void onStart();
17 | void onCompleted();
18 | void onError(Throwable e);
19 | void onNext(AppInfoModel appInfoModel);
20 | void onNext(AppInfoModel appInfoModel,int state);
21 | }
--------------------------------------------------------------------------------
/myupdateapklibrary/src/main/java/com/trycath/myupdateapklibrary/listener/HttpClientHelper.java:
--------------------------------------------------------------------------------
1 | package com.trycath.myupdateapklibrary.listener;
2 |
3 | import java.io.IOException;
4 | import java.util.concurrent.TimeUnit;
5 |
6 | import okhttp3.Interceptor;
7 | import okhttp3.OkHttpClient;
8 | import okhttp3.Response;
9 | import okhttp3.logging.HttpLoggingInterceptor;
10 |
11 | /**
12 | * 在此写用途
13 | *
14 | * @author: guoyoujin
15 | * @mail: guoyoujin123@gmail.com
16 | * @date: 2016-09-12 14:52
17 | * @version: V1.0 <描述当前版本功能>
18 | */
19 |
20 | public class HttpClientHelper {
21 | /**
22 | * 包装OkHttpClient,用于下载文件的回调
23 | * @param progressListener 进度回调接口
24 | * @return 包装后的OkHttpClient
25 | */
26 | public static OkHttpClient addProgressResponseListener(final ProgressResponseListener progressListener){
27 | OkHttpClient.Builder client = new OkHttpClient.Builder();
28 | client.connectTimeout(60 * 1000, TimeUnit.MILLISECONDS)
29 | .readTimeout(60 * 1000, TimeUnit.MILLISECONDS)
30 | .retryOnConnectionFailure(true)
31 | .connectTimeout(15, TimeUnit.SECONDS)
32 | .readTimeout(20, TimeUnit.SECONDS);
33 | //增加拦截器
34 | client.addInterceptor(new Interceptor() {
35 | @Override
36 | public Response intercept(Chain chain) throws IOException {
37 | //拦截
38 | Response originalResponse = chain.proceed(chain.request());
39 | //包装响应体并返回
40 | return originalResponse.newBuilder()
41 | .body(new ProgressResponseBody(originalResponse.body(), progressListener))
42 | .build();
43 | }
44 | });
45 | return client.build();
46 | }
47 | public static OkHttpClient getOkhttp(){
48 | HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
49 | interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
50 | return new OkHttpClient.Builder()
51 | .connectTimeout(60 * 1000, TimeUnit.MILLISECONDS)
52 | .readTimeout(60 * 1000, TimeUnit.MILLISECONDS)
53 | .addInterceptor(interceptor)
54 | .retryOnConnectionFailure(true)
55 | .connectTimeout(15, TimeUnit.SECONDS)
56 | .readTimeout(20, TimeUnit.SECONDS)
57 | .build();
58 | }
59 | }
--------------------------------------------------------------------------------
/myupdateapklibrary/src/main/java/com/trycath/myupdateapklibrary/listener/ProgressResponseBody.java:
--------------------------------------------------------------------------------
1 | package com.trycath.myupdateapklibrary.listener;
2 |
3 | import java.io.IOException;
4 |
5 | import okhttp3.MediaType;
6 | import okhttp3.ResponseBody;
7 | import okio.Buffer;
8 | import okio.BufferedSource;
9 | import okio.ForwardingSource;
10 | import okio.Okio;
11 | import okio.Source;
12 |
13 | /**
14 | * 在此写用途
15 | *
16 | * @author: guoyoujin
17 | * @mail: guoyoujin123@gmail.com
18 | * @date: 2016-09-12 14:49
19 | * @version: V1.0 <描述当前版本功能>
20 | */
21 |
22 | public class ProgressResponseBody extends ResponseBody {
23 | //实际的待包装响应体
24 | private final ResponseBody responseBody;
25 | //进度回调接口
26 | private final ProgressResponseListener progressListener;
27 | //包装完成的BufferedSource
28 | private BufferedSource bufferedSource;
29 |
30 | /**
31 | * 构造函数,赋值
32 | *
33 | * @param responseBody 待包装的响应体
34 | * @param progressListener 回调接口
35 | */
36 | public ProgressResponseBody(ResponseBody responseBody, ProgressResponseListener progressListener) {
37 | this.responseBody = responseBody;
38 | this.progressListener = progressListener;
39 | }
40 |
41 |
42 | /**
43 | * 重写调用实际的响应体的contentType
44 | *
45 | * @return MediaType
46 | */
47 | @Override
48 | public MediaType contentType() {
49 | return responseBody.contentType();
50 | }
51 |
52 | /**
53 | * 重写调用实际的响应体的contentLength
54 | *
55 | * @return contentLength
56 | * @throws IOException 异常
57 | */
58 | @Override
59 | public long contentLength() {
60 | return responseBody.contentLength();
61 | }
62 |
63 | /**
64 | * 重写进行包装source
65 | *
66 | * @return BufferedSource
67 | */
68 | @Override
69 | public BufferedSource source() {
70 | if (bufferedSource == null) {
71 | //包装
72 | bufferedSource = Okio.buffer(source(responseBody.source()));
73 | }
74 | return bufferedSource;
75 | }
76 |
77 | /**
78 | * 读取,回调进度接口
79 | *
80 | * @param source Source
81 | * @return Source
82 | */
83 | private Source source(Source source) {
84 | return new ForwardingSource(source) {
85 | //当前读取字节数
86 | long totalBytesRead = 0L;
87 | @Override
88 | public long read(Buffer sink, long byteCount) throws IOException {
89 | long bytesRead = super.read(sink, byteCount);
90 | //增加当前读取的字节数,如果读取完成了bytesRead会返回-1
91 | totalBytesRead += bytesRead != -1 ? bytesRead : 0;
92 | //回调,如果contentLength()不知道长度,会返回-1
93 | if(progressListener != null){
94 | progressListener.onResponseProgress(totalBytesRead, responseBody.contentLength(), bytesRead == -1);
95 | }
96 | return bytesRead;
97 | }
98 | };
99 | }
100 | }
--------------------------------------------------------------------------------
/myupdateapklibrary/src/main/java/com/trycath/myupdateapklibrary/listener/ProgressResponseListener.java:
--------------------------------------------------------------------------------
1 | package com.trycath.myupdateapklibrary.listener;
2 |
3 | /**
4 | * 在此写用途
5 | *
6 | * @author: guoyoujin
7 | * @mail: guoyoujin123@gmail.com
8 | * @date: 2016-09-12 14:49
9 | * @version: V1.0 <描述当前版本功能>
10 | */
11 |
12 | public interface ProgressResponseListener {
13 | void onResponseProgress(long bytesRead, long contentLength, boolean done);
14 | }
--------------------------------------------------------------------------------
/myupdateapklibrary/src/main/java/com/trycath/myupdateapklibrary/listener/ServiceGenerator.java:
--------------------------------------------------------------------------------
1 | package com.trycath.myupdateapklibrary.listener;
2 |
3 | import com.google.gson.Gson;
4 | import com.google.gson.GsonBuilder;
5 | import com.trycath.myupdateapklibrary.UpdateKey;
6 |
7 | import retrofit2.Retrofit;
8 | import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
9 | import retrofit2.converter.gson.GsonConverterFactory;
10 |
11 | /**
12 | * 在此写用途
13 | *
14 | * @author: guoyoujin
15 | * @mail: guoyoujin123@gmail.com
16 | * @date: 2016-09-12 14:52
17 | * @version: V1.0 <描述当前版本功能>
18 | */
19 |
20 | public class ServiceGenerator {
21 | private static final String HOST = UpdateKey.BASE_URL;
22 | private static Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").serializeNulls().create();
23 | private static Retrofit.Builder builder = new Retrofit.Builder().baseUrl(HOST)
24 | .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
25 | .addConverterFactory(GsonConverterFactory.create());
26 |
27 | public static T createService(Class tClass){
28 | return builder
29 | .client(HttpClientHelper.getOkhttp())
30 | .build()
31 | .create(tClass);
32 | }
33 |
34 | /**
35 | * 创建带响应进度(下载进度)回调的service
36 | */
37 | public static T createResponseService(Class tClass, ProgressResponseListener listener){
38 | return builder.client(HttpClientHelper.addProgressResponseListener(listener)).build().create(tClass);
39 | }
40 | }
--------------------------------------------------------------------------------
/myupdateapklibrary/src/main/java/com/trycath/myupdateapklibrary/model/AppInfoModel.java:
--------------------------------------------------------------------------------
1 | package com.trycath.myupdateapklibrary.model;
2 |
3 | import java.io.Serializable;
4 |
5 | /**
6 | * 在此写用途
7 | *
8 | * @author: guoyoujin
9 | * @mail: guoyoujin123@gmail.com
10 | * @date: 2016-08-26 14:09
11 | * @version: V1.0 <描述当前版本功能>
12 | */
13 |
14 | public class AppInfoModel implements Serializable{
15 | private static final String TAG = "AppInfoModel";
16 | private String name;
17 | private String version;
18 | private String changelog;
19 | private String versionShort;
20 | private String build;
21 | private String installUrl;
22 | private String install_url;
23 | private String direct_install_url;
24 | private String update_url;
25 | private BinaryModel binary;
26 | private long updated_at;
27 |
28 | public long getUpdated_at() {
29 | return updated_at;
30 | }
31 |
32 | public void setUpdated_at(long updated_at) {
33 | this.updated_at = updated_at;
34 | }
35 |
36 | @Override
37 | public String toString() {
38 | final StringBuffer sb = new StringBuffer("AppInfoModel{");
39 | sb.append("name='").append(name).append('\'');
40 | sb.append(", version='").append(version).append('\'');
41 | sb.append(", changelog='").append(changelog).append('\'');
42 | sb.append(", versionShort='").append(versionShort).append('\'');
43 | sb.append(", build='").append(build).append('\'');
44 | sb.append(", installUrl='").append(installUrl).append('\'');
45 | sb.append(", install_url='").append(install_url).append('\'');
46 | sb.append(", direct_install_url='").append(direct_install_url).append('\'');
47 | sb.append(", update_url='").append(update_url).append('\'');
48 | sb.append(", binary=").append(binary);
49 | sb.append(", updated_at=").append(updated_at);
50 | sb.append('}');
51 | return sb.toString();
52 | }
53 |
54 | public String getName() {
55 | return name;
56 | }
57 |
58 | public void setName(String name) {
59 | this.name = name;
60 | }
61 |
62 | public String getVersion() {
63 | return version;
64 | }
65 |
66 | public void setVersion(String version) {
67 | this.version = version;
68 | }
69 |
70 | public String getChangelog() {
71 | return changelog;
72 | }
73 |
74 | public void setChangelog(String changelog) {
75 | this.changelog = changelog;
76 | }
77 |
78 |
79 |
80 | public String getVersionShort() {
81 | return versionShort;
82 | }
83 |
84 | public void setVersionShort(String versionShort) {
85 | this.versionShort = versionShort;
86 | }
87 |
88 | public String getBuild() {
89 | return build;
90 | }
91 |
92 | public void setBuild(String build) {
93 | this.build = build;
94 | }
95 |
96 | public String getInstallUrl() {
97 | return installUrl;
98 | }
99 |
100 | public void setInstallUrl(String installUrl) {
101 | this.installUrl = installUrl;
102 | }
103 |
104 | public String getInstall_url() {
105 | return install_url;
106 | }
107 |
108 | public void setInstall_url(String install_url) {
109 | this.install_url = install_url;
110 | }
111 |
112 | public String getDirect_install_url() {
113 | return direct_install_url;
114 | }
115 |
116 | public void setDirect_install_url(String direct_install_url) {
117 | this.direct_install_url = direct_install_url;
118 | }
119 |
120 | public String getUpdate_url() {
121 | return update_url;
122 | }
123 |
124 | public void setUpdate_url(String update_url) {
125 | this.update_url = update_url;
126 | }
127 |
128 | public BinaryModel getBinary() {
129 | return binary;
130 | }
131 |
132 | public void setBinary(BinaryModel binary) {
133 | this.binary = binary;
134 | }
135 | }
--------------------------------------------------------------------------------
/myupdateapklibrary/src/main/java/com/trycath/myupdateapklibrary/model/BinaryModel.java:
--------------------------------------------------------------------------------
1 | package com.trycath.myupdateapklibrary.model;
2 |
3 | import java.io.Serializable;
4 |
5 | /**
6 | * 在此写用途
7 | *
8 | * @author: guoyoujin
9 | * @mail: guoyoujin123@gmail.com
10 | * @date: 2016-08-26 14:11
11 | * @version: V1.0 <描述当前版本功能>
12 | */
13 |
14 | public class BinaryModel implements Serializable{
15 | private long fsize;
16 |
17 | @Override
18 | public String toString() {
19 | final StringBuffer sb = new StringBuffer("BinaryModel{");
20 | sb.append("fsize=").append(fsize);
21 | sb.append('}');
22 | return sb.toString();
23 | }
24 |
25 | public long getFsize() {
26 | return fsize;
27 | }
28 |
29 | public void setFsize(long fsize) {
30 | this.fsize = fsize;
31 | }
32 | }
--------------------------------------------------------------------------------
/myupdateapklibrary/src/main/java/com/trycath/myupdateapklibrary/model/DownloadModel.java:
--------------------------------------------------------------------------------
1 | package com.trycath.myupdateapklibrary.model;
2 |
3 | import android.os.Parcel;
4 | import android.os.Parcelable;
5 |
6 |
7 | public class DownloadModel implements Parcelable {
8 |
9 | private int progress;
10 | private long currentFileSize;
11 | private long totalFileSize;
12 |
13 | public int getProgress() {
14 | return progress;
15 | }
16 |
17 | public void setProgress(int progress) {
18 | this.progress = progress;
19 | }
20 |
21 | public long getCurrentFileSize() {
22 | return currentFileSize;
23 | }
24 |
25 | public void setCurrentFileSize(long currentFileSize) {
26 | this.currentFileSize = currentFileSize;
27 | }
28 |
29 | public long getTotalFileSize() {
30 | return totalFileSize;
31 | }
32 |
33 | public void setTotalFileSize(long totalFileSize) {
34 | this.totalFileSize = totalFileSize;
35 | }
36 |
37 | @Override
38 | public int describeContents() {
39 | return 0;
40 | }
41 |
42 | @Override
43 | public void writeToParcel(Parcel dest, int flags) {
44 | dest.writeInt(this.progress);
45 | dest.writeLong(this.currentFileSize);
46 | dest.writeLong(this.totalFileSize);
47 | }
48 |
49 | public DownloadModel() {
50 | }
51 |
52 | protected DownloadModel(Parcel in) {
53 | this.progress = in.readInt();
54 | this.currentFileSize = in.readLong();
55 | this.totalFileSize = in.readLong();
56 | }
57 |
58 | public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
59 | @Override
60 | public DownloadModel createFromParcel(Parcel source) {
61 | return new DownloadModel(source);
62 | }
63 |
64 | @Override
65 | public DownloadModel[] newArray(int size) {
66 | return new DownloadModel[size];
67 | }
68 | };
69 | }
70 |
--------------------------------------------------------------------------------
/myupdateapklibrary/src/main/java/com/trycath/myupdateapklibrary/rxbus/RxBus.java:
--------------------------------------------------------------------------------
1 | package com.trycath.myupdateapklibrary.rxbus;
2 |
3 | import java.util.HashMap;
4 | import java.util.Map;
5 |
6 | import rx.android.schedulers.AndroidSchedulers;
7 | import rx.functions.Action1;
8 | import rx.schedulers.Schedulers;
9 | import rx.subjects.PublishSubject;
10 | import rx.subjects.SerializedSubject;
11 | import rx.subjects.Subject;
12 |
13 | /**
14 | * 在此写用途
15 | *
16 | * @author: guoyoujin
17 | * @mail: guoyoujin123@gmail.com
18 | * @date: 2016-09-13 10:08
19 | * @version: V1.0 <描述当前版本功能>
20 | */
21 |
22 | public class RxBus {
23 | private static final String TAG = "RxBus";
24 | private final Subject