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