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 java.util.HashSet;
20 import java.util.Set;
21 
22 /**
23  * Tracks and updates the hold capability of every call or conference across PhoneAccountHandles.
24  *
25  * @hide
26  */
27 public class HoldTracker {
28     private final Set<Holdable> mHoldables;
29 
HoldTracker()30     public HoldTracker() {
31         mHoldables = new HashSet<>();
32     }
33 
34     /**
35      * Adds the holdable, and updates the hold capability for all holdables.
36      */
addHoldable(Holdable holdable)37     public void addHoldable(Holdable holdable) {
38         if (!mHoldables.contains(holdable)) {
39             mHoldables.add(holdable);
40             updateHoldCapability();
41         }
42     }
43 
44     /**
45      * Removes the holdable, and updates the hold capability for all holdable.
46      */
removeHoldable(Holdable holdable)47     public void removeHoldable(Holdable holdable) {
48         if (mHoldables.remove(holdable)) {
49             updateHoldCapability();
50         }
51     }
52 
53     /**
54      * Updates the hold capability for all tracked holdables.
55      */
updateHoldCapability()56     public void updateHoldCapability() {
57         int topHoldableCount = 0;
58         for (Holdable holdable : mHoldables) {
59             if (!holdable.isChildHoldable()) {
60                 ++topHoldableCount;
61             }
62         }
63 
64         Log.d(this, "updateHoldCapability(): topHoldableCount = "
65                 + topHoldableCount);
66         boolean isHoldable = topHoldableCount < 2;
67         for (Holdable holdable : mHoldables) {
68             holdable.setHoldable(holdable.isChildHoldable() ? false : isHoldable);
69         }
70     }
71 }
72