From 05b6a53db23cf8c2d90442d1c84c14fd09519390 Mon Sep 17 00:00:00 2001 From: zachh Date: Mon, 11 Dec 2017 13:57:57 -0800 Subject: Don't save UI executor in UiListener. When an activity is killed by the system, a new UiListener is created and attached, but it doesn't have its executor set. Rather than save it as an instance field, just get the Ui executor when needed, by passing the context to the listen method. Bug: 70510707 Test: unit and manual via "don't keep activities" developer option PiperOrigin-RevId: 178668338 Change-Id: I5360b525377edab5f3a117d1f0f50bf6da6a6f0c --- .../com/android/dialer/calllog/ui/NewCallLogFragment.java | 3 ++- .../dialer/common/concurrent/DialerExecutorComponent.java | 2 +- java/com/android/dialer/common/concurrent/UiListener.java | 15 ++++++++------- 3 files changed, 11 insertions(+), 9 deletions(-) (limited to 'java') diff --git a/java/com/android/dialer/calllog/ui/NewCallLogFragment.java b/java/com/android/dialer/calllog/ui/NewCallLogFragment.java index a5dccaf69..c10b52123 100644 --- a/java/com/android/dialer/calllog/ui/NewCallLogFragment.java +++ b/java/com/android/dialer/calllog/ui/NewCallLogFragment.java @@ -141,7 +141,8 @@ public final class NewCallLogFragment extends Fragment checkDirty ? refreshAnnotatedCallLogWorker.refreshWithDirtyCheck() : refreshAnnotatedCallLogWorker.refreshWithoutDirtyCheck(); - refreshAnnotatedCallLogListener.listen(future, unused -> {}, RuntimeException::new); + refreshAnnotatedCallLogListener.listen( + getContext(), future, unused -> {}, RuntimeException::new); }; ThreadUtil.getUiThreadHandler().postDelayed(refreshAnnotatedCallLogRunnable, WAIT_MILLIS); } diff --git a/java/com/android/dialer/common/concurrent/DialerExecutorComponent.java b/java/com/android/dialer/common/concurrent/DialerExecutorComponent.java index 7ee30a083..28abf96fd 100644 --- a/java/com/android/dialer/common/concurrent/DialerExecutorComponent.java +++ b/java/com/android/dialer/common/concurrent/DialerExecutorComponent.java @@ -36,7 +36,7 @@ public abstract class DialerExecutorComponent { public UiListener createUiListener( FragmentManager fragmentManager, String taskId) { - return UiListener.create(uiExecutorService(), fragmentManager, taskId); + return UiListener.create(fragmentManager, taskId); } @NonUiParallel diff --git a/java/com/android/dialer/common/concurrent/UiListener.java b/java/com/android/dialer/common/concurrent/UiListener.java index 11302d299..9541dbc0c 100644 --- a/java/com/android/dialer/common/concurrent/UiListener.java +++ b/java/com/android/dialer/common/concurrent/UiListener.java @@ -18,6 +18,7 @@ package com.android.dialer.common.concurrent; import android.app.Fragment; import android.app.FragmentManager; +import android.content.Context; import android.os.Bundle; import android.support.annotation.MainThread; import android.support.annotation.NonNull; @@ -29,7 +30,6 @@ import com.android.dialer.common.concurrent.DialerExecutor.SuccessListener; import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; -import java.util.concurrent.Executor; /** * A headless fragment for use in UI components that interact with ListenableFutures. @@ -53,19 +53,17 @@ import java.util.concurrent.Executor; * * private void userDidSomething() { * ListenableFuture<MyOutputType> future = callSomeMethodReturningListenableFuture(input); - * uiListener.listen(future, this::onSuccess, this::onFailure); + * uiListener.listen(this, future, this::onSuccess, this::onFailure); * } * } * */ public class UiListener extends Fragment { - private Executor uiThreadExecutor; private CallbackWrapper callbackWrapper; @MainThread - static UiListener create( - Executor uiThreadExecutor, FragmentManager fragmentManager, String taskId) { + static UiListener create(FragmentManager fragmentManager, String taskId) { @SuppressWarnings("unchecked") UiListener uiListener = (UiListener) fragmentManager.findFragmentByTag(taskId); @@ -73,7 +71,6 @@ public class UiListener extends Fragment { if (uiListener == null) { LogUtil.i("UiListener.create", "creating new UiListener for " + taskId); uiListener = new UiListener<>(); - uiListener.uiThreadExecutor = uiThreadExecutor; fragmentManager.beginTransaction().add(uiListener, taskId).commit(); } return uiListener; @@ -87,12 +84,16 @@ public class UiListener extends Fragment { */ @MainThread public void listen( + Context context, @NonNull ListenableFuture future, @NonNull SuccessListener successListener, @NonNull FailureListener failureListener) { callbackWrapper = new CallbackWrapper<>(Assert.isNotNull(successListener), Assert.isNotNull(failureListener)); - Futures.addCallback(Assert.isNotNull(future), callbackWrapper, uiThreadExecutor); + Futures.addCallback( + Assert.isNotNull(future), + callbackWrapper, + DialerExecutorComponent.get(context).uiExecutorService()); } private static class CallbackWrapper implements FutureCallback { -- cgit v1.2.3