package com.stm.asset.utils;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.stm.asset.base.StmApplication;

/**
 * 类名称：VersionUtil
 * 类描述：版本工具类
 * 创建人：story
 * 创建时间：2021-12-23 20:15
 */
public class VersionUtil {

    public static boolean needUpdate(String serCode) {
        boolean flag = false;
        try {
            if (!StringUtils.isEmptyOrNull(serCode)) {
                PackageManager packageManager = StmApplication.getInstance().getPackageManager();
                PackageInfo packageInfo = packageManager.getPackageInfo(StmApplication.getInstance().getPackageName(), 0);
                String verName = packageInfo.versionName;
                String[] verArrayService = serCode.split("\\.");
                String[] verArrayBase = verName.split("\\.");
                for(int i=0; i < verArrayService.length; i++) {
                    if (verArrayBase.length > i) {
                        int serInt = Integer.parseInt(verArrayService[i]);
                        int basInt = Integer.parseInt(verArrayBase[i]);
                        if (serInt > basInt) {
                            flag = true;
                            break;
                        } else if (serInt < basInt) {
                            break;
                        }
                    } else {
                        flag = true;
                        break;
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

}
