auto register/unregister listeners

This commit is contained in:
iNPUTmice 2014-11-11 17:39:28 +01:00
parent fce78abb1c
commit e555fe4b03
7 changed files with 308 additions and 364 deletions

View File

@ -31,7 +31,7 @@ import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
public class ConferenceDetailsActivity extends XmppActivity { public class ConferenceDetailsActivity extends XmppActivity implements OnConversationUpdate, OnRenameListener {
public static final String ACTION_VIEW_MUC = "view_muc"; public static final String ACTION_VIEW_MUC = "view_muc";
private Conversation conversation; private Conversation conversation;
private TextView mYourNick; private TextView mYourNick;
@ -53,8 +53,28 @@ public class ConferenceDetailsActivity extends XmppActivity {
} }
}; };
@Override
public void onRename(final boolean success) {
runOnUiThread(new Runnable() {
@Override
public void run() {
populateView();
if (success) {
Toast.makeText(
ConferenceDetailsActivity.this,
getString(R.string.your_nick_has_been_changed),
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(ConferenceDetailsActivity.this,
getString(R.string.nick_in_use),
Toast.LENGTH_SHORT).show();
}
}
});
}
private List<User> users = new ArrayList<>(); private List<User> users = new ArrayList<>();
private OnConversationUpdate onConvChanged = new OnConversationUpdate() {
@Override @Override
public void onConversationUpdate() { public void onConversationUpdate() {
@ -66,7 +86,6 @@ public class ConferenceDetailsActivity extends XmppActivity {
} }
}); });
} }
};
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -156,7 +175,6 @@ public class ConferenceDetailsActivity extends XmppActivity {
@Override @Override
void onBackendConnected() { void onBackendConnected() {
registerListener();
if (getIntent().getAction().equals(ACTION_VIEW_MUC)) { if (getIntent().getAction().equals(ACTION_VIEW_MUC)) {
this.uuid = getIntent().getExtras().getString("uuid"); this.uuid = getIntent().getExtras().getString("uuid");
} }
@ -169,42 +187,6 @@ public class ConferenceDetailsActivity extends XmppActivity {
} }
} }
@Override
protected void onStop() {
if (xmppConnectionServiceBound) {
xmppConnectionService.removeOnConversationListChangedListener();
}
super.onStop();
}
protected void registerListener() {
xmppConnectionService
.setOnConversationListChangedListener(this.onConvChanged);
xmppConnectionService.setOnRenameListener(new OnRenameListener() {
@Override
public void onRename(final boolean success) {
runOnUiThread(new Runnable() {
@Override
public void run() {
populateView();
if (success) {
Toast.makeText(
ConferenceDetailsActivity.this,
getString(R.string.your_nick_has_been_changed),
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(ConferenceDetailsActivity.this,
getString(R.string.nick_in_use),
Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
private void populateView() { private void populateView() {
mAccountJid.setText(getString(R.string.using_account, conversation mAccountJid.setText(getString(R.string.using_account, conversation
.getAccount().getJid().toBareJid())); .getAccount().getJid().toBareJid()));

View File

@ -1,9 +1,5 @@
package eu.siacs.conversations.ui; package eu.siacs.conversations.ui;
import java.util.Iterator;
import org.openintents.openpgp.util.OpenPgpUtils;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
@ -21,12 +17,17 @@ 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.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton; import android.widget.ImageButton;
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 org.openintents.openpgp.util.OpenPgpUtils;
import java.util.Iterator;
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;
@ -38,22 +39,10 @@ import eu.siacs.conversations.utils.UIHelper;
import eu.siacs.conversations.xmpp.jid.InvalidJidException; import eu.siacs.conversations.xmpp.jid.InvalidJidException;
import eu.siacs.conversations.xmpp.jid.Jid; import eu.siacs.conversations.xmpp.jid.Jid;
public class ContactDetailsActivity extends XmppActivity { public class ContactDetailsActivity extends XmppActivity implements OnAccountUpdate, OnRosterUpdate {
public static final String ACTION_VIEW_CONTACT = "view_contact"; public static final String ACTION_VIEW_CONTACT = "view_contact";
private Contact contact; private Contact contact;
private Jid accountJid;
private Jid contactJid;
private TextView contactJidTv;
private TextView accountJidTv;
private TextView status;
private TextView lastseen;
private CheckBox send;
private CheckBox receive;
private QuickContactBadge badge;
private DialogInterface.OnClickListener removeFromRoster = new DialogInterface.OnClickListener() { private DialogInterface.OnClickListener removeFromRoster = new DialogInterface.OnClickListener() {
@Override @Override
@ -63,7 +52,54 @@ public class ContactDetailsActivity extends XmppActivity {
ContactDetailsActivity.this.finish(); 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 status;
private TextView lastseen;
private CheckBox send;
private CheckBox receive;
private QuickContactBadge badge;
private DialogInterface.OnClickListener addToPhonebook = new DialogInterface.OnClickListener() { private DialogInterface.OnClickListener addToPhonebook = new DialogInterface.OnClickListener() {
@Override @Override
@ -91,11 +127,8 @@ public class ContactDetailsActivity extends XmppActivity {
builder.create().show(); builder.create().show();
} }
}; };
private LinearLayout keys; private LinearLayout keys;
private OnRosterUpdate rosterUpdate = new OnRosterUpdate() {
@Override @Override
public void onRosterUpdate() { public void onRosterUpdate() {
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@ -106,50 +139,6 @@ 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));
}
}
};
private OnAccountUpdate accountUpdate = new OnAccountUpdate() {
@Override @Override
public void onAccountUpdate() { public void onAccountUpdate() {
@ -161,12 +150,11 @@ public class ContactDetailsActivity extends XmppActivity {
} }
}); });
} }
};
@Override @Override
protected String getShareableUri() { protected String getShareableUri() {
if (contact!=null) { if (contact != null) {
return "xmpp:"+contact.getJid(); return "xmpp:" + contact.getJid();
} else { } else {
return ""; return "";
} }
@ -339,7 +327,7 @@ public class ContactDetailsActivity extends XmppActivity {
boolean hasKeys = false; boolean hasKeys = false;
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (Iterator<String> iterator = contact.getOtrFingerprints() for (Iterator<String> iterator = contact.getOtrFingerprints()
.iterator(); iterator.hasNext();) { .iterator(); iterator.hasNext(); ) {
hasKeys = true; hasKeys = true;
final String otrFingerprint = iterator.next(); final String otrFingerprint = iterator.next();
View view = inflater.inflate(R.layout.contact_key, keys, false); View view = inflater.inflate(R.layout.contact_key, keys, false);
@ -427,9 +415,6 @@ public class ContactDetailsActivity extends XmppActivity {
@Override @Override
public void onBackendConnected() { public void onBackendConnected() {
xmppConnectionService.setOnRosterUpdateListener(this.rosterUpdate);
xmppConnectionService
.setOnAccountListChangedListener(this.accountUpdate);
if ((accountJid != null) && (contactJid != null)) { if ((accountJid != null) && (contactJid != null)) {
Account account = xmppConnectionService Account account = xmppConnectionService
.findAccountByJid(accountJid); .findAccountByJid(accountJid);
@ -440,12 +425,4 @@ public class ContactDetailsActivity extends XmppActivity {
populateView(); populateView();
} }
} }
@Override
protected void onStop() {
super.onStop();
xmppConnectionService.removeOnRosterUpdateListener();
xmppConnectionService.removeOnAccountListChangedListener();
}
} }

View File

@ -633,18 +633,6 @@ public class ConversationActivity extends XmppActivity implements
} }
} }
@Override
protected void onStop() {
if (xmppConnectionServiceBound) {
xmppConnectionService.removeOnConversationListChangedListener();
xmppConnectionService.removeOnAccountListChangedListener();
xmppConnectionService.removeOnRosterUpdateListener();
xmppConnectionService.getNotificationService().setOpenConversation(
null);
}
super.onStop();
}
@Override @Override
public void onSaveInstanceState(final Bundle savedInstanceState) { public void onSaveInstanceState(final Bundle savedInstanceState) {
Conversation conversation = getSelectedConversation(); Conversation conversation = getSelectedConversation();
@ -662,9 +650,7 @@ public class ConversationActivity extends XmppActivity implements
@Override @Override
void onBackendConnected() { void onBackendConnected() {
this.registerListener();
updateConversationList(); updateConversationList();
if (xmppConnectionService.getAccounts().size() == 0) { if (xmppConnectionService.getAccounts().size() == 0) {
startActivity(new Intent(this, EditAccountActivity.class)); startActivity(new Intent(this, EditAccountActivity.class));
} else if (conversationList.size() <= 0) { } else if (conversationList.size() <= 0) {
@ -722,10 +708,10 @@ public class ConversationActivity extends XmppActivity implements
} }
} }
public void registerListener() { @Override
xmppConnectionService.setOnConversationListChangedListener(this); protected void unregisterListeners() {
xmppConnectionService.setOnAccountListChangedListener(this); super.unregisterListeners();
xmppConnectionService.setOnRosterUpdateListener(this); xmppConnectionService.getNotificationService().setOpenConversation(null);
} }
@Override @Override

View File

@ -33,7 +33,7 @@ import eu.siacs.conversations.xmpp.jid.InvalidJidException;
import eu.siacs.conversations.xmpp.jid.Jid; import eu.siacs.conversations.xmpp.jid.Jid;
import eu.siacs.conversations.xmpp.pep.Avatar; import eu.siacs.conversations.xmpp.pep.Avatar;
public class EditAccountActivity extends XmppActivity { public class EditAccountActivity extends XmppActivity implements OnAccountUpdate {
private AutoCompleteTextView mAccountJid; private AutoCompleteTextView mAccountJid;
private EditText mPassword; private EditText mPassword;
@ -132,8 +132,6 @@ public class EditAccountActivity extends XmppActivity {
finish(); finish();
} }
}; };
private OnAccountUpdate mOnAccountUpdateListener = new OnAccountUpdate() {
@Override @Override
public void onAccountUpdate() { public void onAccountUpdate() {
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@ -162,7 +160,6 @@ public class EditAccountActivity extends XmppActivity {
} }
}); });
} }
};
private UiCallback<Avatar> mAvatarFetchCallback = new UiCallback<Avatar>() { private UiCallback<Avatar> mAvatarFetchCallback = new UiCallback<Avatar>() {
@Override @Override
@ -349,21 +346,11 @@ public class EditAccountActivity extends XmppActivity {
} }
} }
@Override
protected void onStop() {
if (xmppConnectionServiceBound) {
xmppConnectionService.removeOnAccountListChangedListener();
}
super.onStop();
}
@Override @Override
protected void onBackendConnected() { protected void onBackendConnected() {
KnownHostsAdapter mKnownHostsAdapter = new KnownHostsAdapter(this, KnownHostsAdapter mKnownHostsAdapter = new KnownHostsAdapter(this,
android.R.layout.simple_list_item_1, android.R.layout.simple_list_item_1,
xmppConnectionService.getKnownHosts()); xmppConnectionService.getKnownHosts());
this.xmppConnectionService
.setOnAccountListChangedListener(this.mOnAccountUpdateListener);
if (this.jidToEdit != null) { if (this.jidToEdit != null) {
this.mAccount = xmppConnectionService.findAccountByJid(jidToEdit); this.mAccount = xmppConnectionService.findAccountByJid(jidToEdit);
updateAccountInformation(); updateAccountInformation();

View File

@ -22,15 +22,13 @@ import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView; import android.widget.ListView;
public class ManageAccountActivity extends XmppActivity { public class ManageAccountActivity extends XmppActivity implements OnAccountUpdate {
protected Account selectedAccount = null; protected Account selectedAccount = null;
protected List<Account> accountList = new ArrayList<Account>(); protected List<Account> accountList = new ArrayList<Account>();
protected ListView accountListView; protected ListView accountListView;
protected AccountAdapter mAccountAdapter; protected AccountAdapter mAccountAdapter;
protected OnAccountUpdate accountChanged = new OnAccountUpdate() {
@Override @Override
public void onAccountUpdate() { public void onAccountUpdate() {
accountList.clear(); accountList.clear();
@ -43,7 +41,6 @@ public class ManageAccountActivity extends XmppActivity {
} }
}); });
} }
};
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -84,17 +81,8 @@ public class ManageAccountActivity extends XmppActivity {
menu.setHeaderTitle(this.selectedAccount.getJid().toBareJid().toString()); menu.setHeaderTitle(this.selectedAccount.getJid().toBareJid().toString());
} }
@Override
protected void onStop() {
if (xmppConnectionServiceBound) {
xmppConnectionService.removeOnAccountListChangedListener();
}
super.onStop();
}
@Override @Override
void onBackendConnected() { void onBackendConnected() {
xmppConnectionService.setOnAccountListChangedListener(accountChanged);
this.accountList.clear(); this.accountList.clear();
this.accountList.addAll(xmppConnectionService.getAccounts()); this.accountList.addAll(xmppConnectionService.getAccounts());
mAccountAdapter.notifyDataSetChanged(); mAccountAdapter.notifyDataSetChanged();

View File

@ -66,59 +66,25 @@ import eu.siacs.conversations.utils.Validator;
import eu.siacs.conversations.xmpp.jid.InvalidJidException; import eu.siacs.conversations.xmpp.jid.InvalidJidException;
import eu.siacs.conversations.xmpp.jid.Jid; import eu.siacs.conversations.xmpp.jid.Jid;
public class StartConversationActivity extends XmppActivity { public class StartConversationActivity extends XmppActivity implements OnRosterUpdate {
private Tab mContactsTab;
private Tab mConferencesTab;
private ViewPager mViewPager;
private MyListFragment mContactsListFragment = new MyListFragment();
private List<ListItem> contacts = new ArrayList<>();
private ArrayAdapter<ListItem> mContactsAdapter;
private MyListFragment mConferenceListFragment = new MyListFragment();
private List<ListItem> conferences = new ArrayList<ListItem>();
private ArrayAdapter<ListItem> mConferenceAdapter;
private List<String> mActivatedAccounts = new ArrayList<String>();
private List<String> mKnownHosts;
private List<String> mKnownConferenceHosts;
private Invite mPendingInvite = null;
private Menu mOptionsMenu;
private EditText mSearchEditText;
public int conference_context_id; public int conference_context_id;
public int contact_context_id; public int contact_context_id;
private Tab mContactsTab;
private TabListener mTabListener = new TabListener() { private Tab mConferencesTab;
private ViewPager mViewPager;
@Override private MyListFragment mContactsListFragment = new MyListFragment();
public void onTabUnselected(Tab tab, FragmentTransaction ft) { private List<ListItem> contacts = new ArrayList<>();
return; private ArrayAdapter<ListItem> mContactsAdapter;
} private MyListFragment mConferenceListFragment = new MyListFragment();
private List<ListItem> conferences = new ArrayList<ListItem>();
@Override private ArrayAdapter<ListItem> mConferenceAdapter;
public void onTabSelected(Tab tab, FragmentTransaction ft) { private List<String> mActivatedAccounts = new ArrayList<String>();
mViewPager.setCurrentItem(tab.getPosition()); private List<String> mKnownHosts;
onTabChanged(); private List<String> mKnownConferenceHosts;
} private Invite mPendingInvite = null;
private Menu mOptionsMenu;
@Override private EditText mSearchEditText;
public void onTabReselected(Tab tab, FragmentTransaction ft) {
return;
}
};
private ViewPager.SimpleOnPageChangeListener mOnPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
getActionBar().setSelectedNavigationItem(position);
onTabChanged();
}
};
private MenuItem.OnActionExpandListener mOnActionExpandListener = new MenuItem.OnActionExpandListener() { private MenuItem.OnActionExpandListener mOnActionExpandListener = new MenuItem.OnActionExpandListener() {
@Override @Override
@ -147,6 +113,31 @@ public class StartConversationActivity extends XmppActivity {
return true; return true;
} }
}; };
private TabListener mTabListener = new TabListener() {
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
return;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
onTabChanged();
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
return;
}
};
private ViewPager.SimpleOnPageChangeListener mOnPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
getActionBar().setSelectedNavigationItem(position);
onTabChanged();
}
};
private TextWatcher mSearchTextWatcher = new TextWatcher() { private TextWatcher mSearchTextWatcher = new TextWatcher() {
@Override @Override
@ -164,7 +155,8 @@ public class StartConversationActivity extends XmppActivity {
int count) { int count) {
} }
}; };
private OnRosterUpdate onRosterUpdate = new OnRosterUpdate() { private MenuItem mMenuSearchView;
private String mInitialJid;
@Override @Override
public void onRosterUpdate() { public void onRosterUpdate() {
@ -178,9 +170,6 @@ public class StartConversationActivity extends XmppActivity {
} }
}); });
} }
};
private MenuItem mMenuSearchView;
private String mInitialJid;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
@ -243,12 +232,6 @@ public class StartConversationActivity extends XmppActivity {
} }
@Override
public void onStop() {
super.onStop();
xmppConnectionService.removeOnRosterUpdateListener();
}
protected void openConversationForContact(int position) { protected void openConversationForContact(int position) {
Contact contact = (Contact) contacts.get(position); Contact contact = (Contact) contacts.get(position);
Conversation conversation = xmppConnectionService Conversation conversation = xmppConnectionService
@ -570,7 +553,6 @@ public class StartConversationActivity extends XmppActivity {
@Override @Override
protected void onBackendConnected() { protected void onBackendConnected() {
xmppConnectionService.setOnRosterUpdateListener(this.onRosterUpdate);
this.mActivatedAccounts.clear(); this.mActivatedAccounts.clear();
for (Account account : xmppConnectionService.getAccounts()) { for (Account account : xmppConnectionService.getAccounts()) {
if (account.getStatus() != Account.STATUS_DISABLED) { if (account.getStatus() != Account.STATUS_DISABLED) {
@ -611,7 +593,7 @@ public class StartConversationActivity extends XmppActivity {
for (Parcelable message : getIntent().getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)) { for (Parcelable message : getIntent().getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)) {
if (message instanceof NdefMessage) { if (message instanceof NdefMessage) {
Log.d(Config.LOGTAG, "received message=" + message); Log.d(Config.LOGTAG, "received message=" + message);
for (NdefRecord record : ((NdefMessage)message).getRecords()) { for (NdefRecord record : ((NdefMessage) message).getRecords()) {
switch (record.getTnf()) { switch (record.getTnf()) {
case NdefRecord.TNF_WELL_KNOWN: case NdefRecord.TNF_WELL_KNOWN:
if (Arrays.equals(record.getType(), NdefRecord.RTD_URI)) { if (Arrays.equals(record.getType(), NdefRecord.RTD_URI)) {

View File

@ -62,6 +62,7 @@ import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.entities.MucOptions;
import eu.siacs.conversations.entities.Presences; import eu.siacs.conversations.entities.Presences;
import eu.siacs.conversations.services.AvatarService; import eu.siacs.conversations.services.AvatarService;
import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.services.XmppConnectionService;
@ -77,6 +78,7 @@ public abstract class XmppActivity extends Activity {
public XmppConnectionService xmppConnectionService; public XmppConnectionService xmppConnectionService;
public boolean xmppConnectionServiceBound = false; public boolean xmppConnectionServiceBound = false;
protected boolean registeredListeners = false;
protected int mPrimaryTextColor; protected int mPrimaryTextColor;
protected int mSecondaryTextColor; protected int mSecondaryTextColor;
@ -105,11 +107,11 @@ public abstract class XmppActivity extends Activity {
XmppConnectionBinder binder = (XmppConnectionBinder) service; XmppConnectionBinder binder = (XmppConnectionBinder) service;
xmppConnectionService = binder.getService(); xmppConnectionService = binder.getService();
xmppConnectionServiceBound = true; xmppConnectionServiceBound = true;
if (!isFinishing() && !isDestroyed()) { if (!registeredListeners) {
onBackendConnected(); registerListeners();
} else { registeredListeners = true;
Log.d(Config.LOGTAG,"omitting call to onBackendConnected()");
} }
onBackendConnected();
} }
@Override @Override
@ -123,6 +125,12 @@ public abstract class XmppActivity extends Activity {
super.onStart(); super.onStart();
if (!xmppConnectionServiceBound) { if (!xmppConnectionServiceBound) {
connectToBackend(); connectToBackend();
} else {
if (!registeredListeners) {
this.registerListeners();
this.registeredListeners = true;
}
this.onBackendConnected();
} }
} }
@ -137,6 +145,10 @@ public abstract class XmppActivity extends Activity {
protected void onStop() { protected void onStop() {
super.onStop(); super.onStop();
if (xmppConnectionServiceBound) { if (xmppConnectionServiceBound) {
if (registeredListeners) {
this.unregisterListeners();
this.registeredListeners = false;
}
unbindService(mConnection); unbindService(mConnection);
xmppConnectionServiceBound = false; xmppConnectionServiceBound = false;
} }
@ -207,6 +219,36 @@ public abstract class XmppActivity extends Activity {
abstract void onBackendConnected(); abstract void onBackendConnected();
protected void registerListeners() {
if (this instanceof XmppConnectionService.OnConversationUpdate) {
this.xmppConnectionService.setOnConversationListChangedListener((XmppConnectionService.OnConversationUpdate) this);
}
if (this instanceof XmppConnectionService.OnAccountUpdate) {
this.xmppConnectionService.setOnAccountListChangedListener((XmppConnectionService.OnAccountUpdate) this);
}
if (this instanceof XmppConnectionService.OnRosterUpdate) {
this.xmppConnectionService.setOnRosterUpdateListener((XmppConnectionService.OnRosterUpdate) this);
}
if (this instanceof MucOptions.OnRenameListener) {
this.xmppConnectionService.setOnRenameListener((MucOptions.OnRenameListener) this);
}
}
protected void unregisterListeners() {
if (this instanceof XmppConnectionService.OnConversationUpdate) {
this.xmppConnectionService.removeOnConversationListChangedListener();
}
if (this instanceof XmppConnectionService.OnAccountUpdate) {
this.xmppConnectionService.removeOnAccountListChangedListener();
}
if (this instanceof XmppConnectionService.OnRosterUpdate) {
this.xmppConnectionService.removeOnRosterUpdateListener();
}
if (this instanceof MucOptions.OnRenameListener) {
this.xmppConnectionService.setOnRenameListener(null);
}
}
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.action_settings: case R.id.action_settings: