package kp.widget.sunpop;
import kp.widget.sunpop.SunPopUtils.ContactIdentification;
import kp.widget.sunpop.utils.Log;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.telephony.PhoneNumberUtils;
import android.telephony.gsm.SmsMessage;
import android.telephony.gsm.SmsMessage.MessageClass;
import android.text.format.DateUtils;
public class SmsMmsMessage {
private Context context;
private long timestamp = 0;
private int messageType = 0;
private String fromAddress = null;
private boolean fromEmailGateway = false;
private MessageClass messageClass = null;
private String messageBody = null;
private String contactName = null;
private String contactId = null;
private String contactLookupKey = null;
private int unreadCount = 0;
private long threadId = 0;
private boolean notify = true;
private int reminderCount = 0;
private long messageId = 0;
public static final int MESSAGE_TYPE_SMS = 0;
public static final int MESSAGE_TYPE_MMS = 1;
public static final int MESSAGE_TYPE_MESSAGE = 2;
//Sprint vars to check for special voicemial messages
private static final String SPRINT_BRAND = "sprint";
private static final String SPRINT_VOICEMAIL_PREFIX = "//ANDROID:";
//Private Extras strings
private static final String PREFIX = "kp.widget.sunpop.";
private static final String EXTRAS_FROM_ADDRESS = PREFIX + "EXTRAS_FROM_ADDRESS";
private static final String EXTRAS_MESSAGE_BODY = PREFIX + "EXTRAS_MESSAGE_BODY";
private static final String EXTRAS_TIMESTAMP = PREFIX + "EXTRAS_TIMESTAMP";
private static final String EXTRAS_UNREAD_COUNT = PREFIX + "EXTRAS_UNREAD_COUNT";
private static final String EXTRAS_THREAD_ID = PREFIX + "EXTRAS_THREAD_ID";
private static final String EXTRAS_CONTACT_ID = PREFIX + "EXTRAS_CONTACT_ID";
private static final String EXTRAS_CONTACT_LOOKUP = PREFIX + "EXTRAS_CONTACT_LOOKUP";
private static final String EXTRAS_CONTACT_NAME = PREFIX + "EXTRAS_CONTACT_NAME";
private static final String EXTRAS_MESSAGE_TYPE = PREFIX + "EXTRAS_MESSAGE_TYPE";
private static final String EXTRAS_MESSAGE_ID = PREFIX + "EXTRAS_MESSAGE_ID";
private static final String EXTRAS_EMAIL_GATEWAY = PREFIX + "EXTRAS_EMAIL_GATEWAY";
//Public Extras Strings
public static final String EXTRAS_NOTIFY = PREFIX + "EXTRAS_NOTIFY";
public static final String EXTRAS_REMINDER_COUNT = PREFIX + "EXTRAS_REMINDER_COUNT";
public static final String EXTRAS_REPLYING = PREFIX + "EXTRAS_REPLYING";
public static final String EXTRAS_QUICKREPLY = PREFIX + "EXTRAS_QUICKREPLY";
/**
* Construct SmsMmsMessage given a raw message (created from pdu), used for when
* a message is initially received via the network.
*/
public SmsMmsMessage(Context _context, SmsMessage[] messages, long _timestamp){
SmsMessage sms = messages[0];
context = _context;
timestamp = _timestamp;
messageType = MESSAGE_TYPE_SMS;
/*
* Fetch data from raw sms
*/
fromAddress = sms.getDisplayOriginatingAddress();
fromEmailGateway = sms.isEmail();
messageClass = sms.getMessageClass();
String body = "";
try{
if(messages.length == 1 || sms.isReplace()){
body = sms.getDisplayMessageBody();
}else{
StringBuffer bodyText = new StringBuffer();
for(int i = 0; i<messages.length; i++){
bodyText.append(messages[i].getMessageBody());
}
body = bodyText.toString();
}
}catch(Exception e){
if(Log.DEBUG){
Log.v("SmsMmsMessage<init> exception: "+e.toString());
}
}
messageBody = body;
/*
* Lookup the rest of the info from the system db
*/
ContactIdentification contactIdentify = null;
// If this SMS is from an email gateway then lookup contactId by email address
if(fromEmailGateway){
if(Log.DEBUG){
Log.v("Sms came from email gateway");
}
contactIdentify = SunPopUtils.getPersonIdFromEmail(context, fromAddress);
contactName = fromAddress;
}else{
if(Log.DEBUG){
Log.v("Sms not came from email gateway");
}
contactIdentify = SunPopUtils.getPersonIdFromPhoneNumber(context, fromAddress);
contactName = PhoneNumberUtils.formatNumber(fromAddress);
}
if(contactIdentify != null){
contactId = contactIdentify.contactId;
contactLookupKey = contactIdentify.contactLookup;
contactName = contactIdentify.contactName;
}
unreadCount = SunPopUtils.getUnreadMessagesCount(context, timestamp, messageBody);
if(contactName == null){
contactName = context.getString(android.R.string.unknownName);
}
}
/**
* Construct SmsMmsMessage from an extras bundle
* @return
*/
public SmsMmsMessage(Context _context, Bundle b){
context = _context;
fromAddress = b.getString(EXTRAS_FROM_ADDRESS);
messageBody = b.getString(EXTRAS_MESSAGE_BODY);
timestamp = b.getLong(EXTRAS_TIMESTAMP);
contactId = b.getString(EXTRAS_CONTACT_ID);
contactLookupKey = b.getString(EXTRAS_CONTACT_LOOKUP);
contactName = b.getString(EXTRAS_CONTACT_NAME);
unreadCount = b.getInt(EXTRAS_UNREAD_COUNT, 1);
threadId = b.getLong(EXTRAS_THREAD_ID, 0);
messageType = b.getInt(EXTRAS_MESSAGE_TYPE, MESSAGE_TYPE_SMS);
notify = b.getBoolean(EXTRAS_NOTIFY, false);
reminderCount = b.getInt(EXTRAS_REMINDER_COUNT, 0);
messageId = b.getLong(EXTRAS_MESSAGE_ID, 0);
fromEmailGateway = b.getBoolean(EXTRAS_EMAIL_GATEWAY, false);
}
/**
* Constuct SmsMmsMessage for getSmsDetails() - info fetched from the SMS
* database table
* @return
*/
public SmsMmsMessage(Context _context, String _fromAddress, String _messageBody,
long _timestamp, long _threadId, int _unreadCount, long _messageId, int _messageType){
context = _context;
fromAddress = _fromAddress;
messageBody = _messageBody;
timestamp = _timestamp;
messageType = _messageType;
ContactIdentification contactIdentity = null;
if(PhoneNumberUtils.isWellFormedSmsAddress(fromAddress)){
contactIdentity = SunPopUtils.getPersonIdFromPhoneNumber(context, fromAddress);
contactName = PhoneNumberUtils.formatNumber(fromAddress);
fromEmailGateway = false;
}else{
contactIdentity = SunPopUtils.getPersonIdFromEmail(context, fromAddress);
contactName = fromAddress;
fromEmailGateway = true;
}
if(contactIdentity != null){
contactId = contactIdentity.contactId;
contactLookupKey = contactIdentity.contactLookup;
contactName = contactIdentity.contactName;
}
unreadCount = _unreadCount;
threadId = _threadId;
messageId = _messageId;
if(contactName == null){
contactName = context.getString(android.R.string.unknownName);
}
}
public int getMessageType(){
return messageType;
}
public MessageClass getMessageClass(){
return messageClass;
}
//Checks if user is on carrier Sprint and message is a special system message
//Sprint
public boolean isSprintVisualVoicemail(){
if(!SPRINT_BRAND.equals(Build.BRAND)){
return false;
}
if(messageBody != null){
if(messageBody.trim().startsWith(SPRINT_VOICEMAIL_PREFIX)){
return true;
}
}
return false;
}
public String getContactId(){
return contactId;
}
public Intent getPopupIntent(){
Intent popup = new Intent(context, SunPopActivity.class);
popup.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
popup.putExtras(toBundle());
return popup;
}
/**
* Convert all SmsMmsMessage data to a extras bundle to send via an intent
*/
public Bundle toBundle(){
Bundle b = new Bundle();
b.putString(EXTRAS_FROM_ADDRESS, fromAddress);
b.putString(EXTRAS_MESSAGE_BODY, messageBody);
b.putLong(EXTRAS_TIMESTAMP, timestamp);
b.putString(EXTRAS_CONTACT_ID, contactId);
b.putString(EXTRAS_CONTACT_LOOKUP, contactLookupKey);
b.putString(EXTRAS_CONTACT_NAME, contactName);
b.putInt(EXTRAS_UNREAD_COUNT, unreadCount);
b.putLong(EXTRAS_THREAD_ID, threadId);
b.putInt(EXTRAS_MESSAGE_TYPE, messageType);
b.putBoolean(EXTRAS_NOTIFY, notify);
b.putInt(EXTRAS_REMINDER_COUNT, reminderCount);
b.putLong(EXTRAS_MESSAGE_ID, messageId);
b.putBoolean(EXTRAS_EMAIL_GATEWAY, fromEmailGateway);
return b;
}
public String getFromAddress(){
return fromAddress;
}
public String getFormattedTimestamp(){
return DateUtils.formatDateTime(context, timestamp, DateUtils.FORMAT_SHOW_TIME);
}
public String getContactName(){
if(contactName == null){
contactName = context.getString(android.R.string.unknownName);
}
return contactName;
}
public String getMessageBody(){
if(messageBody == null){
return "";
}
return messageBody;
}
public String getContactLookupKey(){
return contactLookupKey;
}
public long getMessageId(){
locateMessageId();
return messageId;
}
public void locateMessageId(){
if(messageId == 0){
if(threadId == 0){
locateThreadId();
}
messageId = SunPopUtils.findMessageId(context, threadId, timestamp, messageBody, messageType);
}
}
public void locateThreadId(){
if(threadId == 0){
threadId = SunPopUtils.findThreadIdFromAddress(context, fromAddress);
}
}
public long getThreadId(){
locateThreadId();
return threadId;
}
/**
* fetch the "reply to" message intent
*/
public Intent getReplyIntent(boolean replyToThread){
if(messageType == MESSAGE_TYPE_SMS){
locateThreadId();
if(replyToThread && threadId>0){
if(Log.DEBUG){
Log.v("replying by threadId: "+ threadId);
}
return SunPopUtils.getSmsToIntent(context, threadId);
}else{
// if(Log.DEBUG){
// Log.v("replying by address: " + fromAddress);
// }
//
// return SunPopUtils.getSmsToIntent(context, fromAddress);
}
}
return null;
}
}
|