anotherim/src/main/java/eu/siacs/conversations/ui/ContactDetailsActivity.java
Sam Whited af7a64491f Add support for XEP-0191 (Blocking command)
Fixes #791

Squash of commits:
534f25d7dae3ce6852243e28fdd0a69ac01e9463
808fdf5147f27a912a60bee39aa4bf1ddd4f43b4
1eaf8a8330710ad35ba7c368e04f909af623ae4c
31585242c2359efdcd0eeddb9745077f54dbc9eb
2e69bd0bd0286ed1e98a42f4c3421ba4d8cf524b
e904fb5015bf3a1904ab941a1957edf3b1e7abd2
eebbadf3b3816bbf8fcccb763e419fed252d266f
7c5b87724ce494e5a6e8026557ed50a8fd9f23e8
b0eaaf446937794fe19cbdb4f8309c3ff83d4e42
8c652f9e8bb3512958d9ad8c6f1326505f2d98c8
ad0ea1ad948ff6f8fde7b0b10f5163dc8852032f
f5d49897e0dba691ef53a0eddb9ed34d129ad442
a08fa64c505bd895b7c626cfad182380373be20b
de67079113e08394a276048c31f6b21baa300829
9069f342173ba30c2b20c67529c7ff497a6a257d
0169fa79d161ee898c4b6762e207087682a952d8
8585a5bd75a5d56927fed8317729bd15fffe4dcc
0053528a078369e0b65dcf71bda251072a1299c7
e901a9c3554bd7cca193e92919b463991eadfea7
c5c78257434813c69ab9b7558bcc8f7cbe858433
e905af348d46d77bc46b5f7211527684acc02fab
13a0f9a10c7892b0f90f5fabd2f2615701b0fd66
2cfba1e24b0139839e4453b92be7e20634d150cf
58e074fb5bb44b05a8104250fccd7c024c808c1a
0d6cf98fc8eab212d798ac79b336f9b70a14f06d
e23620f56b85bcab9f3b5d9ce1c01524cd9674dc
d72cd2fcc8d54176c3ff53411a69b9bb4642eff3
195143dff8836623a37094a6b8fa6aa01ef31580
5f5f3caf3a1e480a99d27ee5c34ba516419c52e4
1dee3d5861c9f9c710da4cbda3688d94c622ca93
23949b8aa32c78b27bab49bb3c4f3ff588925ce1
9bf97f8ae522796e0dacb7f6fe7a7f90f86a93a1
2014-12-22 08:19:00 -05:00

432 lines
14 KiB
Java

package eu.siacs.conversations.ui;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.ContactsContract.CommonDataKinds;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Intents;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.QuickContactBadge;
import android.widget.TextView;
import org.openintents.openpgp.util.OpenPgpUtils;
import java.util.List;
import eu.siacs.conversations.R;
import eu.siacs.conversations.crypto.PgpEngine;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.ListItem;
import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate;
import eu.siacs.conversations.utils.UIHelper;
import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
import eu.siacs.conversations.xmpp.jid.InvalidJidException;
import eu.siacs.conversations.xmpp.jid.Jid;
public class ContactDetailsActivity extends XmppActivity implements OnAccountUpdate, OnRosterUpdate, OnUpdateBlocklist {
public static final String ACTION_VIEW_CONTACT = "view_contact";
private Contact contact;
private DialogInterface.OnClickListener removeFromRoster = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ContactDetailsActivity.this.xmppConnectionService
.deleteContactOnServer(contact);
ContactDetailsActivity.this.finish();
}
};
private OnCheckedChangeListener mOnSendCheckedChange = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
if (contact
.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
xmppConnectionService.sendPresencePacket(contact
.getAccount(),
xmppConnectionService.getPresenceGenerator()
.sendPresenceUpdatesTo(contact));
} else {
contact.setOption(Contact.Options.PREEMPTIVE_GRANT);
}
} else {
contact.resetOption(Contact.Options.PREEMPTIVE_GRANT);
xmppConnectionService.sendPresencePacket(contact.getAccount(),
xmppConnectionService.getPresenceGenerator()
.stopPresenceUpdatesTo(contact));
}
}
};
private OnCheckedChangeListener mOnReceiveCheckedChange = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
xmppConnectionService.sendPresencePacket(contact.getAccount(),
xmppConnectionService.getPresenceGenerator()
.requestPresenceUpdatesFrom(contact));
} else {
xmppConnectionService.sendPresencePacket(contact.getAccount(),
xmppConnectionService.getPresenceGenerator()
.stopPresenceUpdatesFrom(contact));
}
}
};
private Jid accountJid;
private Jid contactJid;
private TextView contactJidTv;
private TextView accountJidTv;
private TextView lastseen;
private CheckBox send;
private CheckBox receive;
private QuickContactBadge badge;
private LinearLayout keys;
private LinearLayout tags;
private boolean showDynamicTags;
private DialogInterface.OnClickListener addToPhonebook = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
intent.setType(Contacts.CONTENT_ITEM_TYPE);
intent.putExtra(Intents.Insert.IM_HANDLE, contact.getJid().toString());
intent.putExtra(Intents.Insert.IM_PROTOCOL,
CommonDataKinds.Im.PROTOCOL_JABBER);
intent.putExtra("finishActivityOnSaveCompleted", true);
ContactDetailsActivity.this.startActivityForResult(intent, 0);
}
};
private OnClickListener onBadgeClick = new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(
ContactDetailsActivity.this);
builder.setTitle(getString(R.string.action_add_phone_book));
builder.setMessage(getString(R.string.add_phone_book_text,
contact.getJid()));
builder.setNegativeButton(getString(R.string.cancel), null);
builder.setPositiveButton(getString(R.string.add), addToPhonebook);
builder.create().show();
}
};
@Override
public void onRosterUpdate() {
runOnUiThread(new Runnable() {
@Override
public void run() {
populateView();
}
});
}
@Override
public void onAccountUpdate() {
runOnUiThread(new Runnable() {
@Override
public void run() {
populateView();
}
});
}
@Override
protected String getShareableUri() {
if (contact != null) {
return contact.getShareableUri();
} else {
return "";
}
}
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getIntent().getAction().equals(ACTION_VIEW_CONTACT)) {
try {
this.accountJid = Jid.fromString(getIntent().getExtras().getString("account"));
} catch (final InvalidJidException ignored) {
}
try {
this.contactJid = Jid.fromString(getIntent().getExtras().getString("contact"));
} catch (final InvalidJidException ignored) {
}
}
setContentView(R.layout.activity_contact_details);
contactJidTv = (TextView) findViewById(R.id.details_contactjid);
accountJidTv = (TextView) findViewById(R.id.details_account);
lastseen = (TextView) findViewById(R.id.details_lastseen);
send = (CheckBox) findViewById(R.id.details_send_presence);
receive = (CheckBox) findViewById(R.id.details_receive_presence);
badge = (QuickContactBadge) findViewById(R.id.details_contact_badge);
keys = (LinearLayout) findViewById(R.id.details_contact_keys);
tags = (LinearLayout) findViewById(R.id.tags);
if (getActionBar() != null) {
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
this.showDynamicTags = preferences.getBoolean("show_dynamic_tags",false);
}
@Override
public boolean onOptionsItemSelected(final MenuItem menuItem) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setNegativeButton(getString(R.string.cancel), null);
switch (menuItem.getItemId()) {
case android.R.id.home:
finish();
break;
case R.id.action_delete_contact:
builder.setTitle(getString(R.string.action_delete_contact))
.setMessage(
getString(R.string.remove_contact_text,
contact.getJid()))
.setPositiveButton(getString(R.string.delete),
removeFromRoster).create().show();
break;
case R.id.action_edit_contact:
if (contact.getSystemAccount() == null) {
quickEdit(contact.getDisplayName(), new OnValueEdited() {
@Override
public void onValueEdited(String value) {
contact.setServerName(value);
ContactDetailsActivity.this.xmppConnectionService
.pushContactToServer(contact);
populateView();
}
});
} else {
Intent intent = new Intent(Intent.ACTION_EDIT);
String[] systemAccount = contact.getSystemAccount().split("#");
long id = Long.parseLong(systemAccount[0]);
Uri uri = Contacts.getLookupUri(id, systemAccount[1]);
intent.setDataAndType(uri, Contacts.CONTENT_ITEM_TYPE);
intent.putExtra("finishActivityOnSaveCompleted", true);
startActivity(intent);
}
break;
}
return super.onOptionsItemSelected(menuItem);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.contact_details, menu);
return true;
}
private void populateView() {
send.setOnCheckedChangeListener(null);
receive.setOnCheckedChangeListener(null);
setTitle(contact.getDisplayName());
if (contact.getOption(Contact.Options.FROM)) {
send.setText(R.string.send_presence_updates);
send.setChecked(true);
} else if (contact
.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
send.setChecked(false);
send.setText(R.string.send_presence_updates);
} else {
send.setText(R.string.preemptively_grant);
if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
send.setChecked(true);
} else {
send.setChecked(false);
}
}
if (contact.getOption(Contact.Options.TO)) {
receive.setText(R.string.receive_presence_updates);
receive.setChecked(true);
} else {
receive.setText(R.string.ask_for_presence_updates);
if (contact.getOption(Contact.Options.ASKING)) {
receive.setChecked(true);
} else {
receive.setChecked(false);
}
}
if (contact.getAccount().getStatus() == Account.State.ONLINE) {
receive.setEnabled(true);
send.setEnabled(true);
} else {
receive.setEnabled(false);
send.setEnabled(false);
}
send.setOnCheckedChangeListener(this.mOnSendCheckedChange);
receive.setOnCheckedChangeListener(this.mOnReceiveCheckedChange);
lastseen.setText(UIHelper.lastseen(getApplicationContext(),
contact.lastseen.time));
if (contact.getPresences().size() > 1) {
contactJidTv.setText(contact.getJid() + " ("
+ contact.getPresences().size() + ")");
} else {
contactJidTv.setText(contact.getJid().toString());
}
accountJidTv.setText(getString(R.string.using_account, contact
.getAccount().getJid().toBareJid()));
prepareContactBadge(badge, contact);
if (contact.getSystemAccount() == null) {
badge.setOnClickListener(onBadgeClick);
}
keys.removeAllViews();
boolean hasKeys = false;
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for(final String otrFingerprint : contact.getOtrFingerprints()) {
hasKeys = true;
View view = inflater.inflate(R.layout.contact_key, keys, false);
TextView key = (TextView) view.findViewById(R.id.key);
TextView keyType = (TextView) view.findViewById(R.id.key_type);
ImageButton remove = (ImageButton) view
.findViewById(R.id.button_remove);
remove.setVisibility(View.VISIBLE);
keyType.setText("OTR Fingerprint");
key.setText(otrFingerprint);
keys.addView(view);
remove.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
confirmToDeleteFingerprint(otrFingerprint);
}
});
}
if (contact.getPgpKeyId() != 0) {
hasKeys = true;
View view = inflater.inflate(R.layout.contact_key, keys, false);
TextView key = (TextView) view.findViewById(R.id.key);
TextView keyType = (TextView) view.findViewById(R.id.key_type);
keyType.setText("PGP Key ID");
key.setText(OpenPgpUtils.convertKeyIdToHex(contact.getPgpKeyId()));
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
PgpEngine pgp = ContactDetailsActivity.this.xmppConnectionService
.getPgpEngine();
if (pgp != null) {
PendingIntent intent = pgp.getIntentForKey(contact);
if (intent != null) {
try {
startIntentSenderForResult(
intent.getIntentSender(), 0, null, 0,
0, 0);
} catch (SendIntentException e) {
}
}
}
}
});
keys.addView(view);
}
if (hasKeys) {
keys.setVisibility(View.VISIBLE);
} else {
keys.setVisibility(View.GONE);
}
List<ListItem.Tag> tagList = contact.getTags();
if (tagList.size() == 0 || !this.showDynamicTags) {
tags.setVisibility(View.GONE);
} else {
tags.setVisibility(View.VISIBLE);
tags.removeAllViewsInLayout();
for(final ListItem.Tag tag : tagList) {
final TextView tv = (TextView) inflater.inflate(R.layout.list_item_tag,tags,false);
tv.setText(tag.getName());
tv.setBackgroundColor(tag.getColor());
tags.addView(tv);
}
}
}
private void prepareContactBadge(QuickContactBadge badge, Contact contact) {
if (contact.getSystemAccount() != null) {
String[] systemAccount = contact.getSystemAccount().split("#");
long id = Long.parseLong(systemAccount[0]);
badge.assignContactUri(Contacts.getLookupUri(id, systemAccount[1]));
}
badge.setImageBitmap(avatarService().get(contact, getPixel(72)));
}
protected void confirmToDeleteFingerprint(final String fingerprint) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.delete_fingerprint);
builder.setMessage(R.string.sure_delete_fingerprint);
builder.setNegativeButton(R.string.cancel, null);
builder.setPositiveButton(R.string.delete,
new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (contact.deleteOtrFingerprint(fingerprint)) {
populateView();
xmppConnectionService.syncRosterToDisk(contact
.getAccount());
}
}
});
builder.create().show();
}
@Override
public void onBackendConnected() {
if ((accountJid != null) && (contactJid != null)) {
Account account = xmppConnectionService
.findAccountByJid(accountJid);
if (account == null) {
return;
}
this.contact = account.getRoster().getContact(contactJid);
populateView();
}
}
@Override
public void OnUpdateBlocklist(final Status status) {
runOnUiThread(new Runnable() {
@Override
public void run() {
populateView();
}
});
}
}