1/*
2 * Copyright (C) 2014 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 R* limitations under the License.
15 */
16
17package android.telecom;
18
19import android.content.ComponentName;
20import android.os.RemoteException;
21
22import com.android.internal.telecom.IConnectionService;
23
24import java.util.HashMap;
25import java.util.Map;
26
27/**
28 * @hide
29 */
30public class RemoteConnectionManager {
31    private final Map<ComponentName, RemoteConnectionService> mRemoteConnectionServices =
32            new HashMap<>();
33    private final ConnectionService mOurConnectionServiceImpl;
34
35    public RemoteConnectionManager(ConnectionService ourConnectionServiceImpl) {
36        mOurConnectionServiceImpl = ourConnectionServiceImpl;
37    }
38
39    void addConnectionService(
40            ComponentName componentName,
41            IConnectionService outgoingConnectionServiceRpc) {
42        if (!mRemoteConnectionServices.containsKey(componentName)) {
43            try {
44                RemoteConnectionService remoteConnectionService = new RemoteConnectionService(
45                        outgoingConnectionServiceRpc,
46                        mOurConnectionServiceImpl);
47                mRemoteConnectionServices.put(componentName, remoteConnectionService);
48            } catch (RemoteException ignored) {
49            }
50        }
51    }
52
53    public RemoteConnection createRemoteConnection(
54            PhoneAccountHandle connectionManagerPhoneAccount,
55            ConnectionRequest request,
56            boolean isIncoming) {
57        PhoneAccountHandle accountHandle = request.getAccountHandle();
58        if (accountHandle == null) {
59            throw new IllegalArgumentException("accountHandle must be specified.");
60        }
61
62        ComponentName componentName = request.getAccountHandle().getComponentName();
63        if (!mRemoteConnectionServices.containsKey(componentName)) {
64            throw new UnsupportedOperationException("accountHandle not supported: "
65                    + componentName);
66        }
67
68        RemoteConnectionService remoteService = mRemoteConnectionServices.get(componentName);
69        if (remoteService != null) {
70            return remoteService.createRemoteConnection(
71                    connectionManagerPhoneAccount, request, isIncoming);
72        }
73        return null;
74    }
75
76    public void conferenceRemoteConnections(RemoteConnection a, RemoteConnection b) {
77        if (a.getConnectionService() == b.getConnectionService()) {
78            try {
79                a.getConnectionService().conference(a.getId(), b.getId());
80            } catch (RemoteException e) {
81            }
82        } else {
83            Log.w(this, "Request to conference incompatible remote connections (%s,%s) (%s,%s)",
84                    a.getConnectionService(), a.getId(),
85                    b.getConnectionService(), b.getId());
86        }
87    }
88}
89