blob: a5d70bfde977d12d546da7ea8a1233992d14d7e8 [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
Sailesh Nepale59bb192014-04-01 18:33:59 -070041 void addCall(Call call) {
42 ThreadUtil.checkOnMainThread();
43 Preconditions.checkNotNull(call);
Sailesh Nepale2ea6532014-04-01 19:45:45 -070044 sIdCount++;
45 String callId = mCallIdPrefix + sIdCount;
Sailesh Nepale59bb192014-04-01 18:33:59 -070046 mCalls.put(callId, call);
47 }
48
49 void removeCall(Call call) {
50 ThreadUtil.checkOnMainThread();
51 Preconditions.checkNotNull(call);
52 mCalls.inverse().remove(call);
53 }
54
55 String getCallId(Call call) {
56 ThreadUtil.checkOnMainThread();
57 Preconditions.checkNotNull(call);
58 return mCalls.inverse().get(call);
59 }
60
61 Call getCall(Object objId) {
62 ThreadUtil.checkOnMainThread();
63
64 String callId = null;
65 if (objId instanceof String) {
66 callId = (String) objId;
67 }
68 Preconditions.checkArgument(isValidCallId(callId));
69
70 return mCalls.get(callId);
71 }
72
73 void checkValidCallId(String callId) {
74 // Note, no need for thread check, this method is thread safe.
75 if (!isValidCallId(callId)) {
76 Log.wtf(this, "%s is not a valid call ID", callId);
77 throw new IllegalArgumentException("Invalid call ID.");
78 }
79 }
80
81 boolean isValidCallId(String callId) {
82 // Note, no need for thread check, this method is thread safe.
83 return callId != null && callId.startsWith(mCallIdPrefix);
84 }
85}