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