Skip to content

Commit

Permalink
Fixing a race condition where the onStop method of MainConfiguration was
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
iiordanov committed Feb 21, 2017
1 parent fa1be76 commit f5de326
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 29 deletions.
4 changes: 2 additions & 2 deletions eclipse_projects/bVNC/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.iiordanov.bVNC" android:installLocation="auto"
android:versionCode="3880" android:versionName="v3.8.8">
android:versionCode="3890" android:versionName="v3.8.9">

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="10"></uses-sdk>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
Expand All @@ -19,7 +19,7 @@
android:smallScreens="true"
android:anyDensity="true"/>

<application android:icon="@drawable/icon" android:label="bVNC" android:allowBackup="false" android:debuggable="false" android:theme="@style/AppTheme" >
<application android:icon="@drawable/icon" android:label="bVNC" android:allowBackup="false" android:debuggable="false" android:theme="@style/AppTheme" android:name="App">
<activity android:label="@string/app_name"
android:name="bVNC"
android:screenOrientation="unspecified"
Expand Down
18 changes: 18 additions & 0 deletions eclipse_projects/bVNC/src/com/iiordanov/bVNC/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.iiordanov.bVNC;

import android.app.Application;

public class App extends Application {

private Database database;

@Override
public void onCreate() {
super.onCreate();
database = new Database(this);
}

public Database getDatabase() {
return database;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,15 @@ boolean isNew()
}

public synchronized void save(SQLiteDatabase database) {
ContentValues values=Gen_getValues();
ContentValues values = Gen_getValues();
values.remove(GEN_FIELD__ID);
// Never save the SSH password and passphrase.
values.put(GEN_FIELD_SSHPASSWORD, "");
values.put(GEN_FIELD_SSHPASSPHRASE, "");
if ( ! getKeepPassword()) {
if (!getKeepPassword()) {
values.put(GEN_FIELD_PASSWORD, "");
}
if ( isNew()) {
if (isNew()) {
set_Id(database.insert(GEN_TABLE_NAME, null, values));
} else {
database.update(GEN_TABLE_NAME, values, GEN_FIELD__ID + " = ?", new String[] { Long.toString(get_Id()) });
Expand Down Expand Up @@ -598,8 +598,7 @@ public static MostRecentBean getMostRecent(SQLiteDatabase db) {
return recents.get(0);
}

public void saveAndWriteRecent(boolean saveEmpty) {
Database database = new Database (c);
public void saveAndWriteRecent(boolean saveEmpty, Database database) {

// We need server address or SSH server to be filled out to save. Otherwise,
// we keep adding empty connections.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,23 @@ public void onNothingSelected(AdapterView<?> ad) {
}
});

database = new Database(this);
database = ((App)getApplication()).getDatabase();
}

@Override
protected void onStart() {
Log.i(TAG, "onStart called");
super.onStart();
System.gc();
//arriveOnPage();
arriveOnPage();
}

@Override
protected void onResume() {
Log.i(TAG, "onResume called");
super.onResume();
System.gc();
//arriveOnPage();
arriveOnPage();
}

@Override
Expand Down Expand Up @@ -133,8 +133,6 @@ protected void onStop() {
if ( selected == null ) {
return;
}
updateSelectedFromView();
selected.saveAndWriteRecent(false);
}

@Override
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()) {
Expand Down
4 changes: 2 additions & 2 deletions eclipse_projects/bVNC/src/com/iiordanov/bVNC/aRDP.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions eclipse_projects/bVNC/src/com/iiordanov/bVNC/aSPICE.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions eclipse_projects/bVNC/src/com/iiordanov/bVNC/bVNC.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -406,7 +409,7 @@ public void updateAutoXInfo () {

// Update and save.
mainConfigDialog.updateViewFromSelected();
selected.saveAndWriteRecent(false);
selected.saveAndWriteRecent(false, database);
}

public void retainAutoXInfo () {
Expand All @@ -416,6 +419,6 @@ public void retainAutoXInfo () {

// Update and save.
mainConfigDialog.updateViewFromSelected();
selected.saveAndWriteRecent(false);
selected.saveAndWriteRecent(false, database);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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"));
Expand All @@ -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();
}

Expand Down

0 comments on commit f5de326

Please sign in to comment.