DownloadDialog.java
3.64 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
package com.polysoft.nafmii.fragment.dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;
import com.polysoft.nafmii.net.OkhttpApi;
import com.polysoft.nafmii.utils.FileUtils;
import com.polysoft.nafmii.utils.ToastUtils;
import com.whaty.nafmii.R;
import java.io.File;
public class DownloadDialog extends DialogFragment {
private String url;
private String fileName;
private TextView mProgressText;
public static void show(FragmentManager manager, String url, String fileName) {
DownloadDialog dialog = new DownloadDialog(url, fileName);
Bundle bundle = new Bundle();
bundle.putString("url", url);
bundle.putString("fileName", fileName);
dialog.setArguments(bundle);
dialog.show(manager, "downloadidalog");
}
private DownloadDialog(String url, String fileName) {
this.url = url;
this.fileName = fileName;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// super.setStyle(DialogFragment.STYLE_NO_TITLE, 0);
super.setCancelable(false);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.download_dialog, container, false);
this.mProgressText = view.findViewById(R.id.download_progress);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
getDialog().getWindow().setDimAmount(0f);
this.start();
}
private void start() {
String name = this.fileName;
File file = new File(FileUtils.getSysDownLoadDir(), name);
OkhttpApi.doDownload(this.url, file, new OkhttpApi.IDownListener() {
@Override
public void onProgress(int progress) {
if (progress == 100) {
String downloadName = FileUtils.getSysDownLoadDir().getName();
String tipsPath = new File(downloadName, name).getAbsolutePath();
mProgressText.post(() -> {
ToastUtils.showLong("下载目录 " + tipsPath);
openFile(file);
dismiss();
});
return;
}
mProgressText.post(() -> mProgressText.setText(String.format("%d%%", progress)));
}
@Override
public void onFail() {
mProgressText.post(() -> {
ToastUtils.showLong("下载失败 ");
dismiss();
});
}
});
}
private void openFile(File file) {
// Intent intent = new Intent(Intent.ACTION_VIEW);
// Uri fileUri = FileProviderUtils.getUriForFile(getContext(), file);
// String extension = MimeTypeMap.getFileExtensionFromUrl(fileUri.toString());
// String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
// intent.setDataAndType(fileUri, mimeType);
// getActivity().startActivity(intent);
}
}