blob: a03b633cca70de74a4b394206f22da9948838007 [file] [log] [blame]
keunyoung6b197692015-11-16 13:54:38 -08001/*
2 * Copyright (C) 2015 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.car;
18
19import android.annotation.Nullable;
20import android.os.IBinder;
21import android.os.IInterface;
22import android.os.RemoteException;
23
24import java.util.Collection;
25import java.util.HashMap;
26
27/**
28 * Helper class to hold client's binder interface.
29 */
30public class BinderInterfaceContainer<T extends IInterface> {
31
32 public static class BinderInterface<T extends IInterface>
33 implements IBinder.DeathRecipient {
keunyounge4c90c42015-11-16 18:42:52 -080034 public final T binderInterface;
keunyoung6b197692015-11-16 13:54:38 -080035 private final BinderInterfaceContainer<T> mContainer;
36
Keun-young Parke54ac272016-02-16 19:02:18 -080037 public BinderInterface(BinderInterfaceContainer<T> container, T binderInterface) {
keunyoung6b197692015-11-16 13:54:38 -080038 mContainer = container;
keunyounge4c90c42015-11-16 18:42:52 -080039 this.binderInterface = binderInterface;
keunyoung6b197692015-11-16 13:54:38 -080040 }
41
42 @Override
43 public void binderDied() {
44 binderInterface.asBinder().unlinkToDeath(this, 0);
keunyounge4c90c42015-11-16 18:42:52 -080045 mContainer.handleBinderDeath(this);
keunyoung6b197692015-11-16 13:54:38 -080046 }
47 }
48
49 public interface BinderEventHandler<T extends IInterface> {
50 void onBinderDeath(BinderInterface<T> bInterface);
51 }
52
53 private final BinderEventHandler<T> mEventHandler;
54 private final HashMap<IBinder, BinderInterface<T>> mBinders = new HashMap<>();
55
56 public BinderInterfaceContainer(@Nullable BinderEventHandler<T> eventHandler) {
57 mEventHandler = eventHandler;
58 }
59
Keun-young Parkfe1a8f12017-01-17 20:06:34 -080060 public BinderInterfaceContainer() {
61 mEventHandler = null;
62 }
63
Keun-young Parke54ac272016-02-16 19:02:18 -080064 public void addBinder(T binderInterface) {
keunyoung6b197692015-11-16 13:54:38 -080065 IBinder binder = binderInterface.asBinder();
66 synchronized (this) {
keunyounge4c90c42015-11-16 18:42:52 -080067 BinderInterface<T> bInterface = mBinders.get(binder);
keunyoung6b197692015-11-16 13:54:38 -080068 if (bInterface != null) {
69 return;
70 }
Keun-young Parke54ac272016-02-16 19:02:18 -080071 bInterface = new BinderInterface<T>(this, binderInterface);
keunyoung6b197692015-11-16 13:54:38 -080072 try {
73 binder.linkToDeath(bInterface, 0);
74 } catch (RemoteException e) {
75 throw new IllegalArgumentException(e);
76 }
77 mBinders.put(binder, bInterface);
78 }
79 }
80
81 public void removeBinder(T binderInterface) {
82 IBinder binder = binderInterface.asBinder();
83 synchronized(this) {
keunyounge4c90c42015-11-16 18:42:52 -080084 BinderInterface<T> bInterface = mBinders.get(binder);
Keun-young Parkc26f6be2016-10-11 19:59:08 -070085 if (bInterface == null) {
keunyoung6b197692015-11-16 13:54:38 -080086 return;
87 }
88 binder.unlinkToDeath(bInterface, 0);
89 mBinders.remove(binder);
90 }
91 }
92
keunyounge4c90c42015-11-16 18:42:52 -080093 public BinderInterface<T> getBinderInterface(T binderInterface) {
94 IBinder binder = binderInterface.asBinder();
95 synchronized (this) {
96 return mBinders.get(binder);
97 }
98 }
99
100 public void addBinderInterface(BinderInterface<T> bInterface) {
101 IBinder binder = bInterface.binderInterface.asBinder();
102 synchronized (this) {
103 try {
104 binder.linkToDeath(bInterface, 0);
105 } catch (RemoteException e) {
106 throw new IllegalArgumentException(e);
107 }
108 mBinders.put(binder, bInterface);
109 }
110 }
111
keunyoung6b197692015-11-16 13:54:38 -0800112 public Collection<BinderInterface<T>> getInterfaces() {
113 synchronized (this) {
114 return mBinders.values();
115 }
116 }
117
Keun-young Parkfe1a8f12017-01-17 20:06:34 -0800118 public synchronized int size() {
119 return mBinders.size();
120 }
121
keunyounge4c90c42015-11-16 18:42:52 -0800122 public synchronized void clear() {
keunyoung6b197692015-11-16 13:54:38 -0800123 Collection<BinderInterface<T>> interfaces = getInterfaces();
124 for (BinderInterface<T> bInterface : interfaces) {
125 removeBinder(bInterface.binderInterface);
126 }
127 }
128
129 private void handleBinderDeath(BinderInterface<T> bInterface) {
130 removeBinder(bInterface.binderInterface);
131 if (mEventHandler != null) {
132 mEventHandler.onBinderDeath(bInterface);
133 }
134 }
135}