1 /*
2  * Copyright 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  *      https://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.content.ContentProvider
19 import android.content.ContentValues
20 import android.content.Context
21 import android.database.AbstractCursor
22 import android.database.Cursor
23 import android.net.Uri
24 import android.os.Build
25 import android.telephony.TelephonyManager
26 import android.util.Log
27 import com.android.devicediagnostics.Protos.DeviceReport
28 import com.android.devicediagnostics.evaluated.createAttestationRecord
29 import com.android.devicediagnostics.evaluated.getBatteryInfo
30 import com.android.devicediagnostics.evaluated.getLockInfo
31 import com.android.devicediagnostics.evaluated.getStorageInfo
32 import com.google.protobuf.ByteString
33 import org.json.JSONArray
34 
35 private const val TAG = "GetStatus"
36 
37 class GetStatusContentProvider : ContentProvider() {
onCreatenull38     override fun onCreate(): Boolean {
39         return true
40     }
41 
querynull42     override fun query(
43         uri: Uri,
44         projection: Array<out String>?,
45         selection: String?,
46         selectionArgs: Array<out String>?,
47         sortOrder: String?,
48     ): Cursor? {
49         return StatusCursor(context!!, selection)
50     }
51 
getTypenull52     override fun getType(uri: Uri): String? {
53         Log.d(TAG, "Not implemented")
54         return null
55     }
56 
insertnull57     override fun insert(uri: Uri, values: ContentValues?): Uri? {
58         Log.d(TAG, "Not implemented")
59         return null
60     }
61 
deletenull62     override fun delete(uri: Uri, selection: String?, selectionArgs: Array<out String>?): Int {
63         Log.d(TAG, "Not implemented")
64         return 0
65     }
66 
updatenull67     override fun update(
68         uri: Uri,
69         values: ContentValues?,
70         selection: String?,
71         selectionArgs: Array<out String>?,
72     ): Int {
73         Log.d(TAG, "Not implemented")
74         return 0
75     }
76 
77     class StatusCursor(val context: Context, val selection: String?) : AbstractCursor() {
getCountnull78         override fun getCount(): Int {
79             return 1
80         }
81 
getColumnNamesnull82         override fun getColumnNames(): Array<String> {
83             return arrayOf("Status")
84         }
85 
getStringnull86         override fun getString(column: Int): String {
87             val report =
88                 DeviceReport.newBuilder().run {
89                     setLocks(getLockInfo(context))
90                     if (!locks.factoryResetProtection) {
91                         setBattery(getBatteryInfo(context))
92                         setStorage(getStorageInfo(context))
93                         setLaunchLevel(ApplicationInterface.app.getLaunchLevel())
94                         selection?.run {
95                             setAttestation(
96                                 ByteString.copyFrom(createAttestationRecord(toByteArray()))
97                             )
98                         }
99                     }
100                     build()
101                 }
102             val json = deviceReportToJson(report)
103             if (!report.locks.factoryResetProtection) {
104                 val tm = context.getSystemService(TelephonyManager::class.java)!!
105                 val imeis = JSONArray()
106                 for (i in 0 until tm.activeModemCount) {
107                     try {
108                         imeis.put(tm.getImei(i))
109                     } catch (e: Exception) {
110                         Log.e(TAG, "Could not get device identifiers", e)
111                         break
112                     }
113                 }
114                 json.put("imeis", imeis)
115                 json.put("serial", Build.getSerial())
116             }
117             return json.toString(2)
118         }
119 
getShortnull120         override fun getShort(column: Int): Short {
121             TODO("Not implemented")
122         }
123 
getIntnull124         override fun getInt(column: Int): Int {
125             TODO("Not implemented")
126         }
127 
getLongnull128         override fun getLong(column: Int): Long {
129             TODO("Not implemented")
130         }
131 
getFloatnull132         override fun getFloat(column: Int): Float {
133             TODO("Not implemented")
134         }
135 
getDoublenull136         override fun getDouble(column: Int): Double {
137             TODO("Not implemented")
138         }
139 
isNullnull140         override fun isNull(column: Int): Boolean {
141             TODO("Not implemented")
142         }
143     }
144 }
145