• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
ConnectionServiceRepository( PhoneAccountRegistrar phoneAccountRegistrar, Context context, TelecomSystem.SyncRoot lock, CallsManager callsManager)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
getService(ComponentName componentName, UserHandle userHandle)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     /**
83      * Dumps the state of the {@link ConnectionServiceRepository}.
84      *
85      * @param pw The {@code IndentingPrintWriter} to write the state to.
86      */
dump(IndentingPrintWriter pw)87     public void dump(IndentingPrintWriter pw) {
88         pw.println("mServiceCache:");
89         pw.increaseIndent();
90         for (Pair<ComponentName, UserHandle> cacheKey : mServiceCache.keySet()) {
91             ComponentName componentName = cacheKey.first;
92             pw.println(componentName);
93         }
94         pw.decreaseIndent();
95     }
96 }
97