TokenWatcherpublic abstract class TokenWatcher extends Object Helper class that helps you use IBinder objects as reference counted
tokens. IBinders make good tokens because we find out when they are
removed |
Fields Summary |
---|
private Runnable | mNotificationTask | private WeakHashMap | mTokens | private Handler | mHandler | private String | mTag | private int | mNotificationQueue | private volatile boolean | mAcquired |
Constructors Summary |
---|
public TokenWatcher(Handler h, String tag)Construct the TokenWatcher
mHandler = h;
mTag = tag != null ? tag : "TokenWatcher";
|
Methods Summary |
---|
public void | acquire(IBinder token, java.lang.String tag)Record that this token has been acquired. When acquire is called, and
the current count is 0, the acquired method is called on the given
handler.
synchronized (mTokens) {
// explicitly checked to avoid bogus sendNotification calls because
// of the WeakHashMap and the GC
int oldSize = mTokens.size();
Death d = new Death(token, tag);
try {
token.linkToDeath(d, 0);
} catch (RemoteException e) {
return;
}
mTokens.put(token, d);
if (oldSize == 0 && !mAcquired) {
sendNotificationLocked(true);
mAcquired = true;
}
}
| public abstract void | acquired()Called when the number of active tokens goes from 0 to 1.
| public void | cleanup(IBinder token, boolean unlink)
synchronized (mTokens) {
Death d = mTokens.remove(token);
if (unlink && d != null) {
d.token.unlinkToDeath(d, 0);
d.token = null;
}
if (mTokens.size() == 0 && mAcquired) {
sendNotificationLocked(false);
mAcquired = false;
}
}
| public void | dump()
synchronized (mTokens) {
Set<IBinder> keys = mTokens.keySet();
Log.i(mTag, "Token count: " + mTokens.size());
int i = 0;
for (IBinder b: keys) {
Log.i(mTag, "[" + i + "] " + mTokens.get(b).tag + " - " + b);
i++;
}
}
| public boolean | isAcquired()
synchronized (mTokens) {
return mAcquired;
}
| public void | release(IBinder token)
cleanup(token, true);
| public abstract void | released()Called when the number of active tokens goes from 1 to 0.
| private void | sendNotificationLocked(boolean on)
int value = on ? 1 : 0;
if (mNotificationQueue == -1) {
// empty
mNotificationQueue = value;
mHandler.post(mNotificationTask);
}
else if (mNotificationQueue != value) {
// it's a pair, so cancel it
mNotificationQueue = -1;
mHandler.removeCallbacks(mNotificationTask);
}
// else, same so do nothing -- maybe we should warn?
|
|