From 416481bb656c5fc73d0500b09a0c4ec62590dc00 Mon Sep 17 00:00:00 2001
From: Daniel Gultsch <daniel@gultsch.de>
Date: Wed, 7 Sep 2016 14:34:58 +0200
Subject: [PATCH] be a bit more careful when deleting and deactivating accounts

---
 .../persistance/DatabaseBackend.java          | 11 ++++----
 .../services/XmppConnectionService.java       | 26 ++++++++++++-------
 .../conversations/ui/EditAccountActivity.java |  9 +++++--
 .../ui/ManageAccountActivity.java             |  8 ++++--
 .../conversations/xmpp/XmppConnection.java    | 14 +++++++---
 src/main/res/values/strings.xml               |  1 +
 6 files changed, 47 insertions(+), 22 deletions(-)

diff --git a/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java b/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java
index 9e1085453..d095e2fa2 100644
--- a/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java
+++ b/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java
@@ -607,17 +607,18 @@ public class DatabaseBackend extends SQLiteOpenHelper {
 		return list;
 	}
 
-	public void updateAccount(Account account) {
+	public boolean updateAccount(Account account) {
 		SQLiteDatabase db = this.getWritableDatabase();
 		String[] args = {account.getUuid()};
-		db.update(Account.TABLENAME, account.getContentValues(), Account.UUID
-				+ "=?", args);
+		final int rows = db.update(Account.TABLENAME, account.getContentValues(), Account.UUID + "=?", args);
+		return rows == 1;
 	}
 
-	public void deleteAccount(Account account) {
+	public boolean deleteAccount(Account account) {
 		SQLiteDatabase db = this.getWritableDatabase();
 		String[] args = {account.getUuid()};
-		db.delete(Account.TABLENAME, Account.UUID + "=?", args);
+		final int rows = db.delete(Account.TABLENAME, Account.UUID + "=?", args);
+		return rows == 1;
 	}
 
 	public boolean hasEnabledAccounts() {
diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java
index 337c0b4d2..2fb33bb60 100644
--- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java
+++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java
@@ -1642,12 +1642,17 @@ public class XmppConnectionService extends Service {
 		}
 	}
 
-	public void updateAccount(final Account account) {
-		this.statusListener.onStatusChanged(account);
-		databaseBackend.updateAccount(account);
-		reconnectAccountInBackground(account);
-		updateAccountUi();
-		getNotificationService().updateErrorNotification();
+	public boolean updateAccount(final Account account) {
+		if (databaseBackend.updateAccount(account)) {
+			this.statusListener.onStatusChanged(account);
+			databaseBackend.updateAccount(account);
+			reconnectAccountInBackground(account);
+			updateAccountUi();
+			getNotificationService().updateErrorNotification();
+			return true;
+		} else {
+			return false;
+		}
 	}
 
 	public void updateAccountPasswordOnServer(final Account account, final String newPassword, final OnAccountPasswordChanged callback) {
@@ -1685,12 +1690,14 @@ public class XmppConnectionService extends Service {
 					public void run() {
 						disconnect(account, true);
 					}
-				});
+				}).start();
 			}
 			Runnable runnable = new Runnable() {
 				@Override
 				public void run() {
-					databaseBackend.deleteAccount(account);
+					if (!databaseBackend.deleteAccount(account)) {
+						Log.d(Config.LOGTAG,account.getJid().toBareJid()+": unable to delete account");
+					}
 				}
 			};
 			mDatabaseExecutor.execute(runnable);
@@ -3240,7 +3247,8 @@ public class XmppConnectionService extends Service {
 		}
 	}
 
-	public void sendOfflinePresence(final Account account) {
+	private void sendOfflinePresence(final Account account) {
+		Log.d(Config.LOGTAG,account.getJid().toBareJid()+": sending offline presence");
 		sendPresencePacket(account, mPresenceGenerator.sendOfflinePresence(account));
 	}
 
diff --git a/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java b/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java
index fb9a14a69..c10d2741e 100644
--- a/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java
+++ b/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java
@@ -127,7 +127,9 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate
 			}
 			if (mAccount != null && mAccount.getStatus() == Account.State.DISABLED && !accountInfoEdited()) {
 				mAccount.setOption(Account.OPTION_DISABLED, false);
-				xmppConnectionService.updateAccount(mAccount);
+				if (!xmppConnectionService.updateAccount(mAccount)) {
+					Toast.makeText(EditAccountActivity.this,R.string.unable_to_update_account,Toast.LENGTH_SHORT).show();
+				}
 				return;
 			}
 			final boolean registerNewAccount = mRegisterNew.isChecked() && !Config.DISALLOW_REGISTRATION_IN_UI;
@@ -204,7 +206,10 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate
 				mPasswordConfirm.setError(null);
 				mAccount.setPassword(password);
 				mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
-				xmppConnectionService.updateAccount(mAccount);
+				if (!xmppConnectionService.updateAccount(mAccount)) {
+					Toast.makeText(EditAccountActivity.this,R.string.unable_to_update_account,Toast.LENGTH_SHORT).show();
+					return;
+				}
 			} else {
 				if (xmppConnectionService.findAccountByJid(jid) != null) {
 					mAccountJid.setError(getString(R.string.account_already_exists));
diff --git a/src/main/java/eu/siacs/conversations/ui/ManageAccountActivity.java b/src/main/java/eu/siacs/conversations/ui/ManageAccountActivity.java
index 2d29c521e..f52ccb197 100644
--- a/src/main/java/eu/siacs/conversations/ui/ManageAccountActivity.java
+++ b/src/main/java/eu/siacs/conversations/ui/ManageAccountActivity.java
@@ -318,12 +318,16 @@ public class ManageAccountActivity extends XmppActivity implements OnAccountUpda
 
 	private void disableAccount(Account account) {
 		account.setOption(Account.OPTION_DISABLED, true);
-		xmppConnectionService.updateAccount(account);
+		if (!xmppConnectionService.updateAccount(account)) {
+			Toast.makeText(this,R.string.unable_to_update_account,Toast.LENGTH_SHORT).show();
+		}
 	}
 
 	private void enableAccount(Account account) {
 		account.setOption(Account.OPTION_DISABLED, false);
-		xmppConnectionService.updateAccount(account);
+		if (!xmppConnectionService.updateAccount(account)) {
+			Toast.makeText(this,R.string.unable_to_update_account,Toast.LENGTH_SHORT).show();
+		}
 	}
 
 	private void publishOpenPGPPublicKey(Account account) {
diff --git a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java
index ddc538e54..c49477f3c 100644
--- a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java
+++ b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java
@@ -1381,8 +1381,10 @@ public class XmppConnection implements Runnable {
 			try {
 				socket.close();
 			} catch (IOException e) {
-				e.printStackTrace();
+				Log.d(Config.LOGTAG,account.getJid().toBareJid()+": io exception "+e.getMessage()+" during force close");
 			}
+		} else {
+			Log.d(Config.LOGTAG,account.getJid().toBareJid()+": socket was null during force close");
 		}
 	}
 
@@ -1407,7 +1409,11 @@ public class XmppConnection implements Runnable {
 							Log.d(Config.LOGTAG, account.getJid().toBareJid()+": waiting for tag writer to finish");
 							warned = true;
 						}
-						Thread.sleep(200);
+						try {
+							Thread.sleep(200);
+						} catch(InterruptedException e) {
+							Log.d(Config.LOGTAG,account.getJid().toBareJid()+": sleep interrupted");
+						}
 						i++;
 					}
 					if (warned) {
@@ -1417,8 +1423,8 @@ public class XmppConnection implements Runnable {
 					tagWriter.writeTag(Tag.end("stream:stream"));
 				} catch (final IOException e) {
 					Log.d(Config.LOGTAG,account.getJid().toBareJid()+": io exception during disconnect ("+e.getMessage()+")");
-				} catch (final InterruptedException e) {
-					Log.d(Config.LOGTAG, "interrupted");
+				} finally {
+					forceCloseSocket();
 				}
 			}
 		}
diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml
index dbc4ab378..5a5bb6ad2 100644
--- a/src/main/res/values/strings.xml
+++ b/src/main/res/values/strings.xml
@@ -681,4 +681,5 @@
 	<string name="allow">Allow</string>
 	<string name="no_permission_to_access_x">No permission to access %s</string>
 	<string name="remote_server_not_found">Remote server not found</string>
+	<string name="unable_to_update_account">Unable to update account</string>
 </resources>