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         String selection = TextUtils.isEmpty(apnName) ? null :
     65                 Telephony.Carriers.APN + "='" + apnName.trim() + "'";
     66 
     67         Cursor cursor = SqliteWrapper.query(context, context.getContentResolver(),
     68                             Uri.withAppendedPath(Telephony.Carriers.CONTENT_URI, "current"),
     69                             APN_PROJECTION, selection, null, null);
     70 
     71         if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
     72             Log.v(TAG, "TransactionSettings looking for apn: " + selection + " returned: " +
     73                     (cursor ==null ? "null cursor" : (cursor.getCount() + " hits")));
     74         }
     75 
     76         if (cursor == null) {
     77             Log.e(TAG, "Apn is not found in Database!");
     78             return;
     79         }
     80 
     81         boolean sawValidApn = false;
     82         try {
     83             while (cursor.moveToNext() && TextUtils.isEmpty(mServiceCenter)) {
     84                 // Read values from APN settings
     85                 if (isValidApnType(cursor.getString(COLUMN_TYPE), Phone.APN_TYPE_MMS)) {
     86                     sawValidApn = true;
     87                     mServiceCenter = NetworkUtils.trimV4AddrZeros(
     88                             cursor.getString(COLUMN_MMSC).trim());
     89                     mProxyAddress = NetworkUtils.trimV4AddrZeros(
     90                             cursor.getString(COLUMN_MMSPROXY));
     91                     if (isProxySet()) {
     92                         String portString = cursor.getString(COLUMN_MMSPORT);
     93                         try {
     94                             mProxyPort = Integer.parseInt(portString);
     95                         } catch (NumberFormatException e) {
     96                             if (TextUtils.isEmpty(portString)) {
     97                                 Log.w(TAG, "mms port not set!");
     98                             } else {
     99                                 Log.e(TAG, "Bad port number format: " + portString, e);
    100                             }
    101                         }
    102                     }
    103                 }
    104             }
    105         } finally {
    106             cursor.close();
    107         }
    108 
    109         Log.v(TAG, "APN setting: MMSC: " + mServiceCenter + " looked for: " + selection);
    110 
    111         if (sawValidApn && TextUtils.isEmpty(mServiceCenter)) {
    112             Log.e(TAG, "Invalid APN setting: MMSC is empty");
    113         }
    114     }
    115 
    116     /**
    117      * Constructor that overrides the default settings of the MMS Client.
    118      *
    119      * @param mmscUrl The MMSC URL
    120      * @param proxyAddr The proxy address
    121      * @param proxyPort The port used by the proxy address
    122      * immediately start a SendTransaction upon completion of a NotificationTransaction,
    123      * false otherwise.
    124      */
    125     public TransactionSettings(String mmscUrl, String proxyAddr, int proxyPort) {
    126         mServiceCenter = mmscUrl != null ? mmscUrl.trim() : null;
    127         mProxyAddress = proxyAddr;
    128         mProxyPort = proxyPort;
    129 
    130         if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
    131             Log.v(TAG, "TransactionSettings: " + mServiceCenter +
    132                     " proxyAddress: " + mProxyAddress +
    133                     " proxyPort: " + mProxyPort);
    134         }
    135    }
    136 
    137     public String getMmscUrl() {
    138         return mServiceCenter;
    139     }
    140 
    141     public String getProxyAddress() {
    142         return mProxyAddress;
    143     }
    144 
    145     public int getProxyPort() {
    146         return mProxyPort;
    147     }
    148 
    149     public boolean isProxySet() {
    150         return (mProxyAddress != null) && (mProxyAddress.trim().length() != 0);
    151     }
    152 
    153     static private boolean isValidApnType(String types, String requestType) {
    154         // If APN type is unspecified, assume APN_TYPE_ALL.
    155         if (TextUtils.isEmpty(types)) {
    156             return true;
    157         }
    158 
    159         for (String t : types.split(",")) {
    160             if (t.equals(requestType) || t.equals(Phone.APN_TYPE_ALL)) {
    161                 return true;
    162             }
    163         }
    164         return false;
    165     }
    166 }
    167