1
1
package io.neoterm.ui.term
2
2
3
+ import android.app.Activity
3
4
import android.content.ComponentName
4
5
import android.content.Intent
5
6
import android.content.ServiceConnection
@@ -83,10 +84,16 @@ class NeoTermRemoteInterface : AppCompatActivity(), ServiceConnection {
83
84
return
84
85
}
85
86
val command = intent.getStringExtra(EXTRA_COMMAND )
86
- openTerm(command)
87
+ val foreground = intent.getBooleanExtra(EXTRA_FOREGROUND , true )
88
+ val session = if (intent.hasExtra(EXTRA_SESSION_ID )) {
89
+ intent.getStringExtra(EXTRA_SESSION_ID )
90
+ } else {
91
+ null
92
+ }
93
+ openTerm(command, session, foreground)
87
94
}
88
95
89
- else -> openTerm(null )
96
+ else -> openTerm(null , null )
90
97
}
91
98
finish()
92
99
}
@@ -98,13 +105,15 @@ class NeoTermRemoteInterface : AppCompatActivity(), ServiceConnection {
98
105
val path = MediaUtils .getPath(this , extra)
99
106
val file = File (path)
100
107
val dirPath = if (file.isDirectory) path else file.parent
101
- openTerm(" cd " + TerminalUtils .escapeString(dirPath))
108
+ val command = " cd " + TerminalUtils .escapeString(dirPath)
109
+ openTerm(command, null )
102
110
}
103
111
finish()
104
112
} else {
105
113
App .get().errorDialog(this ,
106
- getString(R .string.unsupported_term_here, intent?.toString()),
107
- { finish() })
114
+ getString(R .string.unsupported_term_here, intent?.toString())) {
115
+ finish()
116
+ }
108
117
}
109
118
}
110
119
@@ -124,10 +133,10 @@ class NeoTermRemoteInterface : AppCompatActivity(), ServiceConnection {
124
133
when (extra) {
125
134
is ArrayList <* > -> {
126
135
extra.takeWhile { it is Uri }
127
- .mapTo(filesToHandle, {
136
+ .mapTo(filesToHandle) {
128
137
val uri = it as Uri
129
138
File (MediaUtils .getPath(this , uri)).absolutePath
130
- })
139
+ }
131
140
}
132
141
is Uri -> {
133
142
filesToHandle.add(File (MediaUtils .getPath(this , extra)).absolutePath)
@@ -142,8 +151,8 @@ class NeoTermRemoteInterface : AppCompatActivity(), ServiceConnection {
142
151
setupUserScriptView(filesToHandle, userScripts)
143
152
} else {
144
153
App .get().errorDialog(this ,
145
- getString(R .string.no_files_selected, intent?.toString()),
146
- { finish() })
154
+ getString(R .string.no_files_selected, intent?.toString())
155
+ ) { finish() }
147
156
}
148
157
}
149
158
@@ -155,10 +164,10 @@ class NeoTermRemoteInterface : AppCompatActivity(), ServiceConnection {
155
164
filesList.setOnItemClickListener { _, _, position, _ ->
156
165
AlertDialog .Builder (this @NeoTermRemoteInterface)
157
166
.setMessage(R .string.confirm_remove_file_from_list)
158
- .setPositiveButton(android.R .string.yes, { _, _ ->
167
+ .setPositiveButton(android.R .string.yes) { _, _ ->
159
168
filesToHandle.removeAt(position)
160
169
filesAdapter.notifyDataSetChanged()
161
- })
170
+ }
162
171
.setNegativeButton(android.R .string.no, null )
163
172
.show()
164
173
}
@@ -186,32 +195,42 @@ class NeoTermRemoteInterface : AppCompatActivity(), ServiceConnection {
186
195
return arguments.toTypedArray()
187
196
}
188
197
189
- private fun openTerm (parameter : ShellParameter ) {
198
+ private fun openTerm (parameter : ShellParameter ,
199
+ foreground : Boolean = true) {
190
200
val session = termService!! .createTermSession(parameter)
191
201
192
- // Set current session to our new one
193
- // In order to switch to it when entering NeoTermActivity
194
- NeoPreference .storeCurrentSession(session)
202
+ if (foreground) {
203
+ // Set current session to our new one
204
+ // In order to switch to it when entering NeoTermActivity
205
+ NeoPreference .storeCurrentSession(session)
206
+
207
+ val intent = Intent (this , NeoTermActivity ::class .java)
208
+ intent.addCategory(Intent .CATEGORY_DEFAULT )
209
+ intent.addFlags(Intent .FLAG_ACTIVITY_NEW_TASK )
210
+ startActivity(intent)
211
+ }
195
212
196
- val intent = Intent (this , NeoTermActivity ::class .java)
197
- intent.addCategory(Intent .CATEGORY_DEFAULT )
198
- intent.addFlags(Intent .FLAG_ACTIVITY_NEW_TASK )
199
- startActivity(intent)
213
+ val data = Intent ()
214
+ data.putExtra(EXTRA_SESSION_ID , session.mHandle)
215
+ setResult(Activity .RESULT_OK , data)
200
216
}
201
217
202
- private fun openCustomExecTerm (executablePath : String? , arguments : Array <String >? , cwd : String? ) {
218
+ private fun openTerm (initialCommand : String? ,
219
+ sessionId : String? = null,
220
+ foreground : Boolean = true) {
203
221
val parameter = ShellParameter ()
204
- .executablePath(executablePath)
205
- .arguments(arguments)
206
- .currentWorkingDirectory(cwd)
222
+ .initialCommand(initialCommand)
207
223
.callback(TermSessionCallback ())
208
224
.systemShell(detectSystemShell())
209
- openTerm(parameter)
225
+ .session(sessionId)
226
+ openTerm(parameter, foreground)
210
227
}
211
228
212
- private fun openTerm ( initialCommand : String? ) {
229
+ private fun openCustomExecTerm ( executablePath : String? , arguments : Array < String > ? , cwd : String? ) {
213
230
val parameter = ShellParameter ()
214
- .initialCommand(initialCommand)
231
+ .executablePath(executablePath)
232
+ .arguments(arguments)
233
+ .currentWorkingDirectory(cwd)
215
234
.callback(TermSessionCallback ())
216
235
.systemShell(detectSystemShell())
217
236
openTerm(parameter)
@@ -224,5 +243,7 @@ class NeoTermRemoteInterface : AppCompatActivity(), ServiceConnection {
224
243
companion object {
225
244
const val ACTION_EXECUTE = " neoterm.action.remote.execute"
226
245
const val EXTRA_COMMAND = " neoterm.extra.remote.execute.command"
246
+ const val EXTRA_SESSION_ID = " neoterm.extra.remote.execute.session"
247
+ const val EXTRA_FOREGROUND = " neoterm.extra.remote.execute.foreground"
227
248
}
228
249
}
0 commit comments