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