UpdateDialog.java
12 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package com.studymachine.www.ui.dialog;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.core.app.NotificationCompat;
import androidx.core.content.FileProvider;
import com.hjq.base.BaseDialog;
import com.studymachine.www.R;
import com.studymachine.www.aop.CheckNet;
import com.studymachine.www.aop.Permissions;
import com.studymachine.www.aop.SingleClick;
import com.studymachine.www.other.AppConfig;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.OnDownloadListener;
import com.hjq.http.model.HttpMethod;
import com.hjq.permissions.Permission;
import java.io.File;
/**
* desc : 升级对话框
*/
public final class UpdateDialog {
public static final class Builder
extends BaseDialog.Builder<Builder> {
private final TextView mNameView;
private final TextView mContentView;
private final ProgressBar mProgressView;
private final TextView mUpdateView;
private final TextView mCloseView;
/** Apk 文件 */
private File mApkFile;
/** 下载地址 */
private String mDownloadUrl;
/** 文件 MD5 */
private String mFileMd5;
/** 是否强制更新 */
private boolean mForceUpdate;
/** 当前是否下载中 */
private boolean mDownloading;
/** 当前是否下载完毕 */
private boolean mDownloadComplete;
public Builder(Context context) {
super(context);
setContentView(R.layout.update_dialog);
setAnimStyle(BaseDialog.ANIM_BOTTOM);
setCancelable(false);
mNameView = findViewById(R.id.tv_update_name);
mContentView = findViewById(R.id.tv_update_content);
mProgressView = findViewById(R.id.pb_update_progress);
mUpdateView = findViewById(R.id.tv_update_update);
mCloseView = findViewById(R.id.tv_update_close);
setOnClickListener(mUpdateView, mCloseView);
// 让 TextView 支持滚动
mContentView.setMovementMethod(new ScrollingMovementMethod());
}
/**
* 设置版本名
*/
public Builder setVersionName(CharSequence name) {
mNameView.setText(name);
return this;
}
/**
* 设置更新日志
*/
public Builder setUpdateLog(CharSequence text) {
mContentView.setText(text);
mContentView.setVisibility(text == null ? View.GONE : View.VISIBLE);
return this;
}
/**
* 设置强制更新
*/
public Builder setForceUpdate(boolean force) {
mForceUpdate = force;
mCloseView.setVisibility(force ? View.GONE : View.VISIBLE);
setCancelable(!force);
return this;
}
/**
* 设置下载 url
*/
public Builder setDownloadUrl(String url) {
mDownloadUrl = url;
return this;
}
/**
* 设置文件 md5
*/
public Builder setFileMd5(String md5) {
mFileMd5 = md5;
return this;
}
@SingleClick
@Override
public void onClick(View view) {
if (view == mCloseView) {
dismiss();
} else if (view == mUpdateView) {
// 判断下载状态
if (mDownloadComplete) {
if (mApkFile.isFile()) {
// 下载完毕,安装 Apk
installApk();
} else {
// 下载失败,重新下载
downloadApk();
}
} else if (!mDownloading) {
// 没有下载,开启下载
downloadApk();
}
}
}
/**
* 下载 Apk
*/
@CheckNet
@Permissions({Permission.READ_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE, Permission.REQUEST_INSTALL_PACKAGES})
private void downloadApk() {
// 设置对话框不能被取消
setCancelable(false);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
int notificationId = getContext().getApplicationInfo().uid;
String channelId = "";
// 适配 Android 8.0 通知渠道新特性
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(getString(R.string.update_notification_channel_id), getString(R.string.update_notification_channel_name), NotificationManager.IMPORTANCE_LOW);
channel.enableLights(false);
channel.enableVibration(false);
channel.setVibrationPattern(new long[]{0});
channel.setSound(null, null);
notificationManager.createNotificationChannel(channel);
channelId = channel.getId();
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getContext(), channelId)
// 设置通知时间
.setWhen(System.currentTimeMillis())
// 设置通知标题
.setContentTitle(getString(R.string.app_name))
// 设置通知小图标
.setSmallIcon(R.mipmap.launcher_ic)
// 设置通知大图标
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.launcher_ic))
// 设置通知静音
.setDefaults(NotificationCompat.FLAG_ONLY_ALERT_ONCE)
// 设置震动频率
.setVibrate(new long[]{0})
// 设置声音文件
.setSound(null)
// 设置通知的优先级
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
// 创建要下载的文件对象
mApkFile = new File(getContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS),
getString(R.string.app_name) + "_v" + mNameView.getText().toString() + ".apk");
EasyHttp.download(getDialog())
.method(HttpMethod.GET)
.file(mApkFile)
.url(mDownloadUrl)
.md5(mFileMd5)
.listener(new OnDownloadListener() {
@Override
public void onStart(File file) {
// 标记为下载中
mDownloading = true;
// 标记成未下载完成
mDownloadComplete = false;
// 后台更新
mCloseView.setVisibility(View.GONE);
// 显示进度条
mProgressView.setVisibility(View.VISIBLE);
mUpdateView.setText(R.string.update_status_start);
}
@Override
public void onProgress(File file, int progress) {
mUpdateView.setText(String.format(getString(R.string.update_status_running), progress));
mProgressView.setProgress(progress);
// 更新下载通知
notificationManager.notify(notificationId, notificationBuilder
// 设置通知的文本
.setContentText(String.format(getString(R.string.update_status_running), progress))
// 设置下载的进度
.setProgress(100, progress, false)
// 设置点击通知后是否自动消失
.setAutoCancel(false)
// 是否正在交互中
.setOngoing(true)
// 重新创建新的通知对象
.build());
}
@Override
public void onComplete(File file) {
// 显示下载成功通知
notificationManager.notify(notificationId, notificationBuilder
// 设置通知的文本
.setContentText(String.format(getString(R.string.update_status_successful), 100))
// 设置下载的进度
.setProgress(100, 100, false)
// 设置通知点击之后的意图
.setContentIntent(PendingIntent.getActivity(getContext(), 1, getInstallIntent(), Intent.FILL_IN_ACTION))
// 设置点击通知后是否自动消失
.setAutoCancel(true)
// 是否正在交互中
.setOngoing(false)
.build());
mUpdateView.setText(R.string.update_status_successful);
// 标记成下载完成
mDownloadComplete = true;
// 安装 Apk
installApk();
}
@SuppressWarnings("ResultOfMethodCallIgnored")
@Override
public void onError(File file, Exception e) {
// 清除通知
notificationManager.cancel(notificationId);
mUpdateView.setText(R.string.update_status_failed);
// 删除下载的文件
file.delete();
}
@Override
public void onEnd(File file) {
// 更新进度条
mProgressView.setProgress(0);
mProgressView.setVisibility(View.GONE);
// 标记当前不是下载中
mDownloading = false;
// 如果当前不是强制更新,对话框就恢复成可取消状态
if (!mForceUpdate) {
setCancelable(true);
}
}
}).start();
}
/**
* 安装 Apk
*/
@Permissions({Permission.REQUEST_INSTALL_PACKAGES})
private void installApk() {
getContext().startActivity(getInstallIntent());
}
/**
* 获取安装意图
*/
private Intent getInstallIntent() {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri = FileProvider.getUriForFile(getContext(), AppConfig.getPackageName() + ".provider", mApkFile);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
} else {
uri = Uri.fromFile(mApkFile);
}
intent.setDataAndType(uri, "application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
return intent;
}
}
}