/* * 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 com.android.internal.annotations.GuardedBy; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Map; /** * Helper class to hold client's binder interface. * * @param type of the value that is wrapped by this class */ public class BinderInterfaceContainer { /** * Wrapper class for objects that want to be notified whenever they are unliked from * the container ({@link BinderInterfaceContainer}). * * @param type of the value that is wrapped by this class */ 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); } } /** * Interface to be implemented by object that want to be notified whenever a binder is unliked * (dies). */ public interface BinderEventHandler { void onBinderDeath(BinderInterface bInterface); } private final Object mLock = new Object(); private final BinderEventHandler mEventHandler; @GuardedBy("mLock") private final Map> mBinders = new HashMap<>(); /** * Constructs a new BinderInterfaceContainer passing an event handler to be used to * notify listeners when a registered binder dies (unlinked). */ public BinderInterfaceContainer(@Nullable BinderEventHandler eventHandler) { mEventHandler = eventHandler; } public BinderInterfaceContainer() { mEventHandler = null; } /** * Add the instance of {@link IInterface} representing the binder interface to this container. * * Internally, this object will be wrapped in an {@link BinderInterface} when added. */ public void addBinder(T binderInterface) { IBinder binder = binderInterface.asBinder(); synchronized (mLock) { 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); } } /** * Removes the {@link BinderInterface} object associated with the passed parameter (if there is * any). */ public void removeBinder(T binderInterface) { IBinder binder = binderInterface.asBinder(); synchronized (mLock) { BinderInterface bInterface = mBinders.get(binder); if (bInterface == null) { return; } binder.unlinkToDeath(bInterface, 0); mBinders.remove(binder); } } /** * Returns the {@link BinderInterface} object associated with the passed parameter. */ public BinderInterface getBinderInterface(T binderInterface) { IBinder binder = binderInterface.asBinder(); synchronized (mLock) { return mBinders.get(binder); } } /** * Adds a new {@link BinderInterface} in this container. */ public void addBinderInterface(BinderInterface bInterface) { IBinder binder = bInterface.binderInterface.asBinder(); synchronized (mLock) { try { binder.linkToDeath(bInterface, 0); } catch (RemoteException e) { throw new IllegalArgumentException(e); } mBinders.put(binder, bInterface); } } /** * Returns a shallow copy of all registered {@link BinderInterface} objects in this container. */ public Collection> getInterfaces() { synchronized (mLock) { return new ArrayList<>(mBinders.values()); } } /** * Returns the number of registered {@link BinderInterface} objects in this container. */ public int size() { synchronized (mLock) { return mBinders.size(); } } /** * Clears all registered {@link BinderInterface} objects. */ public void clear() { synchronized (mLock) { Collection> interfaces = getInterfaces(); for (BinderInterface bInterface : interfaces) { IBinder binder = bInterface.binderInterface.asBinder(); binder.unlinkToDeath(bInterface, 0); } mBinders.clear(); } } private void handleBinderDeath(BinderInterface bInterface) { if (mEventHandler != null) { mEventHandler.onBinderDeath(bInterface); } removeBinder(bInterface.binderInterface); } }