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.net.Uri;
 19import android.os.Bundle;
 20import android.preference.PreferenceManager;
 21import android.util.Log;
 22import android.view.View;
 23import android.view.View.OnClickListener;
 24import android.widget.ImageView;
 25import android.widget.LinearLayout;
 26import android.widget.TextView;
 27
 28public class ShareWithActivity extends XmppActivity {
 29
 30	private LinearLayout conversations;
 31	private LinearLayout contacts;
 32
 33	@Override
 34	protected void onCreate(Bundle savedInstanceState) {
 35
 36		super.onCreate(savedInstanceState);
 37
 38		setContentView(R.layout.share_with);
 39		setTitle(getString(R.string.title_activity_sharewith));
 40
 41		contacts = (LinearLayout) findViewById(R.id.contacts);
 42		conversations = (LinearLayout) findViewById(R.id.conversations);
 43
 44	}
 45
 46	public View createContactView(String name, String msgTxt, Bitmap bm) {
 47		View view = (View) getLayoutInflater().inflate(R.layout.contact, null);
 48		view.setBackgroundResource(R.drawable.greybackground);
 49		TextView contactName = (TextView) view
 50				.findViewById(R.id.contact_display_name);
 51		contactName.setText(name);
 52		TextView msg = (TextView) view.findViewById(R.id.contact_jid);
 53		msg.setText(msgTxt);
 54		ImageView imageView = (ImageView) view.findViewById(R.id.contact_photo);
 55		imageView.setImageBitmap(bm);
 56		return view;
 57	}
 58
 59	@Override
 60	void onBackendConnected() {
 61		final boolean isImage = (getIntent().getType() != null && getIntent()
 62				.getType().startsWith("image/"));
 63		SharedPreferences preferences = PreferenceManager
 64				.getDefaultSharedPreferences(this);
 65		boolean useSubject = preferences.getBoolean("use_subject_in_muc", true);
 66
 67		Set<Contact> displayedContacts = new HashSet<Contact>();
 68		conversations.removeAllViews();
 69		List<Conversation> convList = xmppConnectionService.getConversations();
 70		Collections.sort(convList, new Comparator<Conversation>() {
 71			@Override
 72			public int compare(Conversation lhs, Conversation rhs) {
 73				return (int) (rhs.getLatestMessage().getTimeSent() - lhs
 74						.getLatestMessage().getTimeSent());
 75			}
 76		});
 77		for (final Conversation conversation : convList) {
 78			if (!isImage || conversation.getMode() == Conversation.MODE_SINGLE) {
 79				View view = createContactView(
 80						conversation.getName(useSubject),
 81						conversation.getLatestMessage().getBody().trim(),
 82						UIHelper.getContactPicture(conversation, 48,
 83								this.getApplicationContext(), false));
 84				view.setOnClickListener(new OnClickListener() {
 85
 86					@Override
 87					public void onClick(View v) {
 88						String sharedText = null;
 89						if (isImage) {
 90							Uri uri = (Uri) getIntent().getParcelableExtra(Intent.EXTRA_STREAM);
 91							Log.d(LOGTAG,uri.toString());
 92						} else {
 93							sharedText = getIntent().getStringExtra(
 94								Intent.EXTRA_TEXT);
 95						}
 96						switchToConversation(conversation, sharedText, true);
 97						finish();
 98					}
 99				});
100				conversations.addView(view);
101				displayedContacts.add(conversation.getContact());
102			}
103		}
104		contacts.removeAllViews();
105		List<Contact> contactsList = new ArrayList<Contact>();
106		for (Account account : xmppConnectionService.getAccounts()) {
107			for (Contact contact : account.getRoster().getContacts()) {
108				if (!displayedContacts.contains(contact)
109						&& (contact.showInRoster())) {
110					contactsList.add(contact);
111				}
112			}
113		}
114
115		Collections.sort(contactsList, new Comparator<Contact>() {
116			@Override
117			public int compare(Contact lhs, Contact rhs) {
118				return lhs.getDisplayName().compareToIgnoreCase(
119						rhs.getDisplayName());
120			}
121		});
122
123		for (int i = 0; i < contactsList.size(); ++i) {
124			final Contact con = contactsList.get(i);
125			View view = createContactView(
126					con.getDisplayName(),
127					con.getJid(),
128					UIHelper.getContactPicture(con, 48,
129							this.getApplicationContext(), false));
130			view.setOnClickListener(new OnClickListener() {
131
132				@Override
133				public void onClick(View v) {
134					String sharedText = getIntent().getStringExtra(
135							Intent.EXTRA_TEXT);
136					Conversation conversation = xmppConnectionService
137							.findOrCreateConversation(con.getAccount(),
138									con.getJid(), false);
139					switchToConversation(conversation, sharedText, true);
140					finish();
141				}
142			});
143			contacts.addView(view);
144		}
145	}
146
147}