2017-07-01 00:09:23 +08:00
|
|
|
package io.neoterm.utils
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import io.neoterm.R
|
|
|
|
import io.neoterm.backend.TerminalSession
|
2017-07-02 23:07:56 +08:00
|
|
|
import io.neoterm.customize.font.FontManager
|
2017-07-01 00:09:23 +08:00
|
|
|
import io.neoterm.preference.NeoPreference
|
2017-07-18 23:55:01 +08:00
|
|
|
import io.neoterm.preference.NeoTermPath
|
2017-07-21 00:42:30 +08:00
|
|
|
import io.neoterm.terminal.ShellTermSession
|
2017-07-03 17:05:42 +08:00
|
|
|
import io.neoterm.view.ExtraKeysView
|
2017-07-21 00:42:30 +08:00
|
|
|
import io.neoterm.view.TerminalDialog
|
2017-07-02 23:07:56 +08:00
|
|
|
import io.neoterm.view.TerminalView
|
2017-07-21 00:42:30 +08:00
|
|
|
import io.neoterm.view.TerminalViewClient
|
2017-07-01 00:09:23 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author kiva
|
|
|
|
*/
|
|
|
|
object TerminalUtils {
|
2017-07-21 00:42:30 +08:00
|
|
|
fun setupTerminalView(terminalView: TerminalView?, terminalViewClient: TerminalViewClient? = null) {
|
2017-07-03 17:05:42 +08:00
|
|
|
terminalView?.textSize = NeoPreference.loadInt(NeoPreference.KEY_FONT_SIZE, 30)
|
|
|
|
terminalView?.setTypeface(FontManager.getCurrentFont().getTypeFace())
|
2017-07-02 23:07:56 +08:00
|
|
|
if (terminalViewClient != null) {
|
2017-07-18 00:08:08 +08:00
|
|
|
terminalView?.setTerminalViewClient(terminalViewClient)
|
2017-07-02 23:07:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-03 17:05:42 +08:00
|
|
|
fun setupExtraKeysView(extraKeysView: ExtraKeysView?) {
|
|
|
|
extraKeysView?.setTypeface(FontManager.getCurrentFont().getTypeFace())
|
|
|
|
}
|
|
|
|
|
|
|
|
fun setupTerminalSession(session: TerminalSession?) {
|
|
|
|
}
|
|
|
|
|
2017-07-21 00:42:30 +08:00
|
|
|
fun createShellSession(context: Context, executablePath: String?, arguments: Array<String>?,
|
|
|
|
cwd: String?, initialCommand: String?, env: Array<Pair<String, String>>?,
|
|
|
|
sessionCallback: TerminalSession.SessionChangedCallback?,
|
|
|
|
systemShell: Boolean): TerminalSession {
|
|
|
|
val initCommand = initialCommand ?:
|
|
|
|
NeoPreference.loadString(R.string.key_general_initial_command, "")
|
2017-07-18 23:55:01 +08:00
|
|
|
|
2017-07-21 00:42:30 +08:00
|
|
|
val session = ShellTermSession.Builder()
|
|
|
|
.shell(executablePath)
|
|
|
|
.currentWorkingDirectory(cwd)
|
|
|
|
.callback(sessionCallback)
|
|
|
|
.systemShell(systemShell)
|
|
|
|
.envArray(env)
|
|
|
|
.argArray(arguments)
|
|
|
|
.create(context)
|
2017-07-03 17:05:42 +08:00
|
|
|
setupTerminalSession(session)
|
2017-07-21 00:42:30 +08:00
|
|
|
session.initialCommand = initCommand
|
2017-07-01 00:09:23 +08:00
|
|
|
return session
|
|
|
|
}
|
2017-07-18 23:55:01 +08:00
|
|
|
|
|
|
|
fun escapeString(s: String?): String {
|
|
|
|
if (s == null) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
val builder = StringBuilder()
|
|
|
|
val specialChars = "\"\\$`!"
|
|
|
|
builder.append('"')
|
|
|
|
val length = s.length
|
|
|
|
for (i in 0..length - 1) {
|
|
|
|
val c = s[i]
|
|
|
|
if (specialChars.indexOf(c) >= 0) {
|
|
|
|
builder.append('\\')
|
|
|
|
}
|
|
|
|
builder.append(c)
|
|
|
|
}
|
|
|
|
builder.append('"')
|
|
|
|
return builder.toString()
|
|
|
|
}
|
2017-07-21 00:42:30 +08:00
|
|
|
|
|
|
|
fun executeApt(context: Context, subCommand: String, callback: (Int, TerminalDialog) -> Unit) {
|
|
|
|
TerminalDialog(context)
|
|
|
|
.onFinish(object : TerminalDialog.SessionFinishedCallback {
|
|
|
|
override fun onSessionFinished(dialog: TerminalDialog, finishedSession: TerminalSession?) {
|
|
|
|
val exit = finishedSession?.exitStatus ?: 1
|
|
|
|
callback(exit, dialog)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.imeEnabled(true)
|
|
|
|
.execute(NeoTermPath.APT_BIN_PATH, arrayOf("apt", subCommand))
|
|
|
|
.show("apt $subCommand")
|
|
|
|
}
|
2017-07-01 00:09:23 +08:00
|
|
|
}
|