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(Pair.create(service.getComponentName(), 47 service.getUserHandle())); 48 } 49 } 50 }; 51 52 ConnectionServiceRepository( 53 PhoneAccountRegistrar phoneAccountRegistrar, 54 Context context, 55 TelecomSystem.SyncRoot lock, 56 CallsManager callsManager) { 57 mPhoneAccountRegistrar = phoneAccountRegistrar; 58 mContext = context; 59 mLock = lock; 60 mCallsManager = callsManager; 61 } 62 63 @VisibleForTesting 64 public ConnectionServiceWrapper getService(ComponentName componentName, UserHandle userHandle) { 65 Pair<ComponentName, UserHandle> cacheKey = Pair.create(componentName, userHandle); 66 ConnectionServiceWrapper service = mServiceCache.get(cacheKey); 67 if (service == null) { 68 service = new ConnectionServiceWrapper( 69 componentName, 70 this, 71 mPhoneAccountRegistrar, 72 mCallsManager, 73 mContext, 74 mLock, 75 userHandle); 76 service.addListener(mUnbindListener); 77 mServiceCache.put(cacheKey, service); 78 } 79 return service; 80 } 81 82 @VisibleForTesting 83 public void setService(ComponentName componentName, UserHandle userHandle, 84 ConnectionServiceWrapper service) { 85 Pair<ComponentName, UserHandle> cacheKey = Pair.create(componentName, userHandle); 86 mServiceCache.put(cacheKey, service); 87 } 88 89 /** 90 * Dumps the state of the {@link ConnectionServiceRepository}. 91 * 92 * @param pw The {@code IndentingPrintWriter} to write the state to. 93 */ 94 public void dump(IndentingPrintWriter pw) { 95 pw.println("mServiceCache:"); 96 pw.increaseIndent(); 97 for (Pair<ComponentName, UserHandle> cacheKey : mServiceCache.keySet()) { 98 ComponentName componentName = cacheKey.first; 99 pw.println(componentName); 100 } 101 pw.decreaseIndent(); 102 } 103 } 104