Home | History | Annotate | Download | only in telephony
      1 /*
      2  * Copyright 2017 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 android.telephony;
     18 
     19 import android.annotation.IntDef;
     20 import android.os.RemoteException;
     21 import android.telephony.NetworkService.NetworkServiceProvider;
     22 
     23 import java.lang.annotation.Retention;
     24 import java.lang.annotation.RetentionPolicy;
     25 import java.lang.ref.WeakReference;
     26 
     27 /**
     28  * Network service callback. Object of this class is passed to NetworkServiceProvider upon
     29  * calling getNetworkRegistrationState, to receive asynchronous feedback from NetworkServiceProvider
     30  * upon onGetNetworkRegistrationStateComplete. It's like a wrapper of INetworkServiceCallback
     31  * because INetworkServiceCallback can't be a parameter type in public APIs.
     32  *
     33  * @hide
     34  */
     35 public class NetworkServiceCallback {
     36 
     37     private static final String mTag = NetworkServiceCallback.class.getSimpleName();
     38 
     39     /**
     40      * Result of network requests
     41      * @hide
     42      */
     43     @Retention(RetentionPolicy.SOURCE)
     44     @IntDef({RESULT_SUCCESS, RESULT_ERROR_UNSUPPORTED, RESULT_ERROR_INVALID_ARG, RESULT_ERROR_BUSY,
     45             RESULT_ERROR_ILLEGAL_STATE, RESULT_ERROR_FAILED})
     46     public @interface Result {}
     47 
     48     /** Request is completed successfully */
     49     public static final int RESULT_SUCCESS              = 0;
     50     /** Request is not support */
     51     public static final int RESULT_ERROR_UNSUPPORTED    = 1;
     52     /** Request contains invalid arguments */
     53     public static final int RESULT_ERROR_INVALID_ARG    = 2;
     54     /** Service is busy */
     55     public static final int RESULT_ERROR_BUSY           = 3;
     56     /** Request sent in illegal state */
     57     public static final int RESULT_ERROR_ILLEGAL_STATE  = 4;
     58     /** Request failed */
     59     public static final int RESULT_ERROR_FAILED         = 5;
     60 
     61     private final WeakReference<INetworkServiceCallback> mCallback;
     62 
     63     /** @hide */
     64     public NetworkServiceCallback(INetworkServiceCallback callback) {
     65         mCallback = new WeakReference<>(callback);
     66     }
     67 
     68     /**
     69      * Called to indicate result of
     70      * {@link NetworkServiceProvider#getNetworkRegistrationState(int, NetworkServiceCallback)}
     71      *
     72      * @param result Result status like {@link NetworkServiceCallback#RESULT_SUCCESS} or
     73      *                {@link NetworkServiceCallback#RESULT_ERROR_UNSUPPORTED}
     74      * @param state The state information to be returned to callback.
     75      */
     76     public void onGetNetworkRegistrationStateComplete(int result, NetworkRegistrationState state) {
     77         INetworkServiceCallback callback = mCallback.get();
     78         if (callback != null) {
     79             try {
     80                 callback.onGetNetworkRegistrationStateComplete(result, state);
     81             } catch (RemoteException e) {
     82                 Rlog.e(mTag, "Failed to onGetNetworkRegistrationStateComplete on the remote");
     83             }
     84         } else {
     85             Rlog.e(mTag, "Weak reference of callback is null.");
     86         }
     87     }
     88 }