Skip to content

Commit 7ade00f

Browse files
Wiring up the ShellExecutorFactory to provide a ShellExecutorFileObserverImpl when the binderKey has the correct form.
PiperOrigin-RevId: 534931746
1 parent 3091479 commit 7ade00f

File tree

3 files changed

+105
-2
lines changed

3 files changed

+105
-2
lines changed

services/shellexecutor/java/androidx/test/services/shellexecutor/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ kt_android_library(
6363
"ShellExecSharedConstants.java",
6464
"ShellExecutor.java",
6565
"ShellExecutorFactory.java",
66+
"ShellExecutorFileObserverImpl.kt",
6667
"ShellExecutorImpl.java",
6768
],
6869
idl_srcs = ["Command.aidl"],

services/shellexecutor/java/androidx/test/services/shellexecutor/ShellExecutorFactory.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,13 @@ public ShellExecutorFactory(Context context, String binderKey) {
2929
this.binderKey = binderKey;
3030
}
3131

32-
@SuppressWarnings("deprecation") // Temporary until we make the constructor package-private
3332
public ShellExecutor create() {
34-
return new ShellExecutorImpl(context, binderKey);
33+
// Binder keys for SpeakEasy are a string of hex digits. Binder keys for the FileObserver
34+
// protocol are the absolute path of the directory that the server is watching.
35+
if (binderKey.startsWith("/")) {
36+
return new ShellExecutorFileObserverImpl(binderKey);
37+
} else {
38+
return new ShellExecutorImpl(context, binderKey);
39+
}
3540
}
3641
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright (C) 2023 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package androidx.test.services.shellexecutor
18+
19+
import java.io.InputStream
20+
21+
/** ShellExecutor that talks to FileObserverShellMain. */
22+
class ShellExecutorFileObserverImpl(private val binderKey: String) : ShellExecutor {
23+
private val client = ShellCommandFileObserverClient()
24+
25+
override fun getBinderKey() = binderKey
26+
27+
/** {@inheritDoc} */
28+
override fun executeShellCommandSync(
29+
command: String?,
30+
parameters: List<String>?,
31+
shellEnv: Map<String, String>?,
32+
executeThroughShell: Boolean,
33+
timeoutMs: Long
34+
): String {
35+
if (command == null || command.isEmpty()) {
36+
throw IllegalArgumentException("Null or empty command")
37+
}
38+
39+
val message =
40+
Messages.Command(
41+
command,
42+
parameters ?: emptyList(),
43+
shellEnv ?: emptyMap(),
44+
executeThroughShell,
45+
redirectErrorStream = true,
46+
if (timeoutMs > 0L) timeoutMs else TIMEOUT_FOREVER
47+
)
48+
49+
val execution = client.run(binderKey, message)
50+
return execution.await().stdout.toString(Charsets.UTF_8)
51+
}
52+
53+
/** {@inheritDoc} */
54+
override fun executeShellCommandSync(
55+
command: String?,
56+
parameters: List<String>?,
57+
shellEnv: Map<String, String>?,
58+
executeThroughShell: Boolean
59+
) = executeShellCommandSync(command, parameters, shellEnv, executeThroughShell, TIMEOUT_FOREVER)
60+
61+
/** {@inheritDoc} */
62+
override fun executeShellCommand(
63+
command: String?,
64+
parameters: List<String>?,
65+
shellEnv: Map<String, String>?,
66+
executeThroughShell: Boolean,
67+
timeoutMs: Long
68+
): InputStream {
69+
if (command == null || command.isEmpty()) {
70+
throw IllegalArgumentException("Null or empty command")
71+
}
72+
73+
val message =
74+
Messages.Command(
75+
command,
76+
parameters ?: emptyList(),
77+
shellEnv ?: emptyMap(),
78+
executeThroughShell,
79+
redirectErrorStream = true,
80+
if (timeoutMs > 0L) timeoutMs else TIMEOUT_FOREVER
81+
)
82+
83+
return client.run(binderKey, message).asStream()
84+
}
85+
86+
/** {@inheritDoc} */
87+
override fun executeShellCommand(
88+
command: String?,
89+
parameters: List<String>?,
90+
shellEnv: Map<String, String>?,
91+
executeThroughShell: Boolean
92+
) = executeShellCommand(command, parameters, shellEnv, executeThroughShell, TIMEOUT_FOREVER)
93+
94+
companion object {
95+
const val TIMEOUT_FOREVER = 24 * 60 * 60 * 1000L
96+
}
97+
}

0 commit comments

Comments
 (0)