/* * Copyright 2024 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 * * https://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.devicediagnostics.evaluated import android.content.Intent import android.os.Bundle import android.util.Log import android.view.LayoutInflater import android.view.View import android.widget.Button import android.widget.LinearLayout import android.widget.TextView import androidx.fragment.app.commit import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat import com.android.devicediagnostics.ApplicationInterface import com.android.devicediagnostics.AttestationController import com.android.devicediagnostics.DisplayResultFragment import com.android.devicediagnostics.MainActivity import com.android.devicediagnostics.Protos.DeviceReport import com.android.devicediagnostics.R import com.android.devicediagnostics.runInBackground import com.android.settingslib.collapsingtoolbar.CollapsingToolbarBaseActivity private const val TAG = "EvaluationFinalize" class EvaluationFinalizeFragment(private val ok: Boolean) : PreferenceFragmentCompat() { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { setPreferencesFromResource(R.xml.preferences_evaluated_done, rootKey) findPreference("Done")!!.also { if (!ok) { it.setTitle(R.string.bluetooth_error_title) it.setSummary(R.string.bluetooth_error_summary) } it.setOnPreferenceClickListener { val intent = Intent(activity, MainActivity::class.java) intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) activity!!.startActivity(intent) activity!!.finish() true } } } } class EvaluationFinalizeActivity : CollapsingToolbarBaseActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.evaluated_finalize) setTitle(R.string.evaluation_mode_title) if (savedInstanceState == null) { showProgressIndicator(R.string.evaluated_finalizing_report) buildReport() } } private fun buildReport() { val state = TestState.fromActivity(this)!! // Launch in separate thread since gathering attestation takes time and we don't want to // block the UI from appearing runInBackground { val reportBuilder = DeviceReport.newBuilder() reportBuilder.setTests(state.testResults) val batteryInfo = getBatteryInfo(this) val storageInfo = getStorageInfo(this) val launchLevel = ApplicationInterface.app.getLaunchLevel() val attestation = createAttestationRecord(state.trustedDevice.challenge.toByteArray()) val bluetoothClient = ApplicationInterface.app.getBluetoothClient() val connectionData = bluetoothClient.connectionData if (connectionData != null && connectionData.version == 4) { reportBuilder.setOldAttestation(attestation.certificates) } else { reportBuilder.setAttestation(attestation) } reportBuilder.setBattery(batteryInfo) reportBuilder.setStorage(storageInfo) reportBuilder.setLaunchLevel(launchLevel) val report = reportBuilder.build() runOnUiThread { onReportReady(report) } } } private fun onReportReady(report: DeviceReport) { val confirmation = showHeaderLayout(R.layout.evaluated_final_confirmation) val button = confirmation.findViewById