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