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
android:name=".ui.StartConversationActivity"
android:configChanges="orientation|screenSize"
android:label="@string/title_activity_start_conversation"
android:launchMode="singleTask">
<intent-filter>

View file

@ -1,6 +1,7 @@
package eu.siacs.conversations.ui;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
@ -120,8 +121,9 @@ public class EnterJidDialog {
this.listener = listener;
}
public void show() {
public Dialog show() {
this.dialog.show();
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.TabListener;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.app.PendingIntent;
@ -24,6 +26,7 @@ import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.TextWatcher;
@ -35,6 +38,7 @@ import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
@ -83,13 +87,12 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
private Tab mContactsTab;
private Tab mConferencesTab;
private ViewPager mViewPager;
private MyListFragment mContactsListFragment = new MyListFragment();
private ListPagerAdapter mListPagerAdapter;
private List<ListItem> contacts = new ArrayList<>();
private ArrayAdapter<ListItem> mContactsAdapter;
private MyListFragment mConferenceListFragment = new MyListFragment();
private List<ListItem> conferences = new ArrayList<ListItem>();
private List<ListItem> conferences = new ArrayList<>();
private ArrayAdapter<ListItem> mConferenceAdapter;
private List<String> mActivatedAccounts = new ArrayList<String>();
private List<String> mActivatedAccounts = new ArrayList<>();
private List<String> mKnownHosts;
private List<String> mKnownConferenceHosts;
private Invite mPendingInvite = null;
@ -97,6 +100,7 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
private AtomicBoolean mRequestedContactsPermission = new AtomicBoolean(false);
private final int REQUEST_SYNC_CONTACTS = 0x3b28cf;
private final int REQUEST_CREATE_CONFERENCE = 0x3b39da;
private Dialog mCurrentDialog = null;
private MenuItem.OnActionExpandListener mOnActionExpandListener = new MenuItem.OnActionExpandListener() {
@ -165,29 +169,27 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
};
private TextView.OnEditorActionListener mSearchDone = new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (getActionBar().getSelectedNavigationIndex() == 0) {
int pos = getActionBar().getSelectedNavigationIndex();
if (pos == 0) {
if (contacts.size() == 1) {
openConversationForContact((Contact) contacts.get(0));
} else {
hideKeyboard();
mContactsListFragment.getListView().requestFocus();
return true;
}
} else {
if (conferences.size() == 1) {
openConversationsForBookmark((Bookmark) conferences.get(0));
} else {
return true;
}
}
hideKeyboard();
mConferenceListFragment.getListView().requestFocus();
}
}
mListPagerAdapter.requestFocus(pos);
return true;
}
};
@ -267,50 +269,12 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
actionBar.addTab(mConferencesTab);
mViewPager.setOnPageChangeListener(mOnPageChangeListener);
mViewPager.setAdapter(new FragmentPagerAdapter(getFragmentManager()) {
@Override
public int getCount() {
return 2;
}
@Override
public Fragment getItem(int position) {
if (position == 0) {
return mContactsListFragment;
} else {
return mConferenceListFragment;
}
}
});
mListPagerAdapter = new ListPagerAdapter(getFragmentManager());
mViewPager.setAdapter(mListPagerAdapter);
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);
((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);
}
@ -321,6 +285,14 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
askForContactsPermissions();
}
@Override
public void onStop() {
if (mCurrentDialog != null) {
mCurrentDialog.dismiss();
}
super.onStop();
}
protected void openConversationForContact(int position) {
Contact contact = (Contact) contacts.get(position);
openConversationForContact(contact);
@ -451,7 +423,7 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
}
});
dialog.show();
mCurrentDialog = dialog.show();
}
@SuppressLint("InflateParams")
@ -476,6 +448,7 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
builder.setPositiveButton(R.string.join, null);
final AlertDialog dialog = builder.create();
dialog.show();
mCurrentDialog = dialog;
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(
new View.OnClickListener() {
@ -516,6 +489,7 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
xmppConnectionService.joinMuc(conversation);
}
dialog.dismiss();
mCurrentDialog = null;
switchToConversation(conversation);
}
} else {
@ -526,6 +500,7 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
xmppConnectionService.joinMuc(conversation);
}
dialog.dismiss();
mCurrentDialog = null;
switchToConversation(conversation);
}
}
@ -560,7 +535,8 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
}
});
builder.setNegativeButton(R.string.cancel, null);
builder.create().show();
mCurrentDialog = builder.create();
mCurrentDialog.show();
}
private Account getSelectedAccount(Spinner spinner) {
@ -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 {
private AdapterView.OnItemClickListener mOnItemClickListener;
private int mResContextMenu;