summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/simulator/impl/SimulatorConnectionsBankImpl.java
blob: 75f144fde003486b7fdd4093443a47ffc1582016 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.dialer.simulator.impl;

import android.content.Context;
import android.support.annotation.NonNull;
import android.telecom.Conferenceable;
import android.telecom.Connection;
import android.telecom.DisconnectCause;
import com.android.dialer.common.LogUtil;
import com.android.dialer.simulator.Simulator;
import com.android.dialer.simulator.Simulator.ConferenceType;
import com.android.dialer.simulator.Simulator.Event;
import com.android.dialer.simulator.SimulatorConnectionsBank;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.inject.Inject;

/** Wraps a list of connection tags and common methods around the connection tags list. */
public class SimulatorConnectionsBankImpl
    implements SimulatorConnectionsBank, SimulatorConference.Listener {
  private final List<String> connectionTags = new ArrayList<>();

  @Inject
  public SimulatorConnectionsBankImpl() {}

  @Override
  public List<String> getConnectionTags() {
    return connectionTags;
  }

  @Override
  public void add(Connection connection) {
    connectionTags.add(SimulatorSimCallManager.getConnectionTag(connection));
  }

  @Override
  public void remove(Connection connection) {
    connectionTags.remove(SimulatorSimCallManager.getConnectionTag(connection));
  }

  @Override
  public void mergeAllConnections(@ConferenceType int conferenceType, Context context) {
    SimulatorConference simulatorConference = null;
    if (conferenceType == Simulator.CONFERENCE_TYPE_GSM) {
      simulatorConference =
          SimulatorConference.newGsmConference(
              SimulatorSimCallManager.getSystemPhoneAccountHandle(context));
    } else if (conferenceType == Simulator.CONFERENCE_TYPE_VOLTE) {
      simulatorConference =
          SimulatorConference.newVoLteConference(
              SimulatorSimCallManager.getSystemPhoneAccountHandle(context));
    }
    Collection<Connection> connections =
        SimulatorConnectionService.getInstance().getAllConnections();
    for (Connection connection : connections) {
      simulatorConference.addConnection(connection);
    }
    simulatorConference.addListener(this);
    SimulatorConnectionService.getInstance().addConference(simulatorConference);
  }

  @Override
  public void disconnectAllConnections() {
    Collection<Connection> connections =
        SimulatorConnectionService.getInstance().getAllConnections();
    for (Connection connection : connections) {
      connection.setDisconnected(new DisconnectCause(DisconnectCause.LOCAL));
    }
  }

  @Override
  public void updateConferenceableConnections() {
    LogUtil.enterBlock("SimulatorConferenceCreator.updateConferenceableConnections");
    for (String connectionTag : connectionTags) {
      SimulatorConnection connection = SimulatorSimCallManager.findConnectionByTag(connectionTag);
      List<Conferenceable> conferenceables = getSimulatorConferenceables();
      conferenceables.remove(connection);
      conferenceables.remove(connection.getConference());
      connection.setConferenceables(conferenceables);
    }
  }

  private List<Conferenceable> getSimulatorConferenceables() {
    List<Conferenceable> conferenceables = new ArrayList<>();
    for (String connectionTag : connectionTags) {
      SimulatorConnection connection = SimulatorSimCallManager.findConnectionByTag(connectionTag);
      conferenceables.add(connection);
      if (connection.getConference() != null
          && !conferenceables.contains(connection.getConference())) {
        conferenceables.add(connection.getConference());
      }
    }
    return conferenceables;
  }

  @Override
  public boolean isSimulatorConnection(@NonNull Connection connection) {
    for (String connectionTag : connectionTags) {
      if (connection.getExtras().getBoolean(connectionTag)) {
        return true;
      }
    }
    return false;
  }

  @Override
  public void onEvent(@NonNull SimulatorConference conference, @NonNull Event event) {
    switch (event.type) {
      case Event.MERGE:
        int capabilities = conference.getConnectionCapabilities();
        capabilities |= Connection.CAPABILITY_SWAP_CONFERENCE;
        conference.setConnectionCapabilities(capabilities);
        break;
      case Event.SEPARATE:
        SimulatorConnection connectionToRemove =
            SimulatorSimCallManager.findConnectionByTag(event.data1);
        conference.removeConnection(connectionToRemove);
        break;
      case Event.DISCONNECT:
        for (Connection connection : new ArrayList<>(conference.getConnections())) {
          connection.setDisconnected(new DisconnectCause(DisconnectCause.LOCAL));
        }
        conference.setDisconnected(new DisconnectCause(DisconnectCause.LOCAL));
        break;
      default:
        LogUtil.i(
            "SimulatorConferenceCreator.onEvent", "unexpected conference event: " + event.type);
        break;
    }
  }
}