1 /* 2 * Copyright (C) 2021 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.ims.rcs.uce.request; 18 19 import com.android.ims.rcs.uce.request.UceRequestManager.RequestManagerCallback; 20 21 import java.util.HashMap; 22 import java.util.Map; 23 24 /** 25 * This class is responsible for storing the capabilities request. 26 */ 27 public class UceRequestRepository { 28 29 // Dispatch the UceRequest to be executed. 30 private final UceRequestDispatcher mDispatcher; 31 32 // Store all the capabilities requests 33 private final Map<Long, UceRequestCoordinator> mRequestCoordinators; 34 35 private volatile boolean mDestroyed = false; 36 UceRequestRepository(int subId, RequestManagerCallback callback)37 public UceRequestRepository(int subId, RequestManagerCallback callback) { 38 mRequestCoordinators = new HashMap<>(); 39 mDispatcher = new UceRequestDispatcher(subId, callback); 40 } 41 42 /** 43 * Clear the collection when the instance is destroyed. 44 */ onDestroy()45 public synchronized void onDestroy() { 46 mDestroyed = true; 47 mDispatcher.onDestroy(); 48 mRequestCoordinators.forEach((taskId, requestCoord) -> requestCoord.onFinish()); 49 mRequestCoordinators.clear(); 50 } 51 52 /** 53 * Add new UceRequestCoordinator and notify the RequestDispatcher to check whether the given 54 * requests can be executed or not. 55 */ addRequestCoordinatorAndDispatch(UceRequestCoordinator coordinator)56 public synchronized void addRequestCoordinatorAndDispatch(UceRequestCoordinator coordinator) { 57 if (mDestroyed) return; 58 mRequestCoordinators.put(coordinator.getCoordinatorId(), coordinator); 59 mDispatcher.addRequest(coordinator.getCoordinatorId(), 60 coordinator.getActivatedRequestTaskIds()); 61 } 62 63 /** 64 * Remove the RequestCoordinator from the RequestCoordinator collection. 65 */ removeRequestCoordinator(Long coordinatorId)66 public synchronized UceRequestCoordinator removeRequestCoordinator(Long coordinatorId) { 67 return mRequestCoordinators.remove(coordinatorId); 68 69 } 70 71 /** 72 * Add new UceRequestCoordinator for retry only and not execute the given request. 73 */ addRequestCoordinator(UceRequestCoordinator coordinator)74 public synchronized void addRequestCoordinator(UceRequestCoordinator coordinator) { 75 if (mDestroyed) return; 76 mRequestCoordinators.put(coordinator.getCoordinatorId(), coordinator); 77 } 78 79 /** 80 * Retrieve the RequestCoordinator associated with the given coordinatorId. 81 */ getRequestCoordinator(Long coordinatorId)82 public synchronized UceRequestCoordinator getRequestCoordinator(Long coordinatorId) { 83 return mRequestCoordinators.get(coordinatorId); 84 } 85 getUceRequest(Long taskId)86 public synchronized UceRequest getUceRequest(Long taskId) { 87 for (UceRequestCoordinator coordinator : mRequestCoordinators.values()) { 88 UceRequest request = coordinator.getUceRequest(taskId); 89 if (request != null) { 90 return request; 91 } 92 } 93 return null; 94 } 95 96 // Notify that the task is finished. notifyRequestFinished(Long taskId)97 public synchronized void notifyRequestFinished(Long taskId) { 98 mDispatcher.onRequestFinished(taskId); 99 } 100 } 101