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.server.telecom;
18 
19 import android.telecom.Log;
20 import android.telecom.PhoneAccountHandle;
21 
22 import com.android.internal.telecom.ICallEventCallback;
23 
24 import java.util.HashMap;
25 import java.util.Map;
26 
27 /**
28  * Tracks all TransactionalServiceWrappers that have an ongoing call. Removes wrappers that have no
29  * more calls.
30  */
31 public class TransactionalServiceRepository {
32     private static final String TAG = TransactionalServiceRepository.class.getSimpleName();
33     private static final Map<PhoneAccountHandle, TransactionalServiceWrapper> mServiceLookupTable =
34             new HashMap<>();
35 
TransactionalServiceRepository()36     public TransactionalServiceRepository() {
37     }
38 
addNewCallForTransactionalServiceWrapper(PhoneAccountHandle phoneAccountHandle, ICallEventCallback callEventCallback, CallsManager callsManager, Call call)39     public TransactionalServiceWrapper addNewCallForTransactionalServiceWrapper
40             (PhoneAccountHandle phoneAccountHandle, ICallEventCallback callEventCallback,
41                     CallsManager callsManager, Call call) {
42         TransactionalServiceWrapper service;
43         // Only create a new TransactionalServiceWrapper if this is the first call for a package.
44         // Otherwise, get the existing TSW and add the new call to the service.
45         if (!hasExistingServiceWrapper(phoneAccountHandle)) {
46             Log.d(TAG, "creating a new TSW; handle=[%s]", phoneAccountHandle);
47             service = new TransactionalServiceWrapper(callEventCallback,
48                     callsManager, phoneAccountHandle, call, this);
49         } else {
50             Log.d(TAG, "add a new call to an existing TSW; handle=[%s]", phoneAccountHandle);
51             service = getTransactionalServiceWrapper(phoneAccountHandle);
52             if (service == null) {
53                 throw new IllegalStateException("service is null");
54             } else {
55                 service.trackCall(call);
56             }
57         }
58 
59         mServiceLookupTable.put(phoneAccountHandle, service);
60 
61         return service;
62     }
63 
getTransactionalServiceWrapper(PhoneAccountHandle pah)64     private TransactionalServiceWrapper getTransactionalServiceWrapper(PhoneAccountHandle pah) {
65         return mServiceLookupTable.get(pah);
66     }
67 
hasExistingServiceWrapper(PhoneAccountHandle pah)68     private boolean hasExistingServiceWrapper(PhoneAccountHandle pah) {
69         return mServiceLookupTable.containsKey(pah);
70     }
71 
removeServiceWrapper(PhoneAccountHandle pah)72     public boolean removeServiceWrapper(PhoneAccountHandle pah) {
73         Log.i(TAG, "removeServiceWrapper: for phoneAccountHandle=[%s]", pah);
74         if (!hasExistingServiceWrapper(pah)) {
75             return false;
76         }
77         mServiceLookupTable.remove(pah);
78         return true;
79     }
80 }
81