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