1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.mms; 18 19 import java.io.IOException; 20 21 import org.xmlpull.v1.XmlPullParser; 22 import org.xmlpull.v1.XmlPullParserException; 23 24 import android.content.Context; 25 import android.content.res.XmlResourceParser; 26 import android.util.Config; 27 import android.util.Log; 28 29 public class MmsConfig { 30 private static final String TAG = "MmsConfig"; 31 private static final boolean DEBUG = false; 32 private static final boolean LOCAL_LOGV = DEBUG ? Config.LOGD : Config.LOGV; 33 34 private static final String DEFAULT_HTTP_KEY_X_WAP_PROFILE = "x-wap-profile"; 35 private static final String DEFAULT_USER_AGENT = "Android-Mms/2.0"; 36 37 private static final int MAX_IMAGE_HEIGHT = 480; 38 private static final int MAX_IMAGE_WIDTH = 640; 39 40 /** 41 * Whether to hide MMS functionality from the user (i.e. SMS only). 42 */ 43 private static boolean mTransIdEnabled = false; 44 private static int mMmsEnabled = 1; // default to true 45 private static int mMaxMessageSize = 300 * 1024; // default to 300k max size 46 private static String mUserAgent = DEFAULT_USER_AGENT; 47 private static String mUaProfTagName = DEFAULT_HTTP_KEY_X_WAP_PROFILE; 48 private static String mUaProfUrl = null; 49 private static String mHttpParams = null; 50 private static String mHttpParamsLine1Key = null; 51 private static String mEmailGateway = null; 52 private static int mMaxImageHeight = MAX_IMAGE_HEIGHT; // default value 53 private static int mMaxImageWidth = MAX_IMAGE_WIDTH; // default value 54 private static int mRecipientLimit = Integer.MAX_VALUE; // default value 55 private static int mDefaultSMSMessagesPerThread = 200; // default value 56 private static int mDefaultMMSMessagesPerThread = 20; // default value 57 private static int mMinMessageCountPerThread = 2; // default value 58 private static int mMaxMessageCountPerThread = 5000; // default value 59 private static int mHttpSocketTimeout = 60*1000; // default to 1 min 60 private static int mMinimumSlideElementDuration = 7; // default to 7 sec 61 private static boolean mNotifyWapMMSC = false; 62 private static boolean mAllowAttachAudio = true; 63 64 // This is the max amount of storage multiplied by mMaxMessageSize that we 65 // allow of unsent messages before blocking the user from sending any more 66 // MMS's. 67 private static int mMaxSizeScaleForPendingMmsAllowed = 4; // default value 68 69 // Email gateway alias support, including the master switch and different rules 70 private static boolean mAliasEnabled = false; 71 private static int mAliasRuleMinChars = 2; 72 private static int mAliasRuleMaxChars = 48; 73 74 public static void init(Context context) { 75 if (LOCAL_LOGV) { 76 Log.v(TAG, "MmsConfig.init()"); 77 } 78 79 loadMmsSettings(context); 80 } 81 82 public static boolean getMmsEnabled() { 83 return mMmsEnabled == 1 ? true : false; 84 } 85 86 public static int getMaxMessageSize() { 87 return mMaxMessageSize; 88 } 89 90 /** 91 * This function returns the value of "enabledTransID" present in mms_config file. 92 * In case of single segment wap push message, this "enabledTransID" indicates whether 93 * TransactionID should be appended to URI or not. 94 */ 95 public static boolean getTransIdEnabled() { 96 return mTransIdEnabled; 97 } 98 99 public static String getUserAgent() { 100 return mUserAgent; 101 } 102 103 public static String getUaProfTagName() { 104 return mUaProfTagName; 105 } 106 107 public static String getUaProfUrl() { 108 return mUaProfUrl; 109 } 110 111 public static String getHttpParams() { 112 return mHttpParams; 113 } 114 115 public static String getHttpParamsLine1Key() { 116 return mHttpParamsLine1Key; 117 } 118 119 public static String getEmailGateway() { 120 return mEmailGateway; 121 } 122 123 public static int getMaxImageHeight() { 124 return mMaxImageHeight; 125 } 126 127 public static int getMaxImageWidth() { 128 return mMaxImageWidth; 129 } 130 131 public static int getRecipientLimit() { 132 return mRecipientLimit; 133 } 134 135 public static int getDefaultSMSMessagesPerThread() { 136 return mDefaultSMSMessagesPerThread; 137 } 138 139 public static int getDefaultMMSMessagesPerThread() { 140 return mDefaultMMSMessagesPerThread; 141 } 142 143 public static int getMinMessageCountPerThread() { 144 return mMinMessageCountPerThread; 145 } 146 147 public static int getMaxMessageCountPerThread() { 148 return mMaxMessageCountPerThread; 149 } 150 151 public static int getHttpSocketTimeout() { 152 return mHttpSocketTimeout; 153 } 154 155 public static int getMinimumSlideElementDuration() { 156 return mMinimumSlideElementDuration; 157 } 158 159 public static boolean getNotifyWapMMSC() { 160 return mNotifyWapMMSC; 161 } 162 163 public static int getMaxSizeScaleForPendingMmsAllowed() { 164 return mMaxSizeScaleForPendingMmsAllowed; 165 } 166 167 public static boolean isAliasEnabled() { 168 return mAliasEnabled; 169 } 170 171 public static int getAliasMinChars() { 172 return mAliasRuleMinChars; 173 } 174 175 public static int getAliasMaxChars() { 176 return mAliasRuleMaxChars; 177 } 178 179 public static boolean getAllowAttachAudio() { 180 return mAllowAttachAudio; 181 } 182 183 public static final void beginDocument(XmlPullParser parser, String firstElementName) throws XmlPullParserException, IOException 184 { 185 int type; 186 while ((type=parser.next()) != parser.START_TAG 187 && type != parser.END_DOCUMENT) { 188 ; 189 } 190 191 if (type != parser.START_TAG) { 192 throw new XmlPullParserException("No start tag found"); 193 } 194 195 if (!parser.getName().equals(firstElementName)) { 196 throw new XmlPullParserException("Unexpected start tag: found " + parser.getName() + 197 ", expected " + firstElementName); 198 } 199 } 200 201 public static final void nextElement(XmlPullParser parser) throws XmlPullParserException, IOException 202 { 203 int type; 204 while ((type=parser.next()) != parser.START_TAG 205 && type != parser.END_DOCUMENT) { 206 ; 207 } 208 } 209 210 private static void loadMmsSettings(Context context) { 211 XmlResourceParser parser = context.getResources().getXml(R.xml.mms_config); 212 213 try { 214 beginDocument(parser, "mms_config"); 215 216 while (true) { 217 nextElement(parser); 218 String tag = parser.getName(); 219 if (tag == null) { 220 break; 221 } 222 String name = parser.getAttributeName(0); 223 String value = parser.getAttributeValue(0); 224 String text = null; 225 if (parser.next() == XmlPullParser.TEXT) { 226 text = parser.getText(); 227 } 228 229 if (DEBUG) { 230 Log.v(TAG, "tag: " + tag + " value: " + value); 231 } 232 if ("name".equalsIgnoreCase(name)) { 233 if ("bool".equals(tag)) { 234 // bool config tags go here 235 if ("enabledMMS".equalsIgnoreCase(value)) { 236 mMmsEnabled = "true".equalsIgnoreCase(text) ? 1 : 0; 237 } else if ("enabledTransID".equalsIgnoreCase(value)) { 238 mTransIdEnabled = "true".equalsIgnoreCase(text); 239 } else if ("enabledNotifyWapMMSC".equalsIgnoreCase(value)) { 240 mNotifyWapMMSC = "true".equalsIgnoreCase(text); 241 } else if ("aliasEnabled".equalsIgnoreCase(value)) { 242 mAliasEnabled = "true".equalsIgnoreCase(text); 243 } else if ("allowAttachAudio".equalsIgnoreCase(value)) { 244 mAllowAttachAudio = "true".equalsIgnoreCase(text); 245 } 246 } else if ("int".equals(tag)) { 247 // int config tags go here 248 if ("maxMessageSize".equalsIgnoreCase(value)) { 249 mMaxMessageSize = Integer.parseInt(text); 250 } else if ("maxImageHeight".equalsIgnoreCase(value)) { 251 mMaxImageHeight = Integer.parseInt(text); 252 } else if ("maxImageWidth".equalsIgnoreCase(value)) { 253 mMaxImageWidth = Integer.parseInt(text); 254 } else if ("defaultSMSMessagesPerThread".equalsIgnoreCase(value)) { 255 mDefaultSMSMessagesPerThread = Integer.parseInt(text); 256 } else if ("defaultMMSMessagesPerThread".equalsIgnoreCase(value)) { 257 mDefaultMMSMessagesPerThread = Integer.parseInt(text); 258 } else if ("minMessageCountPerThread".equalsIgnoreCase(value)) { 259 mMinMessageCountPerThread = Integer.parseInt(text); 260 } else if ("maxMessageCountPerThread".equalsIgnoreCase(value)) { 261 mMaxMessageCountPerThread = Integer.parseInt(text); 262 } else if ("recipientLimit".equalsIgnoreCase(value)) { 263 mRecipientLimit = Integer.parseInt(text); 264 if (mRecipientLimit < 0) { 265 mRecipientLimit = Integer.MAX_VALUE; 266 } 267 } else if ("httpSocketTimeout".equalsIgnoreCase(value)) { 268 mHttpSocketTimeout = Integer.parseInt(text); 269 } else if ("minimumSlideElementDuration".equalsIgnoreCase(value)) { 270 mMinimumSlideElementDuration = Integer.parseInt(text); 271 } else if ("maxSizeScaleForPendingMmsAllowed".equalsIgnoreCase(value)) { 272 mMaxSizeScaleForPendingMmsAllowed = Integer.parseInt(text); 273 } else if ("aliasMinChars".equalsIgnoreCase(value)) { 274 mAliasRuleMinChars = Integer.parseInt(text); 275 } else if ("aliasMaxChars".equalsIgnoreCase(value)) { 276 mAliasRuleMaxChars = Integer.parseInt(text); 277 } 278 } else if ("string".equals(tag)) { 279 // string config tags go here 280 if ("userAgent".equalsIgnoreCase(value)) { 281 mUserAgent = text; 282 } else if ("uaProfTagName".equalsIgnoreCase(value)) { 283 mUaProfTagName = text; 284 } else if ("uaProfUrl".equalsIgnoreCase(value)) { 285 mUaProfUrl = text; 286 } else if ("httpParams".equalsIgnoreCase(value)) { 287 mHttpParams = text; 288 } else if ("httpParamsLine1Key".equalsIgnoreCase(value)) { 289 mHttpParamsLine1Key = text; 290 } else if ("emailGatewayNumber".equalsIgnoreCase(value)) { 291 mEmailGateway = text; 292 } 293 } 294 } 295 } 296 } catch (XmlPullParserException e) { 297 Log.e(TAG, "loadMmsSettings caught ", e); 298 } catch (NumberFormatException e) { 299 Log.e(TAG, "loadMmsSettings caught ", e); 300 } catch (IOException e) { 301 Log.e(TAG, "loadMmsSettings caught ", e); 302 } finally { 303 parser.close(); 304 } 305 306 String errorStr = null; 307 308 if (getMmsEnabled() && mUaProfUrl == null) { 309 errorStr = "uaProfUrl"; 310 } 311 312 if (errorStr != null) { 313 String err = 314 String.format("MmsConfig.loadMmsSettings mms_config.xml missing %s setting", 315 errorStr); 316 Log.e(TAG, err); 317 throw new ContentRestrictionException(err); 318 } 319 } 320 321 } 322