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 | |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 17 | package com.android.server.telecom; |
Sailesh Nepal | 905dfba | 2014-07-14 08:20:41 -0700 | [diff] [blame] | 18 | |
| 19 | import android.content.ComponentName; |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 20 | import android.content.Context; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 21 | import android.os.UserHandle; |
| 22 | import android.util.Pair; |
Sailesh Nepal | 905dfba | 2014-07-14 08:20:41 -0700 | [diff] [blame] | 23 | |
Brad Ebinger | f190007 | 2015-11-12 17:25:06 -0800 | [diff] [blame] | 24 | import com.android.internal.annotations.VisibleForTesting; |
Tyler Gunn | 9787e0e | 2014-10-14 14:36:12 -0700 | [diff] [blame] | 25 | import com.android.internal.util.IndentingPrintWriter; |
| 26 | |
Sailesh Nepal | 905dfba | 2014-07-14 08:20:41 -0700 | [diff] [blame] | 27 | import java.util.HashMap; |
| 28 | |
| 29 | /** |
| 30 | * Searches for and returns connection services. |
| 31 | */ |
Brad Ebinger | f190007 | 2015-11-12 17:25:06 -0800 | [diff] [blame] | 32 | @VisibleForTesting |
| 33 | public class ConnectionServiceRepository { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 34 | private final HashMap<Pair<ComponentName, UserHandle>, ConnectionServiceWrapper> mServiceCache = |
| 35 | new HashMap<>(); |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 36 | private final PhoneAccountRegistrar mPhoneAccountRegistrar; |
| 37 | private final Context mContext; |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 38 | private final TelecomSystem.SyncRoot mLock; |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 39 | private final CallsManager mCallsManager; |
Sailesh Nepal | 905dfba | 2014-07-14 08:20:41 -0700 | [diff] [blame] | 40 | |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 41 | private final ServiceBinder.Listener<ConnectionServiceWrapper> mUnbindListener = |
| 42 | new ServiceBinder.Listener<ConnectionServiceWrapper>() { |
| 43 | @Override |
| 44 | public void onUnbind(ConnectionServiceWrapper service) { |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 45 | synchronized (mLock) { |
Andreas Gampe | 98c1577 | 2018-03-01 13:58:57 -0800 | [diff] [blame] | 46 | mServiceCache.remove(Pair.create(service.getComponentName(), |
| 47 | service.getUserHandle())); |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 48 | } |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 49 | } |
| 50 | }; |
| 51 | |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 52 | ConnectionServiceRepository( |
| 53 | PhoneAccountRegistrar phoneAccountRegistrar, |
| 54 | Context context, |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 55 | TelecomSystem.SyncRoot lock, |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 56 | CallsManager callsManager) { |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 57 | mPhoneAccountRegistrar = phoneAccountRegistrar; |
| 58 | mContext = context; |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 59 | mLock = lock; |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 60 | mCallsManager = callsManager; |
Sailesh Nepal | 905dfba | 2014-07-14 08:20:41 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Brad Ebinger | f190007 | 2015-11-12 17:25:06 -0800 | [diff] [blame] | 63 | @VisibleForTesting |
| 64 | public ConnectionServiceWrapper getService(ComponentName componentName, UserHandle userHandle) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 65 | Pair<ComponentName, UserHandle> cacheKey = Pair.create(componentName, userHandle); |
| 66 | ConnectionServiceWrapper service = mServiceCache.get(cacheKey); |
Sailesh Nepal | 905dfba | 2014-07-14 08:20:41 -0700 | [diff] [blame] | 67 | if (service == null) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 68 | service = new ConnectionServiceWrapper( |
| 69 | componentName, |
| 70 | this, |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 71 | mPhoneAccountRegistrar, |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 72 | mCallsManager, |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 73 | mContext, |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 74 | mLock, |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 75 | userHandle); |
Ihab Awad | 78a5e6b | 2015-02-06 10:13:05 -0800 | [diff] [blame] | 76 | service.addListener(mUnbindListener); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 77 | mServiceCache.put(cacheKey, service); |
Sailesh Nepal | 905dfba | 2014-07-14 08:20:41 -0700 | [diff] [blame] | 78 | } |
| 79 | return service; |
| 80 | } |
| 81 | |
| 82 | /** |
Tyler Gunn | 9787e0e | 2014-10-14 14:36:12 -0700 | [diff] [blame] | 83 | * Dumps the state of the {@link ConnectionServiceRepository}. |
| 84 | * |
| 85 | * @param pw The {@code IndentingPrintWriter} to write the state to. |
| 86 | */ |
| 87 | public void dump(IndentingPrintWriter pw) { |
| 88 | pw.println("mServiceCache:"); |
| 89 | pw.increaseIndent(); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 90 | for (Pair<ComponentName, UserHandle> cacheKey : mServiceCache.keySet()) { |
| 91 | ComponentName componentName = cacheKey.first; |
Tyler Gunn | 9787e0e | 2014-10-14 14:36:12 -0700 | [diff] [blame] | 92 | pw.println(componentName); |
| 93 | } |
| 94 | pw.decreaseIndent(); |
| 95 | } |
Sailesh Nepal | 905dfba | 2014-07-14 08:20:41 -0700 | [diff] [blame] | 96 | } |