summaryrefslogtreecommitdiff
path: root/java
diff options
context:
space:
mode:
authorzachh <zachh@google.com>2017-12-06 18:06:06 -0800
committerCopybara-Service <copybara-piper@google.com>2017-12-07 18:29:07 -0800
commit0c0c0a318d72e4b6f24514c4c6b3a173e249297d (patch)
tree76408b119a13584de5845a147feab6c9f485746a /java
parent32632f8cbed8ec7651e7c831b4defdfd4d81e70d (diff)
Added bindings for ListeningExecutorServices.
Use them where appropriate. Bug: 34672501 Test: existing PiperOrigin-RevId: 178182298 Change-Id: If454225e0d636c7cb14b5af02d46780d7732abf0
Diffstat (limited to 'java')
-rw-r--r--java/com/android/dialer/calllog/RefreshAnnotatedCallLogWorker.java35
-rw-r--r--java/com/android/dialer/calllog/database/MutationApplier.java12
-rw-r--r--java/com/android/dialer/calllog/datasources/phonelookup/PhoneLookupDataSource.java18
-rw-r--r--java/com/android/dialer/calllog/datasources/systemcalllog/SystemCallLogDataSource.java16
-rw-r--r--java/com/android/dialer/common/concurrent/Annotations.java8
-rw-r--r--java/com/android/dialer/common/concurrent/DefaultDialerExecutorFactory.java4
-rw-r--r--java/com/android/dialer/common/concurrent/DialerExecutorModule.java23
-rw-r--r--java/com/android/dialer/phonelookup/PhoneLookupModule.java9
-rw-r--r--java/com/android/dialer/phonelookup/composite/CompositePhoneLookup.java17
-rw-r--r--java/com/android/dialer/phonelookup/cp2/Cp2PhoneLookup.java39
10 files changed, 107 insertions, 74 deletions
diff --git a/java/com/android/dialer/calllog/RefreshAnnotatedCallLogWorker.java b/java/com/android/dialer/calllog/RefreshAnnotatedCallLogWorker.java
index de8905db8..e5cc3eb89 100644
--- a/java/com/android/dialer/calllog/RefreshAnnotatedCallLogWorker.java
+++ b/java/com/android/dialer/calllog/RefreshAnnotatedCallLogWorker.java
@@ -23,7 +23,8 @@ import com.android.dialer.calllog.datasources.CallLogDataSource;
import com.android.dialer.calllog.datasources.CallLogMutations;
import com.android.dialer.calllog.datasources.DataSources;
import com.android.dialer.common.LogUtil;
-import com.android.dialer.common.concurrent.Annotations.NonUiParallel;
+import com.android.dialer.common.concurrent.Annotations.BackgroundExecutor;
+import com.android.dialer.common.concurrent.Annotations.LightweightExecutor;
import com.android.dialer.common.concurrent.DialerFutureSerializer;
import com.android.dialer.common.concurrent.DialerFutures;
import com.android.dialer.inject.ApplicationContext;
@@ -32,10 +33,8 @@ import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
-import com.google.common.util.concurrent.MoreExecutors;
import java.util.ArrayList;
import java.util.List;
-import java.util.concurrent.ExecutorService;
import javax.inject.Inject;
import javax.inject.Singleton;
@@ -46,7 +45,8 @@ public class RefreshAnnotatedCallLogWorker {
private final Context appContext;
private final DataSources dataSources;
private final SharedPreferences sharedPreferences;
- private final ListeningExecutorService parallelUiListeningExecutorService;
+ private final ListeningExecutorService backgroundExecutorService;
+ private final ListeningExecutorService lightweightExecutorService;
// Used to ensure that only one refresh flow runs at a time. (Note that
// RefreshAnnotatedCallLogWorker is a @Singleton.)
private final DialerFutureSerializer dialerFutureSerializer = new DialerFutureSerializer();
@@ -56,14 +56,13 @@ public class RefreshAnnotatedCallLogWorker {
@ApplicationContext Context appContext,
DataSources dataSources,
@Unencrypted SharedPreferences sharedPreferences,
- @NonUiParallel ExecutorService parallelUiExecutorService) {
+ @BackgroundExecutor ListeningExecutorService backgroundExecutorService,
+ @LightweightExecutor ListeningExecutorService lightweightExecutorService) {
this.appContext = appContext;
this.dataSources = dataSources;
this.sharedPreferences = sharedPreferences;
-
- // TODO(zachh): Create and use bindings for ListeningExecutorServices.
- this.parallelUiListeningExecutorService =
- MoreExecutors.listeningDecorator(parallelUiExecutorService);
+ this.backgroundExecutorService = backgroundExecutorService;
+ this.lightweightExecutorService = lightweightExecutorService;
}
/** Checks if the annotated call log is dirty and refreshes it if necessary. */
@@ -78,16 +77,14 @@ public class RefreshAnnotatedCallLogWorker {
private ListenableFuture<Void> refresh(boolean checkDirty) {
LogUtil.i("RefreshAnnotatedCallLogWorker.refresh", "submitting serialized refresh request");
- // Note: directExecutor is safe to use here and throughout because all methods are async.
return dialerFutureSerializer.submitAsync(
- () -> checkDirtyAndRebuildIfNecessary(appContext, checkDirty),
- MoreExecutors.directExecutor());
+ () -> checkDirtyAndRebuildIfNecessary(appContext, checkDirty), lightweightExecutorService);
}
private ListenableFuture<Void> checkDirtyAndRebuildIfNecessary(
Context appContext, boolean checkDirty) {
ListenableFuture<Boolean> forceRebuildFuture =
- parallelUiListeningExecutorService.submit(
+ backgroundExecutorService.submit(
() -> {
LogUtil.i(
"RefreshAnnotatedCallLogWorker.checkDirtyAndRebuildIfNecessary",
@@ -115,7 +112,7 @@ public class RefreshAnnotatedCallLogWorker {
Preconditions.checkNotNull(forceRebuild)
? Futures.immediateFuture(true)
: isDirty(appContext),
- MoreExecutors.directExecutor());
+ lightweightExecutorService);
// After determining isDirty, conditionally call rebuild.
return Futures.transformAsync(
@@ -124,7 +121,7 @@ public class RefreshAnnotatedCallLogWorker {
Preconditions.checkNotNull(isDirty)
? rebuild(appContext)
: Futures.immediateFuture(null),
- MoreExecutors.directExecutor());
+ lightweightExecutorService);
}
private ListenableFuture<Boolean> isDirty(Context appContext) {
@@ -151,7 +148,7 @@ public class RefreshAnnotatedCallLogWorker {
Futures.transformAsync(
fillFuture,
unused -> dataSource.fill(appContext, mutations),
- MoreExecutors.directExecutor());
+ lightweightExecutorService);
}
// After all data sources are filled, apply mutations (at this point "fillFuture" is the result
@@ -163,7 +160,7 @@ public class RefreshAnnotatedCallLogWorker {
CallLogDatabaseComponent.get(appContext)
.mutationApplier()
.applyToDatabase(mutations, appContext),
- MoreExecutors.directExecutor());
+ lightweightExecutorService);
// After mutations applied, call onSuccessfulFill for each data source (in parallel).
ListenableFuture<List<Void>> onSuccessfulFillFuture =
@@ -177,7 +174,7 @@ public class RefreshAnnotatedCallLogWorker {
}
return Futures.allAsList(onSuccessfulFillFutures);
},
- MoreExecutors.directExecutor());
+ lightweightExecutorService);
// After onSuccessfulFill is called for every data source, write the shared pref.
return Futures.transform(
@@ -186,6 +183,6 @@ public class RefreshAnnotatedCallLogWorker {
sharedPreferences.edit().putBoolean(CallLogFramework.PREF_FORCE_REBUILD, false).apply();
return null;
},
- parallelUiListeningExecutorService);
+ backgroundExecutorService);
}
}
diff --git a/java/com/android/dialer/calllog/database/MutationApplier.java b/java/com/android/dialer/calllog/database/MutationApplier.java
index 720daec54..eee810eb8 100644
--- a/java/com/android/dialer/calllog/database/MutationApplier.java
+++ b/java/com/android/dialer/calllog/database/MutationApplier.java
@@ -28,25 +28,23 @@ import com.android.dialer.calllog.database.contract.AnnotatedCallLogContract.Ann
import com.android.dialer.calllog.datasources.CallLogMutations;
import com.android.dialer.common.Assert;
import com.android.dialer.common.LogUtil;
-import com.android.dialer.common.concurrent.Annotations.NonUiParallel;
+import com.android.dialer.common.concurrent.Annotations.BackgroundExecutor;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
-import com.google.common.util.concurrent.MoreExecutors;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map.Entry;
-import java.util.concurrent.ExecutorService;
import javax.inject.Inject;
/** Applies {@link CallLogMutations} to the annotated call log. */
public class MutationApplier {
- private final ListeningExecutorService executorService;
+ private final ListeningExecutorService backgroundExecutorService;
@Inject
- MutationApplier(@NonUiParallel ExecutorService executorService) {
- this.executorService = MoreExecutors.listeningDecorator(executorService);
+ MutationApplier(@BackgroundExecutor ListeningExecutorService backgroundExecutorService) {
+ this.backgroundExecutorService = backgroundExecutorService;
}
/** Applies the provided {@link CallLogMutations} to the annotated call log. */
@@ -54,7 +52,7 @@ public class MutationApplier {
if (mutations.isEmpty()) {
return Futures.immediateFuture(null);
}
- return executorService.submit(
+ return backgroundExecutorService.submit(
() -> {
applyToDatabaseInternal(mutations, appContext);
return null;
diff --git a/java/com/android/dialer/calllog/datasources/phonelookup/PhoneLookupDataSource.java b/java/com/android/dialer/calllog/datasources/phonelookup/PhoneLookupDataSource.java
index 41eaf2bae..9b90ad5cc 100644
--- a/java/com/android/dialer/calllog/datasources/phonelookup/PhoneLookupDataSource.java
+++ b/java/com/android/dialer/calllog/datasources/phonelookup/PhoneLookupDataSource.java
@@ -29,7 +29,7 @@ import com.android.dialer.calllog.database.contract.AnnotatedCallLogContract.Ann
import com.android.dialer.calllog.datasources.CallLogDataSource;
import com.android.dialer.calllog.datasources.CallLogMutations;
import com.android.dialer.common.LogUtil;
-import com.android.dialer.common.concurrent.Annotations.NonUiParallel;
+import com.android.dialer.common.concurrent.Annotations.BackgroundExecutor;
import com.android.dialer.phonelookup.PhoneLookup;
import com.android.dialer.phonelookup.PhoneLookupInfo;
import com.android.dialer.phonelookup.PhoneLookupSelector;
@@ -40,7 +40,6 @@ import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
-import com.google.common.util.concurrent.MoreExecutors;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.protobuf.InvalidProtocolBufferException;
import java.util.Arrays;
@@ -49,7 +48,6 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ExecutionException;
-import java.util.concurrent.ExecutorService;
import javax.inject.Inject;
/**
@@ -59,27 +57,29 @@ import javax.inject.Inject;
public final class PhoneLookupDataSource implements CallLogDataSource {
private final PhoneLookup phoneLookup;
- private final ListeningExecutorService executorService;
+ private final ListeningExecutorService backgroundExecutorService;
@Inject
- PhoneLookupDataSource(PhoneLookup phoneLookup, @NonUiParallel ExecutorService executorService) {
+ PhoneLookupDataSource(
+ PhoneLookup phoneLookup,
+ @BackgroundExecutor ListeningExecutorService backgroundExecutorService) {
this.phoneLookup = phoneLookup;
- this.executorService = MoreExecutors.listeningDecorator(executorService);
+ this.backgroundExecutorService = backgroundExecutorService;
}
@Override
public ListenableFuture<Boolean> isDirty(Context appContext) {
- return executorService.submit(() -> isDirtyInternal(appContext));
+ return backgroundExecutorService.submit(() -> isDirtyInternal(appContext));
}
@Override
public ListenableFuture<Void> fill(Context appContext, CallLogMutations mutations) {
- return executorService.submit(() -> fillInternal(appContext, mutations));
+ return backgroundExecutorService.submit(() -> fillInternal(appContext, mutations));
}
@Override
public ListenableFuture<Void> onSuccessfulFill(Context appContext) {
- return executorService.submit(this::onSuccessfulFillInternal);
+ return backgroundExecutorService.submit(this::onSuccessfulFillInternal);
}
@WorkerThread
diff --git a/java/com/android/dialer/calllog/datasources/systemcalllog/SystemCallLogDataSource.java b/java/com/android/dialer/calllog/datasources/systemcalllog/SystemCallLogDataSource.java
index dfc768c0a..91db915ef 100644
--- a/java/com/android/dialer/calllog/datasources/systemcalllog/SystemCallLogDataSource.java
+++ b/java/com/android/dialer/calllog/datasources/systemcalllog/SystemCallLogDataSource.java
@@ -45,7 +45,7 @@ import com.android.dialer.calllog.datasources.util.RowCombiner;
import com.android.dialer.calllogutils.PhoneAccountUtils;
import com.android.dialer.common.Assert;
import com.android.dialer.common.LogUtil;
-import com.android.dialer.common.concurrent.Annotations.NonUiParallel;
+import com.android.dialer.common.concurrent.Annotations.BackgroundExecutor;
import com.android.dialer.common.concurrent.ThreadUtil;
import com.android.dialer.phonenumberproto.DialerPhoneNumberUtil;
import com.android.dialer.storage.StorageComponent;
@@ -53,12 +53,10 @@ import com.android.dialer.theme.R;
import com.android.dialer.util.PermissionsUtil;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
-import com.google.common.util.concurrent.MoreExecutors;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
-import java.util.concurrent.ExecutorService;
import javax.inject.Inject;
/**
@@ -71,13 +69,13 @@ public class SystemCallLogDataSource implements CallLogDataSource {
@VisibleForTesting
static final String PREF_LAST_TIMESTAMP_PROCESSED = "systemCallLogLastTimestampProcessed";
- private final ListeningExecutorService executorService;
+ private final ListeningExecutorService backgroundExecutorService;
@Nullable private Long lastTimestampProcessed;
@Inject
- SystemCallLogDataSource(@NonUiParallel ExecutorService executorService) {
- this.executorService = MoreExecutors.listeningDecorator(executorService);
+ SystemCallLogDataSource(@BackgroundExecutor ListeningExecutorService backgroundExecutorService) {
+ this.backgroundExecutorService = backgroundExecutorService;
}
@MainThread
@@ -105,17 +103,17 @@ public class SystemCallLogDataSource implements CallLogDataSource {
@Override
public ListenableFuture<Boolean> isDirty(Context appContext) {
- return executorService.submit(() -> isDirtyInternal(appContext));
+ return backgroundExecutorService.submit(() -> isDirtyInternal(appContext));
}
@Override
public ListenableFuture<Void> fill(Context appContext, CallLogMutations mutations) {
- return executorService.submit(() -> fillInternal(appContext, mutations));
+ return backgroundExecutorService.submit(() -> fillInternal(appContext, mutations));
}
@Override
public ListenableFuture<Void> onSuccessfulFill(Context appContext) {
- return executorService.submit(() -> onSuccessfulFillInternal(appContext));
+ return backgroundExecutorService.submit(() -> onSuccessfulFillInternal(appContext));
}
@WorkerThread
diff --git a/java/com/android/dialer/common/concurrent/Annotations.java b/java/com/android/dialer/common/concurrent/Annotations.java
index 5e3954cf9..62d5b318e 100644
--- a/java/com/android/dialer/common/concurrent/Annotations.java
+++ b/java/com/android/dialer/common/concurrent/Annotations.java
@@ -39,4 +39,12 @@ public class Annotations {
/** Annotation for retrieving the UI serial executor. */
@Qualifier
public @interface UiSerial {}
+
+ /** Annotation for retrieving the lightweight executor. */
+ @Qualifier
+ public @interface LightweightExecutor {}
+
+ /** Annotation for retrieving the background executor. */
+ @Qualifier
+ public @interface BackgroundExecutor {}
}
diff --git a/java/com/android/dialer/common/concurrent/DefaultDialerExecutorFactory.java b/java/com/android/dialer/common/concurrent/DefaultDialerExecutorFactory.java
index ab01654aa..317807b1d 100644
--- a/java/com/android/dialer/common/concurrent/DefaultDialerExecutorFactory.java
+++ b/java/com/android/dialer/common/concurrent/DefaultDialerExecutorFactory.java
@@ -40,14 +40,14 @@ import javax.inject.Inject;
public class DefaultDialerExecutorFactory implements DialerExecutorFactory {
private final ExecutorService nonUiThreadPool;
private final ScheduledExecutorService nonUiSerialExecutor;
- private final Executor uiThreadPool;
+ private final ExecutorService uiThreadPool;
private final ScheduledExecutorService uiSerialExecutor;
@Inject
DefaultDialerExecutorFactory(
@NonUiParallel ExecutorService nonUiThreadPool,
@NonUiSerial ScheduledExecutorService nonUiSerialExecutor,
- @UiParallel Executor uiThreadPool,
+ @UiParallel ExecutorService uiThreadPool,
@UiSerial ScheduledExecutorService uiSerialExecutor) {
this.nonUiThreadPool = nonUiThreadPool;
this.nonUiSerialExecutor = nonUiSerialExecutor;
diff --git a/java/com/android/dialer/common/concurrent/DialerExecutorModule.java b/java/com/android/dialer/common/concurrent/DialerExecutorModule.java
index 5e0190e8d..98738ed37 100644
--- a/java/com/android/dialer/common/concurrent/DialerExecutorModule.java
+++ b/java/com/android/dialer/common/concurrent/DialerExecutorModule.java
@@ -17,16 +17,18 @@ package com.android.dialer.common.concurrent;
import android.os.AsyncTask;
import com.android.dialer.common.LogUtil;
+import com.android.dialer.common.concurrent.Annotations.BackgroundExecutor;
+import com.android.dialer.common.concurrent.Annotations.LightweightExecutor;
import com.android.dialer.common.concurrent.Annotations.NonUiParallel;
import com.android.dialer.common.concurrent.Annotations.NonUiSerial;
import com.android.dialer.common.concurrent.Annotations.Ui;
import com.android.dialer.common.concurrent.Annotations.UiParallel;
import com.android.dialer.common.concurrent.Annotations.UiSerial;
import com.google.common.util.concurrent.ListeningExecutorService;
+import com.google.common.util.concurrent.MoreExecutors;
import dagger.Binds;
import dagger.Module;
import dagger.Provides;
-import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
@@ -85,8 +87,8 @@ public abstract class DialerExecutorModule {
@Provides
@UiParallel
- static Executor provideUiThreadPool() {
- return AsyncTask.THREAD_POOL_EXECUTOR;
+ static ExecutorService provideUiThreadPool() {
+ return (ExecutorService) AsyncTask.THREAD_POOL_EXECUTOR;
}
@Provides
@@ -105,4 +107,19 @@ public abstract class DialerExecutorModule {
}
});
}
+
+ @Provides
+ @Singleton
+ @LightweightExecutor
+ static ListeningExecutorService provideLightweightExecutor(@UiParallel ExecutorService delegate) {
+ return MoreExecutors.listeningDecorator(delegate);
+ }
+
+ @Provides
+ @Singleton
+ @BackgroundExecutor
+ static ListeningExecutorService provideBackgroundExecutor(
+ @NonUiParallel ExecutorService delegate) {
+ return MoreExecutors.listeningDecorator(delegate);
+ }
}
diff --git a/java/com/android/dialer/phonelookup/PhoneLookupModule.java b/java/com/android/dialer/phonelookup/PhoneLookupModule.java
index 400caff3a..39b0a5083 100644
--- a/java/com/android/dialer/phonelookup/PhoneLookupModule.java
+++ b/java/com/android/dialer/phonelookup/PhoneLookupModule.java
@@ -27,7 +27,12 @@ import dagger.Provides;
public abstract class PhoneLookupModule {
@Provides
- static PhoneLookup providePhoneLookup(Cp2PhoneLookup cp2PhoneLookup) {
- return new CompositePhoneLookup(ImmutableList.of(cp2PhoneLookup));
+ static ImmutableList<PhoneLookup> providePhoneLookupList(Cp2PhoneLookup cp2PhoneLookup) {
+ return ImmutableList.of(cp2PhoneLookup);
+ }
+
+ @Provides
+ static PhoneLookup providePhoneLookup(CompositePhoneLookup compositePhoneLookup) {
+ return compositePhoneLookup;
}
}
diff --git a/java/com/android/dialer/phonelookup/composite/CompositePhoneLookup.java b/java/com/android/dialer/phonelookup/composite/CompositePhoneLookup.java
index f432e27ae..abc0c74fd 100644
--- a/java/com/android/dialer/phonelookup/composite/CompositePhoneLookup.java
+++ b/java/com/android/dialer/phonelookup/composite/CompositePhoneLookup.java
@@ -20,6 +20,7 @@ import android.support.annotation.NonNull;
import android.telecom.Call;
import com.android.dialer.DialerPhoneNumber;
import com.android.dialer.common.LogUtil;
+import com.android.dialer.common.concurrent.Annotations.LightweightExecutor;
import com.android.dialer.common.concurrent.DialerFutures;
import com.android.dialer.phonelookup.PhoneLookup;
import com.android.dialer.phonelookup.PhoneLookupInfo;
@@ -29,9 +30,10 @@ import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
-import com.google.common.util.concurrent.MoreExecutors;
+import com.google.common.util.concurrent.ListeningExecutorService;
import java.util.ArrayList;
import java.util.List;
+import javax.inject.Inject;
/**
* {@link PhoneLookup} which delegates to a configured set of {@link PhoneLookup PhoneLookups},
@@ -40,9 +42,14 @@ import java.util.List;
public final class CompositePhoneLookup implements PhoneLookup {
private final ImmutableList<PhoneLookup> phoneLookups;
+ private final ListeningExecutorService lightweightExecutorService;
- public CompositePhoneLookup(ImmutableList<PhoneLookup> phoneLookups) {
+ @Inject
+ CompositePhoneLookup(
+ ImmutableList<PhoneLookup> phoneLookups,
+ @LightweightExecutor ListeningExecutorService lightweightExecutorService) {
this.phoneLookups = phoneLookups;
+ this.lightweightExecutorService = lightweightExecutorService;
}
/**
@@ -68,7 +75,7 @@ public final class CompositePhoneLookup implements PhoneLookup {
}
return mergedInfo.build();
},
- MoreExecutors.directExecutor());
+ lightweightExecutorService);
}
@Override
@@ -117,7 +124,7 @@ public final class CompositePhoneLookup implements PhoneLookup {
}
return combinedMap.build();
},
- MoreExecutors.directExecutor());
+ lightweightExecutorService);
}
@Override
@@ -127,6 +134,6 @@ public final class CompositePhoneLookup implements PhoneLookup {
futures.add(phoneLookup.onSuccessfulBulkUpdate());
}
return Futures.transform(
- Futures.allAsList(futures), unused -> null, MoreExecutors.directExecutor());
+ Futures.allAsList(futures), unused -> null, lightweightExecutorService);
}
}
diff --git a/java/com/android/dialer/phonelookup/cp2/Cp2PhoneLookup.java b/java/com/android/dialer/phonelookup/cp2/Cp2PhoneLookup.java
index cfb8fb7b8..cd645a447 100644
--- a/java/com/android/dialer/phonelookup/cp2/Cp2PhoneLookup.java
+++ b/java/com/android/dialer/phonelookup/cp2/Cp2PhoneLookup.java
@@ -30,6 +30,7 @@ import android.telecom.Call;
import android.text.TextUtils;
import com.android.dialer.DialerPhoneNumber;
import com.android.dialer.common.Assert;
+import com.android.dialer.common.concurrent.Annotations.BackgroundExecutor;
import com.android.dialer.inject.ApplicationContext;
import com.android.dialer.phonelookup.PhoneLookup;
import com.android.dialer.phonelookup.PhoneLookupInfo;
@@ -39,7 +40,7 @@ import com.android.dialer.storage.Unencrypted;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.util.concurrent.ListenableFuture;
-import com.google.common.util.concurrent.MoreExecutors;
+import com.google.common.util.concurrent.ListeningExecutorService;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
@@ -71,25 +72,29 @@ public final class Cp2PhoneLookup implements PhoneLookup {
private final Context appContext;
private final SharedPreferences sharedPreferences;
+ private final ListeningExecutorService backgroundExecutorService;
+
@Nullable private Long currentLastTimestampProcessed;
@Inject
Cp2PhoneLookup(
- @ApplicationContext Context appContext, @Unencrypted SharedPreferences sharedPreferences) {
+ @ApplicationContext Context appContext,
+ @Unencrypted SharedPreferences sharedPreferences,
+ @BackgroundExecutor ListeningExecutorService backgroundExecutorService) {
this.appContext = appContext;
this.sharedPreferences = sharedPreferences;
+ this.backgroundExecutorService = backgroundExecutorService;
}
@Override
public ListenableFuture<PhoneLookupInfo> lookup(@NonNull Call call) {
// TODO(zachh): Implementation.
- return MoreExecutors.newDirectExecutorService().submit(PhoneLookupInfo::getDefaultInstance);
+ return backgroundExecutorService.submit(PhoneLookupInfo::getDefaultInstance);
}
@Override
public ListenableFuture<Boolean> isDirty(ImmutableSet<DialerPhoneNumber> phoneNumbers) {
- // TODO(calderwoodra): consider a different thread pool
- return MoreExecutors.newDirectExecutorService().submit(() -> isDirtyInternal(phoneNumbers));
+ return backgroundExecutorService.submit(() -> isDirtyInternal(phoneNumbers));
}
private boolean isDirtyInternal(ImmutableSet<DialerPhoneNumber> phoneNumbers) {
@@ -185,8 +190,7 @@ public final class Cp2PhoneLookup implements PhoneLookup {
@Override
public ListenableFuture<ImmutableMap<DialerPhoneNumber, PhoneLookupInfo>> bulkUpdate(
ImmutableMap<DialerPhoneNumber, PhoneLookupInfo> existingInfoMap) {
- return MoreExecutors.newDirectExecutorService()
- .submit(() -> bulkUpdateInternal(existingInfoMap));
+ return backgroundExecutorService.submit(() -> bulkUpdateInternal(existingInfoMap));
}
private ImmutableMap<DialerPhoneNumber, PhoneLookupInfo> bulkUpdateInternal(
@@ -234,17 +238,16 @@ public final class Cp2PhoneLookup implements PhoneLookup {
@Override
public ListenableFuture<Void> onSuccessfulBulkUpdate() {
- return MoreExecutors.newDirectExecutorService()
- .submit(
- () -> {
- if (currentLastTimestampProcessed != null) {
- sharedPreferences
- .edit()
- .putLong(PREF_LAST_TIMESTAMP_PROCESSED, currentLastTimestampProcessed)
- .apply();
- }
- return null;
- });
+ return backgroundExecutorService.submit(
+ () -> {
+ if (currentLastTimestampProcessed != null) {
+ sharedPreferences
+ .edit()
+ .putLong(PREF_LAST_TIMESTAMP_PROCESSED, currentLastTimestampProcessed)
+ .apply();
+ }
+ return null;
+ });
}
/**