UpdateDialog.java 7.11 KB
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));
    }
}