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 protected void refreshUiReal() {
163 invalidateOptionsMenu();
164 populateView();
165 }
166
167 @Override
168 protected String getShareableUri() {
169 if (contact != null) {
170 return contact.getShareableUri();
171 } else {
172 return "";
173 }
174 }
175
176 @Override
177 protected void onCreate(final Bundle savedInstanceState) {
178 super.onCreate(savedInstanceState);
179 if (getIntent().getAction().equals(ACTION_VIEW_CONTACT)) {
180 try {
181 this.accountJid = Jid.fromString(getIntent().getExtras().getString("account"));
182 } catch (final InvalidJidException ignored) {
183 }
184 try {
185 this.contactJid = Jid.fromString(getIntent().getExtras().getString("contact"));
186 } catch (final InvalidJidException ignored) {
187 }
188 }
189 setContentView(R.layout.activity_contact_details);
190
191 contactJidTv = (TextView) findViewById(R.id.details_contactjid);
192 accountJidTv = (TextView) findViewById(R.id.details_account);
193 lastseen = (TextView) findViewById(R.id.details_lastseen);
194 send = (CheckBox) findViewById(R.id.details_send_presence);
195 receive = (CheckBox) findViewById(R.id.details_receive_presence);
196 badge = (QuickContactBadge) findViewById(R.id.details_contact_badge);
197 addContactButton = (Button) findViewById(R.id.add_contact_button);
198 addContactButton.setOnClickListener(new OnClickListener() {
199 @Override
200 public void onClick(View view) {
201 showAddToRosterDialog(contact);
202 }
203 });
204 keys = (LinearLayout) findViewById(R.id.details_contact_keys);
205 tags = (LinearLayout) findViewById(R.id.tags);
206 if (getActionBar() != null) {
207 getActionBar().setHomeButtonEnabled(true);
208 getActionBar().setDisplayHomeAsUpEnabled(true);
209 }
210
211 final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
212 this.showDynamicTags = preferences.getBoolean("show_dynamic_tags",false);
213 }
214
215 @Override
216 public boolean onOptionsItemSelected(final MenuItem menuItem) {
217 final AlertDialog.Builder builder = new AlertDialog.Builder(this);
218 builder.setNegativeButton(getString(R.string.cancel), null);
219 switch (menuItem.getItemId()) {
220 case android.R.id.home:
221 finish();
222 break;
223 case R.id.action_delete_contact:
224 builder.setTitle(getString(R.string.action_delete_contact))
225 .setMessage(
226 getString(R.string.remove_contact_text,
227 contact.getJid()))
228 .setPositiveButton(getString(R.string.delete),
229 removeFromRoster).create().show();
230 break;
231 case R.id.action_edit_contact:
232 if (contact.getSystemAccount() == null) {
233 quickEdit(contact.getDisplayName(), new OnValueEdited() {
234
235 @Override
236 public void onValueEdited(String value) {
237 contact.setServerName(value);
238 ContactDetailsActivity.this.xmppConnectionService
239 .pushContactToServer(contact);
240 populateView();
241 }
242 });
243 } else {
244 Intent intent = new Intent(Intent.ACTION_EDIT);
245 String[] systemAccount = contact.getSystemAccount().split("#");
246 long id = Long.parseLong(systemAccount[0]);
247 Uri uri = Contacts.getLookupUri(id, systemAccount[1]);
248 intent.setDataAndType(uri, Contacts.CONTENT_ITEM_TYPE);
249 intent.putExtra("finishActivityOnSaveCompleted", true);
250 startActivity(intent);
251 }
252 break;
253 case R.id.action_block:
254 BlockContactDialog.show(this, xmppConnectionService, contact);
255 break;
256 case R.id.action_unblock:
257 BlockContactDialog.show(this, xmppConnectionService, contact);
258 break;
259 }
260 return super.onOptionsItemSelected(menuItem);
261 }
262
263 @Override
264 public boolean onCreateOptionsMenu(final Menu menu) {
265 getMenuInflater().inflate(R.menu.contact_details, menu);
266 MenuItem block = menu.findItem(R.id.action_block);
267 MenuItem unblock = menu.findItem(R.id.action_unblock);
268 MenuItem edit = menu.findItem(R.id.action_edit_contact);
269 MenuItem delete = menu.findItem(R.id.action_delete_contact);
270 if (contact == null) {
271 return true;
272 }
273 final XmppConnection connection = contact.getAccount().getXmppConnection();
274 if (connection != null && connection.getFeatures().blocking()) {
275 if (this.contact.isBlocked()) {
276 block.setVisible(false);
277 } else {
278 unblock.setVisible(false);
279 }
280 } else {
281 unblock.setVisible(false);
282 block.setVisible(false);
283 }
284 if (!contact.showInRoster()) {
285 edit.setVisible(false);
286 delete.setVisible(false);
287 }
288 return true;
289 }
290
291 private void populateView() {
292 invalidateOptionsMenu();
293 setTitle(contact.getDisplayName());
294 if (contact.showInRoster()) {
295 send.setVisibility(View.VISIBLE);
296 receive.setVisibility(View.VISIBLE);
297 addContactButton.setVisibility(View.GONE);
298 send.setOnCheckedChangeListener(null);
299 receive.setOnCheckedChangeListener(null);
300
301 if (contact.getOption(Contact.Options.FROM)) {
302 send.setText(R.string.send_presence_updates);
303 send.setChecked(true);
304 } else if (contact.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
305 send.setChecked(false);
306 send.setText(R.string.send_presence_updates);
307 } else {
308 send.setText(R.string.preemptively_grant);
309 if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
310 send.setChecked(true);
311 } else {
312 send.setChecked(false);
313 }
314 }
315 if (contact.getOption(Contact.Options.TO)) {
316 receive.setText(R.string.receive_presence_updates);
317 receive.setChecked(true);
318 } else {
319 receive.setText(R.string.ask_for_presence_updates);
320 if (contact.getOption(Contact.Options.ASKING)) {
321 receive.setChecked(true);
322 } else {
323 receive.setChecked(false);
324 }
325 }
326 if (contact.getAccount().isOnlineAndConnected()) {
327 receive.setEnabled(true);
328 send.setEnabled(true);
329 } else {
330 receive.setEnabled(false);
331 send.setEnabled(false);
332 }
333
334 send.setOnCheckedChangeListener(this.mOnSendCheckedChange);
335 receive.setOnCheckedChangeListener(this.mOnReceiveCheckedChange);
336 } else {
337 addContactButton.setVisibility(View.VISIBLE);
338 send.setVisibility(View.GONE);
339 receive.setVisibility(View.GONE);
340 }
341
342 if (contact.isBlocked() && !this.showDynamicTags) {
343 lastseen.setText(R.string.contact_blocked);
344 } else {
345 lastseen.setText(UIHelper.lastseen(getApplicationContext(), contact.lastseen.time));
346 }
347
348 if (contact.getPresences().size() > 1) {
349 contactJidTv.setText(contact.getJid() + " ("
350 + contact.getPresences().size() + ")");
351 } else {
352 contactJidTv.setText(contact.getJid().toString());
353 }
354 accountJidTv.setText(getString(R.string.using_account, contact.getAccount().getJid().toBareJid()));
355 badge.setImageBitmap(avatarService().get(contact, getPixel(72)));
356 badge.setOnClickListener(this.onBadgeClick);
357
358 keys.removeAllViews();
359 boolean hasKeys = false;
360 LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
361 for(final String otrFingerprint : contact.getOtrFingerprints()) {
362 hasKeys = true;
363 View view = inflater.inflate(R.layout.contact_key, keys, false);
364 TextView key = (TextView) view.findViewById(R.id.key);
365 TextView keyType = (TextView) view.findViewById(R.id.key_type);
366 ImageButton remove = (ImageButton) view
367 .findViewById(R.id.button_remove);
368 remove.setVisibility(View.VISIBLE);
369 keyType.setText("OTR Fingerprint");
370 key.setText(CryptoHelper.prettifyFingerprint(otrFingerprint));
371 keys.addView(view);
372 remove.setOnClickListener(new OnClickListener() {
373
374 @Override
375 public void onClick(View v) {
376 confirmToDeleteFingerprint(otrFingerprint);
377 }
378 });
379 }
380 for(final IdentityKey identityKey : xmppConnectionService.databaseBackend.loadIdentityKeys(
381 contact.getAccount(), contact.getJid().toBareJid().toString())) {
382 hasKeys = true;
383 View view = inflater.inflate(R.layout.contact_key, keys, false);
384 TextView key = (TextView) view.findViewById(R.id.key);
385 TextView keyType = (TextView) view.findViewById(R.id.key_type);
386 ImageButton remove = (ImageButton) view
387 .findViewById(R.id.button_remove);
388 remove.setVisibility(View.VISIBLE);
389 keyType.setText("Axolotl Fingerprint");
390 key.setText(identityKey.getFingerprint());
391 keys.addView(view);
392 remove.setOnClickListener(new OnClickListener() {
393
394 @Override
395 public void onClick(View v) {
396 //confirmToDeleteFingerprint(otrFingerprint);
397 }
398 });
399 }
400 if (contact.getPgpKeyId() != 0) {
401 hasKeys = true;
402 View view = inflater.inflate(R.layout.contact_key, keys, false);
403 TextView key = (TextView) view.findViewById(R.id.key);
404 TextView keyType = (TextView) view.findViewById(R.id.key_type);
405 keyType.setText("PGP Key ID");
406 key.setText(OpenPgpUtils.convertKeyIdToHex(contact.getPgpKeyId()));
407 view.setOnClickListener(new OnClickListener() {
408
409 @Override
410 public void onClick(View v) {
411 PgpEngine pgp = ContactDetailsActivity.this.xmppConnectionService
412 .getPgpEngine();
413 if (pgp != null) {
414 PendingIntent intent = pgp.getIntentForKey(contact);
415 if (intent != null) {
416 try {
417 startIntentSenderForResult(
418 intent.getIntentSender(), 0, null, 0,
419 0, 0);
420 } catch (SendIntentException e) {
421
422 }
423 }
424 }
425 }
426 });
427 keys.addView(view);
428 }
429 if (hasKeys) {
430 keys.setVisibility(View.VISIBLE);
431 } else {
432 keys.setVisibility(View.GONE);
433 }
434
435 List<ListItem.Tag> tagList = contact.getTags();
436 if (tagList.size() == 0 || !this.showDynamicTags) {
437 tags.setVisibility(View.GONE);
438 } else {
439 tags.setVisibility(View.VISIBLE);
440 tags.removeAllViewsInLayout();
441 for(final ListItem.Tag tag : tagList) {
442 final TextView tv = (TextView) inflater.inflate(R.layout.list_item_tag,tags,false);
443 tv.setText(tag.getName());
444 tv.setBackgroundColor(tag.getColor());
445 tags.addView(tv);
446 }
447 }
448 }
449
450 protected void confirmToDeleteFingerprint(final String fingerprint) {
451 AlertDialog.Builder builder = new AlertDialog.Builder(this);
452 builder.setTitle(R.string.delete_fingerprint);
453 builder.setMessage(R.string.sure_delete_fingerprint);
454 builder.setNegativeButton(R.string.cancel, null);
455 builder.setPositiveButton(R.string.delete,
456 new android.content.DialogInterface.OnClickListener() {
457
458 @Override
459 public void onClick(DialogInterface dialog, int which) {
460 if (contact.deleteOtrFingerprint(fingerprint)) {
461 populateView();
462 xmppConnectionService.syncRosterToDisk(contact.getAccount());
463 }
464 }
465
466 });
467 builder.create().show();
468 }
469
470 @Override
471 public void onBackendConnected() {
472 if ((accountJid != null) && (contactJid != null)) {
473 Account account = xmppConnectionService
474 .findAccountByJid(accountJid);
475 if (account == null) {
476 return;
477 }
478 this.contact = account.getRoster().getContact(contactJid);
479 populateView();
480 }
481 }
482
483 @Override
484 public void OnUpdateBlocklist(final Status status) {
485 runOnUiThread(new Runnable() {
486
487 @Override
488 public void run() {
489 invalidateOptionsMenu();
490 populateView();
491 }
492 });
493 }
494}