fixed crashes when activity got destroyed when selecting pgp key

This commit is contained in:
Daniel Gultsch 2015-12-01 14:18:07 +01:00
parent d42c82abf2
commit 2cd43f7042
2 changed files with 95 additions and 40 deletions

View File

@ -17,6 +17,7 @@ import android.provider.MediaStore;
import android.support.v4.widget.SlidingPaneLayout; import android.support.v4.widget.SlidingPaneLayout;
import android.support.v4.widget.SlidingPaneLayout.PanelSlideListener; import android.support.v4.widget.SlidingPaneLayout.PanelSlideListener;
import android.util.Log; import android.util.Log;
import android.util.Pair;
import android.view.Gravity; import android.view.Gravity;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.Menu; import android.view.Menu;
@ -109,6 +110,7 @@ public class ConversationActivity extends XmppActivity
private boolean mActivityPaused = false; private boolean mActivityPaused = false;
private AtomicBoolean mRedirected = new AtomicBoolean(false); private AtomicBoolean mRedirected = new AtomicBoolean(false);
private Pair<Integer, Intent> mPostponedActivityResult;
public Conversation getSelectedConversation() { public Conversation getSelectedConversation() {
return this.mSelectedConversation; return this.mSelectedConversation;
@ -1101,6 +1103,7 @@ public class ConversationActivity extends XmppActivity
mPendingFileUris.clear(); mPendingFileUris.clear();
mPendingGeoUri = null; mPendingGeoUri = null;
setSelectedConversation(conversationList.get(0)); setSelectedConversation(conversationList.get(0));
mPostponedActivityResult = null;
this.mConversationFragment.reInit(getSelectedConversation()); this.mConversationFragment.reInit(getSelectedConversation());
} else { } else {
this.mConversationFragment.messageListAdapter.updatePreferences(); this.mConversationFragment.messageListAdapter.updatePreferences();
@ -1108,6 +1111,10 @@ public class ConversationActivity extends XmppActivity
this.mConversationFragment.setupIme(); this.mConversationFragment.setupIme();
} }
if (this.mPostponedActivityResult != null) {
this.onActivityResult(mPostponedActivityResult.first, RESULT_OK, mPostponedActivityResult.second);
}
if(!forbidProcessingPendings) { if(!forbidProcessingPendings) {
for (Iterator<Uri> i = mPendingImageUris.iterator(); i.hasNext(); i.remove()) { for (Iterator<Uri> i = mPendingImageUris.iterator(); i.hasNext(); i.remove()) {
Uri foo = i.next(); Uri foo = i.next();
@ -1208,14 +1215,24 @@ public class ConversationActivity extends XmppActivity
if (requestCode == REQUEST_DECRYPT_PGP) { if (requestCode == REQUEST_DECRYPT_PGP) {
mConversationFragment.onActivityResult(requestCode, resultCode, data); mConversationFragment.onActivityResult(requestCode, resultCode, data);
} else if (requestCode == REQUEST_CHOOSE_PGP_ID) { } else if (requestCode == REQUEST_CHOOSE_PGP_ID) {
if (xmppConnectionServiceBound) {
if (data.getExtras().containsKey(OpenPgpApi.EXTRA_SIGN_KEY_ID)) { if (data.getExtras().containsKey(OpenPgpApi.EXTRA_SIGN_KEY_ID)) {
mSelectedConversation.getAccount().setPgpSignId(data.getExtras().getLong(OpenPgpApi.EXTRA_SIGN_KEY_ID)); mSelectedConversation.getAccount().setPgpSignId(data.getExtras().getLong(OpenPgpApi.EXTRA_SIGN_KEY_ID));
announcePgp(mSelectedConversation.getAccount(), null); announcePgp(mSelectedConversation.getAccount(), null);
} else { } else {
choosePgpSignId(mSelectedConversation.getAccount()); choosePgpSignId(mSelectedConversation.getAccount());
} }
this.mPostponedActivityResult = null;
} else {
this.mPostponedActivityResult = new Pair<>(requestCode, data);
}
} else if (requestCode == REQUEST_ANNOUNCE_PGP) { } else if (requestCode == REQUEST_ANNOUNCE_PGP) {
if (xmppConnectionServiceBound) {
announcePgp(mSelectedConversation.getAccount(), null); announcePgp(mSelectedConversation.getAccount(), null);
this.mPostponedActivityResult = null;
} else {
this.mPostponedActivityResult = new Pair<>(requestCode, data);
}
} else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_IMAGE) { } else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_IMAGE) {
mPendingImageUris.clear(); mPendingImageUris.clear();
mPendingImageUris.addAll(extractUriFromIntent(data)); mPendingImageUris.addAll(extractUriFromIntent(data));

View File

@ -9,6 +9,7 @@ import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.security.KeyChain; import android.security.KeyChain;
import android.security.KeyChainAliasCallback; import android.security.KeyChainAliasCallback;
import android.util.Pair;
import android.view.ContextMenu; import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo; import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu; import android.view.Menu;
@ -30,17 +31,25 @@ import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate; import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
import eu.siacs.conversations.ui.adapter.AccountAdapter; import eu.siacs.conversations.ui.adapter.AccountAdapter;
import eu.siacs.conversations.xmpp.jid.InvalidJidException;
import eu.siacs.conversations.xmpp.jid.Jid;
import org.openintents.openpgp.util.OpenPgpApi; import org.openintents.openpgp.util.OpenPgpApi;
public class ManageAccountActivity extends XmppActivity implements OnAccountUpdate, KeyChainAliasCallback, XmppConnectionService.OnAccountCreated { public class ManageAccountActivity extends XmppActivity implements OnAccountUpdate, KeyChainAliasCallback, XmppConnectionService.OnAccountCreated {
private final String STATE_SELECTED_ACCOUNT = "selected_account";
protected Account selectedAccount = null; protected Account selectedAccount = null;
protected Jid selectedAccountJid = null;
protected final List<Account> accountList = new ArrayList<>(); protected final List<Account> accountList = new ArrayList<>();
protected ListView accountListView; protected ListView accountListView;
protected AccountAdapter mAccountAdapter; protected AccountAdapter mAccountAdapter;
protected AtomicBoolean mInvokedAddAccount = new AtomicBoolean(false); protected AtomicBoolean mInvokedAddAccount = new AtomicBoolean(false);
protected Pair<Integer, Intent> mPostponedActivityResult = null;
@Override @Override
public void onAccountUpdate() { public void onAccountUpdate() {
refreshUi(); refreshUi();
@ -68,6 +77,17 @@ public class ManageAccountActivity extends XmppActivity implements OnAccountUpda
setContentView(R.layout.manage_accounts); setContentView(R.layout.manage_accounts);
if (savedInstanceState != null) {
String jid = savedInstanceState.getString(STATE_SELECTED_ACCOUNT);
if (jid != null) {
try {
this.selectedAccountJid = Jid.fromString(jid);
} catch (InvalidJidException e) {
this.selectedAccountJid = null;
}
}
}
accountListView = (ListView) findViewById(R.id.account_list); accountListView = (ListView) findViewById(R.id.account_list);
this.mAccountAdapter = new AccountAdapter(this, accountList); this.mAccountAdapter = new AccountAdapter(this, accountList);
accountListView.setAdapter(this.mAccountAdapter); accountListView.setAdapter(this.mAccountAdapter);
@ -83,12 +103,19 @@ public class ManageAccountActivity extends XmppActivity implements OnAccountUpda
} }
@Override @Override
public void onCreateContextMenu(ContextMenu menu, View v, public void onSaveInstanceState(final Bundle savedInstanceState) {
ContextMenuInfo menuInfo) { if (selectedAccount != null) {
savedInstanceState.putString(STATE_SELECTED_ACCOUNT, selectedAccount.getJid().toBareJid().toString());
}
super.onSaveInstanceState(savedInstanceState);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo); super.onCreateContextMenu(menu, v, menuInfo);
ManageAccountActivity.this.getMenuInflater().inflate( ManageAccountActivity.this.getMenuInflater().inflate(
R.menu.manageaccounts_context, menu); R.menu.manageaccounts_context, menu);
AdapterView.AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) menuInfo; AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) menuInfo;
this.selectedAccount = accountList.get(acmi.position); this.selectedAccount = accountList.get(acmi.position);
if (this.selectedAccount.isOptionSet(Account.OPTION_DISABLED)) { if (this.selectedAccount.isOptionSet(Account.OPTION_DISABLED)) {
menu.findItem(R.id.mgmt_account_disable).setVisible(false); menu.findItem(R.id.mgmt_account_disable).setVisible(false);
@ -103,9 +130,15 @@ public class ManageAccountActivity extends XmppActivity implements OnAccountUpda
@Override @Override
void onBackendConnected() { void onBackendConnected() {
if (selectedAccountJid != null) {
this.selectedAccount = xmppConnectionService.findAccountByJid(selectedAccountJid);
}
refreshUiReal(); refreshUiReal();
if (this.mPostponedActivityResult != null) {
this.onActivityResult(mPostponedActivityResult.first, RESULT_OK, mPostponedActivityResult.second);
}
if (Config.X509_VERIFICATION && this.accountList.size() == 0) { if (Config.X509_VERIFICATION && this.accountList.size() == 0) {
if (mInvokedAddAccount.compareAndSet(false,true)) { if (mInvokedAddAccount.compareAndSet(false, true)) {
addAccountFromKey(); addAccountFromKey();
} }
} }
@ -149,7 +182,7 @@ public class ManageAccountActivity extends XmppActivity implements OnAccountUpda
deleteAccount(selectedAccount); deleteAccount(selectedAccount);
return true; return true;
case R.id.mgmt_account_announce_pgp: case R.id.mgmt_account_announce_pgp:
choosePgpSignId(selectedAccount); publishOpenPGPPublicKey(selectedAccount);
return true; return true;
default: default:
return super.onContextItemSelected(item); return super.onContextItemSelected(item);
@ -211,7 +244,7 @@ public class ManageAccountActivity extends XmppActivity implements OnAccountUpda
try { try {
KeyChain.choosePrivateKeyAlias(this, this, null, null, null, -1, null); KeyChain.choosePrivateKeyAlias(this, this, null, null, null, -1, null);
} catch (ActivityNotFoundException e) { } catch (ActivityNotFoundException e) {
Toast.makeText(this,R.string.device_does_not_support_certificates,Toast.LENGTH_LONG).show(); Toast.makeText(this, R.string.device_does_not_support_certificates, Toast.LENGTH_LONG).show();
} }
} }
@ -231,7 +264,7 @@ public class ManageAccountActivity extends XmppActivity implements OnAccountUpda
} }
} }
} }
for(Account account : list) { for (Account account : list) {
disableAccount(account); disableAccount(account);
} }
} }
@ -267,7 +300,7 @@ public class ManageAccountActivity extends XmppActivity implements OnAccountUpda
} }
} }
} }
for(Account account : list) { for (Account account : list) {
enableAccount(account); enableAccount(account);
} }
} }
@ -284,7 +317,7 @@ public class ManageAccountActivity extends XmppActivity implements OnAccountUpda
private void publishOpenPGPPublicKey(Account account) { private void publishOpenPGPPublicKey(Account account) {
if (ManageAccountActivity.this.hasPgp()) { if (ManageAccountActivity.this.hasPgp()) {
announcePgp(account, null); choosePgpSignId(selectedAccount);
} else { } else {
this.showInstallPgpDialog(); this.showInstallPgpDialog();
} }
@ -312,6 +345,7 @@ public class ManageAccountActivity extends XmppActivity implements OnAccountUpda
protected void onActivityResult(int requestCode, int resultCode, Intent data) { protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) { if (resultCode == RESULT_OK) {
if (xmppConnectionServiceBound) {
if (requestCode == REQUEST_CHOOSE_PGP_ID) { if (requestCode == REQUEST_CHOOSE_PGP_ID) {
if (data.getExtras().containsKey(OpenPgpApi.EXTRA_SIGN_KEY_ID)) { if (data.getExtras().containsKey(OpenPgpApi.EXTRA_SIGN_KEY_ID)) {
selectedAccount.setPgpSignId(data.getExtras().getLong(OpenPgpApi.EXTRA_SIGN_KEY_ID)); selectedAccount.setPgpSignId(data.getExtras().getLong(OpenPgpApi.EXTRA_SIGN_KEY_ID));
@ -322,6 +356,10 @@ public class ManageAccountActivity extends XmppActivity implements OnAccountUpda
} else if (requestCode == REQUEST_ANNOUNCE_PGP) { } else if (requestCode == REQUEST_ANNOUNCE_PGP) {
announcePgp(selectedAccount, null); announcePgp(selectedAccount, null);
} }
this.mPostponedActivityResult = null;
} else {
this.mPostponedActivityResult = new Pair<>(requestCode, data);
}
} }
} }
@ -342,7 +380,7 @@ public class ManageAccountActivity extends XmppActivity implements OnAccountUpda
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
Toast.makeText(ManageAccountActivity.this,r,Toast.LENGTH_LONG).show(); Toast.makeText(ManageAccountActivity.this, r, Toast.LENGTH_LONG).show();
} }
}); });
} }