simplified presence subscription in contact details

This commit is contained in:
iNPUTmice 2014-07-20 03:14:47 +02:00
parent 185c485fc6
commit b5005d60de
2 changed files with 83 additions and 106 deletions

View file

@ -30,14 +30,11 @@ public class PresenceParser extends AbstractParser implements
Conversation muc = mXmppConnectionService.find(account,packet Conversation muc = mXmppConnectionService.find(account,packet
.getAttribute("from").split("/")[0]); .getAttribute("from").split("/")[0]);
if (muc != null) { if (muc != null) {
int error = muc.getMucOptions().getError();
muc.getMucOptions().processPacket(packet, mPgpEngine); muc.getMucOptions().processPacket(packet, mPgpEngine);
if (muc.getMucOptions().getError() != error) { }
}
mXmppConnectionService.updateConversationUi(); mXmppConnectionService.updateConversationUi();
} }
}
}
}
public void parseContactPresence(PresencePacket packet, Account account) { public void parseContactPresence(PresencePacket packet, Account account) {
PresenceGenerator mPresenceGenerator = mXmppConnectionService.getPresenceGenerator(); PresenceGenerator mPresenceGenerator = mXmppConnectionService.getPresenceGenerator();

View file

@ -21,19 +21,18 @@ import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.widget.CheckBox; import android.widget.CheckBox;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.CompoundButton;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.QuickContactBadge; import android.widget.QuickContactBadge;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import eu.siacs.conversations.R; import eu.siacs.conversations.R;
import eu.siacs.conversations.crypto.PgpEngine; import eu.siacs.conversations.crypto.PgpEngine;
import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Presences; import eu.siacs.conversations.entities.Presences;
import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate; import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate;
import eu.siacs.conversations.utils.UIHelper; import eu.siacs.conversations.utils.UIHelper;
import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
public class ContactDetailsActivity extends XmppActivity { public class ContactDetailsActivity extends XmppActivity {
public static final String ACTION_VIEW_CONTACT = "view_contact"; public static final String ACTION_VIEW_CONTACT = "view_contact";
@ -81,7 +80,8 @@ public class ContactDetailsActivity extends XmppActivity {
public void onClick(View v) { public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity); AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle(getString(R.string.action_add_phone_book)); builder.setTitle(getString(R.string.action_add_phone_book));
builder.setMessage(getString(R.string.add_phone_book_text, contact.getJid())); builder.setMessage(getString(R.string.add_phone_book_text,
contact.getJid()));
builder.setNegativeButton(getString(R.string.cancel), null); builder.setNegativeButton(getString(R.string.cancel), null);
builder.setPositiveButton(getString(R.string.add), addToPhonebook); builder.setPositiveButton(getString(R.string.add), addToPhonebook);
builder.create().show(); builder.create().show();
@ -104,6 +104,46 @@ public class ContactDetailsActivity extends XmppActivity {
} }
}; };
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));
}
}
};
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -118,7 +158,9 @@ public class ContactDetailsActivity extends XmppActivity {
status = (TextView) findViewById(R.id.details_contactstatus); status = (TextView) findViewById(R.id.details_contactstatus);
lastseen = (TextView) findViewById(R.id.details_lastseen); lastseen = (TextView) findViewById(R.id.details_lastseen);
send = (CheckBox) findViewById(R.id.details_send_presence); send = (CheckBox) findViewById(R.id.details_send_presence);
send.setOnCheckedChangeListener(this.mOnSendCheckedChange);
receive = (CheckBox) findViewById(R.id.details_receive_presence); receive = (CheckBox) findViewById(R.id.details_receive_presence);
receive.setOnCheckedChangeListener(this.mOnReceiveCheckedChange);
badge = (QuickContactBadge) findViewById(R.id.details_contact_badge); badge = (QuickContactBadge) findViewById(R.id.details_contact_badge);
keys = (LinearLayout) findViewById(R.id.details_contact_keys); keys = (LinearLayout) findViewById(R.id.details_contact_keys);
getActionBar().setHomeButtonEnabled(true); getActionBar().setHomeButtonEnabled(true);
@ -139,8 +181,8 @@ public class ContactDetailsActivity extends XmppActivity {
.setMessage( .setMessage(
getString(R.string.remove_contact_text, getString(R.string.remove_contact_text,
contact.getJid())) contact.getJid()))
.setPositiveButton(getString(R.string.delete), removeFromRoster).create() .setPositiveButton(getString(R.string.delete),
.show(); removeFromRoster).create().show();
break; break;
case R.id.action_edit_contact: case R.id.action_edit_contact:
if (contact.getSystemAccount() == null) { if (contact.getSystemAccount() == null) {
@ -149,7 +191,8 @@ public class ContactDetailsActivity extends XmppActivity {
@Override @Override
public void onValueEdited(String value) { public void onValueEdited(String value) {
contact.setServerName(value); contact.setServerName(value);
activity.xmppConnectionService.pushContactToServer(contact); activity.xmppConnectionService
.pushContactToServer(contact);
populateView(); populateView();
} }
}); });
@ -176,19 +219,22 @@ public class ContactDetailsActivity extends XmppActivity {
private void populateView() { private void populateView() {
setTitle(contact.getDisplayName()); setTitle(contact.getDisplayName());
if (contact.getOption(Contact.Options.FROM)) { if (contact.getOption(Contact.Options.FROM)) {
send.setText(R.string.send_presence_updates);
send.setChecked(true); send.setChecked(true);
} else if (contact.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)){ } else if (contact
.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
send.setChecked(false); send.setChecked(false);
send.setText(R.string.send_presence_updates);
} else { } else {
send.setText(R.string.preemptively_grant); send.setText(R.string.preemptively_grant);
if (contact if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
send.setChecked(true); send.setChecked(true);
} else { } else {
send.setChecked(false); send.setChecked(false);
} }
} }
if (contact.getOption(Contact.Options.TO)) { if (contact.getOption(Contact.Options.TO)) {
receive.setText(R.string.receive_presence_updates);
receive.setChecked(true); receive.setChecked(true);
} else { } else {
receive.setText(R.string.ask_for_presence_updates); receive.setText(R.string.ask_for_presence_updates);
@ -199,7 +245,8 @@ public class ContactDetailsActivity extends XmppActivity {
} }
} }
lastseen.setText(UIHelper.lastseen(getApplicationContext(),contact.lastseen.time)); lastseen.setText(UIHelper.lastseen(getApplicationContext(),
contact.lastseen.time));
switch (contact.getMostAvailableStatus()) { switch (contact.getMostAvailableStatus()) {
case Presences.CHAT: case Presences.CHAT:
@ -232,13 +279,15 @@ public class ContactDetailsActivity extends XmppActivity {
break; break;
} }
if (contact.getPresences().size() > 1) { if (contact.getPresences().size() > 1) {
contactJidTv.setText(contact.getJid()+" ("+contact.getPresences().size()+")"); contactJidTv.setText(contact.getJid() + " ("
+ contact.getPresences().size() + ")");
} else { } else {
contactJidTv.setText(contact.getJid()); contactJidTv.setText(contact.getJid());
} }
accountJidTv.setText(contact.getAccount().getJid()); accountJidTv.setText(contact.getAccount().getJid());
UIHelper.prepareContactBadge(this, badge, contact, getApplicationContext()); UIHelper.prepareContactBadge(this, badge, contact,
getApplicationContext());
if (contact.getSystemAccount() == null) { if (contact.getSystemAccount() == null) {
badge.setOnClickListener(onBadgeClick); badge.setOnClickListener(onBadgeClick);
@ -266,12 +315,15 @@ public class ContactDetailsActivity extends XmppActivity {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
PgpEngine pgp = activity.xmppConnectionService.getPgpEngine(); PgpEngine pgp = activity.xmppConnectionService
.getPgpEngine();
if (pgp != null) { if (pgp != null) {
PendingIntent intent = pgp.getIntentForKey(contact); PendingIntent intent = pgp.getIntentForKey(contact);
if (intent != null) { if (intent != null) {
try { try {
startIntentSenderForResult(intent.getIntentSender(), 0, null, 0, 0, 0); startIntentSenderForResult(
intent.getIntentSender(), 0, null, 0,
0, 0);
} catch (SendIntentException e) { } catch (SendIntentException e) {
} }
@ -287,7 +339,8 @@ public class ContactDetailsActivity extends XmppActivity {
public void onBackendConnected() { public void onBackendConnected() {
xmppConnectionService.setOnRosterUpdateListener(this.rosterUpdate); xmppConnectionService.setOnRosterUpdateListener(this.rosterUpdate);
if ((accountJid != null) && (contactJid != null)) { if ((accountJid != null) && (contactJid != null)) {
Account account = xmppConnectionService.findAccountByJid(accountJid); Account account = xmppConnectionService
.findAccountByJid(accountJid);
if (account == null) { if (account == null) {
return; return;
} }
@ -299,79 +352,6 @@ public class ContactDetailsActivity extends XmppActivity {
@Override @Override
protected void onStop() { protected void onStop() {
super.onStop(); super.onStop();
XmppConnectionService xcs = activity.xmppConnectionService;
PresencePacket packet = null;
boolean updated = false;
if (contact!=null) {
boolean online = contact.getAccount().getStatus() == Account.STATUS_ONLINE;
if (contact.getOption(Contact.Options.FROM)) {
if (!send.isChecked()) {
if (online) {
contact.resetOption(Contact.Options.FROM);
contact.resetOption(Contact.Options.PREEMPTIVE_GRANT);
packet = xcs.getPresenceGenerator().stopPresenceUpdatesTo(contact);
}
updated = true;
}
} else {
if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
if (!send.isChecked()) {
if (online) {
contact.resetOption(Contact.Options.PREEMPTIVE_GRANT);
}
updated = true;
}
} else {
if (send.isChecked()) {
if (online) {
if (contact.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
packet = xcs.getPresenceGenerator().sendPresenceUpdatesTo(contact);
} else {
contact.setOption(Contact.Options.PREEMPTIVE_GRANT);
}
}
updated = true;
}
}
}
if (contact.getOption(Contact.Options.TO)) {
if (!receive.isChecked()) {
if (online) {
contact.resetOption(Contact.Options.TO);
packet = xcs.getPresenceGenerator().stopPresenceUpdatesFrom(contact);
}
updated = true;
}
} else {
if (contact.getOption(Contact.Options.ASKING)) {
if (!receive.isChecked()) {
if (online) {
contact.resetOption(Contact.Options.ASKING);
packet = xcs.getPresenceGenerator().stopPresenceUpdatesFrom(contact);
}
updated = true;
}
} else {
if (receive.isChecked()) {
if (online) {
contact.setOption(Contact.Options.ASKING);
packet = xcs.getPresenceGenerator().requestPresenceUpdatesFrom(contact);
}
updated = true;
}
}
}
if (updated) {
if (online) {
if (packet!=null) {
xcs.sendPresencePacket(contact.getAccount(), packet);
}
Toast.makeText(getApplicationContext(), getString(R.string.subscription_updated), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), getString(R.string.subscription_not_updated_offline), Toast.LENGTH_SHORT).show();
}
}
}
xmppConnectionService.removeOnRosterUpdateListener(); xmppConnectionService.removeOnRosterUpdateListener();
} }