MucDetailsActivity.java

  1package eu.siacs.conversations.ui;
  2
  3import java.util.ArrayList;
  4import java.util.List;
  5
  6import org.openintents.openpgp.util.OpenPgpUtils;
  7
  8import eu.siacs.conversations.R;
  9import eu.siacs.conversations.crypto.PgpEngine;
 10import eu.siacs.conversations.entities.Conversation;
 11import eu.siacs.conversations.entities.MucOptions;
 12import eu.siacs.conversations.entities.MucOptions.OnRenameListener;
 13import eu.siacs.conversations.entities.MucOptions.User;
 14import eu.siacs.conversations.services.XmppConnectionService.OnConversationUpdate;
 15import eu.siacs.conversations.utils.UIHelper;
 16import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
 17import android.app.PendingIntent;
 18import android.content.Context;
 19import android.content.IntentSender.SendIntentException;
 20import android.graphics.Bitmap;
 21import android.os.Bundle;
 22import android.view.LayoutInflater;
 23import android.view.Menu;
 24import android.view.MenuItem;
 25import android.view.View;
 26import android.view.View.OnClickListener;
 27import android.widget.Button;
 28import android.widget.ImageButton;
 29import android.widget.ImageView;
 30import android.widget.LinearLayout;
 31import android.widget.TextView;
 32import android.widget.Toast;
 33
 34public class MucDetailsActivity extends XmppActivity {
 35	public static final String ACTION_VIEW_MUC = "view_muc";
 36	private Conversation conversation;
 37	private TextView mYourNick;
 38	private ImageView mYourPhoto;
 39	private ImageButton mEditNickButton;
 40	private TextView mRoleAffiliaton;
 41	private TextView mFullJid;
 42	private LinearLayout membersView;
 43	private LinearLayout mMoreDetails;
 44	private Button mInviteButton;
 45	private String uuid = null;
 46
 47	private OnClickListener inviteListener = new OnClickListener() {
 48
 49		@Override
 50		public void onClick(View v) {
 51			/*
 52			 * Intent intent = new Intent(getApplicationContext(),
 53			 * ContactsActivity.class); intent.setAction("invite");
 54			 * intent.putExtra("uuid",conversation.getUuid());
 55			 * startActivity(intent);
 56			 */
 57		}
 58	};
 59
 60	private List<User> users = new ArrayList<MucOptions.User>();
 61	private OnConversationUpdate onConvChanged = new OnConversationUpdate() {
 62		
 63		@Override
 64		public void onConversationUpdate() {
 65			runOnUiThread(new Runnable() {
 66
 67				@Override
 68				public void run() {
 69					populateView();
 70				}
 71			});
 72		}
 73	};
 74
 75	@Override
 76	protected void onCreate(Bundle savedInstanceState) {
 77		super.onCreate(savedInstanceState);
 78		setContentView(R.layout.activity_muc_details);
 79		mYourNick = (TextView) findViewById(R.id.muc_your_nick);
 80		mYourPhoto = (ImageView) findViewById(R.id.your_photo);
 81		mEditNickButton = (ImageButton) findViewById(R.id.edit_nick_button);
 82		mFullJid = (TextView) findViewById(R.id.muc_jabberid);
 83		membersView = (LinearLayout) findViewById(R.id.muc_members);
 84		mMoreDetails = (LinearLayout) findViewById(R.id.muc_more_details);
 85		mMoreDetails.setVisibility(View.GONE);
 86		mInviteButton = (Button) findViewById(R.id.invite);
 87		mInviteButton.setOnClickListener(inviteListener);
 88		getActionBar().setHomeButtonEnabled(true);
 89		getActionBar().setDisplayHomeAsUpEnabled(true);
 90		mEditNickButton.setOnClickListener(new OnClickListener() {
 91
 92			@Override
 93			public void onClick(View v) {
 94				quickEdit(conversation.getMucOptions().getActualNick(),
 95						new OnValueEdited() {
 96
 97							@Override
 98							public void onValueEdited(String value) {
 99								xmppConnectionService.renameInMuc(conversation,
100										value);
101							}
102						});
103			}
104		});
105	}
106
107	@Override
108	public boolean onOptionsItemSelected(MenuItem menuItem) {
109		switch (menuItem.getItemId()) {
110		case android.R.id.home:
111			finish();
112			break;
113		case R.id.action_edit_subject:
114			if (conversation != null) {
115				quickEdit(conversation.getName(true), new OnValueEdited() {
116
117					@Override
118					public void onValueEdited(String value) {
119						MessagePacket packet = xmppConnectionService
120								.getMessageGenerator().conferenceSubject(
121										conversation, value);
122						xmppConnectionService.sendMessagePacket(
123								conversation.getAccount(), packet);
124					}
125				});
126			}
127			break;
128		}
129		return super.onOptionsItemSelected(menuItem);
130	}
131
132	public String getReadableRole(int role) {
133		switch (role) {
134		case User.ROLE_MODERATOR:
135			return getString(R.string.moderator);
136		case User.ROLE_PARTICIPANT:
137			return getString(R.string.participant);
138		case User.ROLE_VISITOR:
139			return getString(R.string.visitor);
140		default:
141			return "";
142		}
143	}
144
145	@Override
146	public boolean onCreateOptionsMenu(Menu menu) {
147		getMenuInflater().inflate(R.menu.muc_details, menu);
148		return true;
149	}
150
151	@Override
152	void onBackendConnected() {
153		registerListener();
154		if (getIntent().getAction().equals(ACTION_VIEW_MUC)) {
155			this.uuid = getIntent().getExtras().getString("uuid");
156		}
157		if (uuid != null) {
158			for (Conversation mConv : xmppConnectionService.getConversations()) {
159				if (mConv.getUuid().equals(uuid)) {
160					this.conversation = mConv;
161				}
162			}
163			if (this.conversation != null) {
164				populateView();
165			}
166		}
167	}
168	
169	@Override
170	protected void onStop() {
171		if (xmppConnectionServiceBound) {
172			xmppConnectionService.removeOnConversationListChangedListener();
173		}
174		super.onStop();
175	}
176	
177	protected void registerListener() {
178		if (xmppConnectionServiceBound) {
179			xmppConnectionService
180					.setOnConversationListChangedListener(this.onConvChanged);
181		xmppConnectionService.setOnRenameListener(new OnRenameListener() {
182
183			@Override
184			public void onRename(final boolean success) {
185				runOnUiThread(new Runnable() {
186
187					@Override
188					public void run() {
189						populateView();
190						if (success) {
191							Toast.makeText(MucDetailsActivity.this,
192									getString(R.string.your_nick_has_been_changed),
193									Toast.LENGTH_SHORT).show();
194						} else {
195							Toast.makeText(MucDetailsActivity.this,
196									getString(R.string.nick_in_use),
197									Toast.LENGTH_SHORT).show();
198						}
199					}
200				});
201			}
202		});
203		}
204	}
205
206	private void populateView() {
207		mYourPhoto.setImageBitmap(UIHelper.getContactPicture(conversation
208				.getMucOptions().getActualNick(), 48, this, false));
209		setTitle(conversation.getName(true));
210		mFullJid.setText(conversation.getContactJid().split("/")[0]);
211		mYourNick.setText(conversation.getMucOptions().getActualNick());
212		mRoleAffiliaton = (TextView) findViewById(R.id.muc_role);
213		if (conversation.getMucOptions().online()) {
214			mMoreDetails.setVisibility(View.VISIBLE);
215			User self = conversation.getMucOptions().getSelf();
216			switch (self.getAffiliation()) {
217			case User.AFFILIATION_ADMIN:
218				mRoleAffiliaton.setText(getReadableRole(self.getRole()) + " ("
219						+ getString(R.string.admin) + ")");
220				break;
221			case User.AFFILIATION_OWNER:
222				mRoleAffiliaton.setText(getReadableRole(self.getRole()) + " ("
223						+ getString(R.string.owner) + ")");
224				break;
225			default:
226				mRoleAffiliaton.setText(getReadableRole(self.getRole()));
227				break;
228			}
229		}
230		this.users.clear();
231		this.users.addAll(conversation.getMucOptions().getUsers());
232		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
233		membersView.removeAllViews();
234		for (final User contact : conversation.getMucOptions().getUsers()) {
235			View view = (View) inflater.inflate(R.layout.contact, null);
236			TextView displayName = (TextView) view
237					.findViewById(R.id.contact_display_name);
238			TextView key = (TextView) view.findViewById(R.id.key);
239			displayName.setText(contact.getName());
240			TextView role = (TextView) view.findViewById(R.id.contact_jid);
241			role.setText(getReadableRole(contact.getRole()));
242			if (contact.getPgpKeyId() != 0) {
243				key.setVisibility(View.VISIBLE);
244				key.setOnClickListener(new OnClickListener() {
245
246					@Override
247					public void onClick(View v) {
248						viewPgpKey(contact);
249					}
250				});
251				key.setText(OpenPgpUtils.convertKeyIdToHex(contact
252						.getPgpKeyId()));
253			}
254			Bitmap bm = UIHelper.getContactPicture(contact.getName(), 48, this,
255					false);
256			ImageView iv = (ImageView) view.findViewById(R.id.contact_photo);
257			iv.setImageBitmap(bm);
258			membersView.addView(view);
259		}
260	}
261
262	private void viewPgpKey(User user) {
263		PgpEngine pgp = xmppConnectionService.getPgpEngine();
264		if (pgp != null) {
265			PendingIntent intent = pgp.getIntentForKey(
266					conversation.getAccount(), user.getPgpKeyId());
267			if (intent != null) {
268				try {
269					startIntentSenderForResult(intent.getIntentSender(), 0,
270							null, 0, 0, 0);
271				} catch (SendIntentException e) {
272
273				}
274			}
275		}
276	}
277}