1/* 2 * Copyright (C) 2015 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 */ 16package com.android.car.dialer.telecom; 17 18import android.support.v4.util.Pair; 19import android.util.SparseArray; 20 21import java.util.ArrayList; 22import java.util.HashMap; 23import java.util.List; 24import java.util.Map; 25 26/** 27 * Holds a list of {@link UiCall} and implements convenient mapping from underlying 28 * {@code Call} class to {@link UiCall}. 29 */ 30public abstract class UiCallList<C> { 31 private Map<C, UiCall> mCallToCallInfoMap = new HashMap<>(); 32 private SparseArray<Pair<UiCall, C>> mIdToCallMap = new SparseArray<>(); 33 34 /** 35 * Creates or gets existing {@code CallInfo} instance for a given key 36 */ 37 public UiCall getOrCreate(C call) { 38 if (call == null) { 39 return null; 40 } 41 UiCall uiCall = mCallToCallInfoMap.get(call); 42 if (uiCall == null) { 43 uiCall = createUiCall(call); 44 mCallToCallInfoMap.put(call, uiCall); 45 mIdToCallMap.append(uiCall.getId(), new Pair<>(uiCall, call)); 46 } 47 return uiCall; 48 } 49 50 /** 51 * Returns a list of existing {@code CallInfo}. 52 */ 53 public List<UiCall> getCalls() { 54 return new ArrayList<>(mCallToCallInfoMap.values()); 55 } 56 57 /** 58 * Clears a list. 59 */ 60 public void clearCalls() { 61 mCallToCallInfoMap.clear(); 62 mIdToCallMap.clear(); 63 } 64 65 /** 66 * Removes {@code CallInfo} by given key. 67 */ 68 public void remove(UiCall uiCall) { 69 int callId = uiCall.getId(); 70 C call = getCall(callId); 71 if (mCallToCallInfoMap.containsKey(call)) { 72 mCallToCallInfoMap.remove(call); 73 } 74 mIdToCallMap.remove(callId); 75 } 76 77 protected C getCall(int callId) { 78 return mIdToCallMap.get(callId).second; 79 } 80 81 public UiCall getUiCall(int callId) { 82 return mIdToCallMap.get(callId).first; 83 } 84 85 abstract protected UiCall createUiCall(C call); 86} 87