ContactDetailsActivity.java

  1package eu.siacs.conversations.ui;
  2
  3import android.app.AlertDialog;
  4import android.app.PendingIntent;
  5import android.content.Context;
  6import android.content.DialogInterface;
  7import android.content.Intent;
  8import android.content.IntentSender.SendIntentException;
  9import android.content.SharedPreferences;
 10import android.net.Uri;
 11import android.os.Bundle;
 12import android.preference.PreferenceManager;
 13import android.provider.ContactsContract.CommonDataKinds;
 14import android.provider.ContactsContract.Contacts;
 15import android.provider.ContactsContract.Intents;
 16import android.view.LayoutInflater;
 17import android.view.Menu;
 18import android.view.MenuItem;
 19import android.view.View;
 20import android.view.View.OnClickListener;
 21import android.widget.Button;
 22import android.widget.CheckBox;
 23import android.widget.CompoundButton;
 24import android.widget.CompoundButton.OnCheckedChangeListener;
 25import android.widget.ImageButton;
 26import android.widget.LinearLayout;
 27import android.widget.QuickContactBadge;
 28import android.widget.TextView;
 29import android.widget.Toast;
 30
 31import com.wefika.flowlayout.FlowLayout;
 32
 33import org.openintents.openpgp.util.OpenPgpUtils;
 34
 35import java.security.cert.X509Certificate;
 36import java.util.List;
 37
 38import eu.siacs.conversations.Config;
 39import eu.siacs.conversations.OmemoActivity;
 40import eu.siacs.conversations.R;
 41import eu.siacs.conversations.crypto.PgpEngine;
 42import eu.siacs.conversations.crypto.axolotl.AxolotlService;
 43import eu.siacs.conversations.crypto.axolotl.FingerprintStatus;
 44import eu.siacs.conversations.crypto.axolotl.XmppAxolotlSession;
 45import eu.siacs.conversations.entities.Account;
 46import eu.siacs.conversations.entities.Contact;
 47import eu.siacs.conversations.entities.ListItem;
 48import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
 49import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate;
 50import eu.siacs.conversations.utils.CryptoHelper;
 51import eu.siacs.conversations.utils.UIHelper;
 52import eu.siacs.conversations.utils.XmppUri;
 53import eu.siacs.conversations.xmpp.OnKeyStatusUpdated;
 54import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
 55import eu.siacs.conversations.xmpp.XmppConnection;
 56import eu.siacs.conversations.xmpp.jid.InvalidJidException;
 57import eu.siacs.conversations.xmpp.jid.Jid;
 58
 59public class ContactDetailsActivity extends OmemoActivity implements OnAccountUpdate, OnRosterUpdate, OnUpdateBlocklist, OnKeyStatusUpdated {
 60	public static final String ACTION_VIEW_CONTACT = "view_contact";
 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,
 74				boolean isChecked) {
 75			if (isChecked) {
 76				if (contact
 77						.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
 78					xmppConnectionService.sendPresencePacket(contact
 79							.getAccount(),
 80							xmppConnectionService.getPresenceGenerator()
 81							.sendPresenceUpdatesTo(contact));
 82				} else {
 83					contact.setOption(Contact.Options.PREEMPTIVE_GRANT);
 84				}
 85			} else {
 86				contact.resetOption(Contact.Options.PREEMPTIVE_GRANT);
 87				xmppConnectionService.sendPresencePacket(contact.getAccount(),
 88						xmppConnectionService.getPresenceGenerator()
 89						.stopPresenceUpdatesTo(contact));
 90			}
 91		}
 92	};
 93	private OnCheckedChangeListener mOnReceiveCheckedChange = new OnCheckedChangeListener() {
 94
 95		@Override
 96		public void onCheckedChanged(CompoundButton buttonView,
 97				boolean isChecked) {
 98			if (isChecked) {
 99				xmppConnectionService.sendPresencePacket(contact.getAccount(),
100						xmppConnectionService.getPresenceGenerator()
101						.requestPresenceUpdatesFrom(contact));
102			} else {
103				xmppConnectionService.sendPresencePacket(contact.getAccount(),
104						xmppConnectionService.getPresenceGenerator()
105						.stopPresenceUpdatesFrom(contact));
106			}
107		}
108	};
109	private Jid accountJid;
110	private TextView lastseen;
111	private Jid contactJid;
112	private TextView contactJidTv;
113	private TextView accountJidTv;
114	private TextView statusMessage;
115	private CheckBox send;
116	private CheckBox receive;
117	private Button addContactButton;
118	private QuickContactBadge badge;
119	private LinearLayout keys;
120	private FlowLayout tags;
121	private boolean showDynamicTags = false;
122	private boolean showLastSeen = false;
123	private String messageFingerprint;
124
125	private DialogInterface.OnClickListener addToPhonebook = new DialogInterface.OnClickListener() {
126
127		@Override
128		public void onClick(DialogInterface dialog, int which) {
129			Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
130			intent.setType(Contacts.CONTENT_ITEM_TYPE);
131			intent.putExtra(Intents.Insert.IM_HANDLE, contact.getJid().toString());
132			intent.putExtra(Intents.Insert.IM_PROTOCOL,
133					CommonDataKinds.Im.PROTOCOL_JABBER);
134			intent.putExtra("finishActivityOnSaveCompleted", true);
135			ContactDetailsActivity.this.startActivityForResult(intent, 0);
136		}
137	};
138
139	private OnClickListener onBadgeClick = new OnClickListener() {
140
141		@Override
142		public void onClick(View v) {
143			Uri systemAccount = contact.getSystemAccount();
144			if (systemAccount == null) {
145				AlertDialog.Builder builder = new AlertDialog.Builder(
146						ContactDetailsActivity.this);
147				builder.setTitle(getString(R.string.action_add_phone_book));
148				builder.setMessage(getString(R.string.add_phone_book_text,
149						contact.getDisplayJid()));
150				builder.setNegativeButton(getString(R.string.cancel), null);
151				builder.setPositiveButton(getString(R.string.add), addToPhonebook);
152				builder.create().show();
153			} else {
154				Intent intent = new Intent(Intent.ACTION_VIEW);
155				intent.setData(systemAccount);
156				startActivity(intent);
157			}
158		}
159	};
160
161	@Override
162	public void onRosterUpdate() {
163		refreshUi();
164	}
165
166	@Override
167	public void onAccountUpdate() {
168		refreshUi();
169	}
170
171	@Override
172	public void OnUpdateBlocklist(final Status status) {
173		refreshUi();
174	}
175
176	@Override
177	protected void refreshUiReal() {
178		invalidateOptionsMenu();
179		populateView();
180	}
181
182	@Override
183	protected String getShareableUri() {
184		if (contact != null) {
185			return "xmpp:"+contact.getJid().toBareJid().toString();
186		} else {
187			return "";
188		}
189	}
190
191	@Override
192	protected void onCreate(final Bundle savedInstanceState) {
193		super.onCreate(savedInstanceState);
194		if (getIntent().getAction().equals(ACTION_VIEW_CONTACT)) {
195			try {
196				this.accountJid = Jid.fromString(getIntent().getExtras().getString(EXTRA_ACCOUNT));
197			} catch (final InvalidJidException ignored) {
198			}
199			try {
200				this.contactJid = Jid.fromString(getIntent().getExtras().getString("contact"));
201			} catch (final InvalidJidException ignored) {
202			}
203		}
204		this.messageFingerprint = getIntent().getStringExtra("fingerprint");
205		setContentView(R.layout.activity_contact_details);
206
207		contactJidTv = (TextView) findViewById(R.id.details_contactjid);
208		accountJidTv = (TextView) findViewById(R.id.details_account);
209		lastseen = (TextView) findViewById(R.id.details_lastseen);
210		statusMessage = (TextView) findViewById(R.id.status_message);
211		send = (CheckBox) findViewById(R.id.details_send_presence);
212		receive = (CheckBox) findViewById(R.id.details_receive_presence);
213		badge = (QuickContactBadge) findViewById(R.id.details_contact_badge);
214		addContactButton = (Button) findViewById(R.id.add_contact_button);
215		addContactButton.setOnClickListener(new OnClickListener() {
216			@Override
217			public void onClick(View view) {
218				showAddToRosterDialog(contact);
219			}
220		});
221		keys = (LinearLayout) findViewById(R.id.details_contact_keys);
222		tags = (FlowLayout) findViewById(R.id.tags);
223		if (getActionBar() != null) {
224			getActionBar().setHomeButtonEnabled(true);
225			getActionBar().setDisplayHomeAsUpEnabled(true);
226		}
227	}
228
229	@Override
230	public void onStart() {
231		super.onStart();
232		final int theme = findTheme();
233		if (this.mTheme != theme) {
234			recreate();
235		} else {
236			final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
237			this.showDynamicTags = preferences.getBoolean("show_dynamic_tags", false);
238			this.showLastSeen = preferences.getBoolean("last_activity", false);
239		}
240	}
241
242	@Override
243	public boolean onOptionsItemSelected(final MenuItem menuItem) {
244		final AlertDialog.Builder builder = new AlertDialog.Builder(this);
245		builder.setNegativeButton(getString(R.string.cancel), null);
246		switch (menuItem.getItemId()) {
247			case android.R.id.home:
248				finish();
249				break;
250			case R.id.action_share:
251				shareUri();
252				break;
253			case R.id.action_delete_contact:
254				builder.setTitle(getString(R.string.action_delete_contact))
255					.setMessage(
256							getString(R.string.remove_contact_text,
257								contact.getDisplayJid()))
258					.setPositiveButton(getString(R.string.delete),
259							removeFromRoster).create().show();
260				break;
261			case R.id.action_edit_contact:
262				Uri systemAccount = contact.getSystemAccount();
263				if (systemAccount == null) {
264					quickEdit(contact.getDisplayName(), 0, new OnValueEdited() {
265
266						@Override
267						public void onValueEdited(String value) {
268							contact.setServerName(value);
269							ContactDetailsActivity.this.xmppConnectionService
270								.pushContactToServer(contact);
271							populateView();
272						}
273					});
274				} else {
275					Intent intent = new Intent(Intent.ACTION_EDIT);
276					intent.setDataAndType(systemAccount, Contacts.CONTENT_ITEM_TYPE);
277					intent.putExtra("finishActivityOnSaveCompleted", true);
278					startActivity(intent);
279				}
280				break;
281			case R.id.action_block:
282				BlockContactDialog.show(this, xmppConnectionService, contact);
283				break;
284			case R.id.action_unblock:
285				BlockContactDialog.show(this, xmppConnectionService, contact);
286				break;
287		}
288		return super.onOptionsItemSelected(menuItem);
289	}
290
291	@Override
292	public boolean onCreateOptionsMenu(final Menu menu) {
293		getMenuInflater().inflate(R.menu.contact_details, menu);
294		MenuItem block = menu.findItem(R.id.action_block);
295		MenuItem unblock = menu.findItem(R.id.action_unblock);
296		MenuItem edit = menu.findItem(R.id.action_edit_contact);
297		MenuItem delete = menu.findItem(R.id.action_delete_contact);
298		if (contact == null) {
299			return true;
300		}
301		final XmppConnection connection = contact.getAccount().getXmppConnection();
302		if (connection != null && connection.getFeatures().blocking()) {
303			if (this.contact.isBlocked()) {
304				block.setVisible(false);
305			} else {
306				unblock.setVisible(false);
307			}
308		} else {
309			unblock.setVisible(false);
310			block.setVisible(false);
311		}
312		if (!contact.showInRoster()) {
313			edit.setVisible(false);
314			delete.setVisible(false);
315		}
316		return super.onCreateOptionsMenu(menu);
317	}
318
319	private void populateView() {
320		if (contact == null) {
321			return;
322		}
323		invalidateOptionsMenu();
324		setTitle(contact.getDisplayName());
325		if (contact.showInRoster()) {
326			send.setVisibility(View.VISIBLE);
327			receive.setVisibility(View.VISIBLE);
328			addContactButton.setVisibility(View.GONE);
329			send.setOnCheckedChangeListener(null);
330			receive.setOnCheckedChangeListener(null);
331
332			List<String> statusMessages = contact.getPresences().getStatusMessages();
333			if (statusMessages.size() == 0) {
334				statusMessage.setVisibility(View.GONE);
335			} else {
336				StringBuilder builder = new StringBuilder();
337				statusMessage.setVisibility(View.VISIBLE);
338				int s = statusMessages.size();
339				for(int i = 0; i < s; ++i) {
340					if (s > 1) {
341						builder.append("");
342					}
343					builder.append(statusMessages.get(i));
344					if (i < s - 1) {
345						builder.append("\n");
346					}
347				}
348				statusMessage.setText(builder);
349			}
350
351			if (contact.getOption(Contact.Options.FROM)) {
352				send.setText(R.string.send_presence_updates);
353				send.setChecked(true);
354			} else if (contact.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
355				send.setChecked(false);
356				send.setText(R.string.send_presence_updates);
357			} else {
358				send.setText(R.string.preemptively_grant);
359				if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
360					send.setChecked(true);
361				} else {
362					send.setChecked(false);
363				}
364			}
365			if (contact.getOption(Contact.Options.TO)) {
366				receive.setText(R.string.receive_presence_updates);
367				receive.setChecked(true);
368			} else {
369				receive.setText(R.string.ask_for_presence_updates);
370				if (contact.getOption(Contact.Options.ASKING)) {
371					receive.setChecked(true);
372				} else {
373					receive.setChecked(false);
374				}
375			}
376			if (contact.getAccount().isOnlineAndConnected()) {
377				receive.setEnabled(true);
378				send.setEnabled(true);
379			} else {
380				receive.setEnabled(false);
381				send.setEnabled(false);
382			}
383			send.setOnCheckedChangeListener(this.mOnSendCheckedChange);
384			receive.setOnCheckedChangeListener(this.mOnReceiveCheckedChange);
385		} else {
386			addContactButton.setVisibility(View.VISIBLE);
387			send.setVisibility(View.GONE);
388			receive.setVisibility(View.GONE);
389			statusMessage.setVisibility(View.GONE);
390		}
391
392		if (contact.isBlocked() && !this.showDynamicTags) {
393			lastseen.setVisibility(View.VISIBLE);
394			lastseen.setText(R.string.contact_blocked);
395		} else {
396			if (showLastSeen && contact.getLastseen() > 0) {
397				lastseen.setVisibility(View.VISIBLE);
398				lastseen.setText(UIHelper.lastseen(getApplicationContext(), contact.isActive(), contact.getLastseen()));
399			} else {
400				lastseen.setVisibility(View.GONE);
401			}
402		}
403
404		if (contact.getPresences().size() > 1) {
405			contactJidTv.setText(contact.getDisplayJid() + " ("
406					+ contact.getPresences().size() + ")");
407		} else {
408			contactJidTv.setText(contact.getDisplayJid());
409		}
410		String account;
411		if (Config.DOMAIN_LOCK != null) {
412			account = contact.getAccount().getJid().getLocalpart();
413		} else {
414			account = contact.getAccount().getJid().toBareJid().toString();
415		}
416		accountJidTv.setText(getString(R.string.using_account, account));
417		badge.setImageBitmap(avatarService().get(contact, getPixel(72)));
418		badge.setOnClickListener(this.onBadgeClick);
419
420		keys.removeAllViews();
421		boolean hasKeys = false;
422		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
423		if (Config.supportOtr()) {
424			for (final String otrFingerprint : contact.getOtrFingerprints()) {
425				hasKeys = true;
426				View view = inflater.inflate(R.layout.contact_key, keys, false);
427				TextView key = (TextView) view.findViewById(R.id.key);
428				TextView keyType = (TextView) view.findViewById(R.id.key_type);
429				ImageButton removeButton = (ImageButton) view
430						.findViewById(R.id.button_remove);
431				removeButton.setVisibility(View.VISIBLE);
432				key.setText(CryptoHelper.prettifyFingerprint(otrFingerprint));
433				if (otrFingerprint != null && otrFingerprint.equals(messageFingerprint)) {
434					keyType.setText(R.string.otr_fingerprint_selected_message);
435					keyType.setTextColor(getResources().getColor(R.color.accent));
436				} else {
437					keyType.setText(R.string.otr_fingerprint);
438				}
439				keys.addView(view);
440				removeButton.setOnClickListener(new OnClickListener() {
441
442					@Override
443					public void onClick(View v) {
444						confirmToDeleteFingerprint(otrFingerprint);
445					}
446				});
447			}
448		}
449		if (Config.supportOmemo()) {
450			for (final XmppAxolotlSession session : contact.getAccount().getAxolotlService().findSessionsForContact(contact)) {
451				if (!session.getTrust().isCompromised()) {
452					boolean highlight = session.getFingerprint().equals(messageFingerprint);
453					hasKeys = true;
454					addFingerprintRow(keys, session, highlight);
455				}
456			}
457		}
458		if (Config.supportOpenPgp() && contact.getPgpKeyId() != 0) {
459			hasKeys = true;
460			View view = inflater.inflate(R.layout.contact_key, keys, false);
461			TextView key = (TextView) view.findViewById(R.id.key);
462			TextView keyType = (TextView) view.findViewById(R.id.key_type);
463			keyType.setText(R.string.openpgp_key_id);
464			if ("pgp".equals(messageFingerprint)) {
465				keyType.setTextColor(getResources().getColor(R.color.accent));
466			}
467			key.setText(OpenPgpUtils.convertKeyIdToHex(contact.getPgpKeyId()));
468			view.setOnClickListener(new OnClickListener() {
469
470				@Override
471				public void onClick(View v) {
472					PgpEngine pgp = ContactDetailsActivity.this.xmppConnectionService
473						.getPgpEngine();
474					if (pgp != null) {
475						PendingIntent intent = pgp.getIntentForKey(contact);
476						if (intent != null) {
477							try {
478								startIntentSenderForResult(
479										intent.getIntentSender(), 0, null, 0,
480										0, 0);
481							} catch (SendIntentException e) {
482
483							}
484						}
485					}
486				}
487			});
488			keys.addView(view);
489		}
490		if (hasKeys) {
491			keys.setVisibility(View.VISIBLE);
492		} else {
493			keys.setVisibility(View.GONE);
494		}
495
496		List<ListItem.Tag> tagList = contact.getTags(this);
497		if (tagList.size() == 0 || !this.showDynamicTags) {
498			tags.setVisibility(View.GONE);
499		} else {
500			tags.setVisibility(View.VISIBLE);
501			tags.removeAllViewsInLayout();
502			for(final ListItem.Tag tag : tagList) {
503				final TextView tv = (TextView) inflater.inflate(R.layout.list_item_tag,tags,false);
504				tv.setText(tag.getName());
505				tv.setBackgroundColor(tag.getColor());
506				tags.addView(tv);
507			}
508		}
509	}
510
511	protected void confirmToDeleteFingerprint(final String fingerprint) {
512		AlertDialog.Builder builder = new AlertDialog.Builder(this);
513		builder.setTitle(R.string.delete_fingerprint);
514		builder.setMessage(R.string.sure_delete_fingerprint);
515		builder.setNegativeButton(R.string.cancel, null);
516		builder.setPositiveButton(R.string.delete,
517				new android.content.DialogInterface.OnClickListener() {
518
519					@Override
520					public void onClick(DialogInterface dialog, int which) {
521						if (contact.deleteOtrFingerprint(fingerprint)) {
522							populateView();
523							xmppConnectionService.syncRosterToDisk(contact.getAccount());
524						}
525					}
526
527				});
528		builder.create().show();
529	}
530
531	public void onBackendConnected() {
532		if (accountJid != null && contactJid != null) {
533			Account account = xmppConnectionService.findAccountByJid(accountJid);
534			if (account == null) {
535				return;
536			}
537			this.contact = account.getRoster().getContact(contactJid);
538			if (mPendingFingerprintVerificationUri != null) {
539				processFingerprintVerification(mPendingFingerprintVerificationUri);
540				mPendingFingerprintVerificationUri = null;
541			}
542			populateView();
543		}
544	}
545
546	@Override
547	public void onKeyStatusUpdated(AxolotlService.FetchStatus report) {
548		refreshUi();
549	}
550
551	@Override
552	protected void processFingerprintVerification(XmppUri uri) {
553		if (contact != null && contact.getJid().toBareJid().equals(uri.getJid()) && uri.hasFingerprints()) {
554			if (xmppConnectionService.verifyFingerprints(contact,uri.getFingerprints())) {
555				Toast.makeText(this,R.string.verified_fingerprints,Toast.LENGTH_SHORT).show();
556			}
557		} else {
558			Toast.makeText(this,R.string.invalid_barcode,Toast.LENGTH_SHORT).show();
559		}
560	}
561}