fixed recreation issues in StartConversationActivity

This commit is contained in:
Daniel Gultsch 2016-08-31 17:04:43 +02:00
parent f7d8580969
commit aca7054174
3 changed files with 1031 additions and 977 deletions

View File

@ -51,7 +51,6 @@
</activity> </activity>
<activity <activity
android:name=".ui.StartConversationActivity" android:name=".ui.StartConversationActivity"
android:configChanges="orientation|screenSize"
android:label="@string/title_activity_start_conversation" android:label="@string/title_activity_start_conversation"
android:launchMode="singleTask"> android:launchMode="singleTask">
<intent-filter> <intent-filter>

View File

@ -1,6 +1,7 @@
package eu.siacs.conversations.ui; package eu.siacs.conversations.ui;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context; import android.content.Context;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -120,8 +121,9 @@ public class EnterJidDialog {
this.listener = listener; this.listener = listener;
} }
public void show() { public Dialog show() {
this.dialog.show(); this.dialog.show();
this.dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(this.dialogOnClick); this.dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(this.dialogOnClick);
return this.dialog;
} }
} }

View File

@ -7,7 +7,9 @@ import android.app.ActionBar;
import android.app.ActionBar.Tab; import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener; import android.app.ActionBar.TabListener;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Fragment; import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction; import android.app.FragmentTransaction;
import android.app.ListFragment; import android.app.ListFragment;
import android.app.PendingIntent; import android.app.PendingIntent;
@ -24,6 +26,7 @@ import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Parcelable; import android.os.Parcelable;
import android.support.v13.app.FragmentPagerAdapter; import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager;
import android.text.Editable; import android.text.Editable;
import android.text.TextWatcher; import android.text.TextWatcher;
@ -35,6 +38,7 @@ import android.view.KeyEvent;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.AdapterView.AdapterContextMenuInfo;
@ -83,13 +87,12 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
private Tab mContactsTab; private Tab mContactsTab;
private Tab mConferencesTab; private Tab mConferencesTab;
private ViewPager mViewPager; private ViewPager mViewPager;
private MyListFragment mContactsListFragment = new MyListFragment(); private ListPagerAdapter mListPagerAdapter;
private List<ListItem> contacts = new ArrayList<>(); private List<ListItem> contacts = new ArrayList<>();
private ArrayAdapter<ListItem> mContactsAdapter; private ArrayAdapter<ListItem> mContactsAdapter;
private MyListFragment mConferenceListFragment = new MyListFragment(); private List<ListItem> conferences = new ArrayList<>();
private List<ListItem> conferences = new ArrayList<ListItem>();
private ArrayAdapter<ListItem> mConferenceAdapter; private ArrayAdapter<ListItem> mConferenceAdapter;
private List<String> mActivatedAccounts = new ArrayList<String>(); private List<String> mActivatedAccounts = new ArrayList<>();
private List<String> mKnownHosts; private List<String> mKnownHosts;
private List<String> mKnownConferenceHosts; private List<String> mKnownConferenceHosts;
private Invite mPendingInvite = null; private Invite mPendingInvite = null;
@ -97,6 +100,7 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
private AtomicBoolean mRequestedContactsPermission = new AtomicBoolean(false); private AtomicBoolean mRequestedContactsPermission = new AtomicBoolean(false);
private final int REQUEST_SYNC_CONTACTS = 0x3b28cf; private final int REQUEST_SYNC_CONTACTS = 0x3b28cf;
private final int REQUEST_CREATE_CONFERENCE = 0x3b39da; private final int REQUEST_CREATE_CONFERENCE = 0x3b39da;
private Dialog mCurrentDialog = null;
private MenuItem.OnActionExpandListener mOnActionExpandListener = new MenuItem.OnActionExpandListener() { private MenuItem.OnActionExpandListener mOnActionExpandListener = new MenuItem.OnActionExpandListener() {
@ -165,29 +169,27 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
} }
@Override @Override
public void onTextChanged(CharSequence s, int start, int before, public void onTextChanged(CharSequence s, int start, int before, int count) {
int count) {
} }
}; };
private TextView.OnEditorActionListener mSearchDone = new TextView.OnEditorActionListener() { private TextView.OnEditorActionListener mSearchDone = new TextView.OnEditorActionListener() {
@Override @Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (getActionBar().getSelectedNavigationIndex() == 0) { int pos = getActionBar().getSelectedNavigationIndex();
if (pos == 0) {
if (contacts.size() == 1) { if (contacts.size() == 1) {
openConversationForContact((Contact) contacts.get(0)); openConversationForContact((Contact) contacts.get(0));
} else { return true;
hideKeyboard();
mContactsListFragment.getListView().requestFocus();
} }
} else { } else {
if (conferences.size() == 1) { if (conferences.size() == 1) {
openConversationsForBookmark((Bookmark) conferences.get(0)); openConversationsForBookmark((Bookmark) conferences.get(0));
} else { return true;
}
}
hideKeyboard(); hideKeyboard();
mConferenceListFragment.getListView().requestFocus(); mListPagerAdapter.requestFocus(pos);
}
}
return true; return true;
} }
}; };
@ -242,7 +244,7 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
protected void replaceToast(String msg) { protected void replaceToast(String msg) {
hideToast(); hideToast();
mToast = Toast.makeText(this, msg ,Toast.LENGTH_LONG); mToast = Toast.makeText(this, msg, Toast.LENGTH_LONG);
mToast.show(); mToast.show();
} }
@ -267,50 +269,12 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
actionBar.addTab(mConferencesTab); actionBar.addTab(mConferencesTab);
mViewPager.setOnPageChangeListener(mOnPageChangeListener); mViewPager.setOnPageChangeListener(mOnPageChangeListener);
mViewPager.setAdapter(new FragmentPagerAdapter(getFragmentManager()) { mListPagerAdapter = new ListPagerAdapter(getFragmentManager());
mViewPager.setAdapter(mListPagerAdapter);
@Override
public int getCount() {
return 2;
}
@Override
public Fragment getItem(int position) {
if (position == 0) {
return mContactsListFragment;
} else {
return mConferenceListFragment;
}
}
});
mConferenceAdapter = new ListItemAdapter(this, conferences); mConferenceAdapter = new ListItemAdapter(this, conferences);
mConferenceListFragment.setListAdapter(mConferenceAdapter);
mConferenceListFragment.setContextMenu(R.menu.conference_context);
mConferenceListFragment
.setOnListItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
openConversationForBookmark(position);
}
});
mContactsAdapter = new ListItemAdapter(this, contacts); mContactsAdapter = new ListItemAdapter(this, contacts);
((ListItemAdapter) mContactsAdapter).setOnTagClickedListener(this.mOnTagClickedListener); ((ListItemAdapter) mContactsAdapter).setOnTagClickedListener(this.mOnTagClickedListener);
mContactsListFragment.setListAdapter(mContactsAdapter);
mContactsListFragment.setContextMenu(R.menu.contact_context);
mContactsListFragment
.setOnListItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
openConversationForContact(position);
}
});
this.mHideOfflineContacts = getPreferences().getBoolean("hide_offline", false); this.mHideOfflineContacts = getPreferences().getBoolean("hide_offline", false);
} }
@ -321,6 +285,14 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
askForContactsPermissions(); askForContactsPermissions();
} }
@Override
public void onStop() {
if (mCurrentDialog != null) {
mCurrentDialog.dismiss();
}
super.onStop();
}
protected void openConversationForContact(int position) { protected void openConversationForContact(int position) {
Contact contact = (Contact) contacts.get(position); Contact contact = (Contact) contacts.get(position);
openConversationForContact(contact); openConversationForContact(contact);
@ -350,10 +322,10 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
protected void openConversationsForBookmark(Bookmark bookmark) { protected void openConversationsForBookmark(Bookmark bookmark) {
Jid jid = bookmark.getJid(); Jid jid = bookmark.getJid();
if (jid == null) { if (jid == null) {
Toast.makeText(this,R.string.invalid_jid,Toast.LENGTH_SHORT).show(); Toast.makeText(this, R.string.invalid_jid, Toast.LENGTH_SHORT).show();
return; return;
} }
Conversation conversation = xmppConnectionService.findOrCreateConversation(bookmark.getAccount(),jid, true); Conversation conversation = xmppConnectionService.findOrCreateConversation(bookmark.getAccount(), jid, true);
conversation.setBookmark(bookmark); conversation.setBookmark(bookmark);
if (!conversation.getMucOptions().online()) { if (!conversation.getMucOptions().online()) {
xmppConnectionService.joinMuc(conversation); xmppConnectionService.joinMuc(conversation);
@ -451,7 +423,7 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
} }
}); });
dialog.show(); mCurrentDialog = dialog.show();
} }
@SuppressLint("InflateParams") @SuppressLint("InflateParams")
@ -476,6 +448,7 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
builder.setPositiveButton(R.string.join, null); builder.setPositiveButton(R.string.join, null);
final AlertDialog dialog = builder.create(); final AlertDialog dialog = builder.create();
dialog.show(); dialog.show();
mCurrentDialog = dialog;
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener( dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(
new View.OnClickListener() { new View.OnClickListener() {
@ -516,6 +489,7 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
xmppConnectionService.joinMuc(conversation); xmppConnectionService.joinMuc(conversation);
} }
dialog.dismiss(); dialog.dismiss();
mCurrentDialog = null;
switchToConversation(conversation); switchToConversation(conversation);
} }
} else { } else {
@ -526,6 +500,7 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
xmppConnectionService.joinMuc(conversation); xmppConnectionService.joinMuc(conversation);
} }
dialog.dismiss(); dialog.dismiss();
mCurrentDialog = null;
switchToConversation(conversation); switchToConversation(conversation);
} }
} }
@ -555,12 +530,13 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
intent.putExtra("show_enter_jid", true); intent.putExtra("show_enter_jid", true);
intent.putExtra("subject", subject.getText().toString()); intent.putExtra("subject", subject.getText().toString());
intent.putExtra(EXTRA_ACCOUNT, account.getJid().toBareJid().toString()); intent.putExtra(EXTRA_ACCOUNT, account.getJid().toBareJid().toString());
intent.putExtra(ChooseContactActivity.EXTRA_TITLE_RES_ID,R.string.choose_participants); intent.putExtra(ChooseContactActivity.EXTRA_TITLE_RES_ID, R.string.choose_participants);
startActivityForResult(intent, REQUEST_CREATE_CONFERENCE); startActivityForResult(intent, REQUEST_CREATE_CONFERENCE);
} }
}); });
builder.setNegativeButton(R.string.cancel, null); builder.setNegativeButton(R.string.cancel, null);
builder.create().show(); mCurrentDialog = builder.create();
mCurrentDialog.show();
} }
private Account getSelectedAccount(Spinner spinner) { private Account getSelectedAccount(Spinner spinner) {
@ -634,7 +610,7 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.action_create_contact: case R.id.action_create_contact:
showCreateContactDialog(null,null); showCreateContactDialog(null, null);
return true; return true;
case R.id.action_join_conference: case R.id.action_join_conference:
showJoinConferenceDialog(null); showJoinConferenceDialog(null);
@ -719,7 +695,7 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
} }
if (account != null && jids.size() > 0) { if (account != null && jids.size() > 0) {
xmppConnectionService.createAdhocConference(account, subject, jids, mAdhocConferenceCallback); xmppConnectionService.createAdhocConference(account, subject, jids, mAdhocConferenceCallback);
mToast = Toast.makeText(this, R.string.creating_conference,Toast.LENGTH_LONG); mToast = Toast.makeText(this, R.string.creating_conference, Toast.LENGTH_LONG);
mToast.show(); mToast.show();
} }
} }
@ -872,13 +848,13 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
return false; return false;
} }
} else if (contacts.size() == 0) { } else if (contacts.size() == 0) {
showCreateContactDialog(invite.getJid().toString(),invite.getFingerprint()); showCreateContactDialog(invite.getJid().toString(), invite.getFingerprint());
return false; return false;
} else if (contacts.size() == 1) { } else if (contacts.size() == 1) {
Contact contact = contacts.get(0); Contact contact = contacts.get(0);
if (invite.getFingerprint() != null) { if (invite.getFingerprint() != null) {
if (contact.addOtrFingerprint(invite.getFingerprint())) { if (contact.addOtrFingerprint(invite.getFingerprint())) {
Log.d(Config.LOGTAG,"added new fingerprint"); Log.d(Config.LOGTAG, "added new fingerprint");
xmppConnectionService.syncRosterToDisk(contact.getAccount()); xmppConnectionService.syncRosterToDisk(contact.getAccount());
} }
} }
@ -954,6 +930,83 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
} }
} }
public class ListPagerAdapter extends PagerAdapter {
FragmentManager fragmentManager;
MyListFragment[] fragments;
public ListPagerAdapter(FragmentManager fm) {
fragmentManager = fm;
fragments = new MyListFragment[2];
}
public void requestFocus(int pos) {
if (fragments.length > pos) {
fragments[pos].getListView().requestFocus();
}
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
assert (0 <= position && position < fragments.length);
FragmentTransaction trans = fragmentManager.beginTransaction();
trans.remove(fragments[position]);
trans.commit();
fragments[position] = null;
}
@Override
public Fragment instantiateItem(ViewGroup container, int position) {
Fragment fragment = getItem(position);
FragmentTransaction trans = fragmentManager.beginTransaction();
trans.add(container.getId(), fragment, "fragment:" + position);
trans.commit();
return fragment;
}
@Override
public int getCount() {
return fragments.length;
}
@Override
public boolean isViewFromObject(View view, Object fragment) {
return ((Fragment) fragment).getView() == view;
}
public Fragment getItem(int position) {
assert (0 <= position && position < fragments.length);
if (fragments[position] == null) {
final MyListFragment listFragment = new MyListFragment();
if (position == 1) {
listFragment.setListAdapter(mConferenceAdapter);
listFragment.setContextMenu(R.menu.conference_context);
listFragment.setOnListItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
openConversationForBookmark(position);
}
});
} else {
listFragment.setListAdapter(mContactsAdapter);
listFragment.setContextMenu(R.menu.contact_context);
listFragment.setOnListItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
openConversationForContact(position);
}
});
}
fragments[position] = listFragment;
}
return fragments[position];
}
}
public static class MyListFragment extends ListFragment { public static class MyListFragment extends ListFragment {
private AdapterView.OnItemClickListener mOnItemClickListener; private AdapterView.OnItemClickListener mOnItemClickListener;
private int mResContextMenu; private int mResContextMenu;
@ -989,7 +1042,7 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
final AdapterView.AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) menuInfo; final AdapterView.AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) menuInfo;
if (mResContextMenu == R.menu.conference_context) { if (mResContextMenu == R.menu.conference_context) {
activity.conference_context_id = acmi.position; activity.conference_context_id = acmi.position;
} else if (mResContextMenu == R.menu.contact_context){ } else if (mResContextMenu == R.menu.contact_context) {
activity.contact_context_id = acmi.position; activity.contact_context_id = acmi.position;
final Blockable contact = (Contact) activity.contacts.get(acmi.position); final Blockable contact = (Contact) activity.contacts.get(acmi.position);
final MenuItem blockUnblockItem = menu.findItem(R.id.context_contact_block_unblock); final MenuItem blockUnblockItem = menu.findItem(R.id.context_contact_block_unblock);