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;
}
};
@ -242,7 +244,7 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
protected void replaceToast(String msg) {
hideToast();
mToast = Toast.makeText(this, msg ,Toast.LENGTH_LONG);
mToast = Toast.makeText(this, msg, Toast.LENGTH_LONG);
mToast.show();
}
@ -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);
@ -350,10 +322,10 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
protected void openConversationsForBookmark(Bookmark bookmark) {
Jid jid = bookmark.getJid();
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;
}
Conversation conversation = xmppConnectionService.findOrCreateConversation(bookmark.getAccount(),jid, true);
Conversation conversation = xmppConnectionService.findOrCreateConversation(bookmark.getAccount(), jid, true);
conversation.setBookmark(bookmark);
if (!conversation.getMucOptions().online()) {
xmppConnectionService.joinMuc(conversation);
@ -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);
}
}
@ -555,12 +530,13 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
intent.putExtra("show_enter_jid", true);
intent.putExtra("subject", subject.getText().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);
}
});
builder.setNegativeButton(R.string.cancel, null);
builder.create().show();
mCurrentDialog = builder.create();
mCurrentDialog.show();
}
private Account getSelectedAccount(Spinner spinner) {
@ -634,7 +610,7 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_create_contact:
showCreateContactDialog(null,null);
showCreateContactDialog(null, null);
return true;
case R.id.action_join_conference:
showJoinConferenceDialog(null);
@ -719,7 +695,7 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
}
if (account != null && jids.size() > 0) {
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();
}
}
@ -872,13 +848,13 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
return false;
}
} else if (contacts.size() == 0) {
showCreateContactDialog(invite.getJid().toString(),invite.getFingerprint());
showCreateContactDialog(invite.getJid().toString(), invite.getFingerprint());
return false;
} else if (contacts.size() == 1) {
Contact contact = contacts.get(0);
if (invite.getFingerprint() != null) {
if (contact.addOtrFingerprint(invite.getFingerprint())) {
Log.d(Config.LOGTAG,"added new fingerprint");
Log.d(Config.LOGTAG, "added new fingerprint");
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 {
private AdapterView.OnItemClickListener mOnItemClickListener;
private int mResContextMenu;
@ -989,7 +1042,7 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
final AdapterView.AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) menuInfo;
if (mResContextMenu == R.menu.conference_context) {
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;
final Blockable contact = (Contact) activity.contacts.get(acmi.position);
final MenuItem blockUnblockItem = menu.findItem(R.id.context_contact_block_unblock);