Fix NPE when 'from' missing in NotificationInd

Fixes #2806
// FREEBIE
This commit is contained in:
Jake McGinty 2015-03-27 10:26:17 -07:00
parent 17383ddfe0
commit 6a6f900b7d

View File

@ -22,6 +22,7 @@ import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri; import android.net.Uri;
import android.support.annotation.NonNull;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
@ -339,15 +340,13 @@ public class MmsDatabase extends MessagingDatabase {
} }
} }
private long getThreadIdFor(NotificationInd notification) throws RecipientFormattingException { private long getThreadIdFor(@NonNull NotificationInd notification) {
try { String fromString = notification.getFrom() != null && notification.getFrom().getTextString() != null
EncodedStringValue encodedString = notification.getFrom(); ? Util.toIsoString(notification.getFrom().getTextString())
String fromString = new String(encodedString.getTextString(), CharacterSets.MIMENAME_ISO_8859_1); : "";
Recipients recipients = RecipientFactory.getRecipientsFromString(context, fromString, false); Recipients recipients = RecipientFactory.getRecipientsFromString(context, fromString, false);
if (recipients.isEmpty()) recipients = new Recipients(Recipient.getUnknownRecipient(context));
return DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipients); return DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipients);
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
} }
public Cursor getMessage(long messageId) { public Cursor getMessage(long messageId) {
@ -646,21 +645,20 @@ public class MmsDatabase extends MessagingDatabase {
(retrieved.isPushMessage() ? Types.PUSH_MESSAGE_BIT : 0)); (retrieved.isPushMessage() ? Types.PUSH_MESSAGE_BIT : 0));
} }
public Pair<Long, Long> insertMessageInbox(NotificationInd notification) { public Pair<Long, Long> insertMessageInbox(@NonNull NotificationInd notification) {
try {
SQLiteDatabase db = databaseHelper.getWritableDatabase(); SQLiteDatabase db = databaseHelper.getWritableDatabase();
MmsAddressDatabase addressDatabase = DatabaseFactory.getMmsAddressDatabase(context); MmsAddressDatabase addressDatabase = DatabaseFactory.getMmsAddressDatabase(context);
long threadId = getThreadIdFor(notification); long threadId = getThreadIdFor(notification);
PduHeaders headers = notification.getPduHeaders(); PduHeaders headers = notification.getPduHeaders();
ContentValues contentValues = getContentValuesFromHeader(headers); ContentValues contentValues = getContentValuesFromHeader(headers);
Log.w("MmsDatabse", "Message received type: " + headers.getOctet(PduHeaders.MESSAGE_TYPE)); Log.w(TAG, "Message received type: " + headers.getOctet(PduHeaders.MESSAGE_TYPE));
contentValues.put(MESSAGE_BOX, Types.BASE_INBOX_TYPE); contentValues.put(MESSAGE_BOX, Types.BASE_INBOX_TYPE);
contentValues.put(THREAD_ID, threadId); contentValues.put(THREAD_ID, threadId);
contentValues.put(STATUS, Status.DOWNLOAD_INITIALIZED); contentValues.put(STATUS, Status.DOWNLOAD_INITIALIZED);
contentValues.put(DATE_RECEIVED, System.currentTimeMillis() / 1000); contentValues.put(DATE_RECEIVED, System.currentTimeMillis() / 1000);
contentValues.put(READ, org.thoughtcrime.securesms.util.Util.isDefaultSmsProvider(context) ? 0 : 1); contentValues.put(READ, Util.isDefaultSmsProvider(context) ? 0 : 1);
if (!contentValues.containsKey(DATE_SENT)) if (!contentValues.containsKey(DATE_SENT))
contentValues.put(DATE_SENT, contentValues.getAsLong(DATE_RECEIVED)); contentValues.put(DATE_SENT, contentValues.getAsLong(DATE_RECEIVED));
@ -668,16 +666,7 @@ public class MmsDatabase extends MessagingDatabase {
long messageId = db.insert(TABLE_NAME, null, contentValues); long messageId = db.insert(TABLE_NAME, null, contentValues);
addressDatabase.insertAddressesForId(messageId, headers); addressDatabase.insertAddressesForId(messageId, headers);
// notifyConversationListeners(threadId); return new Pair<>(messageId, threadId);
// DatabaseFactory.getThreadDatabase(context).update(threadId);
// DatabaseFactory.getThreadDatabase(context).setUnread(threadId);
// Trimmer.trimThread(context, threadId);
return new Pair<Long, Long>(messageId, threadId);
} catch (RecipientFormattingException rfe) {
Log.w("MmsDatabase", rfe);
return new Pair<Long, Long>(-1L, -1L);
}
} }
public void markIncomingNotificationReceived(long threadId) { public void markIncomingNotificationReceived(long threadId) {