blob: b82d1e1a1e85498766fd05df9a273df3c68ebff4 [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
Sailesh Nepale59bb192014-04-01 18:33:59 -070019import com.google.common.collect.HashBiMap;
20
Sailesh Nepale59bb192014-04-01 18:33:59 -070021/** Utility to map {@link Call} objects to unique IDs. IDs are generated when a call is added. */
22class CallIdMapper {
23 private final HashBiMap<String, Call> mCalls = HashBiMap.create();
24 private final String mCallIdPrefix;
Sailesh Nepale2ea6532014-04-01 19:45:45 -070025 private static int sIdCount;
Sailesh Nepale59bb192014-04-01 18:33:59 -070026
27 CallIdMapper(String callIdPrefix) {
28 ThreadUtil.checkOnMainThread();
29 mCallIdPrefix = callIdPrefix + "@";
30 }
31
Sailesh Nepal0e5410a2014-04-04 01:20:58 -070032 void replaceCall(Call newCall, Call callToReplace) {
33 ThreadUtil.checkOnMainThread();
34
35 // Use the old call's ID for the new call.
36 String callId = getCallId(callToReplace);
37 mCalls.put(callId, newCall);
38 }
39
Santos Cordona1610702014-06-04 20:22:56 -070040 void addCall(Call call, String id) {
Jay Shrauner969755a2014-08-11 20:34:43 -070041 if (call == null) {
42 return;
43 }
Santos Cordona1610702014-06-04 20:22:56 -070044 ThreadUtil.checkOnMainThread();
45 mCalls.put(id, call);
46 }
47
Sailesh Nepale59bb192014-04-01 18:33:59 -070048 void addCall(Call call) {
49 ThreadUtil.checkOnMainThread();
Santos Cordona1610702014-06-04 20:22:56 -070050 addCall(call, getNewId());
Sailesh Nepale59bb192014-04-01 18:33:59 -070051 }
52
53 void removeCall(Call call) {
Jay Shrauner969755a2014-08-11 20:34:43 -070054 if (call == null) {
55 return;
56 }
Sailesh Nepale59bb192014-04-01 18:33:59 -070057 ThreadUtil.checkOnMainThread();
Sailesh Nepale59bb192014-04-01 18:33:59 -070058 mCalls.inverse().remove(call);
59 }
60
Santos Cordon682fe6b2014-05-20 08:56:39 -070061 void removeCall(String callId) {
62 ThreadUtil.checkOnMainThread();
63 mCalls.remove(callId);
64 }
65
Sailesh Nepale59bb192014-04-01 18:33:59 -070066 String getCallId(Call call) {
Jay Shrauner969755a2014-08-11 20:34:43 -070067 if (call == null) {
68 return null;
69 }
Sailesh Nepale59bb192014-04-01 18:33:59 -070070 ThreadUtil.checkOnMainThread();
Sailesh Nepale59bb192014-04-01 18:33:59 -070071 return mCalls.inverse().get(call);
72 }
73
74 Call getCall(Object objId) {
75 ThreadUtil.checkOnMainThread();
76
77 String callId = null;
78 if (objId instanceof String) {
79 callId = (String) objId;
80 }
Santos Cordon0fbe6322014-08-14 04:04:25 -070081 if (!isValidCallId(callId) && !isValidConferenceId(callId)) {
Jay Shrauner969755a2014-08-11 20:34:43 -070082 return null;
83 }
Sailesh Nepale59bb192014-04-01 18:33:59 -070084
85 return mCalls.get(callId);
86 }
87
Santos Cordon682fe6b2014-05-20 08:56:39 -070088 void clear() {
89 mCalls.clear();
90 }
91
Sailesh Nepale59bb192014-04-01 18:33:59 -070092 boolean isValidCallId(String callId) {
93 // Note, no need for thread check, this method is thread safe.
94 return callId != null && callId.startsWith(mCallIdPrefix);
95 }
Santos Cordona1610702014-06-04 20:22:56 -070096
Santos Cordon0fbe6322014-08-14 04:04:25 -070097 boolean isValidConferenceId(String callId) {
98 return callId != null;
99 }
100
Santos Cordona1610702014-06-04 20:22:56 -0700101 String getNewId() {
102 sIdCount++;
103 return mCallIdPrefix + sIdCount;
104 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700105}