2012-09-30 18:46:45 +00:00
|
|
|
/**
|
2011-12-20 18:20:44 +00:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2012-09-30 18:46:45 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2012-09-30 18:46:45 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package org.thoughtcrime.securesms.mms;
|
|
|
|
|
2012-09-30 18:46:45 +00:00
|
|
|
import android.content.Context;
|
2013-03-05 01:43:04 +00:00
|
|
|
import android.net.ConnectivityManager;
|
2014-04-28 21:01:47 +00:00
|
|
|
import android.net.NetworkInfo;
|
2014-07-29 22:31:20 +00:00
|
|
|
import android.net.Uri;
|
2013-02-18 06:32:17 +00:00
|
|
|
import android.net.http.AndroidHttpClient;
|
2012-09-30 18:46:45 +00:00
|
|
|
import android.util.Log;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2013-02-18 06:32:17 +00:00
|
|
|
import org.apache.http.HttpHost;
|
2011-12-20 18:20:44 +00:00
|
|
|
import org.apache.http.HttpResponse;
|
|
|
|
import org.apache.http.StatusLine;
|
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
|
import org.apache.http.entity.ByteArrayEntity;
|
2013-07-10 02:48:33 +00:00
|
|
|
import org.whispersystems.textsecure.util.Util;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2012-09-30 18:46:45 +00:00
|
|
|
import java.io.IOException;
|
2013-02-18 06:32:17 +00:00
|
|
|
import java.net.URI;
|
|
|
|
import java.net.URISyntaxException;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2013-05-25 20:17:18 +00:00
|
|
|
import ws.com.google.android.mms.pdu.PduParser;
|
|
|
|
import ws.com.google.android.mms.pdu.SendConf;
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public class MmsSendHelper extends MmsCommunication {
|
2014-04-28 21:01:47 +00:00
|
|
|
private final static String TAG = MmsSendHelper.class.getSimpleName();
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2014-07-29 22:31:20 +00:00
|
|
|
private static byte[] makePost(Context context, String url, String proxy, int proxyPort, byte[] mms)
|
2013-07-17 02:52:02 +00:00
|
|
|
throws IOException
|
|
|
|
{
|
2013-02-18 06:32:17 +00:00
|
|
|
AndroidHttpClient client = null;
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2013-02-18 06:32:17 +00:00
|
|
|
try {
|
2014-04-28 21:01:47 +00:00
|
|
|
Log.w(TAG, "Sending MMS1 of length: " + (mms != null ? mms.length : "null"));
|
2014-07-29 22:31:20 +00:00
|
|
|
client = constructHttpClient(context, proxy, proxyPort);
|
|
|
|
URI targetUrl = new URI(url);
|
2013-05-07 02:45:05 +00:00
|
|
|
|
|
|
|
if (Util.isEmpty(targetUrl.getHost()))
|
|
|
|
throw new IOException("Invalid target host: " + targetUrl.getHost() + " , " + targetUrl);
|
|
|
|
|
2013-02-18 06:32:17 +00:00
|
|
|
HttpHost target = new HttpHost(targetUrl.getHost(), targetUrl.getPort(), HttpHost.DEFAULT_SCHEME_NAME);
|
2014-07-29 22:31:20 +00:00
|
|
|
HttpPost request = new HttpPost(url);
|
2013-02-18 06:32:17 +00:00
|
|
|
ByteArrayEntity entity = new ByteArrayEntity(mms);
|
|
|
|
|
|
|
|
entity.setContentType("application/vnd.wap.mms-message");
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2013-02-18 06:32:17 +00:00
|
|
|
request.setEntity(entity);
|
|
|
|
request.setParams(client.getParams());
|
|
|
|
request.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");
|
2013-03-14 15:44:42 +00:00
|
|
|
request.addHeader("x-wap-profile", "http://www.google.com/oha/rdf/ua-profile-kila.xml");
|
2013-02-18 06:32:17 +00:00
|
|
|
HttpResponse response = client.execute(target, request);
|
|
|
|
StatusLine status = response.getStatusLine();
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2013-02-18 06:32:17 +00:00
|
|
|
if (status.getStatusCode() != 200)
|
|
|
|
throw new IOException("Non-successful HTTP response: " + status.getReasonPhrase());
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2013-02-18 06:32:17 +00:00
|
|
|
return parseResponse(response.getEntity());
|
|
|
|
} catch (URISyntaxException use) {
|
2014-04-28 21:01:47 +00:00
|
|
|
Log.w(TAG, use);
|
2013-02-18 06:32:17 +00:00
|
|
|
throw new IOException("Couldn't parse URI.");
|
|
|
|
} finally {
|
|
|
|
if (client != null)
|
|
|
|
client.close();
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2013-05-07 02:09:06 +00:00
|
|
|
public static void sendNotificationReceived(Context context, byte[] mms, String apn,
|
|
|
|
boolean usingMmsRadio, boolean useProxyIfAvailable)
|
|
|
|
throws IOException
|
|
|
|
{
|
|
|
|
sendBytes(context, mms, apn, usingMmsRadio, useProxyIfAvailable);
|
|
|
|
}
|
|
|
|
|
2013-02-21 02:10:33 +00:00
|
|
|
public static SendConf sendMms(Context context, byte[] mms, String apn,
|
|
|
|
boolean usingMmsRadio, boolean useProxyIfAvailable)
|
2013-05-07 02:09:06 +00:00
|
|
|
throws IOException
|
|
|
|
{
|
|
|
|
byte[] response = sendBytes(context, mms, apn, usingMmsRadio, useProxyIfAvailable);
|
|
|
|
return (SendConf) new PduParser(response).parse();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static byte[] sendBytes(Context context, byte[] mms, String apn,
|
|
|
|
boolean usingMmsRadio, boolean useProxyIfAvailable)
|
2013-02-21 02:10:33 +00:00
|
|
|
throws IOException
|
|
|
|
{
|
2014-04-28 21:01:47 +00:00
|
|
|
Log.w(TAG, "Sending MMS of length: " + mms.length);
|
2011-12-20 18:20:44 +00:00
|
|
|
try {
|
2014-07-29 22:31:20 +00:00
|
|
|
MmsConnectionParameters parameters = getMmsConnectionParameters(context, apn);
|
|
|
|
|
2014-08-12 23:07:09 +00:00
|
|
|
for (MmsConnectionParameters.Apn param : parameters.get()) {
|
|
|
|
try {
|
|
|
|
if (useProxyIfAvailable && param.hasProxy()) {
|
|
|
|
if (checkRouteToHost(context, param.getProxy(), usingMmsRadio)) {
|
|
|
|
byte[] response = makePost(context, param.getMmsc(), param.getProxy(), param.getPort(), mms);
|
|
|
|
if (response != null) return response;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (checkRouteToHost(context, Uri.parse(param.getMmsc()).getHost(), usingMmsRadio)) {
|
|
|
|
byte[] response = makePost(context, param.getMmsc(), null, -1, mms);
|
|
|
|
if (response != null) return response;
|
|
|
|
}
|
2014-07-29 22:31:20 +00:00
|
|
|
}
|
2014-08-12 23:07:09 +00:00
|
|
|
} catch (IOException ioe) {
|
|
|
|
Log.w(TAG, ioe);
|
2014-01-14 12:02:52 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-12 23:07:09 +00:00
|
|
|
|
2014-01-14 12:02:52 +00:00
|
|
|
throw new IOException("Connection manager could not obtain route to host.");
|
2012-12-09 00:46:38 +00:00
|
|
|
} catch (ApnUnavailableException aue) {
|
2014-04-28 21:01:47 +00:00
|
|
|
Log.w(TAG, aue);
|
2011-12-20 18:20:44 +00:00
|
|
|
throw new IOException("Failed to get MMSC information...");
|
|
|
|
}
|
|
|
|
}
|
2013-03-05 01:43:04 +00:00
|
|
|
|
|
|
|
public static boolean hasNecessaryApnDetails(Context context) {
|
|
|
|
try {
|
|
|
|
ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
2014-04-28 21:01:47 +00:00
|
|
|
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(MmsRadio.TYPE_MOBILE_MMS);
|
|
|
|
if (networkInfo == null) {
|
|
|
|
Log.w(TAG, "MMS network info was null, unsupported by this device");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
String apn = networkInfo.getExtraInfo();
|
2013-03-05 01:43:04 +00:00
|
|
|
|
2014-07-29 22:31:20 +00:00
|
|
|
MmsCommunication.getMmsConnectionParameters(context, apn);
|
2013-03-05 01:43:04 +00:00
|
|
|
return true;
|
|
|
|
} catch (ApnUnavailableException e) {
|
|
|
|
Log.w("MmsSendHelper", e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|