Home | History | Annotate | Download | only in async
      1 /*
      2  * Copyright (C) 2018 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.internal.telephony.uicc.euicc.async;
     18 
     19 import android.os.AsyncResult;
     20 import android.os.Handler;
     21 import android.os.Message;
     22 
     23 /**
     24  * This class wraps an invocation to an asynchronous method using {@link Message} to be working with
     25  * {@link AsyncResultCallback}. With this class, you can use callbacks instead of managing a state
     26  * machine to complete a task relying on multiple asynchronous method calls.
     27  *
     28  * <p>Subclasses should override the abstract methods to invoke the actual asynchronous method and
     29  * parse the returned result.
     30  *
     31  * @param  Class of the request data.
     32  * @param  Class of the response data.
     33  *
     34  * @hide
     35  */
     36 public abstract class AsyncMessageInvocation<Request, Response> implements Handler.Callback {
     37     /**
     38      * Executes an invocation.
     39      *
     40      * @param request The request to be sent with the invocation.
     41      * @param resultCallback Will be called after result is returned.
     42      * @param handler The handler that {@code resultCallback} will be executed on.
     43      */
     44     public final void invoke(
     45             Request request, AsyncResultCallback<Response> resultCallback, Handler handler) {
     46         Handler h = new Handler(handler.getLooper(), this);
     47         sendRequestMessage(request, h.obtainMessage(0, resultCallback));
     48     }
     49 
     50     @SuppressWarnings("unchecked")
     51     @Override
     52     public boolean handleMessage(Message msg) {
     53         AsyncResult result = (AsyncResult) msg.obj;
     54         AsyncResultCallback<Response> resultCallback =
     55                 (AsyncResultCallback<Response>) result.userObj;
     56         try {
     57             resultCallback.onResult(parseResult(result));
     58         } catch (Throwable t) {
     59             resultCallback.onException(t);
     60         }
     61         return true;
     62     }
     63 
     64     /**
     65      * Calls the asynchronous method with the given {@code msg}. The implementation should convert
     66      * the given {@code request} to the parameters of the asynchronous method.
     67      */
     68     protected abstract void sendRequestMessage(Request request, Message msg);
     69 
     70     /** Parses the asynchronous result returned by the method to a {@link Response}. */
     71     protected abstract Response parseResult(AsyncResult result) throws Throwable;
     72 }
     73