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