Skip to content

Instantly share code, notes, and snippets.

@AshuTyagi16
Created November 16, 2020 09:55
Show Gist options
  • Save AshuTyagi16/99dc764d9bd6025173f0b2ab21d71ee1 to your computer and use it in GitHub Desktop.
Save AshuTyagi16/99dc764d9bd6025173f0b2ab21d71ee1 to your computer and use it in GitHub Desktop.
public class LocationManagerService extends ILocationManager.Stub {
private Receiver getReceiverLocked(ILocationListener listener, int pid, int uid,
String packageName) {
IBinder binder = listener.asBinder();
Receiver receiver = mReceivers.get(binder);
if (receiver == null) {
receiver = new Receiver(listener, null, pid, uid, packageName);
mReceivers.put(binder, receiver);
try {
// Register to receive a notification when the process hosting
// the token goes away.
receiver.getListener().asBinder().linkToDeath(receiver, 0);
} catch (RemoteException e) {
Slog.e(TAG, "linkToDeath failed:", e);
return null;
}
}
return receiver;
}
private final class Receiver implements IBinder.DeathRecipient, PendingIntent.OnFinished {
@Override
public void binderDied() {
synchronized (mLock) {
removeUpdatesLocked(this);
}
}
private void removeUpdatesLocked(Receiver receiver) {
if (mReceivers.remove(receiver.mKey) != null && receiver.isListener()) {
// We no longer care about receiving death notifications.
receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
synchronized (receiver) {
receiver.clearPendingBroadcastsLocked();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment