1 /* <lambda>null2 * 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 * 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.virtualization.terminal 17 18 import android.content.Context 19 import android.content.Intent 20 import android.os.Bundle 21 import android.text.method.ScrollingMovementMethod 22 import android.view.View 23 import android.widget.TextView 24 import java.io.IOException 25 import java.io.PrintWriter 26 import java.io.StringWriter 27 import java.lang.Exception 28 import java.lang.RuntimeException 29 30 class ErrorActivity : BaseActivity() { 31 override fun onCreate(savedInstanceState: Bundle?) { 32 super.onCreate(savedInstanceState) 33 34 setContentView(R.layout.activity_error) 35 36 val button = findViewById<View>(R.id.recovery) 37 button.setOnClickListener(View.OnClickListener { _ -> launchRecoveryActivity() }) 38 findViewById<TextView>(R.id.cause).setMovementMethod(ScrollingMovementMethod()) 39 } 40 41 override fun onNewIntent(intent: Intent) { 42 super.onNewIntent(intent) 43 setIntent(intent) 44 } 45 46 override fun onResume() { 47 super.onResume() 48 49 val intent = getIntent() 50 val e = intent.getParcelableExtra<Exception?>(EXTRA_CAUSE, Exception::class.java) 51 val cause = findViewById<TextView>(R.id.cause) 52 cause.text = e?.let { getString(R.string.error_code, getStackTrace(it)) } 53 } 54 55 private fun launchRecoveryActivity() { 56 val intent = Intent(this, SettingsRecoveryActivity::class.java) 57 startActivity(intent) 58 } 59 60 companion object { 61 private const val EXTRA_CAUSE = "cause" 62 63 fun start(context: Context, e: Exception) { 64 val intent = Intent(context, ErrorActivity::class.java) 65 intent.putExtra(EXTRA_CAUSE, e) 66 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK) 67 context.startActivity(intent) 68 } 69 70 private fun getStackTrace(e: Exception): String? { 71 try { 72 StringWriter().use { sWriter -> 73 PrintWriter(sWriter).use { pWriter -> 74 e.printStackTrace(pWriter) 75 return sWriter.toString() 76 } 77 } 78 } catch (ex: IOException) { 79 // This shall never happen 80 throw RuntimeException(ex) 81 } 82 } 83 } 84 } 85