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.provider.Telephony;
     29 import android.text.TextUtils;
     30 import android.util.Log;
     31 
     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 = cursor.getString(COLUMN_MMSC).trim();
     88                     mProxyAddress = cursor.getString(COLUMN_MMSPROXY);
     89                     if (isProxySet()) {
     90                         String portString = cursor.getString(COLUMN_MMSPORT);
     91                         try {
     92                             mProxyPort = Integer.parseInt(portString);
     93                         } catch (NumberFormatException e) {
     94                             if (TextUtils.isEmpty(portString)) {
     95                                 Log.w(TAG, "mms port not set!");
     96                             } else {
     97                                 Log.e(TAG, "Bad port number format: " + portString, e);
     98                             }
     99                         }
    100                     }
    101                 }
    102             }
    103         } finally {
    104             cursor.close();
    105         }
    106 
    107         Log.v(TAG, "APN setting: MMSC: " + mServiceCenter + " looked for: " + selection);
    108 
    109         if (sawValidApn && TextUtils.isEmpty(mServiceCenter)) {
    110             Log.e(TAG, "Invalid APN setting: MMSC is empty");
    111         }
    112     }
    113 
    114     /**
    115      * Constructor that overrides the default settings of the MMS Client.
    116      *
    117      * @param mmscUrl The MMSC URL
    118      * @param proxyAddr The proxy address
    119      * @param proxyPort The port used by the proxy address
    120      * immediately start a SendTransaction upon completion of a NotificationTransaction,
    121      * false otherwise.
    122      */
    123     public TransactionSettings(String mmscUrl, String proxyAddr, int proxyPort) {
    124         mServiceCenter = mmscUrl != null ? mmscUrl.trim() : null;
    125         mProxyAddress = proxyAddr;
    126         mProxyPort = proxyPort;
    127 
    128         if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
    129             Log.v(TAG, "TransactionSettings: " + mServiceCenter +
    130                     " proxyAddress: " + mProxyAddress +
    131                     " proxyPort: " + mProxyPort);
    132         }
    133    }
    134 
    135     public String getMmscUrl() {
    136         return mServiceCenter;
    137     }
    138 
    139     public String getProxyAddress() {
    140         return mProxyAddress;
    141     }
    142 
    143     public int getProxyPort() {
    144         return mProxyPort;
    145     }
    146 
    147     public boolean isProxySet() {
    148         return (mProxyAddress != null) && (mProxyAddress.trim().length() != 0);
    149     }
    150 
    151     static private boolean isValidApnType(String types, String requestType) {
    152         // If APN type is unspecified, assume APN_TYPE_ALL.
    153         if (TextUtils.isEmpty(types)) {
    154             return true;
    155         }
    156 
    157         for (String t : types.split(",")) {
    158             if (t.equals(requestType) || t.equals(Phone.APN_TYPE_ALL)) {
    159                 return true;
    160             }
    161         }
    162         return false;
    163     }
    164 }
    165