ConferenceDetailsActivity.java

  1package eu.siacs.conversations.ui;
  2
  3import android.annotation.TargetApi;
  4import android.app.PendingIntent;
  5import android.content.Context;
  6import android.content.IntentSender.SendIntentException;
  7import android.graphics.Bitmap;
  8import android.os.Build;
  9import android.os.Bundle;
 10import android.view.ContextMenu;
 11import android.view.LayoutInflater;
 12import android.view.Menu;
 13import android.view.MenuItem;
 14import android.view.View;
 15import android.view.View.OnClickListener;
 16import android.widget.Button;
 17import android.widget.ImageButton;
 18import android.widget.ImageView;
 19import android.widget.LinearLayout;
 20import android.widget.TextView;
 21import android.widget.Toast;
 22
 23import org.openintents.openpgp.util.OpenPgpUtils;
 24
 25import java.util.ArrayList;
 26import java.util.List;
 27
 28import eu.siacs.conversations.R;
 29import eu.siacs.conversations.crypto.PgpEngine;
 30import eu.siacs.conversations.entities.Account;
 31import eu.siacs.conversations.entities.Bookmark;
 32import eu.siacs.conversations.entities.Contact;
 33import eu.siacs.conversations.entities.Conversation;
 34import eu.siacs.conversations.entities.MucOptions.User;
 35import eu.siacs.conversations.services.XmppConnectionService.OnMucRosterUpdate;
 36import eu.siacs.conversations.services.XmppConnectionService.OnConversationUpdate;
 37import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
 38
 39public class ConferenceDetailsActivity extends XmppActivity implements OnConversationUpdate, OnMucRosterUpdate {
 40	public static final String ACTION_VIEW_MUC = "view_muc";
 41	private Conversation mConversation;
 42	private OnClickListener inviteListener = new OnClickListener() {
 43
 44		@Override
 45		public void onClick(View v) {
 46			inviteToConversation(mConversation);
 47		}
 48	};
 49	private TextView mYourNick;
 50	private ImageView mYourPhoto;
 51	private ImageButton mEditNickButton;
 52	private TextView mRoleAffiliaton;
 53	private TextView mFullJid;
 54	private TextView mAccountJid;
 55	private LinearLayout membersView;
 56	private LinearLayout mMoreDetails;
 57	private Button mInviteButton;
 58	private String uuid = null;
 59	private List<User> users = new ArrayList<>();
 60	private User mSelectedUser = null;
 61
 62	private UiCallback<Conversation> renameCallback = new UiCallback<Conversation>() {
 63		@Override
 64		public void success(Conversation object) {
 65			runOnUiThread(new Runnable() {
 66				@Override
 67				public void run() {
 68					Toast.makeText(ConferenceDetailsActivity.this,getString(R.string.your_nick_has_been_changed),Toast.LENGTH_SHORT).show();
 69					populateView();
 70				}
 71			});
 72
 73		}
 74
 75		@Override
 76		public void error(final int errorCode, Conversation object) {
 77			runOnUiThread(new Runnable() {
 78				@Override
 79				public void run() {
 80					Toast.makeText(ConferenceDetailsActivity.this,getString(errorCode),Toast.LENGTH_SHORT).show();
 81				}
 82			});
 83		}
 84
 85		@Override
 86		public void userInputRequried(PendingIntent pi, Conversation object) {
 87
 88		}
 89	};
 90
 91	@Override
 92	public void onConversationUpdate() {
 93		runOnUiThread(new Runnable() {
 94
 95			@Override
 96			public void run() {
 97				populateView();
 98			}
 99		});
100	}
101
102	@Override
103	public void onMucRosterUpdate() {
104		runOnUiThread(new Runnable() {
105
106			@Override
107			public void run() {
108				populateView();
109			}
110		});
111	}
112
113	@Override
114	protected void onCreate(Bundle savedInstanceState) {
115		super.onCreate(savedInstanceState);
116		setContentView(R.layout.activity_muc_details);
117		mYourNick = (TextView) findViewById(R.id.muc_your_nick);
118		mYourPhoto = (ImageView) findViewById(R.id.your_photo);
119		mEditNickButton = (ImageButton) findViewById(R.id.edit_nick_button);
120		mFullJid = (TextView) findViewById(R.id.muc_jabberid);
121		membersView = (LinearLayout) findViewById(R.id.muc_members);
122		mAccountJid = (TextView) findViewById(R.id.details_account);
123		mMoreDetails = (LinearLayout) findViewById(R.id.muc_more_details);
124		mMoreDetails.setVisibility(View.GONE);
125		mInviteButton = (Button) findViewById(R.id.invite);
126		mInviteButton.setOnClickListener(inviteListener);
127		getActionBar().setHomeButtonEnabled(true);
128		getActionBar().setDisplayHomeAsUpEnabled(true);
129		mEditNickButton.setOnClickListener(new OnClickListener() {
130
131			@Override
132			public void onClick(View v) {
133				quickEdit(mConversation.getMucOptions().getActualNick(),
134						new OnValueEdited() {
135
136							@Override
137							public void onValueEdited(String value) {
138								xmppConnectionService.renameInMuc(mConversation,value,renameCallback);
139							}
140						});
141			}
142		});
143	}
144
145	@Override
146	public boolean onOptionsItemSelected(MenuItem menuItem) {
147		switch (menuItem.getItemId()) {
148			case android.R.id.home:
149				finish();
150				break;
151			case R.id.action_edit_subject:
152				if (mConversation != null) {
153					quickEdit(mConversation.getName(), new OnValueEdited() {
154
155						@Override
156						public void onValueEdited(String value) {
157							MessagePacket packet = xmppConnectionService
158									.getMessageGenerator().conferenceSubject(
159											mConversation, value);
160							xmppConnectionService.sendMessagePacket(
161									mConversation.getAccount(), packet);
162						}
163					});
164				}
165				break;
166			case R.id.action_save_as_bookmark:
167				saveAsBookmark();
168				break;
169			case R.id.action_delete_bookmark:
170				deleteBookmark();
171				break;
172		}
173		return super.onOptionsItemSelected(menuItem);
174	}
175
176	public String getReadableRole(int role) {
177		switch (role) {
178			case User.ROLE_MODERATOR:
179				return getString(R.string.moderator);
180			case User.ROLE_PARTICIPANT:
181				return getString(R.string.participant);
182			case User.ROLE_VISITOR:
183				return getString(R.string.visitor);
184			default:
185				return "";
186		}
187	}
188
189	@Override
190	protected String getShareableUri() {
191		if (mConversation != null) {
192			return "xmpp:" + mConversation.getContactJid().toBareJid().toString() + "?join";
193		} else {
194			return "";
195		}
196	}
197
198	@Override
199	public boolean onPrepareOptionsMenu(Menu menu) {
200		MenuItem menuItemSaveBookmark = menu.findItem(R.id.action_save_as_bookmark);
201		MenuItem menuItemDeleteBookmark = menu.findItem(R.id.action_delete_bookmark);
202		Account account = mConversation.getAccount();
203		if (account.hasBookmarkFor(mConversation.getContactJid().toBareJid())) {
204			menuItemSaveBookmark.setVisible(false);
205			menuItemDeleteBookmark.setVisible(true);
206		} else {
207			menuItemDeleteBookmark.setVisible(false);
208			menuItemSaveBookmark.setVisible(true);
209		}
210		return true;
211	}
212
213	@Override
214	public boolean onCreateOptionsMenu(Menu menu) {
215		getMenuInflater().inflate(R.menu.muc_details, menu);
216		return true;
217	}
218
219	@Override
220	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
221		Object tag = v.getTag();
222		if (tag instanceof User) {
223			getMenuInflater().inflate(R.menu.muc_details_context,menu);
224			final User user = (User) tag;
225			this.mSelectedUser = user;
226			String name;
227			final Contact contact = user.getContact();
228			if (contact != null) {
229				name = contact.getDisplayName();
230			} else if (user.getJid() != null) {
231				name = user.getJid().toBareJid().toString();
232			} else {
233				name = user.getName();
234			}
235			menu.setHeaderTitle(name);
236			MenuItem startConversation = menu.findItem(R.id.start_conversation);
237			if (user.getJid() == null) {
238				startConversation.setVisible(false);
239			}
240		}
241		super.onCreateContextMenu(menu,v,menuInfo);
242	}
243
244	@Override
245	public boolean onContextItemSelected(MenuItem item) {
246		switch (item.getItemId()) {
247			case R.id.start_conversation:
248				startConversation(mSelectedUser);
249				return true;
250			default:
251				return super.onContextItemSelected(item);
252		}
253	}
254
255	protected void startConversation(User user) {
256		if (user.getJid() != null) {
257			Conversation conversation = xmppConnectionService.findOrCreateConversation(this.mConversation.getAccount(),user.getJid(),false);
258			switchToConversation(conversation);
259		}
260	}
261
262	protected void saveAsBookmark() {
263		Account account = mConversation.getAccount();
264		Bookmark bookmark = new Bookmark(account, mConversation.getContactJid().toBareJid());
265		account.getBookmarks().add(bookmark);
266		xmppConnectionService.pushBookmarks(account);
267		mConversation.setBookmark(bookmark);
268	}
269
270	protected void deleteBookmark() {
271		Account account = mConversation.getAccount();
272		Bookmark bookmark = mConversation.getBookmark();
273		bookmark.unregisterConversation();
274		account.getBookmarks().remove(bookmark);
275		xmppConnectionService.pushBookmarks(account);
276	}
277
278	@Override
279	void onBackendConnected() {
280		if (getIntent().getAction().equals(ACTION_VIEW_MUC)) {
281			this.uuid = getIntent().getExtras().getString("uuid");
282		}
283		if (uuid != null) {
284			this.mConversation = xmppConnectionService
285					.findConversationByUuid(uuid);
286			if (this.mConversation != null) {
287				populateView();
288			}
289		}
290	}
291
292	private void populateView() {
293		mAccountJid.setText(getString(R.string.using_account, mConversation
294				.getAccount().getJid().toBareJid()));
295		mYourPhoto.setImageBitmap(avatarService().get(
296				mConversation.getAccount(), getPixel(48)));
297		setTitle(mConversation.getName());
298		mFullJid.setText(mConversation.getContactJid().toBareJid().toString());
299		mYourNick.setText(mConversation.getMucOptions().getActualNick());
300		mRoleAffiliaton = (TextView) findViewById(R.id.muc_role);
301		if (mConversation.getMucOptions().online()) {
302			mMoreDetails.setVisibility(View.VISIBLE);
303			User self = mConversation.getMucOptions().getSelf();
304			switch (self.getAffiliation()) {
305				case User.AFFILIATION_ADMIN:
306					mRoleAffiliaton.setText(getReadableRole(self.getRole()) + " ("
307							+ getString(R.string.admin) + ")");
308					break;
309				case User.AFFILIATION_OWNER:
310					mRoleAffiliaton.setText(getReadableRole(self.getRole()) + " ("
311							+ getString(R.string.owner) + ")");
312					break;
313				default:
314					mRoleAffiliaton.setText(getReadableRole(self.getRole()));
315					break;
316			}
317		}
318		this.users.clear();
319		this.users.addAll(mConversation.getMucOptions().getUsers());
320		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
321		membersView.removeAllViews();
322		for (final User user : mConversation.getMucOptions().getUsers()) {
323			View view = inflater.inflate(R.layout.contact, membersView,
324					false);
325			this.setListItemBackgroundOnView(view);
326			view.setOnClickListener(new OnClickListener() {
327				@Override
328				public void onClick(View view) {
329					highlightInMuc(mConversation, user.getName());
330				}
331			});
332			registerForContextMenu(view);
333			view.setTag(user);
334			TextView name = (TextView) view
335					.findViewById(R.id.contact_display_name);
336			TextView key = (TextView) view.findViewById(R.id.key);
337			TextView role = (TextView) view.findViewById(R.id.contact_jid);
338			if (user.getPgpKeyId() != 0) {
339				key.setVisibility(View.VISIBLE);
340				key.setOnClickListener(new OnClickListener() {
341
342					@Override
343					public void onClick(View v) {
344						viewPgpKey(user);
345					}
346				});
347				key.setText(OpenPgpUtils.convertKeyIdToHex(user.getPgpKeyId()));
348			}
349			Bitmap bm;
350			Contact contact = user.getContact();
351			if (contact != null) {
352				bm = avatarService().get(contact, getPixel(48));
353				name.setText(contact.getDisplayName());
354				role.setText(user.getName() + " \u2022 "
355						+ getReadableRole(user.getRole()));
356			} else {
357				bm = avatarService().get(user.getName(), getPixel(48));
358				name.setText(user.getName());
359				role.setText(getReadableRole(user.getRole()));
360			}
361			ImageView iv = (ImageView) view.findViewById(R.id.contact_photo);
362			iv.setImageBitmap(bm);
363			membersView.addView(view);
364		}
365	}
366
367	@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
368	private void setListItemBackgroundOnView(View view) {
369		int sdk = android.os.Build.VERSION.SDK_INT;
370		if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
371			view.setBackgroundDrawable(getResources().getDrawable(R.drawable.greybackground));
372		} else {
373			view.setBackground(getResources().getDrawable(R.drawable.greybackground));
374		}
375	}
376
377	private void viewPgpKey(User user) {
378		PgpEngine pgp = xmppConnectionService.getPgpEngine();
379		if (pgp != null) {
380			PendingIntent intent = pgp.getIntentForKey(
381					mConversation.getAccount(), user.getPgpKeyId());
382			if (intent != null) {
383				try {
384					startIntentSenderForResult(intent.getIntentSender(), 0,
385							null, 0, 0, 0);
386				} catch (SendIntentException e) {
387
388				}
389			}
390		}
391	}
392}