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