2012-09-30 18:46:45 +00:00
|
|
|
/**
|
2013-07-17 02:52:02 +00:00
|
|
|
* Copyright (C) 2013 Open 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.service;
|
|
|
|
|
2012-09-30 18:46:45 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2014-07-29 22:31:20 +00:00
|
|
|
import android.net.Uri;
|
2013-03-04 02:44:58 +00:00
|
|
|
import android.telephony.TelephonyManager;
|
2012-09-30 18:46:45 +00:00
|
|
|
import android.util.Log;
|
2013-04-26 18:23:43 +00:00
|
|
|
import android.util.Pair;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2013-02-21 02:10:33 +00:00
|
|
|
import org.thoughtcrime.securesms.R;
|
2011-12-20 18:20:44 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.DecryptingQueue;
|
2014-09-16 23:21:41 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.IncomingMmsConnection;
|
|
|
|
import org.thoughtcrime.securesms.mms.MmsConnection;
|
|
|
|
import org.thoughtcrime.securesms.mms.MmsConnection.Apn;
|
|
|
|
import org.thoughtcrime.securesms.mms.OutgoingMmsConnection;
|
2013-08-18 01:37:18 +00:00
|
|
|
import org.whispersystems.textsecure.crypto.MasterSecret;
|
2011-12-20 18:20:44 +00:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.MmsDatabase;
|
2013-09-16 07:55:01 +00:00
|
|
|
import org.thoughtcrime.securesms.database.model.NotificationMmsMessageRecord;
|
|
|
|
import org.thoughtcrime.securesms.mms.ApnUnavailableException;
|
2013-07-19 00:42:45 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.IncomingMediaMessage;
|
2013-07-17 02:52:02 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.MmsRadio;
|
|
|
|
import org.thoughtcrime.securesms.mms.MmsRadioException;
|
2013-04-26 18:23:43 +00:00
|
|
|
import org.thoughtcrime.securesms.notifications.MessageNotifier;
|
2011-12-20 18:20:44 +00:00
|
|
|
import org.thoughtcrime.securesms.protocol.WirePrefix;
|
|
|
|
|
2013-09-16 07:55:01 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2013-05-07 02:09:06 +00:00
|
|
|
import ws.com.google.android.mms.InvalidHeaderValueException;
|
2011-12-20 18:20:44 +00:00
|
|
|
import ws.com.google.android.mms.MmsException;
|
2013-05-07 02:09:06 +00:00
|
|
|
import ws.com.google.android.mms.pdu.NotifyRespInd;
|
|
|
|
import ws.com.google.android.mms.pdu.PduComposer;
|
|
|
|
import ws.com.google.android.mms.pdu.PduHeaders;
|
2011-12-20 18:20:44 +00:00
|
|
|
import ws.com.google.android.mms.pdu.RetrieveConf;
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2013-07-17 02:52:02 +00:00
|
|
|
public class MmsDownloader {
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2013-07-17 02:52:02 +00:00
|
|
|
private final Context context;
|
2011-12-20 18:20:44 +00:00
|
|
|
private final SendReceiveService.ToastHandler toastHandler;
|
2013-07-17 02:52:02 +00:00
|
|
|
private final MmsRadio radio;
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public MmsDownloader(Context context, SendReceiveService.ToastHandler toastHandler) {
|
2013-07-17 02:52:02 +00:00
|
|
|
this.context = context;
|
2011-12-20 18:20:44 +00:00
|
|
|
this.toastHandler = toastHandler;
|
2013-07-17 02:52:02 +00:00
|
|
|
this.radio = MmsRadio.getInstance(context);
|
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 void process(MasterSecret masterSecret, Intent intent) {
|
2014-03-07 21:05:35 +00:00
|
|
|
if (SendReceiveService.DOWNLOAD_MMS_ACTION.equals(intent.getAction())) {
|
2013-07-17 02:52:02 +00:00
|
|
|
handleDownloadMms(masterSecret, intent);
|
2014-03-07 21:05:35 +00:00
|
|
|
} else if (SendReceiveService.DOWNLOAD_MMS_PENDING_APN_ACTION.equals(intent.getAction())) {
|
2013-09-16 07:55:01 +00:00
|
|
|
handleMmsPendingApnDownloads(masterSecret);
|
2013-02-21 02:10:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-16 07:55:01 +00:00
|
|
|
private void handleMmsPendingApnDownloads(MasterSecret masterSecret) {
|
2014-09-16 23:21:41 +00:00
|
|
|
if (!IncomingMmsConnection.isConnectionPossible(context, null))
|
2013-09-16 07:55:01 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
MmsDatabase mmsDatabase = DatabaseFactory.getMmsDatabase(context);
|
|
|
|
MmsDatabase.Reader stalledMmsReader = mmsDatabase.getNotificationsWithDownloadState(masterSecret,
|
|
|
|
MmsDatabase.Status.DOWNLOAD_APN_UNAVAILABLE);
|
|
|
|
while (stalledMmsReader.getNext() != null) {
|
|
|
|
NotificationMmsMessageRecord stalledMmsRecord = (NotificationMmsMessageRecord) stalledMmsReader.getCurrent();
|
|
|
|
|
|
|
|
Intent intent = new Intent(SendReceiveService.DOWNLOAD_MMS_ACTION, null, context, SendReceiveService.class);
|
|
|
|
intent.putExtra("content_location", new String(stalledMmsRecord.getContentLocation()));
|
|
|
|
intent.putExtra("message_id", stalledMmsRecord.getId());
|
|
|
|
intent.putExtra("transaction_id", stalledMmsRecord.getTransactionId());
|
|
|
|
intent.putExtra("thread_id", stalledMmsRecord.getThreadId());
|
|
|
|
intent.putExtra("automatic", true);
|
|
|
|
context.startService(intent);
|
|
|
|
}
|
|
|
|
|
|
|
|
stalledMmsReader.close();
|
|
|
|
}
|
|
|
|
|
2013-07-17 02:52:02 +00:00
|
|
|
private void handleDownloadMms(MasterSecret masterSecret, Intent intent) {
|
|
|
|
long messageId = intent.getLongExtra("message_id", -1);
|
|
|
|
long threadId = intent.getLongExtra("thread_id", -1);
|
|
|
|
byte[] transactionId = intent.getByteArrayExtra("transaction_id");
|
|
|
|
String contentLocation = intent.getStringExtra("content_location");
|
|
|
|
boolean automatic = intent.getBooleanExtra("automatic", false);
|
|
|
|
MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2013-07-17 02:52:02 +00:00
|
|
|
database.markDownloadState(messageId, MmsDatabase.Status.DOWNLOAD_CONNECTING);
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2014-07-29 22:31:20 +00:00
|
|
|
Log.w("MmsDownloader", "Downloading mms at "+ Uri.parse(contentLocation).getHost());
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
try {
|
2013-07-17 02:52:02 +00:00
|
|
|
if (isCdmaNetwork()) {
|
|
|
|
Log.w("MmsDownloader", "Connecting directly...");
|
|
|
|
try {
|
|
|
|
retrieveAndStore(masterSecret, messageId, threadId, contentLocation,
|
|
|
|
transactionId, false, false);
|
|
|
|
return;
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w("MmsDownloader", e);
|
|
|
|
}
|
2013-01-01 20:54:12 +00:00
|
|
|
}
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2013-07-17 02:52:02 +00:00
|
|
|
Log.w("MmsDownloader", "Changing radio to MMS mode..");
|
|
|
|
radio.connect();
|
|
|
|
|
2014-07-29 22:31:20 +00:00
|
|
|
Log.w("MmsDownloader", "Downloading in MMS mode with proxy...");
|
2013-07-17 02:52:02 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
retrieveAndStore(masterSecret, messageId, threadId, contentLocation,
|
2014-07-29 22:31:20 +00:00
|
|
|
transactionId, true, true);
|
2013-07-17 02:52:02 +00:00
|
|
|
radio.disconnect();
|
|
|
|
return;
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w("MmsDownloader", e);
|
|
|
|
}
|
|
|
|
|
2014-07-29 22:31:20 +00:00
|
|
|
Log.w("MmsDownloader", "Downloading in MMS mode without proxy...");
|
2013-07-17 02:52:02 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
retrieveAndStore(masterSecret, messageId, threadId,
|
2014-07-29 22:31:20 +00:00
|
|
|
contentLocation, transactionId, true, false);
|
2013-07-17 02:52:02 +00:00
|
|
|
radio.disconnect();
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w("MmsDownloader", e);
|
|
|
|
radio.disconnect();
|
|
|
|
handleDownloadError(masterSecret, messageId, threadId,
|
|
|
|
MmsDatabase.Status.DOWNLOAD_SOFT_FAILURE,
|
|
|
|
context.getString(R.string.MmsDownloader_error_connecting_to_mms_provider),
|
|
|
|
automatic);
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2013-09-16 07:55:01 +00:00
|
|
|
} catch (ApnUnavailableException e) {
|
|
|
|
Log.w("MmsDownloader", e);
|
2013-07-17 02:52:02 +00:00
|
|
|
handleDownloadError(masterSecret, messageId, threadId, MmsDatabase.Status.DOWNLOAD_APN_UNAVAILABLE,
|
|
|
|
context.getString(R.string.MmsDownloader_error_reading_mms_settings), automatic);
|
2011-12-20 18:20:44 +00:00
|
|
|
} catch (MmsException e) {
|
|
|
|
Log.w("MmsDownloader", e);
|
2013-07-17 02:52:02 +00:00
|
|
|
handleDownloadError(masterSecret, messageId, threadId,
|
|
|
|
MmsDatabase.Status.DOWNLOAD_HARD_FAILURE,
|
|
|
|
context.getString(R.string.MmsDownloader_error_storing_mms),
|
|
|
|
automatic);
|
|
|
|
} catch (MmsRadioException e) {
|
|
|
|
Log.w("MmsDownloader", e);
|
|
|
|
handleDownloadError(masterSecret, messageId, threadId,
|
|
|
|
MmsDatabase.Status.DOWNLOAD_SOFT_FAILURE,
|
|
|
|
context.getString(R.string.MmsDownloader_error_connecting_to_mms_provider),
|
|
|
|
automatic);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2013-07-17 02:52:02 +00:00
|
|
|
private void retrieveAndStore(MasterSecret masterSecret, long messageId, long threadId,
|
|
|
|
String contentLocation, byte[] transactionId,
|
|
|
|
boolean radioEnabled, boolean useProxy)
|
2013-11-18 23:29:19 +00:00
|
|
|
throws IOException, MmsException, ApnUnavailableException
|
2013-07-17 02:52:02 +00:00
|
|
|
{
|
2014-09-16 23:21:41 +00:00
|
|
|
Apn dbApn = MmsConnection.getApn(context, radio.getApnInformation());
|
|
|
|
Apn contentApn = new Apn(contentLocation, dbApn.getProxy(), Integer.toString(dbApn.getPort()));
|
|
|
|
IncomingMmsConnection connection = new IncomingMmsConnection(context, contentApn);
|
|
|
|
RetrieveConf retrieved = connection.retrieve(radioEnabled, useProxy);
|
2013-07-17 02:52:02 +00:00
|
|
|
|
|
|
|
storeRetrievedMms(masterSecret, contentLocation, messageId, threadId, retrieved);
|
|
|
|
sendRetrievedAcknowledgement(transactionId, radioEnabled, useProxy);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void storeRetrievedMms(MasterSecret masterSecret, String contentLocation,
|
|
|
|
long messageId, long threadId, RetrieveConf retrieved)
|
2013-02-21 02:10:33 +00:00
|
|
|
throws MmsException
|
|
|
|
{
|
2013-07-19 00:42:45 +00:00
|
|
|
MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
|
|
|
|
IncomingMediaMessage message = new IncomingMediaMessage(retrieved);
|
|
|
|
|
2013-04-26 18:23:43 +00:00
|
|
|
Pair<Long, Long> messageAndThreadId;
|
|
|
|
|
2013-02-21 02:10:33 +00:00
|
|
|
if (retrieved.getSubject() != null && WirePrefix.isEncryptedMmsSubject(retrieved.getSubject().getString())) {
|
2013-07-19 00:42:45 +00:00
|
|
|
messageAndThreadId = database.insertSecureMessageInbox(masterSecret, message,
|
2013-07-17 02:52:02 +00:00
|
|
|
contentLocation, threadId);
|
2013-02-21 02:10:33 +00:00
|
|
|
|
2013-07-17 02:52:02 +00:00
|
|
|
if (masterSecret != null)
|
|
|
|
DecryptingQueue.scheduleDecryption(context, masterSecret, messageAndThreadId.first,
|
2013-04-26 18:23:43 +00:00
|
|
|
messageAndThreadId.second, retrieved);
|
2013-02-21 02:10:33 +00:00
|
|
|
|
|
|
|
} else {
|
2013-07-19 00:42:45 +00:00
|
|
|
messageAndThreadId = database.insertMessageInbox(masterSecret, message,
|
2013-07-17 02:52:02 +00:00
|
|
|
contentLocation, threadId);
|
2013-02-21 02:10:33 +00:00
|
|
|
}
|
|
|
|
|
2013-07-17 02:52:02 +00:00
|
|
|
database.delete(messageId);
|
|
|
|
MessageNotifier.updateNotification(context, masterSecret, messageAndThreadId.second);
|
2013-02-21 02:10:33 +00:00
|
|
|
}
|
|
|
|
|
2013-07-17 02:52:02 +00:00
|
|
|
private void sendRetrievedAcknowledgement(byte[] transactionId,
|
|
|
|
boolean usingRadio,
|
|
|
|
boolean useProxy)
|
2014-09-16 23:21:41 +00:00
|
|
|
throws ApnUnavailableException
|
2013-07-17 02:52:02 +00:00
|
|
|
{
|
2013-05-07 02:09:06 +00:00
|
|
|
try {
|
|
|
|
NotifyRespInd notifyResponse = new NotifyRespInd(PduHeaders.CURRENT_MMS_VERSION,
|
2013-07-17 02:52:02 +00:00
|
|
|
transactionId,
|
2013-05-07 02:09:06 +00:00
|
|
|
PduHeaders.STATUS_RETRIEVED);
|
|
|
|
|
2014-09-16 23:21:41 +00:00
|
|
|
OutgoingMmsConnection connection = new OutgoingMmsConnection(context, radio.getApnInformation(), new PduComposer(context, notifyResponse).make());
|
|
|
|
connection.sendNotificationReceived(usingRadio, useProxy);
|
2013-05-07 02:09:06 +00:00
|
|
|
} catch (InvalidHeaderValueException e) {
|
|
|
|
Log.w("MmsDownloader", e);
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w("MmsDownloader", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2013-07-17 02:52:02 +00:00
|
|
|
private void handleDownloadError(MasterSecret masterSecret, long messageId, long threadId,
|
|
|
|
int downloadStatus, String error, boolean automatic)
|
|
|
|
{
|
2013-04-26 18:23:43 +00:00
|
|
|
MmsDatabase db = DatabaseFactory.getMmsDatabase(context);
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2013-07-17 02:52:02 +00:00
|
|
|
db.markDownloadState(messageId, downloadStatus);
|
2013-02-21 02:10:33 +00:00
|
|
|
|
2013-07-17 02:52:02 +00:00
|
|
|
if (automatic) {
|
|
|
|
db.markIncomingNotificationReceived(threadId);
|
|
|
|
MessageNotifier.updateNotification(context, masterSecret, threadId);
|
2013-02-21 02:10:33 +00:00
|
|
|
}
|
|
|
|
|
2013-07-17 02:52:02 +00:00
|
|
|
toastHandler.makeToast(error);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
|
2013-07-17 02:52:02 +00:00
|
|
|
private boolean isCdmaNetwork() {
|
|
|
|
return ((TelephonyManager)context
|
|
|
|
.getSystemService(Context.TELEPHONY_SERVICE))
|
|
|
|
.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA;
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
}
|