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