private int validateIpConfigFields(LinkProperties linkProperties) {
    String ipAddr = mIpAddressView.getText().toString();
    InetAddress inetAddr = null;
    try {
      inetAddr = NetworkUtils.numericToInetAddress(ipAddr);
    } catch (IllegalArgumentException e) {
      return R.string.wifi_ip_settings_invalid_ip_address;
    }

    int networkPrefixLength = -1;
    try {
      networkPrefixLength = Integer.parseInt(mNetworkPrefixLengthView.getText().toString());
    } catch (NumberFormatException e) {
      // Use -1
    }
    if (networkPrefixLength < 0 || networkPrefixLength > 32) {
      return R.string.wifi_ip_settings_invalid_network_prefix_length;
    }
    linkProperties.addLinkAddress(new LinkAddress(inetAddr, networkPrefixLength));

    String gateway = mGatewayView.getText().toString();
    InetAddress gatewayAddr = null;
    try {
      gatewayAddr = NetworkUtils.numericToInetAddress(gateway);
    } catch (IllegalArgumentException e) {
      return R.string.wifi_ip_settings_invalid_gateway;
    }
    linkProperties.addRoute(new RouteInfo(gatewayAddr));

    String dns = mDns1View.getText().toString();
    InetAddress dnsAddr = null;
    try {
      dnsAddr = NetworkUtils.numericToInetAddress(dns);
    } catch (IllegalArgumentException e) {
      return R.string.wifi_ip_settings_invalid_dns;
    }
    linkProperties.addDns(dnsAddr);
    if (mDns2View.length() > 0) {
      dns = mDns2View.getText().toString();
      try {
        dnsAddr = NetworkUtils.numericToInetAddress(dns);
      } catch (IllegalArgumentException e) {
        return R.string.wifi_ip_settings_invalid_dns;
      }
      linkProperties.addDns(dnsAddr);
    }
    return 0;
  }
 private boolean readLinkProperty(String iface) {
   String DhcpPrefix = "dhcp." + iface + ".";
   String ip = SystemProperties.get(DhcpPrefix + "ipaddress");
   String dns1 = SystemProperties.get(DhcpPrefix + "dns1");
   String dns2 = SystemProperties.get(DhcpPrefix + "dns2");
   String gateway = SystemProperties.get(DhcpPrefix + "gateway");
   String mask = SystemProperties.get(DhcpPrefix + "mask");
   if (ip.isEmpty() || gateway.isEmpty()) {
     Log.e(TAG, "readLinkProperty, ip: " + ip + ", gateway: " + gateway + ", can not be empty");
     return false;
   }
   int PrefixLen = countPrefixLength(NetworkUtils.numericToInetAddress(mask).getAddress());
   mLinkProperties.addLinkAddress(
       new LinkAddress(NetworkUtils.numericToInetAddress(ip), PrefixLen));
   RouteInfo ri = new RouteInfo(NetworkUtils.numericToInetAddress(gateway));
   mLinkProperties.addRoute(ri);
   if (!dns1.isEmpty()) mLinkProperties.addDns(NetworkUtils.numericToInetAddress(dns1));
   if (!dns2.isEmpty()) mLinkProperties.addDns(NetworkUtils.numericToInetAddress(dns2));
   mLinkProperties.setInterfaceName(iface);
   return true;
 }
  public synchronized void stopReverseTether(String iface) {
    NetworkUtils.stopDhcp(iface);

    mLinkProperties.clear();
    mNetworkInfo.setIsAvailable(false);
    mNetworkInfo.setDetailedState(DetailedState.DISCONNECTED, null, null);

    Message msg = mCsHandler.obtainMessage(EVENT_CONFIGURATION_CHANGED, mNetworkInfo);
    msg.sendToTarget();

    msg = mCsHandler.obtainMessage(EVENT_STATE_CHANGED, mNetworkInfo);
    msg.sendToTarget();
  }
  private void updateTile() {
    if (isEnabled()) {
      WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
      WifiInfo wifiInfo = wifiManager.getConnectionInfo();

      if (wifiInfo != null) {
        // if wifiInfo is not null, set the label to "hostAddress"
        InetAddress address = NetworkUtils.intToInetAddress(wifiInfo.getIpAddress());
        mLabel = address.getHostAddress();
      } else {
        // if wifiInfo is null, set the enabled label without host address
        mLabel = mContext.getString(R.string.quick_settings_network_adb_enabled_label);
      }
      mDrawable = R.drawable.ic_qs_network_adb_on;
    } else {
      // Otherwise set the disabled label and icon
      mLabel = mContext.getString(R.string.quick_settings_network_adb_disabled_label);
      mDrawable = R.drawable.ic_qs_network_adb_off;
    }
  }
  void stopReverseTether() {
    synchronized (mLinkPropertiesLock) {
      if (TextUtils.isEmpty(mLinkProperties.getInterfaceName())) {
        Log.e(TAG, "attempted to stop reverse tether with nothing tethered");
        return;
      }
      NetworkUtils.stopDhcp(mLinkProperties.getInterfaceName());
      mLinkProperties.clear();
      synchronized (mNetworkInfoLock) {
        mNetworkInfo.setIsAvailable(false);
        mNetworkInfo.setDetailedState(DetailedState.DISCONNECTED, null, null);

        if (mCsHandler != null) {
          mCsHandler
              .obtainMessage(EVENT_STATE_CHANGED, new NetworkInfo(mNetworkInfo))
              .sendToTarget();
        }
      }
    }
  }
  /**
   * Constructor that uses the default settings of the MMS Client.
   *
   * @param context The context of the MMS Client
   */
  public TransactionSettings(Context context, String apnName) {
    if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
      Log.v(TAG, "TransactionSettings: apnName: " + apnName);
    }
    String selection = Telephony.Carriers.CURRENT + " IS NOT NULL";
    String[] selectionArgs = null;
    if (!TextUtils.isEmpty(apnName)) {
      selection += " AND " + Telephony.Carriers.APN + "=?";
      selectionArgs = new String[] {apnName.trim()};
    }

    Cursor cursor =
        SqliteWrapper.query(
            context,
            context.getContentResolver(),
            Telephony.Carriers.CONTENT_URI,
            APN_PROJECTION,
            selection,
            selectionArgs,
            null);

    if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
      Log.v(
          TAG,
          "TransactionSettings looking for apn: "
              + selection
              + " returned: "
              + (cursor == null ? "null cursor" : (cursor.getCount() + " hits")));
    }

    if (cursor == null) {
      Log.e(TAG, "Apn is not found in Database!");
      return;
    }

    boolean sawValidApn = false;
    try {
      while (cursor.moveToNext() && TextUtils.isEmpty(mServiceCenter)) {
        // Read values from APN settings
        if (isValidApnType(cursor.getString(COLUMN_TYPE), PhoneConstants.APN_TYPE_MMS)) {
          sawValidApn = true;

          String mmsc = cursor.getString(COLUMN_MMSC);
          if (mmsc == null) {
            continue;
          }

          mServiceCenter = NetworkUtils.trimV4AddrZeros(mmsc.trim());
          mProxyAddress = NetworkUtils.trimV4AddrZeros(cursor.getString(COLUMN_MMSPROXY));
          if (isProxySet()) {
            String portString = cursor.getString(COLUMN_MMSPORT);
            try {
              mProxyPort = Integer.parseInt(portString);
            } catch (NumberFormatException e) {
              if (TextUtils.isEmpty(portString)) {
                Log.w(TAG, "mms port not set!");
              } else {
                Log.e(TAG, "Bad port number format: " + portString, e);
              }
            }
          }
        }
      }
    } finally {
      cursor.close();
    }

    Log.v(TAG, "APN setting: MMSC: " + mServiceCenter + " looked for: " + selection);

    if (sawValidApn && TextUtils.isEmpty(mServiceCenter)) {
      Log.e(TAG, "Invalid APN setting: MMSC is empty");
    }
  }
 /**
  * Load APN settings from system
  *
  * @param context
  * @param apnName the optional APN name to match
  */
 public static ApnSettings load(Context context, String apnName, int subId) throws ApnException {
   if (Log.isLoggable(TAG, Log.VERBOSE)) {
     Log.v(TAG, "ApnSettings: apnName " + apnName);
   }
   // TODO: CURRENT semantics is currently broken in telephony. Revive this when it is fixed.
   // String selection = Telephony.Carriers.CURRENT + " IS NOT NULL";
   String selection = null;
   String[] selectionArgs = null;
   apnName = apnName != null ? apnName.trim() : null;
   if (!TextUtils.isEmpty(apnName)) {
     // selection += " AND " + Telephony.Carriers.APN + "=?";
     selection = Telephony.Carriers.APN + "=?";
     selectionArgs = new String[] {apnName};
   }
   Cursor cursor = null;
   try {
     cursor =
         SqliteWrapper.query(
             context,
             context.getContentResolver(),
             Uri.withAppendedPath(Telephony.Carriers.CONTENT_URI, "/subId/" + subId),
             APN_PROJECTION,
             selection,
             selectionArgs,
             null /*sortOrder*/);
     if (cursor != null) {
       String mmscUrl = null;
       String proxyAddress = null;
       int proxyPort = -1;
       while (cursor.moveToNext()) {
         // Read values from APN settings
         if (isValidApnType(cursor.getString(COLUMN_TYPE), PhoneConstants.APN_TYPE_MMS)) {
           mmscUrl = trimWithNullCheck(cursor.getString(COLUMN_MMSC));
           if (TextUtils.isEmpty(mmscUrl)) {
             continue;
           }
           mmscUrl = NetworkUtils.trimV4AddrZeros(mmscUrl);
           try {
             new URI(mmscUrl);
           } catch (URISyntaxException e) {
             throw new ApnException("Invalid MMSC url " + mmscUrl);
           }
           proxyAddress = trimWithNullCheck(cursor.getString(COLUMN_MMSPROXY));
           if (!TextUtils.isEmpty(proxyAddress)) {
             proxyAddress = NetworkUtils.trimV4AddrZeros(proxyAddress);
             final String portString = trimWithNullCheck(cursor.getString(COLUMN_MMSPORT));
             if (portString != null) {
               try {
                 proxyPort = Integer.parseInt(portString);
               } catch (NumberFormatException e) {
                 Log.e(TAG, "Invalid port " + portString);
                 throw new ApnException("Invalid port " + portString);
               }
             }
           }
           return new ApnSettings(mmscUrl, proxyAddress, proxyPort, getDebugText(cursor));
         }
       }
     }
   } finally {
     if (cursor != null) {
       cursor.close();
     }
   }
   throw new ApnException("Can not find valid APN");
 }
  private int validateIpConfigFields(LinkProperties linkProperties) {
    if (mIpAddressView == null) return 0;

    String ipAddr = mIpAddressView.getText().toString();
    if (TextUtils.isEmpty(ipAddr)) return R.string.wifi_ip_settings_invalid_ip_address;

    InetAddress inetAddr = null;
    try {
      inetAddr = NetworkUtils.numericToInetAddress(ipAddr);
    } catch (IllegalArgumentException e) {
      return R.string.wifi_ip_settings_invalid_ip_address;
    }

    int networkPrefixLength = -1;
    try {
      networkPrefixLength = Integer.parseInt(mNetworkPrefixLengthView.getText().toString());
      if (networkPrefixLength < 0 || networkPrefixLength > 32) {
        return R.string.wifi_ip_settings_invalid_network_prefix_length;
      }
      linkProperties.addLinkAddress(new LinkAddress(inetAddr, networkPrefixLength));
    } catch (NumberFormatException e) {
      // Set the hint as default after user types in ip address
      mNetworkPrefixLengthView.setText(
          mConfigUi.getContext().getString(R.string.wifi_network_prefix_length_hint));
    }

    String gateway = mGatewayView.getText().toString();
    if (TextUtils.isEmpty(gateway)) {
      try {
        // Extract a default gateway from IP address
        InetAddress netPart = NetworkUtils.getNetworkPart(inetAddr, networkPrefixLength);
        byte[] addr = netPart.getAddress();
        addr[addr.length - 1] = 1;
        mGatewayView.setText(InetAddress.getByAddress(addr).getHostAddress());
      } catch (RuntimeException ee) {
      } catch (java.net.UnknownHostException u) {
      }
    } else {
      InetAddress gatewayAddr = null;
      try {
        gatewayAddr = NetworkUtils.numericToInetAddress(gateway);
      } catch (IllegalArgumentException e) {
        return R.string.wifi_ip_settings_invalid_gateway;
      }
      linkProperties.addRoute(new RouteInfo(gatewayAddr));
    }

    String dns = mDns1View.getText().toString();
    InetAddress dnsAddr = null;

    if (TextUtils.isEmpty(dns)) {
      // If everything else is valid, provide hint as a default option
      mDns1View.setText(mConfigUi.getContext().getString(R.string.wifi_dns1_hint));
    } else {
      try {
        dnsAddr = NetworkUtils.numericToInetAddress(dns);
      } catch (IllegalArgumentException e) {
        return R.string.wifi_ip_settings_invalid_dns;
      }
      linkProperties.addDns(dnsAddr);
    }

    if (mDns2View.length() > 0) {
      dns = mDns2View.getText().toString();
      try {
        dnsAddr = NetworkUtils.numericToInetAddress(dns);
      } catch (IllegalArgumentException e) {
        return R.string.wifi_ip_settings_invalid_dns;
      }
      linkProperties.addDns(dnsAddr);
    }
    return 0;
  }