Merge "Have requestRecommendation return a CompletableFuture."
This commit is contained in:
@@ -21,6 +21,7 @@ import static android.net.NetworkRecommendationProvider.EXTRA_RECOMMENDATION_RES
|
|||||||
import android.Manifest;
|
import android.Manifest;
|
||||||
import android.annotation.IntDef;
|
import android.annotation.IntDef;
|
||||||
import android.annotation.NonNull;
|
import android.annotation.NonNull;
|
||||||
|
import android.annotation.Nullable;
|
||||||
import android.annotation.SdkConstant;
|
import android.annotation.SdkConstant;
|
||||||
import android.annotation.SdkConstant.SdkConstantType;
|
import android.annotation.SdkConstant.SdkConstantType;
|
||||||
import android.annotation.SystemApi;
|
import android.annotation.SystemApi;
|
||||||
@@ -37,6 +38,7 @@ import com.android.internal.util.Preconditions;
|
|||||||
|
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
import java.lang.annotation.RetentionPolicy;
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class that manages communication between network subsystems and a network scorer.
|
* Class that manages communication between network subsystems and a network scorer.
|
||||||
@@ -371,25 +373,26 @@ public class NetworkScoreManager {
|
|||||||
*
|
*
|
||||||
* @param request a {@link RecommendationRequest} instance containing additional
|
* @param request a {@link RecommendationRequest} instance containing additional
|
||||||
* request details
|
* request details
|
||||||
* @param callback a {@link RecommendationCallback} instance that will be invoked when
|
* @param handler a {@link Handler} instance representing the thread to complete the future on.
|
||||||
* the {@link RecommendationResult} is available
|
* If null the responding binder thread will be used.
|
||||||
* @param handler a {@link Handler} instance representing the thread to run the callback on.
|
* @return a {@link CompletableFuture} instance that will eventually receive the
|
||||||
|
* {@link RecommendationResult}.
|
||||||
* @throws SecurityException
|
* @throws SecurityException
|
||||||
* @hide
|
* @hide
|
||||||
*/
|
*/
|
||||||
public void requestRecommendation(
|
public CompletableFuture<RecommendationResult> requestRecommendation(
|
||||||
final @NonNull RecommendationRequest request,
|
final @NonNull RecommendationRequest request,
|
||||||
final @NonNull RecommendationCallback callback,
|
final @Nullable Handler handler) {
|
||||||
final @NonNull Handler handler) {
|
|
||||||
Preconditions.checkNotNull(request, "RecommendationRequest cannot be null.");
|
Preconditions.checkNotNull(request, "RecommendationRequest cannot be null.");
|
||||||
Preconditions.checkNotNull(callback, "RecommendationCallback cannot be null.");
|
|
||||||
Preconditions.checkNotNull(handler, "Handler cannot be null.");
|
final CompletableFuture<RecommendationResult> futureResult =
|
||||||
|
new CompletableFuture<>();
|
||||||
|
|
||||||
RemoteCallback remoteCallback = new RemoteCallback(new RemoteCallback.OnResultListener() {
|
RemoteCallback remoteCallback = new RemoteCallback(new RemoteCallback.OnResultListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onResult(Bundle data) {
|
public void onResult(Bundle data) {
|
||||||
RecommendationResult result = data.getParcelable(EXTRA_RECOMMENDATION_RESULT);
|
RecommendationResult result = data.getParcelable(EXTRA_RECOMMENDATION_RESULT);
|
||||||
callback.onRecommendationAvailable(result);
|
futureResult.complete(result);
|
||||||
}
|
}
|
||||||
}, handler);
|
}, handler);
|
||||||
|
|
||||||
@@ -398,18 +401,7 @@ public class NetworkScoreManager {
|
|||||||
} catch (RemoteException e) {
|
} catch (RemoteException e) {
|
||||||
throw e.rethrowFromSystemServer();
|
throw e.rethrowFromSystemServer();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
return futureResult;
|
||||||
* Callers of {@link #requestRecommendation(RecommendationRequest, RecommendationCallback, Handler)}
|
|
||||||
* must pass in an implementation of this class.
|
|
||||||
* @hide
|
|
||||||
*/
|
|
||||||
public abstract static class RecommendationCallback {
|
|
||||||
/**
|
|
||||||
* Invoked when a {@link RecommendationResult} is available.
|
|
||||||
* @param result a {@link RecommendationResult} instance.
|
|
||||||
*/
|
|
||||||
public abstract void onRecommendationAvailable(RecommendationResult result);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user