From 50005bc79486364d58cd117865bbaf2128b47daf Mon Sep 17 00:00:00 2001 From: Fredrik Fornwall Date: Mon, 28 Dec 2015 20:47:34 +0100 Subject: [PATCH] Fix problem with font and color loading at startup Using View#post() does not work in onCreate(). --- .../java/com/termux/app/TermuxActivity.java | 14 +-- .../java/com/termux/view/TerminalView.java | 99 ++++++------------- 2 files changed, 37 insertions(+), 76 deletions(-) diff --git a/app/src/main/java/com/termux/app/TermuxActivity.java b/app/src/main/java/com/termux/app/TermuxActivity.java index 53764cdf..3f40e57b 100644 --- a/app/src/main/java/com/termux/app/TermuxActivity.java +++ b/app/src/main/java/com/termux/app/TermuxActivity.java @@ -136,9 +136,8 @@ public final class TermuxActivity extends Activity implements ServiceConnection if (ensureStoragePermissionGranted()) TermuxInstaller.setupStorageSymlinks(TermuxActivity.this); return; } - if (whatToReload == null || "colors".equals(whatToReload)) mTerminalView.checkForColors(); - if (whatToReload == null || "font".equals(whatToReload)) mTerminalView.checkForTypeface(); - if (whatToReload == null || "settings".equals(whatToReload)) mSettings.reloadFromProperties(TermuxActivity.this); + mTerminalView.checkForFontAndColors(); + mSettings.reloadFromProperties(TermuxActivity.this); } } }; @@ -281,14 +280,16 @@ public final class TermuxActivity extends Activity implements ServiceConnection } }); - findViewById(R.id.new_session_button).setOnClickListener(new OnClickListener() { + View newSessionButton = findViewById(R.id.new_session_button); + + newSessionButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { addNewSession(false, null); } }); - findViewById(R.id.new_session_button).setOnLongClickListener(new OnLongClickListener() { + newSessionButton.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { Resources res = getResources(); @@ -336,8 +337,7 @@ public final class TermuxActivity extends Activity implements ServiceConnection startService(serviceIntent); if (!bindService(serviceIntent, this, 0)) throw new RuntimeException("bindService() failed"); - mTerminalView.checkForTypeface(); - mTerminalView.checkForColors(); + mTerminalView.checkForFontAndColors(); mBellSoundId = mBellSoundPool.load(this, R.raw.bell, 1); } diff --git a/app/src/main/java/com/termux/view/TerminalView.java b/app/src/main/java/com/termux/view/TerminalView.java index 6958297e..7424426e 100644 --- a/app/src/main/java/com/termux/view/TerminalView.java +++ b/app/src/main/java/com/termux/view/TerminalView.java @@ -1,16 +1,5 @@ package com.termux.view; -import java.io.File; -import java.io.FileInputStream; -import java.io.InputStream; -import java.util.Properties; - -import com.termux.terminal.EmulatorDebug; -import com.termux.terminal.KeyHandler; -import com.termux.terminal.TerminalColors; -import com.termux.terminal.TerminalEmulator; -import com.termux.terminal.TerminalSession; - import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Canvas; @@ -30,6 +19,17 @@ import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.widget.Scroller; +import com.termux.terminal.EmulatorDebug; +import com.termux.terminal.KeyHandler; +import com.termux.terminal.TerminalColors; +import com.termux.terminal.TerminalEmulator; +import com.termux.terminal.TerminalSession; + +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.util.Properties; + /** View displaying and interacting with a {@link TerminalSession}. */ public final class TerminalView extends View { @@ -710,67 +710,28 @@ public final class TerminalView extends View { return false; } - public void checkForTypeface() { - new Thread() { - @Override - public void run() { - try { - File fontFile = new File("/data/data/com.termux/files/home/.termux/font.ttf"); - final Typeface newTypeface = fontFile.exists() ? Typeface.createFromFile(fontFile) : Typeface.MONOSPACE; - if (newTypeface != mRenderer.mTypeface) { - post(new Runnable() { - @Override - public void run() { - try { - mRenderer = new TerminalRenderer(mRenderer.mTextSize, newTypeface); - updateSize(); - invalidate(); - } catch (Exception e) { - Log.e(EmulatorDebug.LOG_TAG, "Error loading font", e); - } - } - }); - } - } catch (Exception e) { - Log.e(EmulatorDebug.LOG_TAG, "Error loading font", e); - } - } - }.start(); - } + public void checkForFontAndColors() { + try { + File fontFile = new File("/data/data/com.termux/files/home/.termux/font.ttf"); + File colorsFile = new File("/data/data/com.termux/files/home/.termux/colors.properties"); - public void checkForColors() { - new Thread() { - @Override - public void run() { - try { - File colorsFile = new File("/data/data/com.termux/files/home/.termux/colors.properties"); - final Properties props = colorsFile.isFile() ? new Properties() : null; - if (props != null) { - try (InputStream in = new FileInputStream(colorsFile)) { - props.load(in); - } - } - post(new Runnable() { - @Override - public void run() { - try { - if (props == null) { - TerminalColors.COLOR_SCHEME.reset(); - } else { - TerminalColors.COLOR_SCHEME.updateWith(props); - } - if (mEmulator != null) mEmulator.mColors.reset(); - invalidate(); - } catch (Exception e) { - Log.e(EmulatorDebug.LOG_TAG, "Setting colors failed: " + e.getMessage()); - } - } - }); - } catch (Exception e) { - Log.e(EmulatorDebug.LOG_TAG, "Failed colors handling", e); + final Properties props = new Properties(); + if (colorsFile.isFile()) { + try (InputStream in = new FileInputStream(colorsFile)) { + props.load(in); } } - }.start(); + TerminalColors.COLOR_SCHEME.updateWith(props); + if (mEmulator != null) mEmulator.mColors.reset(); + + final Typeface newTypeface = fontFile.exists() ? Typeface.createFromFile(fontFile) : Typeface.MONOSPACE; + mRenderer = new TerminalRenderer(mRenderer.mTextSize, newTypeface); + updateSize(); + + invalidate(); + } catch (Exception e) { + Log.e(EmulatorDebug.LOG_TAG, "Error loading font", e); + } } /**