summaryrefslogtreecommitdiff
path: root/adblib-tools/src/com/android/adblib/tools/debugging/packets/ddms/DdmsChunkTypes.kt
blob: 81fa27212cb019db5971d6940d8c1be165f92793 (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
/*
 * Copyright (C) 2022 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.adblib.tools.debugging.packets.ddms

@JvmInline
value class DdmsChunkTypes(val value: Int) {

    val text: String
        get() = chunkTypeToString(this)

    override fun toString(): String {
        return text
    }

    @Suppress("SpellCheckingInspection")
    companion object {
        val NULL = DdmsChunkTypes(0)

        val FAIL = chunkTypeFromString("FAIL")

        val HELO = chunkTypeFromString("HELO")

        val FEAT = chunkTypeFromString("FEAT")

        /**
         * "REAE: REcent Allocation Enable"
         */
        val REAE = chunkTypeFromString("REAE")

        /**
         * "REAQ: REcent Allocation Query"
         */
        val REAQ = chunkTypeFromString("REAQ")

        /**
         * "REAL: REcent Allocation List"
         */
        val REAL = chunkTypeFromString("REAL")

        val APNM = chunkTypeFromString("APNM")

        val WAIT = chunkTypeFromString("WAIT")

        val EXIT = chunkTypeFromString("EXIT")

        /**
         * Requests a `Method Profiling Streaming Start`
         */
        val MPSS = chunkTypeFromString("MPSS")

        /**
         * Requests a `Method Profiling Streaming End`
         */
        val MPSE = chunkTypeFromString("MPSE")

        /**
         * Requests a `Method PRofiling Start`
         */
        val MPRS = chunkTypeFromString("MPRS")

        /**
         * Requests a `Method PRofiling End`
         */
        val MPRE = chunkTypeFromString("MPRE")

        /**
         * Requests a `Method PRofiling Query`
         */
        val MPRQ = chunkTypeFromString("MPRQ")

        /**
         * Requests a `Sampling Profiling Streaming Start`
         */
        val SPSS = chunkTypeFromString("SPSS")

        /**
         * Requests a `Sampling Profiling Streaming End`
         */
        val SPSE = chunkTypeFromString("SPSE")

        /**
         * List `ViewRootImpl`'s of this process
         */
        val VULW = chunkTypeFromString("VULW")

        /**
         * Operation on view root, first parameter in packet should be one of VURT_* constants
         */
        val VURT = chunkTypeFromString("VURT")

        enum class VURTOpCode(val value: Int) {
            /**
             * Dump view hierarchy
             */
            VURT_DUMP_HIERARCHY(1);
        }

        /**
         * Generic View Operation, first parameter in the packet should be one of the VUOP_* constants
         * below.
         */
        val VUOP = chunkTypeFromString("VUOP")

        /**
         * Requests a Gabage Collection of a process (`HeaP Gabage Collect`)
         */
        val HPGC = chunkTypeFromString("HPGC")

        enum class VUOPOpCode(val value: Int) {

            /** Capture View.  */
            VUOP_CAPTURE_VIEW(1);
        }

        /**
         * Convert a 4-character string to a 32-bit chunk type.
         */
        private fun chunkTypeFromString(type: String): DdmsChunkTypes {
            var result = 0
            check(type.length == 4) { "Type name must be 4 letter long" }
            for (i in 0..3) {
                result = result shl 8
                result = result or type[i].code.toByte().toInt()
            }
            return DdmsChunkTypes(result)
        }

        /**
         * Convert an integer type to a 4-character string.
         */
        private fun chunkTypeToString(type: DdmsChunkTypes): String {
            val ascii = ByteArray(4)
            ascii[0] = (type.value shr 24 and 0xff).toByte()
            ascii[1] = (type.value shr 16 and 0xff).toByte()
            ascii[2] = (type.value shr 8 and 0xff).toByte()
            ascii[3] = (type.value and 0xff).toByte()
            return String(ascii, Charsets.US_ASCII)
        }
    }
}