package com.quyunshuo.androidbaseframemvvm.base.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.telephony.TelephonyManager;

import com.quyunshuo.androidbaseframemvvm.base.BaseApplication;


/**
 * Created by MinKin
 * 2018/11/10
 */
public class SystemUtil {

    public static int getVersionCode() {
        int versionCode = 0;
        PackageManager pm = BaseApplication.context.getPackageManager();
        try {
            PackageInfo pi = pm.getPackageInfo( BaseApplication.context.getPackageName(), 0);
            versionCode = pi.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionCode;
    }

    public static String getVersionName() {
        String versionName = "";
        PackageManager pm =  BaseApplication.context.getPackageManager();
        try {
            PackageInfo pi = pm.getPackageInfo( BaseApplication.context.getPackageName(), 0);
            versionName = pi.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }

    /**
     * 获取设备号
     */
    @SuppressLint("MissingPermission")
    public static String getDeviceId() {
        TelephonyManager tm = (TelephonyManager) BaseApplication.context.getSystemService(Context.TELEPHONY_SERVICE);
        String deviceId = null;
        if (tm != null) {
            deviceId = tm.getDeviceId();
        }
        return deviceId;
    }

    /**
     * 获取SimSerialNumber(ICCID)
     */
    @SuppressLint("MissingPermission")
    public static String getSimSerialNumber(){
        TelephonyManager tm = (TelephonyManager) BaseApplication.context.getSystemService(Context.TELEPHONY_SERVICE);
        String simSerialNumber = "";
        if (tm != null) {
            try {
                simSerialNumber = tm.getSimSerialNumber();
                if (simSerialNumber==null){
                    return "";
                }
            }catch (Exception e){
                return "";
            }

        }

        return simSerialNumber;
    }


}
