Revert "Convert ErrorActivity+a to kotlin"
This reverts commit c3d5d6bdb1261f6e0c2f65e63efb14e687f8bff0.
Reason for revert: <Potential culprit for b/385686293 - verifying through ABTD before revert submission. This is part of the standard investigation process, and does not mean your CL will be reverted>
Change-Id: I45827c1b1e2c2a9c48c4806c7883f8c23a885b3c
diff --git a/android/TerminalApp/java/com/android/virtualization/terminal/ErrorActivity.java b/android/TerminalApp/java/com/android/virtualization/terminal/ErrorActivity.java
new file mode 100644
index 0000000..7099f22
--- /dev/null
+++ b/android/TerminalApp/java/com/android/virtualization/terminal/ErrorActivity.java
@@ -0,0 +1,88 @@
+/*
+ * 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
deleted file mode 100644
index f253f86..0000000
--- a/android/TerminalApp/java/com/android/virtualization/terminal/ErrorActivity.kt
+++ /dev/null
@@ -1,83 +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 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
new file mode 100644
index 0000000..4ab2b77
--- /dev/null
+++ b/android/TerminalApp/java/com/android/virtualization/terminal/TerminalExceptionHandler.java
@@ -0,0 +1,47 @@
+/*
+ * 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
deleted file mode 100644
index 3a8c444..0000000
--- a/android/TerminalApp/java/com/android/virtualization/terminal/TerminalExceptionHandler.kt
+++ /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 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
new file mode 100644
index 0000000..5ee535d
--- /dev/null
+++ b/android/TerminalApp/java/com/android/virtualization/terminal/TerminalThreadFactory.java
@@ -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 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
deleted file mode 100644
index f8e909d..0000000
--- a/android/TerminalApp/java/com/android/virtualization/terminal/TerminalThreadFactory.kt
+++ /dev/null
@@ -1,28 +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
-
-class TerminalThreadFactory(private val context: Context) : ThreadFactory {
- override fun newThread(r: Runnable): Thread {
- return Executors.defaultThreadFactory().newThread(r).also {
- it.uncaughtExceptionHandler = TerminalExceptionHandler(context)
- }
- }
-}