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 | |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 41 | void addCall(Call call, String id) { |
| 42 | Preconditions.checkNotNull(call); |
| 43 | ThreadUtil.checkOnMainThread(); |
| 44 | mCalls.put(id, call); |
| 45 | } |
| 46 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 47 | void addCall(Call call) { |
| 48 | ThreadUtil.checkOnMainThread(); |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 49 | addCall(call, getNewId()); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void removeCall(Call call) { |
| 53 | ThreadUtil.checkOnMainThread(); |
| 54 | Preconditions.checkNotNull(call); |
| 55 | mCalls.inverse().remove(call); |
| 56 | } |
| 57 | |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 58 | void removeCall(String callId) { |
| 59 | ThreadUtil.checkOnMainThread(); |
| 60 | mCalls.remove(callId); |
| 61 | } |
| 62 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 63 | 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 Cordon | 3d3b405 | 2014-05-05 12:05:36 -0700 | [diff] [blame] | 76 | checkValidCallId(callId); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 77 | |
| 78 | return mCalls.get(callId); |
| 79 | } |
| 80 | |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 81 | void clear() { |
| 82 | mCalls.clear(); |
| 83 | } |
| 84 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 85 | void checkValidCallId(String callId) { |
| 86 | // Note, no need for thread check, this method is thread safe. |
| 87 | if (!isValidCallId(callId)) { |
Santos Cordon | df39986 | 2014-08-06 04:39:15 -0700 | [diff] [blame^] | 88 | // TODO: Re-enable this once we stop getting updates to |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 89 | // ConnectionServiceWrapper for remote connections. |
Santos Cordon | 5924bea | 2014-06-18 06:39:51 -0700 | [diff] [blame] | 90 | //throw new IllegalArgumentException( |
| 91 | // "Invalid call ID for " + mCallIdPrefix + ": " + callId); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 92 | } |
| 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 Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 99 | |
| 100 | String getNewId() { |
| 101 | sIdCount++; |
| 102 | return mCallIdPrefix + sIdCount; |
| 103 | } |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 104 | } |