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