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 android.provider.Telephony;
     27 import android.text.TextUtils;
     28 import android.util.Config;
     29 import android.util.Log;
     30 
     31 
     32 /**
     33  * Container of transaction settings. Instances of this class are contained
     34  * within Transaction instances to allow overriding of the default APN
     35  * settings or of the MMS Client.
     36  */
     37 public class TransactionSettings {
     38     private static final String TAG = "TransactionSettings";
     39     private static final boolean DEBUG = true;
     40     private static final boolean LOCAL_LOGV = DEBUG ? Config.LOGD : Config.LOGV;
     41 
     42     private String mServiceCenter;
     43     private String mProxyAddress;
     44     private int mProxyPort = -1;
     45 
     46     private static final String[] APN_PROJECTION = {
     47             Telephony.Carriers.TYPE,            // 0
     48             Telephony.Carriers.MMSC,            // 1
     49             Telephony.Carriers.MMSPROXY,        // 2
     50             Telephony.Carriers.MMSPORT          // 3
     51     };
     52     private static final int COLUMN_TYPE         = 0;
     53     private static final int COLUMN_MMSC         = 1;
     54     private static final int COLUMN_MMSPROXY     = 2;
     55     private static final int COLUMN_MMSPORT      = 3;
     56 
     57     /**
     58      * Constructor that uses the default settings of the MMS Client.
     59      *
     60      * @param context The context of the MMS Client
     61      */
     62     public TransactionSettings(Context context, String apnName) {
     63         String selection = (apnName != null)?
     64                 Telephony.Carriers.APN + "='" + apnName.trim() + "'": null;
     65 
     66         Cursor cursor = SqliteWrapper.query(context, context.getContentResolver(),
     67                             Uri.withAppendedPath(Telephony.Carriers.CONTENT_URI, "current"),
     68                             APN_PROJECTION, selection, null, null);
     69 
     70         if (cursor == null) {
     71             Log.e(TAG, "Apn is not found in Database!");
     72             return;
     73         }
     74 
     75         boolean sawValidApn = false;
     76         try {
     77             while (cursor.moveToNext() && TextUtils.isEmpty(mServiceCenter)) {
     78                 // Read values from APN settings
     79                 if (isValidApnType(cursor.getString(COLUMN_TYPE), Phone.APN_TYPE_MMS)) {
     80                     sawValidApn = true;
     81                     mServiceCenter = cursor.getString(COLUMN_MMSC).trim();
     82                     mProxyAddress = cursor.getString(COLUMN_MMSPROXY);
     83                     if (isProxySet()) {
     84                         String portString = cursor.getString(COLUMN_MMSPORT);
     85                         try {
     86                             mProxyPort = Integer.parseInt(portString);
     87                         } catch (NumberFormatException e) {
     88                             if (TextUtils.isEmpty(portString)) {
     89                                 Log.w(TAG, "mms port not set!");
     90                             } else {
     91                                 Log.e(TAG, "Bad port number format: " + portString, e);
     92                             }
     93                         }
     94                     }
     95                 }
     96             }
     97         } finally {
     98             cursor.close();
     99         }
    100 
    101         if (sawValidApn && TextUtils.isEmpty(mServiceCenter)) {
    102             Log.e(TAG, "Invalid APN setting: MMSC is empty");
    103         }
    104     }
    105 
    106     /**
    107      * Constructor that overrides the default settings of the MMS Client.
    108      *
    109      * @param mmscUrl The MMSC URL
    110      * @param proxyAddr The proxy address
    111      * @param proxyPort The port used by the proxy address
    112      * immediately start a SendTransaction upon completion of a NotificationTransaction,
    113      * false otherwise.
    114      */
    115     public TransactionSettings(String mmscUrl, String proxyAddr, int proxyPort) {
    116         mServiceCenter = mmscUrl != null ? mmscUrl.trim() : null;
    117         mProxyAddress = proxyAddr;
    118         mProxyPort = proxyPort;
    119     }
    120 
    121     public String getMmscUrl() {
    122         return mServiceCenter;
    123     }
    124 
    125     public String getProxyAddress() {
    126         return mProxyAddress;
    127     }
    128 
    129     public int getProxyPort() {
    130         return mProxyPort;
    131     }
    132 
    133     public boolean isProxySet() {
    134         return (mProxyAddress != null) && (mProxyAddress.trim().length() != 0);
    135     }
    136 
    137     static private boolean isValidApnType(String types, String requestType) {
    138         // If APN type is unspecified, assume APN_TYPE_ALL.
    139         if (TextUtils.isEmpty(types)) {
    140             return true;
    141         }
    142 
    143         for (String t : types.split(",")) {
    144             if (t.equals(requestType) || t.equals(Phone.APN_TYPE_ALL)) {
    145                 return true;
    146             }
    147         }
    148         return false;
    149     }
    150 }
    151