Convert ErrorActivity+a to kotlin
Bug: 383243644
Test: adb shell input keyevent 0
Change-Id: Ia4b083967aaffe66cc6d4ec48dffa944781ecab4
diff --git a/android/TerminalApp/java/com/android/virtualization/terminal/ErrorActivity.java b/android/TerminalApp/java/com/android/virtualization/terminal/ErrorActivity.java
deleted file mode 100644
index 7099f22..0000000
--- a/android/TerminalApp/java/com/android/virtualization/terminal/ErrorActivity.java
+++ /dev/null
@@ -1,88 +0,0 @@
-/*
- * 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
- *
- * http://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.virtualization.terminal;
-
-import android.content.Context;
-import android.content.Intent;
-import android.os.Bundle;
-import android.view.View;
-import android.widget.TextView;
-
-import androidx.annotation.NonNull;
-import androidx.annotation.Nullable;
-
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.io.StringWriter;
-
-public class ErrorActivity extends BaseActivity {
- private static final String EXTRA_CAUSE = "cause";
-
- public static void start(Context context, Exception e) {
- Intent intent = new Intent(context, ErrorActivity.class);
- intent.putExtra(EXTRA_CAUSE, e);
- intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
- context.startActivity(intent);
- }
-
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- setContentView(R.layout.activity_error);
-
- View button = findViewById(R.id.recovery);
- button.setOnClickListener((event) -> launchRecoveryActivity());
- }
-
- @Override
- protected void onNewIntent(@NonNull Intent intent) {
- super.onNewIntent(intent);
- setIntent(intent);
- }
-
- @Override
- public void onResume() {
- super.onResume();
-
- Intent intent = getIntent();
- Exception e = intent.getParcelableExtra(EXTRA_CAUSE, Exception.class);
- TextView cause = findViewById(R.id.cause);
- if (e != null) {
- String stackTrace = getStackTrace(e);
- cause.setText(getString(R.string.error_code, stackTrace));
- } else {
- cause.setText(null);
- }
- }
-
- private void launchRecoveryActivity() {
- Intent intent = new Intent(this, SettingsRecoveryActivity.class);
- startActivity(intent);
- }
-
- private static String getStackTrace(Exception e) {
- try (StringWriter sWriter = new StringWriter();
- PrintWriter pWriter = new PrintWriter(sWriter)) {
- e.printStackTrace(pWriter);
- return sWriter.toString();
- } catch (IOException ex) {
- // This shall never happen
- throw new RuntimeException(ex);
- }
- }
-}
diff --git a/android/TerminalApp/java/com/android/virtualization/terminal/ErrorActivity.kt b/android/TerminalApp/java/com/android/virtualization/terminal/ErrorActivity.kt
new file mode 100644
index 0000000..f253f86
--- /dev/null
+++ b/android/TerminalApp/java/com/android/virtualization/terminal/ErrorActivity.kt
@@ -0,0 +1,83 @@
+/*
+ * 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
+ *
+ * http://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.virtualization.terminal
+
+import android.content.Context
+import android.content.Intent
+import android.os.Bundle
+import android.view.View
+import android.widget.TextView
+import java.io.IOException
+import java.io.PrintWriter
+import java.io.StringWriter
+import java.lang.Exception
+import java.lang.RuntimeException
+
+class ErrorActivity : BaseActivity() {
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+
+ setContentView(R.layout.activity_error)
+
+ val button = findViewById<View>(R.id.recovery)
+ button.setOnClickListener(View.OnClickListener { _ -> launchRecoveryActivity() })
+ }
+
+ override fun onNewIntent(intent: Intent) {
+ super.onNewIntent(intent)
+ setIntent(intent)
+ }
+
+ override fun onResume() {
+ super.onResume()
+
+ val intent = getIntent()
+ val e = intent.getParcelableExtra<Exception?>(EXTRA_CAUSE, Exception::class.java)
+ val cause = findViewById<TextView>(R.id.cause)
+ cause.text = e?.let { getString(R.string.error_code, getStackTrace(it)) }
+ }
+
+ private fun launchRecoveryActivity() {
+ val intent = Intent(this, SettingsRecoveryActivity::class.java)
+ startActivity(intent)
+ }
+
+ companion object {
+ private const val EXTRA_CAUSE = "cause"
+
+ @JvmStatic
+ fun start(context: Context, e: Exception) {
+ val intent = Intent(context, ErrorActivity::class.java)
+ intent.putExtra(EXTRA_CAUSE, e)
+ intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
+ context.startActivity(intent)
+ }
+
+ private fun getStackTrace(e: Exception): String? {
+ try {
+ StringWriter().use { sWriter ->
+ PrintWriter(sWriter).use { pWriter ->
+ e.printStackTrace(pWriter)
+ return sWriter.toString()
+ }
+ }
+ } catch (ex: IOException) {
+ // This shall never happen
+ throw RuntimeException(ex)
+ }
+ }
+ }
+}
diff --git a/android/TerminalApp/java/com/android/virtualization/terminal/TerminalExceptionHandler.java b/android/TerminalApp/java/com/android/virtualization/terminal/TerminalExceptionHandler.java
deleted file mode 100644
index 4ab2b77..0000000
--- a/android/TerminalApp/java/com/android/virtualization/terminal/TerminalExceptionHandler.java
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * 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
- *
- * http://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.virtualization.terminal;
-
-import android.content.Context;
-import android.util.Log;
-
-public class TerminalExceptionHandler implements Thread.UncaughtExceptionHandler {
- private static final String TAG = "TerminalExceptionHandler";
-
- private final Context mContext;
-
- public TerminalExceptionHandler(Context context) {
- mContext = context;
- }
-
- @Override
- public void uncaughtException(Thread thread, Throwable throwable) {
- Exception exception;
- if (throwable instanceof Exception) {
- exception = (Exception) throwable;
- } else {
- exception = new Exception(throwable);
- }
- try {
- ErrorActivity.start(mContext, exception);
- } catch (Exception ex) {
- Log.wtf(TAG, "Failed to launch error activity for an exception", exception);
- }
-
- thread.getDefaultUncaughtExceptionHandler().uncaughtException(thread, throwable);
- }
-}
diff --git a/android/TerminalApp/java/com/android/virtualization/terminal/TerminalExceptionHandler.kt b/android/TerminalApp/java/com/android/virtualization/terminal/TerminalExceptionHandler.kt
new file mode 100644
index 0000000..3a8c444
--- /dev/null
+++ b/android/TerminalApp/java/com/android/virtualization/terminal/TerminalExceptionHandler.kt
@@ -0,0 +1,37 @@
+/*
+ * 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
+ *
+ * http://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.virtualization.terminal
+
+import android.content.Context
+import android.util.Log
+import java.lang.Exception
+
+class TerminalExceptionHandler(private val context: Context) : Thread.UncaughtExceptionHandler {
+
+ override fun uncaughtException(thread: Thread, throwable: Throwable) {
+ val exception = (throwable as? Exception) ?: Exception(throwable)
+ try {
+ ErrorActivity.start(context, exception)
+ } catch (_: Exception) {
+ Log.wtf(TAG, "Failed to launch error activity for an exception", exception)
+ }
+ Thread.getDefaultUncaughtExceptionHandler()?.uncaughtException(thread, throwable)
+ }
+
+ companion object {
+ private const val TAG = "TerminalExceptionHandler"
+ }
+}
diff --git a/android/TerminalApp/java/com/android/virtualization/terminal/TerminalThreadFactory.java b/android/TerminalApp/java/com/android/virtualization/terminal/TerminalThreadFactory.java
deleted file mode 100644
index 5ee535d..0000000
--- a/android/TerminalApp/java/com/android/virtualization/terminal/TerminalThreadFactory.java
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * 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
- *
- * http://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.virtualization.terminal;
-
-import android.content.Context;
-
-import java.util.concurrent.Executors;
-import java.util.concurrent.ThreadFactory;
-
-public class TerminalThreadFactory implements ThreadFactory {
- private final Context mContext;
-
- public TerminalThreadFactory(Context context) {
- mContext = context;
- }
-
- @Override
- public Thread newThread(Runnable r) {
- Thread thread = Executors.defaultThreadFactory().newThread(r);
- thread.setUncaughtExceptionHandler(new TerminalExceptionHandler(mContext));
- return thread;
- }
-}
diff --git a/android/TerminalApp/java/com/android/virtualization/terminal/TerminalThreadFactory.kt b/android/TerminalApp/java/com/android/virtualization/terminal/TerminalThreadFactory.kt
new file mode 100644
index 0000000..f8e909d
--- /dev/null
+++ b/android/TerminalApp/java/com/android/virtualization/terminal/TerminalThreadFactory.kt
@@ -0,0 +1,28 @@
+/*
+ * 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
+ *
+ * http://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.virtualization.terminal
+
+import android.content.Context
+import java.util.concurrent.Executors
+import java.util.concurrent.ThreadFactory
+
+class TerminalThreadFactory(private val context: Context) : ThreadFactory {
+ override fun newThread(r: Runnable): Thread {
+ return Executors.defaultThreadFactory().newThread(r).also {
+ it.uncaughtExceptionHandler = TerminalExceptionHandler(context)
+ }
+ }
+}