blob: bf567066891fd5c3bf9dbcfa8469479f49d08562 [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 Parke54ac272016-02-16 19:02:18 -080060 public void addBinder(T binderInterface) {
keunyoung6b197692015-11-16 13:54:38 -080061 IBinder binder = binderInterface.asBinder();
62 synchronized (this) {
keunyounge4c90c42015-11-16 18:42:52 -080063 BinderInterface<T> bInterface = mBinders.get(binder);
keunyoung6b197692015-11-16 13:54:38 -080064 if (bInterface != null) {
65 return;
66 }
Keun-young Parke54ac272016-02-16 19:02:18 -080067 bInterface = new BinderInterface<T>(this, binderInterface);
keunyoung6b197692015-11-16 13:54:38 -080068 try {
69 binder.linkToDeath(bInterface, 0);
70 } catch (RemoteException e) {
71 throw new IllegalArgumentException(e);
72 }
73 mBinders.put(binder, bInterface);
74 }
75 }
76
77 public void removeBinder(T binderInterface) {
78 IBinder binder = binderInterface.asBinder();
79 synchronized(this) {
keunyounge4c90c42015-11-16 18:42:52 -080080 BinderInterface<T> bInterface = mBinders.get(binder);
keunyoung6b197692015-11-16 13:54:38 -080081 if (bInterface != null) {
82 return;
83 }
84 binder.unlinkToDeath(bInterface, 0);
85 mBinders.remove(binder);
86 }
87 }
88
keunyounge4c90c42015-11-16 18:42:52 -080089 public BinderInterface<T> getBinderInterface(T binderInterface) {
90 IBinder binder = binderInterface.asBinder();
91 synchronized (this) {
92 return mBinders.get(binder);
93 }
94 }
95
96 public void addBinderInterface(BinderInterface<T> bInterface) {
97 IBinder binder = bInterface.binderInterface.asBinder();
98 synchronized (this) {
99 try {
100 binder.linkToDeath(bInterface, 0);
101 } catch (RemoteException e) {
102 throw new IllegalArgumentException(e);
103 }
104 mBinders.put(binder, bInterface);
105 }
106 }
107
keunyoung6b197692015-11-16 13:54:38 -0800108 public Collection<BinderInterface<T>> getInterfaces() {
109 synchronized (this) {
110 return mBinders.values();
111 }
112 }
113
keunyounge4c90c42015-11-16 18:42:52 -0800114 public synchronized void clear() {
keunyoung6b197692015-11-16 13:54:38 -0800115 Collection<BinderInterface<T>> interfaces = getInterfaces();
116 for (BinderInterface<T> bInterface : interfaces) {
117 removeBinder(bInterface.binderInterface);
118 }
119 }
120
121 private void handleBinderDeath(BinderInterface<T> bInterface) {
122 removeBinder(bInterface.binderInterface);
123 if (mEventHandler != null) {
124 mEventHandler.onBinderDeath(bInterface);
125 }
126 }
127}