From 57858606312290666eba6a8534a80b279421377a Mon Sep 17 00:00:00 2001 From: Ruben Pollan Date: Tue, 14 Jan 2014 02:02:52 -1000 Subject: [PATCH] Support for multiple APN settings on the same provider --- .../securesms/mms/ApnDefaults.java | 4 +- .../securesms/mms/MmsCommunication.java | 83 ++++++++++++------- .../securesms/mms/MmsDownloadHelper.java | 14 ++-- .../securesms/mms/MmsSendHelper.java | 16 ++-- 4 files changed, 70 insertions(+), 47 deletions(-) diff --git a/src/org/thoughtcrime/securesms/mms/ApnDefaults.java b/src/org/thoughtcrime/securesms/mms/ApnDefaults.java index 9926fc13b7..cd5e358f76 100644 --- a/src/org/thoughtcrime/securesms/mms/ApnDefaults.java +++ b/src/org/thoughtcrime/securesms/mms/ApnDefaults.java @@ -22,7 +22,8 @@ public class ApnDefaults { put("310260", new MmsConnectionParameters("http://mms.msg.eng.t-mobile.com/mms/wapenc", null, null)); //AT&T - Testd - put("310410", new MmsConnectionParameters("http://mmsc.mobile.att.net", "proxy.mobile.att.net", "80")); + put("310410", (new MmsConnectionParameters("http://mmsc.cingular.com", "wireless.cingular.com", "80")) + .add("http://mmsc.mobile.att.net", "proxy.mobile.att.net", "80")); //Verizon - Untested put("310004", new MmsConnectionParameters("http://mms.vtext.com/servlets/mms", null, null)); @@ -257,7 +258,6 @@ public class ApnDefaults { put("31039", new MmsConnectionParameters("http://mms.metropcs.net:3128/mmsc", "proxy.metropcs.net", "3128")); put("31040", new MmsConnectionParameters("http://mms.sprintpcs.com", "68.28.31.7", "80")); put("31041", new MmsConnectionParameters("http://mms.metropcs.net:3128/mmsc", "proxy.metropcs.net", "3128")); - put("310410", new MmsConnectionParameters("http://mmsc.mobile.att.net", "proxy.mobile.att.net", "80")); put("31042", new MmsConnectionParameters("http://mms.metropcs.net:3128/mmsc", "proxy.metropcs.net", "3128")); put("310420", new MmsConnectionParameters("http://mms.gocbw.com:8088/mms", "216.68.79.202", "80")); put("31043", new MmsConnectionParameters("http://mms.metropcs.net:3128/mmsc", "proxy.metropcs.net", "3128")); diff --git a/src/org/thoughtcrime/securesms/mms/MmsCommunication.java b/src/org/thoughtcrime/securesms/mms/MmsCommunication.java index 7bc19fac19..a3c8683f74 100644 --- a/src/org/thoughtcrime/securesms/mms/MmsCommunication.java +++ b/src/org/thoughtcrime/securesms/mms/MmsCommunication.java @@ -39,6 +39,8 @@ import org.whispersystems.textsecure.util.Util; import java.io.DataInputStream; import java.io.IOException; import java.net.InetAddress; +import java.util.ArrayList; +import java.util.List; public class MmsCommunication { @@ -122,17 +124,17 @@ public class MmsCommunication { } } - protected static void checkRouteToHost(Context context, MmsConnectionParameters parameters, + protected static boolean checkRouteToHost(Context context, MmsConnectionParameters.Apn parameters, String url, boolean usingMmsRadio) throws IOException { if (parameters == null || !parameters.hasProxy()) - checkRouteToHost(context, Uri.parse(url).getHost(), usingMmsRadio); + return checkRouteToHost(context, Uri.parse(url).getHost(), usingMmsRadio); else - checkRouteToHost(context, parameters.getProxy(), usingMmsRadio); + return checkRouteToHost(context, parameters.getProxy(), usingMmsRadio); } - private static void checkRouteToHost(Context context, String host, boolean usingMmsRadio) + private static boolean checkRouteToHost(Context context, String host, boolean usingMmsRadio) throws IOException { InetAddress inetAddress = InetAddress.getByName(host); @@ -142,7 +144,7 @@ public class MmsCommunication { throw new IOException("RFC1918 address in non-MMS radio situation!"); } - return; + return true; } Log.w("MmsCommunication", "Checking route to address: " + host + " , " + inetAddress.getHostAddress()); @@ -153,12 +155,12 @@ public class MmsCommunication { int ipAddress = Conversions.byteArrayToIntLittleEndian(ipAddressBytes, 0); ConnectivityManager manager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); - if (!manager.requestRouteToHost(MmsRadio.TYPE_MOBILE_MMS, ipAddress)) - throw new IOException("Connection manager could not obtain route to host."); + return manager.requestRouteToHost(MmsRadio.TYPE_MOBILE_MMS, ipAddress); } + return true; } - protected static AndroidHttpClient constructHttpClient(Context context, MmsConnectionParameters mmsConfig) { + protected static AndroidHttpClient constructHttpClient(Context context, MmsConnectionParameters.Apn mmsConfig) { AndroidHttpClient client = AndroidHttpClient.newInstance("Android-Mms/2.0", context); HttpParams params = client.getParams(); HttpProtocolParams.setContentCharset(params, "UTF-8"); @@ -188,36 +190,53 @@ public class MmsCommunication { } protected static class MmsConnectionParameters { - private final String mmsc; - private final String proxy; - private final String port; + public class Apn { + private final String mmsc; + private final String proxy; + private final String port; + + public Apn(String mmsc, String proxy, String port) { + this.mmsc = mmsc; + this.proxy = proxy; + this.port = port; + } + + public boolean hasProxy() { + return !Util.isEmpty(proxy); + } + + public String getMmsc() { + return mmsc; + } + + public String getProxy() { + if (!hasProxy()) + return null; + + return proxy; + } + + public int getPort() { + if (Util.isEmpty(port)) + return 80; + + return Integer.parseInt(port); + } + } + + private List apn = new ArrayList(); public MmsConnectionParameters(String mmsc, String proxy, String port) { - this.mmsc = mmsc; - this.proxy = proxy; - this.port = port; + apn.add(new Apn(mmsc, proxy, port)); } - public boolean hasProxy() { - return !Util.isEmpty(proxy); + public MmsConnectionParameters add(String mmsc, String proxy, String port) { + apn.add(new Apn(mmsc, proxy, port)); + return this; } - public String getMmsc() { - return mmsc; - } - - public String getProxy() { - if (!hasProxy()) - return null; - - return proxy; - } - - public int getPort() { - if (Util.isEmpty(port)) - return 80; - - return Integer.parseInt(port); + public List get() { + return apn; } } diff --git a/src/org/thoughtcrime/securesms/mms/MmsDownloadHelper.java b/src/org/thoughtcrime/securesms/mms/MmsDownloadHelper.java index 22585f1afa..07ea1eeffe 100644 --- a/src/org/thoughtcrime/securesms/mms/MmsDownloadHelper.java +++ b/src/org/thoughtcrime/securesms/mms/MmsDownloadHelper.java @@ -34,7 +34,7 @@ import ws.com.google.android.mms.pdu.RetrieveConf; public class MmsDownloadHelper extends MmsCommunication { - private static byte[] makeRequest(Context context, MmsConnectionParameters connectionParameters, String url) + private static byte[] makeRequest(Context context, MmsConnectionParameters.Apn connectionParameters, String url) throws IOException { AndroidHttpClient client = null; @@ -78,13 +78,17 @@ public class MmsDownloadHelper extends MmsCommunication { throws IOException, ApnUnavailableException { MmsConnectionParameters connectionParameters = getMmsConnectionParameters(context, apn, proxyIfPossible); + byte[] pdu = null; - checkRouteToHost(context, connectionParameters, url, usingMmsRadio); - - byte[] pdu = makeRequest(context, connectionParameters, url); + for (MmsConnectionParameters.Apn param : connectionParameters.get()) { + if (checkRouteToHost(context, param, param.getMmsc(), usingMmsRadio)) { + pdu = makeRequest(context, param, url); + if (pdu != null) break; + } + } if (pdu == null) { - throw new IOException("Retrieved null PDU!"); + throw new IOException("Connection manager could not obtain route to host."); } RetrieveConf retrieved = (RetrieveConf)new PduParser(pdu).parse(); diff --git a/src/org/thoughtcrime/securesms/mms/MmsSendHelper.java b/src/org/thoughtcrime/securesms/mms/MmsSendHelper.java index 983471a940..ef059fa899 100644 --- a/src/org/thoughtcrime/securesms/mms/MmsSendHelper.java +++ b/src/org/thoughtcrime/securesms/mms/MmsSendHelper.java @@ -39,7 +39,7 @@ import ws.com.google.android.mms.pdu.SendConf; public class MmsSendHelper extends MmsCommunication { private final static String TAG = MmsSendHelper.class.getSimpleName(); - private static byte[] makePost(Context context, MmsConnectionParameters parameters, byte[] mms) + private static byte[] makePost(Context context, MmsConnectionParameters.Apn parameters, byte[] mms) throws IOException { AndroidHttpClient client = null; @@ -90,11 +90,6 @@ public class MmsSendHelper extends MmsCommunication { throws IOException { byte[] response = sendBytes(context, mms, apn, usingMmsRadio, useProxyIfAvailable); - - if (response == null) { - throw new IOException("Got null response!"); - } - return (SendConf) new PduParser(response).parse(); } @@ -105,8 +100,13 @@ public class MmsSendHelper extends MmsCommunication { Log.w(TAG, "Sending MMS of length: " + mms.length); try { MmsConnectionParameters parameters = getMmsConnectionParameters(context, apn, useProxyIfAvailable); - checkRouteToHost(context, parameters, parameters.getMmsc(), usingMmsRadio); - return makePost(context, parameters, mms); + for (MmsConnectionParameters.Apn param : parameters.get()) { + if (checkRouteToHost(context, param, param.getMmsc(), usingMmsRadio)) { + byte[] response = makePost(context, param, mms); + if (response != null) return response; + } + } + throw new IOException("Connection manager could not obtain route to host."); } catch (ApnUnavailableException aue) { Log.w(TAG, aue); throw new IOException("Failed to get MMSC information...");