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