Example #1
0
  // @FIXME: This method is doing so many things at this moment and has become too complex thus
  // needs a complete refactor, However at this moment due to other prior tasks the refactoring
  // task has been kept in backlog.
  private void connectionEvent(Intent intent)
      throws PayloadException, CertificateException, NetworkException, ContactManagerException {
    try {
      if (mDisconnectedByBattery) {
        return;
      }

      if (!intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
        return;
      }

      boolean connectivity =
          intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
      String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
      boolean failover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);
      if (sLogger.isActivated()) {
        sLogger.debug(
            "Connectivity event change: failover="
                + failover
                + ", connectivity="
                + !connectivity
                + ", reason="
                + reason);
      }
      NetworkInfo networkInfo = mCnxManager.getActiveNetworkInfo();
      if (networkInfo == null) {
        if (sLogger.isActivated()) {
          sLogger.debug("Disconnect from IMS: no network (e.g. air plane mode)");
        }
        disconnectFromIms();
        return;
      }
      if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
        String lastUserAccount = LauncherUtils.getLastUserAccount(mCtx);
        String currentUserAccount = LauncherUtils.getCurrentUserAccount(mCtx);
        if (lastUserAccount != null) {
          if ((currentUserAccount == null)
              || !currentUserAccount.equalsIgnoreCase(lastUserAccount)) {
            mImsModule.getCoreListener().onSimChangeDetected();
            return;
          }
        }
      }
      String localIpAddr = null;
      if (networkInfo.getType() != mCurrentNetworkInterface.getType()) {
        if (sLogger.isActivated()) {
          sLogger.info("Data connection state: NETWORK ACCESS CHANGED");
        }
        if (sLogger.isActivated()) {
          sLogger.debug("Disconnect from IMS: network access has changed");
        }
        disconnectFromIms();

        if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
          if (sLogger.isActivated()) {
            sLogger.debug("Change the network interface to mobile");
          }
          mCurrentNetworkInterface = getMobileNetworkInterface();
        } else if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
          if (sLogger.isActivated()) {
            sLogger.debug("Change the network interface to Wi-Fi");
          }
          mCurrentNetworkInterface = getWifiNetworkInterface();
        }

        loadUserProfile();

        try {
          mDnsResolvedFields = mCurrentNetworkInterface.getDnsResolvedFields();
        } catch (UnknownHostException e) {
          /*
           * Even if we are not able to resolve host name , we should still continue to
           * get local IP as this is a very obvious case, Specially for networks
           * supporting IPV4 protocol.
           */
          if (sLogger.isActivated()) {
            sLogger.debug(e.getMessage());
          }
        }
        localIpAddr =
            NetworkFactory.getFactory()
                .getLocalIpAddress(mDnsResolvedFields, networkInfo.getType());
      } else {
        /* Check if the IP address has changed */
        try {
          if (mDnsResolvedFields == null) {
            mDnsResolvedFields = mCurrentNetworkInterface.getDnsResolvedFields();
          }
        } catch (UnknownHostException e) {
          /*
           * Even if we are not able to resolve host name , we should still continue to
           * get local IP as this is a very obvious case, Specially for networks
           * supporting IPV4 protocol.
           */
          if (sLogger.isActivated()) {
            sLogger.debug(e.getMessage());
          }
        }
        localIpAddr =
            NetworkFactory.getFactory()
                .getLocalIpAddress(mDnsResolvedFields, networkInfo.getType());
        String lastIpAddr = mCurrentNetworkInterface.getNetworkAccess().getIpAddress();
        if (!localIpAddr.equals(lastIpAddr)) {
          // Changed by Deutsche Telekom
          if (lastIpAddr != null) {
            if (sLogger.isActivated()) {
              sLogger.debug("Disconnect from IMS: IP address has changed");
            }
            disconnectFromIms();
          } else {
            if (sLogger.isActivated()) {
              sLogger.debug("IP address available (again)");
            }
          }
        } else {
          // Changed by Deutsche Telekom
          if (sLogger.isActivated()) {
            sLogger.debug("Neither interface nor IP address has changed; nothing to do.");
          }
          return;
        }
      }
      if (networkInfo.isConnected()) {
        String remoteAddress;
        if (mDnsResolvedFields != null) {
          remoteAddress = mDnsResolvedFields.mIpAddress;
        } else {
          remoteAddress = new String("unresolved");
        }

        if (sLogger.isActivated()) {
          sLogger.info(
              "Data connection state: CONNECTED to "
                  + networkInfo.getTypeName()
                  + " with local IP "
                  + localIpAddr
                  + " valid for "
                  + remoteAddress);
        }

        if (!NetworkAccessType.ANY.equals(mNetwork)
            && (mNetwork.toInt() != networkInfo.getType())) {
          if (sLogger.isActivated()) {
            sLogger.warn("Network access " + networkInfo.getTypeName() + " is not authorized");
          }
          return;
        }

        TelephonyManager tm = (TelephonyManager) mCtx.getSystemService(Context.TELEPHONY_SERVICE);
        String currentOpe = tm.getSimOperatorName();
        if (mOperator != null && !currentOpe.equalsIgnoreCase(mOperator)) {
          if (sLogger.isActivated()) {
            sLogger.warn(
                "Operator not authorized current=" + currentOpe + " authorized=" + mOperator);
          }
          return;
        }

        if (!mCurrentNetworkInterface.isInterfaceConfigured()) {
          if (sLogger.isActivated()) {
            sLogger.warn("IMS network interface not well configured");
          }
          return;
        }

        if (sLogger.isActivated()) {
          sLogger.debug("Connect to IMS");
        }
        connectToIms(localIpAddr);
      }
    } catch (SocketException e) {
      if (sLogger.isActivated()) {
        sLogger.debug(e.getMessage());
      }
      disconnectFromIms();
    } catch (IOException e) {
      if (sLogger.isActivated()) {
        sLogger.debug(e.getMessage());
      }
      disconnectFromIms();
    }
  }