blob: ae1386084b0019449e5ced53336d9696e15c7a36 [file] [log] [blame]
Sailesh Nepale59bb192014-04-01 18:33:59 -07001/*
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 * limitations under the License.
15 */
16
17package com.android.telecomm;
18
19import com.google.common.base.Preconditions;
20import com.google.common.collect.HashBiMap;
21
Sailesh Nepale59bb192014-04-01 18:33:59 -070022/** Utility to map {@link Call} objects to unique IDs. IDs are generated when a call is added. */
23class CallIdMapper {
24 private final HashBiMap<String, Call> mCalls = HashBiMap.create();
25 private final String mCallIdPrefix;
Sailesh Nepale2ea6532014-04-01 19:45:45 -070026 private static int sIdCount;
Sailesh Nepale59bb192014-04-01 18:33:59 -070027
28 CallIdMapper(String callIdPrefix) {
29 ThreadUtil.checkOnMainThread();
30 mCallIdPrefix = callIdPrefix + "@";
31 }
32
Sailesh Nepal0e5410a2014-04-04 01:20:58 -070033 void replaceCall(Call newCall, Call callToReplace) {
34 ThreadUtil.checkOnMainThread();
35
36 // Use the old call's ID for the new call.
37 String callId = getCallId(callToReplace);
38 mCalls.put(callId, newCall);
39 }
40
Santos Cordona1610702014-06-04 20:22:56 -070041 void addCall(Call call, String id) {
42 Preconditions.checkNotNull(call);
43 ThreadUtil.checkOnMainThread();
44 mCalls.put(id, call);
45 }
46
Sailesh Nepale59bb192014-04-01 18:33:59 -070047 void addCall(Call call) {
48 ThreadUtil.checkOnMainThread();
Santos Cordona1610702014-06-04 20:22:56 -070049 addCall(call, getNewId());
Sailesh Nepale59bb192014-04-01 18:33:59 -070050 }
51
52 void removeCall(Call call) {
53 ThreadUtil.checkOnMainThread();
54 Preconditions.checkNotNull(call);
55 mCalls.inverse().remove(call);
56 }
57
Santos Cordon682fe6b2014-05-20 08:56:39 -070058 void removeCall(String callId) {
59 ThreadUtil.checkOnMainThread();
60 mCalls.remove(callId);
61 }
62
Sailesh Nepale59bb192014-04-01 18:33:59 -070063 String getCallId(Call call) {
64 ThreadUtil.checkOnMainThread();
65 Preconditions.checkNotNull(call);
66 return mCalls.inverse().get(call);
67 }
68
69 Call getCall(Object objId) {
70 ThreadUtil.checkOnMainThread();
71
72 String callId = null;
73 if (objId instanceof String) {
74 callId = (String) objId;
75 }
Santos Cordon3d3b4052014-05-05 12:05:36 -070076 checkValidCallId(callId);
Sailesh Nepale59bb192014-04-01 18:33:59 -070077
78 return mCalls.get(callId);
79 }
80
Santos Cordon682fe6b2014-05-20 08:56:39 -070081 void clear() {
82 mCalls.clear();
83 }
84
Sailesh Nepale59bb192014-04-01 18:33:59 -070085 void checkValidCallId(String callId) {
86 // Note, no need for thread check, this method is thread safe.
87 if (!isValidCallId(callId)) {
Santos Cordondf399862014-08-06 04:39:15 -070088 // TODO: Re-enable this once we stop getting updates to
Sailesh Nepalc92c4362014-07-04 18:33:21 -070089 // ConnectionServiceWrapper for remote connections.
Santos Cordon5924bea2014-06-18 06:39:51 -070090 //throw new IllegalArgumentException(
91 // "Invalid call ID for " + mCallIdPrefix + ": " + callId);
Sailesh Nepale59bb192014-04-01 18:33:59 -070092 }
93 }
94
95 boolean isValidCallId(String callId) {
96 // Note, no need for thread check, this method is thread safe.
97 return callId != null && callId.startsWith(mCallIdPrefix);
98 }
Santos Cordona1610702014-06-04 20:22:56 -070099
100 String getNewId() {
101 sIdCount++;
102 return mCallIdPrefix + sIdCount;
103 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700104}