package com.dowse.base.manager;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.Handler;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import com.dowse.base.data.DS4GAPNInfo;
import com.dowse.base.data.DS4GInfo;
import com.dowse.base.log.DSLog;
import com.dowse.base.param.video.ChannelConst;
import com.dowse.base.util.ContextUtil;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.List;

/* loaded from: ds_base_2.0.9_23030112.aar:classes.jar:com/dowse/base/manager/DS4GManager.class */
public class DS4GManager {
    private static final String TAG = "DS4GManager";
    private static Uri APN_URI = Uri.parse("content://telephony/carriers");
    private static Uri CURRENT_APN_URI = Uri.parse("content://telephony/carriers/preferapn");
    private Context mContext;
    private SignalStrength lastSignalStrength;
    private Handler mHandler;
    private static final int MAX_GET_SINGNAL_TIME = 60000;
    private PhoneStateListener mSignalListener;

    /* JADX INFO: Access modifiers changed from: private */
    public synchronized void updateSignalStrength(SignalStrength signalStrength) {
        this.lastSignalStrength = signalStrength;
    }

    public static DS4GManager getInstance() {
        return DS4GManagerHolder.instance;
    }

    /* loaded from: ds_base_2.0.9_23030112.aar:classes.jar:com/dowse/base/manager/DS4GManager$DS4GManagerHolder.class */
    private static class DS4GManagerHolder {
        public static final DS4GManager instance = new DS4GManager();

        private DS4GManagerHolder() {
        }
    }

    private DS4GManager() {
        this.mContext = null;
        this.mSignalListener = new PhoneStateListener() { // from class: com.dowse.base.manager.DS4GManager.1
            @Override // android.telephony.PhoneStateListener
            public void onSignalStrengthsChanged(SignalStrength signalStrength) {
                super.onSignalStrengthsChanged(signalStrength);
                DS4GManager.this.updateSignalStrength(signalStrength);
            }
        };
        this.mContext = ContextUtil.getApplication();
        this.mHandler = new Handler();
        update4GSignal();
    }

    public boolean hasSimCard() {
        if (this.mContext != null) {
            TelephonyManager telMgr = (TelephonyManager) this.mContext.getSystemService("phone");
            int simState = telMgr.getSimState();
            boolean result = true;
            switch (simState) {
                case ChannelConst.MTK_FACING_BACK /* 0 */:
                case 1:
                    result = false;
                    break;
            }
            return result;
        }
        DSLog.e(TAG, "hasSimCard mContext is null!!!");
        return false;
    }

    public synchronized SignalStrength getLatestSignalStrength() {
        return this.lastSignalStrength;
    }

    public void cleanSignalListener(PhoneStateListener listener) {
        TelephonyManager mTelephonyManager;
        if (this.mContext != null) {
            if (hasSimCard() && (mTelephonyManager = (TelephonyManager) this.mContext.getSystemService("phone")) != null) {
                mTelephonyManager.listen(listener, 0);
                return;
            }
            return;
        }
        DSLog.e(TAG, "cleanSignalListener mContext is null!!!");
    }

    public void getSignal(PhoneStateListener listener) {
        TelephonyManager mTelephonyManager;
        if (this.mContext != null) {
            if (hasSimCard() && (mTelephonyManager = (TelephonyManager) this.mContext.getSystemService("phone")) != null) {
                mTelephonyManager.listen(listener, 256);
                return;
            }
            return;
        }
        DSLog.e(TAG, "getSignal mContext is null!!!");
    }

    public boolean setDataEnabled(boolean enable) {
        boolean result = false;
        if (this.mContext != null) {
            try {
                int subid = SubscriptionManager.from(this.mContext).getActiveSubscriptionInfoForSimSlotIndex(0).getSubscriptionId();
                TelephonyManager telephonyService = (TelephonyManager) this.mContext.getSystemService("phone");
                Method setDataEnabled = telephonyService.getClass().getDeclaredMethod("setDataEnabled", Integer.TYPE, Boolean.TYPE);
                if (null != setDataEnabled) {
                    setDataEnabled.invoke(telephonyService, Integer.valueOf(subid), Boolean.valueOf(enable));
                    result = true;
                }
            } catch (Exception e) {
                e.printStackTrace();
                DSLog.e(TAG, "setDataEnabled exception " + e.getMessage());
            }
        } else {
            DSLog.e(TAG, "setDataEnabled mContext is null!!!");
        }
        return result;
    }

    public boolean getDataEnabled() {
        SubscriptionInfo subscriptionInfo = null;
        if (this.mContext != null) {
            boolean enabled = false;
            try {
                subscriptionInfo = SubscriptionManager.from(this.mContext).getActiveSubscriptionInfoForSimSlotIndex(0);
            } catch (Exception e) {
                e.printStackTrace();
                DSLog.e(TAG, "getDataEnabled Exception msg=" + e.getMessage());
            }
            if (subscriptionInfo == null) {
                return false;
            }
            int subid = subscriptionInfo.getSubscriptionId();
            TelephonyManager telephonyService = (TelephonyManager) this.mContext.getSystemService("phone");
            try
            {
                Method getDataEnabled = telephonyService.getClass().getDeclaredMethod("getDataEnabled", Integer.TYPE);
                if (null != getDataEnabled) {
                    enabled = ((Boolean) getDataEnabled.invoke(telephonyService, Integer.valueOf(subid))).booleanValue();
                }
            }
            catch (NoSuchMethodException ex)
            {
            }
            catch (IllegalAccessException ex)
            {

            }
            catch (InvocationTargetException ex)
            {

            }

            return enabled;
        }
        DSLog.e(TAG, "getDataEnabled mContext is null!!!");
        return false;
    }

    public String getIMEI() {
        String imei = "";
        if (this.mContext != null) {
            TelephonyManager manager = (TelephonyManager) this.mContext.getSystemService("phone");
            try {
                Method method = manager.getClass().getMethod("getImei", Integer.TYPE);
                String imei1 = (String) method.invoke(manager, 0);
                imei = imei1;
            } catch (Exception e) {
                e.printStackTrace();
                DSLog.e(TAG, "getIMEI Exception msg=" + e.getMessage());
            }
        } else {
            DSLog.e(TAG, "getIMEI mContext is null!!!");
        }
        return imei;
    }

    public String getICCID() {
        String iccId = "";
        if (this.mContext != null) {
            SubscriptionManager sm = SubscriptionManager.from(this.mContext);
            List<SubscriptionInfo> sis = sm.getActiveSubscriptionInfoList();
            if (sis != null && sis.size() > 0) {
                SubscriptionInfo si = (SubscriptionInfo)sis.get(0);
                iccId = si.getIccId();
            }
        } else {
            DSLog.e(TAG, "getICCID mContext is null!!!");
        }
        return iccId;
    }

    public String get4GIPAddress() {
        String ipStr = "";
        if (this.mContext != null) {
            if (((ConnectivityManager) this.mContext.getSystemService("connectivity")).getActiveNetworkInfo() == null) {
                return ipStr;
            }
            try {
                Enumeration en = NetworkInterface.getNetworkInterfaces();
                while (en.hasMoreElements()) {
                    NetworkInterface intf = (NetworkInterface)en.nextElement();
                    Enumeration enumIpAddr = intf.getInetAddresses();
                    while (true) {
                        if (enumIpAddr.hasMoreElements()) {
                            InetAddress inetAddress = (InetAddress)enumIpAddr.nextElement();
                            if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress() && intf.getName().contains("ccmni") && inetAddress.getHostAddress().toString().contains(".")) {
                                ipStr = inetAddress.getHostAddress().toString();
                                break;
                            }
                        }
                    }
                }
            } catch (SocketException ex) {
                DSLog.e(TAG, "get4GIPAddress SocketException:" + ex.toString());
            }
        } else {
            DSLog.e(TAG, "get4GIPAddress mContext is null!!!");
        }
        return ipStr;
    }

    public static String getCellularType(TelephonyManager mTelephonyManager) {
        String cellularType;
        int nSubType = mTelephonyManager.getNetworkType();
        if (nSubType == 1 || nSubType == 2 || nSubType == 7 || nSubType == 4 || nSubType == 16 || nSubType == 11) {
            cellularType = "2G";
        } else if (nSubType == 3 || nSubType == 17 || nSubType == 12 || nSubType == 8 || nSubType == 9 || nSubType == 10 || nSubType == 5 || nSubType == 6 || nSubType == 15) {
            cellularType = "3G";
        } else if (nSubType == 13 || nSubType == 18) {
            cellularType = "4G";
        } else if (nSubType == 0) {
            cellularType = "0G";
        } else {
            cellularType = String.valueOf(nSubType);
        }
        return cellularType;
    }

    public String getNetWorkType() {
        String cellulartype = "";
        if (this.mContext != null) {
            TelephonyManager mTelephonyManager = (TelephonyManager) this.mContext.getSystemService("phone");
            cellulartype = getCellularType(mTelephonyManager);
            if (cellulartype.equals("0G")) {
                cellulartype = "";
            }
        } else {
            DSLog.e(TAG, "getNetWorkType mContext is null!!!");
        }
        return cellulartype;
    }

    public String getSimOperatorName() {
        String simOperator = "";
        if (this.mContext != null) {
            TelephonyManager tm = (TelephonyManager) this.mContext.getSystemService("phone");
            simOperator = tm.getSimOperatorName();
        } else {
            DSLog.e(TAG, "getSimOperatorName mContext is null!!!");
        }
        return simOperator;
    }

    private void delApn(String apn) {
        if (this.mContext != null) {
            int rows = this.mContext.getContentResolver().delete(APN_URI, "apn='" + apn + "'", null);
            DSLog.d(TAG, "delApn apn=" + apn + ",rowse=" + rows);
            return;
        }
        DSLog.e(TAG, "delApn mContext is null!!!");
    }

    private int getAPNId(String apn) {
        int id = -1;
        if (this.mContext != null) {
            Cursor cr = this.mContext.getContentResolver().query(APN_URI, null, null, null, null);
            while (true) {
                if (cr == null || !cr.moveToNext()) {
                    break;
                } else if (cr.getString(cr.getColumnIndex("apn")).equals(apn)) {
                    int idIndex = cr.getColumnIndex("_id");
                    id = cr.getShort(idIndex);
                    break;
                }
            }
            cr.close();
        } else {
            DSLog.e(TAG, "getAPNId mContext is null!!!");
        }
        return id;
    }

    private void setAPN(int id) {
        if (this.mContext != null) {
            ContentResolver resolver = this.mContext.getContentResolver();
            ContentValues values = new ContentValues();
            values.put("apn_id", Integer.valueOf(id));
            resolver.update(CURRENT_APN_URI, values, null, null);
            return;
        }
        DSLog.e(TAG, "setAPN mContext is null!!!");
    }

    private String getSIMInfo() {
        if (this.mContext != null) {
            TelephonyManager iPhoneManager = (TelephonyManager) this.mContext.getSystemService("phone");
            return iPhoneManager.getSimOperator();
        }
        DSLog.e(TAG, "getSIMInfo mContext is null!!!");
        return "";
    }

    private int addAPN(String apnName, String apn, String user, String password, String mcc, String mnc, Integer authtype) {
        int id = -1;
        if (this.mContext != null) {
            String NUMERIC = getSIMInfo();
            if (NUMERIC == null) {
                return -1;
            }
            ContentResolver resolver = this.mContext.getContentResolver();
            ContentValues values = new ContentValues();
            values.put("name", apnName);
            values.put("apn", apn);
            values.put("type", "default");
            values.put("numeric", NUMERIC);
            values.put("mcc", mcc);
            values.put("mnc", mnc);
            values.put("proxy", "");
            values.put("port", "");
            values.put("mmsproxy", "");
            values.put("mmsport", "");
            values.put("user", user);
            values.put("server", "");
            values.put("password", password);
            values.put("mmsc", "");
            values.put("authtype", authtype);
            Cursor c = null;
            Uri newRow = resolver.insert(APN_URI, values);
            if (newRow != null) {
                c = resolver.query(newRow, null, null, null, null);
                int idIndex = c.getColumnIndex("_id");
                c.moveToFirst();
                id = c.getShort(idIndex);
            }
            if (c != null) {
                c.close();
            }
        } else {
            DSLog.e(TAG, "addAPN mContext is null!!!");
        }
        return id;
    }

    public DS4GAPNInfo getAPNInfo() {
        DS4GAPNInfo apnInfo = new DS4GAPNInfo();
        if (this.mContext != null) {
            ContentResolver cResolver = this.mContext.getContentResolver();
            Cursor cr = cResolver.query(CURRENT_APN_URI, null, null, null, null);
            if (cr != null && cr.getCount() > 0) {
                cr.moveToFirst();
                apnInfo.setApn(cr.getString(cr.getColumnIndex("apn")));
                apnInfo.setPassword(cr.getString(cr.getColumnIndex("password")));
                apnInfo.setUser(cr.getString(cr.getColumnIndex("user")));
                apnInfo.setMcc(cr.getString(cr.getColumnIndex("mcc")));
                apnInfo.setMnc(cr.getString(cr.getColumnIndex("mnc")));
                apnInfo.setAuthtype(cr.getInt(cr.getColumnIndex("authtype")));
                cr.close();
            }
        } else {
            DSLog.e(TAG, "getAPNInfo mContext is null!!!");
        }
        return apnInfo;
    }

    public void setAPNInfo(DS4GAPNInfo apnInfo) {
        delApn(apnInfo.getApn());
        int apnId = getAPNId(apnInfo.getApn());
        if (apnId < 0) {
            apnId = addAPN(apnInfo.getApn(), apnInfo.getApn(), apnInfo.getUser(), apnInfo.getPassword(), apnInfo.getMcc(), apnInfo.getMnc(), Integer.valueOf(apnInfo.getAuthtype()));
        }
        setAPN(apnId);
    }

    private void update4GSignal() {
        getSignal(this.mSignalListener);
        this.mHandler.postDelayed(new Runnable() { // from class: com.dowse.base.manager.DS4GManager.2
            @Override // java.lang.Runnable
            public void run() {
                DS4GManager.this.cleanSignalListener(DS4GManager.this.mSignalListener);
            }
        }, 60000L);
    }

    public DS4GInfo getDS4GInfo() {
        update4GSignal();
        DS4GInfo ds4GInfo = new DS4GInfo();
        DS4GAPNInfo ds4GAPNInfo = getAPNInfo();
        ds4GInfo.setApn(ds4GAPNInfo.getApn());
        ds4GInfo.setAuthtype(ds4GAPNInfo.getAuthtype());
        ds4GInfo.setMcc(ds4GAPNInfo.getMcc());
        ds4GInfo.setPassword(ds4GAPNInfo.getPassword());
        ds4GInfo.setUser(ds4GAPNInfo.getUser());
        ds4GInfo.setMnc(ds4GAPNInfo.getMnc());
        ds4GInfo.setIccid(getICCID());
        ds4GInfo.setImei(getIMEI());
        ds4GInfo.setIp(get4GIPAddress());
        ds4GInfo.setNetEnable(getDataEnabled() ? 1 : 0);
        ds4GInfo.setOperateName(getSimOperatorName());
        ds4GInfo.setNetType(getNetWorkType());
        ds4GInfo.setLevel(0);
        ds4GInfo.setdBm(0);
        SignalStrength strength = getLatestSignalStrength();
        if (strength != null) {
            int asu = strength.getGsmSignalStrength();
            int lastSignal = (-113) + (2 * asu);
            ds4GInfo.setLevel(strength.getLevel());
            ds4GInfo.setdBm(lastSignal);
        }
        return ds4GInfo;
    }
}
