Skip to content

Commit d203301

Browse files
author
jingtian
committed
修改版本更新功能
1 parent c408114 commit d203301

File tree

9 files changed

+291
-228
lines changed

9 files changed

+291
-228
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ dependencies {
3131
testCompile 'junit:junit:4.12'
3232
compile project(':library')
3333
compile 'org.byteam.superadapter:superadapter:3.6.6'
34-
// compile 'com.github.LuoGuoXin:BaseAndroid:1.0.0'
34+
// compile 'com.github.LuoGuoXin:BaseAndroid:1.0.4'
3535
compile 'com.android.support:recyclerview-v7:24.2.1'
3636
compile 'com.malinskiy:superrecyclerview:1.1.4'
3737

app/src/main/java/luo/myapplication/MainActivity.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ private void newsList(View view) {
5353
openActivity(NewsListActivity.class);
5454
}
5555

56+
@Event(R.id.btn_update)
57+
private void update(View view) {
58+
BaseAndroid.checkUpdate(MainActivity.this, 2,
59+
"http://f5.market.mi-img.com/download/AppStore/0f4a347f5ce5a7e01315dda1ec35944fa56431d44/luo.footprint.apk", "更新了XXX\n修复OOO", true);
60+
}
61+
5662
//常用操作
5763
void init() {
5864
//设置标题栏标题,记得先在布局上面添加标题栏布局: <include layout="@layout/titleview_layout"/>
@@ -107,7 +113,7 @@ public void onClick(View v) {
107113
clearSp();
108114

109115
//版本更新
110-
BaseAndroid.checkUpdate(MainActivity.this,findViewById(R.id.activity_main), 2, "http://f5.market.mi-img.com/download/AppStore/0f4a347f5ce5a7e01315dda1ec35944fa56431d44/luo.footprint.apk", "更新了XXX\n修复OOO", false);
116+
BaseAndroid.checkUpdate(MainActivity.this, 2, "http://f5.market.mi-img.com/download/AppStore/0f4a347f5ce5a7e01315dda1ec35944fa56431d44/luo.footprint.apk", "更新了XXX\n修复OOO", false);
111117

112118
}
113119

app/src/main/res/layout/activity_main.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,10 @@
2929
android:layout_height="wrap_content"
3030
android:text="网络请求的使用" />
3131

32+
<Button
33+
android:id="@+id/btn_update"
34+
android:layout_width="match_parent"
35+
android:layout_height="wrap_content"
36+
android:text="版本更新的使用" />
37+
3238
</LinearLayout>

library/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ android {
2424
}
2525

2626
dependencies {
27-
compile 'org.xutils:xutils:3.3.40'
27+
compile 'org.xutils:xutils:3.3.42'
2828
compile 'com.github.bumptech.glide:glide:3.7.0'
2929
compile 'com.google.code.gson:gson:2.8.0'
3030
compile 'com.android.support:support-fragment:24.2.1'

library/src/main/java/luo/library/base/base/BaseAndroid.java

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package luo.library.base.base;
22

33
import android.content.Context;
4-
import android.content.pm.PackageInfo;
5-
import android.content.pm.PackageManager;
6-
import android.net.ConnectivityManager;
7-
import android.net.NetworkInfo;
8-
import android.view.View;
4+
import android.os.Environment;
5+
import android.text.TextUtils;
6+
7+
import java.io.File;
98

109
import luo.library.base.utils.UpdateManager;
1110

@@ -36,7 +35,7 @@ public static BaseConfig getBaseConfig() {
3635
* @param updateMessage 更新内容
3736
* @param isForced 是否强制更新
3837
*/
39-
public static void checkUpdate(Context context, View view, int versionCode, String url, String updateMessage, boolean isForced) {
38+
public static void checkUpdate(Context context, int versionCode, String url, String updateMessage, boolean isForced) {
4039
if (versionCode > UpdateManager.getInstance().getVersionCode(context)) {
4140
int type = 0;//更新方式,0:引导更新,1:安装更新,2:强制更新
4241
if (UpdateManager.getInstance().isWifi(context)) {
@@ -45,21 +44,25 @@ public static void checkUpdate(Context context, View view, int versionCode, Stri
4544
if (isForced) {
4645
type = 2;
4746
}
47+
48+
//检测是否已下载
49+
String downLoadPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/downloads/";
50+
File dir = new File(downLoadPath);
51+
if (!dir.exists()) {
52+
dir.mkdir();
53+
}
54+
String fileName = url.substring(url.lastIndexOf("/") + 1, url.length());
55+
if (fileName == null && TextUtils.isEmpty(fileName) && !fileName.contains(".apk")) {
56+
fileName = context.getPackageName() + ".apk";
57+
}
58+
File file = new File(downLoadPath + fileName);
59+
4860
//设置参数
49-
UpdateManager.getInstance().setView(view).setType(type).setUrl(url).setUpdateMessage(updateMessage);
50-
switch (type) {
51-
case 0:
52-
//非wifi情况下,先弹框后下载
53-
UpdateManager.getInstance().showPop(context);
54-
break;
55-
case 1:
56-
//wifi情况下,先下载后弹框
57-
UpdateManager.getInstance().downloadFile(context, false);
58-
break;
59-
case 2:
60-
//强制更新情况下,无论是否wifi都应该是先弹框
61-
UpdateManager.getInstance().showPop(context);
62-
break;
61+
UpdateManager.getInstance().setType(type).setUrl(url).setUpdateMessage(updateMessage).setFileName(fileName).setIsDownload(file.exists());
62+
if (type == 1 && !file.exists()) {
63+
UpdateManager.getInstance().downloadFile(context, false);
64+
} else {
65+
UpdateManager.getInstance().showDialog(context);
6366
}
6467
}
6568

library/src/main/java/luo/library/base/utils/UpdateManager.java

Lines changed: 78 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,8 @@
1111
import android.net.Uri;
1212
import android.os.Environment;
1313
import android.text.TextUtils;
14-
import android.view.Gravity;
15-
import android.view.LayoutInflater;
1614
import android.view.View;
17-
import android.view.ViewGroup;
18-
import android.widget.PopupWindow;
19-
import android.widget.RelativeLayout;
2015
import android.widget.RemoteViews;
21-
import android.widget.TextView;
2216
import android.widget.Toast;
2317

2418
import org.xutils.common.Callback;
@@ -30,21 +24,23 @@
3024

3125
import luo.library.R;
3226
import luo.library.base.base.BaseAndroid;
27+
import luo.library.base.widget.BaseDialog;
3328

3429
/**
3530
* 版本更新
3631
*/
3732

3833
public class UpdateManager {
39-
public String DOWNLOAD_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/downloads/";
34+
public String downLoadPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/downloads/";
4035
public int type = 0;//更新方式,0:引导更新,1:安装更新,2:强制更新
4136
public String url = "";//apk下载地址
4237
public String updateMessage = "";//更新内容
43-
public View view;//activity根布局的view
4438
public String fileName = null;//文件名
39+
public boolean isDownload = false;//是否下载
4540
public Notification notification;
4641
public RemoteViews contentView;
4742
public NotificationManager notificationManager;
43+
public BaseDialog dialog;
4844

4945
public static UpdateManager updateManager;
5046

@@ -59,119 +55,63 @@ private UpdateManager() {
5955

6056
}
6157

62-
public UpdateManager setUrl(String url) {
63-
this.url = url;
64-
return this;
65-
}
66-
67-
68-
public UpdateManager setType(int type) {
69-
this.type = type;
70-
return this;
71-
}
72-
73-
74-
public UpdateManager setUpdateMessage(String updateMessage) {
75-
this.updateMessage = updateMessage;
76-
return this;
77-
}
78-
79-
80-
public UpdateManager setView(View view) {
81-
this.view = view;
82-
return this;
83-
}
84-
8558
/**
8659
* 弹出版本更新提示框
8760
*/
88-
public void showPop(final Context context) {
89-
View contentView = LayoutInflater.from(context).inflate(R.layout.base_dialog, null);
90-
final PopupWindow popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
91-
final TextView title = (TextView) contentView.findViewById(R.id.tv_title);
92-
TextView left = (TextView) contentView.findViewById(R.id.tv_left);
93-
TextView right = (TextView) contentView.findViewById(R.id.tv_right);
94-
TextView message = (TextView) contentView.findViewById(R.id.tv_message);
95-
RelativeLayout relativeLayout = (RelativeLayout) contentView.findViewById(R.id.rl);
96-
97-
//根据更新方式显示不同的文案
98-
if (type == 0) {
99-
title.setText("发现新版本");
100-
left.setText("立即更新");
61+
public void showDialog(final Context context) {
62+
String title = "";
63+
String left = "";
64+
boolean cancelable = true;
65+
if (type == 1 | isDownload) {
66+
title = "安装新版本";
67+
left = "立即安装";
10168
} else {
102-
title.setText("安装新版本");
103-
left.setText("立即安装");
69+
title = "发现新版本";
70+
left = "立即更新";
10471
}
105-
right.setText("取消");
106-
message.setText(updateMessage);
107-
//升级按钮,根据更新方式来做不同的操作,0和2是下载apk并显示通知栏,1是直接安装apk
108-
left.setOnClickListener(new View.OnClickListener() {
109-
@Override
110-
public void onClick(View view) {
111-
popupWindow.dismiss();
112-
if (type == 0 | type == 2) {
113-
if (url != null && !TextUtils.isEmpty(url)) {
114-
createNotification(context);
115-
downloadFile(context, true);
116-
} else {
117-
Toast.makeText(context, "下载地址错误", Toast.LENGTH_SHORT).show();
118-
}
119-
} else {
120-
installApk(context, new File(DOWNLOAD_PATH, fileName));
121-
}
122-
123-
}
124-
});
125-
//取消按钮,当更新方式为强制更新时,直接退出
126-
right.setOnClickListener(new View.OnClickListener() {
127-
@Override
128-
public void onClick(View view) {
129-
popupWindow.dismiss();
130-
if (type == 2) {
131-
System.exit(0);
132-
}
133-
}
134-
});
135-
//点击外边区域消失窗口
136-
if (type != 2) {
137-
relativeLayout.setOnClickListener(new View.OnClickListener() {
138-
@Override
139-
public void onClick(View view) {
140-
popupWindow.dismiss();
141-
}
142-
});
72+
if (type == 2) {
73+
cancelable = false;
14374
}
144-
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
75+
dialog = new BaseDialog.Builder(context).setTitle(title).setMessage(updateMessage).setCancelable(cancelable)
76+
.setLeftClick(left, new View.OnClickListener() {
77+
@Override
78+
public void onClick(View view) {
79+
dialog.dismiss();
80+
if (type == 1 | isDownload) {
81+
installApk(context, new File(downLoadPath, fileName));
82+
} else {
83+
if (url != null && !TextUtils.isEmpty(url)) {
84+
createNotification(context);
85+
downloadFile(context, true);
86+
} else {
87+
Toast.makeText(context, "下载地址错误", Toast.LENGTH_SHORT).show();
88+
}
89+
}
90+
}
91+
})
92+
.setRightClick("取消", new View.OnClickListener() {
93+
@Override
94+
public void onClick(View view) {
95+
dialog.dismiss();
96+
if (type == 2) {
97+
System.exit(0);
98+
}
99+
}
100+
})
101+
.create();
102+
dialog.show();
145103
}
146104

147105

148106
/**
149107
* 下载apk
108+
*
150109
* @param context
151-
* @param installApk 下载完成后是否需要安装
110+
* @param installApk 下载完成后是否立即安装
152111
*/
153112
public void downloadFile(final Context context, final boolean installApk) {
154-
File dir = new File(DOWNLOAD_PATH);
155-
if (!dir.exists()) {
156-
dir.mkdir();
157-
}
158-
//截取apk的文件名
159-
fileName = url.substring(url.lastIndexOf("/") + 1, url.length());
160-
if (fileName == null && TextUtils.isEmpty(fileName) && !fileName.contains(".apk")) {
161-
fileName = context.getPackageName() + ".apk";
162-
}
163-
//判断是否存在了这个apk,存在了就不下载了,直接安装或者弹框提示
164-
File file = new File(DOWNLOAD_PATH + fileName);
165-
if (file.exists()) {
166-
if (installApk) {
167-
installApk(context, new File(DOWNLOAD_PATH, fileName));
168-
} else {
169-
showPop(context);
170-
}
171-
return;
172-
}
173113
RequestParams params = new RequestParams(url);
174-
params.setSaveFilePath(DOWNLOAD_PATH + fileName);
114+
params.setSaveFilePath(downLoadPath + fileName);
175115
x.http().request(HttpMethod.GET, params, new Callback.ProgressCallback<File>() {
176116

177117
@Override
@@ -181,7 +121,7 @@ public void onSuccess(File result) {
181121

182122
@Override
183123
public void onError(Throwable ex, boolean isOnCallback) {
184-
Toast.makeText(context, "网络错误", Toast.LENGTH_SHORT).show();
124+
Toast.makeText(context, ex.getMessage(), Toast.LENGTH_SHORT).show();
185125
}
186126

187127
@Override
@@ -211,10 +151,11 @@ public void onLoading(long total, long current, boolean isDownloading) {
211151
notifyNotification(current, total);
212152
}
213153
if (total == current) {
154+
notificationManager.cancelAll();
214155
if (installApk) {
215-
installApk(context, new File(DOWNLOAD_PATH, fileName));
156+
installApk(context, new File(downLoadPath, fileName));
216157
} else {
217-
showPop(context);
158+
showDialog(context);
218159
}
219160
}
220161
}
@@ -236,14 +177,14 @@ public void createNotification(Context context) {
236177
notification.contentView = contentView;
237178
notification.flags = Notification.DEFAULT_ALL;
238179
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
239-
notificationManager.notify(R.layout.notification_item, notification);
180+
notificationManager.notify(0, notification);
240181
}
241182

242183
public void notifyNotification(long percent, long length) {
243184
contentView.setTextViewText(R.id.tv_progress, (percent * 100 / length) + "%");
244185
contentView.setProgressBar(R.id.progress, (int) length, (int) percent, false);
245186
notification.contentView = contentView;
246-
notificationManager.notify(R.layout.notification_item, notification);
187+
notificationManager.notify(0, notification);
247188
}
248189

249190
/**
@@ -282,11 +223,35 @@ public boolean isWifi(Context mContext) {
282223
ConnectivityManager connectivityManager = (ConnectivityManager) mContext
283224
.getSystemService(Context.CONNECTIVITY_SERVICE);
284225
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
285-
if (activeNetInfo != null
286-
&& activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
226+
if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
287227
return true;
288228
}
289229
return false;
290230
}
291231

232+
public UpdateManager setUrl(String url) {
233+
this.url = url;
234+
return this;
235+
}
236+
237+
public UpdateManager setType(int type) {
238+
this.type = type;
239+
return this;
240+
}
241+
242+
public UpdateManager setUpdateMessage(String updateMessage) {
243+
this.updateMessage = updateMessage;
244+
return this;
245+
}
246+
247+
public UpdateManager setFileName(String fileName) {
248+
this.fileName = fileName;
249+
return this;
250+
}
251+
252+
public UpdateManager setIsDownload(boolean download) {
253+
isDownload = download;
254+
return this;
255+
}
256+
292257
}

0 commit comments

Comments
 (0)