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.entities.Message;
 15import eu.siacs.conversations.utils.UIHelper;
 16import android.app.PendingIntent;
 17import android.content.Intent;
 18import android.content.SharedPreferences;
 19import android.graphics.Bitmap;
 20import android.net.Uri;
 21import android.os.Bundle;
 22import android.preference.PreferenceManager;
 23import android.view.View;
 24import android.view.View.OnClickListener;
 25import android.widget.ImageView;
 26import android.widget.LinearLayout;
 27import android.widget.TextView;
 28import android.widget.Toast;
 29
 30public class ShareWithActivity extends XmppActivity {
 31
 32	private LinearLayout conversations;
 33	private LinearLayout contacts;
 34	private boolean isImage = false;
 35
 36	private UiCallback<Message> attachImageCallback = new UiCallback<Message>() {
 37
 38		@Override
 39		public void userInputRequried(PendingIntent pi, Message object) {
 40			// TODO Auto-generated method stub
 41
 42		}
 43
 44		@Override
 45		public void success(Message message) {
 46			xmppConnectionService.sendMessage(message);
 47		}
 48
 49		@Override
 50		public void error(int errorCode, Message object) {
 51			// TODO Auto-generated method stub
 52
 53		}
 54	};
 55
 56	@Override
 57	protected void onCreate(Bundle savedInstanceState) {
 58
 59		super.onCreate(savedInstanceState);
 60
 61		setContentView(R.layout.share_with);
 62		setTitle(getString(R.string.title_activity_sharewith));
 63
 64		contacts = (LinearLayout) findViewById(R.id.contacts);
 65		conversations = (LinearLayout) findViewById(R.id.conversations);
 66
 67	}
 68
 69	public View createContactView(String name, String msgTxt, Bitmap bm) {
 70		View view = (View) getLayoutInflater().inflate(R.layout.contact, null);
 71		view.setBackgroundResource(R.drawable.greybackground);
 72		TextView contactName = (TextView) view
 73				.findViewById(R.id.contact_display_name);
 74		contactName.setText(name);
 75		TextView msg = (TextView) view.findViewById(R.id.contact_jid);
 76		msg.setText(msgTxt);
 77		ImageView imageView = (ImageView) view.findViewById(R.id.contact_photo);
 78		imageView.setImageBitmap(bm);
 79		return view;
 80	}
 81
 82	@Override
 83	void onBackendConnected() {
 84		this.isImage = (getIntent().getType() != null && getIntent().getType()
 85				.startsWith("image/"));
 86		SharedPreferences preferences = PreferenceManager
 87				.getDefaultSharedPreferences(this);
 88		boolean useSubject = preferences.getBoolean("use_subject_in_muc", true);
 89
 90		Set<Contact> displayedContacts = new HashSet<Contact>();
 91		conversations.removeAllViews();
 92		List<Conversation> convList = new ArrayList<Conversation>();
 93		xmppConnectionService.populateWithOrderedConversations(convList);
 94		Collections.sort(convList, new Comparator<Conversation>() {
 95			@Override
 96			public int compare(Conversation lhs, Conversation rhs) {
 97				return (int) (rhs.getLatestMessage().getTimeSent() - lhs
 98						.getLatestMessage().getTimeSent());
 99			}
100		});
101		for (final Conversation conversation : convList) {
102			if (!isImage || conversation.getMode() == Conversation.MODE_SINGLE) {
103				View view = createContactView(
104						conversation.getName(useSubject),
105						conversation.getLatestMessage().getBody().trim(),
106						UIHelper.getContactPicture(conversation, 48,
107								this.getApplicationContext(), false));
108				view.setOnClickListener(new OnClickListener() {
109
110					@Override
111					public void onClick(View v) {
112						share(conversation);
113					}
114				});
115				conversations.addView(view);
116				displayedContacts.add(conversation.getContact());
117			}
118		}
119		contacts.removeAllViews();
120		List<Contact> contactsList = new ArrayList<Contact>();
121		for (Account account : xmppConnectionService.getAccounts()) {
122			for (Contact contact : account.getRoster().getContacts()) {
123				if (!displayedContacts.contains(contact)
124						&& (contact.showInRoster())) {
125					contactsList.add(contact);
126				}
127			}
128		}
129
130		Collections.sort(contactsList, new Comparator<Contact>() {
131			@Override
132			public int compare(Contact lhs, Contact rhs) {
133				return lhs.getDisplayName().compareToIgnoreCase(
134						rhs.getDisplayName());
135			}
136		});
137
138		for (int i = 0; i < contactsList.size(); ++i) {
139			final Contact con = contactsList.get(i);
140			View view = createContactView(
141					con.getDisplayName(),
142					con.getJid(),
143					UIHelper.getContactPicture(con, 48,
144							this.getApplicationContext(), false));
145			view.setOnClickListener(new OnClickListener() {
146
147				@Override
148				public void onClick(View v) {
149					Conversation conversation = xmppConnectionService
150							.findOrCreateConversation(con.getAccount(),
151									con.getJid(), false);
152					share(conversation);
153				}
154			});
155			contacts.addView(view);
156		}
157	}
158
159	private void share(final Conversation conversation) {
160		String sharedText = null;
161		if (isImage) {
162			final Uri uri = (Uri) getIntent().getParcelableExtra(
163					Intent.EXTRA_STREAM);
164			selectPresence(conversation, new OnPresenceSelected() {
165				@Override
166				public void onPresenceSelected() {
167					Toast.makeText(getApplicationContext(),
168							getText(R.string.preparing_image),
169							Toast.LENGTH_LONG).show();
170					ShareWithActivity.this.xmppConnectionService
171							.attachImageToConversation(conversation, uri,
172									attachImageCallback);
173					switchToConversation(conversation, null, true);
174					finish();
175				}
176			});
177
178		} else {
179			sharedText = getIntent().getStringExtra(Intent.EXTRA_TEXT);
180			switchToConversation(conversation, sharedText, true);
181			finish();
182		}
183
184	}
185
186}