package com.secspace.lib.common.utils;

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

import java.lang.reflect.Method;

public class ReflectTelephonyUtil {

    /**
     * 打开默认卡的数据流量
     *
     * @param context
     * @param enable
     */
    public static void setDataEnabled(Context context, boolean enable) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
            try {
                TelephonyManager telephonyService = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                Method setDataEnabled = telephonyService.getClass().getDeclaredMethod("setDataEnabled", boolean.class);
                if (null != setDataEnabled) {
                    setDataEnabled.invoke(telephonyService, enable);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 默认卡数据流量是否打开
     *
     * @param context
     * @return
     */
    public static boolean getDataEnabled(Context context) {

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
            try {
                TelephonyManager telephonyService = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                Method getDataEnabled = telephonyService.getClass().getDeclaredMethod("getDataEnabled", new Class[]{});
                if (null != getDataEnabled) {
                    return (Boolean) getDataEnabled.invoke(telephonyService, new Object[]{});
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public void setDataEnabled(Context context, int subId, boolean enable) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
            try {
                TelephonyManager telephonyService = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                Method setDataEnabled = telephonyService.getClass().getDeclaredMethod("setDataEnabled", int.class, boolean.class);
                if (null != setDataEnabled) {
                    setDataEnabled.invoke(telephonyService, subId, enable);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public boolean getDataEnabled(Context context, int subId) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
            try {
                TelephonyManager telephonyService = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                Method getDataEnabled = telephonyService.getClass().getDeclaredMethod("getDataEnabled", int.class);
                if (null != getDataEnabled) {
                    return (Boolean) getDataEnabled.invoke(telephonyService, subId);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    public void setPreferredNetworkType(Context context, int subId, int networkType) {
        try {
            TelephonyManager telephonyService = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            Method setDataEnabled = telephonyService.getClass().getDeclaredMethod("setPreferredNetworkType", int.class, int.class);
            if (null != setDataEnabled) {
                setDataEnabled.invoke(telephonyService, subId, networkType);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
