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.dialer.simulator.impl;
18
19import android.content.Context;
20import android.support.annotation.NonNull;
21import android.telecom.Conferenceable;
22import android.telecom.Connection;
23import android.telecom.DisconnectCause;
24import com.android.dialer.common.LogUtil;
25import com.android.dialer.simulator.Simulator;
26import com.android.dialer.simulator.Simulator.ConferenceType;
27import com.android.dialer.simulator.Simulator.Event;
28import com.android.dialer.simulator.SimulatorConnectionsBank;
29import java.util.ArrayList;
30import java.util.Collection;
31import java.util.List;
32import javax.inject.Inject;
33
34/** Wraps a list of connection tags and common methods around the connection tags list. */
35public class SimulatorConnectionsBankImpl
36    implements SimulatorConnectionsBank, SimulatorConference.Listener {
37  private final List<String> connectionTags = new ArrayList<>();
38
39  @Inject
40  public SimulatorConnectionsBankImpl() {}
41
42  @Override
43  public List<String> getConnectionTags() {
44    return connectionTags;
45  }
46
47  @Override
48  public void add(Connection connection) {
49    connectionTags.add(SimulatorSimCallManager.getConnectionTag(connection));
50  }
51
52  @Override
53  public void remove(Connection connection) {
54    connectionTags.remove(SimulatorSimCallManager.getConnectionTag(connection));
55  }
56
57  @Override
58  public void mergeAllConnections(@ConferenceType int conferenceType, Context context) {
59    SimulatorConference simulatorConference = null;
60    if (conferenceType == Simulator.CONFERENCE_TYPE_GSM) {
61      simulatorConference =
62          SimulatorConference.newGsmConference(
63              SimulatorSimCallManager.getSystemPhoneAccountHandle(context));
64    } else if (conferenceType == Simulator.CONFERENCE_TYPE_VOLTE) {
65      simulatorConference =
66          SimulatorConference.newVoLteConference(
67              SimulatorSimCallManager.getSystemPhoneAccountHandle(context));
68    }
69    Collection<Connection> connections =
70        SimulatorConnectionService.getInstance().getAllConnections();
71    for (Connection connection : connections) {
72      simulatorConference.addConnection(connection);
73    }
74    simulatorConference.addListener(this);
75    SimulatorConnectionService.getInstance().addConference(simulatorConference);
76  }
77
78  @Override
79  public void disconnectAllConnections() {
80    Collection<Connection> connections =
81        SimulatorConnectionService.getInstance().getAllConnections();
82    for (Connection connection : connections) {
83      connection.setDisconnected(new DisconnectCause(DisconnectCause.LOCAL));
84    }
85  }
86
87  @Override
88  public void updateConferenceableConnections() {
89    LogUtil.enterBlock("SimulatorConferenceCreator.updateConferenceableConnections");
90    for (String connectionTag : connectionTags) {
91      SimulatorConnection connection = SimulatorSimCallManager.findConnectionByTag(connectionTag);
92      List<Conferenceable> conferenceables = getSimulatorConferenceables();
93      conferenceables.remove(connection);
94      conferenceables.remove(connection.getConference());
95      connection.setConferenceables(conferenceables);
96    }
97  }
98
99  private List<Conferenceable> getSimulatorConferenceables() {
100    List<Conferenceable> conferenceables = new ArrayList<>();
101    for (String connectionTag : connectionTags) {
102      SimulatorConnection connection = SimulatorSimCallManager.findConnectionByTag(connectionTag);
103      conferenceables.add(connection);
104      if (connection.getConference() != null
105          && !conferenceables.contains(connection.getConference())) {
106        conferenceables.add(connection.getConference());
107      }
108    }
109    return conferenceables;
110  }
111
112  @Override
113  public boolean isSimulatorConnection(@NonNull Connection connection) {
114    for (String connectionTag : connectionTags) {
115      if (connection.getExtras().getBoolean(connectionTag)) {
116        return true;
117      }
118    }
119    return false;
120  }
121
122  @Override
123  public void onEvent(@NonNull SimulatorConference conference, @NonNull Event event) {
124    switch (event.type) {
125      case Event.MERGE:
126        int capabilities = conference.getConnectionCapabilities();
127        capabilities |= Connection.CAPABILITY_SWAP_CONFERENCE;
128        conference.setConnectionCapabilities(capabilities);
129        break;
130      case Event.SEPARATE:
131        SimulatorConnection connectionToRemove =
132            SimulatorSimCallManager.findConnectionByTag(event.data1);
133        conference.removeConnection(connectionToRemove);
134        break;
135      case Event.DISCONNECT:
136        for (Connection connection : new ArrayList<>(conference.getConnections())) {
137          connection.setDisconnected(new DisconnectCause(DisconnectCause.LOCAL));
138        }
139        conference.setDisconnected(new DisconnectCause(DisconnectCause.LOCAL));
140        break;
141      default:
142        LogUtil.i(
143            "SimulatorConferenceCreator.onEvent", "unexpected conference event: " + event.type);
144        break;
145    }
146  }
147}
148