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