UpdateDialog.java
7.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
package com.polysoft.nafmii.fragment.dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;
import com.alibaba.fastjson.JSON;
import com.polysoft.nafmii.bean.UpdateVersion;
import com.polysoft.nafmii.net.OkhttpApi;
import com.polysoft.nafmii.utils.CtxUtils;
import com.polysoft.nafmii.utils.FileUtils;
import com.polysoft.nafmii.utils.TimerUtils;
import com.polysoft.nafmii.utils.ToastUtils;
import com.whaty.nafmii.BuildConfig;
import com.whaty.nafmii.R;
import java.io.File;
public class UpdateDialog extends DialogFragment implements View.OnClickListener {
public static UpdateDialog show(FragmentManager mgr, UpdateVersion info) {
// if (null == info) {
// info = new UpdateVersion();
// info.setIsForce("0");
// info.setvNumber("2.1.1");
// info.setLocation("https://cos.pgyer.com/319735d279de31ab4aea67d07c0706e3.apk?sign=34fa92d8e78cc084322dc868a10299ab&t=1634904770&response-content-disposition=attachment%3Bfilename%3DNAFMII%E4%B9%8B%E7%AA%97-qp_2.1.apk");
// }
UpdateDialog dialog = new UpdateDialog();
Bundle bundle = new Bundle();
bundle.putString("data", JSON.toJSONString(info));
dialog.setArguments(bundle);
dialog.show(mgr, "UpdateDialog");
return dialog;
}
private UpdateVersion versionInfo;
private View tipsLayout, progressLayout;
private ProgressBar progressBar;
private TextView progressText, tipsContent, tipsOk;
private boolean isInstall = false;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Material_Light_Dialog);
super.setCancelable(false);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Bundle bundle = getArguments();
if (null != bundle) {
String json = bundle.getString("data");
if (TextUtils.isEmpty(json)) {
this.versionInfo = new UpdateVersion();
} else {
this.versionInfo = JSON.parseObject(json, UpdateVersion.class);
}
}
View view = inflater.inflate(R.layout.update_dialog, container, false);
this.initView(view);
return view;
}
void initView(View view) {
this.tipsLayout = view.findViewById(R.id.update_layout_tips);
this.tipsLayout.findViewById(R.id.update_tips_cancel).setOnClickListener(this);
this.tipsLayout.findViewById(R.id.update_tips_ok).setOnClickListener(this);
this.tipsContent = this.tipsLayout.findViewById(R.id.update_tips_content);
this.tipsOk = this.tipsLayout.findViewById(R.id.update_tips_ok);
this.progressLayout = view.findViewById(R.id.update_layout_progress);
this.progressBar = this.progressLayout.findViewById(R.id.update_progress_bar);
this.progressText = this.progressLayout.findViewById(R.id.update_progress_text);
if (this.isMustUpdate()) {
this.tipsLayout.findViewById(R.id.update_tips_cancel).setVisibility(View.GONE);
}
}
boolean isMustUpdate() {
return "0".equals(this.versionInfo.getIsForce());
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.update_tips_cancel:
dismiss();
break;
case R.id.update_tips_ok:
this.start();
break;
}
}
void showFailTips() {
this.progressLayout.setVisibility(View.GONE);
this.tipsLayout.setVisibility(View.VISIBLE);
this.tipsContent.setText("版本更新失败,请重新更新!");
}
void showInstallTips() {
this.progressLayout.setVisibility(View.GONE);
this.tipsLayout.setVisibility(View.VISIBLE);
this.tipsContent.setText("下载完成立即安装?");
}
boolean checkPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//是否有安装位置来源的权限
if (!getContext().getPackageManager().canRequestPackageInstalls()) {
ToastUtils.showLong("安装应用需要打开安装未知来源应用权限,请去设置中开启权限");
Uri packageUri = Uri.parse("package:" + CtxUtils.getPackageName());
Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageUri);
;
TimerUtils.setTimeout(() -> {
startActivity(intent);
}, 2000);
return false;
}
}
return true;
}
void start() {
if (!this.checkPermission()) {
return;
}
this.tipsLayout.setVisibility(View.GONE);
this.progressLayout.setVisibility(View.VISIBLE);
this.download();
}
@Override
public void onResume() {
super.onResume();
if (this.isInstall) {
if (!this.isMustUpdate()) {
dismiss();
}
ToastUtils.showLong("安装失败!");
}
}
void download() {
this.updateProgress(0);
String url = this.versionInfo.getLocation();
if (TextUtils.isEmpty(url)) {
showFailTips();
return;
}
if (!url.startsWith("http")) {
url = BuildConfig.IMG_API + url;
}
String version = this.versionInfo.getvNumber();
String name = String.format("nafmii-%s.apk", version);
File file = new File(FileUtils.getSysDownLoadDir(), name);
if (file.exists() && BuildConfig.DEBUG) {
updateProgress(100);
CtxUtils.installApk(getActivity(), file.getAbsolutePath());
isInstall = true;
return;
}
OkhttpApi.doDownload(url, file, new OkhttpApi.IDownListener() {
@Override
public void onProgress(int progress) {
if (progress == 100) {
progressText.post(() -> {
isInstall = true;
updateProgress(progress);
CtxUtils.installApk(getActivity(), file.getAbsolutePath());
});
return;
}
progressText.post(() -> updateProgress(progress));
}
@Override
public void onFail() {
progressText.post(() -> showFailTips());
}
});
}
private void updateProgress(int progress) {
progressBar.setProgress(progress);
progressText.setText(String.format("%d%%", progress));
}
}