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.service;
|
|
|
|
|
2012-09-30 18:46:45 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.util.Log;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.crypto.DecryptingQueue;
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.MmsDatabase;
|
|
|
|
import org.thoughtcrime.securesms.mms.MmsDownloadHelper;
|
|
|
|
import org.thoughtcrime.securesms.protocol.WirePrefix;
|
|
|
|
|
|
|
|
import ws.com.google.android.mms.MmsException;
|
|
|
|
import ws.com.google.android.mms.pdu.PduParser;
|
|
|
|
import ws.com.google.android.mms.pdu.RetrieveConf;
|
2012-09-30 18:46:45 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.LinkedList;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
|
|
|
public class MmsDownloader extends MmscProcessor {
|
|
|
|
|
|
|
|
private final LinkedList<DownloadItem> pendingMessages = new LinkedList<DownloadItem>();
|
|
|
|
private final SendReceiveService.ToastHandler toastHandler;
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public MmsDownloader(Context context, SendReceiveService.ToastHandler toastHandler) {
|
|
|
|
super(context);
|
|
|
|
this.toastHandler = toastHandler;
|
|
|
|
}
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private void handleDownloadMms(DownloadItem item) {
|
|
|
|
if (!isConnectivityPossible()) {
|
|
|
|
DatabaseFactory.getMmsDatabase(context).markDownloadState(item.getMessageId(), MmsDatabase.Types.DOWNLOAD_NO_CONNECTIVITY);
|
|
|
|
toastHandler.makeToast("No connectivity available for MMS download, try again later...");
|
|
|
|
Log.w("MmsDownloadService", "Unable to download MMS, please try again later.");
|
|
|
|
} else {
|
|
|
|
DatabaseFactory.getMmsDatabase(context).markDownloadState(item.getMessageId(), MmsDatabase.Types.DOWNLOAD_CONNECTING);
|
|
|
|
pendingMessages.add(item);
|
|
|
|
issueConnectivityRequest();
|
|
|
|
}
|
|
|
|
}
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private void handleDownloadMmsContinued(DownloadItem item) {
|
|
|
|
Log.w("MmsDownloadService", "Handling MMS download continuation...");
|
|
|
|
MmsDatabase mmsDatabase;
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
if (item.getMasterSecret() == null)
|
|
|
|
mmsDatabase = DatabaseFactory.getMmsDatabase(context);
|
|
|
|
else
|
|
|
|
mmsDatabase = DatabaseFactory.getEncryptingMmsDatabase(context, item.getMasterSecret());
|
2012-09-30 18:46:45 +00:00
|
|
|
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
try {
|
2012-09-30 18:46:45 +00:00
|
|
|
byte[] pdu = MmsDownloadHelper.retrieveMms(context, item.getContentLocation(),
|
|
|
|
getApnInformation());
|
2011-12-20 18:20:44 +00:00
|
|
|
RetrieveConf retrieved = (RetrieveConf)new PduParser(pdu).parse();
|
|
|
|
|
|
|
|
if (retrieved == null)
|
2013-01-01 20:54:12 +00:00
|
|
|
throw new IOException("Bad retrieved PDU");
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2013-01-01 20:54:12 +00:00
|
|
|
for (int i=0;i<retrieved.getBody().getPartsNum();i++) {
|
|
|
|
Log.w("MmsDownloader", "Sent MMS part of content-type: " +
|
|
|
|
new String(retrieved.getBody().getPart(i).getContentType()));
|
|
|
|
}
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
if (retrieved.getSubject() != null && WirePrefix.isEncryptedMmsSubject(retrieved.getSubject().getString())) {
|
2013-01-01 20:54:12 +00:00
|
|
|
long messageId = mmsDatabase.insertSecureMessageReceived(retrieved, item.getContentLocation(), item.getThreadId());
|
|
|
|
if (item.getMasterSecret() != null)
|
|
|
|
DecryptingQueue.scheduleDecryption(context, item.getMasterSecret(), messageId, item.getThreadId(), retrieved);
|
|
|
|
} else {
|
|
|
|
mmsDatabase.insertMessageReceived(retrieved, item.getContentLocation(), item.getThreadId());
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
mmsDatabase.delete(item.getMessageId());
|
|
|
|
|
|
|
|
// NotifyRespInd notifyResponse = new NotifyRespInd(PduHeaders.CURRENT_MMS_VERSION, item.getTransactionId(), PduHeaders.STATUS_RETRIEVED);
|
|
|
|
// MmsSendHelper.sendMms(context, new PduComposer(context, notifyResponse).make());
|
|
|
|
|
|
|
|
if (this.pendingMessages.isEmpty())
|
2013-01-01 20:54:12 +00:00
|
|
|
finishConnectivity();
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
DatabaseFactory.getMmsDatabase(context).markDownloadState(item.getMessageId(), MmsDatabase.Types.DOWNLOAD_SOFT_FAILURE);
|
|
|
|
toastHandler.makeToast("Error connecting to MMS provider...");
|
|
|
|
Log.w("MmsDownloader", e);
|
|
|
|
} catch (MmsException e) {
|
|
|
|
DatabaseFactory.getMmsDatabase(context).markDownloadState(item.getMessageId(), MmsDatabase.Types.DOWNLOAD_HARD_FAILURE);
|
|
|
|
toastHandler.makeToast("Error downloading MMS!");
|
|
|
|
Log.w("MmsDownloader", e);
|
|
|
|
}
|
|
|
|
}
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
protected void handleConnectivityChange() {
|
|
|
|
if (!isConnected()) {
|
|
|
|
if (!isConnectivityPossible() && !pendingMessages.isEmpty()) {
|
2013-01-01 20:54:12 +00:00
|
|
|
DatabaseFactory.getMmsDatabase(context).markDownloadState(pendingMessages.remove().getMessageId(), MmsDatabase.Types.DOWNLOAD_NO_CONNECTIVITY);
|
|
|
|
toastHandler.makeToast("No connectivity available for MMS download, try again later...");
|
|
|
|
Log.w("MmsDownloadService", "Unable to download MMS, please try again later.");
|
|
|
|
finishConnectivity();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
if (!pendingMessages.isEmpty()) handleDownloadMmsContinued(pendingMessages.remove());
|
|
|
|
else finishConnectivity();
|
|
|
|
}
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public void process(MasterSecret masterSecret, Intent intent) {
|
|
|
|
if (intent.getAction().equals(SendReceiveService.DOWNLOAD_MMS_ACTION)) {
|
2012-09-30 18:46:45 +00:00
|
|
|
DownloadItem item = new DownloadItem(intent.getLongExtra("message_id", -1),
|
2013-01-01 20:54:12 +00:00
|
|
|
intent.getLongExtra("thread_id", -1),
|
|
|
|
intent.getStringExtra("content_location"),
|
|
|
|
intent.getByteArrayExtra("transaction_id"),
|
|
|
|
masterSecret);
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2012-09-30 18:46:45 +00:00
|
|
|
handleDownloadMms(item);
|
2011-12-20 18:20:44 +00:00
|
|
|
} else if (intent.getAction().equals(SendReceiveService.DOWNLOAD_MMS_CONNECTIVITY_ACTION)) {
|
|
|
|
handleConnectivityChange();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private class DownloadItem {
|
|
|
|
private long threadId;
|
|
|
|
private long messageId;
|
|
|
|
private byte[] transactionId;
|
|
|
|
private String contentLocation;
|
|
|
|
private MasterSecret masterSecret;
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public DownloadItem(long messageId, long threadId, String contentLocation, byte[] transactionId, MasterSecret masterSecret) {
|
|
|
|
this.threadId = threadId;
|
|
|
|
this.messageId = messageId;
|
|
|
|
this.contentLocation = contentLocation;
|
|
|
|
this.masterSecret = masterSecret;
|
|
|
|
this.transactionId = transactionId;
|
|
|
|
}
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public long getThreadId() {
|
|
|
|
return threadId;
|
|
|
|
}
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public long getMessageId() {
|
|
|
|
return messageId;
|
|
|
|
}
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public String getContentLocation() {
|
|
|
|
return contentLocation;
|
|
|
|
}
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public byte[] getTransactionId() {
|
|
|
|
return transactionId;
|
|
|
|
}
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public MasterSecret getMasterSecret() {
|
|
|
|
return masterSecret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-01-01 20:54:12 +00:00
|
|
|
protected String getConnectivityAction() {
|
2011-12-20 18:20:44 +00:00
|
|
|
return SendReceiveService.DOWNLOAD_MMS_CONNECTIVITY_ACTION;
|
|
|
|
}
|
|
|
|
}
|