반응형

클래스 만들기

import android.content.Context;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;

import java.lang.reflect.Method;
import java.util.List;

public final class TelephonyInfo {

    private static final String TAG = "TelephonyInfo";
    private static TelephonyInfo telephonyInfo;
    private String imeiSIM1;
    private String imeiSIM2;
    private boolean isSIM1Ready;
    private boolean isSIM2Ready;

    public String getImsiSIM1() {
        return imeiSIM1;
    }

    public String getImsiSIM2() {
        return imeiSIM2;
    }

    public boolean isSIM1Ready() {
        return isSIM1Ready;
    }

    public boolean isSIM2Ready() {
        return isSIM2Ready;
    }

    public boolean isDualSIM() {
        return imeiSIM2 != null;
    }

    private TelephonyInfo() { }

    /*
    * 생성.
    * */
    public static TelephonyInfo getInstance(Context context){

        if(telephonyInfo == null) {

            telephonyInfo = new TelephonyInfo();

            TelephonyManager telephonyManager = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE));

            telephonyInfo.imeiSIM1 = telephonyManager.getDeviceId();;
            telephonyInfo.imeiSIM2 = null;

            /*
              get Device id
            */
            String[] methods = {"getDeviceIdGemini","getDeviceId","getDeviceIdDs","getSimSerialNumberGemini"};
            boolean success = false;
            for(int i=0; i < methods.length; i++) {
                try {
                    telephonyInfo.imeiSIM1 = getDeviceIdBySlot(context, methods[i], 0);
                    telephonyInfo.imeiSIM2 = getDeviceIdBySlot(context, methods[i], 1);
                    success = true;
                } catch(GeminiMethodNotFoundException e) {
                  
                }
                if (success) break;
            }
            

            /*
               sim state
            */
            telephonyInfo.isSIM1Ready = telephonyManager.getSimState() == TelephonyManager.SIM_STATE_READY;
            telephonyInfo.isSIM2Ready = false;

            String[] simMethods = {"getSimStateGemini","getSimState"};
            boolean success2 = false;
            for(int i=0; i < simMethods.length; i++) {
                try {
                    telephonyInfo.isSIM1Ready = getSIMStateBySlot(context,  simMethods[i], 0);
                    telephonyInfo.isSIM2Ready = getSIMStateBySlot(context,  simMethods[i], 1);
                    success2 = true;
                } catch(GeminiMethodNotFoundException e) {
                    
                }
                if (success2) break;
            }

            printTelephonyManagerMethodNamesForThisDevice(context);
        }

        return telephonyInfo;
    }

    /*
    * 슬롯의 device id를 가저온다.
    * */
    private static String getDeviceIdBySlot(Context context, String predictedMethodName, int slotID) throws GeminiMethodNotFoundException {

        String imei = null;

        TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        try{

            Class<?> telephonyClass = Class.forName(telephony.getClass().getName());

            Class<?>[] parameter = new Class[1];
            parameter[0] = int.class;
            Method getSimID = telephonyClass.getMethod(predictedMethodName, parameter);

            Object[] obParameter = new Object[1];
            obParameter[0] = slotID;
            Object ob_phone = getSimID.invoke(telephony, obParameter);

            if(ob_phone != null){
                imei = ob_phone.toString();

            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new GeminiMethodNotFoundException(predictedMethodName);
        }

        return imei;
    }

    /*
    * slot에 sim상태를 가저온다.
    * */
    private static  boolean getSIMStateBySlot(Context context, String predictedMethodName, int slotID) throws GeminiMethodNotFoundException {

        boolean isReady = false;

        TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        try{

            Class<?> telephonyClass = Class.forName(telephony.getClass().getName());

            Class<?>[] parameter = new Class[1];
            parameter[0] = int.class;
            Method getSimStateGemini = telephonyClass.getMethod(predictedMethodName, parameter);

            Object[] obParameter = new Object[1];
            obParameter[0] = slotID;
            Object ob_phone = getSimStateGemini.invoke(telephony, obParameter);

            if(ob_phone != null){
                int simState = Integer.parseInt(ob_phone.toString());
                if(simState == TelephonyManager.SIM_STATE_READY){
                    isReady = true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new GeminiMethodNotFoundException(predictedMethodName);
        }

        return isReady;
    }


    private static class GeminiMethodNotFoundException extends Exception {

        private static final long serialVersionUID = -996812356902545308L;

        public GeminiMethodNotFoundException(String info) {
            super(info);
        }
    }
    /*
    * 리플랙트로 가저온 모든 method명을 프린트한다.
    * */
    public static void printTelephonyManagerMethodNamesForThisDevice(Context context) {

        TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        Class<?> telephonyClass;
        try {
            telephonyClass = Class.forName(telephony.getClass().getName());
            Method[] methods = telephonyClass.getMethods();
            for (int idx = 0; idx < methods.length; idx++) {
                ELog.i("## " + methods[idx] + " declared by " + methods[idx].getDeclaringClass());
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    /*
     * dual sim 번호를 가저온다.
     * */

    public static String getDualSimNumber(Context context){
        String number = null;
        TelephonyInfo ti =TelephonyInfo.getInstance(context);

        boolean isSIM1Ready = ti.isSIM1Ready();//1번 장착
        boolean isSIM2Ready = ti.isSIM2Ready();//2번 장착
        boolean isDualsim = ti.isDualSIM();// 듀얼심 기종인지 체크
        
        if (isDualsim){
            if (isSIM1Ready&&isSIM2Ready){
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
                    SubscriptionManager subscriptionManager = SubscriptionManager.from(context);
                    try {
                        List<SubscriptionInfo> subsInfoList = subscriptionManager.getActiveSubscriptionInfoList();
                        for (SubscriptionInfo subscriptionInfo : subsInfoList) {
                            number = subscriptionInfo.getNumber();
                        }
                    }
                    catch (SecurityException se){
                        return number;
                    }

                }
            }
        }

        return number;
    }

    /*
    * 번호가 더블
    * */
    public static List<String> getDualSimNumberList(Context context){
        List<String> lst = null;
        TelephonyInfo ti =TelephonyInfo.getInstance(context);

        boolean isSIM1Ready = ti.isSIM1Ready();//1번 장착
        boolean isSIM2Ready = ti.isSIM2Ready();//2번 장착
        boolean isDualsim = ti.isDualSIM();// 듀얼심 기종인지 체크


        if (isDualsim){
            if (isSIM1Ready&&isSIM2Ready){
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
                    SubscriptionManager subscriptionManager = SubscriptionManager.from(context);
                    try {
                        List<SubscriptionInfo> subsInfoList = subscriptionManager.getActiveSubscriptionInfoList();
                        for (SubscriptionInfo subscriptionInfo : subsInfoList) {
                            lst.add(subscriptionInfo.getNumber());
                        }
                    }
                    catch (SecurityException se){
                        return lst;
                    }

                }
            }
        }

        return lst;
    }
}

사용

if (TelephonyInfo.getDualSimNumber(MainActivity.this) !=null){
	String phoneNumber = TelephonyInfo.getDualSimNumber(MainActivity.this);
}
반응형

+ Recent posts