Profile: Use DefaultPreference instead of hardcoded values

This commit is contained in:
zt515 2017-12-05 00:07:35 +08:00
parent 119632918e
commit 68e6dbfd3b
10 changed files with 55 additions and 28 deletions

View File

@ -8,6 +8,7 @@ import io.neoterm.R
import io.neoterm.frontend.preference.NeoPreference import io.neoterm.frontend.preference.NeoPreference
import io.neoterm.frontend.preference.NeoTermPath import io.neoterm.frontend.preference.NeoTermPath
import io.neoterm.frontend.component.NeoComponent import io.neoterm.frontend.component.NeoComponent
import io.neoterm.frontend.preference.DefaultPreference
import io.neoterm.frontend.terminal.TerminalView import io.neoterm.frontend.terminal.TerminalView
import io.neoterm.frontend.terminal.eks.ExtraKeysView import io.neoterm.frontend.terminal.eks.ExtraKeysView
import io.neoterm.utils.AssetsUtils import io.neoterm.utils.AssetsUtils
@ -17,8 +18,6 @@ import java.io.File
* @author kiva * @author kiva
*/ */
class FontComponent : NeoComponent { class FontComponent : NeoComponent {
private val DEFAULT_FONT_NAME = "SourceCodePro"
private lateinit var DEFAULT_FONT: NeoFont private lateinit var DEFAULT_FONT: NeoFont
private lateinit var fonts: MutableMap<String, NeoFont> private lateinit var fonts: MutableMap<String, NeoFont>
@ -35,10 +34,11 @@ class FontComponent : NeoComponent {
} }
fun getCurrentFontName(): String { fun getCurrentFontName(): String {
var currentFontName = NeoPreference.loadString(R.string.key_customization_font, DEFAULT_FONT_NAME) val defaultFont = DefaultPreference.defaultFont
var currentFontName = NeoPreference.loadString(R.string.key_customization_font, defaultFont)
if (!fonts.containsKey(currentFontName)) { if (!fonts.containsKey(currentFontName)) {
currentFontName = DEFAULT_FONT_NAME currentFontName = defaultFont
NeoPreference.store(R.string.key_customization_font, DEFAULT_FONT_NAME) NeoPreference.store(R.string.key_customization_font, defaultFont)
} }
return currentFontName return currentFontName
} }
@ -64,8 +64,10 @@ class FontComponent : NeoComponent {
val font = NeoFont(file) val font = NeoFont(file)
fonts.put(fontName, font) fonts.put(fontName, font)
} }
if (fonts.containsKey(DEFAULT_FONT_NAME)) {
DEFAULT_FONT = fonts[DEFAULT_FONT_NAME]!! val defaultFont = DefaultPreference.defaultFont
if (fonts.containsKey(defaultFont)) {
DEFAULT_FONT = fonts[defaultFont]!!
return true return true
} }
return false return false
@ -83,7 +85,8 @@ class FontComponent : NeoComponent {
} }
private fun loadDefaultFontFromAsset(context: Context): NeoFont { private fun loadDefaultFontFromAsset(context: Context): NeoFont {
return NeoFont(Typeface.createFromAsset(context.assets, "fonts/$DEFAULT_FONT_NAME.ttf")) val defaultFont = DefaultPreference.defaultFont
return NeoFont(Typeface.createFromAsset(context.assets, "fonts/$defaultFont.ttf"))
} }
private fun extractDefaultFont(context: Context): Boolean { private fun extractDefaultFont(context: Context): Boolean {
@ -108,18 +111,20 @@ class FontComponent : NeoComponent {
fonts = mutableMapOf() fonts = mutableMapOf()
val context = App.get() val context = App.get()
val defaultFontFile = fontFile(DEFAULT_FONT_NAME) val defaultFont = DefaultPreference.defaultFont
val defaultFontFile = fontFile(defaultFont)
if (!defaultFontFile.exists()) { if (!defaultFontFile.exists()) {
if (!extractDefaultFont(context)) { if (!extractDefaultFont(context)) {
DEFAULT_FONT = loadDefaultFontFromAsset(context) DEFAULT_FONT = loadDefaultFontFromAsset(context)
fonts.put(DEFAULT_FONT_NAME, DEFAULT_FONT) fonts.put(defaultFont, DEFAULT_FONT)
return return
} }
} }
if (!reloadFonts()) { if (!reloadFonts()) {
DEFAULT_FONT = loadDefaultFontFromAsset(context) DEFAULT_FONT = loadDefaultFontFromAsset(context)
fonts.put(DEFAULT_FONT_NAME, DEFAULT_FONT) fonts.put(defaultFont, DEFAULT_FONT)
} }
} }
} }

View File

@ -8,6 +8,7 @@ import io.neoterm.NeoXorgSettings
import io.neoterm.R import io.neoterm.R
import io.neoterm.frontend.component.NeoComponent import io.neoterm.frontend.component.NeoComponent
import io.neoterm.frontend.logging.NLog import io.neoterm.frontend.logging.NLog
import io.neoterm.frontend.preference.DefaultPreference
import io.neoterm.frontend.preference.NeoPreference import io.neoterm.frontend.preference.NeoPreference
import io.neoterm.frontend.preference.NeoTermPath import io.neoterm.frontend.preference.NeoTermPath
import io.neoterm.frontend.session.shell.ShellParameter import io.neoterm.frontend.session.shell.ShellParameter
@ -101,7 +102,8 @@ class SessionComponent : NeoComponent {
fun createSession(context: Context, parameter: ShellParameter): ShellTermSession { fun createSession(context: Context, parameter: ShellParameter): ShellTermSession {
val initCommand = parameter.initialCommand ?: val initCommand = parameter.initialCommand ?:
NeoPreference.loadString(R.string.key_general_initial_command, "") NeoPreference.loadString(R.string.key_general_initial_command,
DefaultPreference.initialCommand)
val session = ShellTermSession.Builder() val session = ShellTermSession.Builder()
.executablePath(parameter.executablePath) .executablePath(parameter.executablePath)

View File

@ -1,9 +1,13 @@
package io.neoterm.frontend.preference package io.neoterm.frontend.preference
import io.neoterm.component.color.DefaultColorScheme
/** /**
* @author kiva * @author kiva
*/ */
object DefaultPreference { object DefaultPreference {
const val fontSize = 30
const val enableBell = false const val enableBell = false
const val enableVibrate = false const val enableVibrate = false
const val enableExecveWrapper = true const val enableExecveWrapper = true
@ -13,8 +17,10 @@ object DefaultPreference {
const val enableSwitchNextTab = false const val enableSwitchNextTab = false
const val enableExtraKeys = true const val enableExtraKeys = true
const val enableExplicitExtraKeysWeight = false const val enableExplicitExtraKeysWeight = false
const val enableBackButtonBeMappedToEscape = false
const val enableSpecialVolumeKeys = false const val enableSpecialVolumeKeys = false
const val loginShell = "bash" const val loginShell = "sh"
const val initialCommand = "" const val initialCommand = ""
const val defaultFont = "SourceCodePro"
} }

View File

@ -138,7 +138,7 @@ object NeoPreference {
} }
fun getLoginShell(): String { fun getLoginShell(): String {
val loginProgramName = loadString(R.string.key_general_shell, "sh") val loginProgramName = loadString(R.string.key_general_shell, DefaultPreference.loginShell)
// Some programs like ssh needs it // Some programs like ssh needs it
val shell = File(NeoTermPath.NEOTERM_SHELL_PATH) val shell = File(NeoTermPath.NEOTERM_SHELL_PATH)
@ -179,7 +179,7 @@ object NeoPreference {
} }
fun getFontSize(): Int { fun getFontSize(): Int {
return loadInt(NeoPreference.KEY_FONT_SIZE, 30) return loadInt(NeoPreference.KEY_FONT_SIZE, DefaultPreference.fontSize)
} }

View File

@ -4,6 +4,7 @@ import android.content.Context
import io.neoterm.App import io.neoterm.App
import io.neoterm.R import io.neoterm.R
import io.neoterm.backend.TerminalSession import io.neoterm.backend.TerminalSession
import io.neoterm.frontend.preference.DefaultPreference
import io.neoterm.frontend.session.shell.client.TermSessionCallback import io.neoterm.frontend.session.shell.client.TermSessionCallback
import io.neoterm.frontend.preference.NeoPreference import io.neoterm.frontend.preference.NeoPreference
import io.neoterm.frontend.preference.NeoTermPath import io.neoterm.frontend.preference.NeoTermPath
@ -183,7 +184,8 @@ open class ShellTermSession private constructor(shellPath: String, cwd: String,
var ldPreloadEnv = "" var ldPreloadEnv = ""
// execve(2) wrapper to avoid incorrect shebang // execve(2) wrapper to avoid incorrect shebang
if (NeoPreference.loadBoolean(R.string.key_general_use_execve_wrapper, true)) { if (NeoPreference.loadBoolean(R.string.key_general_use_execve_wrapper,
DefaultPreference.enableExecveWrapper)) {
ldPreloadEnv = "LD_PRELOAD=${App.get().applicationInfo.nativeLibraryDir}/libnexec.so" ldPreloadEnv = "LD_PRELOAD=${App.get().applicationInfo.nativeLibraryDir}/libnexec.so"
} }

View File

@ -7,6 +7,7 @@ import android.media.SoundPool
import android.os.Vibrator import android.os.Vibrator
import io.neoterm.R import io.neoterm.R
import io.neoterm.backend.TerminalSession import io.neoterm.backend.TerminalSession
import io.neoterm.frontend.preference.DefaultPreference
import io.neoterm.frontend.preference.NeoPreference import io.neoterm.frontend.preference.NeoPreference
/** /**
@ -43,7 +44,7 @@ class TermSessionCallback : TerminalSession.SessionChangedCallback {
override fun onBell(session: TerminalSession?) { override fun onBell(session: TerminalSession?) {
val termView = termData?.termView ?: return val termView = termData?.termView ?: return
if (NeoPreference.loadBoolean(R.string.key_general_bell, false)) { if (NeoPreference.loadBoolean(R.string.key_general_bell, DefaultPreference.enableBell)) {
if (soundPool == null) { if (soundPool == null) {
soundPool = SoundPool.Builder().setMaxStreams(1).build() soundPool = SoundPool.Builder().setMaxStreams(1).build()
bellId = soundPool!!.load(termView.context, R.raw.bell, 1) bellId = soundPool!!.load(termView.context, R.raw.bell, 1)
@ -51,7 +52,7 @@ class TermSessionCallback : TerminalSession.SessionChangedCallback {
soundPool?.play(bellId, 1f, 1f, 0, 0, 1f) soundPool?.play(bellId, 1f, 1f, 0, 0, 1f)
} }
if (NeoPreference.loadBoolean(R.string.key_general_vibrate, false)) { if (NeoPreference.loadBoolean(R.string.key_general_vibrate, DefaultPreference.enableVibrate)) {
val vibrator = termView.context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator val vibrator = termView.context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
vibrator.vibrate(100) vibrator.vibrate(100)
} }

View File

@ -12,6 +12,7 @@ import io.neoterm.backend.KeyHandler
import io.neoterm.backend.TerminalSession import io.neoterm.backend.TerminalSession
import io.neoterm.component.eks.ExtraKeysComponent import io.neoterm.component.eks.ExtraKeysComponent
import io.neoterm.frontend.component.ComponentManager import io.neoterm.frontend.component.ComponentManager
import io.neoterm.frontend.preference.DefaultPreference
import io.neoterm.frontend.preference.NeoPreference import io.neoterm.frontend.preference.NeoPreference
import io.neoterm.frontend.terminal.TerminalViewClient import io.neoterm.frontend.terminal.TerminalViewClient
@ -44,7 +45,8 @@ class TermViewClient(val context: Context) : TerminalViewClient {
} }
override fun shouldBackButtonBeMappedToEscape(): Boolean { override fun shouldBackButtonBeMappedToEscape(): Boolean {
return NeoPreference.loadBoolean(R.string.key_generaL_backspace_map_to_esc, false) return NeoPreference.loadBoolean(R.string.key_generaL_backspace_map_to_esc,
DefaultPreference.enableBackButtonBeMappedToEscape)
} }
override fun copyModeChanged(copyMode: Boolean) { override fun copyModeChanged(copyMode: Boolean) {
@ -196,7 +198,7 @@ class TermViewClient(val context: Context) : TerminalViewClient {
} }
// Volume keys as special keys // Volume keys as special keys
val volumeAsSpecialKeys = NeoPreference.loadBoolean(R.string.key_general_volume_as_control, val volumeAsSpecialKeys = NeoPreference.loadBoolean(R.string.key_general_volume_as_control,
false) DefaultPreference.enableSpecialVolumeKeys)
val inputDevice = event.device val inputDevice = event.device
if (inputDevice != null && inputDevice.keyboardType == InputDevice.KEYBOARD_TYPE_ALPHABETIC) { if (inputDevice != null && inputDevice.keyboardType == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
@ -230,7 +232,8 @@ class TermViewClient(val context: Context) : TerminalViewClient {
private fun updateExtraKeysVisibility(): Boolean { private fun updateExtraKeysVisibility(): Boolean {
val extraKeysView = termData?.extraKeysView ?: return false val extraKeysView = termData?.extraKeysView ?: return false
return if (NeoPreference.loadBoolean(R.string.key_ui_eks_enabled, true)) { return if (NeoPreference.loadBoolean(R.string.key_ui_eks_enabled,
DefaultPreference.enableExtraKeys)) {
extraKeysView.visibility = View.VISIBLE extraKeysView.visibility = View.VISIBLE
true true
} else { } else {

View File

@ -8,6 +8,7 @@ import android.widget.GridLayout
import android.widget.LinearLayout import android.widget.LinearLayout
import io.neoterm.R import io.neoterm.R
import io.neoterm.component.eks.NeoExtraKey import io.neoterm.component.eks.NeoExtraKey
import io.neoterm.frontend.preference.DefaultPreference
import io.neoterm.frontend.preference.NeoPreference import io.neoterm.frontend.preference.NeoPreference
import io.neoterm.frontend.preference.NeoTermPath import io.neoterm.frontend.preference.NeoTermPath
import io.neoterm.frontend.session.shell.client.event.ToggleImeEvent import io.neoterm.frontend.session.shell.client.event.ToggleImeEvent
@ -161,7 +162,8 @@ class ExtraKeysView(context: Context, attrs: AttributeSet) : LinearLayout(contex
val line = LinearLayout(context) val line = LinearLayout(context)
val layoutParams = val layoutParams =
if (NeoPreference.loadBoolean(R.string.key_ui_eks_weight_explicit, false)) if (NeoPreference.loadBoolean(R.string.key_ui_eks_weight_explicit,
DefaultPreference.enableExplicitExtraKeysWeight))
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1f) LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1f)
else else
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,

View File

@ -5,6 +5,7 @@ import android.os.Bundle
import android.view.MenuItem import android.view.MenuItem
import io.neoterm.R import io.neoterm.R
import io.neoterm.frontend.logging.NLog import io.neoterm.frontend.logging.NLog
import io.neoterm.frontend.preference.DefaultPreference
import io.neoterm.frontend.preference.NeoPreference import io.neoterm.frontend.preference.NeoPreference
import io.neoterm.utils.PackageUtils import io.neoterm.utils.PackageUtils
@ -19,7 +20,7 @@ class GeneralSettingsActivity : BasePreferenceActivity() {
supportActionBar?.setDisplayHomeAsUpEnabled(true) supportActionBar?.setDisplayHomeAsUpEnabled(true)
addPreferencesFromResource(R.xml.setting_general) addPreferencesFromResource(R.xml.setting_general)
val currentShell = NeoPreference.loadString(R.string.key_general_shell, "sh") val currentShell = NeoPreference.loadString(R.string.key_general_shell, DefaultPreference.loginShell)
findPreference(getString(R.string.key_general_shell)).setOnPreferenceChangeListener { _, value -> findPreference(getString(R.string.key_general_shell)).setOnPreferenceChangeListener { _, value ->
val shellName = value.toString() val shellName = value.toString()
val newShell = NeoPreference.findLoginProgram(shellName) val newShell = NeoPreference.findLoginProgram(shellName)

View File

@ -21,6 +21,7 @@ import io.neoterm.App
import io.neoterm.R import io.neoterm.R
import io.neoterm.backend.TerminalSession import io.neoterm.backend.TerminalSession
import io.neoterm.component.setup.BaseFileInstaller import io.neoterm.component.setup.BaseFileInstaller
import io.neoterm.frontend.preference.DefaultPreference
import io.neoterm.frontend.session.shell.client.TermSessionCallback import io.neoterm.frontend.session.shell.client.TermSessionCallback
import io.neoterm.frontend.session.shell.client.TermViewClient import io.neoterm.frontend.session.shell.client.TermViewClient
import io.neoterm.frontend.session.shell.client.event.* import io.neoterm.frontend.session.shell.client.event.*
@ -62,7 +63,8 @@ class NeoTermActivity : AppCompatActivity(), ServiceConnection, SharedPreference
NeoPermission.initAppPermission(this, NeoPermission.REQUEST_APP_PERMISSION) NeoPermission.initAppPermission(this, NeoPermission.REQUEST_APP_PERMISSION)
val fullscreen = NeoPreference.loadBoolean(R.string.key_ui_fullscreen, false) val fullscreen = NeoPreference.loadBoolean(R.string.key_ui_fullscreen,
DefaultPreference.enableFullScreen)
if (fullscreen) { if (fullscreen) {
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN) WindowManager.LayoutParams.FLAG_FULLSCREEN)
@ -99,8 +101,10 @@ class NeoTermActivity : AppCompatActivity(), ServiceConnection, SharedPreference
return return
} }
if (NeoPreference.loadBoolean(R.string.key_ui_fullscreen, false) if (NeoPreference.loadBoolean(R.string.key_ui_fullscreen,
|| NeoPreference.loadBoolean(R.string.key_ui_hide_toolbar, false)) { DefaultPreference.enableFullScreen)
|| NeoPreference.loadBoolean(R.string.key_ui_hide_toolbar,
DefaultPreference.enableAutoHideToolbar)) {
val toolbarHeight = toolbar.height.toFloat() val toolbarHeight = toolbar.height.toFloat()
val translationY = if (visible) 0.toFloat() else -toolbarHeight val translationY = if (visible) 0.toFloat() else -toolbarHeight
if (visible) { if (visible) {
@ -292,7 +296,7 @@ class NeoTermActivity : AppCompatActivity(), ServiceConnection, SharedPreference
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) { override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
if (key == getString(R.string.key_ui_fullscreen)) { if (key == getString(R.string.key_ui_fullscreen)) {
setFullScreenMode(NeoPreference.loadBoolean(key, false)) setFullScreenMode(NeoPreference.loadBoolean(key, DefaultPreference.enableFullScreen))
} else if (key == getString(R.string.key_customization_color_scheme)) { } else if (key == getString(R.string.key_customization_color_scheme)) {
if (tabSwitcher.count > 0) { if (tabSwitcher.count > 0) {
val tab = tabSwitcher.selectedTab val tab = tabSwitcher.selectedTab
@ -661,7 +665,8 @@ class NeoTermActivity : AppCompatActivity(), ServiceConnection, SharedPreference
if (tabSwitcher.count > 1) { if (tabSwitcher.count > 1) {
var index = tabSwitcher.indexOf(tab) var index = tabSwitcher.indexOf(tab)
if (NeoPreference.loadBoolean(R.string.key_ui_next_tab_anim, false)) { if (NeoPreference.loadBoolean(R.string.key_ui_next_tab_anim,
DefaultPreference.enableSwitchNextTab)) {
// 关闭当前窗口后,向下一个窗口切换 // 关闭当前窗口后,向下一个窗口切换
if (--index < 0) index = tabSwitcher.count - 1 if (--index < 0) index = tabSwitcher.count - 1
} else { } else {