2013-08-22 02:34:11 +00:00
|
|
|
/**
|
|
|
|
* Copyright (C) 2013 Open 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/>.
|
|
|
|
*/
|
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
|
|
|
package org.thoughtcrime.securesms.transport;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2013-07-13 00:40:41 +00:00
|
|
|
import android.util.Log;
|
2013-07-17 22:13:00 +00:00
|
|
|
import android.util.Pair;
|
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
|
|
|
|
2013-08-18 01:37:18 +00:00
|
|
|
import org.whispersystems.textsecure.crypto.MasterSecret;
|
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
|
|
|
import org.thoughtcrime.securesms.database.model.SmsMessageRecord;
|
2013-07-13 00:40:41 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2013-07-17 22:13:00 +00:00
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
2013-07-13 00:40:41 +00:00
|
|
|
import org.whispersystems.textsecure.directory.NumberFilter;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2013-07-17 22:13:00 +00:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import ws.com.google.android.mms.pdu.EncodedStringValue;
|
|
|
|
import ws.com.google.android.mms.pdu.SendReq;
|
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
|
|
|
|
|
|
|
public class UniversalTransport {
|
|
|
|
|
2013-07-13 00:40:41 +00:00
|
|
|
private final Context context;
|
|
|
|
private final PushTransport pushTransport;
|
|
|
|
private final SmsTransport smsTransport;
|
2013-07-17 22:13:00 +00:00
|
|
|
private final MmsTransport mmsTransport;
|
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
|
|
|
|
|
|
|
public UniversalTransport(Context context, MasterSecret masterSecret) {
|
2013-07-13 00:40:41 +00:00
|
|
|
this.context = context;
|
|
|
|
this.pushTransport = new PushTransport(context, masterSecret);
|
|
|
|
this.smsTransport = new SmsTransport(context, masterSecret);
|
2013-07-17 22:13:00 +00:00
|
|
|
this.mmsTransport = new MmsTransport(context, masterSecret);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public void deliver(SmsMessageRecord message) throws UndeliverableMessageException {
|
2013-07-13 00:40:41 +00:00
|
|
|
if (!TextSecurePreferences.isPushRegistered(context)) {
|
|
|
|
smsTransport.deliver(message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Recipient recipient = message.getIndividualRecipient();
|
2013-07-17 22:13:00 +00:00
|
|
|
String number = Util.canonicalizeNumber(context, recipient.getNumber());
|
2013-07-13 00:40:41 +00:00
|
|
|
|
|
|
|
if (NumberFilter.getInstance(context).containsNumber(number)) {
|
|
|
|
try {
|
|
|
|
Log.w("UniversalTransport", "Delivering with GCM...");
|
|
|
|
pushTransport.deliver(message);
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
Log.w("UniversalTransport", ioe);
|
|
|
|
smsTransport.deliver(message);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Log.w("UniversalTransport", "Delivering with SMS...");
|
|
|
|
smsTransport.deliver(message);
|
|
|
|
}
|
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
|
|
|
}
|
2013-07-17 22:13:00 +00:00
|
|
|
|
|
|
|
public Pair<byte[], Integer> deliver(SendReq mediaMessage) throws UndeliverableMessageException {
|
|
|
|
if (!TextSecurePreferences.isPushRegistered(context)) {
|
|
|
|
return mmsTransport.deliver(mediaMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
List<String> destinations = getMediaDestinations(mediaMessage);
|
|
|
|
|
|
|
|
if (NumberFilter.getInstance(context).containsNumbers(destinations)) {
|
|
|
|
try {
|
|
|
|
Log.w("UniversalTransport", "Delivering media message with GCM...");
|
|
|
|
pushTransport.deliver(mediaMessage, destinations);
|
|
|
|
return new Pair<byte[], Integer>("push".getBytes("UTF-8"), 0);
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
Log.w("UniversalTransport", ioe);
|
|
|
|
return mmsTransport.deliver(mediaMessage);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Log.w("UniversalTransport", "Delivering media message with MMS...");
|
|
|
|
return mmsTransport.deliver(mediaMessage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<String> getMediaDestinations(SendReq mediaMessage) {
|
|
|
|
LinkedList<String> destinations = new LinkedList<String>();
|
|
|
|
|
|
|
|
if (mediaMessage.getTo() != null) {
|
|
|
|
for (EncodedStringValue to : mediaMessage.getTo()) {
|
|
|
|
destinations.add(Util.canonicalizeNumber(context, to.getString()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mediaMessage.getCc() != null) {
|
|
|
|
for (EncodedStringValue cc : mediaMessage.getCc()) {
|
|
|
|
destinations.add(Util.canonicalizeNumber(context, cc.getString()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mediaMessage.getBcc() != null) {
|
|
|
|
for (EncodedStringValue bcc : mediaMessage.getBcc()) {
|
|
|
|
destinations.add(Util.canonicalizeNumber(context, bcc.getString()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return destinations;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|