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;
21
22import java.util.HashMap;
23
24/**
25 * Searches for and returns connection services.
26 */
27final class ConnectionServiceRepository
28        implements ServiceBinder.Listener<ConnectionServiceWrapper> {
29    private final HashMap<ComponentName, ConnectionServiceWrapper> mServiceCache =
30            new HashMap<ComponentName, ConnectionServiceWrapper>();
31    private final PhoneAccountRegistrar mPhoneAccountRegistrar;
32    private final Context mContext;
33
34    ConnectionServiceRepository(PhoneAccountRegistrar phoneAccountRegistrar, Context context) {
35        mPhoneAccountRegistrar = phoneAccountRegistrar;
36        mContext = context;
37    }
38
39    ConnectionServiceWrapper getService(ComponentName componentName) {
40        ConnectionServiceWrapper service = mServiceCache.get(componentName);
41        if (service == null) {
42            service = new ConnectionServiceWrapper(
43                    componentName,
44                    this,
45                    mPhoneAccountRegistrar,
46                    mContext);
47            service.addListener(this);
48            mServiceCache.put(componentName, service);
49        }
50        return service;
51    }
52
53    /**
54     * Removes the specified service from the cache when the service unbinds.
55     *
56     * {@inheritDoc}
57     */
58    @Override
59    public void onUnbind(ConnectionServiceWrapper service) {
60        mServiceCache.remove(service.getComponentName());
61    }
62}
63