summaryrefslogtreecommitdiff
path: root/fakeadbserver/src/main/java/com/android/fakeadbserver/DeviceState.kt
blob: 434e94d7d4331dbb275dd95340fe7881405e63fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.fakeadbserver

import com.android.fakeadbserver.services.Service
import com.android.fakeadbserver.services.ServiceManager
import com.android.fakeadbserver.statechangehubs.ClientStateChangeHandlerFactory
import com.android.fakeadbserver.statechangehubs.ClientStateChangeHub
import com.android.fakeadbserver.statechangehubs.StateChangeQueue
import com.google.common.collect.ImmutableMap
import java.util.Collections
import java.util.TreeMap
import java.util.Vector
import java.util.function.Consumer
import java.util.stream.Collectors

class DeviceState internal constructor(
    private val mServer: FakeAdbServer,
    val deviceId: String,
    val manufacturer: String,
    val model: String,
    val buildVersionRelease: String,
    val buildVersionSdk: String,
    val cpuAbi: String,
    properties: Map<String, String>,
    val hostConnectionType: HostConnectionType,
    val transportId: Int
) {

    val clientChangeHub = ClientStateChangeHub()
    private val mFiles: MutableMap<String, DeviceFileState> = HashMap()
    private val mLogcatMessages: MutableList<String> = ArrayList()

    /** PID -> [ProcessState]  */
    private val mProcessStates: MutableMap<Int, ProcessState> = HashMap()
    private val mPortForwarders: MutableMap<Int, PortForwarder?> = HashMap()
    private val mReversePortForwarders: MutableMap<Int, PortForwarder?> = HashMap()
    val features: Set<String>
    val properties: Map<String, String>
    private var mDeviceStatus: DeviceStatus
    val serviceManager: ServiceManager

    // Keep track of all PM commands invocation
    private val mPmLogs = Vector<String>()

    // Keep track of all cmd commands invocation
    private val mCmdLogs = Vector<String>()

    // Keep track of all ABB/ABB_EXEC commands invocation
    private val mAbbLogs = Vector<String>()

    init {
        features = initFeatures(buildVersionSdk)
        this.properties =
            combinedProperties(
                deviceId,
                manufacturer,
                model,
                buildVersionRelease,
                buildVersionSdk,
                cpuAbi,
                properties
            )
        mDeviceStatus = DeviceStatus.OFFLINE
        serviceManager = ServiceManager()
    }

    internal constructor(server: FakeAdbServer, transportId: Int, config: DeviceStateConfig) : this(
        server,
        config.serialNumber,
        config.manufacturer,
        config.model,
        config.buildVersionRelease,
        config.buildVersionSdk,
        config.cpuAbi,
        config.properties,
        config.hostConnectionType,
        transportId
    ) {
        config.files.forEach(Consumer { fileState: DeviceFileState ->
            mFiles[fileState.path] =
                fileState
        })
        mLogcatMessages.addAll(config.logcatMessages)
        mDeviceStatus = config.deviceStatus
        config.processes
            .forEach(Consumer { clientState: ProcessState ->
                mProcessStates[clientState.pid] =
                    clientState
            })
    }

    fun stop() {
        clientChangeHub.stop()
    }

    val apiLevel: Int
        get() = try {
            buildVersionSdk.toInt()
        } catch (e: NumberFormatException) {
            1
        }

    var deviceStatus: DeviceStatus
        get() = mDeviceStatus
        set(status) {
            mDeviceStatus = status
            mServer.deviceChangeHub.deviceStatusChanged(this, status)
        }

    fun addLogcatMessage(message: String) {
        synchronized(mLogcatMessages) {
            mLogcatMessages.add(message)
            clientChangeHub.logcatMessageAdded(message)
        }
    }

    fun subscribeLogcatChangeHandler(
        handlerFactory: ClientStateChangeHandlerFactory
    ): LogcatChangeHandlerSubscriptionResult? {
        synchronized(mLogcatMessages) {
            val queue = clientChangeHub.subscribe(handlerFactory) ?: return null
            return LogcatChangeHandlerSubscriptionResult(
                queue, ArrayList(mLogcatMessages)
            )
        }
    }

    fun createFile(file: DeviceFileState) {
        synchronized(mFiles) { mFiles.put(file.path, file) }
    }

    fun getFile(filepath: String): DeviceFileState? {
        synchronized(mFiles) { return mFiles[filepath] }
    }

    fun deleteFile(filepath: String) {
        synchronized(mFiles) { mFiles.remove(filepath) }
    }

    fun startClient(
        pid: Int, uid: Int, packageName: String, isWaiting: Boolean
    ): ClientState {
        return startClient(pid, uid, packageName, packageName, isWaiting)
    }

    fun startClient(
        pid: Int,
        uid: Int,
        processName: String,
        packageName: String,
        isWaiting: Boolean
    ): ClientState {
        synchronized(mProcessStates) {
            val clientState = ClientState(pid, uid, processName, packageName, isWaiting)
            mProcessStates[pid] = clientState
            clientChangeHub.clientListChanged()
            clientChangeHub.appProcessListChanged()
            return clientState
        }
    }

    fun stopClient(pid: Int) {
        synchronized(mProcessStates) {
            val processState = mProcessStates.remove(pid)
            if (processState is ClientState) {
                clientChangeHub.clientListChanged()
                clientChangeHub.appProcessListChanged()
                processState.stopJdwpSession()
            }
        }
    }

    fun getClient(pid: Int): ClientState? {
        synchronized(mProcessStates) {
            val processState = mProcessStates[pid]
            return if (processState is ClientState) {
                processState
            } else {
                null
            }
        }
    }

    fun startProfileableProcess(
        pid: Int, architecture: String, commandLine: String
    ): ProfileableProcessState {
        synchronized(mProcessStates) {
            val process = ProfileableProcessState(pid, architecture, commandLine)
            mProcessStates[pid] = process
            clientChangeHub.appProcessListChanged()
            return process
        }
    }

    fun stopProfileableProcess(pid: Int) {
        synchronized(mProcessStates) {
            val process = mProcessStates.remove(pid)
            if (process is ProfileableProcessState) {
                clientChangeHub.appProcessListChanged()
            }
        }
    }

    fun getProfileableProcess(pid: Int): ProfileableProcessState? {
        synchronized(mProcessStates) {
            val process = mProcessStates[pid]
            return if (process is ProfileableProcessState) {
                process
            } else {
                null
            }
        }
    }

    val allPortForwarders: ImmutableMap<Int, PortForwarder?>
        get() {
            synchronized(mPortForwarders) { return ImmutableMap.copyOf(mPortForwarders) }
        }

    val allReversePortForwarders: ImmutableMap<Int, PortForwarder?>
        get() {
            synchronized(mReversePortForwarders) { return ImmutableMap.copyOf(mReversePortForwarders) }
        }

    fun addPortForwarder(forwarder: PortForwarder, noRebind: Boolean): Boolean {
        synchronized(mPortForwarders) {
            return if (noRebind) {
                (mPortForwarders.computeIfAbsent(
                    forwarder.source.mPort
                ) { port: Int? -> forwarder }
                        == forwarder)
            } else {
                // Just overwrite the previous forwarder.
                mPortForwarders[forwarder.source.mPort] = forwarder
                true
            }
        }
    }

    fun addReversePortForwarder(forwarder: PortForwarder, noRebind: Boolean): Boolean {
        synchronized(mReversePortForwarders) {
            return if (noRebind) {
                (mReversePortForwarders.computeIfAbsent(
                    forwarder.source.mPort
                ) { port: Int? -> forwarder }
                        == forwarder)
            } else {
                // Just overwrite the previous forwarder.
                mReversePortForwarders[forwarder.source.mPort] = forwarder
                true
            }
        }
    }

    fun removePortForwarder(hostPort: Int): Boolean {
        synchronized(mPortForwarders) { return mPortForwarders.remove(hostPort) != null }
    }

    fun removeReversePortForwarder(hostPort: Int): Boolean {
        synchronized(mReversePortForwarders) { return mReversePortForwarders.remove(hostPort) != null }
    }

    fun removeAllPortForwarders() {
        synchronized(mPortForwarders) { mPortForwarders.clear() }
    }

    fun removeAllReversePortForwarders() {
        synchronized(mReversePortForwarders) { mReversePortForwarders.clear() }
    }

    val clientListString: String
        get() {
            synchronized(mProcessStates) {
                return mProcessStates.values.stream()
                    .filter { process: ProcessState? -> process is ClientState }
                    .map { clientState: ProcessState -> Integer.toString(clientState.pid) }
                    .collect(Collectors.joining("\n"))
            }
        }

    fun copyOfProcessStates(): List<ProcessState> {
        synchronized(mProcessStates) { return ArrayList(mProcessStates.values) }
    }

    val config: DeviceStateConfig
        get() = DeviceStateConfig(
            deviceId,
            ArrayList(mFiles.values),
            ArrayList(mLogcatMessages),
            ArrayList(mProcessStates.values),
            hostConnectionType,
            manufacturer,
            model,
            buildVersionRelease,
            buildVersionSdk,
            cpuAbi,
            properties,
            mDeviceStatus
        )

    fun setActivityManager(newActivityManager: Service?) {
        serviceManager.setActivityManager(newActivityManager!!)
    }

    fun addPmLog(cmd: String) {
        mPmLogs.add(cmd)
    }

    val pmLogs: List<String>
        get() = mPmLogs.clone() as List<String>

    fun addCmdLog(cmd: String) {
        mCmdLogs.add(cmd)
    }

    val cmdLogs: List<String>
        get() = mCmdLogs.clone() as List<String>

    fun addAbbLog(cmd: String) {
        mAbbLogs.add(cmd)
    }

    val abbLogs: List<String>
        get() = mAbbLogs.clone() as List<String>

    /**
     * The state of a device.
     */
    enum class DeviceStatus( //$NON-NLS-1$
        val state: String
    ) {

        BOOTLOADER("bootloader"),  //$NON-NLS-1$

        /** bootloader mode with is-userspace = true though `adb reboot fastboot`  */
        FASTBOOTD("fastbootd"),  //$NON-NLS-1$
        OFFLINE("offline"),  //$NON-NLS-1$
        ONLINE("device"),  //$NON-NLS-1$
        RECOVERY("recovery"),  //$NON-NLS-1$

        /**
         * Device is in "sideload" state either through `adb sideload` or recovery menu
         */
        SIDELOAD("sideload"),  //$NON-NLS-1$
        UNAUTHORIZED("unauthorized"),  //$NON-NLS-1$
        DISCONNECTED("disconnected");

        companion object {

            /**
             * Returns a [DeviceStatus] from the string returned by `adb devices`.
             *
             * @param state the device state.
             * @return a {DeviceStatus} object or `null` if the state is unknown.
             */
            fun getState(state: String): DeviceStatus? {
                for (deviceStatus in values()) {
                    if (deviceStatus.state == state) {
                        return deviceStatus
                    }
                }
                return null
            }
        }
    }

    enum class HostConnectionType {
        USB, LOCAL, NETWORK
    }

    /**
     * This class represents the result of calling [subscribeLogcatChangeHandler]. This is needed to
     * synchronize between adding the listener and getting the correct lines from the logcat buffer.
     */
    class LogcatChangeHandlerSubscriptionResult(
        @JvmField val mQueue: StateChangeQueue,
        @JvmField val mLogcatContents: List<String>
    )

    companion object {

        private fun initFeatures(sdk: String): Set<String> {
            val features: MutableSet<String> =
                HashSet(mutableListOf("push_sync", "fixed_push_mkdir", "apex"))
            try {
                val api = sdk.toInt()
                if (api >= 24) {
                    features.add("cmd")
                    features.add("shell_v2")
                    features.add("stat_v2")
                }
                if (api >= 30) {
                    features.add("abb")
                    features.add("abb_exec")
                }
            } catch (e: NumberFormatException) {
                // Cannot add more features based on API level since it is not the expected integer
                // This is expected in many of our test that don't pass a correct value but instead
                // pass "sdk". In such case, we return the default set of features.
                // TODO: Fix adblist test to not send "sdk" and delete this catch.
            }
            return Collections.unmodifiableSet(features)
        }

        private fun combinedProperties(
            serialNumber: String,
            manufacturer: String,
            model: String,
            release: String,
            sdk: String,
            cpuAbi: String,
            properties: Map<String, String>
        ): Map<String, String> {
            val combined: MutableMap<String, String> = TreeMap(properties)
            combined["ro.serialno"] = serialNumber
            combined["ro.product.manufacturer"] = manufacturer
            combined["ro.product.model"] = model
            combined["ro.build.version.release"] = release
            combined["ro.build.version.sdk"] = sdk
            combined["ro.product.cpu.abi"] = cpuAbi
            return combined
        }
    }
}