Sailesh Nepal | 905dfba | 2014-07-14 08:20:41 -0700 | [diff] [blame] | 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.telecomm; |
| 18 | |
| 19 | import android.content.ComponentName; |
| 20 | import android.content.Intent; |
| 21 | import android.content.pm.PackageManager; |
| 22 | import android.content.pm.ResolveInfo; |
| 23 | import android.content.pm.ServiceInfo; |
Santos Cordon | f2f14ef | 2014-07-20 18:00:20 -0700 | [diff] [blame] | 24 | import android.telecomm.ConnectionService; |
Sailesh Nepal | 905dfba | 2014-07-14 08:20:41 -0700 | [diff] [blame] | 25 | |
| 26 | import java.util.ArrayList; |
| 27 | import java.util.Collection; |
| 28 | import java.util.HashMap; |
| 29 | |
| 30 | /** |
| 31 | * Searches for and returns connection services. |
| 32 | */ |
| 33 | final class ConnectionServiceRepository |
| 34 | implements ServiceBinder.Listener<ConnectionServiceWrapper> { |
Sailesh Nepal | 905dfba | 2014-07-14 08:20:41 -0700 | [diff] [blame] | 35 | private final HashMap<ComponentName, ConnectionServiceWrapper> mServiceCache = |
| 36 | new HashMap<ComponentName, ConnectionServiceWrapper>(); |
| 37 | |
Sailesh Nepal | 664837f | 2014-07-14 16:31:51 -0700 | [diff] [blame] | 38 | ConnectionServiceRepository() { |
Sailesh Nepal | 905dfba | 2014-07-14 08:20:41 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Sailesh Nepal | 905dfba | 2014-07-14 08:20:41 -0700 | [diff] [blame] | 41 | ConnectionServiceWrapper getService(ComponentName componentName) { |
| 42 | ConnectionServiceWrapper service = mServiceCache.get(componentName); |
| 43 | if (service == null) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 44 | service = new ConnectionServiceWrapper( |
| 45 | componentName, |
| 46 | this, |
| 47 | TelecommApp.getInstance().getPhoneAccountRegistrar()); |
Sailesh Nepal | 905dfba | 2014-07-14 08:20:41 -0700 | [diff] [blame] | 48 | service.addListener(this); |
| 49 | mServiceCache.put(componentName, service); |
| 50 | } |
| 51 | return service; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Removes the specified service from the cache when the service unbinds. |
| 56 | * |
| 57 | * {@inheritDoc} |
| 58 | */ |
| 59 | @Override |
| 60 | public void onUnbind(ConnectionServiceWrapper service) { |
| 61 | mServiceCache.remove(service.getComponentName()); |
| 62 | } |
| 63 | } |