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