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