Refactor MMS send/download to be synchronous.
1) Make the radio change a synchronous action with a timeout. 2) Move the send logic into an MmsTransport, in preparation for UniversalTransport composition. 3) Move the download logic into a synchronous receiver.pull/1/head
parent
53803630d4
commit
fd045f2354
@ -0,0 +1,143 @@
|
||||
package org.thoughtcrime.securesms.mms;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.os.PowerManager;
|
||||
import android.util.Log;
|
||||
|
||||
import org.thoughtcrime.securesms.util.Util;
|
||||
|
||||
public class MmsRadio {
|
||||
|
||||
private static MmsRadio instance;
|
||||
|
||||
public static synchronized MmsRadio getInstance(Context context) {
|
||||
if (instance == null)
|
||||
instance = new MmsRadio(context);
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
///
|
||||
|
||||
private static final String FEATURE_ENABLE_MMS = "enableMMS";
|
||||
private static final int APN_ALREADY_ACTIVE = 0;
|
||||
public static final int TYPE_MOBILE_MMS = 2;
|
||||
|
||||
private final Context context;
|
||||
|
||||
private ConnectivityManager connectivityManager;
|
||||
private ConnectivityListener connectivityListener;
|
||||
private PowerManager.WakeLock wakeLock;
|
||||
private int connectedCounter = 0;
|
||||
|
||||
private MmsRadio(Context context) {
|
||||
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
||||
this.context = context;
|
||||
this.connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
this.wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MMS Connection");
|
||||
this.wakeLock.setReferenceCounted(true);
|
||||
}
|
||||
|
||||
public String getApnInformation() {
|
||||
return connectivityManager.getNetworkInfo(TYPE_MOBILE_MMS).getExtraInfo();
|
||||
}
|
||||
|
||||
public synchronized void disconnect() {
|
||||
Log.w("MmsRadio", "MMS Radio Disconnect Called...");
|
||||
wakeLock.release();
|
||||
connectedCounter--;
|
||||
|
||||
Log.w("MmsRadio", "Reference count: " + connectedCounter);
|
||||
|
||||
if (connectedCounter == 0) {
|
||||
Log.w("MmsRadio", "Turning off MMS radio...");
|
||||
connectivityManager.stopUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, FEATURE_ENABLE_MMS);
|
||||
|
||||
if (connectivityListener != null) {
|
||||
Log.w("MmsRadio", "Unregistering receiver...");
|
||||
context.unregisterReceiver(connectivityListener);
|
||||
connectivityListener = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void connect() throws MmsRadioException {
|
||||
int status = connectivityManager.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE,
|
||||
FEATURE_ENABLE_MMS);
|
||||
|
||||
Log.w("MmsRadio", "startUsingNetworkFeature status: " + status);
|
||||
|
||||
if (status == APN_ALREADY_ACTIVE) {
|
||||
wakeLock.acquire();
|
||||
connectedCounter++;
|
||||
return;
|
||||
} else {
|
||||
wakeLock.acquire();
|
||||
connectedCounter++;
|
||||
|
||||
if (connectivityListener == null) {
|
||||
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
|
||||
connectivityListener = new ConnectivityListener();
|
||||
context.registerReceiver(connectivityListener, filter);
|
||||
}
|
||||
|
||||
Util.wait(this, 30000);
|
||||
|
||||
if (!isConnected()) {
|
||||
Log.w("MmsRadio", "Got back from connectivity wait, and not connected...");
|
||||
disconnect();
|
||||
throw new MmsRadioException("Unable to successfully enable MMS radio.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isConnected() {
|
||||
NetworkInfo info = connectivityManager.getNetworkInfo(TYPE_MOBILE_MMS);
|
||||
|
||||
if ((info == null) || (info.getType() != TYPE_MOBILE_MMS) || !info.isConnected())
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean isConnectivityPossible() {
|
||||
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(TYPE_MOBILE_MMS);
|
||||
|
||||
return networkInfo != null && networkInfo.isAvailable();
|
||||
}
|
||||
|
||||
private boolean isConnectivityFailure() {
|
||||
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(TYPE_MOBILE_MMS);
|
||||
|
||||
return networkInfo == null || networkInfo.getDetailedState() == NetworkInfo.DetailedState.FAILED;
|
||||
}
|
||||
|
||||
private synchronized void issueConnectivityChange() {
|
||||
if (isConnected()) {
|
||||
Log.w("MmsRadio", "Notifying connected...");
|
||||
notifyAll();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isConnected() && (isConnectivityFailure() || !isConnectivityPossible())) {
|
||||
Log.w("MmsRadio", "Notifying not connected...");
|
||||
notifyAll();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private class ConnectivityListener extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
Log.w("MmsRadio", "Got connectivity change...");
|
||||
issueConnectivityChange();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package org.thoughtcrime.securesms.mms;
|
||||
|
||||
public class MmsRadioException extends Throwable {
|
||||
public MmsRadioException(String s) {
|
||||
super(s);
|
||||
}
|
||||
}
|
@ -1,123 +0,0 @@
|
||||
/**
|
||||
* Copyright (C) 2011 Whisper Systems
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.service;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.os.PowerManager;
|
||||
import android.os.PowerManager.WakeLock;
|
||||
import android.util.Log;
|
||||
|
||||
public abstract class MmscProcessor {
|
||||
|
||||
private static final String FEATURE_ENABLE_MMS = "enableMMS";
|
||||
private static final int APN_ALREADY_ACTIVE = 0;
|
||||
public static final int TYPE_MOBILE_MMS = 2;
|
||||
|
||||
private ConnectivityManager connectivityManager;
|
||||
private ConnectivityListener connectivityListener;
|
||||
private WakeLock wakeLock;
|
||||
|
||||
protected final Context context;
|
||||
|
||||
public MmscProcessor(Context context) {
|
||||
this.context = context;
|
||||
PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
|
||||
this.connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
this.wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MMS Connection");
|
||||
this.wakeLock.setReferenceCounted(false);
|
||||
}
|
||||
|
||||
protected String getApnInformation() {
|
||||
return connectivityManager.getNetworkInfo(TYPE_MOBILE_MMS).getExtraInfo();
|
||||
}
|
||||
|
||||
protected boolean isConnected() {
|
||||
NetworkInfo info = connectivityManager.getNetworkInfo(TYPE_MOBILE_MMS);
|
||||
|
||||
Log.w("MmsService", "NetworkInfo: " + info);
|
||||
|
||||
if ((info == null) || (info.getType() != TYPE_MOBILE_MMS) || !info.isConnected())
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected abstract String getConnectivityAction();
|
||||
|
||||
protected void issueConnectivityRequest() {
|
||||
int status = connectivityManager.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, FEATURE_ENABLE_MMS);
|
||||
|
||||
Log.w("MmscProcessor", "startUsingNetworkFeature status: " + status);
|
||||
|
||||
if (status == APN_ALREADY_ACTIVE) {
|
||||
issueConnectivityChange();
|
||||
} else if (connectivityListener == null) {
|
||||
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
|
||||
connectivityListener = new ConnectivityListener();
|
||||
context.registerReceiver(connectivityListener, filter);
|
||||
|
||||
wakeLock.acquire();
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean isConnectivityFailure() {
|
||||
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(TYPE_MOBILE_MMS);
|
||||
|
||||
return networkInfo == null || networkInfo.getDetailedState() == NetworkInfo.DetailedState.FAILED;
|
||||
}
|
||||
|
||||
|
||||
protected boolean isConnectivityPossible() {
|
||||
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(TYPE_MOBILE_MMS);
|
||||
Log.w("MmsService", "Got network info: " + networkInfo);
|
||||
|
||||
return networkInfo != null && networkInfo.isAvailable();
|
||||
}
|
||||
|
||||
protected void finishConnectivity() {
|
||||
Log.w("MmsService", "Calling stop using network feature!");
|
||||
connectivityManager.stopUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, FEATURE_ENABLE_MMS);
|
||||
|
||||
if (connectivityListener != null) {
|
||||
context.unregisterReceiver(connectivityListener);
|
||||
connectivityListener = null;
|
||||
}
|
||||
|
||||
if (this.wakeLock.isHeld())
|
||||
this.wakeLock.release();
|
||||
}
|
||||
|
||||
private void issueConnectivityChange() {
|
||||
Intent intent = new Intent(context, SendReceiveService.class);
|
||||
intent.setAction(getConnectivityAction());
|
||||
context.startService(intent);
|
||||
}
|
||||
|
||||
private class ConnectivityListener extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
Log.w("MmsService", "Dispatching connectivity change...");
|
||||
issueConnectivityChange();
|
||||
Log.w("MmsService", "Dispatched...");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,157 @@
|
||||
package org.thoughtcrime.securesms.transport;
|
||||
|
||||
import android.content.Context;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.util.Log;
|
||||
import android.util.Pair;
|
||||
|
||||
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
||||
import org.thoughtcrime.securesms.crypto.SessionCipher;
|
||||
import org.thoughtcrime.securesms.database.MmsDatabase;
|
||||
import org.thoughtcrime.securesms.mms.MmsRadio;
|
||||
import org.thoughtcrime.securesms.mms.MmsRadioException;
|
||||
import org.thoughtcrime.securesms.mms.MmsSendHelper;
|
||||
import org.thoughtcrime.securesms.mms.TextTransport;
|
||||
import org.thoughtcrime.securesms.protocol.WirePrefix;
|
||||
import org.thoughtcrime.securesms.recipients.Recipient;
|
||||
import org.thoughtcrime.securesms.util.Hex;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
|
||||
import ws.com.google.android.mms.ContentType;
|
||||
import ws.com.google.android.mms.pdu.EncodedStringValue;
|
||||
import ws.com.google.android.mms.pdu.PduBody;
|
||||
import ws.com.google.android.mms.pdu.PduComposer;
|
||||
import ws.com.google.android.mms.pdu.PduHeaders;
|
||||
import ws.com.google.android.mms.pdu.PduPart;
|
||||
import ws.com.google.android.mms.pdu.SendConf;
|
||||
import ws.com.google.android.mms.pdu.SendReq;
|
||||
|
||||
public class MmsTransport {
|
||||
|
||||
private final Context context;
|
||||
private final MasterSecret masterSecret;
|
||||
private final MmsRadio radio;
|
||||
|
||||
public MmsTransport(Context context, MasterSecret masterSecret) {
|
||||
this.context = context;
|
||||
this.masterSecret = masterSecret;
|
||||
this.radio = MmsRadio.getInstance(context);
|
||||
}
|
||||
|
||||
public Pair<byte[], Integer> deliver(SendReq message) throws UndeliverableMessageException {
|
||||
try {
|
||||
if (isCdmaDevice()) {
|
||||
Log.w("MmsTransport", "Sending MMS directly without radio change...");
|
||||
try {
|
||||
return sendMms(message, false, false);
|
||||
} catch (IOException e) {
|
||||
Log.w("MmsTransport", e);
|
||||
}
|
||||
}
|
||||
|
||||
Log.w("MmsTransport", "Sending MMS with radio change...");
|
||||
radio.connect();
|
||||
|
||||
try {
|
||||
Pair<byte[], Integer> result = sendMms(message, true, false);
|
||||
radio.disconnect();
|
||||
return result;
|
||||
} catch (IOException e) {
|
||||
Log.w("MmsTransport", e);
|
||||
}
|
||||
|
||||
Log.w("MmsTransport", "Sending MMS with radio change and proxy...");
|
||||
|
||||
try {
|
||||
Pair<byte[], Integer> result = sendMms(message, true, true);
|
||||
radio.disconnect();
|
||||
return result;
|
||||
} catch (IOException ioe) {
|
||||
Log.w("MmsTransport", ioe);
|
||||
radio.disconnect();
|
||||
throw new UndeliverableMessageException(ioe);
|
||||
}
|
||||
|
||||
} catch (MmsRadioException mre) {
|
||||
Log.w("MmsTransport", mre);
|
||||
throw new UndeliverableMessageException(mre);
|
||||
}
|
||||
}
|
||||
|
||||
private Pair<byte[], Integer> sendMms(SendReq message, boolean usingMmsRadio, boolean useProxy)
|
||||
throws IOException, UndeliverableMessageException
|
||||
{
|
||||
String number = ((TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE)).getLine1Number();
|
||||
|
||||
if (MmsDatabase.Types.isSecureType(message.getDatabaseMessageBox())) {
|
||||
message = getEncryptedMessage(message);
|
||||
}
|
||||
|
||||
if (number != null && number.trim().length() != 0) {
|
||||
message.setFrom(new EncodedStringValue(number));
|
||||
}
|
||||
|
||||
SendConf conf = MmsSendHelper.sendMms(context, new PduComposer(context, message).make(),
|
||||
radio.getApnInformation(), usingMmsRadio, useProxy);
|
||||
|
||||
for (int i=0;i<message.getBody().getPartsNum();i++) {
|
||||
Log.w("MmsSender", "Sent MMS part of content-type: " + new String(message.getBody().getPart(i).getContentType()));
|
||||
}
|
||||
|
||||
if (conf == null) {
|
||||
throw new UndeliverableMessageException("No M-Send.conf received in response to send.");
|
||||
} else if (conf.getResponseStatus() != PduHeaders.RESPONSE_STATUS_OK) {
|
||||
throw new UndeliverableMessageException("Got bad response: " + conf.getResponseStatus());
|
||||
} else if (isInconsistentResponse(message, conf)) {
|
||||
throw new UndeliverableMessageException("Mismatched response!");
|
||||
} else {
|
||||
return new Pair<byte[], Integer>(conf.getMessageId(), conf.getResponseStatus());
|
||||
}
|
||||
}
|
||||
|
||||
private SendReq getEncryptedMessage(SendReq pdu) {
|
||||
EncodedStringValue[] encodedRecipient = pdu.getTo();
|
||||
String recipient = encodedRecipient[0].getString();
|
||||
byte[] pduBytes = new PduComposer(context, pdu).make();
|
||||
byte[] encryptedPduBytes = getEncryptedPdu(masterSecret, recipient, pduBytes);
|
||||
|
||||
PduBody body = new PduBody();
|
||||
PduPart part = new PduPart();
|
||||
SendReq encryptedPdu = new SendReq(pdu.getPduHeaders(), body);
|
||||
|
||||
part.setContentId((System.currentTimeMillis()+"").getBytes());
|
||||
part.setContentType(ContentType.TEXT_PLAIN.getBytes());
|
||||
part.setName((System.currentTimeMillis()+"").getBytes());
|
||||
part.setData(encryptedPduBytes);
|
||||
body.addPart(part);
|
||||
encryptedPdu.setSubject(new EncodedStringValue(WirePrefix.calculateEncryptedMmsSubject()));
|
||||
encryptedPdu.setBody(body);
|
||||
|
||||
return encryptedPdu;
|
||||
}
|
||||
|
||||
private byte[] getEncryptedPdu(MasterSecret masterSecret, String recipient, byte[] pduBytes) {
|
||||
synchronized (SessionCipher.CIPHER_LOCK) {
|
||||
SessionCipher cipher = new SessionCipher(context, masterSecret,
|
||||
new Recipient(null, recipient, null, null),
|
||||
new TextTransport());
|
||||
|
||||
return cipher.encryptMessage(pduBytes);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isInconsistentResponse(SendReq message, SendConf response) {
|
||||
Log.w("MmsTransport", "Comparing: " + Hex.toString(message.getTransactionId()));
|
||||
Log.w("MmsTransport", "With: " + Hex.toString(response.getTransactionId()));
|
||||
return !Arrays.equals(message.getTransactionId(), response.getTransactionId());
|
||||
}
|
||||
|
||||
private boolean isCdmaDevice() {
|
||||
return ((TelephonyManager)context
|
||||
.getSystemService(Context.TELEPHONY_SERVICE))
|
||||
.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue