Fix problem with font and color loading at startup

Using View#post() does not work in onCreate().
This commit is contained in:
Fredrik Fornwall
2015-12-28 20:47:34 +01:00
parent 96f5ed985a
commit 50005bc794
2 changed files with 37 additions and 76 deletions

View File

@@ -136,9 +136,8 @@ public final class TermuxActivity extends Activity implements ServiceConnection
if (ensureStoragePermissionGranted()) TermuxInstaller.setupStorageSymlinks(TermuxActivity.this); if (ensureStoragePermissionGranted()) TermuxInstaller.setupStorageSymlinks(TermuxActivity.this);
return; return;
} }
if (whatToReload == null || "colors".equals(whatToReload)) mTerminalView.checkForColors(); mTerminalView.checkForFontAndColors();
if (whatToReload == null || "font".equals(whatToReload)) mTerminalView.checkForTypeface(); mSettings.reloadFromProperties(TermuxActivity.this);
if (whatToReload == null || "settings".equals(whatToReload)) 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 @Override
public void onClick(View v) { public void onClick(View v) {
addNewSession(false, null); addNewSession(false, null);
} }
}); });
findViewById(R.id.new_session_button).setOnLongClickListener(new OnLongClickListener() { newSessionButton.setOnLongClickListener(new OnLongClickListener() {
@Override @Override
public boolean onLongClick(View v) { public boolean onLongClick(View v) {
Resources res = getResources(); Resources res = getResources();
@@ -336,8 +337,7 @@ public final class TermuxActivity extends Activity implements ServiceConnection
startService(serviceIntent); startService(serviceIntent);
if (!bindService(serviceIntent, this, 0)) throw new RuntimeException("bindService() failed"); if (!bindService(serviceIntent, this, 0)) throw new RuntimeException("bindService() failed");
mTerminalView.checkForTypeface(); mTerminalView.checkForFontAndColors();
mTerminalView.checkForColors();
mBellSoundId = mBellSoundPool.load(this, R.raw.bell, 1); mBellSoundId = mBellSoundPool.load(this, R.raw.bell, 1);
} }

View File

@@ -1,16 +1,5 @@
package com.termux.view; 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.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.graphics.Canvas; import android.graphics.Canvas;
@@ -30,6 +19,17 @@ import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnection;
import android.widget.Scroller; 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}. */ /** View displaying and interacting with a {@link TerminalSession}. */
public final class TerminalView extends View { public final class TerminalView extends View {
@@ -710,67 +710,28 @@ public final class TerminalView extends View {
return false; return false;
} }
public void checkForTypeface() { public void checkForFontAndColors() {
new Thread() { try {
@Override File fontFile = new File("/data/data/com.termux/files/home/.termux/font.ttf");
public void run() { File colorsFile = new File("/data/data/com.termux/files/home/.termux/colors.properties");
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 checkForColors() { final Properties props = new Properties();
new Thread() { if (colorsFile.isFile()) {
@Override try (InputStream in = new FileInputStream(colorsFile)) {
public void run() { props.load(in);
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);
} }
} }
}.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);
}
} }
/** /**