Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 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 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.telecomm; |
| 18 | |
| 19 | import com.google.common.base.Preconditions; |
| 20 | import com.google.common.collect.HashBiMap; |
| 21 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 22 | /** Utility to map {@link Call} objects to unique IDs. IDs are generated when a call is added. */ |
| 23 | class CallIdMapper { |
| 24 | private final HashBiMap<String, Call> mCalls = HashBiMap.create(); |
| 25 | private final String mCallIdPrefix; |
Sailesh Nepal | e2ea653 | 2014-04-01 19:45:45 -0700 | [diff] [blame] | 26 | private static int sIdCount; |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 27 | |
| 28 | CallIdMapper(String callIdPrefix) { |
| 29 | ThreadUtil.checkOnMainThread(); |
| 30 | mCallIdPrefix = callIdPrefix + "@"; |
| 31 | } |
| 32 | |
Sailesh Nepal | 0e5410a | 2014-04-04 01:20:58 -0700 | [diff] [blame] | 33 | 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 Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 41 | void addCall(Call call) { |
| 42 | ThreadUtil.checkOnMainThread(); |
| 43 | Preconditions.checkNotNull(call); |
Sailesh Nepal | e2ea653 | 2014-04-01 19:45:45 -0700 | [diff] [blame] | 44 | sIdCount++; |
| 45 | String callId = mCallIdPrefix + sIdCount; |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 46 | 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 | |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 55 | void removeCall(String callId) { |
| 56 | ThreadUtil.checkOnMainThread(); |
| 57 | mCalls.remove(callId); |
| 58 | } |
| 59 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 60 | String getCallId(Call call) { |
| 61 | ThreadUtil.checkOnMainThread(); |
| 62 | Preconditions.checkNotNull(call); |
| 63 | return mCalls.inverse().get(call); |
| 64 | } |
| 65 | |
| 66 | Call getCall(Object objId) { |
| 67 | ThreadUtil.checkOnMainThread(); |
| 68 | |
| 69 | String callId = null; |
| 70 | if (objId instanceof String) { |
| 71 | callId = (String) objId; |
| 72 | } |
Santos Cordon | 3d3b405 | 2014-05-05 12:05:36 -0700 | [diff] [blame] | 73 | checkValidCallId(callId); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 74 | |
| 75 | return mCalls.get(callId); |
| 76 | } |
| 77 | |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 78 | void clear() { |
| 79 | mCalls.clear(); |
| 80 | } |
| 81 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 82 | void checkValidCallId(String callId) { |
| 83 | // Note, no need for thread check, this method is thread safe. |
| 84 | if (!isValidCallId(callId)) { |
Santos Cordon | 3d3b405 | 2014-05-05 12:05:36 -0700 | [diff] [blame] | 85 | throw new IllegalArgumentException( |
| 86 | "Invalid call ID for " + mCallIdPrefix + ": " + callId); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
| 90 | boolean isValidCallId(String callId) { |
| 91 | // Note, no need for thread check, this method is thread safe. |
| 92 | return callId != null && callId.startsWith(mCallIdPrefix); |
| 93 | } |
| 94 | } |