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