From f5de32689ecd9180fe12e22432508f267f17ccb2 Mon Sep 17 00:00:00 2001 From: Iordan Iordanov Date: Tue, 21 Feb 2017 09:15:41 -0500 Subject: [PATCH] Fixing a race condition where the onStop method of MainConfiguration was overwriting the connection settings after the SSH host key was set. Also ensuring usage of the SQLite database is thread safe by implementing a method of instantiating and obtaining it following best practices. --- eclipse_projects/bVNC/AndroidManifest.xml | 4 ++-- .../bVNC/src/com/iiordanov/bVNC/App.java | 18 ++++++++++++++++++ .../src/com/iiordanov/bVNC/ConnectionBean.java | 9 ++++----- .../com/iiordanov/bVNC/MainConfiguration.java | 12 +++++------- .../iiordanov/bVNC/RemoteCanvasActivity.java | 4 ++-- .../bVNC/src/com/iiordanov/bVNC/aRDP.java | 4 ++-- .../bVNC/src/com/iiordanov/bVNC/aSPICE.java | 6 +++--- .../bVNC/src/com/iiordanov/bVNC/bVNC.java | 6 +++--- .../bVNC/dialogs/AutoXCustomizeDialog.java | 9 ++++++--- .../bVNC/dialogs/ImportTlsCaDialog.java | 7 +++++-- 10 files changed, 50 insertions(+), 29 deletions(-) create mode 100644 eclipse_projects/bVNC/src/com/iiordanov/bVNC/App.java diff --git a/eclipse_projects/bVNC/AndroidManifest.xml b/eclipse_projects/bVNC/AndroidManifest.xml index 5685367ea..65729db35 100644 --- a/eclipse_projects/bVNC/AndroidManifest.xml +++ b/eclipse_projects/bVNC/AndroidManifest.xml @@ -1,7 +1,7 @@ + android:versionCode="3890" android:versionName="v3.8.9"> @@ -19,7 +19,7 @@ android:smallScreens="true" android:anyDensity="true"/> - + ad) { } }); - database = new Database(this); + database = ((App)getApplication()).getDatabase(); } @Override @@ -92,7 +92,7 @@ protected void onStart() { Log.i(TAG, "onStart called"); super.onStart(); System.gc(); - //arriveOnPage(); + arriveOnPage(); } @Override @@ -100,7 +100,7 @@ protected void onResume() { Log.i(TAG, "onResume called"); super.onResume(); System.gc(); - //arriveOnPage(); + arriveOnPage(); } @Override @@ -133,8 +133,6 @@ protected void onStop() { if ( selected == null ) { return; } - updateSelectedFromView(); - selected.saveAndWriteRecent(false); } @Override @@ -172,7 +170,7 @@ protected void canvasStart() { private void start () { isConnecting = true; updateSelectedFromView(); - selected.saveAndWriteRecent(false); + selected.saveAndWriteRecent(false, database); Intent intent = new Intent(this, RemoteCanvasActivity.class); intent.putExtra(Constants.CONNECTION, selected.Gen_getValues()); startActivity(intent); @@ -305,7 +303,7 @@ public boolean onOptionsItemSelected(MenuItem item) { textNickname.setText("Copy of "+selected.getNickname()); updateSelectedFromView(); selected.set_Id(0); - selected.saveAndWriteRecent(false); + selected.saveAndWriteRecent(false, database); arriveOnPage(); break; case R.id.itemDeleteConnection: diff --git a/eclipse_projects/bVNC/src/com/iiordanov/bVNC/RemoteCanvasActivity.java b/eclipse_projects/bVNC/src/com/iiordanov/bVNC/RemoteCanvasActivity.java index 082414f0c..adb5555a0 100644 --- a/eclipse_projects/bVNC/src/com/iiordanov/bVNC/RemoteCanvasActivity.java +++ b/eclipse_projects/bVNC/src/com/iiordanov/bVNC/RemoteCanvasActivity.java @@ -193,7 +193,7 @@ void initialize () { if (Utils.querySharedPreferenceBoolean(this, Constants.forceLandscapeTag)) setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); - database = new Database(this); + database = ((App)getApplication()).getDatabase(); Intent i = getIntent(); connection = null; @@ -220,7 +220,7 @@ void initialize () { } if (connection.isSaved()) { - connection.saveAndWriteRecent(false); + connection.saveAndWriteRecent(false, database); } // we need to save the connection to display the loading screen, so otherwise we should exit if (!connection.isReadyForConnection()) { diff --git a/eclipse_projects/bVNC/src/com/iiordanov/bVNC/aRDP.java b/eclipse_projects/bVNC/src/com/iiordanov/bVNC/aRDP.java index 15a561052..ff73d1e96 100644 --- a/eclipse_projects/bVNC/src/com/iiordanov/bVNC/aRDP.java +++ b/eclipse_projects/bVNC/src/com/iiordanov/bVNC/aRDP.java @@ -487,7 +487,7 @@ public void setRemoteSoundTypeFromSettings (int type) { */ private void generatePubkey () { updateSelectedFromView(); - selected.saveAndWriteRecent(false); + selected.saveAndWriteRecent(false, database); Intent intent = new Intent(this, GeneratePubkeyActivity.class); intent.putExtra("PrivateKey",selected.getSshPrivKey()); startActivityForResult(intent, Constants.ACTIVITY_GEN_KEY); @@ -509,7 +509,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { " button to share, copy to clipboard, or export the public key now.", Toast.LENGTH_LONG).show(); selected.setSshPrivKey(privateKey); selected.setSshPubKey((String)b.get("PublicKey")); - selected.saveAndWriteRecent(false); + selected.saveAndWriteRecent(false, database); } else Log.i (TAG, "The user cancelled SSH key generation."); break; diff --git a/eclipse_projects/bVNC/src/com/iiordanov/bVNC/aSPICE.java b/eclipse_projects/bVNC/src/com/iiordanov/bVNC/aSPICE.java index 1fa90ac28..886489059 100644 --- a/eclipse_projects/bVNC/src/com/iiordanov/bVNC/aSPICE.java +++ b/eclipse_projects/bVNC/src/com/iiordanov/bVNC/aSPICE.java @@ -279,7 +279,7 @@ protected Dialog onCreateDialog(int id) { case R.id.itemMainScreenHelp: return createHelpDialog(); case R.layout.import_tls_ca_dialog: - return new ImportTlsCaDialog(this); + return new ImportTlsCaDialog(this, database); } return null; } @@ -446,7 +446,7 @@ protected void updateSelectedFromView() { */ private void generatePubkey() { updateSelectedFromView(); - selected.saveAndWriteRecent(false); + selected.saveAndWriteRecent(false, database); Intent intent = new Intent(this, GeneratePubkeyActivity.class); intent.putExtra("PrivateKey", selected.getSshPrivKey()); startActivityForResult(intent, Constants.ACTIVITY_GEN_KEY); @@ -473,7 +473,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { Toast.LENGTH_LONG).show(); selected.setSshPrivKey(privateKey); selected.setSshPubKey((String) b.get("PublicKey")); - selected.saveAndWriteRecent(false); + selected.saveAndWriteRecent(false, database); } else Log.i(TAG, "The user cancelled SSH key generation."); break; diff --git a/eclipse_projects/bVNC/src/com/iiordanov/bVNC/bVNC.java b/eclipse_projects/bVNC/src/com/iiordanov/bVNC/bVNC.java index 8935b609d..bf7b3de67 100644 --- a/eclipse_projects/bVNC/src/com/iiordanov/bVNC/bVNC.java +++ b/eclipse_projects/bVNC/src/com/iiordanov/bVNC/bVNC.java @@ -280,7 +280,7 @@ protected Dialog onCreateDialog(int id) { case R.layout.repeater_dialog: return new RepeaterDialog(this); case R.layout.auto_x_customize: - Dialog d = new AutoXCustomizeDialog(this); + Dialog d = new AutoXCustomizeDialog(this, database); d.setCancelable(false); return d; } @@ -446,7 +446,7 @@ protected void updateSelectedFromView() { */ private void generatePubkey () { updateSelectedFromView(); - selected.saveAndWriteRecent(false); + selected.saveAndWriteRecent(false, database); Intent intent = new Intent(this, GeneratePubkeyActivity.class); intent.putExtra("PrivateKey",selected.getSshPrivKey()); startActivityForResult(intent, Constants.ACTIVITY_GEN_KEY); @@ -468,7 +468,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { " button to share, copy to clipboard, or export the public key now.", Toast.LENGTH_LONG).show(); selected.setSshPrivKey(privateKey); selected.setSshPubKey((String)b.get("PublicKey")); - selected.saveAndWriteRecent(false); + selected.saveAndWriteRecent(false, database); } else Log.i (TAG, "The user cancelled SSH key generation."); break; diff --git a/eclipse_projects/bVNC/src/com/iiordanov/bVNC/dialogs/AutoXCustomizeDialog.java b/eclipse_projects/bVNC/src/com/iiordanov/bVNC/dialogs/AutoXCustomizeDialog.java index d6d11df72..212cd5b91 100644 --- a/eclipse_projects/bVNC/src/com/iiordanov/bVNC/dialogs/AutoXCustomizeDialog.java +++ b/eclipse_projects/bVNC/src/com/iiordanov/bVNC/dialogs/AutoXCustomizeDialog.java @@ -19,6 +19,7 @@ package com.iiordanov.bVNC.dialogs; +import com.iiordanov.bVNC.Database; import com.iiordanov.bVNC.bVNC; import com.iiordanov.bVNC.ConnectionBean; import com.iiordanov.bVNC.R; @@ -75,16 +76,18 @@ public class AutoXCustomizeDialog extends AlertDialog { private CheckBox checkboxAutoXUnixAuth; private RandomString rnd; private Button buttonAutoXHelp; + private Database database; /** * @param context */ - public AutoXCustomizeDialog(Context context) { + public AutoXCustomizeDialog(Context context, Database database) { super(context); setOwnerActivity((Activity)context); mainConfigDialog = (bVNC)context; selected = mainConfigDialog.getCurrentConnection(); rnd = new RandomString(); + this.database = database; } private static final Intent docIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://iiordanov.blogspot.ca/2012/10/looking-for-nx-client-for-android-or.html")); @@ -406,7 +409,7 @@ public void updateAutoXInfo () { // Update and save. mainConfigDialog.updateViewFromSelected(); - selected.saveAndWriteRecent(false); + selected.saveAndWriteRecent(false, database); } public void retainAutoXInfo () { @@ -416,6 +419,6 @@ public void retainAutoXInfo () { // Update and save. mainConfigDialog.updateViewFromSelected(); - selected.saveAndWriteRecent(false); + selected.saveAndWriteRecent(false, database); } } diff --git a/eclipse_projects/bVNC/src/com/iiordanov/bVNC/dialogs/ImportTlsCaDialog.java b/eclipse_projects/bVNC/src/com/iiordanov/bVNC/dialogs/ImportTlsCaDialog.java index 2bad9176f..589119611 100644 --- a/eclipse_projects/bVNC/src/com/iiordanov/bVNC/dialogs/ImportTlsCaDialog.java +++ b/eclipse_projects/bVNC/src/com/iiordanov/bVNC/dialogs/ImportTlsCaDialog.java @@ -26,6 +26,7 @@ import java.io.IOException; import com.iiordanov.bVNC.ConnectionBean; +import com.iiordanov.bVNC.Database; import com.iiordanov.bVNC.R; import com.iiordanov.bVNC.aSPICE; @@ -54,15 +55,17 @@ public class ImportTlsCaDialog extends AlertDialog { private EditText caCert; private Button importButton; private Button helpButton; + private Database database; /** * @param context */ - public ImportTlsCaDialog(Context context) { + public ImportTlsCaDialog(Context context, Database database) { super(context); setOwnerActivity((Activity)context); mainConfigPage = (aSPICE)context; selected = mainConfigPage.getCurrentConnection(); + this.database = database; } private static final Intent docIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://spice-space.org/page/SSLConnection")); @@ -80,7 +83,7 @@ public void onBackPressed () { selected.setCaCert(caCert.getText().toString()); selected.setCertSubject(certSubject.getText().toString()); mainConfigPage.updateViewFromSelected(); - selected.saveAndWriteRecent(false); + selected.saveAndWriteRecent(false, database); dismiss(); }