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