|
| 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