版本升级

/**
 * 主要代码
 */
public class VersionUpdate implements VersionUpdateContract.View {
    private final PermissionContract.Presenter mPermissionPresenter;
    private VersionUpdateContract.Presenter mPresenter;
    private MainActivity mContext;
    private ProgressDialog mDialog;
    private UpgradeInfoBean info;
    private File apkFile;

    public VersionUpdate(Activity context, PermissionContract.Presenter permissionPresenter) {
        this.mContext = (MainActivity) context;
        mPermissionPresenter = permissionPresenter;
    }

    public void update() {
        mPresenter = new VersionPresenter(mContext, this);
        mPresenter.loadData();
    }

    @Override
    public void setPresenter(VersionUpdateContract.Presenter presenter) {
        mPresenter = presenter;
    }
    //普通升级弹窗
    @Override
    public void showDialog() {
        if (info.getContent() != null && info.getContent().getAppUrl() != null) {
            AlertDialog dialog = new AlertDialog.Builder(mContext)
                    .setMessage(info.getContent().getVersionDes())
                    .setPositiveButton("马上更新", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            downloadAPK();
                            dialog.dismiss();
                        }
                    }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    }).create();
            dialog.show();
            dialog.setCanceledOnTouchOutside(false);
        }
    }
    //强制升级弹窗
    @Override
    public void showForcedDialog() {
        if (info.getContent() != null && info.getContent().getAppUrl() != null) {
            AlertDialog dialog = new AlertDialog.Builder(mContext)
                    .setMessage(info.getContent().getVersionDes())
                    .setPositiveButton("马上更新", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            downloadAPK();
                            dialog.dismiss();
                        }
                    }).create();
            dialog.show();
            dialog.setCancelable(false);
        }
    }

    @Override
    public void setData(UpgradeInfoBean upgradeInfoBean) {
        info = upgradeInfoBean;
        showUpdateDialog();
    }
    
    public void showUpdateDialog() {
        if(info.getContent() != null) {
            if(info.getContent().getVersionType() == 1) {//普通升级
                showDialog();
            } else if(info.getContent().getVersionType() == 2) {//强制升级
                showForcedDialog();
            }
        }
    }

    public void downloadAPK() {
        boolean isGetSDPermission = false;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
            isGetSDPermission = mPermissionPresenter.isGetSDPermission(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, PermissionPresenter.SDPERMISSION);
        }
        if (isGetSDPermission) {
            if (!NetworkUtils.isNetworkAvailable(mContext)) {
                ToastUtils.custom(mContext.getString(R.string.network_access_err));
                return;
            } else {
                mDialog = new ProgressDialog(mContext);
                mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                mDialog.setCancelable(false);
                mDialog.setTitle("版本更新");
                mDialog.setMax(100);
                mDialog.setProgressPercentFormat(null);
                mDialog.show();
                try {
                    if(info.getContent() != null && info.getContent().getAppUrl() != null){
                        download(info.getContent().getAppUrl());
                    }
                } catch (Exception e) {
                    LogUtils.wtf(e);
                }
            }
        }
    }

    public void download(String serverPath) throws Exception {
        OkHttpUtils.get().url(serverPath).build().execute(new FileCallBack(mContext.getExternalCacheDir().getAbsolutePath(), "/Qian.apk") {
            @Override
            public void onError(Call call, Exception e, int i) {
                ToastUtils.custom("下载失败!");
                dismissDialog();
                showUpdateDialog();
                LogUtils.wtf(e);
            }

            @Override
            public void onResponse(File file, int i) {
                if (file.exists() && file.length() > 0) {
                    ToastUtils.custom("下载完成!");
                    dismissDialog();
                    apkFile = file;
                    checkInstallApk();
                } else {
                    ToastUtils.custom("下载失败!");
                    if (file.exists()) {
                        file.delete();
                    }
                    dismissDialog();
                    showUpdateDialog();
                }
            }

            @Override
            public void inProgress(float progress, long total, int id) {
                super.inProgress(progress, total, id);
                mDialog.setProgress((int) (100 * progress));
            }
        });
    }

    private void dismissDialog() {
        if (mDialog != null || mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }

    public void checkInstallApk() {
        if (Build.VERSION.SDK_INT >= 26) {
            //来判断应用是否有权限安装apk
            boolean installAllowed= mContext.getPackageManager().canRequestPackageInstalls();
            //有权限
            if (installAllowed) {
                //安装apk
                installApk();
            } else {
                //无权限 申请权限
                ActivityCompat.requestPermissions(mContext, new String[]{Manifest.permission.REQUEST_INSTALL_PACKAGES}, mContext.INSTALL_APK_REQUESTCODE);
            }
        } else {
            installApk();
        }
    }

    public void installApk() {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
        } else {
            Uri uriForFile = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID + ".provider", apkFile);
            intent.setDataAndType(uriForFile, "application/vnd.android.package-archive");
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }
        mContext.startActivity(intent);
        android.os.Process.killProcess(android.os.Process.myPid());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值