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