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