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 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 19 | import com.google.common.collect.HashBiMap; |
| 20 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 21 | /** Utility to map {@link Call} objects to unique IDs. IDs are generated when a call is added. */ |
| 22 | class CallIdMapper { |
| 23 | private final HashBiMap<String, Call> mCalls = HashBiMap.create(); |
| 24 | private final String mCallIdPrefix; |
Sailesh Nepal | e2ea653 | 2014-04-01 19:45:45 -0700 | [diff] [blame] | 25 | private static int sIdCount; |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 26 | |
| 27 | CallIdMapper(String callIdPrefix) { |
| 28 | ThreadUtil.checkOnMainThread(); |
| 29 | mCallIdPrefix = callIdPrefix + "@"; |
| 30 | } |
| 31 | |
Sailesh Nepal | 0e5410a | 2014-04-04 01:20:58 -0700 | [diff] [blame] | 32 | 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 Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 40 | void addCall(Call call, String id) { |
Jay Shrauner | 969755a | 2014-08-11 20:34:43 -0700 | [diff] [blame] | 41 | if (call == null) { |
| 42 | return; |
| 43 | } |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 44 | ThreadUtil.checkOnMainThread(); |
| 45 | mCalls.put(id, call); |
| 46 | } |
| 47 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 48 | void addCall(Call call) { |
| 49 | ThreadUtil.checkOnMainThread(); |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 50 | addCall(call, getNewId()); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void removeCall(Call call) { |
Jay Shrauner | 969755a | 2014-08-11 20:34:43 -0700 | [diff] [blame] | 54 | if (call == null) { |
| 55 | return; |
| 56 | } |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 57 | ThreadUtil.checkOnMainThread(); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 58 | mCalls.inverse().remove(call); |
| 59 | } |
| 60 | |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 61 | void removeCall(String callId) { |
| 62 | ThreadUtil.checkOnMainThread(); |
| 63 | mCalls.remove(callId); |
| 64 | } |
| 65 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 66 | String getCallId(Call call) { |
Jay Shrauner | 969755a | 2014-08-11 20:34:43 -0700 | [diff] [blame] | 67 | if (call == null) { |
| 68 | return null; |
| 69 | } |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 70 | ThreadUtil.checkOnMainThread(); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 71 | 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 Cordon | 0fbe632 | 2014-08-14 04:04:25 -0700 | [diff] [blame^] | 81 | if (!isValidCallId(callId) && !isValidConferenceId(callId)) { |
Jay Shrauner | 969755a | 2014-08-11 20:34:43 -0700 | [diff] [blame] | 82 | return null; |
| 83 | } |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 84 | |
| 85 | return mCalls.get(callId); |
| 86 | } |
| 87 | |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 88 | void clear() { |
| 89 | mCalls.clear(); |
| 90 | } |
| 91 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 92 | 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 Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 96 | |
Santos Cordon | 0fbe632 | 2014-08-14 04:04:25 -0700 | [diff] [blame^] | 97 | boolean isValidConferenceId(String callId) { |
| 98 | return callId != null; |
| 99 | } |
| 100 | |
Santos Cordon | a161070 | 2014-06-04 20:22:56 -0700 | [diff] [blame] | 101 | String getNewId() { |
| 102 | sIdCount++; |
| 103 | return mCallIdPrefix + sIdCount; |
| 104 | } |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 105 | } |