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