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