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 android.telephony.ims.stub;
18 
19 import android.annotation.IntDef;
20 import android.os.RemoteException;
21 import android.telephony.ims.ImsException;
22 import android.telephony.ims.aidl.IRcsFeatureListener;
23 import android.telephony.ims.feature.ImsFeature;
24 import android.telephony.ims.feature.RcsFeature;
25 
26 import java.lang.annotation.Retention;
27 import java.lang.annotation.RetentionPolicy;
28 
29 /**
30  * Base class for different types of Capability exchange, presence using
31  * {@link RcsPresenceExchangeImplBase} and SIP OPTIONS exchange using {@link RcsSipOptionsImplBase}.
32  *
33  * @hide
34  */
35 public class RcsCapabilityExchange {
36 
37     /**  Service is unknown. */
38     public static final int COMMAND_CODE_SERVICE_UNKNOWN = 0;
39     /** The command completed successfully. */
40     public static final int COMMAND_CODE_SUCCESS = 1;
41     /** The command failed with an unknown error. */
42     public static final int COMMAND_CODE_GENERIC_FAILURE = 2;
43     /**  Invalid parameter(s). */
44     public static final int COMMAND_CODE_INVALID_PARAM = 3;
45     /**  Fetch error. */
46     public static final int COMMAND_CODE_FETCH_ERROR = 4;
47     /**  Request timed out. */
48     public static final int COMMAND_CODE_REQUEST_TIMEOUT = 5;
49     /**  Failure due to insufficient memory available. */
50     public static final int COMMAND_CODE_INSUFFICIENT_MEMORY = 6;
51     /**  Network connection is lost. */
52     public static final int COMMAND_CODE_LOST_NETWORK_CONNECTION = 7;
53     /**  Requested feature/resource is not supported. */
54     public static final int COMMAND_CODE_NOT_SUPPORTED = 8;
55     /**  Contact or resource is not found. */
56     public static final int COMMAND_CODE_NOT_FOUND = 9;
57     /**  Service is not available. */
58     public static final int COMMAND_CODE_SERVICE_UNAVAILABLE = 10;
59     /**  No Change in Capabilities */
60     public static final int COMMAND_CODE_NO_CHANGE_IN_CAP = 11;
61 
62     /** @hide*/
63     @Retention(RetentionPolicy.SOURCE)
64     @IntDef(prefix = "COMMAND_CODE_", value = {
65             COMMAND_CODE_SERVICE_UNKNOWN,
66             COMMAND_CODE_SUCCESS,
67             COMMAND_CODE_GENERIC_FAILURE,
68             COMMAND_CODE_INVALID_PARAM,
69             COMMAND_CODE_FETCH_ERROR,
70             COMMAND_CODE_REQUEST_TIMEOUT,
71             COMMAND_CODE_INSUFFICIENT_MEMORY,
72             COMMAND_CODE_LOST_NETWORK_CONNECTION,
73             COMMAND_CODE_NOT_SUPPORTED,
74             COMMAND_CODE_NOT_FOUND,
75             COMMAND_CODE_SERVICE_UNAVAILABLE,
76             COMMAND_CODE_NO_CHANGE_IN_CAP
77     })
78     public @interface CommandCode {}
79 
80 
81     private RcsFeature mFeature;
82 
83     /** @hide */
initialize(RcsFeature feature)84     public final void initialize(RcsFeature feature) {
85         mFeature = feature;
86     }
87 
88     /** @hide */
getListener()89     protected final IRcsFeatureListener getListener() throws ImsException {
90         IRcsFeatureListener listener = mFeature.getListener();
91         if (listener == null) {
92             throw new ImsException("Connection to Framework has not been established, wait for "
93                     + "onFeatureReady().", ImsException.CODE_ERROR_SERVICE_UNAVAILABLE);
94         }
95         return mFeature.getListener();
96     }
97 
98     /**
99      * Provides the framework with an update as to whether or not a command completed successfully
100      * locally. This includes capabilities requests and updates from the network. If it does not
101      * complete successfully, then the framework may retry the command again later, depending on the
102      * error. If the command does complete successfully, the framework will then wait for network
103      * updates.
104      *
105      * @param code The result of the pending command. If {@link #COMMAND_CODE_SUCCESS}, further
106      *             updates will be sent for this command using the associated operationToken.
107      * @param operationToken the token associated with the pending command.
108      * @throws ImsException If this {@link RcsCapabilityExchange} instance is not currently
109      * connected to the framework. This can happen if the {@link RcsFeature} is not
110      * {@link ImsFeature#STATE_READY} and the {@link RcsFeature} has not received the
111      * {@link ImsFeature#onFeatureReady()} callback. This may also happen in rare cases when the
112      * Telephony stack has crashed.
113      */
onCommandUpdate(@ommandCode int code, int operationToken)114     public final void onCommandUpdate(@CommandCode int code, int operationToken)
115             throws ImsException {
116         try {
117             getListener().onCommandUpdate(code, operationToken);
118         } catch (RemoteException e) {
119             throw new ImsException(e.getMessage(), ImsException.CODE_ERROR_SERVICE_UNAVAILABLE);
120         }
121     }
122 }
123