/**
   * 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");
    }
  }
Ejemplo n.º 2
0
 /**
  * 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");
 }