session-android/src/org/thoughtcrime/securesms/service/MmsDownloader.java

233 lines
8.5 KiB
Java
Raw Normal View History

/**
2011-12-20 18:20:44 +00:00
* Copyright (C) 2011 Whisper Systems
*
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.
*
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;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
2011-12-20 18:20:44 +00:00
import org.thoughtcrime.securesms.R;
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.RetrieveConf;
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;
2011-12-20 18:20:44 +00:00
public MmsDownloader(Context context, SendReceiveService.ToastHandler toastHandler) {
super(context);
this.toastHandler = toastHandler;
}
public void process(MasterSecret masterSecret, Intent intent) {
if (intent.getAction().equals(SendReceiveService.DOWNLOAD_MMS_ACTION)) {
DownloadItem item = new DownloadItem(masterSecret, false, false,
intent.getLongExtra("message_id", -1),
intent.getLongExtra("thread_id", -1),
intent.getStringExtra("content_location"),
intent.getByteArrayExtra("transaction_id"));
handleDownloadMmsAction(item);
} else if (intent.getAction().equals(SendReceiveService.DOWNLOAD_MMS_CONNECTIVITY_ACTION)) {
handleConnectivityChange();
}
}
private void handleDownloadMmsAction(DownloadItem item) {
2011-12-20 18:20:44 +00:00
if (!isConnectivityPossible()) {
Log.w("MmsDownloader", "No MMS connectivity available!");
2011-12-20 18:20:44 +00:00
DatabaseFactory.getMmsDatabase(context).markDownloadState(item.getMessageId(), MmsDatabase.Types.DOWNLOAD_NO_CONNECTIVITY);
toastHandler.makeToast(context.getString(R.string.MmsDownloader_no_connectivity_available_for_mms_download_try_again_later));
return;
2011-12-20 18:20:44 +00:00
}
DatabaseFactory.getMmsDatabase(context).markDownloadState(item.getMessageId(), MmsDatabase.Types.DOWNLOAD_CONNECTING);
if (item.useMmsRadioMode()) downloadMmsWithRadioChange(item);
else downloadMms(item);
}
private void downloadMmsWithRadioChange(DownloadItem item) {
Log.w("MmsDownloader", "Handling MMS download with radio change...");
pendingMessages.add(item);
issueConnectivityRequest();
2011-12-20 18:20:44 +00:00
}
private void downloadMms(DownloadItem item) {
Log.w("MmsDownloadService", "Handling actual MMS download...");
2011-12-20 18:20:44 +00:00
MmsDatabase mmsDatabase;
if (item.getMasterSecret() == null) {
2011-12-20 18:20:44 +00:00
mmsDatabase = DatabaseFactory.getMmsDatabase(context);
} else {
2011-12-20 18:20:44 +00:00
mmsDatabase = DatabaseFactory.getEncryptingMmsDatabase(context, item.getMasterSecret());
}
2011-12-20 18:20:44 +00:00
try {
RetrieveConf retrieved = MmsDownloadHelper.retrieveMms(context, item.getContentLocation(),
getApnInformation(),
item.useMmsRadioMode(),
item.proxyRequestIfPossible());
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", "Got MMS part of content-type: " +
2013-01-01 20:54:12 +00:00
new String(retrieved.getBody().getPart(i).getContentType()));
}
storeRetrievedMms(mmsDatabase, item, retrieved);
2011-12-20 18:20:44 +00:00
// NotifyRespInd notifyResponse = new NotifyRespInd(PduHeaders.CURRENT_MMS_VERSION, item.getTransactionId(), PduHeaders.STATUS_RETRIEVED);
// MmsSendHelper.sendMms(context, new PduComposer(context, notifyResponse).make());
} catch (IOException e) {
Log.w("MmsDownloader", e);
if (!item.useMmsRadioMode() && !item.proxyRequestIfPossible()) {
scheduleDownloadWithRadioMode(item);
} else if (!item.proxyRequestIfPossible()) {
scheduleDownloadWithRadioModeAndProxy(item);
} else {
DatabaseFactory.getMmsDatabase(context).markDownloadState(item.getMessageId(), MmsDatabase.Types.DOWNLOAD_SOFT_FAILURE);
toastHandler.makeToast(context.getString(R.string.MmsDownloader_error_connecting_to_mms_provider));
}
2011-12-20 18:20:44 +00:00
} catch (MmsException e) {
Log.w("MmsDownloader", e);
DatabaseFactory.getMmsDatabase(context).markDownloadState(item.getMessageId(), MmsDatabase.Types.DOWNLOAD_HARD_FAILURE);
toastHandler.makeToast(context.getString(R.string.MmsDownloader_error_storing_mms));
2011-12-20 18:20:44 +00:00
}
}
private void storeRetrievedMms(MmsDatabase mmsDatabase, DownloadItem item, RetrieveConf retrieved)
throws MmsException
{
if (retrieved.getSubject() != null && WirePrefix.isEncryptedMmsSubject(retrieved.getSubject().getString())) {
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());
}
mmsDatabase.delete(item.getMessageId());
}
2011-12-20 18:20:44 +00:00
protected void handleConnectivityChange() {
LinkedList<DownloadItem> downloadItems = (LinkedList<DownloadItem>)pendingMessages.clone();
if (isConnected()) {
pendingMessages.clear();
for (DownloadItem item : downloadItems) {
downloadMms(item);
2011-12-20 18:20:44 +00:00
}
finishConnectivity();
} else if (!isConnected() && !isConnectivityPossible()) {
pendingMessages.clear();
for (DownloadItem item : downloadItems) {
DatabaseFactory.getMmsDatabase(context).markDownloadState(item.getMessageId(), MmsDatabase.Types.DOWNLOAD_NO_CONNECTIVITY);
}
toastHandler.makeToast(context
.getString(R.string.MmsDownloader_no_connectivity_available_for_mms_download_try_again_later));
finishConnectivity();
}
2011-12-20 18:20:44 +00:00
}
2011-12-20 18:20:44 +00:00
private void scheduleDownloadWithRadioMode(DownloadItem item) {
item.mmsRadioMode = true;
handleDownloadMmsAction(item);
}
private void scheduleDownloadWithRadioModeAndProxy(DownloadItem item) {
item.mmsRadioMode = true;
item.proxyIfPossible = true;
handleDownloadMmsAction(item);
2011-12-20 18:20:44 +00:00
}
private static class DownloadItem {
private final MasterSecret masterSecret;
private boolean mmsRadioMode;
private boolean proxyIfPossible;
2011-12-20 18:20:44 +00:00
private long threadId;
private long messageId;
private byte[] transactionId;
private String contentLocation;
public DownloadItem(MasterSecret masterSecret, boolean mmsRadioMode, boolean proxyIfPossible,
long messageId, long threadId, String contentLocation, byte[] transactionId)
{
this.masterSecret = masterSecret;
this.mmsRadioMode = mmsRadioMode;
this.proxyIfPossible = proxyIfPossible;
2011-12-20 18:20:44 +00:00
this.threadId = threadId;
this.messageId = messageId;
this.contentLocation = contentLocation;
this.transactionId = transactionId;
}
2011-12-20 18:20:44 +00:00
public long getThreadId() {
return threadId;
}
2011-12-20 18:20:44 +00:00
public long getMessageId() {
return messageId;
}
2011-12-20 18:20:44 +00:00
public String getContentLocation() {
return contentLocation;
}
2011-12-20 18:20:44 +00:00
public byte[] getTransactionId() {
return transactionId;
}
2011-12-20 18:20:44 +00:00
public MasterSecret getMasterSecret() {
return masterSecret;
}
public boolean proxyRequestIfPossible() {
return proxyIfPossible;
}
public boolean useMmsRadioMode() {
return mmsRadioMode;
}
2011-12-20 18:20:44 +00:00
}
@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;
}
}