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