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;
|
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.ClientProtocolException;
|
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
|
import org.apache.http.entity.ByteArrayEntity;
|
2013-03-05 01:43:04 +00:00
|
|
|
import org.thoughtcrime.securesms.service.MmscProcessor;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2013-02-21 02:10:33 +00:00
|
|
|
import ws.com.google.android.mms.pdu.PduParser;
|
|
|
|
import ws.com.google.android.mms.pdu.SendConf;
|
|
|
|
|
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
|
|
|
|
|
|
|
public class MmsSendHelper extends MmsCommunication {
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2013-02-18 06:32:17 +00:00
|
|
|
private static byte[] makePost(Context context, MmsConnectionParameters parameters, byte[] mms) throws ClientProtocolException, IOException {
|
|
|
|
AndroidHttpClient client = null;
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2013-02-18 06:32:17 +00:00
|
|
|
try {
|
2013-02-27 06:56:15 +00:00
|
|
|
Log.w("MmsSender", "Sending MMS1 of length: " + (mms != null ? mms.length : "null"));
|
2013-02-18 06:32:17 +00:00
|
|
|
client = constructHttpClient(context, parameters);
|
|
|
|
URI targetUrl = new URI(parameters.getMmsc());
|
|
|
|
HttpHost target = new HttpHost(targetUrl.getHost(), targetUrl.getPort(), HttpHost.DEFAULT_SCHEME_NAME);
|
|
|
|
HttpPost request = new HttpPost(parameters.getMmsc());
|
|
|
|
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");
|
2012-09-30 18:46:45 +00:00
|
|
|
|
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) {
|
|
|
|
Log.w("MmsSendHelper", use);
|
|
|
|
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-02-21 02:10:33 +00:00
|
|
|
public static SendConf sendMms(Context context, byte[] mms, String apn,
|
|
|
|
boolean usingMmsRadio, boolean useProxyIfAvailable)
|
|
|
|
throws IOException
|
|
|
|
{
|
2011-12-20 18:20:44 +00:00
|
|
|
Log.w("MmsSender", "Sending MMS of length: " + mms.length);
|
|
|
|
try {
|
2013-02-21 02:10:33 +00:00
|
|
|
MmsConnectionParameters parameters = getMmsConnectionParameters(context, apn, useProxyIfAvailable);
|
|
|
|
checkRouteToHost(context, parameters, parameters.getMmsc(), usingMmsRadio);
|
|
|
|
byte[] response = makePost(context, parameters, mms);
|
|
|
|
return (SendConf) new PduParser(response).parse();
|
2012-12-09 00:46:38 +00:00
|
|
|
} catch (ApnUnavailableException aue) {
|
|
|
|
Log.w("MmsSender", 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);
|
|
|
|
String apn = connectivityManager.getNetworkInfo(MmscProcessor.TYPE_MOBILE_MMS).getExtraInfo();
|
|
|
|
|
|
|
|
MmsCommunication.getMmsConnectionParameters(context, apn, true);
|
|
|
|
return true;
|
|
|
|
} catch (ApnUnavailableException e) {
|
|
|
|
Log.w("MmsSendHelper", e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|