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