From 57afa9bdc5b269a48bd27fc8455cab4cb956d02a Mon Sep 17 00:00:00 2001 From: Kenny Root Date: Mon, 7 Sep 2015 22:15:57 -0700 Subject: Never cache instances of the HostDatabase --- .../main/java/org/connectbot/ColorsActivity.java | 40 +++++++--------------- .../java/org/connectbot/HostEditorActivity.java | 18 ++++------ .../main/java/org/connectbot/HostListActivity.java | 13 ++----- .../org/connectbot/PortForwardListActivity.java | 13 +++---- .../org/connectbot/service/TerminalBridge.java | 11 +++--- .../org/connectbot/service/TerminalManager.java | 15 +++----- .../main/java/org/connectbot/transport/SSH.java | 20 ++++++----- .../java/org/connectbot/util/HostDatabase.java | 2 -- 8 files changed, 50 insertions(+), 82 deletions(-) diff --git a/app/src/main/java/org/connectbot/ColorsActivity.java b/app/src/main/java/org/connectbot/ColorsActivity.java index 54f52da..8725a51 100644 --- a/app/src/main/java/org/connectbot/ColorsActivity.java +++ b/app/src/main/java/org/connectbot/ColorsActivity.java @@ -56,7 +56,6 @@ public class ColorsActivity extends Activity implements OnItemClickListener, OnC private int mColorScheme; private List mColorList; - private HostDatabase mHostDb; private int mCurrentColor = 0; @@ -74,10 +73,10 @@ public class ColorsActivity extends Activity implements OnItemClickListener, OnC mColorScheme = HostDatabase.DEFAULT_COLOR_SCHEME; - mHostDb = HostDatabase.get(this); + HostDatabase hostDb = HostDatabase.get(this); - mColorList = Arrays.asList(mHostDb.getColorsForScheme(mColorScheme)); - mDefaultColors = mHostDb.getDefaultColorsForScheme(mColorScheme); + mColorList = Arrays.asList(hostDb.getColorsForScheme(mColorScheme)); + mDefaultColors = hostDb.getDefaultColorsForScheme(mColorScheme); mColorGrid = (GridView) findViewById(R.id.color_grid); mColorGrid.setAdapter(new ColorsAdapter(true)); @@ -95,24 +94,6 @@ public class ColorsActivity extends Activity implements OnItemClickListener, OnC mBgSpinner.setOnItemSelectedListener(this); } - @Override - protected void onDestroy() { - super.onDestroy(); - - if (mHostDb != null) { - mHostDb.close(); - mHostDb = null; - } - } - - @Override - protected void onResume() { - super.onResume(); - - if (mHostDb == null) - mHostDb = HostDatabase.get(this); - } - private class ColorsAdapter extends BaseAdapter { private final boolean mSquareViews; private final int mResourceLabel; @@ -304,7 +285,8 @@ public class ColorsActivity extends Activity implements OnItemClickListener, OnC public void onNothingSelected(AdapterView arg0) { } public void colorChanged(int value) { - mHostDb.setGlobalColor(mCurrentColor, value); + HostDatabase hostDb = HostDatabase.get(this); + hostDb.setGlobalColor(mCurrentColor, value); mColorList.set(mCurrentColor, value); mColorGrid.invalidateViews(); } @@ -324,8 +306,10 @@ public class ColorsActivity extends Activity implements OnItemClickListener, OnC } } - if (needUpdate) - mHostDb.setDefaultColorsForScheme(mColorScheme, mDefaultColors[0], mDefaultColors[1]); + if (needUpdate) { + HostDatabase hostDb = HostDatabase.get(this); + hostDb.setDefaultColorsForScheme(mColorScheme, mDefaultColors[0], mDefaultColors[1]); + } } @Override @@ -338,10 +322,12 @@ public class ColorsActivity extends Activity implements OnItemClickListener, OnC reset.setIcon(android.R.drawable.ic_menu_revert); reset.setOnMenuItemClickListener(new OnMenuItemClickListener() { public boolean onMenuItemClick(MenuItem arg0) { + HostDatabase hostDb = HostDatabase.get(ColorsActivity.this); + // Reset each individual color to defaults. for (int i = 0; i < Colors.defaults.length; i++) { if (!mColorList.get(i).equals(Colors.defaults[i])) { - mHostDb.setGlobalColor(i, Colors.defaults[i]); + hostDb.setGlobalColor(i, Colors.defaults[i]); mColorList.set(i, Colors.defaults[i]); } } @@ -350,7 +336,7 @@ public class ColorsActivity extends Activity implements OnItemClickListener, OnC // Reset the default FG/BG colors as well. mFgSpinner.setSelection(HostDatabase.DEFAULT_FG_COLOR); mBgSpinner.setSelection(HostDatabase.DEFAULT_BG_COLOR); - mHostDb.setDefaultColorsForScheme(HostDatabase.DEFAULT_COLOR_SCHEME, + hostDb.setDefaultColorsForScheme(HostDatabase.DEFAULT_COLOR_SCHEME, HostDatabase.DEFAULT_FG_COLOR, HostDatabase.DEFAULT_BG_COLOR); return true; diff --git a/app/src/main/java/org/connectbot/HostEditorActivity.java b/app/src/main/java/org/connectbot/HostEditorActivity.java index 4fc41bf..7316775 100644 --- a/app/src/main/java/org/connectbot/HostEditorActivity.java +++ b/app/src/main/java/org/connectbot/HostEditorActivity.java @@ -68,6 +68,7 @@ public class HostEditorActivity extends PreferenceActivity implements OnSharedPr // fill a cursor and cache the values locally // this makes sure we don't have any floating cursor to dispose later + HostDatabase hostdb = HostDatabase.get(HostEditorActivity.this); SQLiteDatabase db = hostdb.getWritableDatabase(); Cursor cursor = db.query(table, null, "_id = ?", new String[] { String.valueOf(id) }, null, null, null); @@ -98,7 +99,7 @@ public class HostEditorActivity extends PreferenceActivity implements OnSharedPr } public boolean commit() { - //Log.d(this.getClass().toString(), "commit() changes back to database"); + HostDatabase hostdb = HostDatabase.get(HostEditorActivity.this); SQLiteDatabase db = hostdb.getWritableDatabase(); db.beginTransaction(); try { @@ -214,9 +215,6 @@ public class HostEditorActivity extends PreferenceActivity implements OnSharedPr protected static final String TAG = "CB.HostEditorActivity"; - protected HostDatabase hostdb = null; - private PubkeyDatabase pubkeydb = null; - private CursorPreferenceHack pref; private ServiceConnection connection; @@ -232,8 +230,7 @@ public class HostEditorActivity extends PreferenceActivity implements OnSharedPr // TODO: we could pass through a specific ContentProvider uri here //this.getPreferenceManager().setSharedPreferencesName(uri); - this.hostdb = HostDatabase.get(this); - this.pubkeydb = PubkeyDatabase.get(this); + HostDatabase hostdb = HostDatabase.get(this); host = hostdb.findHostById(hostId); @@ -259,6 +256,7 @@ public class HostEditorActivity extends PreferenceActivity implements OnSharedPr // TODO: should consider moving into onStart, but we dont have a good way of resetting the listpref after filling once ListPreference pubkeyPref = (ListPreference) findPreference(HostDatabase.FIELD_HOST_PUBKEYID); + PubkeyDatabase pubkeydb = PubkeyDatabase.get(this); List pubkeyNicks = new LinkedList(Arrays.asList(pubkeyPref.getEntries())); pubkeyNicks.addAll(pubkeydb.allValues(PubkeyDatabase.FIELD_PUBKEY_NICKNAME)); pubkeyPref.setEntries(pubkeyNicks.toArray(new CharSequence[pubkeyNicks.size()])); @@ -293,9 +291,6 @@ public class HostEditorActivity extends PreferenceActivity implements OnSharedPr super.onStart(); bindService(new Intent(this, TerminalManager.class), connection, Context.BIND_AUTO_CREATE); - - hostdb = HostDatabase.get(this); - pubkeydb = PubkeyDatabase.get(this); } @Override @@ -303,12 +298,11 @@ public class HostEditorActivity extends PreferenceActivity implements OnSharedPr super.onStop(); unbindService(connection); - - hostdb = null; - pubkeydb = null; } private void updateSummaries() { + PubkeyDatabase pubkeydb = PubkeyDatabase.get(this); + // for all text preferences, set hint as current database value for (String key : this.pref.values.keySet()) { if (key.equals(HostDatabase.FIELD_HOST_POSTLOGIN)) continue; diff --git a/app/src/main/java/org/connectbot/HostListActivity.java b/app/src/main/java/org/connectbot/HostListActivity.java index 4c0a443..7a4c334 100644 --- a/app/src/main/java/org/connectbot/HostListActivity.java +++ b/app/src/main/java/org/connectbot/HostListActivity.java @@ -72,7 +72,6 @@ public class HostListActivity extends ListActivity implements OnHostStatusChange protected TerminalManager bound = null; - protected HostDatabase hostdb; private List hosts; protected LayoutInflater inflater = null; @@ -125,8 +124,6 @@ public class HostListActivity extends ListActivity implements OnHostStatusChange // start the terminal manager service this.bindService(new Intent(this, TerminalManager.class), connection, Context.BIND_AUTO_CREATE); - - hostdb = HostDatabase.get(this); } @Override @@ -134,8 +131,6 @@ public class HostListActivity extends ListActivity implements OnHostStatusChange super.onStop(); this.unbindService(connection); - hostdb = null; - closeOnDisconnectAll = true; } @@ -206,7 +201,6 @@ public class HostListActivity extends ListActivity implements OnHostStatusChange || Intent.ACTION_PICK.equals(getIntent().getAction()); // connect with hosts database and populate list - this.hostdb = HostDatabase.get(this); ListView list = this.getListView(); this.sortedByColor = prefs.getBoolean(PreferenceConstants.SORT_BY_COLOR, false); @@ -399,7 +393,7 @@ public class HostListActivity extends ListActivity implements OnHostStatusChange if (bridge != null) bridge.dispatchDisconnect(true); - hostdb.deleteHost(host); + HostDatabase.get(HostListActivity.this).deleteHost(host); updateList(); } }) @@ -460,6 +454,7 @@ public class HostListActivity extends ListActivity implements OnHostStatusChange return false; } + HostDatabase hostdb = HostDatabase.get(this); HostBean host = TransportFactory.findHost(hostdb, uri); if (host == null) { host = TransportFactory.getTransport(uri.getScheme()).createHost(uri); @@ -485,9 +480,7 @@ public class HostListActivity extends ListActivity implements OnHostStatusChange edit.commit(); } - if (hostdb == null) - hostdb = HostDatabase.get(this); - + HostDatabase hostdb = HostDatabase.get(this); hosts = hostdb.getHosts(sortedByColor); // Don't lose hosts that are connected via shortcuts but not in the database. diff --git a/app/src/main/java/org/connectbot/PortForwardListActivity.java b/app/src/main/java/org/connectbot/PortForwardListActivity.java index b0f835d..408e194 100644 --- a/app/src/main/java/org/connectbot/PortForwardListActivity.java +++ b/app/src/main/java/org/connectbot/PortForwardListActivity.java @@ -68,8 +68,6 @@ public class PortForwardListActivity extends ListActivity { private static final int LISTENER_CYCLE_TIME = 500; - protected HostDatabase hostdb; - private List portForwards; private ServiceConnection connection = null; @@ -83,8 +81,6 @@ public class PortForwardListActivity extends ListActivity { super.onStart(); this.bindService(new Intent(this, TerminalManager.class), connection, Context.BIND_AUTO_CREATE); - - hostdb = HostDatabase.get(this); } @Override @@ -92,8 +88,6 @@ public class PortForwardListActivity extends ListActivity { super.onStop(); this.unbindService(connection); - - hostdb = null; } @Override @@ -105,7 +99,7 @@ public class PortForwardListActivity extends ListActivity { setContentView(R.layout.act_portforwardlist); // connect with hosts database and populate list - this.hostdb = HostDatabase.get(this); + HostDatabase hostdb = HostDatabase.get(this); host = hostdb.findHostById(hostId); { @@ -342,6 +336,7 @@ public class PortForwardListActivity extends ListActivity { if (hostBridge != null) hostBridge.removePortForward(pfb); + HostDatabase hostdb = HostDatabase.get(PortForwardListActivity.this); hostdb.deletePortForward(pfb); } catch (Exception e) { Log.e(TAG, "Could not delete port forward", e); @@ -368,8 +363,8 @@ public class PortForwardListActivity extends ListActivity { if (hostBridge != null) { this.portForwards = hostBridge.getPortForwards(); } else { - if (this.hostdb == null) return; - this.portForwards = this.hostdb.getPortForwardsForHost(host); + HostDatabase hostdb = HostDatabase.get(this); + this.portForwards = hostdb.getPortForwardsForHost(host); } PortForwardAdapter adapter = new PortForwardAdapter(this, portForwards); diff --git a/app/src/main/java/org/connectbot/service/TerminalBridge.java b/app/src/main/java/org/connectbot/service/TerminalBridge.java index 6d47f96..6f76b02 100644 --- a/app/src/main/java/org/connectbot/service/TerminalBridge.java +++ b/app/src/main/java/org/connectbot/service/TerminalBridge.java @@ -273,7 +273,8 @@ public class TerminalBridge implements VDUDisplay { transport.setEmulation(emulation); if (transport.canForwardPorts()) { - for (PortForwardBean portForward : manager.hostdb.getPortForwardsForHost(host)) + HostDatabase hostDb = HostDatabase.get(manager); + for (PortForwardBean portForward : hostDb.getPortForwardsForHost(host)) transport.addPortForward(portForward); } @@ -533,7 +534,8 @@ public class TerminalBridge implements VDUDisplay { } host.setFontSize((int) sizeDp); - manager.hostdb.updateFontSize(host); + HostDatabase hostDb = HostDatabase.get(manager); + hostDb.updateFontSize(host); forcedSize = false; } @@ -950,11 +952,12 @@ public class TerminalBridge implements VDUDisplay { } public final void resetColors() { - int[] defaults = manager.hostdb.getDefaultColorsForScheme(HostDatabase.DEFAULT_COLOR_SCHEME); + HostDatabase hostDb = HostDatabase.get(manager); + int[] defaults = hostDb.getDefaultColorsForScheme(HostDatabase.DEFAULT_COLOR_SCHEME); defaultFg = defaults[0]; defaultBg = defaults[1]; - color = manager.hostdb.getColorsForScheme(HostDatabase.DEFAULT_COLOR_SCHEME); + color = hostDb.getColorsForScheme(HostDatabase.DEFAULT_COLOR_SCHEME); } private static Pattern urlPattern = null; diff --git a/app/src/main/java/org/connectbot/service/TerminalManager.java b/app/src/main/java/org/connectbot/service/TerminalManager.java index 1f0ee29..3a58c8a 100644 --- a/app/src/main/java/org/connectbot/service/TerminalManager.java +++ b/app/src/main/java/org/connectbot/service/TerminalManager.java @@ -87,9 +87,6 @@ public class TerminalManager extends Service implements BridgeDisconnectedListen public Resources res; - public HostDatabase hostdb; - public PubkeyDatabase pubkeydb; - protected SharedPreferences prefs; final private IBinder binder = new TerminalBinder(); @@ -129,11 +126,9 @@ public class TerminalManager extends Service implements BridgeDisconnectedListen pubkeyTimer = new Timer("pubkeyTimer", true); - hostdb = HostDatabase.get(this); - pubkeydb = PubkeyDatabase.get(this); - // load all marked pubkeys into memory updateSavingKeys(); + PubkeyDatabase pubkeydb = PubkeyDatabase.get(this); List pubkeys = pubkeydb.getAllStartPubkeys(); for (PubkeyBean pubkey : pubkeys) { @@ -173,9 +168,6 @@ public class TerminalManager extends Service implements BridgeDisconnectedListen disconnectAll(true, false); - hostdb = null; - pubkeydb = null; - synchronized (this) { if (idleTimer != null) idleTimer.cancel(); @@ -270,10 +262,12 @@ public class TerminalManager extends Service implements BridgeDisconnectedListen * format specified by an individual transport. */ public TerminalBridge openConnection(Uri uri) throws Exception { + HostDatabase hostdb = HostDatabase.get(this); HostBean host = TransportFactory.findHost(hostdb, uri); - if (host == null) + if (host == null) { host = TransportFactory.getTransport(uri.getScheme()).createHost(uri); + } return openConnection(host); } @@ -283,6 +277,7 @@ public class TerminalManager extends Service implements BridgeDisconnectedListen * to {@link HostDatabase}. */ private void touchHost(HostBean host) { + HostDatabase hostdb = HostDatabase.get(this); hostdb.touchHost(host); } diff --git a/app/src/main/java/org/connectbot/transport/SSH.java b/app/src/main/java/org/connectbot/transport/SSH.java index 4bad2f1..f3b85a6 100644 --- a/app/src/main/java/org/connectbot/transport/SSH.java +++ b/app/src/main/java/org/connectbot/transport/SSH.java @@ -141,7 +141,8 @@ public class SSH extends AbsTransport implements ConnectionMonitor, InteractiveC String serverHostKeyAlgorithm, byte[] serverHostKey) throws IOException { // read in all known hosts from hostdb - KnownHosts hosts = manager.hostdb.getKnownHosts(); + HostDatabase hostDb = HostDatabase.get(manager); + KnownHosts hosts = hostDb.getKnownHosts(); Boolean result; String matchName = String.format(Locale.US, "%s:%d", hostname, port); @@ -172,7 +173,7 @@ public class SSH extends AbsTransport implements ConnectionMonitor, InteractiveC if (result == null) return false; if (result.booleanValue()) { // save this key in known database - manager.hostdb.saveKnownHost(hostname, port, serverHostKeyAlgorithm, serverHostKey); + hostDb.saveKnownHost(hostname, port, serverHostKeyAlgorithm, serverHostKey); } return result.booleanValue(); @@ -195,7 +196,7 @@ public class SSH extends AbsTransport implements ConnectionMonitor, InteractiveC result = bridge.promptHelper.requestBooleanPrompt(null, manager.res.getString(R.string.prompt_continue_connecting)); if (result != null && result.booleanValue()) { // save this key in known database - manager.hostdb.saveKnownHost(hostname, port, serverHostKeyAlgorithm, serverHostKey); + hostDb.saveKnownHost(hostname, port, serverHostKeyAlgorithm, serverHostKey); return true; } else { return false; @@ -249,13 +250,16 @@ public class SSH extends AbsTransport implements ConnectionMonitor, InteractiveC } else { bridge.outputLine(manager.res.getString(R.string.terminal_auth_pubkey_specific)); // use a specific key for this host, as requested - PubkeyBean pubkey = manager.pubkeydb.findPubkeyById(pubkeyId); + PubkeyDatabase pubkeyDb = PubkeyDatabase.get(manager); + PubkeyBean pubkey = pubkeyDb.findPubkeyById(pubkeyId); - if (pubkey == null) + if (pubkey == null) { bridge.outputLine(manager.res.getString(R.string.terminal_auth_pubkey_invalid)); - else - if (tryPublicKey(pubkey)) + } else { + if (tryPublicKey(pubkey)) { finishConnection(); + } + } } pubkeysExhausted = true; @@ -293,7 +297,7 @@ public class SSH extends AbsTransport implements ConnectionMonitor, InteractiveC /** * Attempt connection with database row pointed to by cursor. - * @param cursor + * @param pubkey * @return true for successful authentication * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException diff --git a/app/src/main/java/org/connectbot/util/HostDatabase.java b/app/src/main/java/org/connectbot/util/HostDatabase.java index 067dc80..83fc6be 100644 --- a/app/src/main/java/org/connectbot/util/HostDatabase.java +++ b/app/src/main/java/org/connectbot/util/HostDatabase.java @@ -702,8 +702,6 @@ public class HostDatabase extends RobustSQLiteOpenHelper { } public void setColorForScheme(int scheme, int number, int value) { - final SQLiteDatabase db; - final String[] whereArgs = new String[] { String.valueOf(scheme), String.valueOf(number) }; if (value == Colors.defaults[number]) { -- cgit v1.2.3