1 /* 2 * Copyright (C) 2022 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.internal.telecom; 18 19 import android.telecom.PhoneAccountHandle; 20 21 import java.util.Map; 22 import java.util.concurrent.ConcurrentHashMap; 23 24 /** 25 * @hide 26 */ 27 public class ClientTransactionalServiceRepository { 28 29 private static final Map<PhoneAccountHandle, ClientTransactionalServiceWrapper> LOOKUP_TABLE = 30 new ConcurrentHashMap<>(); 31 32 /** 33 * creates a new {@link ClientTransactionalServiceWrapper} if this is the first call being 34 * tracked for a particular package Or adds a new call for an existing 35 * {@link ClientTransactionalServiceWrapper} 36 * 37 * @param phoneAccountHandle for a particular package requesting to create a call 38 * @return the {@link ClientTransactionalServiceWrapper} that is tied tot the PhoneAccountHandle 39 */ addNewCallForTransactionalServiceWrapper( PhoneAccountHandle phoneAccountHandle)40 public ClientTransactionalServiceWrapper addNewCallForTransactionalServiceWrapper( 41 PhoneAccountHandle phoneAccountHandle) { 42 43 ClientTransactionalServiceWrapper service = null; 44 if (!hasExistingServiceWrapper(phoneAccountHandle)) { 45 service = new ClientTransactionalServiceWrapper(phoneAccountHandle, this); 46 } else { 47 service = getTransactionalServiceWrapper(phoneAccountHandle); 48 } 49 50 LOOKUP_TABLE.put(phoneAccountHandle, service); 51 52 return service; 53 } 54 getTransactionalServiceWrapper( PhoneAccountHandle pah)55 private ClientTransactionalServiceWrapper getTransactionalServiceWrapper( 56 PhoneAccountHandle pah) { 57 return LOOKUP_TABLE.get(pah); 58 } 59 hasExistingServiceWrapper(PhoneAccountHandle pah)60 private boolean hasExistingServiceWrapper(PhoneAccountHandle pah) { 61 return LOOKUP_TABLE.containsKey(pah); 62 } 63 64 /** 65 * @param pah that is tied to a particular package with potential tracked calls 66 * @return if the {@link ClientTransactionalServiceWrapper} was successfully removed 67 */ removeServiceWrapper(PhoneAccountHandle pah)68 public boolean removeServiceWrapper(PhoneAccountHandle pah) { 69 if (!hasExistingServiceWrapper(pah)) { 70 return false; 71 } 72 LOOKUP_TABLE.remove(pah); 73 return true; 74 } 75 76 /** 77 * @param pah that is tied to a particular package with potential tracked calls 78 * @param callId of the TransactionalCall that you want to remove 79 * @return if the call was successfully removed from the service wrapper 80 */ removeCallFromServiceWrapper(PhoneAccountHandle pah, String callId)81 public boolean removeCallFromServiceWrapper(PhoneAccountHandle pah, String callId) { 82 if (!hasExistingServiceWrapper(pah)) { 83 return false; 84 } 85 ClientTransactionalServiceWrapper service = LOOKUP_TABLE.get(pah); 86 service.untrackCall(callId); 87 return true; 88 } 89 90 } 91