Search in sources :

Example 1 with RecommendationRequest

use of android.net.RecommendationRequest in project platform_frameworks_base by android.

the class NetworkScoreService method requestRecommendationAsync.

/**
     * Request a recommendation for the best network to connect to
     * taking into account the inputs from the {@link RecommendationRequest}.
     *
     * @param request a {@link RecommendationRequest} instance containing the details of the request
     * @param remoteCallback a {@link IRemoteCallback} instance to invoke when the recommendation
     *                       is available.
     * @throws SecurityException if the caller is not the system
     */
@Override
public void requestRecommendationAsync(RecommendationRequest request, RemoteCallback remoteCallback) {
    mContext.enforceCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES, TAG);
    final OneTimeCallback oneTimeCallback = new OneTimeCallback(remoteCallback);
    final Pair<RecommendationRequest, OneTimeCallback> pair = Pair.create(request, oneTimeCallback);
    final Message timeoutMsg = mHandler.obtainMessage(ServiceHandler.MSG_RECOMMENDATION_REQUEST_TIMEOUT, pair);
    final INetworkRecommendationProvider provider = getRecommendationProvider();
    final long token = Binder.clearCallingIdentity();
    try {
        if (provider != null) {
            try {
                mHandler.sendMessageDelayed(timeoutMsg, mRecommendationRequestTimeoutMs);
                provider.requestRecommendation(request, new IRemoteCallback.Stub() {

                    @Override
                    public void sendResult(Bundle data) throws RemoteException {
                        // Remove the timeout message
                        mHandler.removeMessages(timeoutMsg.what, pair);
                        oneTimeCallback.sendResult(data);
                    }
                }, 0);
                return;
            } catch (RemoteException e) {
                Log.w(TAG, "Failed to request a recommendation.", e);
                // TODO(jjoslin): 12/15/16 - Keep track of failures.
                // Remove the timeout message
                mHandler.removeMessages(timeoutMsg.what, pair);
            // Will fall through and send back the default recommendation.
            }
        }
    } finally {
        Binder.restoreCallingIdentity(token);
    }
    // Else send back the default recommendation.
    sendDefaultRecommendationResponse(request, oneTimeCallback);
}
Also used : IRemoteCallback(android.os.IRemoteCallback) Message(android.os.Message) Bundle(android.os.Bundle) INetworkRecommendationProvider(android.net.INetworkRecommendationProvider) RecommendationRequest(android.net.RecommendationRequest) RemoteException(android.os.RemoteException)

Aggregations

INetworkRecommendationProvider (android.net.INetworkRecommendationProvider)1 RecommendationRequest (android.net.RecommendationRequest)1 Bundle (android.os.Bundle)1 IRemoteCallback (android.os.IRemoteCallback)1 Message (android.os.Message)1 RemoteException (android.os.RemoteException)1