Home | History | Annotate | Download | only in transaction
      1 /*
      2  * Copyright (C) 2007-2008 Esmertec AG.
      3  * Copyright (C) 2007-2008 The Android Open Source Project
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *      http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 package com.android.mms.transaction;
     19 
     20 import android.database.sqlite.SqliteWrapper;
     21 
     22 import android.content.Context;
     23 import android.database.Cursor;
     24 import android.net.Uri;
     25 import com.android.internal.telephony.Phone;
     26 import com.android.mms.LogTag;
     27 
     28 import android.net.NetworkUtils;
     29 import android.provider.Telephony;
     30 import android.text.TextUtils;
     31 import android.util.Log;
     32 
     33 /**
     34  * Container of transaction settings. Instances of this class are contained
     35  * within Transaction instances to allow overriding of the default APN
     36  * settings or of the MMS Client.
     37  */
     38 public class TransactionSettings {
     39     private static final String TAG = "TransactionSettings";
     40     private static final boolean DEBUG = true;
     41     private static final boolean LOCAL_LOGV = false;
     42 
     43     private String mServiceCenter;
     44     private String mProxyAddress;
     45     private int mProxyPort = -1;
     46 
     47     private static final String[] APN_PROJECTION = {
     48             Telephony.Carriers.TYPE,            // 0
     49             Telephony.Carriers.MMSC,            // 1
     50             Telephony.Carriers.MMSPROXY,        // 2
     51             Telephony.Carriers.MMSPORT          // 3
     52     };
     53     private static final int COLUMN_TYPE         = 0;
     54     private static final int COLUMN_MMSC         = 1;
     55     private static final int COLUMN_MMSPROXY     = 2;
     56     private static final int COLUMN_MMSPORT      = 3;
     57 
     58     /**
     59      * Constructor that uses the default settings of the MMS Client.
     60      *
     61      * @param context The context of the MMS Client
     62      */
     63     public TransactionSettings(Context context, String apnName) {
     64         if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
     65             Log.v(TAG, "TransactionSettings: apnName: " + apnName);
     66         }
     67         String selection = Telephony.Carriers.CURRENT + " IS NOT NULL";
     68         String[] selectionArgs = null;
     69         if (!TextUtils.isEmpty(apnName)) {
     70             selection += " AND " + Telephony.Carriers.APN + "=?";
     71             selectionArgs = new String[]{ apnName.trim() };
     72         }
     73 
     74         Cursor cursor = SqliteWrapper.query(context, context.getContentResolver(),
     75                             Telephony.Carriers.CONTENT_URI,
     76                             APN_PROJECTION, selection, selectionArgs, null);
     77 
     78         if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
     79             Log.v(TAG, "TransactionSettings looking for apn: " + selection + " returned: " +
     80                     (cursor ==null ? "null cursor" : (cursor.getCount() + " hits")));
     81         }
     82 
     83         if (cursor == null) {
     84             Log.e(TAG, "Apn is not found in Database!");
     85             return;
     86         }
     87 
     88         boolean sawValidApn = false;
     89         try {
     90             while (cursor.moveToNext() && TextUtils.isEmpty(mServiceCenter)) {
     91                 // Read values from APN settings
     92                 if (isValidApnType(cursor.getString(COLUMN_TYPE), Phone.APN_TYPE_MMS)) {
     93                     sawValidApn = true;
     94                     mServiceCenter = NetworkUtils.trimV4AddrZeros(
     95                             cursor.getString(COLUMN_MMSC).trim());
     96                     mProxyAddress = NetworkUtils.trimV4AddrZeros(
     97                             cursor.getString(COLUMN_MMSPROXY));
     98                     if (isProxySet()) {
     99                         String portString = cursor.getString(COLUMN_MMSPORT);
    100                         try {
    101                             mProxyPort = Integer.parseInt(portString);
    102                         } catch (NumberFormatException e) {
    103                             if (TextUtils.isEmpty(portString)) {
    104                                 Log.w(TAG, "mms port not set!");
    105                             } else {
    106                                 Log.e(TAG, "Bad port number format: " + portString, e);
    107                             }
    108                         }
    109                     }
    110                 }
    111             }
    112         } finally {
    113             cursor.close();
    114         }
    115 
    116         Log.v(TAG, "APN setting: MMSC: " + mServiceCenter + " looked for: " + selection);
    117 
    118         if (sawValidApn && TextUtils.isEmpty(mServiceCenter)) {
    119             Log.e(TAG, "Invalid APN setting: MMSC is empty");
    120         }
    121     }
    122 
    123     /**
    124      * Constructor that overrides the default settings of the MMS Client.
    125      *
    126      * @param mmscUrl The MMSC URL
    127      * @param proxyAddr The proxy address
    128      * @param proxyPort The port used by the proxy address
    129      * immediately start a SendTransaction upon completion of a NotificationTransaction,
    130      * false otherwise.
    131      */
    132     public TransactionSettings(String mmscUrl, String proxyAddr, int proxyPort) {
    133         mServiceCenter = mmscUrl != null ? mmscUrl.trim() : null;
    134         mProxyAddress = proxyAddr;
    135         mProxyPort = proxyPort;
    136 
    137         if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
    138             Log.v(TAG, "TransactionSettings: " + mServiceCenter +
    139                     " proxyAddress: " + mProxyAddress +
    140                     " proxyPort: " + mProxyPort);
    141         }
    142    }
    143 
    144     public String getMmscUrl() {
    145         return mServiceCenter;
    146     }
    147 
    148     public String getProxyAddress() {
    149         return mProxyAddress;
    150     }
    151 
    152     public int getProxyPort() {
    153         return mProxyPort;
    154     }
    155 
    156     public boolean isProxySet() {
    157         return (mProxyAddress != null) && (mProxyAddress.trim().length() != 0);
    158     }
    159 
    160     static private boolean isValidApnType(String types, String requestType) {
    161         // If APN type is unspecified, assume APN_TYPE_ALL.
    162         if (TextUtils.isEmpty(types)) {
    163             return true;
    164         }
    165 
    166         for (String t : types.split(",")) {
    167             if (t.equals(requestType) || t.equals(Phone.APN_TYPE_ALL)) {
    168                 return true;
    169             }
    170         }
    171         return false;
    172     }
    173 }
    174