• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.getProductInfo
32 import com.android.devicediagnostics.evaluated.getStorageInfo
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 challenge: ByteArray
88             if (selection == null) {
89                 challenge = ByteArray(0)
90             } else {
91                 challenge = selection.toByteArray(Charsets.UTF_8)
92             }
93 
94             val report =
95                 DeviceReport.newBuilder().run {
96                     setLocks(getLockInfo(context))
97                     if (!locks.factoryResetProtection) {
98                         setBattery(getBatteryInfo(context))
99                         setStorage(getStorageInfo(context))
100                         setLaunchLevel(ApplicationInterface.app.getLaunchLevel())
101                         setProduct(getProductInfo())
102                         selection?.run { setAttestation(createAttestationRecord(challenge)) }
103                     }
104                     build()
105                 }
106             val json = deviceReportToJson(report)
107             if (!report.locks.factoryResetProtection) {
108                 val tm = context.getSystemService(TelephonyManager::class.java)!!
109                 val imeis = JSONArray()
110                 for (i in 0 until tm.activeModemCount) {
111                     try {
112                         imeis.put(tm.getImei(i))
113                     } catch (e: Exception) {
114                         Log.e(TAG, "Could not get device identifiers", e)
115                         break
116                     }
117                 }
118                 json.put("imeis", imeis)
119                 json.put("serial", Build.getSerial())
120             }
121             return json.toString(2)
122         }
123 
getShortnull124         override fun getShort(column: Int): Short {
125             TODO("Not implemented")
126         }
127 
getIntnull128         override fun getInt(column: Int): Int {
129             TODO("Not implemented")
130         }
131 
getLongnull132         override fun getLong(column: Int): Long {
133             TODO("Not implemented")
134         }
135 
getFloatnull136         override fun getFloat(column: Int): Float {
137             TODO("Not implemented")
138         }
139 
getDoublenull140         override fun getDouble(column: Int): Double {
141             TODO("Not implemented")
142         }
143 
isNullnull144         override fun isNull(column: Int): Boolean {
145             TODO("Not implemented")
146         }
147     }
148 }
149