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 | |
| 33 | void addCall(Call call) { |
| 34 | ThreadUtil.checkOnMainThread(); |
| 35 | Preconditions.checkNotNull(call); |
Sailesh Nepal | e2ea653 | 2014-04-01 19:45:45 -0700 | [diff] [blame] | 36 | sIdCount++; |
| 37 | String callId = mCallIdPrefix + sIdCount; |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 38 | mCalls.put(callId, call); |
| 39 | } |
| 40 | |
| 41 | void removeCall(Call call) { |
| 42 | ThreadUtil.checkOnMainThread(); |
| 43 | Preconditions.checkNotNull(call); |
| 44 | mCalls.inverse().remove(call); |
| 45 | } |
| 46 | |
| 47 | String getCallId(Call call) { |
| 48 | ThreadUtil.checkOnMainThread(); |
| 49 | Preconditions.checkNotNull(call); |
| 50 | return mCalls.inverse().get(call); |
| 51 | } |
| 52 | |
| 53 | Call getCall(Object objId) { |
| 54 | ThreadUtil.checkOnMainThread(); |
| 55 | |
| 56 | String callId = null; |
| 57 | if (objId instanceof String) { |
| 58 | callId = (String) objId; |
| 59 | } |
| 60 | Preconditions.checkArgument(isValidCallId(callId)); |
| 61 | |
| 62 | return mCalls.get(callId); |
| 63 | } |
| 64 | |
| 65 | void checkValidCallId(String callId) { |
| 66 | // Note, no need for thread check, this method is thread safe. |
| 67 | if (!isValidCallId(callId)) { |
| 68 | Log.wtf(this, "%s is not a valid call ID", callId); |
| 69 | throw new IllegalArgumentException("Invalid call ID."); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | boolean isValidCallId(String callId) { |
| 74 | // Note, no need for thread check, this method is thread safe. |
| 75 | return callId != null && callId.startsWith(mCallIdPrefix); |
| 76 | } |
| 77 | } |