2012-09-30 11:46:45 -07:00
|
|
|
/**
|
2011-12-20 10:20:44 -08:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2012-09-30 11:46:45 -07:00
|
|
|
*
|
2011-12-20 10:20:44 -08: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 11:46:45 -07:00
|
|
|
*
|
2011-12-20 10:20:44 -08: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 11:46:45 -07:00
|
|
|
import android.content.Context;
|
2013-02-17 22:32:17 -08:00
|
|
|
import android.net.http.AndroidHttpClient;
|
2012-09-30 11:46:45 -07:00
|
|
|
import android.util.Log;
|
2011-12-20 10:20:44 -08:00
|
|
|
|
2013-02-17 22:32:17 -08:00
|
|
|
import org.apache.http.HttpHost;
|
2011-12-20 10:20:44 -08:00
|
|
|
import org.apache.http.HttpResponse;
|
|
|
|
import org.apache.http.StatusLine;
|
|
|
|
import org.apache.http.client.ClientProtocolException;
|
|
|
|
import org.apache.http.client.methods.HttpGet;
|
|
|
|
|
2012-09-30 11:46:45 -07:00
|
|
|
import java.io.IOException;
|
2013-02-17 22:32:17 -08:00
|
|
|
import java.net.URI;
|
|
|
|
import java.net.URISyntaxException;
|
2011-12-20 10:20:44 -08:00
|
|
|
|
|
|
|
public class MmsDownloadHelper extends MmsCommunication {
|
2012-09-30 11:46:45 -07:00
|
|
|
|
2013-02-17 22:32:17 -08:00
|
|
|
private static byte[] makeRequest(Context context, MmsConnectionParameters connectionParameters, String url)
|
2012-09-30 11:46:45 -07:00
|
|
|
throws ClientProtocolException, IOException
|
|
|
|
{
|
2013-02-17 22:32:17 -08:00
|
|
|
AndroidHttpClient client = null;
|
2012-09-30 11:46:45 -07:00
|
|
|
|
2013-02-17 22:32:17 -08:00
|
|
|
try {
|
|
|
|
client = constructHttpClient(context, connectionParameters);
|
|
|
|
URI targetUrl = new URI(url.trim());
|
|
|
|
HttpHost target = new HttpHost(targetUrl.getHost(), targetUrl.getPort(), HttpHost.DEFAULT_SCHEME_NAME);
|
|
|
|
HttpGet request = new HttpGet(url.trim());
|
|
|
|
|
|
|
|
request.setParams(client.getParams());
|
|
|
|
request.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");
|
2012-09-30 11:46:45 -07:00
|
|
|
|
2013-02-17 22:32:17 -08:00
|
|
|
// java.util.logging.Logger.getLogger("org.apache.http.wire").setLevel(java.util.logging.Level.FINEST);
|
|
|
|
// java.util.logging.Logger.getLogger("org.apache.http.headers").setLevel(java.util.logging.Level.FINEST);
|
|
|
|
//
|
|
|
|
// System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
|
|
|
|
// System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
|
|
|
|
// System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", "debug");
|
|
|
|
// System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http", "debug");
|
|
|
|
// System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.headers", "debug");
|
2012-09-30 11:46:45 -07:00
|
|
|
|
2013-02-17 22:32:17 -08:00
|
|
|
HttpResponse response = client.execute(target, request);
|
|
|
|
StatusLine status = response.getStatusLine();
|
2012-09-30 11:46:45 -07:00
|
|
|
|
2013-02-17 22:32:17 -08:00
|
|
|
if (status.getStatusCode() != 200)
|
|
|
|
throw new IOException("Non-successful HTTP response: " + status.getReasonPhrase());
|
|
|
|
|
|
|
|
return parseResponse(response.getEntity());
|
|
|
|
} catch (URISyntaxException use) {
|
|
|
|
Log.w("MmsDownloadHelper", use);
|
|
|
|
throw new IOException("Couldn't parse URI");
|
|
|
|
} finally {
|
|
|
|
if (client != null)
|
|
|
|
client.close();
|
|
|
|
}
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
2012-09-30 11:46:45 -07:00
|
|
|
|
|
|
|
public static byte[] retrieveMms(Context context, String url, String apn) throws IOException {
|
2012-11-18 14:51:20 -08:00
|
|
|
MmsConnectionParameters connectionParameters;
|
2012-09-30 11:46:45 -07:00
|
|
|
|
2012-11-18 14:51:20 -08:00
|
|
|
try {
|
|
|
|
connectionParameters = getMmsConnectionParameters(context, apn);
|
2012-12-08 16:46:38 -08:00
|
|
|
} catch (ApnUnavailableException aue) {
|
|
|
|
Log.w("MmsDownloadHelper", aue);
|
2012-11-18 14:51:20 -08:00
|
|
|
connectionParameters = new MmsConnectionParameters(null, null, null);
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
2012-11-18 14:51:20 -08:00
|
|
|
|
|
|
|
checkRouteToHost(context, connectionParameters, url);
|
2013-02-17 22:32:17 -08:00
|
|
|
return makeRequest(context, connectionParameters, url);
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
|
|
|
}
|