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