blob: d34ea3c075e73efb375b5743c3f47b0f2384e80f [file] [log] [blame]
Sailesh Nepal905dfba2014-07-14 08:20:41 -07001/*
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 Gunn7cc70b42014-09-12 22:17:27 -070017package com.android.server.telecom;
Sailesh Nepal905dfba2014-07-14 08:20:41 -070018
19import android.content.ComponentName;
Tyler Gunn91d43cf2014-09-17 12:19:39 -070020import android.content.Context;
Evan Charlton105d9772014-11-25 14:08:53 -080021import android.os.UserHandle;
22import android.util.Pair;
Sailesh Nepal905dfba2014-07-14 08:20:41 -070023
Brad Ebingerf1900072015-11-12 17:25:06 -080024import com.android.internal.annotations.VisibleForTesting;
Tyler Gunn9787e0e2014-10-14 14:36:12 -070025import com.android.internal.util.IndentingPrintWriter;
26
Sailesh Nepal905dfba2014-07-14 08:20:41 -070027import java.util.HashMap;
28
29/**
30 * Searches for and returns connection services.
31 */
Brad Ebingerf1900072015-11-12 17:25:06 -080032@VisibleForTesting
33public class ConnectionServiceRepository {
Evan Charlton105d9772014-11-25 14:08:53 -080034 private final HashMap<Pair<ComponentName, UserHandle>, ConnectionServiceWrapper> mServiceCache =
35 new HashMap<>();
Tyler Gunn91d43cf2014-09-17 12:19:39 -070036 private final PhoneAccountRegistrar mPhoneAccountRegistrar;
37 private final Context mContext;
Ihab Awad8d5d9dd2015-03-12 11:11:06 -070038 private final TelecomSystem.SyncRoot mLock;
Ihab Awad8de76912015-02-17 12:25:52 -080039 private final CallsManager mCallsManager;
Sailesh Nepal905dfba2014-07-14 08:20:41 -070040
Ihab Awad78a5e6b2015-02-06 10:13:05 -080041 private final ServiceBinder.Listener<ConnectionServiceWrapper> mUnbindListener =
42 new ServiceBinder.Listener<ConnectionServiceWrapper>() {
43 @Override
44 public void onUnbind(ConnectionServiceWrapper service) {
Ihab Awad8d5d9dd2015-03-12 11:11:06 -070045 synchronized (mLock) {
Andreas Gampe98c15772018-03-01 13:58:57 -080046 mServiceCache.remove(Pair.create(service.getComponentName(),
47 service.getUserHandle()));
Ihab Awad8d5d9dd2015-03-12 11:11:06 -070048 }
Ihab Awad78a5e6b2015-02-06 10:13:05 -080049 }
50 };
51
Ihab Awad8de76912015-02-17 12:25:52 -080052 ConnectionServiceRepository(
53 PhoneAccountRegistrar phoneAccountRegistrar,
54 Context context,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -070055 TelecomSystem.SyncRoot lock,
Ihab Awad8de76912015-02-17 12:25:52 -080056 CallsManager callsManager) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -070057 mPhoneAccountRegistrar = phoneAccountRegistrar;
58 mContext = context;
Ihab Awad8d5d9dd2015-03-12 11:11:06 -070059 mLock = lock;
Ihab Awad8de76912015-02-17 12:25:52 -080060 mCallsManager = callsManager;
Sailesh Nepal905dfba2014-07-14 08:20:41 -070061 }
62
Brad Ebingerf1900072015-11-12 17:25:06 -080063 @VisibleForTesting
64 public ConnectionServiceWrapper getService(ComponentName componentName, UserHandle userHandle) {
Evan Charlton105d9772014-11-25 14:08:53 -080065 Pair<ComponentName, UserHandle> cacheKey = Pair.create(componentName, userHandle);
66 ConnectionServiceWrapper service = mServiceCache.get(cacheKey);
Sailesh Nepal905dfba2014-07-14 08:20:41 -070067 if (service == null) {
Ihab Awadb78b2762014-07-25 15:16:23 -070068 service = new ConnectionServiceWrapper(
69 componentName,
70 this,
Tyler Gunn91d43cf2014-09-17 12:19:39 -070071 mPhoneAccountRegistrar,
Ihab Awad8de76912015-02-17 12:25:52 -080072 mCallsManager,
Evan Charlton105d9772014-11-25 14:08:53 -080073 mContext,
Ihab Awad8d5d9dd2015-03-12 11:11:06 -070074 mLock,
Evan Charlton105d9772014-11-25 14:08:53 -080075 userHandle);
Ihab Awad78a5e6b2015-02-06 10:13:05 -080076 service.addListener(mUnbindListener);
Evan Charlton105d9772014-11-25 14:08:53 -080077 mServiceCache.put(cacheKey, service);
Sailesh Nepal905dfba2014-07-14 08:20:41 -070078 }
79 return service;
80 }
81
82 /**
Tyler Gunn9787e0e2014-10-14 14:36:12 -070083 * 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 Charlton105d9772014-11-25 14:08:53 -080090 for (Pair<ComponentName, UserHandle> cacheKey : mServiceCache.keySet()) {
91 ComponentName componentName = cacheKey.first;
Tyler Gunn9787e0e2014-10-14 14:36:12 -070092 pw.println(componentName);
93 }
94 pw.decreaseIndent();
95 }
Sailesh Nepal905dfba2014-07-14 08:20:41 -070096}