• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 package com.android.devicediagnostics
17 
18 import android.util.Base64
19 import com.android.devicediagnostics.Protos.BatteryInfo
20 import com.android.devicediagnostics.Protos.DeviceReport
21 import com.android.devicediagnostics.Protos.LockInfo
22 import com.android.devicediagnostics.Protos.ProductInfo
23 import com.android.devicediagnostics.Protos.StorageInfo
24 import com.android.devicediagnostics.Protos.TestResults
25 import org.json.JSONObject
26 
putIfNotEmptynull27 private fun putIfNotEmpty(holder: JSONObject, key: String, obj: JSONObject) {
28     if (obj.length() > 0) holder.put(key, obj)
29 }
30 
putIfPresentnull31 fun <T> JSONObject.putIfPresent(present: Boolean, key: String, value: T) {
32     if (present) put(key, value)
33 }
34 
testResultsToJsonnull35 private fun testResultsToJson(results: TestResults): JSONObject {
36     val obj = JSONObject()
37     obj.putIfPresent(results.hasScreenTest(), "screen_test", results.screenTest)
38     obj.putIfPresent(results.hasTouchTest(), "touch_test", results.touchTest)
39     return obj
40 }
41 
batteryInfoToJsonnull42 private fun batteryInfoToJson(battery: BatteryInfo): JSONObject {
43     val obj = JSONObject()
44     obj.putIfPresent(battery.hasCycleCount(), "cycle_count", battery.cycleCount)
45     obj.putIfPresent(battery.hasStateOfHealth(), "health", battery.stateOfHealth)
46     obj.putIfPresent(battery.hasSerial(), "serial", battery.serial)
47     obj.putIfPresent(battery.hasPartStatus(), "part_status", battery.partStatus)
48     obj.put("state", battery.legacyHealth)
49     obj.putIfPresent(
50         battery.hasManufactureTimestamp(),
51         "manufacturing_date",
52         battery.manufactureTimestamp,
53     )
54     obj.putIfPresent(
55         battery.hasFirstUsageTimestamp(),
56         "first_usage_date",
57         battery.firstUsageTimestamp,
58     )
59     return obj
60 }
61 
productInfoToJsonnull62 private fun productInfoToJson(product: ProductInfo): JSONObject {
63     val obj = JSONObject()
64     obj.putIfPresent(product.hasBrand(), "brand", product.brand)
65     obj.putIfPresent(product.hasDevice(), "device", product.device)
66     obj.putIfPresent(product.hasManufacturer(), "manufacturer", product.manufacturer)
67     obj.putIfPresent(product.hasModel(), "model", product.model)
68     obj.putIfPresent(product.hasName(), "name", product.name)
69     return obj
70 }
71 
storageInfoToJsonnull72 private fun storageInfoToJson(storage: StorageInfo): JSONObject {
73     val obj = JSONObject()
74     obj.putIfPresent(
75         storage.hasUsefulLifetimeRemaining(),
76         "useful_lifetime_remaining",
77         storage.usefulLifetimeRemaining,
78     )
79     obj.put("capacity_bytes", storage.capacityBytes.toString())
80     return obj
81 }
82 
lockInfoToJsonnull83 private fun lockInfoToJson(info: LockInfo): JSONObject {
84     val obj = JSONObject()
85     obj.put("factory_reset_protection", info.factoryResetProtection)
86     return obj
87 }
88 
deviceReportToJsonnull89 fun deviceReportToJson(report: DeviceReport): JSONObject {
90     val obj = JSONObject()
91     if (report.hasTests()) putIfNotEmpty(obj, "tests", testResultsToJson(report.tests))
92     if (report.hasAttestation()) {
93         val info = JSONObject()
94         if (!report.attestation.certificates.isEmpty) {
95             info.put(
96                 "certificates",
97                 Base64.encodeToString(report.attestation.certificates.toByteArray(), Base64.DEFAULT),
98             )
99         }
100         if (!report.attestation.error.isEmpty()) {
101             info.put("error", report.attestation.error)
102         }
103         obj.put("attestation", info)
104     }
105     if (report.hasBattery()) putIfNotEmpty(obj, "battery", batteryInfoToJson(report.battery))
106     if (report.hasStorage()) putIfNotEmpty(obj, "storage", storageInfoToJson(report.storage))
107     obj.putIfPresent(report.hasLaunchLevel(), "launch_level", report.launchLevel)
108     obj.put("locks", lockInfoToJson(report.locks))
109     if (report.hasProduct()) putIfNotEmpty(obj, "product", productInfoToJson(report.product))
110     return obj
111 }
112