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