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