1package eu.siacs.conversations.ui;
2
3import android.databinding.DataBindingUtil;
4import android.support.v7.app.AlertDialog;
5import android.content.DialogInterface;
6import android.content.Intent;
7import android.content.SharedPreferences;
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.v4.content.ContextCompat;
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
26
27import org.openintents.openpgp.util.OpenPgpUtils;
28
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.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
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
102 ActivityContactDetailsBinding binding;
103
104 private Jid accountJid;
105 private Jid contactJid;
106 private boolean showDynamicTags = false;
107 private boolean showLastSeen = false;
108 private boolean showInactiveOmemo = false;
109 private String messageFingerprint;
110
111 private DialogInterface.OnClickListener addToPhonebook = new DialogInterface.OnClickListener() {
112
113 @Override
114 public void onClick(DialogInterface dialog, int which) {
115 Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
116 intent.setType(Contacts.CONTENT_ITEM_TYPE);
117 intent.putExtra(Intents.Insert.IM_HANDLE, contact.getJid().toString());
118 intent.putExtra(Intents.Insert.IM_PROTOCOL,
119 CommonDataKinds.Im.PROTOCOL_JABBER);
120 intent.putExtra("finishActivityOnSaveCompleted", true);
121 ContactDetailsActivity.this.startActivityForResult(intent, 0);
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 final String prefix = http ? "https://conversations.im/i/" : "xmpp:";
170 if (contact != null) {
171 return prefix+contact.getJid().asBareJid().toEscapedString();
172 } else {
173 return "";
174 }
175 }
176
177 @Override
178 protected void onCreate(final Bundle savedInstanceState) {
179 super.onCreate(savedInstanceState);
180 showInactiveOmemo = savedInstanceState != null && savedInstanceState.getBoolean("show_inactive_omemo",false);
181 if (getIntent().getAction().equals(ACTION_VIEW_CONTACT)) {
182 try {
183 this.accountJid = Jid.of(getIntent().getExtras().getString(EXTRA_ACCOUNT));
184 } catch (final IllegalArgumentException ignored) {
185 }
186 try {
187 this.contactJid = Jid.of(getIntent().getExtras().getString("contact"));
188 } catch (final IllegalArgumentException ignored) {
189 }
190 }
191 this.messageFingerprint = getIntent().getStringExtra("fingerprint");
192 this.binding = DataBindingUtil.setContentView(this, R.layout.activity_contact_details);
193
194 setSupportActionBar((Toolbar) binding.toolbar);
195 configureActionBar(getSupportActionBar());
196 binding.showInactiveDevices.setOnClickListener(v -> {
197 showInactiveOmemo = !showInactiveOmemo;
198 populateView();
199 });
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 }
220
221 @Override
222 public boolean onOptionsItemSelected(final MenuItem menuItem) {
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}