Linjiajia
2023-08-27 1df231fd6aafa221aef3532d069c7e27d4331be7
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package com.application.zhangshi_app_android.ui.dialog;
 
import android.Manifest;
import android.animation.ValueAnimator;
import android.app.DownloadManager;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
 
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;
 
 
import com.android.app_base.base.dialog.BaseDialog;
import com.android.app_base.manager.UserManager;
import com.android.app_base.utils.ToastUtils;
import com.android.app_base.utils.Utils;
import com.application.zhangshi_app_android.MyApplication;
import com.application.zhangshi_app_android.R;
import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.PermissionUtils;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.OnDownloadListener;
import com.hjq.http.model.HttpMethod;
 
import java.io.File;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;
 
 
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 fileName;
        /** 下载地址 */
        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.dialog_update);
            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);
            setCanceledOnTouchOutside(!force);
            return this;
        }
 
        /**
         * 设置下载 url
         */
        public Builder setDownloadUrl(String url) {
            mDownloadUrl = url;
            return this;
        }
 
        /**
         * 设置文件 md5
         */
        public Builder setFileMd5(String md5) {
            mFileMd5 = md5;
            return this;
        }
 
        @Override
        public void onClick(View view) {
            if (view == mCloseView) {
                dismiss();
            } else if (view == mUpdateView) {
                fileName = getContext().getString(R.string.app_name) + "_v" + mNameView.getText().toString() + ".apk";
                // 创建要下载的文件对象
//                mApkFile = new File(getContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), fileName);
                String pathname = Environment.getExternalStorageDirectory().getPath() + "/" + Environment.DIRECTORY_DOWNLOADS + "/" + fileName;
                mApkFile = new File(pathname);
 
                if (mApkFile.isFile()) {
                    // 下载完毕,安装 Apk
                    Utils.installAPK(getContext(),fileName);
                } else {
                    // 判断下载状态
                    if (mDownloadComplete) {
                        // 下载完毕,安装 Apk
//                        installApk();
                        Utils.installAPK(getContext(),fileName);
                    } else if (!mDownloading) {
                        // 没有下载,开启下载
                        downloadApk();
                    }
                }
 
            }
        }
 
        /**
         * 下载 Apk
         */
        private void downloadApk() {
            if (!Utils.checkNetwork()){
                ToastUtils.showShort("当前没有网络连接,请检查网络设置");
                return;
            }
 
            // 设置对话框不能被取消
            setCancelable(false);
 
//            NotificationManager notificationManager = ContextCompat.getSystemService(getContext(), NotificationManager.class);
//            int notificationId = getContext().getApplicationInfo().uid;
//            String channelId = "";
//            // 适配 Android 8.0 通知渠道新特性
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//                NotificationChannel channel = new NotificationChannel("update", "升级通知", NotificationManager.IMPORTANCE_LOW);
//                channel.enableLights(false);
//                channel.enableVibration(false);
//                channel.setVibrationPattern(new long[]{0});
//                channel.setSound(null, null);
//                if (notificationManager != null) {
//                    notificationManager.createNotificationChannel(channel);
//                }
//                channelId = channel.getId();
//            }
//
//            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getContext(), channelId)
//                    // 设置通知时间
//                    .setWhen(System.currentTimeMillis())
//                    // 设置通知标题
//                    .setContentTitle(getContext().getString(R.string.app_name))
//                    // 设置通知小图标
//                    .setSmallIcon(R.mipmap.ic_logo)
//                    // 设置通知大图标
//                    .setLargeIcon(BitmapFactory.decodeResource(getContext().getResources(), R.mipmap.ic_logo))
//                    // 设置通知静音
//                    .setDefaults(NotificationCompat.FLAG_ONLY_ALERT_ONCE)
//                    // 设置震动频率
//                    .setVibrate(new long[]{0})
//                    // 设置声音文件
//                    .setSound(null)
//                    // 设置通知的优先级
//                    .setPriority(NotificationCompat.PRIORITY_DEFAULT);
 
 
            //创建下载任务,downloadUrl就是下载链接
            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(mDownloadUrl));
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);//下载进行中和下载完成的通知栏是否显示
            request.allowScanningByMediaScanner();//设置允许被扫描到
            request.setVisibleInDownloadsUi(true);//下载的文件可以被系统的Downloads应用扫描到并管理
 
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName); //指定下载路径和下载文件名
            final DownloadManager downloadManager = (DownloadManager) MyApplication.getInstance().getSystemService(Context.DOWNLOAD_SERVICE);//获取下载管理器
            final long downloadID = downloadManager.enqueue(request);//将下载任务加入下载队列,否则不会进行下载
 
            final DownloadManager.Query query = new DownloadManager.Query();
            query.setFilterById(downloadID);//筛选下载任务,传入任务ID,可变参数
 
            // 标记为下载中
            mDownloading = true;
            // 标记成未下载完成
            mDownloadComplete = false;
            // 后台更新
            mCloseView.setVisibility(View.GONE);
            // 显示进度条
            mProgressView.setVisibility(View.VISIBLE);
            mUpdateView.setText("正在下载..");
 
            final Timer mTimer = new Timer();
            mTimer.schedule(new TimerTask() {
                @Override
                public void run() {
                    Cursor c = null;
                    try {
                        c = downloadManager.query(query);
                        if (c != null) {
                            if (c.moveToFirst()) {
                                // 下载状态
                                int status = c.getInt(c.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS));
                                // 已下载的字节大小
                                final long downloadedSoFar = c.getLong(c.getColumnIndexOrThrow(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
                                // 下载文件的总字节大小
                                final long totalSize = c.getLong(c.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
                                int progress = (int) (downloadedSoFar * 100 / totalSize);
                                switch (status) {
                                    case DownloadManager.STATUS_PAUSED:
                                        Log.d("APPUpdate", "下载暂停:" + fileName);
                                    case DownloadManager.STATUS_PENDING:
                                        Log.d("APPUpdate", "下载延迟:" + fileName);
                                    case DownloadManager.STATUS_RUNNING:
                                        Log.d("APPUpdate", "正在下载:" + fileName);
//                                        ValueAnimator animator = ValueAnimator.ofInt(mProgressView.getProgress(), progress);
//                                        animator.setDuration(100);
//                                        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//                                            @Override
//                                            public void onAnimationUpdate(ValueAnimator animation) {
//                                                int currentValue = (int) animation.getAnimatedValue();
//
//                                            }
//                                        });
//                                        animator.start();
                                        mUpdateView.setText(String.format(Locale.CHINA,"下载中 %d%%", progress));
                                        mProgressView.setProgress(progress);
//                                         更新下载通知
//                                        notificationManager.notify(notificationId, notificationBuilder
//                                                // 设置通知的文本
//                                                .setContentText(String.format(Locale.CHINA,"下载中 %d%%", progress))
//                                                // 设置下载的进度
//                                                .setProgress(100, progress, false)
//                                                // 设置点击通知后是否自动消失
//                                                .setAutoCancel(false)
//                                                // 是否正在交互中
//                                                .setOngoing(true)
//                                                // 重新创建新的通知对象
//                                                .build());
                                        break;
                                    case DownloadManager.STATUS_SUCCESSFUL:
                                        Log.d("APPUpdate", "下载完成:" + fileName);
                                        mTimer.cancel();
                                        dismiss();
 
 
//                                        if (notificationManager != null) {
////                                            // 显示下载成功通知
//                                            notificationManager.notify(notificationId, notificationBuilder
//                                                    // 设置通知的文本
//                                                    .setContentText(String.format(Locale.CHINA,"下载中 %d%%",100))
//                                                    // 设置下载的进度
//                                                    .setProgress(100, 100, false)
//                                                    // 设置通知点击之后的意图
//                                                    .setContentIntent(PendingIntent.getActivity(getContext(), 1, getInstallIntent(), PendingIntent.FLAG_IMMUTABLE))
//                                                    // 设置点击通知后是否自动消失
//                                                    .setAutoCancel(true)
//                                                    // 是否正在交互中
//                                                    .setOngoing(false)
//                                                    .build());
//                                        }
                                        mUpdateView.setText("下载完成,点击安装");
                                        // 标记成下载完成
                                        mDownloadComplete = true;
                                        // 标记当前不是下载中
                                        mDownloading = false;
                                        // 如果当前不是强制更新,对话框就恢复成可取消状态
                                        if (!mForceUpdate) {
                                            setCancelable(true);
                                        }
                                        // 安装 Apk
//                                        installApk();
                                        Utils.installAPK(getContext(), fileName);
                                        break;
                                    case DownloadManager.STATUS_FAILED:
                                        Log.d("APPUpdate", "下载失败:" + fileName);
//                                        if (notificationManager != null) {
//                                            // 清除通知
//                                            notificationManager.cancel(notificationId);
//                                        }
                                        mUpdateView.setText("下载失败,点击重试");
                                        // 删除下载的文件
                                        mApkFile.delete();
                                        break;
                                }
                            }
                        }
                    } finally {
                        if (c != null) {
                            c.close();
                        }
                    }
                }
            }, 0, 1000);
        }
 
        /**
         * 安装 Apk
         */
        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(), getContext().getApplicationContext().getPackageName() + ".fileprovider", 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;
        }
    }
}