aboutsummaryrefslogtreecommitdiff
path: root/src/java/com/android/ims/rcs/uce/presence/publish/PublishRequestResponse.java
blob: aa1b27c1c88b3c2b88ee6cc0f79d9d6c8d463663 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.ims.rcs.uce.presence.publish;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.telephony.ims.RcsUceAdapter;
import android.telephony.ims.SipDetails;
import android.telephony.ims.aidl.IPublishResponseCallback;
import android.telephony.ims.stub.RcsCapabilityExchangeImplBase;
import android.text.TextUtils;
import android.util.Log;

import com.android.ims.rcs.uce.presence.publish.PublishController.PublishControllerCallback;
import com.android.ims.rcs.uce.util.NetworkSipCode;
import com.android.ims.rcs.uce.util.UceUtils;

import java.time.Instant;
import java.util.Optional;

/**
 * Receiving the result callback of the publish request.
 */
public class PublishRequestResponse {

    private static final String LOG_TAG = UceUtils.getLogPrefix() + "PublishRequestResp";

    private final long mTaskId;
    private final String mPidfXml;
    private volatile boolean mNeedRetry;
    private volatile PublishControllerCallback mPublishCtrlCallback;

    private Optional<Integer> mCmdErrorCode;
    private Optional<Integer> mNetworkRespSipCode;
    private Optional<String> mReasonPhrase;
    private Optional<Integer> mReasonHeaderCause;
    private Optional<String> mReasonHeaderText;
    private Optional<SipDetails> mSipDetails;
    // The timestamp when receive the response from the network.
    private Instant mResponseTimestamp;

    public PublishRequestResponse(PublishControllerCallback publishCtrlCallback, long taskId,
            String pidfXml) {
        mTaskId = taskId;
        mPidfXml = pidfXml;
        mPublishCtrlCallback = publishCtrlCallback;
        mCmdErrorCode = Optional.empty();
        mNetworkRespSipCode = Optional.empty();
        mReasonPhrase = Optional.empty();
        mReasonHeaderCause = Optional.empty();
        mReasonHeaderText = Optional.empty();
        mSipDetails = Optional.empty();
    }

    public PublishRequestResponse(String pidfXml, @Nullable SipDetails details) {
        mTaskId = 0L;
        mPublishCtrlCallback = null;
        mCmdErrorCode = Optional.empty();

        mPidfXml = pidfXml;
        mResponseTimestamp = Instant.now();
        mNetworkRespSipCode = Optional.of(details.getResponseCode());
        mReasonPhrase = Optional.ofNullable(details.getResponsePhrase());
        if (details.getReasonHeaderCause() != 0) {
            mReasonHeaderCause = Optional.of(details.getReasonHeaderCause());
        } else {
            mReasonHeaderCause = Optional.empty();
        }
        if (TextUtils.isEmpty(details.getReasonHeaderText())) {
            mReasonHeaderText = Optional.empty();
        } else {
            mReasonHeaderText = Optional.ofNullable(details.getReasonHeaderText());
        }
        mSipDetails = Optional.ofNullable(details);
    }

    // The result callback of the publish capability request.
    private IPublishResponseCallback mResponseCallback = new IPublishResponseCallback.Stub() {
        @Override
        public void onCommandError(int code) {
            PublishRequestResponse.this.onCommandError(code);
        }

        @Override
        public void onNetworkResponse(@NonNull SipDetails details) {
            PublishRequestResponse.this.onNetworkResponse(details);
        }
    };

    public IPublishResponseCallback getResponseCallback() {
        return mResponseCallback;
    }

    public long getTaskId() {
        return mTaskId;
    }

    /**
     * Retrieve the command error code which received from the network.
     */
    public Optional<Integer> getCmdErrorCode() {
        return mCmdErrorCode;
    }

    /**
     * Retrieve the network response sip code which received from the network.
     */
    public Optional<Integer> getNetworkRespSipCode() {
        return mNetworkRespSipCode;
    }

    /**
     * Retrieve the reason phrase of the network response which received from the network.
     */
    public Optional<String> getReasonPhrase() {
        return mReasonPhrase;
    }

    /**
     * Retrieve the reason header from the network response.
     */
    public Optional<Integer> getReasonHeaderCause() {
        return mReasonHeaderCause;
    }

    /**
     * Retrieve the description of the reason header.
     */
    public Optional<String> getReasonHeaderText() {
        return mReasonHeaderText;
    }

    /**
     * Retrieve the sip information which received from the network.
     */
    public Optional<SipDetails> getSipDetails() {
        return mSipDetails;
    }
    /**
     * Retrieve the SIP code from the network response. It will get the value from the Reason
     * Header first. If the ReasonHeader is not present, it will get the value from the Network
     * response instead.
     */
    public Optional<Integer> getResponseSipCode() {
        return (mReasonHeaderCause.isPresent()) ? mReasonHeaderCause : mNetworkRespSipCode;
    }

    /**
     * Retrieve the REASON from the network response. It will get the value from the Reason Header
     * first. If the ReasonHeader is not present, it will get the value from the Network response
     * instead.
     */
    public Optional<String> getResponseReason() {
        return (mReasonHeaderText.isPresent()) ? mReasonHeaderText : mReasonPhrase;
    }

    /**
     * Get the timestamp of receiving the network response callback.
     */
    public @Nullable Instant getResponseTimestamp() {
        return mResponseTimestamp;
    }

    /**
     * @return the PIDF XML sent during this request.
     */
    public String getPidfXml() {
        return mPidfXml;
    }

    public void onDestroy() {
        mPublishCtrlCallback = null;
    }

    private void onCommandError(int errorCode) {
        mResponseTimestamp = Instant.now();
        mCmdErrorCode = Optional.of(errorCode);
        updateRetryFlagByCommandError();

        PublishControllerCallback ctrlCallback = mPublishCtrlCallback;
        if (ctrlCallback != null) {
            ctrlCallback.onRequestCommandError(this);
        } else {
            Log.d(LOG_TAG, "onCommandError: already destroyed. error code=" + errorCode);
        }
    }

    private void onNetworkResponse(@NonNull SipDetails details) {
        // When we send a request to PUBLISH and there is no change to the UCE capabilities, we
        // expected onCommandError() with COMMAND_CODE_NO_CHANGE.
        // But some of the vendor will instead send SIP code 999.
        if (details.getResponseCode() == 999) {
            onCommandError(RcsCapabilityExchangeImplBase.COMMAND_CODE_NO_CHANGE);
            return;
        }
        mResponseTimestamp = Instant.now();
        mNetworkRespSipCode = Optional.of(details.getResponseCode());
        mReasonPhrase = Optional.ofNullable(details.getResponsePhrase());
        if (details.getReasonHeaderCause() != 0) {
            mReasonHeaderCause = Optional.of(details.getReasonHeaderCause());
        }
        if (TextUtils.isEmpty(details.getReasonHeaderText())) {
            mReasonHeaderText = Optional.empty();
        } else {
            mReasonHeaderText = Optional.ofNullable(details.getReasonHeaderText());
        }
        mSipDetails = Optional.ofNullable(details);
        updateRetryFlagByNetworkResponse();

        PublishControllerCallback ctrlCallback = mPublishCtrlCallback;
        if (ctrlCallback != null) {
            ctrlCallback.onRequestNetworkResp(this);
        } else {
            Log.d(LOG_TAG, "onNetworkResponse: already destroyed. sip code="
                    + details.getResponseCode());
        }
    }

    private void updateRetryFlagByCommandError() {
        switch(getCmdErrorCode().orElse(-1)) {
            case RcsCapabilityExchangeImplBase.COMMAND_CODE_REQUEST_TIMEOUT:
            case RcsCapabilityExchangeImplBase.COMMAND_CODE_INSUFFICIENT_MEMORY:
            case RcsCapabilityExchangeImplBase.COMMAND_CODE_LOST_NETWORK_CONNECTION:
            case RcsCapabilityExchangeImplBase.COMMAND_CODE_SERVICE_UNAVAILABLE:
                mNeedRetry = true;
                break;
        }
    }

    private void updateRetryFlagByNetworkResponse() {
        // Disable retry flag because the retry mechanism is implemented in the ImsService.
        mNeedRetry = false;
    }

    /*
     * Check whether the publishing request is successful.
     */
    public boolean isRequestSuccess() {
        if (isCommandError()) {
            return false;
        }
        // The result of the request was treated as successful if the command error code is present
        // and its value is COMMAND_CODE_NO_CHANGE.
        if (isCommandCodeNoChange()) {
            return true;
        }

        final int sipCodeOk = NetworkSipCode.SIP_CODE_OK;
        if (getNetworkRespSipCode().filter(c -> c == sipCodeOk).isPresent() &&
                (!getReasonHeaderCause().isPresent()
                        || getReasonHeaderCause().filter(c -> c == sipCodeOk).isPresent())) {
            return true;
        }
        return false;
    }

    /**
     * Check if the PUBLISH request is failed with receiving the command error.
     * @return true if the command is failure.
     */
    private boolean isCommandError() {
        // The request is failed if the command error code is present and its value is not
        // COMMAND_CODE_NO_CHANGE.
        if (getCmdErrorCode().isPresent() && !isCommandCodeNoChange()) {
            return true;
        }
        return false;
    }

    // @return true If it received the command code COMMAND_CODE_NO_CHANGE
    private boolean isCommandCodeNoChange() {
        if (getCmdErrorCode().filter(code ->
                code == RcsCapabilityExchangeImplBase.COMMAND_CODE_NO_CHANGE).isPresent()) {
            return true;
        }
        return false;
    }

    /**
     * Check whether the publishing request needs to be retried.
     */
    public boolean needRetry() {
        return mNeedRetry;
    }

    /**
     * @return The publish state when the publish request is finished.
     */
     public int getPublishState() {
         if (isCommandError()) {
             return getPublishStateByCmdErrorCode();
         } else {
             return getPublishStateByNetworkResponse();
         }
     }

    /**
     * Convert the command error code to the publish state
     */
    private int getPublishStateByCmdErrorCode() {
        if (getCmdErrorCode().orElse(-1) ==
                RcsCapabilityExchangeImplBase.COMMAND_CODE_REQUEST_TIMEOUT) {
            return RcsUceAdapter.PUBLISH_STATE_REQUEST_TIMEOUT;
        }
        return RcsUceAdapter.PUBLISH_STATE_OTHER_ERROR;
    }

    /**
     * Convert the network sip code to the publish state
     */
    private int getPublishStateByNetworkResponse() {
        int respSipCode;
        if (isCommandCodeNoChange()) {
            // If the command code is COMMAND_CODE_NO_CHANGE, it should be treated as successful.
            respSipCode = NetworkSipCode.SIP_CODE_OK;
        } else if (getReasonHeaderCause().isPresent()) {
            respSipCode = getReasonHeaderCause().get();
        } else {
            respSipCode = getNetworkRespSipCode().orElse(-1);
        }

        switch (respSipCode) {
            case NetworkSipCode.SIP_CODE_OK:
                return RcsUceAdapter.PUBLISH_STATE_OK;
            case NetworkSipCode.SIP_CODE_FORBIDDEN:
            case NetworkSipCode.SIP_CODE_NOT_FOUND:
            case NetworkSipCode.SIP_CODE_SERVER_TIMEOUT:
                return RcsUceAdapter.PUBLISH_STATE_RCS_PROVISION_ERROR;
            case NetworkSipCode.SIP_CODE_REQUEST_TIMEOUT:
                return RcsUceAdapter.PUBLISH_STATE_REQUEST_TIMEOUT;
            default:
                return RcsUceAdapter.PUBLISH_STATE_OTHER_ERROR;
        }
    }

    /**
     * Get the information of the publish request response.
     */
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("taskId=").append(mTaskId)
                .append(", CmdErrorCode=").append(getCmdErrorCode().orElse(-1))
                .append(", NetworkRespSipCode=").append(getNetworkRespSipCode().orElse(-1))
                .append(", ReasonPhrase=").append(getReasonPhrase().orElse(""))
                .append(", ReasonHeaderCause=").append(getReasonHeaderCause().orElse(-1))
                .append(", ReasonHeaderText=").append(getReasonHeaderText().orElse(""))
                .append(", ResponseTimestamp=").append(mResponseTimestamp)
                .append(", isRequestSuccess=").append(isRequestSuccess())
                .append(", needRetry=").append(mNeedRetry);
        return builder.toString();
    }
}