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