fixed #239 - show contact names and pictures in muc if real jid is known

This commit is contained in:
iNPUTmice 2014-07-29 14:42:17 +02:00
parent 861af75576
commit 1521b91b27
6 changed files with 230 additions and 133 deletions

View File

@ -36,6 +36,7 @@ public class Message extends AbstractEntity {
public static String CONVERSATION = "conversationUuid";
public static String COUNTERPART = "counterpart";
public static String TRUE_COUNTERPART = "trueCounterpart";
public static String BODY = "body";
public static String TIME_SENT = "timeSent";
public static String ENCRYPTION = "encryption";
@ -44,6 +45,7 @@ public class Message extends AbstractEntity {
protected String conversationUuid;
protected String counterpart;
protected String trueCounterpart;
protected String body;
protected String encryptedBody;
protected long timeSent;
@ -62,21 +64,22 @@ public class Message extends AbstractEntity {
public Message(Conversation conversation, String body, int encryption) {
this(java.util.UUID.randomUUID().toString(), conversation.getUuid(),
conversation.getContactJid(), body, System.currentTimeMillis(), encryption,
conversation.getContactJid(), null, body, System.currentTimeMillis(), encryption,
Message.STATUS_UNSEND,TYPE_TEXT);
this.conversation = conversation;
}
public Message(Conversation conversation, String counterpart, String body, int encryption, int status) {
this(java.util.UUID.randomUUID().toString(), conversation.getUuid(),counterpart, body, System.currentTimeMillis(), encryption,status,TYPE_TEXT);
this(java.util.UUID.randomUUID().toString(), conversation.getUuid(),counterpart, null, body, System.currentTimeMillis(), encryption,status,TYPE_TEXT);
this.conversation = conversation;
}
public Message(String uuid, String conversationUUid, String counterpart,
public Message(String uuid, String conversationUUid, String counterpart, String trueCounterpart,
String body, long timeSent, int encryption, int status, int type) {
this.uuid = uuid;
this.conversationUuid = conversationUUid;
this.counterpart = counterpart;
this.trueCounterpart = trueCounterpart;
this.body = body;
this.timeSent = timeSent;
this.encryption = encryption;
@ -90,6 +93,7 @@ public class Message extends AbstractEntity {
values.put(UUID, uuid);
values.put(CONVERSATION, conversationUuid);
values.put(COUNTERPART, counterpart);
values.put(TRUE_COUNTERPART,trueCounterpart);
values.put(BODY, body);
values.put(TIME_SENT, timeSent);
values.put(ENCRYPTION, encryption);
@ -110,6 +114,24 @@ public class Message extends AbstractEntity {
return counterpart;
}
public Contact getContact() {
if (this.conversation.getMode() == Conversation.MODE_SINGLE) {
return this.conversation.getContact();
} else {
if (this.trueCounterpart == null) {
return null;
} else {
Account account = this.conversation.getAccount();
Contact contact = account.getRoster().getContact(this.trueCounterpart);
if (contact.showInRoster()) {
return contact;
} else {
return null;
}
}
}
}
public String getBody() {
return body;
}
@ -144,6 +166,7 @@ public class Message extends AbstractEntity {
return new Message(cursor.getString(cursor.getColumnIndex(UUID)),
cursor.getString(cursor.getColumnIndex(CONVERSATION)),
cursor.getString(cursor.getColumnIndex(COUNTERPART)),
cursor.getString(cursor.getColumnIndex(TRUE_COUNTERPART)),
cursor.getString(cursor.getColumnIndex(BODY)),
cursor.getLong(cursor.getColumnIndex(TIME_SENT)),
cursor.getInt(cursor.getColumnIndex(ENCRYPTION)),
@ -207,6 +230,10 @@ public class Message extends AbstractEntity {
}
}
public void setTrueCounterpart(String trueCounterpart) {
this.trueCounterpart = trueCounterpart;
}
public String getPresence() {
String[] counterparts = this.counterpart.split("/");
if (counterparts.length == 2) {

View File

@ -33,18 +33,29 @@ public class MucOptions {
private int role;
private int affiliation;
private String name;
private String jid;
private long pgpKeyId = 0;
public String getName() {
return name;
}
public void setName(String user) {
this.name = user;
}
public void setJid(String jid) {
this.jid = jid;
}
public String getJid() {
return this.jid;
}
public int getRole() {
return this.role;
}
public void setRole(String role) {
role = role.toLowerCase();
if (role.equals("moderator")) {
@ -57,9 +68,11 @@ public class MucOptions {
this.role = ROLE_NONE;
}
}
public int getAffiliation() {
return this.affiliation;
}
public void setAffiliation(String affiliation) {
if (affiliation.equalsIgnoreCase("admin")) {
this.affiliation = AFFILIATION_ADMIN;
@ -73,6 +86,7 @@ public class MucOptions {
this.affiliation = AFFILIATION_NONE;
}
}
public void setPgpKeyId(long id) {
this.pgpKeyId = id;
}
@ -81,6 +95,7 @@ public class MucOptions {
return this.pgpKeyId;
}
}
private Account account;
private List<User> users = new CopyOnWriteArrayList<User>();
private Conversation conversation;
@ -97,7 +112,7 @@ public class MucOptions {
}
public void deleteUser(String name) {
for(int i = 0; i < users.size(); ++i) {
for (int i = 0; i < users.size(); ++i) {
if (users.get(i).getName().equals(name)) {
users.remove(i);
return;
@ -106,7 +121,7 @@ public class MucOptions {
}
public void addUser(User user) {
for(int i = 0; i < users.size(); ++i) {
for (int i = 0; i < users.size(); ++i) {
if (users.get(i).getName().equals(user.getName())) {
users.set(i, user);
return;
@ -117,22 +132,25 @@ public class MucOptions {
public void processPacket(PresencePacket packet, PgpEngine pgp) {
String[] fromParts = packet.getFrom().split("/");
if (fromParts.length>=2) {
if (fromParts.length >= 2) {
String name = fromParts[1];
String type = packet.getAttribute("type");
if (type==null) {
if (type == null) {
User user = new User();
Element item = packet.findChild("x","http://jabber.org/protocol/muc#user").findChild("item");
Element item = packet.findChild("x",
"http://jabber.org/protocol/muc#user")
.findChild("item");
user.setName(name);
user.setAffiliation(item.getAttribute("affiliation"));
user.setRole(item.getAttribute("role"));
user.setJid(item.getAttribute("jid"));
user.setName(name);
if (name.equals(this.joinnick)) {
this.isOnline = true;
this.error = ERROR_NO_ERROR;
self = user;
if (aboutToRename) {
if (renameListener!=null) {
if (renameListener != null) {
renameListener.onRename(true);
}
aboutToRename = false;
@ -141,8 +159,7 @@ public class MucOptions {
addUser(user);
}
if (pgp != null) {
Element x = packet.findChild("x",
"jabber:x:signed");
Element x = packet.findChild("x", "jabber:x:signed");
if (x != null) {
Element status = packet.findChild("status");
String msg;
@ -151,7 +168,8 @@ public class MucOptions {
} else {
msg = "";
}
user.setPgpKeyId(pgp.fetchKeyId(account,msg, x.getContent()));
user.setPgpKeyId(pgp.fetchKeyId(account, msg,
x.getContent()));
}
}
} else if (type.equals("unavailable")) {
@ -160,7 +178,7 @@ public class MucOptions {
Element error = packet.findChild("error");
if (error.hasChild("conflict")) {
if (aboutToRename) {
if (renameListener!=null) {
if (renameListener != null) {
renameListener.onRename(false);
}
aboutToRename = false;
@ -179,7 +197,8 @@ public class MucOptions {
public String getProposedNick() {
String[] mucParts = conversation.getContactJid().split("/");
if (conversation.getBookmark() != null && conversation.getBookmark().getNick() != null) {
if (conversation.getBookmark() != null
&& conversation.getBookmark().getNick() != null) {
return conversation.getBookmark().getNick();
} else {
if (mucParts.length == 2) {
@ -191,7 +210,7 @@ public class MucOptions {
}
public String getActualNick() {
if (this.self.getName()!=null) {
if (this.self.getName() != null) {
return this.self.getName();
} else {
return this.getProposedNick();
@ -246,21 +265,21 @@ public class MucOptions {
public long[] getPgpKeyIds() {
List<Long> ids = new ArrayList<Long>();
for(User user : getUsers()) {
if(user.getPgpKeyId()!=0) {
for (User user : getUsers()) {
if (user.getPgpKeyId() != 0) {
ids.add(user.getPgpKeyId());
}
}
long[] primitivLongArray = new long[ids.size()];
for(int i = 0; i < ids.size(); ++i) {
for (int i = 0; i < ids.size(); ++i) {
primitivLongArray[i] = ids.get(i);
}
return primitivLongArray;
}
public boolean pgpKeysInUse() {
for(User user : getUsers()) {
if (user.getPgpKeyId()!=0) {
for (User user : getUsers()) {
if (user.getPgpKeyId() != 0) {
return true;
}
}
@ -268,8 +287,8 @@ public class MucOptions {
}
public boolean everybodyHasKeys() {
for(User user : getUsers()) {
if (user.getPgpKeyId()==0) {
for (User user : getUsers()) {
if (user.getPgpKeyId() == 0) {
return false;
}
}
@ -277,6 +296,16 @@ public class MucOptions {
}
public String getJoinJid() {
return this.conversation.getContactJid().split("/")[0]+"/"+this.joinnick;
return this.conversation.getContactJid().split("/")[0] + "/"
+ this.joinnick;
}
public String getTrueCounterpart(String counterpart) {
for(User user : this.getUsers()) {
if (user.getName().equals(counterpart)) {
return user.getJid();
}
}
return null;
}
}

View File

@ -146,6 +146,9 @@ public class MessageParser extends AbstractParser implements
Message.ENCRYPTION_PGP, status);
}
finishedMessage.setTime(getTimestamp(packet));
if (status == Message.STATUS_RECIEVED) {
finishedMessage.setTrueCounterpart(conversation.getMucOptions().getTrueCounterpart(counterPart));
}
return finishedMessage;
}

View File

@ -20,15 +20,17 @@ public class DatabaseBackend extends SQLiteOpenHelper {
private static DatabaseBackend instance = null;
private static final String DATABASE_NAME = "history";
private static final int DATABASE_VERSION = 5;
private static final int DATABASE_VERSION = 6;
private static String CREATE_CONTATCS_STATEMENT = "create table "
+ Contact.TABLENAME + "(" + Contact.ACCOUNT + " TEXT, " + Contact.SERVERNAME + " TEXT, "
+ Contact.SYSTEMNAME + " TEXT," + Contact.JID + " TEXT,"
+ Contact.KEYS + " TEXT," + Contact.PHOTOURI + " TEXT,"
+ Contact.OPTIONS + " NUMBER," + Contact.SYSTEMACCOUNT
+ " NUMBER, " + "FOREIGN KEY(" + Contact.ACCOUNT + ") REFERENCES "
+ Account.TABLENAME + "(" + Account.UUID + ") ON DELETE CASCADE, UNIQUE("+Contact.ACCOUNT+", "+Contact.JID+") ON CONFLICT REPLACE);";
+ Contact.TABLENAME + "(" + Contact.ACCOUNT + " TEXT, "
+ Contact.SERVERNAME + " TEXT, " + Contact.SYSTEMNAME + " TEXT,"
+ Contact.JID + " TEXT," + Contact.KEYS + " TEXT,"
+ Contact.PHOTOURI + " TEXT," + Contact.OPTIONS + " NUMBER,"
+ Contact.SYSTEMACCOUNT + " NUMBER, " + "FOREIGN KEY("
+ Contact.ACCOUNT + ") REFERENCES " + Account.TABLENAME + "("
+ Account.UUID + ") ON DELETE CASCADE, UNIQUE(" + Contact.ACCOUNT
+ ", " + Contact.JID + ") ON CONFLICT REPLACE);";
public DatabaseBackend(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
@ -54,8 +56,9 @@ public class DatabaseBackend extends SQLiteOpenHelper {
db.execSQL("create table " + Message.TABLENAME + "( " + Message.UUID
+ " TEXT PRIMARY KEY, " + Message.CONVERSATION + " TEXT, "
+ Message.TIME_SENT + " NUMBER, " + Message.COUNTERPART
+ " TEXT, " + Message.BODY + " TEXT, " + Message.ENCRYPTION
+ " NUMBER, " + Message.STATUS + " NUMBER," + Message.TYPE
+ " TEXT, " + Message.TRUE_COUNTERPART + " TEXT,"
+ Message.BODY + " TEXT, " + Message.ENCRYPTION + " NUMBER, "
+ Message.STATUS + " NUMBER," + Message.TYPE
+ " NUMBER, FOREIGN KEY(" + Message.CONVERSATION
+ ") REFERENCES " + Conversation.TABLENAME + "("
+ Conversation.UUID + ") ON DELETE CASCADE);");
@ -74,9 +77,14 @@ public class DatabaseBackend extends SQLiteOpenHelper {
+ Message.TYPE + " NUMBER");
}
if (oldVersion < 5 && newVersion >= 5) {
db.execSQL("DROP TABLE "+Contact.TABLENAME);
db.execSQL("DROP TABLE " + Contact.TABLENAME);
db.execSQL(CREATE_CONTATCS_STATEMENT);
db.execSQL("UPDATE "+Account.TABLENAME+ " SET "+Account.ROSTERVERSION+" = NULL");
db.execSQL("UPDATE " + Account.TABLENAME + " SET "
+ Account.ROSTERVERSION + " = NULL");
}
if (oldVersion < 6 && newVersion >= 6) {
db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN "
+ Message.TRUE_COUNTERPART + " TEXT");
}
}
@ -129,23 +137,26 @@ public class DatabaseBackend extends SQLiteOpenHelper {
return list;
}
public CopyOnWriteArrayList<Message> getMessages(Conversation conversations, int limit) {
return getMessages(conversations, limit,-1);
public CopyOnWriteArrayList<Message> getMessages(
Conversation conversations, int limit) {
return getMessages(conversations, limit, -1);
}
public CopyOnWriteArrayList<Message> getMessages(Conversation conversation, int limit, long timestamp) {
public CopyOnWriteArrayList<Message> getMessages(Conversation conversation,
int limit, long timestamp) {
CopyOnWriteArrayList<Message> list = new CopyOnWriteArrayList<Message>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor;
if (timestamp==-1) {
if (timestamp == -1) {
String[] selectionArgs = { conversation.getUuid() };
cursor = db.query(Message.TABLENAME, null, Message.CONVERSATION
+ "=?", selectionArgs, null, null, Message.TIME_SENT + " DESC",
String.valueOf(limit));
+ "=?", selectionArgs, null, null, Message.TIME_SENT
+ " DESC", String.valueOf(limit));
} else {
String[] selectionArgs = { conversation.getUuid() , ""+timestamp};
String[] selectionArgs = { conversation.getUuid(), "" + timestamp };
cursor = db.query(Message.TABLENAME, null, Message.CONVERSATION
+ "=? and "+Message.TIME_SENT+"<?", selectionArgs, null, null, Message.TIME_SENT + " DESC",
+ "=? and " + Message.TIME_SENT + "<?", selectionArgs,
null, null, Message.TIME_SENT + " DESC",
String.valueOf(limit));
}
if (cursor.getCount() > 0) {
@ -229,12 +240,12 @@ public class DatabaseBackend extends SQLiteOpenHelper {
public void writeRoster(Roster roster) {
Account account = roster.getAccount();
SQLiteDatabase db = this.getWritableDatabase();
for(Contact contact : roster.getContacts()) {
for (Contact contact : roster.getContacts()) {
if (contact.getOption(Contact.Options.IN_ROSTER)) {
db.insert(Contact.TABLENAME, null, contact.getContentValues());
} else {
String where = Contact.ACCOUNT + "=? AND "+Contact.JID+"=?";
String[] whereArgs = {account.getUuid(), contact.getJid()};
String where = Contact.ACCOUNT + "=? AND " + Contact.JID + "=?";
String[] whereArgs = { account.getUuid(), contact.getJid() };
db.delete(Contact.TABLENAME, where, whereArgs);
}
}

View File

@ -7,6 +7,8 @@ import org.openintents.openpgp.util.OpenPgpUtils;
import eu.siacs.conversations.R;
import eu.siacs.conversations.crypto.PgpEngine;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.MucOptions;
import eu.siacs.conversations.entities.MucOptions.OnRenameListener;
@ -150,7 +152,8 @@ public class ConferenceDetailsActivity extends XmppActivity {
this.uuid = getIntent().getExtras().getString("uuid");
}
if (uuid != null) {
this.conversation = xmppConnectionService.findConversationByUuid(uuid);
this.conversation = xmppConnectionService
.findConversationByUuid(uuid);
if (this.conversation != null) {
populateView();
}
@ -179,7 +182,8 @@ public class ConferenceDetailsActivity extends XmppActivity {
public void run() {
populateView();
if (success) {
Toast.makeText(ConferenceDetailsActivity.this,
Toast.makeText(
ConferenceDetailsActivity.this,
getString(R.string.your_nick_has_been_changed),
Toast.LENGTH_SHORT).show();
} else {
@ -222,28 +226,42 @@ public class ConferenceDetailsActivity extends XmppActivity {
this.users.addAll(conversation.getMucOptions().getUsers());
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
membersView.removeAllViews();
for (final User contact : conversation.getMucOptions().getUsers()) {
View view = (View) inflater.inflate(R.layout.contact, null);
TextView displayName = (TextView) view
Account account = conversation.getAccount();
for (final User user : conversation.getMucOptions().getUsers()) {
View view = (View) inflater.inflate(R.layout.contact, membersView,
false);
TextView name = (TextView) view
.findViewById(R.id.contact_display_name);
TextView key = (TextView) view.findViewById(R.id.key);
displayName.setText(contact.getName());
TextView role = (TextView) view.findViewById(R.id.contact_jid);
role.setText(getReadableRole(contact.getRole()));
if (contact.getPgpKeyId() != 0) {
role.setText(getReadableRole(user.getRole()));
if (user.getPgpKeyId() != 0) {
key.setVisibility(View.VISIBLE);
key.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
viewPgpKey(contact);
viewPgpKey(user);
}
});
key.setText(OpenPgpUtils.convertKeyIdToHex(contact
.getPgpKeyId()));
key.setText(OpenPgpUtils.convertKeyIdToHex(user.getPgpKeyId()));
}
Bitmap bm = UIHelper.getContactPicture(contact.getName(), 48, this,
Bitmap bm;
if (user.getJid() != null) {
Contact contact = account.getRoster().getContact(user.getJid());
if (contact.showInRoster()) {
bm = contact.getImage(48, this);
name.setText(contact.getDisplayName());
} else {
bm = UIHelper.getContactPicture(user.getName(), 48, this,
false);
name.setText(user.getName());
}
} else {
bm = UIHelper
.getContactPicture(user.getName(), 48, this, false);
name.setText(user.getName());
}
ImageView iv = (ImageView) view.findViewById(R.id.contact_photo);
iv.setImageBitmap(bm);
membersView.addView(view);

View File

@ -42,17 +42,12 @@ public class MessageAdapter extends ArrayAdapter<Message> {
private BitmapCache mBitmapCache = new BitmapCache();
private DisplayMetrics metrics;
private boolean useSubject = true;
private OnContactPictureClicked mOnContactPictureClickedListener;
public MessageAdapter(ConversationActivity activity, List<Message> messages) {
super(activity, 0, messages);
this.activity = activity;
metrics = getContext().getResources().getDisplayMetrics();
SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(getContext());
useSubject = preferences.getBoolean("use_subject_in_muc", true);
}
private Bitmap getSelfBitmap() {
@ -130,8 +125,13 @@ public class MessageAdapter extends ArrayAdapter<Message> {
error = true;
default:
if (multiReceived) {
Contact contact = message.getContact();
if (contact != null) {
info = contact.getDisplayName();
} else {
info = message.getCounterpart();
}
}
break;
}
if (error) {
@ -277,7 +277,7 @@ public class MessageAdapter extends ArrayAdapter<Message> {
switch (type) {
case SENT:
view = (View) activity.getLayoutInflater().inflate(
R.layout.message_sent, null);
R.layout.message_sent, parent,false);
viewHolder.message_box = (LinearLayout) view
.findViewById(R.id.message_box);
viewHolder.contact_picture = (ImageView) view
@ -295,7 +295,7 @@ public class MessageAdapter extends ArrayAdapter<Message> {
break;
case RECIEVED:
view = (View) activity.getLayoutInflater().inflate(
R.layout.message_recieved, null);
R.layout.message_recieved, parent,false);
viewHolder.message_box = (LinearLayout) view
.findViewById(R.id.message_box);
viewHolder.contact_picture = (ImageView) view
@ -307,9 +307,7 @@ public class MessageAdapter extends ArrayAdapter<Message> {
if (item.getConversation().getMode() == Conversation.MODE_SINGLE) {
viewHolder.contact_picture.setImageBitmap(mBitmapCache.get(
item.getConversation().getName(useSubject), item
.getConversation().getContact(),
getContext()));
item.getConversation().getContact(), getContext()));
}
viewHolder.indicator = (ImageView) view
@ -324,15 +322,13 @@ public class MessageAdapter extends ArrayAdapter<Message> {
break;
case STATUS:
view = (View) activity.getLayoutInflater().inflate(
R.layout.message_status, null);
R.layout.message_status, parent,false);
viewHolder.contact_picture = (ImageView) view
.findViewById(R.id.message_photo);
if (item.getConversation().getMode() == Conversation.MODE_SINGLE) {
viewHolder.contact_picture.setImageBitmap(mBitmapCache.get(
item.getConversation().getName(useSubject), item
.getConversation().getContact(),
getContext()));
item.getConversation().getContact(), getContext()));
viewHolder.contact_picture.setAlpha(128);
viewHolder.contact_picture
.setOnClickListener(new OnClickListener() {
@ -366,8 +362,14 @@ public class MessageAdapter extends ArrayAdapter<Message> {
if (type == RECIEVED) {
if (item.getConversation().getMode() == Conversation.MODE_MULTI) {
Contact contact = item.getContact();
if (contact != null) {
viewHolder.contact_picture.setImageBitmap(mBitmapCache.get(
item.getCounterpart(), null, getContext()));
contact, getContext()));
} else {
viewHolder.contact_picture.setImageBitmap(mBitmapCache.get(
item.getCounterpart(), getContext()));
}
viewHolder.contact_picture
.setOnClickListener(new OnClickListener() {
@ -453,20 +455,27 @@ public class MessageAdapter extends ArrayAdapter<Message> {
}
private class BitmapCache {
private HashMap<String, Bitmap> bitmaps = new HashMap<String, Bitmap>();
private HashMap<String, Bitmap> contactBitmaps = new HashMap<String, Bitmap>();
private HashMap<String, Bitmap> unknownBitmaps = new HashMap<String, Bitmap>();
public Bitmap get(String name, Contact contact, Context context) {
if (bitmaps.containsKey(name)) {
return bitmaps.get(name);
public Bitmap get(Contact contact, Context context) {
if (contactBitmaps.containsKey(contact.getJid())) {
return contactBitmaps.get(contact.getJid());
} else {
Bitmap bm;
if (contact != null) {
bm = UIHelper
.getContactPicture(contact, 48, context, false);
} else {
bm = UIHelper.getContactPicture(name, 48, context, false);
Bitmap bm = UIHelper.getContactPicture(contact, 48, context,
false);
contactBitmaps.put(contact.getJid(), bm);
return bm;
}
bitmaps.put(name, bm);
}
public Bitmap get(String name, Context context) {
if (unknownBitmaps.containsKey(name)) {
return unknownBitmaps.get(name);
} else {
Bitmap bm = UIHelper
.getContactPicture(name, 48, context, false);
unknownBitmaps.put(name, bm);
return bm;
}
}