1package eu.siacs.conversations.ui;
2
3import android.app.AlertDialog;
4import android.app.PendingIntent;
5import android.content.Context;
6import android.content.DialogInterface;
7import android.content.Intent;
8import android.content.IntentSender.SendIntentException;
9import android.content.SharedPreferences;
10import android.net.Uri;
11import android.os.Bundle;
12import android.preference.PreferenceManager;
13import android.provider.ContactsContract.CommonDataKinds;
14import android.provider.ContactsContract.Contacts;
15import android.provider.ContactsContract.Intents;
16import android.view.LayoutInflater;
17import android.view.Menu;
18import android.view.MenuItem;
19import android.view.View;
20import android.view.View.OnClickListener;
21import android.widget.Button;
22import android.widget.CheckBox;
23import android.widget.CompoundButton;
24import android.widget.CompoundButton.OnCheckedChangeListener;
25import android.widget.ImageButton;
26import android.widget.LinearLayout;
27import android.widget.QuickContactBadge;
28import android.widget.TextView;
29import android.widget.Toast;
30
31import com.wefika.flowlayout.FlowLayout;
32
33import org.openintents.openpgp.util.OpenPgpUtils;
34
35import java.security.cert.X509Certificate;
36import java.util.List;
37
38import eu.siacs.conversations.Config;
39import eu.siacs.conversations.R;
40import eu.siacs.conversations.crypto.PgpEngine;
41import eu.siacs.conversations.crypto.axolotl.AxolotlService;
42import eu.siacs.conversations.crypto.axolotl.XmppAxolotlSession;
43import eu.siacs.conversations.entities.Account;
44import eu.siacs.conversations.entities.Contact;
45import eu.siacs.conversations.entities.ListItem;
46import eu.siacs.conversations.entities.Presence;
47import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
48import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate;
49import eu.siacs.conversations.utils.CryptoHelper;
50import eu.siacs.conversations.utils.UIHelper;
51import eu.siacs.conversations.xmpp.OnKeyStatusUpdated;
52import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
53import eu.siacs.conversations.xmpp.XmppConnection;
54import eu.siacs.conversations.xmpp.jid.InvalidJidException;
55import eu.siacs.conversations.xmpp.jid.Jid;
56
57public class ContactDetailsActivity extends XmppActivity implements OnAccountUpdate, OnRosterUpdate, OnUpdateBlocklist, OnKeyStatusUpdated {
58 public static final String ACTION_VIEW_CONTACT = "view_contact";
59
60 private Contact contact;
61 private DialogInterface.OnClickListener removeFromRoster = new DialogInterface.OnClickListener() {
62
63 @Override
64 public void onClick(DialogInterface dialog, int which) {
65 xmppConnectionService.deleteContactOnServer(contact);
66 }
67 };
68 private OnCheckedChangeListener mOnSendCheckedChange = new OnCheckedChangeListener() {
69
70 @Override
71 public void onCheckedChanged(CompoundButton buttonView,
72 boolean isChecked) {
73 if (isChecked) {
74 if (contact
75 .getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
76 xmppConnectionService.sendPresencePacket(contact
77 .getAccount(),
78 xmppConnectionService.getPresenceGenerator()
79 .sendPresenceUpdatesTo(contact));
80 } else {
81 contact.setOption(Contact.Options.PREEMPTIVE_GRANT);
82 }
83 } else {
84 contact.resetOption(Contact.Options.PREEMPTIVE_GRANT);
85 xmppConnectionService.sendPresencePacket(contact.getAccount(),
86 xmppConnectionService.getPresenceGenerator()
87 .stopPresenceUpdatesTo(contact));
88 }
89 }
90 };
91 private OnCheckedChangeListener mOnReceiveCheckedChange = new OnCheckedChangeListener() {
92
93 @Override
94 public void onCheckedChanged(CompoundButton buttonView,
95 boolean isChecked) {
96 if (isChecked) {
97 xmppConnectionService.sendPresencePacket(contact.getAccount(),
98 xmppConnectionService.getPresenceGenerator()
99 .requestPresenceUpdatesFrom(contact));
100 } else {
101 xmppConnectionService.sendPresencePacket(contact.getAccount(),
102 xmppConnectionService.getPresenceGenerator()
103 .stopPresenceUpdatesFrom(contact));
104 }
105 }
106 };
107 private Jid accountJid;
108 private TextView lastseen;
109 private Jid contactJid;
110 private TextView contactJidTv;
111 private TextView accountJidTv;
112 private TextView statusMessage;
113 private CheckBox send;
114 private CheckBox receive;
115 private Button addContactButton;
116 private QuickContactBadge badge;
117 private LinearLayout keys;
118 private FlowLayout tags;
119 private boolean showDynamicTags = false;
120 private boolean showLastSeen = false;
121 private String messageFingerprint;
122
123 private DialogInterface.OnClickListener addToPhonebook = new DialogInterface.OnClickListener() {
124
125 @Override
126 public void onClick(DialogInterface dialog, int which) {
127 Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
128 intent.setType(Contacts.CONTENT_ITEM_TYPE);
129 intent.putExtra(Intents.Insert.IM_HANDLE, contact.getJid().toString());
130 intent.putExtra(Intents.Insert.IM_PROTOCOL,
131 CommonDataKinds.Im.PROTOCOL_JABBER);
132 intent.putExtra("finishActivityOnSaveCompleted", true);
133 ContactDetailsActivity.this.startActivityForResult(intent, 0);
134 }
135 };
136
137 private OnClickListener onBadgeClick = new OnClickListener() {
138
139 @Override
140 public void onClick(View v) {
141 Uri systemAccount = contact.getSystemAccount();
142 if (systemAccount == null) {
143 AlertDialog.Builder builder = new AlertDialog.Builder(
144 ContactDetailsActivity.this);
145 builder.setTitle(getString(R.string.action_add_phone_book));
146 builder.setMessage(getString(R.string.add_phone_book_text,
147 contact.getDisplayJid()));
148 builder.setNegativeButton(getString(R.string.cancel), null);
149 builder.setPositiveButton(getString(R.string.add), addToPhonebook);
150 builder.create().show();
151 } else {
152 Intent intent = new Intent(Intent.ACTION_VIEW);
153 intent.setData(systemAccount);
154 startActivity(intent);
155 }
156 }
157 };
158
159 @Override
160 public void onRosterUpdate() {
161 refreshUi();
162 }
163
164 @Override
165 public void onAccountUpdate() {
166 refreshUi();
167 }
168
169 @Override
170 public void OnUpdateBlocklist(final Status status) {
171 refreshUi();
172 }
173
174 @Override
175 protected void refreshUiReal() {
176 invalidateOptionsMenu();
177 populateView();
178 }
179
180 @Override
181 protected String getShareableUri() {
182 if (contact != null) {
183 return "xmpp:"+contact.getJid().toBareJid().toString();
184 } else {
185 return "";
186 }
187 }
188
189 @Override
190 protected void onCreate(final Bundle savedInstanceState) {
191 super.onCreate(savedInstanceState);
192 if (getIntent().getAction().equals(ACTION_VIEW_CONTACT)) {
193 try {
194 this.accountJid = Jid.fromString(getIntent().getExtras().getString(EXTRA_ACCOUNT));
195 } catch (final InvalidJidException ignored) {
196 }
197 try {
198 this.contactJid = Jid.fromString(getIntent().getExtras().getString("contact"));
199 } catch (final InvalidJidException ignored) {
200 }
201 }
202 this.messageFingerprint = getIntent().getStringExtra("fingerprint");
203 setContentView(R.layout.activity_contact_details);
204
205 contactJidTv = (TextView) findViewById(R.id.details_contactjid);
206 accountJidTv = (TextView) findViewById(R.id.details_account);
207 lastseen = (TextView) findViewById(R.id.details_lastseen);
208 statusMessage = (TextView) findViewById(R.id.status_message);
209 send = (CheckBox) findViewById(R.id.details_send_presence);
210 receive = (CheckBox) findViewById(R.id.details_receive_presence);
211 badge = (QuickContactBadge) findViewById(R.id.details_contact_badge);
212 addContactButton = (Button) findViewById(R.id.add_contact_button);
213 addContactButton.setOnClickListener(new OnClickListener() {
214 @Override
215 public void onClick(View view) {
216 showAddToRosterDialog(contact);
217 }
218 });
219 keys = (LinearLayout) findViewById(R.id.details_contact_keys);
220 tags = (FlowLayout) findViewById(R.id.tags);
221 if (getActionBar() != null) {
222 getActionBar().setHomeButtonEnabled(true);
223 getActionBar().setDisplayHomeAsUpEnabled(true);
224 }
225 }
226
227 @Override
228 public void onStart() {
229 super.onStart();
230 final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
231 this.showDynamicTags = preferences.getBoolean("show_dynamic_tags",false);
232 this.showLastSeen = preferences.getBoolean("last_activity", false);
233 }
234
235 @Override
236 public boolean onOptionsItemSelected(final MenuItem menuItem) {
237 final AlertDialog.Builder builder = new AlertDialog.Builder(this);
238 builder.setNegativeButton(getString(R.string.cancel), null);
239 switch (menuItem.getItemId()) {
240 case android.R.id.home:
241 finish();
242 break;
243 case R.id.action_share:
244 shareUri();
245 break;
246 case R.id.action_delete_contact:
247 builder.setTitle(getString(R.string.action_delete_contact))
248 .setMessage(
249 getString(R.string.remove_contact_text,
250 contact.getDisplayJid()))
251 .setPositiveButton(getString(R.string.delete),
252 removeFromRoster).create().show();
253 break;
254 case R.id.action_edit_contact:
255 Uri systemAccount = contact.getSystemAccount();
256 if (systemAccount == null) {
257 quickEdit(contact.getDisplayName(), 0, new OnValueEdited() {
258
259 @Override
260 public void onValueEdited(String value) {
261 contact.setServerName(value);
262 ContactDetailsActivity.this.xmppConnectionService
263 .pushContactToServer(contact);
264 populateView();
265 }
266 });
267 } else {
268 Intent intent = new Intent(Intent.ACTION_EDIT);
269 intent.setDataAndType(systemAccount, Contacts.CONTENT_ITEM_TYPE);
270 intent.putExtra("finishActivityOnSaveCompleted", true);
271 startActivity(intent);
272 }
273 break;
274 case R.id.action_block:
275 BlockContactDialog.show(this, xmppConnectionService, contact);
276 break;
277 case R.id.action_unblock:
278 BlockContactDialog.show(this, xmppConnectionService, contact);
279 break;
280 }
281 return super.onOptionsItemSelected(menuItem);
282 }
283
284 @Override
285 public boolean onCreateOptionsMenu(final Menu menu) {
286 getMenuInflater().inflate(R.menu.contact_details, menu);
287 MenuItem block = menu.findItem(R.id.action_block);
288 MenuItem unblock = menu.findItem(R.id.action_unblock);
289 MenuItem edit = menu.findItem(R.id.action_edit_contact);
290 MenuItem delete = menu.findItem(R.id.action_delete_contact);
291 if (contact == null) {
292 return true;
293 }
294 final XmppConnection connection = contact.getAccount().getXmppConnection();
295 if (connection != null && connection.getFeatures().blocking()) {
296 if (this.contact.isBlocked()) {
297 block.setVisible(false);
298 } else {
299 unblock.setVisible(false);
300 }
301 } else {
302 unblock.setVisible(false);
303 block.setVisible(false);
304 }
305 if (!contact.showInRoster()) {
306 edit.setVisible(false);
307 delete.setVisible(false);
308 }
309 return super.onCreateOptionsMenu(menu);
310 }
311
312 private void populateView() {
313 if (contact == null) {
314 return;
315 }
316 invalidateOptionsMenu();
317 setTitle(contact.getDisplayName());
318 if (contact.showInRoster()) {
319 send.setVisibility(View.VISIBLE);
320 receive.setVisibility(View.VISIBLE);
321 addContactButton.setVisibility(View.GONE);
322 send.setOnCheckedChangeListener(null);
323 receive.setOnCheckedChangeListener(null);
324
325 List<String> statusMessages = contact.getPresences().getStatusMessages();
326 if (statusMessages.size() == 0) {
327 statusMessage.setVisibility(View.GONE);
328 } else {
329 StringBuilder builder = new StringBuilder();
330 statusMessage.setVisibility(View.VISIBLE);
331 int s = statusMessages.size();
332 for(int i = 0; i < s; ++i) {
333 if (s > 1) {
334 builder.append("• ");
335 }
336 builder.append(statusMessages.get(i));
337 if (i < s - 1) {
338 builder.append("\n");
339 }
340 }
341 statusMessage.setText(builder);
342 }
343
344 if (contact.getOption(Contact.Options.FROM)) {
345 send.setText(R.string.send_presence_updates);
346 send.setChecked(true);
347 } else if (contact.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
348 send.setChecked(false);
349 send.setText(R.string.send_presence_updates);
350 } else {
351 send.setText(R.string.preemptively_grant);
352 if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
353 send.setChecked(true);
354 } else {
355 send.setChecked(false);
356 }
357 }
358 if (contact.getOption(Contact.Options.TO)) {
359 receive.setText(R.string.receive_presence_updates);
360 receive.setChecked(true);
361 } else {
362 receive.setText(R.string.ask_for_presence_updates);
363 if (contact.getOption(Contact.Options.ASKING)) {
364 receive.setChecked(true);
365 } else {
366 receive.setChecked(false);
367 }
368 }
369 if (contact.getAccount().isOnlineAndConnected()) {
370 receive.setEnabled(true);
371 send.setEnabled(true);
372 } else {
373 receive.setEnabled(false);
374 send.setEnabled(false);
375 }
376 send.setOnCheckedChangeListener(this.mOnSendCheckedChange);
377 receive.setOnCheckedChangeListener(this.mOnReceiveCheckedChange);
378 } else {
379 addContactButton.setVisibility(View.VISIBLE);
380 send.setVisibility(View.GONE);
381 receive.setVisibility(View.GONE);
382 statusMessage.setVisibility(View.GONE);
383 }
384
385 if (contact.isBlocked() && !this.showDynamicTags) {
386 lastseen.setVisibility(View.VISIBLE);
387 lastseen.setText(R.string.contact_blocked);
388 } else {
389 if (showLastSeen && contact.getLastseen() > 0) {
390 lastseen.setVisibility(View.VISIBLE);
391 lastseen.setText(UIHelper.lastseen(getApplicationContext(), contact.isActive(), contact.getLastseen()));
392 } else {
393 lastseen.setVisibility(View.GONE);
394 }
395 }
396
397 if (contact.getPresences().size() > 1) {
398 contactJidTv.setText(contact.getDisplayJid() + " ("
399 + contact.getPresences().size() + ")");
400 } else {
401 contactJidTv.setText(contact.getDisplayJid());
402 }
403 String account;
404 if (Config.DOMAIN_LOCK != null) {
405 account = contact.getAccount().getJid().getLocalpart();
406 } else {
407 account = contact.getAccount().getJid().toBareJid().toString();
408 }
409 accountJidTv.setText(getString(R.string.using_account, account));
410 badge.setImageBitmap(avatarService().get(contact, getPixel(72)));
411 badge.setOnClickListener(this.onBadgeClick);
412
413 keys.removeAllViews();
414 boolean hasKeys = false;
415 LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
416 if (Config.supportOtr()) {
417 for (final String otrFingerprint : contact.getOtrFingerprints()) {
418 hasKeys = true;
419 View view = inflater.inflate(R.layout.contact_key, keys, false);
420 TextView key = (TextView) view.findViewById(R.id.key);
421 TextView keyType = (TextView) view.findViewById(R.id.key_type);
422 ImageButton removeButton = (ImageButton) view
423 .findViewById(R.id.button_remove);
424 removeButton.setVisibility(View.VISIBLE);
425 key.setText(CryptoHelper.prettifyFingerprint(otrFingerprint));
426 if (otrFingerprint != null && otrFingerprint.equals(messageFingerprint)) {
427 keyType.setText(R.string.otr_fingerprint_selected_message);
428 keyType.setTextColor(getResources().getColor(R.color.accent));
429 } else {
430 keyType.setText(R.string.otr_fingerprint);
431 }
432 keys.addView(view);
433 removeButton.setOnClickListener(new OnClickListener() {
434
435 @Override
436 public void onClick(View v) {
437 confirmToDeleteFingerprint(otrFingerprint);
438 }
439 });
440 }
441 }
442 if (Config.supportOmemo()) {
443 for (final String fingerprint : contact.getAccount().getAxolotlService().getFingerprintsForContact(contact)) {
444 boolean highlight = fingerprint.equals(messageFingerprint);
445 hasKeys |= addFingerprintRow(keys, contact.getAccount(), fingerprint, highlight, new OnClickListener() {
446 @Override
447 public void onClick(View v) {
448 onOmemoKeyClicked(contact.getAccount(), fingerprint);
449 }
450 });
451 }
452 }
453 if (Config.supportOpenPgp() && contact.getPgpKeyId() != 0) {
454 hasKeys = true;
455 View view = inflater.inflate(R.layout.contact_key, keys, false);
456 TextView key = (TextView) view.findViewById(R.id.key);
457 TextView keyType = (TextView) view.findViewById(R.id.key_type);
458 keyType.setText(R.string.openpgp_key_id);
459 if ("pgp".equals(messageFingerprint)) {
460 keyType.setTextColor(getResources().getColor(R.color.accent));
461 }
462 key.setText(OpenPgpUtils.convertKeyIdToHex(contact.getPgpKeyId()));
463 view.setOnClickListener(new OnClickListener() {
464
465 @Override
466 public void onClick(View v) {
467 PgpEngine pgp = ContactDetailsActivity.this.xmppConnectionService
468 .getPgpEngine();
469 if (pgp != null) {
470 PendingIntent intent = pgp.getIntentForKey(contact);
471 if (intent != null) {
472 try {
473 startIntentSenderForResult(
474 intent.getIntentSender(), 0, null, 0,
475 0, 0);
476 } catch (SendIntentException e) {
477
478 }
479 }
480 }
481 }
482 });
483 keys.addView(view);
484 }
485 if (hasKeys) {
486 keys.setVisibility(View.VISIBLE);
487 } else {
488 keys.setVisibility(View.GONE);
489 }
490
491 List<ListItem.Tag> tagList = contact.getTags(this);
492 if (tagList.size() == 0 || !this.showDynamicTags) {
493 tags.setVisibility(View.GONE);
494 } else {
495 tags.setVisibility(View.VISIBLE);
496 tags.removeAllViewsInLayout();
497 for(final ListItem.Tag tag : tagList) {
498 final TextView tv = (TextView) inflater.inflate(R.layout.list_item_tag,tags,false);
499 tv.setText(tag.getName());
500 tv.setBackgroundColor(tag.getColor());
501 tags.addView(tv);
502 }
503 }
504 }
505
506 private void onOmemoKeyClicked(Account account, String fingerprint) {
507 final XmppAxolotlSession.Trust trust = account.getAxolotlService().getFingerprintTrust(fingerprint);
508 if (Config.X509_VERIFICATION && trust != null && trust == XmppAxolotlSession.Trust.TRUSTED_X509) {
509 X509Certificate x509Certificate = account.getAxolotlService().getFingerprintCertificate(fingerprint);
510 if (x509Certificate != null) {
511 showCertificateInformationDialog(CryptoHelper.extractCertificateInformation(x509Certificate));
512 } else {
513 Toast.makeText(this,R.string.certificate_not_found, Toast.LENGTH_SHORT).show();
514 }
515 }
516 }
517
518 private void showCertificateInformationDialog(Bundle bundle) {
519 View view = getLayoutInflater().inflate(R.layout.certificate_information, null);
520 final String not_available = getString(R.string.certicate_info_not_available);
521 TextView subject_cn = (TextView) view.findViewById(R.id.subject_cn);
522 TextView subject_o = (TextView) view.findViewById(R.id.subject_o);
523 TextView issuer_cn = (TextView) view.findViewById(R.id.issuer_cn);
524 TextView issuer_o = (TextView) view.findViewById(R.id.issuer_o);
525 TextView sha1 = (TextView) view.findViewById(R.id.sha1);
526
527 subject_cn.setText(bundle.getString("subject_cn", not_available));
528 subject_o.setText(bundle.getString("subject_o", not_available));
529 issuer_cn.setText(bundle.getString("issuer_cn", not_available));
530 issuer_o.setText(bundle.getString("issuer_o", not_available));
531 sha1.setText(bundle.getString("sha1", not_available));
532
533 AlertDialog.Builder builder = new AlertDialog.Builder(this);
534 builder.setTitle(R.string.certificate_information);
535 builder.setView(view);
536 builder.setPositiveButton(R.string.ok, null);
537 builder.create().show();
538 }
539
540 protected void confirmToDeleteFingerprint(final String fingerprint) {
541 AlertDialog.Builder builder = new AlertDialog.Builder(this);
542 builder.setTitle(R.string.delete_fingerprint);
543 builder.setMessage(R.string.sure_delete_fingerprint);
544 builder.setNegativeButton(R.string.cancel, null);
545 builder.setPositiveButton(R.string.delete,
546 new android.content.DialogInterface.OnClickListener() {
547
548 @Override
549 public void onClick(DialogInterface dialog, int which) {
550 if (contact.deleteOtrFingerprint(fingerprint)) {
551 populateView();
552 xmppConnectionService.syncRosterToDisk(contact.getAccount());
553 }
554 }
555
556 });
557 builder.create().show();
558 }
559
560 @Override
561 public void onBackendConnected() {
562 if ((accountJid != null) && (contactJid != null)) {
563 Account account = xmppConnectionService
564 .findAccountByJid(accountJid);
565 if (account == null) {
566 return;
567 }
568 this.contact = account.getRoster().getContact(contactJid);
569 populateView();
570 }
571 }
572
573 @Override
574 public void onKeyStatusUpdated(AxolotlService.FetchStatus report) {
575 refreshUi();
576 }
577}