1 /* 2 * Copyright (C) 2020 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 17 package com.android.server.location; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.os.IBinder; 22 import android.os.RemoteException; 23 24 import java.util.NoSuchElementException; 25 import java.util.function.Consumer; 26 27 /** 28 * Shared utilities for LocationManagerService and GnssManager. 29 */ 30 public class LocationManagerServiceUtils { 31 32 /** 33 * Listener that can be linked to a binder. 34 * @param <TListener> listener type 35 * @param <TRequest> request type 36 */ 37 public static class LinkedListener<TRequest, TListener> extends 38 LinkedListenerBase { 39 @Nullable protected final TRequest mRequest; 40 private final TListener mListener; 41 private final Consumer<TListener> mBinderDeathCallback; 42 LinkedListener( @ullable TRequest request, @NonNull TListener listener, @NonNull CallerIdentity callerIdentity, @NonNull Consumer<TListener> binderDeathCallback)43 public LinkedListener( 44 @Nullable TRequest request, 45 @NonNull TListener listener, 46 @NonNull CallerIdentity callerIdentity, 47 @NonNull Consumer<TListener> binderDeathCallback) { 48 super(callerIdentity); 49 mListener = listener; 50 mRequest = request; 51 mBinderDeathCallback = binderDeathCallback; 52 } 53 54 @Nullable getRequest()55 public TRequest getRequest() { 56 return mRequest; 57 } 58 59 @Override binderDied()60 public void binderDied() { 61 mBinderDeathCallback.accept(mListener); 62 } 63 } 64 65 /** 66 * Skeleton class of listener that can be linked to a binder. 67 */ 68 public abstract static class LinkedListenerBase implements IBinder.DeathRecipient { 69 protected final CallerIdentity mCallerIdentity; 70 LinkedListenerBase(@onNull CallerIdentity callerIdentity)71 LinkedListenerBase(@NonNull CallerIdentity callerIdentity) { 72 mCallerIdentity = callerIdentity; 73 } 74 75 @Override toString()76 public String toString() { 77 return mCallerIdentity.toString(); 78 } 79 getCallerIdentity()80 public CallerIdentity getCallerIdentity() { 81 return mCallerIdentity; 82 } 83 84 /** 85 * Link listener (i.e. callback) to a binder, so that it will be called upon binder's death. 86 */ linkToListenerDeathNotificationLocked(IBinder binder)87 public boolean linkToListenerDeathNotificationLocked(IBinder binder) { 88 try { 89 binder.linkToDeath(this, 0 /* flags */); 90 return true; 91 } catch (RemoteException e) { 92 return false; 93 } 94 } 95 96 /** 97 * Unlink death listener (i.e. callback) from binder. 98 */ unlinkFromListenerDeathNotificationLocked(IBinder binder)99 public void unlinkFromListenerDeathNotificationLocked(IBinder binder) { 100 try { 101 binder.unlinkToDeath(this, 0 /* flags */); 102 } catch (NoSuchElementException e) { 103 // ignore 104 } 105 } 106 } 107 } 108