146 lines
5.3 KiB
Java
Raw Normal View History

/**
2011-12-20 10:20:44 -08:00
* Copyright (C) 2011 Whisper Systems
*
2011-12-20 10:20:44 -08: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 10:20:44 -08: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.sms;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
2014-10-22 18:28:03 -07:00
import android.util.Pair;
2011-12-20 10:20:44 -08:00
2014-10-22 18:28:03 -07:00
import org.thoughtcrime.securesms.database.EncryptingSmsDatabase;
import org.thoughtcrime.securesms.database.MmsDatabase;
import org.thoughtcrime.securesms.database.MmsSmsColumns;
import org.thoughtcrime.securesms.mms.IncomingMediaMessage;
import org.thoughtcrime.securesms.mms.OutgoingMediaMessage;
2014-10-22 18:28:03 -07:00
import org.thoughtcrime.securesms.recipients.Recipients;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.textsecure.crypto.MasterSecret;
2011-12-20 10:20:44 -08:00
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.service.SendReceiveService;
2014-10-22 18:28:03 -07:00
import org.whispersystems.textsecure.push.IncomingPushMessage;
import org.whispersystems.textsecure.push.PushMessageProtos;
import org.whispersystems.textsecure.util.InvalidNumberException;
2011-12-20 10:20:44 -08:00
import java.util.List;
2011-12-20 10:20:44 -08:00
import ws.com.google.android.mms.MmsException;
2014-10-22 18:28:03 -07:00
import ws.com.google.android.mms.pdu.RetrieveConf;
2011-12-20 10:20:44 -08:00
public class MessageSender {
public static long send(Context context, MasterSecret masterSecret,
OutgoingTextMessage message, long threadId,
boolean forceSms)
{
2014-10-22 18:28:03 -07:00
EncryptingSmsDatabase database = DatabaseFactory.getEncryptingSmsDatabase(context);
if (threadId == -1) {
threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(message.getRecipients());
2014-10-22 18:28:03 -07:00
}
2011-12-20 10:20:44 -08:00
2014-10-22 18:28:03 -07:00
List<Long> messageIds = database.insertMessageOutbox(masterSecret, threadId, message, forceSms);
2011-12-20 10:20:44 -08:00
2014-10-22 18:28:03 -07:00
if (!forceSms && isSelfSend(context, message.getRecipients())) {
for (long messageId : messageIds) {
database.markAsSent(messageId);
database.markAsPush(messageId);
2014-10-22 18:28:03 -07:00
Pair<Long, Long> messageAndThreadId = database.copyMessageInbox(messageId);
database.markAsPush(messageAndThreadId.first);
}
} else {
for (long messageId : messageIds) {
Log.w("SMSSender", "Got message id for new message: " + messageId);
Intent intent = new Intent(SendReceiveService.SEND_SMS_ACTION, null,
context, SendReceiveService.class);
intent.putExtra("message_id", messageId);
context.startService(intent);
}
2011-12-20 10:20:44 -08:00
}
2011-12-20 10:20:44 -08:00
return threadId;
}
public static long send(Context context, MasterSecret masterSecret,
OutgoingMediaMessage message,
long threadId, boolean forceSms)
throws MmsException
{
2014-10-22 18:28:03 -07:00
MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
if (threadId == -1) {
threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(message.getRecipients(), message.getDistributionType());
2014-10-22 18:28:03 -07:00
}
2014-10-22 18:28:03 -07:00
long messageId = database.insertMessageOutbox(masterSecret, message, threadId, forceSms);
2014-10-22 18:28:03 -07:00
if (!forceSms && isSelfSend(context, message.getRecipients())) {
database.markAsSent(messageId, "self-send".getBytes(), 0);
database.markAsPush(messageId);
long newMessageId = database.copyMessageInbox(masterSecret, messageId);
database.markAsPush(newMessageId);
} else {
Intent intent = new Intent(SendReceiveService.SEND_MMS_ACTION, null,
context, SendReceiveService.class);
intent.putExtra("message_id", messageId);
intent.putExtra("thread_id", threadId);
2014-10-22 18:28:03 -07:00
context.startService(intent);
}
return threadId;
}
public static void resend(Context context, long messageId, boolean isMms)
{
Intent intent;
if (isMms) {
DatabaseFactory.getMmsDatabase(context).markAsSending(messageId);
intent = new Intent(SendReceiveService.SEND_MMS_ACTION, null,
context, SendReceiveService.class);
} else {
DatabaseFactory.getSmsDatabase(context).markAsSending(messageId);
intent = new Intent(SendReceiveService.SEND_SMS_ACTION, null,
context, SendReceiveService.class);
}
intent.putExtra("message_id", messageId);
context.startService(intent);
}
2014-10-22 18:28:03 -07:00
private static boolean isSelfSend(Context context, Recipients recipients) {
try {
if (!TextSecurePreferences.isPushRegistered(context)) {
return false;
}
if (!recipients.isSingleRecipient()) {
return false;
}
String e164number = Util.canonicalizeNumber(context, recipients.getPrimaryRecipient().getNumber());
return TextSecurePreferences.getLocalNumber(context).equals(e164number);
} catch (InvalidNumberException e) {
Log.w("MessageSender", e);
return false;
}
}
2011-12-20 10:20:44 -08:00
}