1 /*
2  * Copyright (C) 2017 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.services.telephony;
18 
19 import android.telecom.PhoneAccountHandle;
20 
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25 
26 /**
27  * @hide
28  */
29 public class HoldTracker {
30     private final Map<PhoneAccountHandle, List<Holdable>> mHoldables;
31 
HoldTracker()32     public HoldTracker() {
33         mHoldables = new HashMap<>();
34     }
35 
36     /**
37      * Adds the holdable associated with the {@code phoneAccountHandle}, this method may update
38      * the hold state for all holdable associated with the {@code phoneAccountHandle}.
39      */
addHoldable(PhoneAccountHandle phoneAccountHandle, Holdable holdable)40     public void addHoldable(PhoneAccountHandle phoneAccountHandle, Holdable holdable) {
41         if (!mHoldables.containsKey(phoneAccountHandle)) {
42             mHoldables.put(phoneAccountHandle, new ArrayList<>(1));
43         }
44         List<Holdable> holdables = mHoldables.get(phoneAccountHandle);
45         if (!holdables.contains(holdable)) {
46             holdables.add(holdable);
47             updateHoldCapability(phoneAccountHandle);
48         }
49     }
50 
51     /**
52      * Removes the holdable associated with the {@code phoneAccountHandle}, this method may update
53      * the hold state for all holdable associated with the {@code phoneAccountHandle}.
54      */
removeHoldable(PhoneAccountHandle phoneAccountHandle, Holdable holdable)55     public void removeHoldable(PhoneAccountHandle phoneAccountHandle, Holdable holdable) {
56         if (!mHoldables.containsKey(phoneAccountHandle)) {
57             return;
58         }
59 
60         if (mHoldables.get(phoneAccountHandle).remove(holdable)) {
61             updateHoldCapability(phoneAccountHandle);
62         }
63     }
64 
65     /**
66      * Updates the hold capability for all holdables associated with the {@code phoneAccountHandle}.
67      */
updateHoldCapability(PhoneAccountHandle phoneAccountHandle)68     public void updateHoldCapability(PhoneAccountHandle phoneAccountHandle) {
69         if (!mHoldables.containsKey(phoneAccountHandle)) {
70             return;
71         }
72 
73         List<Holdable> holdables = mHoldables.get(phoneAccountHandle);
74         int topHoldableCount = 0;
75         for (Holdable holdable : holdables) {
76             if (!holdable.isChildHoldable()) {
77                 ++topHoldableCount;
78             }
79         }
80 
81         Log.d(this, "topHoldableCount = " + topHoldableCount);
82         boolean isHoldable = topHoldableCount < 2;
83         for (Holdable holdable : holdables) {
84             holdable.setHoldable(holdable.isChildHoldable() ? false : isHoldable);
85         }
86     }
87 }
88