aboutsummaryrefslogtreecommitdiff
path: root/src/java/com/android/ims/rcs/uce/request/SubscribeRequestCoordinator.java
blob: f44686ac08e9e280cff42e27083fccdc833fb9e8 (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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/*
 * Copyright (c) 2021 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.request;

import static android.telephony.ims.stub.RcsCapabilityExchangeImplBase.COMMAND_CODE_GENERIC_FAILURE;

import android.net.Uri;
import android.os.RemoteException;
import android.telephony.ims.RcsContactUceCapability;
import android.telephony.ims.RcsUceAdapter;
import android.telephony.ims.aidl.IRcsUceControllerCallback;

import com.android.ims.rcs.uce.UceDeviceState.DeviceStateResult;
import com.android.ims.rcs.uce.eab.EabCapabilityResult;
import com.android.ims.rcs.uce.presence.pidfparser.PidfParserUtils;
import com.android.ims.rcs.uce.request.SubscriptionTerminatedHelper.TerminatedResult;
import com.android.ims.rcs.uce.request.UceRequestManager.RequestManagerCallback;
import com.android.ims.rcs.uce.UceStatsWriter;
import com.android.internal.annotations.VisibleForTesting;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Responsible for the communication and interaction between SubscribeRequests and triggering
 * the callback to notify the result of the capabilities request.
 */
public class SubscribeRequestCoordinator extends UceRequestCoordinator {
    /**
     * The builder of the SubscribeRequestCoordinator.
     */
    public static final class Builder {
        private SubscribeRequestCoordinator mRequestCoordinator;

        /**
         * The builder of the SubscribeRequestCoordinator class.
         */
        public Builder(int subId, Collection<UceRequest> requests, RequestManagerCallback c) {
            mRequestCoordinator = new SubscribeRequestCoordinator(subId, requests, c,
                    UceStatsWriter.getInstance());
        }
        @VisibleForTesting
        public Builder(int subId, Collection<UceRequest> requests, RequestManagerCallback c,
                UceStatsWriter instance) {
            mRequestCoordinator = new SubscribeRequestCoordinator(subId, requests, c, instance);
        }

        /**
         * Set the callback to receive the request updated.
         */
        public Builder setCapabilitiesCallback(IRcsUceControllerCallback callback) {
            mRequestCoordinator.setCapabilitiesCallback(callback);
            return this;
        }

        /**
         * Get the SubscribeRequestCoordinator instance.
         */
        public SubscribeRequestCoordinator build() {
            return mRequestCoordinator;
        }
    }

    /**
     * Different request updated events will create different {@link RequestResult}. Define the
     * interface to get the {@link RequestResult} instance according to the given task ID and
     * {@link CapabilityRequestResponse}.
     */
    @FunctionalInterface
    private interface RequestResultCreator {
        RequestResult createRequestResult(long taskId, CapabilityRequestResponse response,
                RequestManagerCallback requestMgrCallback);
    }

    // The RequestResult creator of the request error.
    private static final RequestResultCreator sRequestErrorCreator = (taskId, response,
            requestMgrCallback) -> {
        int errorCode = response.getRequestInternalError().orElse(DEFAULT_ERROR_CODE);
        long retryAfter = response.getRetryAfterMillis();
        return RequestResult.createFailedResult(taskId, errorCode, retryAfter);
    };

    // The RequestResult creator of the command error.
    private static final RequestResultCreator sCommandErrorCreator = (taskId, response,
            requestMgrCallback) -> {
        int cmdError = response.getCommandError().orElse(COMMAND_CODE_GENERIC_FAILURE);
        int errorCode = CapabilityRequestResponse.getCapabilityErrorFromCommandError(cmdError);
        long retryAfter = response.getRetryAfterMillis();
        return RequestResult.createFailedResult(taskId, errorCode, retryAfter);
    };

    // The RequestResult creator of the network response error.
    private static final RequestResultCreator sNetworkRespErrorCreator = (taskId, response,
            requestMgrCallback) -> {
        DeviceStateResult deviceState = requestMgrCallback.getDeviceState();
        if (deviceState.isRequestForbidden()) {
            int errorCode = deviceState.getErrorCode().orElse(RcsUceAdapter.ERROR_FORBIDDEN);
            long retryAfter = deviceState.getRequestRetryAfterMillis();
            return RequestResult.createFailedResult(taskId, errorCode, retryAfter);
        } else {
            int errorCode = CapabilityRequestResponse.getCapabilityErrorFromSipCode(response);
            long retryAfter = response.getRetryAfterMillis();
            return RequestResult.createFailedResult(taskId, errorCode, retryAfter);
        }
    };

    // The RequestResult creator of the network response is not 200 OK, however, we can to treat
    // it as a successful result and finish the request
    private static final RequestResultCreator sNetworkRespSuccessfulCreator = (taskId, response,
            requestMgrCallback) -> RequestResult.createSuccessResult(taskId);

    // The RequestResult creator of the request terminated.
    private static final RequestResultCreator sTerminatedCreator = (taskId, response,
            requestMgrCallback) -> {
        // Check the given terminated reason to determine whether clients should retry or not.
        TerminatedResult terminatedResult = SubscriptionTerminatedHelper.getAnalysisResult(
                response.getTerminatedReason(), response.getRetryAfterMillis(),
                response.haveAllRequestCapsUpdatedBeenReceived());
        if (terminatedResult.getErrorCode().isPresent()) {
            // If the terminated error code is present, it means that the request is failed.
            int errorCode = terminatedResult.getErrorCode().get();
            long terminatedRetry = terminatedResult.getRetryAfterMillis();
            return RequestResult.createFailedResult(taskId, errorCode, terminatedRetry);
        } else if (!response.isNetworkResponseOK() || response.getRetryAfterMillis() > 0L) {
            // If the network response is failed or the retryAfter is not 0, this request is failed.
            long retryAfterMillis = response.getRetryAfterMillis();
            int errorCode = CapabilityRequestResponse.getCapabilityErrorFromSipCode(response);
            return RequestResult.createFailedResult(taskId, errorCode, retryAfterMillis);
        } else {
            return RequestResult.createSuccessResult(taskId);
        }
    };

    // The RequestResult creator for does not need to request from the network.
    private static final RequestResultCreator sNotNeedRequestFromNetworkCreator =
            (taskId, response, requestMgrCallback) -> RequestResult.createSuccessResult(taskId);

    // The RequestResult creator of the request timeout.
    private static final RequestResultCreator sRequestTimeoutCreator =
            (taskId, response, requestMgrCallback) -> RequestResult.createFailedResult(taskId,
                    RcsUceAdapter.ERROR_REQUEST_TIMEOUT, 0L);

    // The callback to notify the result of the capabilities request.
    private volatile IRcsUceControllerCallback mCapabilitiesCallback;

    private final UceStatsWriter mUceStatsWriter;

    private SubscribeRequestCoordinator(int subId, Collection<UceRequest> requests,
            RequestManagerCallback requestMgrCallback, UceStatsWriter instance) {
        super(subId, requests, requestMgrCallback);
        mUceStatsWriter = instance;
        logd("SubscribeRequestCoordinator: created");
    }

    private void setCapabilitiesCallback(IRcsUceControllerCallback callback) {
        mCapabilitiesCallback = callback;
    }

    @Override
    public void onFinish() {
        logd("SubscribeRequestCoordinator: onFinish");
        mCapabilitiesCallback = null;
        super.onFinish();
    }

    @Override
    public void onRequestUpdated(long taskId, @UceRequestUpdate int event) {
        if (mIsFinished) return;
        SubscribeRequest request = (SubscribeRequest) getUceRequest(taskId);
        if (request == null) {
            logw("onRequestUpdated: Cannot find SubscribeRequest taskId=" + taskId);
            return;
        }

        logd("onRequestUpdated(SubscribeRequest): taskId=" + taskId + ", event=" +
                REQUEST_EVENT_DESC.get(event));

        switch (event) {
            case REQUEST_UPDATE_ERROR:
                handleRequestError(request);
                break;
            case REQUEST_UPDATE_COMMAND_ERROR:
                handleCommandError(request);
                break;
            case REQUEST_UPDATE_NETWORK_RESPONSE:
                handleNetworkResponse(request);
                break;
            case REQUEST_UPDATE_CAPABILITY_UPDATE:
                handleCapabilitiesUpdated(request);
                break;
            case REQUEST_UPDATE_RESOURCE_TERMINATED:
                handleResourceTerminated(request);
                break;
            case REQUEST_UPDATE_CACHED_CAPABILITY_UPDATE:
                handleCachedCapabilityUpdated(request);
                break;
            case REQUEST_UPDATE_TERMINATED:
                handleTerminated(request);
                break;
            case REQUEST_UPDATE_NO_NEED_REQUEST_FROM_NETWORK:
                handleNoNeedRequestFromNetwork(request);
                break;
            case REQUEST_UPDATE_TIMEOUT:
                handleRequestTimeout(request);
                break;
            default:
                logw("onRequestUpdated(SubscribeRequest): invalid event " + event);
                break;
        }

        // End this instance if all the UceRequests in the coordinator are finished.
        checkAndFinishRequestCoordinator();
    }

    /**
     * Finish the SubscribeRequest because it has encountered error.
     */
    private void handleRequestError(SubscribeRequest request) {
        CapabilityRequestResponse response = request.getRequestResponse();
        logd("handleRequestError: " + request.toString());

        // Finish this request.
        request.onFinish();

        // Remove this request from the activated collection and notify RequestManager.
        Long taskId = request.getTaskId();
        RequestResult requestResult = sRequestErrorCreator.createRequestResult(taskId, response,
                mRequestManagerCallback);
        moveRequestToFinishedCollection(taskId, requestResult);
    }

    /**
     * This method is called when the given SubscribeRequest received the onCommandError callback
     * from the ImsService.
     */
    private void handleCommandError(SubscribeRequest request) {
        CapabilityRequestResponse response = request.getRequestResponse();
        logd("handleCommandError: " + request.toString());

        // Finish this request.
        request.onFinish();

        int commandErrorCode = response.getCommandError().orElse(0);
        mUceStatsWriter.setUceEvent(mSubId, UceStatsWriter.SUBSCRIBE_EVENT,
            false, commandErrorCode, 0);

        // Remove this request from the activated collection and notify RequestManager.
        Long taskId = request.getTaskId();
        RequestResult requestResult = sCommandErrorCreator.createRequestResult(taskId, response,
                mRequestManagerCallback);
        moveRequestToFinishedCollection(taskId, requestResult);
    }

    /**
     * This method is called when the given SubscribeRequest received the onNetworkResponse
     * callback from the ImsService.
     */
    private void handleNetworkResponse(SubscribeRequest request) {
        CapabilityRequestResponse response = request.getRequestResponse();
        logd("handleNetworkResponse: " + response.toString());

        int respCode = response.getNetworkRespSipCode().orElse(0);
        mUceStatsWriter.setSubscribeResponse(mSubId, request.getTaskId(), respCode);

        // Refresh the device state with the request result.
        response.getResponseSipCode().ifPresent(sipCode -> {
            String reason = response.getResponseReason().orElse("");
            mRequestManagerCallback.refreshDeviceState(sipCode, reason);
        });

        // When the network response is unsuccessful, there is no subsequent callback for this
        // request. Check the forbidden state and finish this request. Otherwise, keep waiting for
        // the subsequent callback of this request.
        if (!response.isNetworkResponseOK()) {
            // Handle the network response not OK cases and get the request result to finish this
            // request.
            RequestResult requestResult = handleNetworkResponseFailed(request);

            // Trigger capabilities updated callback if there is any.
            List<RcsContactUceCapability> updatedCapList = response.getUpdatedContactCapability();
            if (!updatedCapList.isEmpty()) {
                mRequestManagerCallback.saveCapabilities(updatedCapList);
                triggerCapabilitiesReceivedCallback(updatedCapList);
                response.removeUpdatedCapabilities(updatedCapList);
            }

            // Finish this request.
            request.onFinish();

            // Remove this request from the activated collection and notify RequestManager.
            moveRequestToFinishedCollection(request.getTaskId(), requestResult);
        }
    }

    private RequestResult handleNetworkResponseFailed(SubscribeRequest request) {
        final long taskId = request.getTaskId();
        final CapabilityRequestResponse response = request.getRequestResponse();
        final List<Uri> requestUris = response.getNotReceiveCapabilityUpdatedContact();
        RequestResult requestResult = null;

        if (response.isNotFound()) {
            // In the network response with the not found case, we won't receive the capabilities
            // updated callback from the ImsService afterward. Therefore, we create the capabilities
            // with the result REQUEST_RESULT_NOT_FOUND by ourself and will trigger the
            // capabilities received callback to the clients later.
            List<RcsContactUceCapability> capabilityList = requestUris.stream().map(uri ->
                    PidfParserUtils.getNotFoundContactCapabilities(uri))
                    .collect(Collectors.toList());
            response.addUpdatedCapabilities(capabilityList);

            // We treat the NOT FOUND is a successful result.
            requestResult = sNetworkRespSuccessfulCreator.createRequestResult(taskId, response,
                    mRequestManagerCallback);
        } else {
            // The request result is unsuccessful and it's not the NOT FOUND error. we need to get
            // the capabilities from the cache.
            List<RcsContactUceCapability> capabilitiesList =
                    getCapabilitiesFromCacheIncludingExpired(requestUris);
            response.addUpdatedCapabilities(capabilitiesList);

            // Add to the throttling list for the inconclusive result of the contacts.
            mRequestManagerCallback.addToThrottlingList(requestUris,
                    response.getResponseSipCode().orElse(
                            com.android.ims.rcs.uce.util.NetworkSipCode.SIP_CODE_REQUEST_TIMEOUT));

            requestResult = sNetworkRespErrorCreator.createRequestResult(taskId, response,
                    mRequestManagerCallback);
        }
        return requestResult;
    }

    /**
     * Get the contact capabilities from the cache even if the capabilities have expired. If the
     * capabilities doesn't exist, create the non-RCS capabilities instead.
     * @param uris the uris to get the capabilities from cache.
     * @return The contact capabilities for the given uris.
     */
    private List<RcsContactUceCapability> getCapabilitiesFromCacheIncludingExpired(List<Uri> uris) {
        List<RcsContactUceCapability> resultList = new ArrayList<>();
        List<RcsContactUceCapability> notFoundFromCacheList = new ArrayList<>();

        // Get the capabilities from the cache.
        List<EabCapabilityResult> eabResultList =
                mRequestManagerCallback.getCapabilitiesFromCacheIncludingExpired(uris);

        eabResultList.forEach(eabResult -> {
            if (eabResult.getStatus() == EabCapabilityResult.EAB_QUERY_SUCCESSFUL ||
                eabResult.getStatus() == EabCapabilityResult.EAB_CONTACT_EXPIRED_FAILURE) {
                // The capabilities are found, add to the result list
                resultList.add(eabResult.getContactCapabilities());
            } else {
                // Cannot get the capabilities from cache, create the non-RCS capabilities instead.
                notFoundFromCacheList.add(PidfParserUtils.getNotFoundContactCapabilities(
                        eabResult.getContact()));
            }
        });

        if (!notFoundFromCacheList.isEmpty()) {
            resultList.addAll(notFoundFromCacheList);
        }

        logd("getCapabilitiesFromCacheIncludingExpired: requesting uris size=" + uris.size() +
                ", capabilities not found from cache size=" + notFoundFromCacheList.size());
        return resultList;
    }

    /**
     * This method is called when the given SubscribeRequest received the onNotifyCapabilitiesUpdate
     * callback from the ImsService.
     */
    private void handleCapabilitiesUpdated(SubscribeRequest request) {
        CapabilityRequestResponse response = request.getRequestResponse();
        Long taskId = request.getTaskId();
        List<RcsContactUceCapability> updatedCapList = response.getUpdatedContactCapability();
        logd("handleCapabilitiesUpdated: taskId=" + taskId + ", size=" + updatedCapList.size());

        if (updatedCapList.isEmpty()) {
            return;
        }

        mUceStatsWriter.setPresenceNotifyEvent(mSubId, taskId, updatedCapList);
        // Save the updated capabilities to the cache.
        mRequestManagerCallback.saveCapabilities(updatedCapList);

        // Trigger the capabilities updated callback and remove the given capabilities that have
        // executed the callback onCapabilitiesReceived.
        triggerCapabilitiesReceivedCallback(updatedCapList);
        response.removeUpdatedCapabilities(updatedCapList);
    }

    /**
     * This method is called when the given SubscribeRequest received the onResourceTerminated
     * callback from the ImsService.
     */
    private void handleResourceTerminated(SubscribeRequest request) {
        CapabilityRequestResponse response = request.getRequestResponse();
        Long taskId = request.getTaskId();
        List<RcsContactUceCapability> terminatedResources = response.getTerminatedResources();
        logd("handleResourceTerminated: taskId=" + taskId + ", size=" + terminatedResources.size());

        if (terminatedResources.isEmpty()) {
            return;
        }

        mUceStatsWriter.setPresenceNotifyEvent(mSubId, taskId, terminatedResources);

        // Save the terminated capabilities to the cache.
        mRequestManagerCallback.saveCapabilities(terminatedResources);

        // Trigger the capabilities updated callback and remove the given capabilities from the
        // resource terminated list.
        triggerCapabilitiesReceivedCallback(terminatedResources);
        response.removeTerminatedResources(terminatedResources);
    }

    /**
     * This method is called when the given SubscribeRequest retrieve the cached capabilities.
     */
    private void handleCachedCapabilityUpdated(SubscribeRequest request) {
        CapabilityRequestResponse response = request.getRequestResponse();
        Long taskId = request.getTaskId();
        List<RcsContactUceCapability> cachedCapList = response.getCachedContactCapability();
        logd("handleCachedCapabilityUpdated: taskId=" + taskId + ", size=" + cachedCapList.size());

        if (cachedCapList.isEmpty()) {
            return;
        }

        // Trigger the capabilities updated callback.
        triggerCapabilitiesReceivedCallback(cachedCapList);
        response.removeCachedContactCapabilities();
    }

    /**
     * This method is called when the given SubscribeRequest received the onTerminated callback
     * from the ImsService.
     */
    private void handleTerminated(SubscribeRequest request) {
        CapabilityRequestResponse response = request.getRequestResponse();
        logd("handleTerminated: " + response.toString());

        // Finish this request.
        request.onFinish();

        // Remove this request from the activated collection and notify RequestManager.
        Long taskId = request.getTaskId();
        mUceStatsWriter.setSubscribeTerminated(mSubId, taskId, response.getTerminatedReason());
        RequestResult requestResult = sTerminatedCreator.createRequestResult(taskId, response,
                mRequestManagerCallback);
        moveRequestToFinishedCollection(taskId, requestResult);
    }

    /**
     * This method is called when all the capabilities can be retrieved from the cached and it does
     * not need to request capabilities from the network.
     */
    private void handleNoNeedRequestFromNetwork(SubscribeRequest request) {
        CapabilityRequestResponse response = request.getRequestResponse();
        logd("handleNoNeedRequestFromNetwork: " + response.toString());

        // Finish this request.
        request.onFinish();

        // Remove this request from the activated collection and notify RequestManager.
        long taskId = request.getTaskId();
        RequestResult requestResult = sNotNeedRequestFromNetworkCreator.createRequestResult(taskId,
                response, mRequestManagerCallback);
        moveRequestToFinishedCollection(taskId, requestResult);
    }

    /**
     * This method is called when the framework did not receive the capabilities request result.
     */
    private void handleRequestTimeout(SubscribeRequest request) {
        CapabilityRequestResponse response = request.getRequestResponse();
        List<Uri> requestUris = response.getNotReceiveCapabilityUpdatedContact();
        logd("handleRequestTimeout: " + response);
        logd("handleRequestTimeout: not received updated uri size=" + requestUris.size());

        // Add to the throttling list for the inconclusive result of the contacts.
        mRequestManagerCallback.addToThrottlingList(requestUris,
                com.android.ims.rcs.uce.util.NetworkSipCode.SIP_CODE_REQUEST_TIMEOUT);

        // Get the capabilities from the cache instead and add to the response.
        List<RcsContactUceCapability> capabilitiesList =
                getCapabilitiesFromCacheIncludingExpired(requestUris);
        response.addUpdatedCapabilities(capabilitiesList);

        // Trigger capabilities updated callback if there is any.
        List<RcsContactUceCapability> updatedCapList = response.getUpdatedContactCapability();
        if (!updatedCapList.isEmpty()) {
            triggerCapabilitiesReceivedCallback(updatedCapList);
            response.removeUpdatedCapabilities(updatedCapList);
        }

        // Remove this request from the activated collection and notify RequestManager.
        long taskId = request.getTaskId();
        RequestResult requestResult = sRequestTimeoutCreator.createRequestResult(taskId,
                response, mRequestManagerCallback);

        // Finish this request
        request.onFinish();

        // Remove this request from the activated collection and notify RequestManager.
        moveRequestToFinishedCollection(taskId, requestResult);
    }

    private void checkAndFinishRequestCoordinator() {
        synchronized (mCollectionLock) {
            // Return because there are requests running.
            if (!mActivatedRequests.isEmpty()) {
                return;
            }

            // All the requests has finished, find the request which has the max retryAfter time.
            // If the result is empty, it means all the request are success.
            Optional<RequestResult> optRequestResult =
                    mFinishedRequests.values().stream()
                        .filter(result -> !result.isRequestSuccess())
                        .max(Comparator.comparingLong(result ->
                                result.getRetryMillis().orElse(-1L)));

            // Trigger the callback
            if (optRequestResult.isPresent()) {
                RequestResult result = optRequestResult.get();
                int errorCode = result.getErrorCode().orElse(DEFAULT_ERROR_CODE);
                long retryAfter = result.getRetryMillis().orElse(0L);
                triggerErrorCallback(errorCode, retryAfter);
            } else {
                triggerCompletedCallback();
            }

            // Notify UceRequestManager to remove this instance from the collection.
            mRequestManagerCallback.notifyRequestCoordinatorFinished(mCoordinatorId);

            logd("checkAndFinishRequestCoordinator(SubscribeRequest) done, id=" + mCoordinatorId);
        }
    }

    /**
     * Trigger the capabilities updated callback.
     */
    private void triggerCapabilitiesReceivedCallback(List<RcsContactUceCapability> capList) {
        try {
            logd("triggerCapabilitiesCallback: size=" + capList.size());
            mCapabilitiesCallback.onCapabilitiesReceived(capList);
        } catch (RemoteException e) {
            logw("triggerCapabilitiesCallback exception: " + e);
        } finally {
            logd("triggerCapabilitiesCallback: done");
        }
    }

    /**
     * Trigger the onComplete callback to notify the request is completed.
     */
    private void triggerCompletedCallback() {
        try {
            logd("triggerCompletedCallback");
            mCapabilitiesCallback.onComplete();
        } catch (RemoteException e) {
            logw("triggerCompletedCallback exception: " + e);
        } finally {
            logd("triggerCompletedCallback: done");
        }
    }

    /**
     * Trigger the onError callback to notify the request is failed.
     */
    private void triggerErrorCallback(int errorCode, long retryAfterMillis) {
        try {
            logd("triggerErrorCallback: errorCode=" + errorCode + ", retry=" + retryAfterMillis);
            mCapabilitiesCallback.onError(errorCode, retryAfterMillis);
        } catch (RemoteException e) {
            logw("triggerErrorCallback exception: " + e);
        } finally {
            logd("triggerErrorCallback: done");
        }
    }

    @VisibleForTesting
    public Collection<UceRequest> getActivatedRequest() {
        return mActivatedRequests.values();
    }

    @VisibleForTesting
    public Collection<RequestResult> getFinishedRequest() {
        return mFinishedRequests.values();
    }
}