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.StorageInfo
23 import com.android.devicediagnostics.Protos.TestResults
24 import org.json.JSONObject
25 
putIfNotEmptynull26 private fun putIfNotEmpty(holder: JSONObject, key: String, obj: JSONObject) {
27     if (obj.length() > 0) holder.put(key, obj)
28 }
29 
putIfPresentnull30 fun <T> JSONObject.putIfPresent(present: Boolean, key: String, value: T) {
31     if (present) put(key, value)
32 }
33 
testResultsToJsonnull34 private fun testResultsToJson(results: TestResults): JSONObject {
35     val obj = JSONObject()
36     obj.putIfPresent(results.hasScreenTest(), "screen_test", results.screenTest)
37     obj.putIfPresent(results.hasTouchTest(), "touch_test", results.touchTest)
38     return obj
39 }
40 
batteryInfoToJsonnull41 private fun batteryInfoToJson(battery: BatteryInfo): JSONObject {
42     val obj = JSONObject()
43     obj.putIfPresent(battery.hasCycleCount(), "cycle_count", battery.cycleCount)
44     obj.putIfPresent(battery.hasStateOfHealth(), "health", battery.stateOfHealth)
45     obj.putIfPresent(battery.hasSerial(), "serial", battery.serial)
46     obj.putIfPresent(battery.hasPartStatus(), "part_status", battery.partStatus)
47     obj.put("state", battery.legacyHealth)
48     obj.putIfPresent(
49         battery.hasManufactureTimestamp(),
50         "manufacturing_date",
51         battery.manufactureTimestamp,
52     )
53     obj.putIfPresent(
54         battery.hasFirstUsageTimestamp(),
55         "first_usage_date",
56         battery.firstUsageTimestamp,
57     )
58     return obj
59 }
60 
storageInfoToJsonnull61 private fun storageInfoToJson(storage: StorageInfo): JSONObject {
62     val obj = JSONObject()
63     obj.putIfPresent(
64         storage.hasUsefulLifetimeRemaining(),
65         "useful_lifetime_remaining",
66         storage.usefulLifetimeRemaining,
67     )
68     obj.put("capacity_bytes", storage.capacityBytes.toString())
69     return obj
70 }
71 
lockInfoToJsonnull72 private fun lockInfoToJson(info: LockInfo): JSONObject {
73     val obj = JSONObject()
74     obj.put("factory_reset_protection", info.factoryResetProtection)
75     return obj
76 }
77 
deviceReportToJsonnull78 fun deviceReportToJson(report: DeviceReport): JSONObject {
79     val obj = JSONObject()
80     if (report.hasTests()) putIfNotEmpty(obj, "tests", testResultsToJson(report.tests))
81     if (!report.attestation.isEmpty)
82         obj.put(
83             "attestation",
84             Base64.encodeToString(report.attestation.toByteArray(), Base64.DEFAULT),
85         )
86     if (report.hasBattery()) putIfNotEmpty(obj, "battery", batteryInfoToJson(report.battery))
87     if (report.hasStorage()) putIfNotEmpty(obj, "storage", storageInfoToJson(report.storage))
88     obj.putIfPresent(report.hasLaunchLevel(), "launch_level", report.launchLevel)
89     obj.put("locks", lockInfoToJson(report.locks))
90     return obj
91 }
92