ShareWithActivity.java

  1package eu.siacs.conversations.ui;
  2
  3import java.util.ArrayList;
  4import java.util.Collections;
  5import java.util.Comparator;
  6import java.util.HashSet;
  7import java.util.List;
  8import java.util.Set;
  9
 10import eu.siacs.conversations.R;
 11import eu.siacs.conversations.entities.Account;
 12import eu.siacs.conversations.entities.Contact;
 13import eu.siacs.conversations.entities.Conversation;
 14import eu.siacs.conversations.utils.UIHelper;
 15import android.content.Intent;
 16import android.content.SharedPreferences;
 17import android.graphics.Bitmap;
 18import android.os.Bundle;
 19import android.preference.PreferenceManager;
 20import android.util.Log;
 21import android.view.View;
 22import android.view.View.OnClickListener;
 23import android.widget.ImageView;
 24import android.widget.LinearLayout;
 25import android.widget.TextView;
 26
 27public class ShareWithActivity extends XmppActivity {
 28	
 29	private LinearLayout conversations;
 30	private LinearLayout contacts;
 31	
 32	private OnClickListener click = new OnClickListener() {
 33		
 34		@Override
 35		public void onClick(View v) {
 36			// TODO Auto-generated method stub
 37			
 38		}
 39	};
 40	
 41	@Override
 42	protected void onCreate(Bundle savedInstanceState) {
 43
 44		super.onCreate(savedInstanceState);
 45
 46		setContentView(R.layout.share_with);
 47		setTitle("Share with Conversation");
 48		
 49		contacts = (LinearLayout) findViewById(R.id.contacts);
 50		conversations = (LinearLayout) findViewById(R.id.conversations);
 51		
 52	}
 53	
 54	
 55	public View createContactView(String name, String msgTxt, Bitmap bm) {
 56		View view = (View) getLayoutInflater().inflate(R.layout.contact, null);
 57		view.setBackgroundResource(R.drawable.greybackground);
 58		TextView contactName =(TextView) view.findViewById(R.id.contact_display_name);
 59		contactName.setText(name);
 60		TextView msg = (TextView) view.findViewById(R.id.contact_jid);
 61		msg.setText(msgTxt);
 62		ImageView imageView = (ImageView) view.findViewById(R.id.contact_photo);
 63		imageView.setImageBitmap(bm);
 64		return view;
 65	}
 66	
 67	
 68	
 69	@Override
 70	void onBackendConnected() {
 71		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
 72		boolean useSubject = preferences.getBoolean("use_subject_in_muc", true);
 73		
 74		Set<String> displayedContacts = new HashSet<String>();
 75		conversations.removeAllViews();
 76		List<Conversation> convList = xmppConnectionService.getConversations();
 77		Collections.sort(convList, new Comparator<Conversation>() {
 78			@Override
 79			public int compare(Conversation lhs, Conversation rhs) {
 80				return (int) (rhs.getLatestMessage().getTimeSent() - lhs.getLatestMessage().getTimeSent());
 81			}
 82		});
 83		for(final Conversation conversation : convList) {
 84			View view = createContactView(conversation.getName(useSubject),
 85					conversation.getLatestMessage().getBody().trim(),
 86					UIHelper.getContactPicture(conversation, 90,this.getApplicationContext()));
 87			view.setOnClickListener(new OnClickListener() {
 88				
 89				@Override
 90				public void onClick(View v) {
 91					 String sharedText = getIntent().getStringExtra(Intent.EXTRA_TEXT);
 92					 switchToConversation(conversation, sharedText);
 93					 finish();
 94				}
 95			});
 96			conversations.addView(view);
 97			if (conversation.getContact() != null) {
 98				displayedContacts.add(conversation.getContact().getUuid());
 99			}
100		}
101		contacts.removeAllViews();
102		final List<Contact> contactsList = new ArrayList<Contact>();
103		for(Account account : xmppConnectionService.getAccounts()) {
104			for(final Contact contact : xmppConnectionService.getRoster(account)) {
105				if (!displayedContacts.contains(contact.getUuid())) {
106					contactsList.add(contact);
107				}
108			}
109		}
110		
111		Collections.sort(contactsList, new Comparator<Contact>() {
112			@Override
113			public int compare(Contact lhs, Contact rhs) {
114				return lhs.getDisplayName().compareToIgnoreCase(rhs.getDisplayName());
115			}
116		});
117		
118		for(int i = 0; i < contactsList.size(); ++i) {
119			final Contact con = contactsList.get(i);
120			View view = createContactView(con.getDisplayName(), con.getJid(), 
121					UIHelper.getContactPicture(con, 90, this.getApplicationContext()));
122			view.setOnClickListener(new OnClickListener() {
123				
124				@Override
125				public void onClick(View v) {
126					 String sharedText = getIntent().getStringExtra(Intent.EXTRA_TEXT);
127					 Conversation conversation = xmppConnectionService.findOrCreateConversation(con.getAccount(), con.getJid(), false);
128					 switchToConversation(conversation, sharedText);
129					 finish();
130				}
131			});
132			contacts.addView(view);
133		}
134	}
135
136}