/* * Copyright (C) 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.car; import android.annotation.Nullable; import android.os.IBinder; import android.os.IInterface; import android.os.RemoteException; import java.util.Collection; import java.util.HashMap; /** * Helper class to hold client's binder interface. */ public class BinderInterfaceContainer { public static class BinderInterface implements IBinder.DeathRecipient { public final T binderInterface; private final BinderInterfaceContainer mContainer; public BinderInterface(BinderInterfaceContainer container, T binderInterface) { mContainer = container; this.binderInterface = binderInterface; } @Override public void binderDied() { binderInterface.asBinder().unlinkToDeath(this, 0); mContainer.handleBinderDeath(this); } } public interface BinderEventHandler { void onBinderDeath(BinderInterface bInterface); } private final BinderEventHandler mEventHandler; private final HashMap> mBinders = new HashMap<>(); public BinderInterfaceContainer(@Nullable BinderEventHandler eventHandler) { mEventHandler = eventHandler; } public BinderInterfaceContainer() { mEventHandler = null; } public void addBinder(T binderInterface) { IBinder binder = binderInterface.asBinder(); synchronized (this) { BinderInterface bInterface = mBinders.get(binder); if (bInterface != null) { return; } bInterface = new BinderInterface(this, binderInterface); try { binder.linkToDeath(bInterface, 0); } catch (RemoteException e) { throw new IllegalArgumentException(e); } mBinders.put(binder, bInterface); } } public void removeBinder(T binderInterface) { IBinder binder = binderInterface.asBinder(); synchronized(this) { BinderInterface bInterface = mBinders.get(binder); if (bInterface == null) { return; } binder.unlinkToDeath(bInterface, 0); mBinders.remove(binder); } } public BinderInterface getBinderInterface(T binderInterface) { IBinder binder = binderInterface.asBinder(); synchronized (this) { return mBinders.get(binder); } } public void addBinderInterface(BinderInterface bInterface) { IBinder binder = bInterface.binderInterface.asBinder(); synchronized (this) { try { binder.linkToDeath(bInterface, 0); } catch (RemoteException e) { throw new IllegalArgumentException(e); } mBinders.put(binder, bInterface); } } public Collection> getInterfaces() { synchronized (this) { return mBinders.values(); } } public synchronized int size() { return mBinders.size(); } public synchronized void clear() { Collection> interfaces = getInterfaces(); for (BinderInterface bInterface : interfaces) { removeBinder(bInterface.binderInterface); } } private void handleBinderDeath(BinderInterface bInterface) { removeBinder(bInterface.binderInterface); if (mEventHandler != null) { mEventHandler.onBinderDeath(bInterface); } } }