From 8a9540082db537752dde396c8e163cd1a9b503ef Mon Sep 17 00:00:00 2001 From: Kenny Root Date: Fri, 27 Mar 2015 20:04:13 +0000 Subject: Use a sane default font size This will also try to scale existing host entries down by the appropriate amount. Fixes #44 --- .../service/FontSizeChangedListener.java | 6 +- src/org/connectbot/service/TerminalBridge.java | 86 +++++++++++++--------- src/org/connectbot/util/HostDatabase.java | 40 +++++----- 3 files changed, 74 insertions(+), 58 deletions(-) diff --git a/src/org/connectbot/service/FontSizeChangedListener.java b/src/org/connectbot/service/FontSizeChangedListener.java index eb1c33d..56b9971 100644 --- a/src/org/connectbot/service/FontSizeChangedListener.java +++ b/src/org/connectbot/service/FontSizeChangedListener.java @@ -24,8 +24,8 @@ package org.connectbot.service; public interface FontSizeChangedListener { /** - * @param size - * new font size + * @param sizeDp + * new font size in dp */ - void onFontSizeChanged(float size); + void onFontSizeChanged(float sizeDp); } diff --git a/src/org/connectbot/service/TerminalBridge.java b/src/org/connectbot/service/TerminalBridge.java index e5a2e97..2065a97 100644 --- a/src/org/connectbot/service/TerminalBridge.java +++ b/src/org/connectbot/service/TerminalBridge.java @@ -61,8 +61,9 @@ import de.mud.terminal.vt320; public class TerminalBridge implements VDUDisplay { public final static String TAG = "ConnectBot.TerminalBridge"; - public final static int DEFAULT_FONT_SIZE = 10; + public final static int DEFAULT_FONT_SIZE_DP = 10; private final static int FONT_SIZE_STEP = 2; + private final float displayDensity; public Integer[] color; @@ -107,7 +108,7 @@ public class TerminalBridge implements VDUDisplay { public int charHeight = -1; private int charTop = -1; - private float fontSize = -1; + private float fontSizeDp = -1; private final List fontSizeChangedListeners; @@ -143,6 +144,8 @@ public class TerminalBridge implements VDUDisplay { emulation = null; manager = null; + displayDensity = 1f; + defaultPaint = new Paint(); selectionArea = new SelectionArea(); @@ -169,6 +172,8 @@ public class TerminalBridge implements VDUDisplay { emulation = manager.getEmulation(); scrollback = manager.getScrollback(); + this.displayDensity = manager.getResources().getDisplayMetrics().density; + // create prompt helper to relay password and hostkey requests up to gui promptHelper = new PromptHelper(this); @@ -182,10 +187,11 @@ public class TerminalBridge implements VDUDisplay { fontSizeChangedListeners = new LinkedList(); - int hostFontSize = host.getFontSize(); - if (hostFontSize <= 0) - hostFontSize = DEFAULT_FONT_SIZE; - setFontSize(hostFontSize); + int hostFontSizeDp = host.getFontSize(); + if (hostFontSizeDp <= 0) { + hostFontSizeDp = DEFAULT_FONT_SIZE_DP; + } + setFontSize(hostFontSizeDp); // create terminal buffer and handle outgoing data // this is probably status reply information @@ -384,7 +390,7 @@ public class TerminalBridge implements VDUDisplay { relayThread.start(); // force font-size to make sure we resizePTY as needed - setFontSize(fontSize); + setFontSize(fontSizeDp); // finally send any post-login string, if requested injectString(host.getPostLogin()); @@ -480,13 +486,18 @@ public class TerminalBridge implements VDUDisplay { /** * Request a different font size. Will make call to parentChanged() to make * sure we resize PTY if needed. + * + * @param sizeDp Size of font in dp */ - /* package */ final void setFontSize(float size) { - if (size <= 0.0) + /* package */ final void setFontSize(float sizeDp) { + if (sizeDp <= 0.0) { return; + } + + final int fontSizePx = (int) (sizeDp * this.displayDensity + 0.5f); - defaultPaint.setTextSize(size); - fontSize = size; + defaultPaint.setTextSize(fontSizePx); + fontSizeDp = sizeDp; // read new metrics to get exact pixel dimensions FontMetrics fm = defaultPaint.getFontMetrics(); @@ -498,13 +509,15 @@ public class TerminalBridge implements VDUDisplay { charHeight = (int)Math.ceil(fm.descent - fm.top); // refresh any bitmap with new font size - if(parent != null) + if (parent != null) { parentChanged(parent); + } - for (FontSizeChangedListener ofscl : fontSizeChangedListeners) - ofscl.onFontSizeChanged(size); + for (FontSizeChangedListener ofscl : fontSizeChangedListeners) { + ofscl.onFontSizeChanged(sizeDp); + } - host.setFontSize((int) fontSize); + host.setFontSize((int) sizeDp); manager.hostdb.updateFontSize(host); forcedSize = false; @@ -771,51 +784,52 @@ public class TerminalBridge implements VDUDisplay { /** * Resize terminal to fit [rows]x[cols] in screen of size [width]x[height] - * @param rows - * @param cols - * @param width - * @param height + * + * @param rows desired number of text rows + * @param cols desired numbor of text colums + * @param width width of screen in pixels + * @param height height of screen in pixels */ public synchronized void resizeComputed(int cols, int rows, int width, int height) { - float size = 8.0f; + float sizeDp = 8.0f; float step = 8.0f; float limit = 0.125f; int direction; - while ((direction = fontSizeCompare(size, cols, rows, width, height)) < 0) - size += step; + while ((direction = fontSizeCompare(sizeDp, cols, rows, width, height)) < 0) + sizeDp += step; if (direction == 0) { - Log.d("fontsize", String.format("Found match at %f", size)); + Log.d("fontsize", String.format("Found match at %f", sizeDp)); return; } step /= 2.0f; - size -= step; + sizeDp -= step; - while ((direction = fontSizeCompare(size, cols, rows, width, height)) != 0 + while ((direction = fontSizeCompare(sizeDp, cols, rows, width, height)) != 0 && step >= limit) { step /= 2.0f; if (direction > 0) { - size -= step; + sizeDp -= step; } else { - size += step; + sizeDp += step; } } if (direction > 0) - size -= step; + sizeDp -= step; this.columns = cols; this.rows = rows; - setFontSize(size); + setFontSize(sizeDp); forcedSize = true; } - private int fontSizeCompare(float size, int cols, int rows, int width, int height) { + private int fontSizeCompare(float sizeDp, int cols, int rows, int width, int height) { // read new metrics to get exact pixel dimensions - defaultPaint.setTextSize(size); + defaultPaint.setTextSize((int) (sizeDp * this.displayDensity + 0.5f)); FontMetrics fm = defaultPaint.getFontMetrics(); float[] widths = new float[1]; @@ -823,7 +837,7 @@ public class TerminalBridge implements VDUDisplay { int termWidth = (int)widths[0] * cols; int termHeight = (int)Math.ceil(fm.descent - fm.top) * rows; - Log.d("fontsize", String.format("font size %f resulted in %d x %d", size, termWidth, termHeight)); + Log.d("fontsize", String.format("font size %fdp resulted in %d x %d", sizeDp, termWidth, termHeight)); // Check to see if it fits in resolution specified. if (termWidth > width || termHeight > height) @@ -1003,16 +1017,16 @@ public class TerminalBridge implements VDUDisplay { } /** - * + * Convenience function to increase the font size by a given step. */ public void increaseFontSize() { - setFontSize(fontSize + FONT_SIZE_STEP); + setFontSize(fontSizeDp + FONT_SIZE_STEP); } /** - * + * Convenience function to decrease the font size by a given step. */ public void decreaseFontSize() { - setFontSize(fontSize - FONT_SIZE_STEP); + setFontSize(fontSizeDp - FONT_SIZE_STEP); } } diff --git a/src/org/connectbot/util/HostDatabase.java b/src/org/connectbot/util/HostDatabase.java index ac35186..c6707ae 100644 --- a/src/org/connectbot/util/HostDatabase.java +++ b/src/org/connectbot/util/HostDatabase.java @@ -47,7 +47,7 @@ public class HostDatabase extends RobustSQLiteOpenHelper { public final static String TAG = "ConnectBot.HostDatabase"; public final static String DB_NAME = "hosts"; - public final static int DB_VERSION = 23; + public final static int DB_VERSION = 24; public final static String TABLE_HOSTS = "hosts"; public final static String FIELD_HOST_NICKNAME = "nickname"; @@ -140,9 +140,14 @@ public class HostDatabase extends RobustSQLiteOpenHelper { public static final Object[] dbLock = new Object[0]; + /** Used during upgrades from DB version 23 to 24. */ + private final float displayDensity; + public HostDatabase(Context context) { super(context, DB_NAME, null, DB_VERSION); + this.displayDensity = context.getResources().getDisplayMetrics().density; + getWritableDatabase().close(); } @@ -264,12 +269,15 @@ public class HostDatabase extends RobustSQLiteOpenHelper { case 22: db.execSQL("ALTER TABLE " + TABLE_HOSTS + " ADD COLUMN " + FIELD_HOST_QUICKDISCONNECT + " TEXT DEFAULT '" + Boolean.toString(false) + "'"); + case 23: + db.execSQL("UPDATE " + TABLE_HOSTS + + " SET " + FIELD_HOST_FONTSIZE + " = " + FIELD_HOST_FONTSIZE + " / " + displayDensity); } } /** * Touch a specific host to update its "last connected" field. - * @param nickname Nickname field of host to update + * @param host host to update */ public void touchHost(HostBean host) { long now = System.currentTimeMillis() / 1000; @@ -358,8 +366,7 @@ public class HostDatabase extends RobustSQLiteOpenHelper { } /** - * @param hosts - * @param c + * @param c cursor to read from */ private List createHostBeans(Cursor c) { List hosts = new LinkedList(); @@ -414,8 +421,8 @@ public class HostDatabase extends RobustSQLiteOpenHelper { } /** - * @param c - * @return + * @param c cursor with zero or more hosts + * @return the first host from the cursor or {@code null} if none. */ private HostBean getFirstHostBean(Cursor c) { HostBean host = null; @@ -430,13 +437,8 @@ public class HostDatabase extends RobustSQLiteOpenHelper { } /** - * @param nickname - * @param protocol - * @param username - * @param hostname - * @param hostname2 - * @param port - * @return + * @param selection parameters describing the desired host + * @return host matching selection or {@code null}. */ public HostBean findHost(Map selection) { StringBuilder selectionBuilder = new StringBuilder(); @@ -481,8 +483,8 @@ public class HostDatabase extends RobustSQLiteOpenHelper { } /** - * @param hostId - * @return + * @param hostId host id for the host + * @return host matching the hostId or {@code null} if none match */ public HostBean findHostById(long hostId) { HostBean host; @@ -502,10 +504,10 @@ public class HostDatabase extends RobustSQLiteOpenHelper { /** * Record the given hostkey into database under this nickname. - * @param hostname - * @param port - * @param hostkeyalgo - * @param hostkey + * @param hostname hostname to match + * @param port port to match + * @param hostkeyalgo algorithm for host key + * @param hostkey the bytes of the host key itself */ public void saveKnownHost(String hostname, int port, String hostkeyalgo, byte[] hostkey) { ContentValues values = new ContentValues(); -- cgit v1.2.3