DownloadDialog.java 3.77 KB
package com.polysoft.nafmii.fragment.dialog;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.MimeTypeMap;
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.FileProviderUtils;
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);
    }

}