Use UI listener for preferred account worker in in call UI

Before this CL it is ran on a non-UI task, where the callback might happen after the activity has already ended.

TEST=manual
Bug: 78517857
Test: manual
PiperOrigin-RevId: 194169377
Change-Id: I0011019cb31b0b2c01c9d774776fb44dcac4d8c6
diff --git a/java/com/android/incallui/InCallActivity.java b/java/com/android/incallui/InCallActivity.java
index 68de8dc..2b6c2b4 100644
--- a/java/com/android/incallui/InCallActivity.java
+++ b/java/com/android/incallui/InCallActivity.java
@@ -63,6 +63,7 @@
 import com.android.dialer.common.LogUtil;
 import com.android.dialer.common.concurrent.DialerExecutorComponent;
 import com.android.dialer.common.concurrent.ThreadUtil;
+import com.android.dialer.common.concurrent.UiListener;
 import com.android.dialer.configprovider.ConfigProviderBindings;
 import com.android.dialer.logging.DialerImpression.Type;
 import com.android.dialer.logging.Logger;
@@ -71,6 +72,7 @@
 import com.android.dialer.metrics.MetricsComponent;
 import com.android.dialer.preferredsim.PreferredAccountRecorder;
 import com.android.dialer.preferredsim.PreferredAccountWorker;
+import com.android.dialer.preferredsim.PreferredAccountWorker.Result;
 import com.android.dialer.preferredsim.suggestion.SuggestionProvider;
 import com.android.dialer.util.ViewUtil;
 import com.android.incallui.answer.bindings.AnswerBindings;
@@ -103,6 +105,7 @@
 import com.android.incallui.video.protocol.VideoCallScreenDelegate;
 import com.android.incallui.video.protocol.VideoCallScreenDelegateFactory;
 import com.google.common.base.Optional;
+import com.google.common.util.concurrent.ListenableFuture;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.util.ArrayList;
@@ -132,6 +135,7 @@
   private static Optional<Integer> audioRouteForTesting = Optional.absent();
 
   private SelectPhoneAccountListener selectPhoneAccountListener;
+  private UiListener<Result> preferredAccountWorkerResultListener;
 
   private Animation dialpadSlideInAnimation;
   private Animation dialpadSlideOutAnimation;
@@ -188,6 +192,10 @@
     Trace.beginSection("InCallActivity.onCreate");
     super.onCreate(bundle);
 
+    preferredAccountWorkerResultListener =
+        DialerExecutorComponent.get(this)
+            .createUiListener(getFragmentManager(), "preferredAccountWorkerResultListener");
+
     selectPhoneAccountListener = new SelectPhoneAccountListener(getApplicationContext());
 
     if (bundle != null) {
@@ -360,72 +368,84 @@
       return false;
     }
 
-    DialerExecutorComponent.get(this)
-        .dialerExecutorFactory()
-        .createNonUiTaskBuilder(new PreferredAccountWorker(waitingForAccountCall.getNumber()))
-        .onSuccess(
-            (result -> {
-              if (result.getPhoneAccountHandle().isPresent()) {
-                Logger.get(this).logImpression(Type.DUAL_SIM_SELECTION_PREFERRED_USED);
-                selectPhoneAccountListener.onPhoneAccountSelected(
-                    result.getPhoneAccountHandle().get(), false, waitingForAccountCall.getId());
-                return;
-              }
-              if (result.getSuggestion().isPresent()) {
-                LogUtil.i(
-                    "CallingAccountSelector.processPreferredAccount",
-                    "SIM suggested: " + result.getSuggestion().get().reason);
-                if (result.getSuggestion().get().shouldAutoSelect) {
-                  Logger.get(this).logImpression(Type.DUAL_SIM_SELECTION_SUGGESTION_AUTO_SELECTED);
-                  LogUtil.i(
-                      "CallingAccountSelector.processPreferredAccount", "Auto selected suggestion");
-                  selectPhoneAccountListener.onPhoneAccountSelected(
-                      result.getSuggestion().get().phoneAccountHandle,
-                      false,
-                      waitingForAccountCall.getId());
-                  return;
-                }
-              }
-              Bundle extras = waitingForAccountCall.getIntentExtras();
-              List<PhoneAccountHandle> phoneAccountHandles =
-                  extras == null
-                      ? new ArrayList<>()
-                      : extras.getParcelableArrayList(Call.AVAILABLE_PHONE_ACCOUNTS);
+    ListenableFuture<PreferredAccountWorker.Result> preferredAccountFuture =
+        DialerExecutorComponent.get(this)
+            .backgroundExecutor()
+            .submit(
+                () -> {
+                  try {
+                    return new PreferredAccountWorker(waitingForAccountCall.getNumber())
+                        .doInBackground(getApplicationContext());
+                  } catch (Throwable throwable) {
+                    throw new Exception(throwable);
+                  }
+                });
 
-              waitingForAccountCall.setPreferredAccountRecorder(
-                  new PreferredAccountRecorder(
-                      waitingForAccountCall.getNumber(),
-                      result.getSuggestion().orNull(),
-                      result.getDataId().orNull()));
-              SelectPhoneAccountDialogOptions.Builder optionsBuilder =
-                  SelectPhoneAccountDialogOptions.newBuilder()
-                      .setTitle(R.string.select_phone_account_for_calls)
-                      .setCanSetDefault(result.getDataId().isPresent())
-                      .setSetDefaultLabel(R.string.select_phone_account_for_calls_remember)
-                      .setCallId(waitingForAccountCall.getId());
+    preferredAccountWorkerResultListener.listen(
+        this,
+        preferredAccountFuture,
+        result -> {
+          if (result.getPhoneAccountHandle().isPresent()) {
+            Logger.get(this).logImpression(Type.DUAL_SIM_SELECTION_PREFERRED_USED);
+            selectPhoneAccountListener.onPhoneAccountSelected(
+                result.getPhoneAccountHandle().get(), false, waitingForAccountCall.getId());
+            return;
+          }
+          if (result.getSuggestion().isPresent()) {
+            LogUtil.i(
+                "CallingAccountSelector.processPreferredAccount",
+                "SIM suggested: " + result.getSuggestion().get().reason);
+            if (result.getSuggestion().get().shouldAutoSelect) {
+              Logger.get(this).logImpression(Type.DUAL_SIM_SELECTION_SUGGESTION_AUTO_SELECTED);
+              LogUtil.i(
+                  "CallingAccountSelector.processPreferredAccount", "Auto selected suggestion");
+              selectPhoneAccountListener.onPhoneAccountSelected(
+                  result.getSuggestion().get().phoneAccountHandle,
+                  false,
+                  waitingForAccountCall.getId());
+              return;
+            }
+          }
+          Bundle extras = waitingForAccountCall.getIntentExtras();
+          List<PhoneAccountHandle> phoneAccountHandles =
+              extras == null
+                  ? new ArrayList<>()
+                  : extras.getParcelableArrayList(Call.AVAILABLE_PHONE_ACCOUNTS);
 
-              for (PhoneAccountHandle phoneAccountHandle : phoneAccountHandles) {
-                SelectPhoneAccountDialogOptions.Entry.Builder entryBuilder =
-                    SelectPhoneAccountDialogOptions.Entry.newBuilder();
-                SelectPhoneAccountDialogOptionsUtil.setPhoneAccountHandle(
-                    entryBuilder, phoneAccountHandle);
-                Optional<String> hint =
-                    SuggestionProvider.getHint(
-                        this, phoneAccountHandle, result.getSuggestion().orNull());
-                if (hint.isPresent()) {
-                  entryBuilder.setHint(hint.get());
-                }
-                optionsBuilder.addEntries(entryBuilder);
-              }
+          waitingForAccountCall.setPreferredAccountRecorder(
+              new PreferredAccountRecorder(
+                  waitingForAccountCall.getNumber(),
+                  result.getSuggestion().orNull(),
+                  result.getDataId().orNull()));
+          SelectPhoneAccountDialogOptions.Builder optionsBuilder =
+              SelectPhoneAccountDialogOptions.newBuilder()
+                  .setTitle(R.string.select_phone_account_for_calls)
+                  .setCanSetDefault(result.getDataId().isPresent())
+                  .setSetDefaultLabel(R.string.select_phone_account_for_calls_remember)
+                  .setCallId(waitingForAccountCall.getId());
 
-              selectPhoneAccountDialogFragment =
-                  SelectPhoneAccountDialogFragment.newInstance(
-                      optionsBuilder.build(), selectPhoneAccountListener);
-              selectPhoneAccountDialogFragment.show(
-                  getFragmentManager(), Tags.SELECT_ACCOUNT_FRAGMENT);
-            }))
-        .build()
-        .executeParallel(this);
+          for (PhoneAccountHandle phoneAccountHandle : phoneAccountHandles) {
+            SelectPhoneAccountDialogOptions.Entry.Builder entryBuilder =
+                SelectPhoneAccountDialogOptions.Entry.newBuilder();
+            SelectPhoneAccountDialogOptionsUtil.setPhoneAccountHandle(
+                entryBuilder, phoneAccountHandle);
+            Optional<String> hint =
+                SuggestionProvider.getHint(
+                    this, phoneAccountHandle, result.getSuggestion().orNull());
+            if (hint.isPresent()) {
+              entryBuilder.setHint(hint.get());
+            }
+            optionsBuilder.addEntries(entryBuilder);
+          }
+
+          selectPhoneAccountDialogFragment =
+              SelectPhoneAccountDialogFragment.newInstance(
+                  optionsBuilder.build(), selectPhoneAccountListener);
+          selectPhoneAccountDialogFragment.show(getFragmentManager(), Tags.SELECT_ACCOUNT_FRAGMENT);
+        },
+        throwable -> {
+          throw new RuntimeException(throwable);
+        });
 
     return true;
   }