1 /* 2 * Copyright (C) 2013 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.server.telecom; 18 19 import android.content.ComponentName; 20 import android.content.Context; 21 import android.os.UserHandle; 22 import android.util.Pair; 23 24 import com.android.internal.annotations.VisibleForTesting; 25 import com.android.internal.util.IndentingPrintWriter; 26 27 import java.util.HashMap; 28 29 /** 30 * Searches for and returns connection services. 31 */ 32 @VisibleForTesting 33 public class ConnectionServiceRepository { 34 private final HashMap<Pair<ComponentName, UserHandle>, ConnectionServiceWrapper> mServiceCache = 35 new HashMap<>(); 36 private final PhoneAccountRegistrar mPhoneAccountRegistrar; 37 private final Context mContext; 38 private final TelecomSystem.SyncRoot mLock; 39 private final CallsManager mCallsManager; 40 41 private final ServiceBinder.Listener<ConnectionServiceWrapper> mUnbindListener = 42 new ServiceBinder.Listener<ConnectionServiceWrapper>() { 43 @Override 44 public void onUnbind(ConnectionServiceWrapper service) { 45 synchronized (mLock) { 46 mServiceCache.remove(service.getComponentName()); 47 } 48 } 49 }; 50 51 ConnectionServiceRepository( 52 PhoneAccountRegistrar phoneAccountRegistrar, 53 Context context, 54 TelecomSystem.SyncRoot lock, 55 CallsManager callsManager) { 56 mPhoneAccountRegistrar = phoneAccountRegistrar; 57 mContext = context; 58 mLock = lock; 59 mCallsManager = callsManager; 60 } 61 62 @VisibleForTesting 63 public ConnectionServiceWrapper getService(ComponentName componentName, UserHandle userHandle) { 64 Pair<ComponentName, UserHandle> cacheKey = Pair.create(componentName, userHandle); 65 ConnectionServiceWrapper service = mServiceCache.get(cacheKey); 66 if (service == null) { 67 service = new ConnectionServiceWrapper( 68 componentName, 69 this, 70 mPhoneAccountRegistrar, 71 mCallsManager, 72 mContext, 73 mLock, 74 userHandle); 75 service.addListener(mUnbindListener); 76 mServiceCache.put(cacheKey, service); 77 } 78 return service; 79 } 80 81 /** 82 * Dumps the state of the {@link ConnectionServiceRepository}. 83 * 84 * @param pw The {@code IndentingPrintWriter} to write the state to. 85 */ 86 public void dump(IndentingPrintWriter pw) { 87 pw.println("mServiceCache:"); 88 pw.increaseIndent(); 89 for (Pair<ComponentName, UserHandle> cacheKey : mServiceCache.keySet()) { 90 ComponentName componentName = cacheKey.first; 91 pw.println(componentName); 92 } 93 pw.decreaseIndent(); 94 } 95 } 96