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