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;
|
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;
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.MmsDatabase;
|
|
|
|
import org.thoughtcrime.securesms.mms.MmsDownloadHelper;
|
2013-05-07 02:09:06 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.MmsSendHelper;
|
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-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
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.LinkedList;
|
2013-04-26 18:23:43 +00:00
|
|
|
import java.util.List;
|
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
|
|
|
|
2013-02-21 02:10:33 +00:00
|
|
|
public void process(MasterSecret masterSecret, Intent intent) {
|
|
|
|
if (intent.getAction().equals(SendReceiveService.DOWNLOAD_MMS_ACTION)) {
|
2013-03-04 02:44:58 +00:00
|
|
|
boolean isCdma = ((TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE)).getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA;
|
|
|
|
DownloadItem item = new DownloadItem(masterSecret, !isCdma, false,
|
2013-02-21 02:10:33 +00:00
|
|
|
intent.getLongExtra("message_id", -1),
|
|
|
|
intent.getLongExtra("thread_id", -1),
|
2013-04-26 18:23:43 +00:00
|
|
|
intent.getBooleanExtra("automatic", false),
|
2013-02-21 02:10:33 +00:00
|
|
|
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()) {
|
2013-02-21 02:10:33 +00:00
|
|
|
Log.w("MmsDownloader", "No MMS connectivity available!");
|
2013-04-26 18:23:43 +00:00
|
|
|
handleDownloadError(item, MmsDatabase.Status.DOWNLOAD_NO_CONNECTIVITY,
|
|
|
|
context.getString(R.string.MmsDownloader_no_connectivity_available_for_mms_download_try_again_later));
|
2013-02-21 02:10:33 +00:00
|
|
|
return;
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2013-02-21 02:10:33 +00:00
|
|
|
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
DatabaseFactory.getMmsDatabase(context).markDownloadState(item.getMessageId(), MmsDatabase.Status.DOWNLOAD_CONNECTING);
|
2013-02-21 02:10:33 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2013-02-21 02:10:33 +00:00
|
|
|
private void downloadMms(DownloadItem item) {
|
|
|
|
Log.w("MmsDownloadService", "Handling actual MMS download...");
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
MmsDatabase mmsDatabase = DatabaseFactory.getMmsDatabase(context);
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
try {
|
2013-02-21 02:10:33 +00:00
|
|
|
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++) {
|
2013-02-21 02:10:33 +00:00
|
|
|
Log.w("MmsDownloader", "Got MMS part of content-type: " +
|
2013-01-01 20:54:12 +00:00
|
|
|
new String(retrieved.getBody().getPart(i).getContentType()));
|
|
|
|
}
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2013-02-21 02:10:33 +00:00
|
|
|
storeRetrievedMms(mmsDatabase, item, retrieved);
|
2013-05-07 02:09:06 +00:00
|
|
|
sendRetrievedAcknowledgement(item);
|
2011-12-20 18:20:44 +00:00
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w("MmsDownloader", e);
|
2013-02-21 02:10:33 +00:00
|
|
|
if (!item.useMmsRadioMode() && !item.proxyRequestIfPossible()) {
|
2013-03-10 22:47:13 +00:00
|
|
|
Log.w("MmsDownloader", "Falling back to just radio mode...");
|
2013-02-21 02:10:33 +00:00
|
|
|
scheduleDownloadWithRadioMode(item);
|
|
|
|
} else if (!item.proxyRequestIfPossible()) {
|
2013-03-10 22:47:13 +00:00
|
|
|
Log.w("MmsDownloadeR", "Falling back to radio mode and proxy...");
|
2013-02-21 02:10:33 +00:00
|
|
|
scheduleDownloadWithRadioModeAndProxy(item);
|
|
|
|
} else {
|
2013-04-26 18:23:43 +00:00
|
|
|
handleDownloadError(item, MmsDatabase.Status.DOWNLOAD_SOFT_FAILURE,
|
|
|
|
context.getString(R.string.MmsDownloader_error_connecting_to_mms_provider));
|
2013-02-21 02:10:33 +00:00
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
} catch (MmsException e) {
|
|
|
|
Log.w("MmsDownloader", e);
|
2013-04-26 18:23:43 +00:00
|
|
|
handleDownloadError(item, MmsDatabase.Status.DOWNLOAD_HARD_FAILURE,
|
|
|
|
context.getString(R.string.MmsDownloader_error_storing_mms));
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2013-02-21 02:10:33 +00:00
|
|
|
private void storeRetrievedMms(MmsDatabase mmsDatabase, DownloadItem item, RetrieveConf retrieved)
|
|
|
|
throws MmsException
|
|
|
|
{
|
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-04-26 18:23:43 +00:00
|
|
|
messageAndThreadId = mmsDatabase.insertSecureMessageInbox(item.getMasterSecret(), retrieved,
|
|
|
|
item.getContentLocation(),
|
|
|
|
item.getThreadId());
|
2013-02-21 02:10:33 +00:00
|
|
|
|
|
|
|
if (item.getMasterSecret() != null)
|
2013-04-26 18:23:43 +00:00
|
|
|
DecryptingQueue.scheduleDecryption(context, item.getMasterSecret(), messageAndThreadId.first,
|
|
|
|
messageAndThreadId.second, retrieved);
|
2013-02-21 02:10:33 +00:00
|
|
|
|
|
|
|
} else {
|
2013-04-26 18:23:43 +00:00
|
|
|
messageAndThreadId = mmsDatabase.insertMessageInbox(item.getMasterSecret(), retrieved,
|
|
|
|
item.getContentLocation(),
|
|
|
|
item.getThreadId());
|
2013-02-21 02:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mmsDatabase.delete(item.getMessageId());
|
2013-04-26 18:23:43 +00:00
|
|
|
MessageNotifier.updateNotification(context, item.getMasterSecret(), messageAndThreadId.second);
|
2013-02-21 02:10:33 +00:00
|
|
|
}
|
|
|
|
|
2013-05-07 02:09:06 +00:00
|
|
|
private void sendRetrievedAcknowledgement(DownloadItem item) {
|
|
|
|
try {
|
|
|
|
NotifyRespInd notifyResponse = new NotifyRespInd(PduHeaders.CURRENT_MMS_VERSION,
|
|
|
|
item.getTransactionId(),
|
|
|
|
PduHeaders.STATUS_RETRIEVED);
|
|
|
|
|
|
|
|
MmsSendHelper.sendNotificationReceived(context, new PduComposer(context, notifyResponse).make(),
|
|
|
|
getApnInformation(), item.useMmsRadioMode(),
|
|
|
|
item.proxyRequestIfPossible());
|
|
|
|
} catch (InvalidHeaderValueException e) {
|
|
|
|
Log.w("MmsDownloader", e);
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w("MmsDownloader", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
protected void handleConnectivityChange() {
|
2013-02-27 06:56:48 +00:00
|
|
|
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
|
|
|
}
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2013-03-04 02:44:58 +00:00
|
|
|
if (pendingMessages.isEmpty())
|
|
|
|
finishConnectivity();
|
|
|
|
|
2013-05-07 22:09:24 +00:00
|
|
|
} else if (!isConnected() && (!isConnectivityPossible() || isConnectivityFailure())) {
|
2013-02-27 06:56:48 +00:00
|
|
|
pendingMessages.clear();
|
2013-04-26 18:23:43 +00:00
|
|
|
handleDownloadError(downloadItems, MmsDatabase.Status.DOWNLOAD_NO_CONNECTIVITY,
|
|
|
|
context.getString(R.string.MmsDownloader_no_connectivity_available_for_mms_download_try_again_later));
|
|
|
|
finishConnectivity();
|
|
|
|
}
|
|
|
|
}
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2013-04-26 18:23:43 +00:00
|
|
|
private void handleDownloadError(List<DownloadItem> items, int downloadStatus, String error) {
|
|
|
|
MmsDatabase db = DatabaseFactory.getMmsDatabase(context);
|
2013-02-21 02:10:33 +00:00
|
|
|
|
2013-04-26 18:23:43 +00:00
|
|
|
for (DownloadItem item : items) {
|
|
|
|
db.markDownloadState(item.getMessageId(), downloadStatus);
|
2013-02-27 06:56:48 +00:00
|
|
|
|
2013-04-26 18:23:43 +00:00
|
|
|
if (item.isAutomatic()) {
|
|
|
|
db.markIncomingNotificationReceived(item.getThreadId());
|
|
|
|
MessageNotifier.updateNotification(context, item.getMasterSecret(), item.getThreadId());
|
|
|
|
}
|
2013-02-27 06:56:48 +00:00
|
|
|
}
|
2013-04-26 18:23:43 +00:00
|
|
|
|
|
|
|
toastHandler.makeToast(error);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2013-04-26 18:23:43 +00:00
|
|
|
private void handleDownloadError(DownloadItem item, int downloadStatus, String error) {
|
|
|
|
MmsDatabase db = DatabaseFactory.getMmsDatabase(context);
|
|
|
|
db.markDownloadState(item.getMessageId(), downloadStatus);
|
|
|
|
|
|
|
|
if (item.isAutomatic()) {
|
|
|
|
db.markIncomingNotificationReceived(item.getThreadId());
|
|
|
|
MessageNotifier.updateNotification(context, item.getMasterSecret(), item.getThreadId());
|
|
|
|
}
|
|
|
|
|
|
|
|
toastHandler.makeToast(error);
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2013-02-21 02:10:33 +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
|
|
|
}
|
|
|
|
|
2013-02-21 02:10:33 +00:00
|
|
|
private static class DownloadItem {
|
|
|
|
private final MasterSecret masterSecret;
|
|
|
|
private boolean mmsRadioMode;
|
|
|
|
private boolean proxyIfPossible;
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private long threadId;
|
|
|
|
private long messageId;
|
|
|
|
private byte[] transactionId;
|
|
|
|
private String contentLocation;
|
2013-04-26 18:23:43 +00:00
|
|
|
private boolean automatic;
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2013-02-21 02:10:33 +00:00
|
|
|
public DownloadItem(MasterSecret masterSecret, boolean mmsRadioMode, boolean proxyIfPossible,
|
2013-04-26 18:23:43 +00:00
|
|
|
long messageId, long threadId, boolean automatic, String contentLocation,
|
|
|
|
byte[] transactionId)
|
2013-02-21 02:10:33 +00:00
|
|
|
{
|
|
|
|
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;
|
2013-04-26 18:23:43 +00:00
|
|
|
this.automatic = automatic;
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|
2013-02-21 02:10:33 +00:00
|
|
|
|
|
|
|
public boolean proxyRequestIfPossible() {
|
|
|
|
return proxyIfPossible;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean useMmsRadioMode() {
|
|
|
|
return mmsRadioMode;
|
|
|
|
}
|
2013-04-26 18:23:43 +00:00
|
|
|
|
|
|
|
public boolean isAutomatic() {
|
|
|
|
return automatic;
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
}
|