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.Toast;
public class ConferenceDetailsActivity extends XmppActivity {
public class ConferenceDetailsActivity extends XmppActivity implements OnConversationUpdate, OnRenameListener {
public static final String ACTION_VIEW_MUC = "view_muc";
private Conversation conversation;
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 OnConversationUpdate onConvChanged = new OnConversationUpdate() {
@Override
public void onConversationUpdate() {
@ -66,7 +86,6 @@ public class ConferenceDetailsActivity extends XmppActivity {
}
});
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -156,7 +175,6 @@ public class ConferenceDetailsActivity extends XmppActivity {
@Override
void onBackendConnected() {
registerListener();
if (getIntent().getAction().equals(ACTION_VIEW_MUC)) {
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() {
mAccountJid.setText(getString(R.string.using_account, conversation
.getAccount().getJid().toBareJid()));

View file

@ -1,9 +1,5 @@
package eu.siacs.conversations.ui;
import java.util.Iterator;
import org.openintents.openpgp.util.OpenPgpUtils;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.Context;
@ -21,12 +17,17 @@ import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton.OnCheckedChangeListener;
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.Iterator;
import eu.siacs.conversations.R;
import eu.siacs.conversations.crypto.PgpEngine;
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.Jid;
public class ContactDetailsActivity extends XmppActivity {
public class ContactDetailsActivity extends XmppActivity implements OnAccountUpdate, OnRosterUpdate {
public static final String ACTION_VIEW_CONTACT = "view_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() {
@Override
@ -63,7 +52,54 @@ public class ContactDetailsActivity extends XmppActivity {
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() {
@Override
@ -91,11 +127,8 @@ public class ContactDetailsActivity extends XmppActivity {
builder.create().show();
}
};
private LinearLayout keys;
private OnRosterUpdate rosterUpdate = new OnRosterUpdate() {
@Override
public void onRosterUpdate() {
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
public void onAccountUpdate() {
@ -161,12 +150,11 @@ public class ContactDetailsActivity extends XmppActivity {
}
});
}
};
@Override
protected String getShareableUri() {
if (contact!=null) {
return "xmpp:"+contact.getJid();
if (contact != null) {
return "xmpp:" + contact.getJid();
} else {
return "";
}
@ -339,7 +327,7 @@ public class ContactDetailsActivity extends XmppActivity {
boolean hasKeys = false;
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (Iterator<String> iterator = contact.getOtrFingerprints()
.iterator(); iterator.hasNext();) {
.iterator(); iterator.hasNext(); ) {
hasKeys = true;
final String otrFingerprint = iterator.next();
View view = inflater.inflate(R.layout.contact_key, keys, false);
@ -427,9 +415,6 @@ public class ContactDetailsActivity extends XmppActivity {
@Override
public void onBackendConnected() {
xmppConnectionService.setOnRosterUpdateListener(this.rosterUpdate);
xmppConnectionService
.setOnAccountListChangedListener(this.accountUpdate);
if ((accountJid != null) && (contactJid != null)) {
Account account = xmppConnectionService
.findAccountByJid(accountJid);
@ -440,12 +425,4 @@ public class ContactDetailsActivity extends XmppActivity {
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
public void onSaveInstanceState(final Bundle savedInstanceState) {
Conversation conversation = getSelectedConversation();
@ -662,9 +650,7 @@ public class ConversationActivity extends XmppActivity implements
@Override
void onBackendConnected() {
this.registerListener();
updateConversationList();
if (xmppConnectionService.getAccounts().size() == 0) {
startActivity(new Intent(this, EditAccountActivity.class));
} else if (conversationList.size() <= 0) {
@ -722,10 +708,10 @@ public class ConversationActivity extends XmppActivity implements
}
}
public void registerListener() {
xmppConnectionService.setOnConversationListChangedListener(this);
xmppConnectionService.setOnAccountListChangedListener(this);
xmppConnectionService.setOnRosterUpdateListener(this);
@Override
protected void unregisterListeners() {
super.unregisterListeners();
xmppConnectionService.getNotificationService().setOpenConversation(null);
}
@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.pep.Avatar;
public class EditAccountActivity extends XmppActivity {
public class EditAccountActivity extends XmppActivity implements OnAccountUpdate {
private AutoCompleteTextView mAccountJid;
private EditText mPassword;
@ -132,8 +132,6 @@ public class EditAccountActivity extends XmppActivity {
finish();
}
};
private OnAccountUpdate mOnAccountUpdateListener = new OnAccountUpdate() {
@Override
public void onAccountUpdate() {
runOnUiThread(new Runnable() {
@ -162,7 +160,6 @@ public class EditAccountActivity extends XmppActivity {
}
});
}
};
private UiCallback<Avatar> mAvatarFetchCallback = new UiCallback<Avatar>() {
@Override
@ -349,21 +346,11 @@ public class EditAccountActivity extends XmppActivity {
}
}
@Override
protected void onStop() {
if (xmppConnectionServiceBound) {
xmppConnectionService.removeOnAccountListChangedListener();
}
super.onStop();
}
@Override
protected void onBackendConnected() {
KnownHostsAdapter mKnownHostsAdapter = new KnownHostsAdapter(this,
android.R.layout.simple_list_item_1,
xmppConnectionService.getKnownHosts());
this.xmppConnectionService
.setOnAccountListChangedListener(this.mOnAccountUpdateListener);
if (this.jidToEdit != null) {
this.mAccount = xmppConnectionService.findAccountByJid(jidToEdit);
updateAccountInformation();

View file

@ -22,15 +22,13 @@ import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class ManageAccountActivity extends XmppActivity {
public class ManageAccountActivity extends XmppActivity implements OnAccountUpdate {
protected Account selectedAccount = null;
protected List<Account> accountList = new ArrayList<Account>();
protected ListView accountListView;
protected AccountAdapter mAccountAdapter;
protected OnAccountUpdate accountChanged = new OnAccountUpdate() {
@Override
public void onAccountUpdate() {
accountList.clear();
@ -43,7 +41,6 @@ public class ManageAccountActivity extends XmppActivity {
}
});
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -84,17 +81,8 @@ public class ManageAccountActivity extends XmppActivity {
menu.setHeaderTitle(this.selectedAccount.getJid().toBareJid().toString());
}
@Override
protected void onStop() {
if (xmppConnectionServiceBound) {
xmppConnectionService.removeOnAccountListChangedListener();
}
super.onStop();
}
@Override
void onBackendConnected() {
xmppConnectionService.setOnAccountListChangedListener(accountChanged);
this.accountList.clear();
this.accountList.addAll(xmppConnectionService.getAccounts());
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.Jid;
public class StartConversationActivity extends XmppActivity {
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 class StartConversationActivity extends XmppActivity implements OnRosterUpdate {
public int conference_context_id;
public int contact_context_id;
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 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;
private MenuItem.OnActionExpandListener mOnActionExpandListener = new MenuItem.OnActionExpandListener() {
@Override
@ -147,6 +113,31 @@ public class StartConversationActivity extends XmppActivity {
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() {
@Override
@ -164,7 +155,8 @@ public class StartConversationActivity extends XmppActivity {
int count) {
}
};
private OnRosterUpdate onRosterUpdate = new OnRosterUpdate() {
private MenuItem mMenuSearchView;
private String mInitialJid;
@Override
public void onRosterUpdate() {
@ -178,9 +170,6 @@ public class StartConversationActivity extends XmppActivity {
}
});
}
};
private MenuItem mMenuSearchView;
private String mInitialJid;
@Override
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) {
Contact contact = (Contact) contacts.get(position);
Conversation conversation = xmppConnectionService
@ -570,7 +553,6 @@ public class StartConversationActivity extends XmppActivity {
@Override
protected void onBackendConnected() {
xmppConnectionService.setOnRosterUpdateListener(this.onRosterUpdate);
this.mActivatedAccounts.clear();
for (Account account : xmppConnectionService.getAccounts()) {
if (account.getStatus() != Account.STATUS_DISABLED) {
@ -611,7 +593,7 @@ public class StartConversationActivity extends XmppActivity {
for (Parcelable message : getIntent().getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)) {
if (message instanceof NdefMessage) {
Log.d(Config.LOGTAG, "received message=" + message);
for (NdefRecord record : ((NdefMessage)message).getRecords()) {
for (NdefRecord record : ((NdefMessage) message).getRecords()) {
switch (record.getTnf()) {
case NdefRecord.TNF_WELL_KNOWN:
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.Conversation;
import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.entities.MucOptions;
import eu.siacs.conversations.entities.Presences;
import eu.siacs.conversations.services.AvatarService;
import eu.siacs.conversations.services.XmppConnectionService;
@ -77,6 +78,7 @@ public abstract class XmppActivity extends Activity {
public XmppConnectionService xmppConnectionService;
public boolean xmppConnectionServiceBound = false;
protected boolean registeredListeners = false;
protected int mPrimaryTextColor;
protected int mSecondaryTextColor;
@ -105,11 +107,11 @@ public abstract class XmppActivity extends Activity {
XmppConnectionBinder binder = (XmppConnectionBinder) service;
xmppConnectionService = binder.getService();
xmppConnectionServiceBound = true;
if (!isFinishing() && !isDestroyed()) {
onBackendConnected();
} else {
Log.d(Config.LOGTAG,"omitting call to onBackendConnected()");
if (!registeredListeners) {
registerListeners();
registeredListeners = true;
}
onBackendConnected();
}
@Override
@ -123,6 +125,12 @@ public abstract class XmppActivity extends Activity {
super.onStart();
if (!xmppConnectionServiceBound) {
connectToBackend();
} else {
if (!registeredListeners) {
this.registerListeners();
this.registeredListeners = true;
}
this.onBackendConnected();
}
}
@ -137,6 +145,10 @@ public abstract class XmppActivity extends Activity {
protected void onStop() {
super.onStop();
if (xmppConnectionServiceBound) {
if (registeredListeners) {
this.unregisterListeners();
this.registeredListeners = false;
}
unbindService(mConnection);
xmppConnectionServiceBound = false;
}
@ -207,6 +219,36 @@ public abstract class XmppActivity extends Activity {
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) {
switch (item.getItemId()) {
case R.id.action_settings: