reworked notification a bit

This commit is contained in:
Daniel Gultsch 2014-02-10 22:45:59 +01:00
parent cfc74e7c8e
commit b673af2ba0
11 changed files with 86 additions and 37 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 932 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.8 KiB

View File

@ -60,10 +60,8 @@ public class Account extends AbstractEntity{
public void setOption(int option, boolean value) { public void setOption(int option, boolean value) {
if (value) { if (value) {
this.options = (this.options | 1 << option); this.options = (this.options | 1 << option);
Log.d("xmppService","enabling option "+this.options);
} else { } else {
this.options = (this.options & 0 << option); this.options = (this.options & 0 << option);
Log.d("xmppService","disabeling option "+this.options);
} }
} }

View File

@ -6,6 +6,7 @@ import java.util.List;
import android.content.ContentValues; import android.content.ContentValues;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.util.Log;
public class Conversation extends AbstractEntity { public class Conversation extends AbstractEntity {
@ -71,6 +72,20 @@ public class Conversation extends AbstractEntity {
return messages; return messages;
} }
public boolean isRead() {
if (this.messages == null) return true;
return this.messages.get(this.messages.size() - 1).isRead();
}
public void markRead() {
Log.d("xmppService", "inside mark read method");
if (this.messages == null) return;
for(int i = this.messages.size() -1; i >= 0; --i) {
if (messages.get(i).isRead()) return;
this.messages.get(i).markRead();
}
}
public String getLatestMessage() { public String getLatestMessage() {
if ((this.messages == null)||(this.messages.size()==0)) { if ((this.messages == null)||(this.messages.size()==0)) {
return null; return null;
@ -121,8 +136,10 @@ public class Conversation extends AbstractEntity {
public void setContact(Contact contact) { public void setContact(Contact contact) {
this.contact = contact; this.contact = contact;
if (contact!=null) {
this.contactUuid = contact.getUuid(); this.contactUuid = contact.getUuid();
} }
}
public void setAccount(Account account) { public void setAccount(Account account) {
this.account = account; this.account = account;
@ -151,7 +168,7 @@ public class Conversation extends AbstractEntity {
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put(UUID, uuid); values.put(UUID, uuid);
values.put(NAME, name); values.put(NAME, name);
values.put(CONTACT, contact.getUuid()); values.put(CONTACT, contactUuid);
values.put(ACCOUNT, accountUuid); values.put(ACCOUNT, accountUuid);
values.put(CONTACTJID, contactJid); values.put(CONTACTJID, contactJid);
values.put(CREATED, created); values.put(CREATED, created);

View File

@ -30,6 +30,7 @@ public class Message extends AbstractEntity {
protected long timeSent; protected long timeSent;
protected int encryption; protected int encryption;
protected int status; protected int status;
protected boolean read = true;
protected transient Conversation conversation = null; protected transient Conversation conversation = null;
@ -115,4 +116,16 @@ public class Message extends AbstractEntity {
this.status = status; this.status = status;
} }
public boolean isRead() {
return this.read;
}
public void markRead() {
this.read = true;
}
public void markUnread() {
this.read = false;
}
} }

View File

@ -86,6 +86,7 @@ public class XmppConnectionService extends Service {
forwarded = packet.findChild("sent").findChild( forwarded = packet.findChild("sent").findChild(
"forwarded"); "forwarded");
status = Message.STATUS_SEND; status = Message.STATUS_SEND;
notify = false;
} else { } else {
return; // massage has no body and is not carbon. just return; // massage has no body and is not carbon. just
// skip // skip
@ -128,6 +129,9 @@ public class XmppConnectionService extends Service {
} }
Message message = new Message(conversation, counterPart, body, Message message = new Message(conversation, counterPart, body,
Message.ENCRYPTION_NONE, status); Message.ENCRYPTION_NONE, status);
if(notify) {
message.markUnread();
}
conversation.getMessages().add(message); conversation.getMessages().add(message);
databaseBackend.createMessage(message); databaseBackend.createMessage(message);
if (convChangedListener != null) { if (convChangedListener != null) {
@ -398,6 +402,7 @@ public class XmppConnectionService extends Service {
Account account = accountLookupTable.get(conv.getAccountUuid()); Account account = accountLookupTable.get(conv.getAccountUuid());
conv.setAccount(account); conv.setAccount(account);
conv.setContact(findContact(account, conv.getContactJid())); conv.setContact(findContact(account, conv.getContactJid()));
conv.setMessages(databaseBackend.getMessages(conv, 50));
} }
} }
return this.conversations; return this.conversations;
@ -407,10 +412,6 @@ public class XmppConnectionService extends Service {
return this.accounts; return this.accounts;
} }
public List<Message> getMessages(Conversation conversation) {
return databaseBackend.getMessages(conversation, 100);
}
public Contact findContact(Account account, String jid) { public Contact findContact(Account account, String jid) {
return databaseBackend.findContact(account, jid); return databaseBackend.findContact(account, jid);
} }

View File

@ -16,6 +16,7 @@ import android.app.FragmentTransaction;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.graphics.Typeface;
import android.support.v4.widget.SlidingPaneLayout; import android.support.v4.widget.SlidingPaneLayout;
import android.support.v4.widget.SlidingPaneLayout.PanelSlideListener; import android.support.v4.widget.SlidingPaneLayout.PanelSlideListener;
import android.util.Log; import android.util.Log;
@ -138,9 +139,20 @@ public class ConversationActivity extends XmppActivity {
view = (View) inflater.inflate( view = (View) inflater.inflate(
R.layout.conversation_list_row, null); R.layout.conversation_list_row, null);
} }
((TextView) view.findViewById(R.id.conversation_name)) Conversation conv = getItem(position);
.setText(getItem(position).getName()); TextView convName = (TextView) view.findViewById(R.id.conversation_name);
((TextView) view.findViewById(R.id.conversation_lastmsg)).setText(getItem(position).getLatestMessage()); convName.setText(conv.getName());
TextView convLastMsg = (TextView) view.findViewById(R.id.conversation_lastmsg);
convLastMsg.setText(conv.getLatestMessage());
if(!conv.isRead()) {
convName.setTypeface(null,Typeface.BOLD);
convLastMsg.setTypeface(null,Typeface.BOLD);
} else {
convName.setTypeface(null,Typeface.NORMAL);
convLastMsg.setTypeface(null,Typeface.NORMAL);
}
((TextView) view.findViewById(R.id.conversation_lastupdate)) ((TextView) view.findViewById(R.id.conversation_lastupdate))
.setText(UIHelper.readableTimeDifference(getItem(position).getLatestMessageDate())); .setText(UIHelper.readableTimeDifference(getItem(position).getLatestMessageDate()));
@ -208,6 +220,10 @@ public class ConversationActivity extends XmppActivity {
getActionBar().setDisplayHomeAsUpEnabled(true); getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setTitle(getSelectedConversation().getName()); getActionBar().setTitle(getSelectedConversation().getName());
invalidateOptionsMenu(); invalidateOptionsMenu();
if (!getSelectedConversation().isRead()) {
getSelectedConversation().markRead();
updateConversationList();
}
} }
} }
@ -310,16 +326,6 @@ public class ConversationActivity extends XmppActivity {
} }
} }
/*@Override
protected void onPause() {
super.onPause();
if (xmppConnectionServiceBound) {
xmppConnectionService.removeOnConversationListChangedListener();
unbindService(mConnection);
xmppConnectionServiceBound = false;
}
}*/
@Override @Override
protected void onStop() { protected void onStop() {
Log.d("gultsch","called on stop in conversation activity"); Log.d("gultsch","called on stop in conversation activity");
@ -346,10 +352,6 @@ public class ConversationActivity extends XmppActivity {
conversationList.addAll(xmppConnectionService conversationList.addAll(xmppConnectionService
.getConversations()); .getConversations());
for(Conversation conversation : conversationList) {
conversation.setMessages(xmppConnectionService.getMessages(conversation));
}
this.updateConversationList(); this.updateConversationList();
} }

View File

@ -180,6 +180,10 @@ public class ConversationFragment extends Fragment {
activity.getActionBar().setDisplayHomeAsUpEnabled(true); activity.getActionBar().setDisplayHomeAsUpEnabled(true);
activity.getActionBar().setTitle(conversation.getName()); activity.getActionBar().setTitle(conversation.getName());
activity.invalidateOptionsMenu(); activity.invalidateOptionsMenu();
if (!conversation.isRead()) {
conversation.markRead();
activity.updateConversationList();
}
} }
} }
} }
@ -194,6 +198,10 @@ public class ConversationFragment extends Fragment {
activity.getActionBar().setDisplayHomeAsUpEnabled(true); activity.getActionBar().setDisplayHomeAsUpEnabled(true);
activity.getActionBar().setTitle(conversation.getName()); activity.getActionBar().setTitle(conversation.getName());
activity.invalidateOptionsMenu(); activity.invalidateOptionsMenu();
if (!conversation.isRead()) {
conversation.markRead();
activity.updateConversationList();
}
} }
} }

View File

@ -4,39 +4,31 @@ import java.io.FileDescriptor;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.List;
import de.gultsch.chat.R; import de.gultsch.chat.R;
import de.gultsch.chat.entities.Contact; import de.gultsch.chat.entities.Contact;
import de.gultsch.chat.entities.Conversation; import de.gultsch.chat.entities.Conversation;
import de.gultsch.chat.entities.Message;
import de.gultsch.chat.ui.ConversationActivity; import de.gultsch.chat.ui.ConversationActivity;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification; import android.app.Notification;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.AssetFileDescriptor;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.Rect; import android.graphics.Rect;
import android.net.Uri; import android.net.Uri;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds;
import android.provider.ContactsContract.Contacts; import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Intents;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder; import android.support.v4.app.TaskStackBuilder;
import android.util.DisplayMetrics;
import android.util.Log; import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.QuickContactBadge; import android.widget.QuickContactBadge;
public class UIHelper { public class UIHelper {
@ -100,7 +92,25 @@ public class UIHelper {
.getName(), (int) res .getName(), (int) res
.getDimension(android.R.dimen.notification_large_icon_width))); .getDimension(android.R.dimen.notification_large_icon_width)));
mBuilder.setContentTitle(conversation.getName()); mBuilder.setContentTitle(conversation.getName());
mBuilder.setContentText(conversation.getLatestMessage()); mBuilder.setTicker(conversation.getLatestMessage().trim());
StringBuilder bigText = new StringBuilder();
List<Message> messages = conversation.getMessages();
String firstLine = "";
for(int i = messages.size() -1; i >= 0; --i) {
if (!messages.get(i).isRead()) {
if (i == messages.size() -1 ) {
firstLine = messages.get(i).getBody().trim();
bigText.append(firstLine);
} else {
firstLine = messages.get(i).getBody().trim();
bigText.insert(0, firstLine+"\n");
}
} else {
break;
}
}
mBuilder.setContentText(firstLine);
mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(bigText.toString()));
mBuilder.setSmallIcon(R.drawable.notification); mBuilder.setSmallIcon(R.drawable.notification);
mBuilder.setLights(0xffffffff, 2000, 4000); mBuilder.setLights(0xffffffff, 2000, 4000);
if (ringtone != null) { if (ringtone != null) {