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;
 30
 31import org.openintents.openpgp.util.OpenPgpUtils;
 32import org.whispersystems.libaxolotl.IdentityKey;
 33
 34import java.util.List;
 35
 36import eu.siacs.conversations.R;
 37import eu.siacs.conversations.crypto.PgpEngine;
 38import eu.siacs.conversations.crypto.axolotl.AxolotlService;
 39import eu.siacs.conversations.entities.Account;
 40import eu.siacs.conversations.entities.Contact;
 41import eu.siacs.conversations.entities.ListItem;
 42import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
 43import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate;
 44import eu.siacs.conversations.utils.CryptoHelper;
 45import eu.siacs.conversations.utils.UIHelper;
 46import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
 47import eu.siacs.conversations.xmpp.XmppConnection;
 48import eu.siacs.conversations.xmpp.jid.InvalidJidException;
 49import eu.siacs.conversations.xmpp.jid.Jid;
 50
 51public class ContactDetailsActivity extends XmppActivity implements OnAccountUpdate, OnRosterUpdate, OnUpdateBlocklist {
 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	private Jid accountJid;
102	private Jid contactJid;
103	private TextView contactJidTv;
104	private TextView accountJidTv;
105	private TextView lastseen;
106	private CheckBox send;
107	private CheckBox receive;
108	private Button addContactButton;
109	private QuickContactBadge badge;
110	private LinearLayout keys;
111	private LinearLayout tags;
112	private boolean showDynamicTags;
113
114	private DialogInterface.OnClickListener addToPhonebook = new DialogInterface.OnClickListener() {
115
116		@Override
117		public void onClick(DialogInterface dialog, int which) {
118			Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
119			intent.setType(Contacts.CONTENT_ITEM_TYPE);
120			intent.putExtra(Intents.Insert.IM_HANDLE, contact.getJid().toString());
121			intent.putExtra(Intents.Insert.IM_PROTOCOL,
122					CommonDataKinds.Im.PROTOCOL_JABBER);
123			intent.putExtra("finishActivityOnSaveCompleted", true);
124			ContactDetailsActivity.this.startActivityForResult(intent, 0);
125		}
126	};
127
128	private OnClickListener onBadgeClick = new OnClickListener() {
129
130		@Override
131		public void onClick(View v) {
132			if (contact.getSystemAccount() == null) {
133				AlertDialog.Builder builder = new AlertDialog.Builder(
134						ContactDetailsActivity.this);
135				builder.setTitle(getString(R.string.action_add_phone_book));
136				builder.setMessage(getString(R.string.add_phone_book_text,
137						contact.getJid()));
138				builder.setNegativeButton(getString(R.string.cancel), null);
139				builder.setPositiveButton(getString(R.string.add), addToPhonebook);
140				builder.create().show();
141			} else {
142					String[] systemAccount = contact.getSystemAccount().split("#");
143					long id = Long.parseLong(systemAccount[0]);
144					Uri uri = ContactsContract.Contacts.getLookupUri(id, systemAccount[1]);
145					Intent intent = new Intent(Intent.ACTION_VIEW);
146					intent.setData(uri);
147					startActivity(intent);
148			}
149		}
150	};
151
152	@Override
153	public void onRosterUpdate() {
154		refreshUi();
155	}
156
157	@Override
158	public void onAccountUpdate() {
159		refreshUi();
160	}
161
162	@Override
163	protected void refreshUiReal() {
164		invalidateOptionsMenu();
165		populateView();
166	}
167
168	@Override
169	protected String getShareableUri() {
170		if (contact != null) {
171			return contact.getShareableUri();
172		} else {
173			return "";
174		}
175	}
176
177	@Override
178	protected void onCreate(final Bundle savedInstanceState) {
179		super.onCreate(savedInstanceState);
180		if (getIntent().getAction().equals(ACTION_VIEW_CONTACT)) {
181			try {
182				this.accountJid = Jid.fromString(getIntent().getExtras().getString("account"));
183			} catch (final InvalidJidException ignored) {
184			}
185			try {
186				this.contactJid = Jid.fromString(getIntent().getExtras().getString("contact"));
187			} catch (final InvalidJidException ignored) {
188			}
189		}
190		setContentView(R.layout.activity_contact_details);
191
192		contactJidTv = (TextView) findViewById(R.id.details_contactjid);
193		accountJidTv = (TextView) findViewById(R.id.details_account);
194		lastseen = (TextView) findViewById(R.id.details_lastseen);
195		send = (CheckBox) findViewById(R.id.details_send_presence);
196		receive = (CheckBox) findViewById(R.id.details_receive_presence);
197		badge = (QuickContactBadge) findViewById(R.id.details_contact_badge);
198		addContactButton = (Button) findViewById(R.id.add_contact_button);
199		addContactButton.setOnClickListener(new OnClickListener() {
200			@Override
201			public void onClick(View view) {
202				showAddToRosterDialog(contact);
203			}
204		});
205		keys = (LinearLayout) findViewById(R.id.details_contact_keys);
206		tags = (LinearLayout) findViewById(R.id.tags);
207		if (getActionBar() != null) {
208			getActionBar().setHomeButtonEnabled(true);
209			getActionBar().setDisplayHomeAsUpEnabled(true);
210		}
211
212		final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
213		this.showDynamicTags = preferences.getBoolean("show_dynamic_tags",false);
214	}
215
216	@Override
217	public boolean onOptionsItemSelected(final MenuItem menuItem) {
218		final AlertDialog.Builder builder = new AlertDialog.Builder(this);
219		builder.setNegativeButton(getString(R.string.cancel), null);
220		switch (menuItem.getItemId()) {
221			case android.R.id.home:
222				finish();
223				break;
224			case R.id.action_delete_contact:
225				builder.setTitle(getString(R.string.action_delete_contact))
226					.setMessage(
227							getString(R.string.remove_contact_text,
228								contact.getJid()))
229					.setPositiveButton(getString(R.string.delete),
230							removeFromRoster).create().show();
231				break;
232			case R.id.action_edit_contact:
233				if (contact.getSystemAccount() == null) {
234					quickEdit(contact.getDisplayName(), new OnValueEdited() {
235
236						@Override
237						public void onValueEdited(String value) {
238							contact.setServerName(value);
239							ContactDetailsActivity.this.xmppConnectionService
240								.pushContactToServer(contact);
241							populateView();
242						}
243					});
244				} else {
245					Intent intent = new Intent(Intent.ACTION_EDIT);
246					String[] systemAccount = contact.getSystemAccount().split("#");
247					long id = Long.parseLong(systemAccount[0]);
248					Uri uri = Contacts.getLookupUri(id, systemAccount[1]);
249					intent.setDataAndType(uri, Contacts.CONTENT_ITEM_TYPE);
250					intent.putExtra("finishActivityOnSaveCompleted", true);
251					startActivity(intent);
252				}
253				break;
254			case R.id.action_block:
255				BlockContactDialog.show(this, xmppConnectionService, contact);
256				break;
257			case R.id.action_unblock:
258				BlockContactDialog.show(this, xmppConnectionService, contact);
259				break;
260		}
261		return super.onOptionsItemSelected(menuItem);
262	}
263
264	@Override
265	public boolean onCreateOptionsMenu(final Menu menu) {
266		getMenuInflater().inflate(R.menu.contact_details, menu);
267		MenuItem block = menu.findItem(R.id.action_block);
268		MenuItem unblock = menu.findItem(R.id.action_unblock);
269		MenuItem edit = menu.findItem(R.id.action_edit_contact);
270		MenuItem delete = menu.findItem(R.id.action_delete_contact);
271		if (contact == null) {
272			return true;
273		}
274		final XmppConnection connection = contact.getAccount().getXmppConnection();
275		if (connection != null && connection.getFeatures().blocking()) {
276			if (this.contact.isBlocked()) {
277				block.setVisible(false);
278			} else {
279				unblock.setVisible(false);
280			}
281		} else {
282			unblock.setVisible(false);
283			block.setVisible(false);
284		}
285		if (!contact.showInRoster()) {
286			edit.setVisible(false);
287			delete.setVisible(false);
288		}
289		return true;
290	}
291
292	private void populateView() {
293		invalidateOptionsMenu();
294		setTitle(contact.getDisplayName());
295		if (contact.showInRoster()) {
296			send.setVisibility(View.VISIBLE);
297			receive.setVisibility(View.VISIBLE);
298			addContactButton.setVisibility(View.GONE);
299			send.setOnCheckedChangeListener(null);
300			receive.setOnCheckedChangeListener(null);
301
302			if (contact.getOption(Contact.Options.FROM)) {
303				send.setText(R.string.send_presence_updates);
304				send.setChecked(true);
305			} else if (contact.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
306				send.setChecked(false);
307				send.setText(R.string.send_presence_updates);
308			} else {
309				send.setText(R.string.preemptively_grant);
310				if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
311					send.setChecked(true);
312				} else {
313					send.setChecked(false);
314				}
315			}
316			if (contact.getOption(Contact.Options.TO)) {
317				receive.setText(R.string.receive_presence_updates);
318				receive.setChecked(true);
319			} else {
320				receive.setText(R.string.ask_for_presence_updates);
321				if (contact.getOption(Contact.Options.ASKING)) {
322					receive.setChecked(true);
323				} else {
324					receive.setChecked(false);
325				}
326			}
327			if (contact.getAccount().isOnlineAndConnected()) {
328				receive.setEnabled(true);
329				send.setEnabled(true);
330			} else {
331				receive.setEnabled(false);
332				send.setEnabled(false);
333			}
334
335			send.setOnCheckedChangeListener(this.mOnSendCheckedChange);
336			receive.setOnCheckedChangeListener(this.mOnReceiveCheckedChange);
337		} else {
338			addContactButton.setVisibility(View.VISIBLE);
339			send.setVisibility(View.GONE);
340			receive.setVisibility(View.GONE);
341		}
342
343		if (contact.isBlocked() && !this.showDynamicTags) {
344			lastseen.setText(R.string.contact_blocked);
345		} else {
346			lastseen.setText(UIHelper.lastseen(getApplicationContext(), contact.lastseen.time));
347		}
348
349		if (contact.getPresences().size() > 1) {
350			contactJidTv.setText(contact.getJid() + " ("
351					+ contact.getPresences().size() + ")");
352		} else {
353			contactJidTv.setText(contact.getJid().toString());
354		}
355		accountJidTv.setText(getString(R.string.using_account, contact.getAccount().getJid().toBareJid()));
356		badge.setImageBitmap(avatarService().get(contact, getPixel(72)));
357		badge.setOnClickListener(this.onBadgeClick);
358
359		keys.removeAllViews();
360		boolean hasKeys = false;
361		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
362		for(final String otrFingerprint : contact.getOtrFingerprints()) {
363			hasKeys = true;
364			View view = inflater.inflate(R.layout.contact_key, keys, false);
365			TextView key = (TextView) view.findViewById(R.id.key);
366			TextView keyType = (TextView) view.findViewById(R.id.key_type);
367			ImageButton removeButton = (ImageButton) view
368				.findViewById(R.id.button_remove);
369			removeButton.setVisibility(View.VISIBLE);
370			keyType.setText("OTR Fingerprint");
371			key.setText(CryptoHelper.prettifyFingerprint(otrFingerprint));
372			keys.addView(view);
373			removeButton.setOnClickListener(new OnClickListener() {
374
375				@Override
376				public void onClick(View v) {
377					confirmToDeleteFingerprint(otrFingerprint);
378				}
379			});
380		}
381		for(final IdentityKey identityKey : xmppConnectionService.databaseBackend.loadIdentityKeys(
382				contact.getAccount(), contact.getJid().toBareJid().toString())) {
383			hasKeys = true;
384			View view = inflater.inflate(R.layout.contact_key, keys, false);
385			TextView key = (TextView) view.findViewById(R.id.key);
386			TextView keyType = (TextView) view.findViewById(R.id.key_type);
387			TextView keyTrust = (TextView) view.findViewById(R.id.key_trust);
388			ImageButton removeButton = (ImageButton) view
389					.findViewById(R.id.button_remove);
390			ImageButton trustButton = (ImageButton) view
391					.findViewById(R.id.button_trust);
392			final AxolotlService axolotlService = contact.getAccount().getAxolotlService();
393			final String fingerprint = identityKey.getFingerprint().replaceAll("\\s", "");
394			final Jid bareJid = contactJid.toBareJid();
395			AxolotlService.SQLiteAxolotlStore.Trust trust = contact.getAccount().getAxolotlService()
396					.getFingerprintTrust(bareJid.toString(), fingerprint);
397			switch (trust) {
398				case TRUSTED:
399					removeButton.setVisibility(View.VISIBLE);
400					//Log.d(Config.LOGTAG, AxolotlService.getLogprefix(contact.getAccount()) + "Setting remove button visible!");
401					break;
402				case UNDECIDED:
403				case UNTRUSTED:
404					//Log.d(Config.LOGTAG, AxolotlService.getLogprefix(contact.getAccount()) + "Setting trust button visible!");
405					trustButton.setVisibility(View.VISIBLE);
406					break;
407			}
408			keyType.setText("Axolotl Fingerprint");
409			key.setText(CryptoHelper.prettifyFingerprint(identityKey.getFingerprint()));
410			keyTrust.setText(trust.toString());
411			keyTrust.setVisibility(View.VISIBLE);
412			keys.addView(view);
413			removeButton.setOnClickListener(new OnClickListener() {
414				@Override
415				public void onClick(View v) {
416					axolotlService.setFingerprintTrust(bareJid.toString(), fingerprint,
417							AxolotlService.SQLiteAxolotlStore.Trust.UNTRUSTED);
418					refreshUi();
419					xmppConnectionService.updateConversationUi();
420				}
421			});
422			trustButton.setOnClickListener(new OnClickListener() {
423				@Override
424				public void onClick(View v) {
425					axolotlService.setFingerprintTrust(bareJid.toString(), fingerprint,
426							AxolotlService.SQLiteAxolotlStore.Trust.TRUSTED);
427					refreshUi();
428					xmppConnectionService.updateConversationUi();
429				}
430			});
431		}
432		if (contact.getPgpKeyId() != 0) {
433			hasKeys = true;
434			View view = inflater.inflate(R.layout.contact_key, keys, false);
435			TextView key = (TextView) view.findViewById(R.id.key);
436			TextView keyType = (TextView) view.findViewById(R.id.key_type);
437			keyType.setText("PGP Key ID");
438			key.setText(OpenPgpUtils.convertKeyIdToHex(contact.getPgpKeyId()));
439			view.setOnClickListener(new OnClickListener() {
440
441				@Override
442				public void onClick(View v) {
443					PgpEngine pgp = ContactDetailsActivity.this.xmppConnectionService
444						.getPgpEngine();
445					if (pgp != null) {
446						PendingIntent intent = pgp.getIntentForKey(contact);
447						if (intent != null) {
448							try {
449								startIntentSenderForResult(
450										intent.getIntentSender(), 0, null, 0,
451										0, 0);
452							} catch (SendIntentException e) {
453
454							}
455						}
456					}
457				}
458			});
459			keys.addView(view);
460		}
461		if (hasKeys) {
462			keys.setVisibility(View.VISIBLE);
463		} else {
464			keys.setVisibility(View.GONE);
465		}
466
467		List<ListItem.Tag> tagList = contact.getTags();
468		if (tagList.size() == 0 || !this.showDynamicTags) {
469			tags.setVisibility(View.GONE);
470		} else {
471			tags.setVisibility(View.VISIBLE);
472			tags.removeAllViewsInLayout();
473			for(final ListItem.Tag tag : tagList) {
474				final TextView tv = (TextView) inflater.inflate(R.layout.list_item_tag,tags,false);
475				tv.setText(tag.getName());
476				tv.setBackgroundColor(tag.getColor());
477				tags.addView(tv);
478			}
479		}
480	}
481
482	protected void confirmToDeleteFingerprint(final String fingerprint) {
483		AlertDialog.Builder builder = new AlertDialog.Builder(this);
484		builder.setTitle(R.string.delete_fingerprint);
485		builder.setMessage(R.string.sure_delete_fingerprint);
486		builder.setNegativeButton(R.string.cancel, null);
487		builder.setPositiveButton(R.string.delete,
488				new android.content.DialogInterface.OnClickListener() {
489
490					@Override
491					public void onClick(DialogInterface dialog, int which) {
492						if (contact.deleteOtrFingerprint(fingerprint)) {
493							populateView();
494							xmppConnectionService.syncRosterToDisk(contact.getAccount());
495						}
496					}
497
498				});
499		builder.create().show();
500	}
501
502	@Override
503	public void onBackendConnected() {
504		if ((accountJid != null) && (contactJid != null)) {
505			Account account = xmppConnectionService
506				.findAccountByJid(accountJid);
507			if (account == null) {
508				return;
509			}
510			this.contact = account.getRoster().getContact(contactJid);
511			populateView();
512		}
513	}
514
515	@Override
516	public void OnUpdateBlocklist(final Status status) {
517		runOnUiThread(new Runnable() {
518
519			@Override
520			public void run() {
521				invalidateOptionsMenu();
522				populateView();
523			}
524		});
525	}
526}